-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathsingle-byte.test.js
More file actions
327 lines (295 loc) · 13.1 KB
/
single-byte.test.js
File metadata and controls
327 lines (295 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { readFileSync } from 'node:fs'
import { join } from 'node:path'
import { test, describe } from 'node:test'
import { createSinglebyteDecoder, createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
import { encodingDecoder, getEncoding } from '../fallback/single-byte.js'
import encodingsObject from '../fallback/single-byte.encodings.js'
import { singleByte } from './encoding/fixtures/indexes.cjs'
const encodings = Object.keys(encodingsObject)
const nonWhatwg = new Set(['iso-8859-1', 'iso-8859-9', 'iso-8859-11'])
// See also tests/encoding/single-byte.tables.test.js for similar TextDecoder tests
describe('single-byte encodings are supersets of ascii', () => {
for (const encoding of encodings) {
test(encoding, (t) => {
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
for (let i = 0; i < 128; i++) {
let str
try {
str = decoder(Uint8Array.of(i))
} catch (cause) {
throw new Error(`Error decoding ${i} in ${encoding}`, { cause })
}
t.assert.strictEqual(str.length, 1, i)
t.assert.strictEqual(str.codePointAt(0), i, i)
t.assert.strictEqual(decoderLoose(Uint8Array.of(i)), str, i)
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(i))
t.assert.deepStrictEqual(encoderLoose(str), Uint8Array.of(i))
}
})
}
})
describe('single-byte encodings match fallback', () => {
for (const encoding of encodings) {
test(encoding, (t) => {
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
const fallback = encodingDecoder(encoding)
for (let i = 0; i < 256; i++) {
const u8 = Uint8Array.of(i)
let found = false
let str
try {
str = decoder(u8)
found = true
} catch {}
if (found) {
t.assert.strictEqual(str.length, 1)
t.assert.notEqual(str, '\uFFFD')
t.assert.strictEqual(decoderLoose(u8), str)
t.assert.strictEqual(fallback(u8), str)
t.assert.strictEqual(fallback(u8, true), str)
} else {
t.assert.ok(i >= 128)
t.assert.throws(() => fallback(u8))
str = decoderLoose(u8)
t.assert.strictEqual(str.length, 1)
t.assert.strictEqual(str, '\uFFFD')
t.assert.strictEqual(fallback(u8, true), str)
}
}
})
}
})
describe('single-byte encodings index: Unicode', () => {
for (const encoding of encodings) {
let fileName
let allowSuperset = false
if (encoding.startsWith('iso-8859-')) {
fileName = `ISO8859/${encoding.replace('iso-', '')}.txt`
} else if (encoding.startsWith('windows-')) {
fileName = `VENDORS/MICSFT/WINDOWS/CP${encoding.replace('windows-', '')}.txt`
allowSuperset = true
} else {
continue
}
test(encoding, (t) => {
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
const text = readFileSync(
join(import.meta.dirname, 'encoding/fixtures/unicode/', fileName),
'utf8'
)
const rows = text
.split('\n')
.map((x) => x.trim())
.filter((x) => x && x[0] !== '#')
.map((x) => x.split('\t'))
.filter((x) => !(['', ' '].includes(x[1]) && x[2] === '#UNDEFINED'))
.map(([iHex, codeHex]) => {
const id = `${fileName}: ${iHex} -> ${codeHex}`
const i = parseInt(iHex.slice(2), 16)
t.assert.ok(i < 256)
const code = parseInt(codeHex.slice(2), 16)
t.assert.strictEqual('0x' + i.toString(16).padStart(2, '0').toUpperCase(), iHex, id)
t.assert.strictEqual('0x' + code.toString(16).padStart(4, '0').toUpperCase(), codeHex, id)
t.assert.ok(i === 0 || code !== 0, id)
t.assert.ok(code !== 0xff_fd && code <= 0xff_ff, id) // can't be a replacement char, has to be <= 16-bit
t.assert.ok(code < 0xd8_00 || code >= 0xe0_00, id) // not a surrogate
t.assert.ok(i > 0x7f || code === i, id)
return [i, code]
})
t.assert.ok(rows.length >= 128 && rows.length <= 256)
const known = new Map(rows)
t.assert.strictEqual(rows.length, known.size) // all unique
for (let byte = 0; byte < 256; byte++) {
let code = known.get(byte)
let str
if (allowSuperset && code === undefined) {
try {
const dec = decoder(Uint8Array.of(byte))
// if it doesn't throw, it should map to the same byte, except windows-1255 which adds more non-trivial entries
code = encoding === 'windows-1255' ? dec.codePointAt(0) : byte
} catch {}
}
if (code === undefined) {
t.assert.throws(() => decoder(Uint8Array.of(byte)))
try {
str = decoderLoose(Uint8Array.of(byte))
} catch (cause) {
throw new Error(`Error decoding unmapped ${byte} in ${encoding}`, { cause })
}
t.assert.strictEqual(str.length, 1)
t.assert.strictEqual(str.codePointAt(0), 0xff_fd)
} else {
try {
str = decoder(Uint8Array.of(byte))
} catch (cause) {
throw new Error(`Error decoding ${byte} in ${encoding}: ${byte}`, { cause })
}
t.assert.strictEqual(str.length, 1, byte)
t.assert.strictEqual(str.codePointAt(0), code, byte)
t.assert.strictEqual(str, decoderLoose(Uint8Array.of(byte)))
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
t.assert.deepStrictEqual(encoderLoose(str), Uint8Array.of(byte))
}
}
})
}
})
describe('single-byte encodings index: WHATWG', () => {
for (const encoding of encodings) {
if (nonWhatwg.has(encoding)) continue
test(encoding, (t) => {
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
const text = readFileSync(
join(import.meta.dirname, 'encoding/fixtures/single-byte', `index-${encoding}.txt`),
'utf8'
)
const rows = text
.split('\n')
.map((x) => x.trim())
.filter((x) => x && x[0] !== '#')
.map((x) => x.split('\t'))
.map(([istr, codeHex, description]) => {
const i = Number(istr)
t.assert.ok(i < 128)
const code = parseInt(codeHex.slice(2), 16)
t.assert.strictEqual(`${i}`, istr)
t.assert.strictEqual('0x' + code.toString(16).padStart(4, '0').toUpperCase(), codeHex)
t.assert.ok(code && code !== 0xff_fd && code <= 0xff_ff) // can't be a replacement char, has to be <= 16-bit
t.assert.ok(code < 0xd8_00 || code >= 0xe0_00) // not a surrogate
return [i, { i, code, description }]
})
t.assert.ok(rows.length <= 128)
const known = new Map(rows)
t.assert.strictEqual(rows.length, known.size) // all unique
for (let i = 0; i < 128; i++) {
const row = known.get(i)
const byte = i + 128
let str
if (row) {
t.assert.strictEqual(i, row.i)
try {
str = decoder(Uint8Array.of(byte))
} catch (cause) {
throw new Error(`Error decoding ${byte} in ${encoding}: ${row.description}`, { cause })
}
t.assert.strictEqual(str.length, 1, row.description)
t.assert.strictEqual(str.codePointAt(0), row.code, row.description)
t.assert.strictEqual(str, decoderLoose(Uint8Array.of(byte)))
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
t.assert.deepStrictEqual(encoderLoose(str), Uint8Array.of(byte))
} else {
t.assert.throws(() => decoder(Uint8Array.of(byte)))
try {
str = decoderLoose(Uint8Array.of(byte))
} catch (cause) {
throw new Error(`Error decoding unmapped ${byte} in ${encoding}`, { cause })
}
t.assert.strictEqual(str.length, 1)
t.assert.strictEqual(str.codePointAt(0), 0xff_fd)
}
}
})
}
})
describe('single-byte encodings index: WHATWG non-normative indexes.json', () => {
for (const [encoding, data] of Object.entries(singleByte)) {
test(encoding, (t) => {
t.assert.ok(!data.includes(0))
t.assert.ok(!data.includes(0xff_fd))
t.assert.ok(Object.hasOwn(encodingsObject, encoding))
const m = getEncoding(encoding).map((x) => (x === 0xff_fd ? null : x))
while (m.length < 128) m.push(128 + m.length)
t.assert.deepStrictEqual(m, data)
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
t.assert.strictEqual(data.length, 128)
for (let i = 0; i < data.length; i++) {
const byte = i + 128
t.assert.notEqual(data[i], 0)
t.assert.notEqual(data[i], 0xff_fd)
const str = data[i] === null ? null : String.fromCodePoint(data[i])
if (str) {
t.assert.ok(data[i] > 0)
t.assert.ok(data[i] <= 0xff_ff)
t.assert.strictEqual(decoder(Uint8Array.of(byte)), str)
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str)
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte))
t.assert.deepStrictEqual(encoderLoose(str), Uint8Array.of(byte))
} else {
t.assert.throws(() => decoder(Uint8Array.of(byte)))
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), '\uFFFD')
}
}
})
}
})
// https://encoding.spec.whatwg.org/#x-user-defined-decoder
describe('x-user-defined', () => {
const encoding = 'x-user-defined'
test('decode', (t) => {
const decoder = createSinglebyteDecoder(encoding)
const decoderLoose = createSinglebyteDecoder(encoding, true)
for (let byte = 0; byte < 256; byte++) {
const str = String.fromCodePoint(byte >= 0x80 ? 0xf7_80 + byte - 0x80 : byte)
t.assert.strictEqual(decoder(Uint8Array.of(byte)), str, byte)
t.assert.strictEqual(decoderLoose(Uint8Array.of(byte)), str, byte)
}
})
test('encode', (t) => {
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
for (let byte = 0; byte < 256; byte++) {
const str = String.fromCodePoint(byte >= 0x80 ? 0xf7_80 + byte - 0x80 : byte)
t.assert.deepStrictEqual(encoder(str), Uint8Array.of(byte), byte)
t.assert.deepStrictEqual(encoderLoose(str), Uint8Array.of(byte), byte)
}
for (let i = 128; i < 512; i++) {
t.assert.throws(() => encoder(String.fromCodePoint(i)), /Input is not well-formed/)
t.assert.deepStrictEqual(encoderLoose(String.fromCodePoint(i)), Uint8Array.of(0x3f), i)
}
})
})
describe('codes above 0x7F are non-ASCII', () => {
// 0x80 maps to U+80
for (const encoding of ['iso-8859-2', 'iso-8859-16']) {
test(encoding, (t) => {
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
t.assert.deepStrictEqual(encoder('\x80'), new Uint8Array(1).fill(0x80))
t.assert.deepStrictEqual(encoder('\x80'.repeat(4)), new Uint8Array(4).fill(0x80))
t.assert.deepStrictEqual(encoder('\x80'.repeat(8)), new Uint8Array(8).fill(0x80))
t.assert.deepStrictEqual(encoder('\x80'.repeat(16)), new Uint8Array(16).fill(0x80))
t.assert.deepStrictEqual(encoderLoose('\x80'), new Uint8Array(1).fill(0x80))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(4)), new Uint8Array(4).fill(0x80))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(8)), new Uint8Array(8).fill(0x80))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(16)), new Uint8Array(16).fill(0x80))
})
}
// 0x80 maps to something else
for (const encoding of ['windows-1250', 'windows-1252', 'x-user-defined']) {
test(encoding, (t) => {
const encoder = createSinglebyteEncoder(encoding)
const encoderLoose = createSinglebyteEncoder(encoding, { mode: 'replacement' })
t.assert.throws(() => encoder('\x80'))
t.assert.throws(() => encoder('\x80'.repeat(4)))
t.assert.throws(() => encoder('\x80'.repeat(8)))
t.assert.throws(() => encoder('\x80'.repeat(16)))
t.assert.deepStrictEqual(encoderLoose('\x80'), new Uint8Array(1).fill(0x3f))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(4)), new Uint8Array(4).fill(0x3f))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(8)), new Uint8Array(8).fill(0x3f))
t.assert.deepStrictEqual(encoderLoose('\x80'.repeat(16)), new Uint8Array(16).fill(0x3f))
})
}
})