| |
| |
| |
| |
| import { charIndexForByteLimit } from "./semanticUtils"; |
|
|
| let passed = 0; |
| let failed = 0; |
|
|
| function assert(desc: string, actual: number, expected: number) { |
| if (actual === expected) { |
| console.log(` β ${desc}`); |
| passed++; |
| } else { |
| console.error(` β ${desc} β expected ${expected}, got ${actual}`); |
| failed++; |
| } |
| } |
|
|
| const enc = new TextEncoder(); |
| function bytes(s: string) { return enc.encode(s).byteLength; } |
|
|
| |
| console.log("1. εΊζ¬θΎΉη"); |
| assert("η©Ίε符串οΌlimit=0", charIndexForByteLimit("", 0, 0), 0); |
| assert("η©Ίε符串οΌlimit=10", charIndexForByteLimit("", 0, 10), 0); |
| assert("limit=0 ζΆη«ε»εζ’", charIndexForByteLimit("abc", 0, 0), 0); |
| assert("limit ζ°ε₯½ηδΊε
¨ζεθζ°", charIndexForByteLimit("abc", 0, 3), 3); |
| assert("limit ε€§δΊε
¨ζεθζ°", charIndexForByteLimit("abc", 0, 100), 3); |
|
|
| |
| console.log("2. ηΊ― ASCII"); |
| assert("limit=1 ε 1 ε符", charIndexForByteLimit("hello", 0, 1), 1); |
| assert("limit=3 ε 3 ε符", charIndexForByteLimit("hello", 0, 3), 3); |
| assert("limit=5 εε
¨ι¨", charIndexForByteLimit("hello", 0, 5), 5); |
| assert("start=2οΌlimit=2", charIndexForByteLimit("hello", 2, 2), 4); |
|
|
| |
| console.log("3. CJK ε符οΌ3 εθ/εοΌ"); |
| const cjk = "δ½ ε₯½δΈη"; |
| assert("limit=3 β 1 ε符", charIndexForByteLimit(cjk, 0, 3), 1); |
| assert("limit=4 β 1 ε符οΌδΈι΄εδΈεΌοΌ", charIndexForByteLimit(cjk, 0, 4), 1); |
| assert("limit=6 β 2 ε符", charIndexForByteLimit(cjk, 0, 6), 2); |
| assert("limit=11 β 3 ε符", charIndexForByteLimit(cjk, 0, 11), 3); |
| assert("limit=12 β 4 ε符", charIndexForByteLimit(cjk, 0, 12), 4); |
| assert("start=1οΌlimit=3 β idx=2", charIndexForByteLimit(cjk, 1, 3), 2); |
|
|
| |
| console.log("4. EmojiοΌ4 εθ/εοΌJS charLen=2οΌ"); |
| const emoji = "πππ"; |
| assert("emoji limit=4 β idx=2οΌ1 emojiοΌ", charIndexForByteLimit(emoji, 0, 4), 2); |
| assert("emoji limit=5 β idx=2οΌεδΈεΌοΌ", charIndexForByteLimit(emoji, 0, 5), 2); |
| assert("emoji limit=8 β idx=4οΌ2 emojiοΌ", charIndexForByteLimit(emoji, 0, 8), 4); |
| assert("emoji limit=12 β idx=6οΌε
¨ι¨οΌ", charIndexForByteLimit(emoji, 0, 12), 6); |
| assert("emoji start=2οΌlimit=4 β idx=4", charIndexForByteLimit(emoji, 2, 4), 4); |
|
|
| |
| console.log("5. ζ··εε符"); |
| |
| const mixed = "Aε₯½π"; |
| assert("ζ··ε limit=1 β 1(A)", charIndexForByteLimit(mixed, 0, 1), 1); |
| assert("ζ··ε limit=3 β 1(AδΈε€ε₯½)", charIndexForByteLimit(mixed, 0, 3), 1); |
| assert("ζ··ε limit=4 β 2(Aε₯½)", charIndexForByteLimit(mixed, 0, 4), 2); |
| assert("ζ··ε limit=7 β 2(δΈε€emoji)",charIndexForByteLimit(mixed, 0, 7), 2); |
| assert("ζ··ε limit=8 β 4(ε
¨ι¨)", charIndexForByteLimit(mixed, 0, 8), 4); |
| assert("ζ··ε start=1οΌlimit=3 β 2(ε₯½)", charIndexForByteLimit(mixed, 1, 3), 2); |
|
|
| |
| console.log("6. start >= text.length"); |
| assert("start=5 on 'abc' β 5", charIndexForByteLimit("abc", 5, 10), 5); |
|
|
| |
| console.log("7. ε«ζ’θ‘符"); |
| const nl = "a\nb\n"; |
| assert("ζ’θ‘ limit=2 β 2", charIndexForByteLimit(nl, 0, 2), 2); |
| assert("ζ’θ‘ limit=3 β 3", charIndexForByteLimit(nl, 0, 3), 3); |
|
|
| |
| console.log(`\nη»ζ: ${passed} ιθΏ / ${failed} ε€±θ΄₯`); |
| if (failed > 0) process.exit(1); |
|
|