@@ -2,8 +2,9 @@ import { readFileSync } from 'node:fs'
22import { join } from 'node:path'
33import { test , describe } from 'node:test'
44import { createSinglebyteDecoder , createSinglebyteEncoder } from '@exodus/bytes/single-byte.js'
5- import { encodingDecoder } from '../fallback/single-byte.js'
5+ import { encodingDecoder , getEncoding } from '../fallback/single-byte.js'
66import encodingsObject from '../fallback/single-byte.encodings.js'
7+ import { singleByte } from './encoding/fixtures/indexes.cjs'
78
89const encodings = Object . keys ( encodingsObject )
910const nonWhatwg = new Set ( [ 'iso-8859-1' , 'iso-8859-9' , 'iso-8859-11' ] )
@@ -214,6 +215,42 @@ describe('single-byte encodings index: WHATWG', () => {
214215 }
215216} )
216217
218+ describe ( 'single-byte encodings index: WHATWG non-normative indexes.json' , ( ) => {
219+ for ( const [ encoding , data ] of Object . entries ( singleByte ) ) {
220+ test ( encoding , ( t ) => {
221+ t . assert . ok ( Object . hasOwn ( encodingsObject , encoding ) )
222+ const high = getEncoding ( encoding )
223+ while ( high . length < 128 ) high . push ( 128 + high . length )
224+ t . assert . deepStrictEqual (
225+ high ,
226+ data . map ( ( x ) => ( x === null ? 65_533 : x ) )
227+ )
228+ const decoder = createSinglebyteDecoder ( encoding )
229+ const decoderLoose = createSinglebyteDecoder ( encoding , true )
230+ const encoder = createSinglebyteEncoder ( encoding )
231+
232+ t . assert . strictEqual ( data . length , 128 )
233+ for ( let i = 0 ; i < data . length ; i ++ ) {
234+ const byte = i + 128
235+ t . assert . notEqual ( data [ i ] , 0 )
236+ t . assert . notEqual ( data [ i ] , 0xff_fd )
237+ const str = data [ i ] === null ? null : String . fromCodePoint ( data [ i ] )
238+
239+ if ( str ) {
240+ t . assert . ok ( data [ i ] > 0 )
241+ t . assert . ok ( data [ i ] <= 0xff_ff )
242+ t . assert . strictEqual ( decoder ( Uint8Array . of ( byte ) ) , str )
243+ t . assert . strictEqual ( decoderLoose ( Uint8Array . of ( byte ) ) , str )
244+ t . assert . deepStrictEqual ( encoder ( str ) , Uint8Array . of ( byte ) )
245+ } else {
246+ t . assert . throws ( ( ) => decoder ( Uint8Array . of ( byte ) ) )
247+ t . assert . strictEqual ( decoderLoose ( Uint8Array . of ( byte ) ) , '\uFFFD' )
248+ }
249+ }
250+ } )
251+ }
252+ } )
253+
217254// https://encoding.spec.whatwg.org/#x-user-defined-decoder
218255describe ( 'x-user-defined' , ( ) => {
219256 const encoding = 'x-user-defined'
0 commit comments