hyparquet/test/snappy.test.js

74 lines
3.0 KiB
JavaScript
Raw Normal View History

2024-04-13 06:58:37 +00:00
import fs from 'fs'
2023-12-29 19:20:34 +00:00
import { describe, expect, it } from 'vitest'
2024-01-04 19:11:00 +00:00
import { snappyUncompress } from '../src/snappy.js'
2023-12-29 19:20:34 +00:00
describe('snappy uncompress', () => {
2025-04-01 06:20:22 +00:00
it('decompresses valid input correctly', () => {
2023-12-29 19:20:34 +00:00
const testCases = [
2024-02-26 18:32:53 +00:00
{ compressed: [0x00], expected: '' },
{ compressed: [0x01, 0x00, 0x68], expected: 'h' },
{ compressed: [0x02, 0x04, 0x68, 0x79], expected: 'hy' },
{ compressed: [0x03, 0x08, 0x68, 0x79, 0x70], expected: 'hyp' },
{ compressed: [0x05, 0x10, 0x68, 0x79, 0x70, 0x65, 0x72], expected: 'hyper' },
{
compressed: [0x0a, 0x24, 0x68, 0x79, 0x70, 0x65, 0x72, 0x70, 0x61, 0x72, 0x61, 0x6d],
expected: 'hyperparam',
},
{
compressed: [0x15, 0x08, 0x68, 0x79, 0x70, 0x46, 0x03, 0x00],
expected: 'hyphyphyphyphyphyphyp',
},
2024-02-19 00:42:58 +00:00
{
// from rowgroups.parquet
2024-02-26 18:32:53 +00:00
compressed: [
2024-02-19 00:42:58 +00:00
80, 4, 1, 0, 9, 1, 0, 2, 9, 7, 4, 0, 3, 13, 8, 0, 4, 13, 8, 0, 5, 13,
8, 0, 6, 13, 8, 0, 7, 13, 8, 0, 8, 13, 8, 60, 9, 0, 0, 0, 0, 0, 0, 0,
10, 0, 0, 0, 0, 0, 0, 0,
2024-02-26 18:32:53 +00:00
],
2024-02-19 00:42:58 +00:00
expected: new Uint8Array([
1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0,
0, 4, 0, 0, 0, 0, 0, 0, 0, 5, 0, 0, 0, 0, 0, 0, 0, 6, 0, 0, 0, 0, 0,
0, 0, 7, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0, 0, 0, 0, 0, 9, 0, 0, 0, 0,
0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0,
]),
},
2024-02-26 18:32:53 +00:00
// from datapage_v2.snappy.parquet
{ compressed: [2, 4, 0, 3], expected: new Uint8Array([0, 3]) },
{ compressed: [ 6, 20, 2, 0, 0, 0, 3, 23], expected: new Uint8Array([2, 0, 0, 0, 3, 23]) },
2023-12-29 19:20:34 +00:00
]
2025-04-01 06:20:22 +00:00
for (const { compressed, expected } of testCases) {
2024-02-26 18:32:53 +00:00
const output = new Uint8Array(expected.length)
2025-04-01 06:20:22 +00:00
snappyUncompress(new Uint8Array(compressed), output)
2024-02-19 00:42:58 +00:00
if (typeof expected === 'string') {
2024-02-26 18:32:53 +00:00
const outputStr = new TextDecoder().decode(output)
2024-02-19 00:42:58 +00:00
expect(outputStr).toBe(expected)
} else {
2024-02-26 18:32:53 +00:00
expect(output).toEqual(expected) // Uint8Array
2024-02-19 00:42:58 +00:00
}
2025-04-01 06:20:22 +00:00
}
2023-12-29 19:20:34 +00:00
})
2024-04-13 06:58:37 +00:00
it('decompress hyparquet.jpg.snappy', async () => {
const compressed = fs.readFileSync('test/files/hyparquet.jpg.snappy')
const expected = fs.readFileSync('hyparquet.jpg')
const output = new Uint8Array(expected.length)
await snappyUncompress(compressed, output)
expect(Array.from(output)).toEqual(Array.from(expected))
})
2024-02-19 00:42:58 +00:00
it('throws for invalid input', () => {
2024-02-26 18:32:53 +00:00
const output = new Uint8Array(10)
expect(() => snappyUncompress(new Uint8Array([]), output))
2024-02-19 00:42:58 +00:00
.toThrow('invalid snappy length header')
2024-02-26 18:32:53 +00:00
expect(() => snappyUncompress(new Uint8Array([0xff]), output))
2024-02-19 00:42:58 +00:00
.toThrow('invalid snappy length header')
2024-02-26 18:32:53 +00:00
expect(() => snappyUncompress(new Uint8Array([0x03, 0x61]), output))
2024-02-19 00:42:58 +00:00
.toThrow('missing eof marker')
2024-02-26 18:32:53 +00:00
expect(() => snappyUncompress(new Uint8Array([0x03, 0xf1]), output))
2024-02-19 00:42:58 +00:00
.toThrow('missing eof marker')
2024-02-26 18:32:53 +00:00
expect(() => snappyUncompress(new Uint8Array([0x02, 0x00, 0x68]), output))
2024-02-19 00:42:58 +00:00
.toThrow('premature end of input')
2023-12-29 19:20:34 +00:00
})
})