|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +require('../common'); |
| 4 | +const fs = require('node:fs'); |
| 5 | +const path = require('node:path'); |
| 6 | +const assert = require('node:assert'); |
| 7 | +const { describe, it } = require('node:test'); |
| 8 | +const tmpdir = require('../common/tmpdir'); |
| 9 | + |
| 10 | +tmpdir.refresh(); |
| 11 | + |
| 12 | +function writeFile(name, buf) { |
| 13 | + const p = path.join(tmpdir.path, name); |
| 14 | + fs.writeFileSync(p, buf); |
| 15 | + return p; |
| 16 | +} |
| 17 | + |
| 18 | +function expectMatches(filePath, rawBuf) { |
| 19 | + assert.strictEqual( |
| 20 | + fs.readFileSync(filePath, 'utf8'), |
| 21 | + rawBuf.toString('utf8'), |
| 22 | + ); |
| 23 | +} |
| 24 | + |
| 25 | +describe('fs.readFileSync utf8 simdutf dispatch', () => { |
| 26 | + it('empty file', () => { |
| 27 | + const p = writeFile('empty.txt', Buffer.alloc(0)); |
| 28 | + assert.strictEqual(fs.readFileSync(p, 'utf8'), ''); |
| 29 | + }); |
| 30 | + |
| 31 | + it('ascii small', () => { |
| 32 | + const buf = Buffer.from('hello'); |
| 33 | + expectMatches(writeFile('tiny-ascii.txt', buf), buf); |
| 34 | + }); |
| 35 | + |
| 36 | + it('ascii 20KB', () => { |
| 37 | + const buf = Buffer.alloc(20 * 1024, 0x41); |
| 38 | + expectMatches(writeFile('medium-ascii.txt', buf), buf); |
| 39 | + }); |
| 40 | + |
| 41 | + it('ascii 1MB', () => { |
| 42 | + const buf = Buffer.alloc(1024 * 1024, 0x61); |
| 43 | + expectMatches(writeFile('large-ascii.txt', buf), buf); |
| 44 | + }); |
| 45 | + |
| 46 | + it('fd input', () => { |
| 47 | + const buf = Buffer.alloc(50 * 1024, 0x62); |
| 48 | + const p = writeFile('fd-ascii.txt', buf); |
| 49 | + const fd = fs.openSync(p, 'r'); |
| 50 | + try { |
| 51 | + assert.strictEqual(fs.readFileSync(fd, 'utf8'), buf.toString('utf8')); |
| 52 | + } finally { |
| 53 | + fs.closeSync(fd); |
| 54 | + } |
| 55 | + }); |
| 56 | + |
| 57 | + it('multibyte UTF-8', () => { |
| 58 | + const buf = Buffer.from('中文测试 — café — 🚀'.repeat(500), 'utf8'); |
| 59 | + expectMatches(writeFile('multibyte.txt', buf), buf); |
| 60 | + }); |
| 61 | + |
| 62 | + it('latin1-fits utf8', () => { |
| 63 | + const buf = Buffer.from('naïve café résumé — niño Köln '.repeat(500), 'utf8'); |
| 64 | + expectMatches(writeFile('latin1-fits.txt', buf), buf); |
| 65 | + }); |
| 66 | + |
| 67 | + it('invalid: lone continuation byte', () => { |
| 68 | + const buf = Buffer.from([0x68, 0x69, 0x80, 0x21]); |
| 69 | + expectMatches(writeFile('invalid-cont.txt', buf), buf); |
| 70 | + }); |
| 71 | + |
| 72 | + it('invalid: overlong', () => { |
| 73 | + const buf = Buffer.from([0x41, 0xC0, 0xAF, 0x42]); |
| 74 | + expectMatches(writeFile('invalid-overlong.txt', buf), buf); |
| 75 | + }); |
| 76 | + |
| 77 | + it('invalid: surrogate', () => { |
| 78 | + const buf = Buffer.from([0x41, 0xED, 0xA0, 0x80, 0x42]); |
| 79 | + expectMatches(writeFile('invalid-surrogate.txt', buf), buf); |
| 80 | + }); |
| 81 | + |
| 82 | + it('latin1 boundary U+00FF', () => { |
| 83 | + const buf = Buffer.from('ÿ'.repeat(2048), 'utf8'); |
| 84 | + expectMatches(writeFile('latin1-boundary.txt', buf), buf); |
| 85 | + }); |
| 86 | + |
| 87 | + it('above latin1 U+0100', () => { |
| 88 | + const buf = Buffer.from('ĀāĂ'.repeat(1024), 'utf8'); |
| 89 | + expectMatches(writeFile('above-latin1.txt', buf), buf); |
| 90 | + }); |
| 91 | + |
| 92 | + it('single codepoint each UTF-8 length', () => { |
| 93 | + for (const cp of [0x41, 0x00E9, 0x4E2D, 0x1F600]) { |
| 94 | + const buf = Buffer.from(String.fromCodePoint(cp), 'utf8'); |
| 95 | + expectMatches(writeFile(`single-cp-${cp.toString(16)}.txt`, buf), buf); |
| 96 | + } |
| 97 | + }); |
| 98 | + |
| 99 | + it('truncated multibyte at EOF', () => { |
| 100 | + const buf = Buffer.from([0x41, 0xE4, 0xB8]); |
| 101 | + expectMatches(writeFile('truncated-multibyte.txt', buf), buf); |
| 102 | + }); |
| 103 | +}); |
0 commit comments