Skip to content

Commit a226778

Browse files
committed
test: lt-codes should allow loss and disorder
1 parent c2b570f commit a226778

File tree

1 file changed

+67
-23
lines changed

1 file changed

+67
-23
lines changed

test/lt.test.ts

+67-23
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,73 @@
11
import fs from 'node:fs/promises'
22
import { join } from 'node:path'
33

4-
import { expect, it } from 'vitest'
4+
import { describe, expect, it } from 'vitest'
55
import { binaryToBlock, blockToBinary, createDecoder, encodeFountain } from '../utils/lt-codes'
66

7-
it('slice binary', async () => {
8-
const input = (await fs.readFile(join('test', 'SampleJPGImage_100kbmb.jpg'), null)).buffer
9-
const data = new Uint32Array(input)
10-
11-
const decoder = createDecoder()
12-
let count = 0
13-
for (const block of encodeFountain(data, 1000)) {
14-
count += 1
15-
if (count > 1000)
16-
throw new Error('Too many blocks')
17-
const binary = blockToBinary(block)
18-
// Use the binary to transfer
19-
const back = binaryToBlock(binary)
20-
const result = decoder.addBlock([back])
21-
if (result)
22-
break
23-
}
24-
25-
const result = decoder.getDecoded()!
26-
expect(result).toBeDefined()
27-
expect(result).toBeInstanceOf(Uint32Array)
28-
expect(result.length).toBe(data.length)
7+
describe('lt-codes', () => {
8+
it('slice binary', async () => {
9+
const input = (await fs.readFile(join('test', 'SampleJPGImage_100kbmb.jpg'), null)).buffer
10+
const data = new Uint32Array(input)
11+
12+
const decoder = createDecoder()
13+
let count = 0
14+
for (const block of encodeFountain(data, 1000)) {
15+
count += 1
16+
if (count > 1000)
17+
throw new Error('Too many blocks')
18+
const binary = blockToBinary(block)
19+
// Use the binary to transfer
20+
const back = binaryToBlock(binary)
21+
const result = decoder.addBlock([back])
22+
if (result)
23+
break
24+
}
25+
26+
const result = decoder.getDecoded()!
27+
expect(result).toBeDefined()
28+
expect(result).toBeInstanceOf(Uint32Array)
29+
expect(result.length).toBe(data.length)
30+
})
31+
32+
it(`allow loss and disorder`, async () => {
33+
const input = (await fs.readFile(join('test', 'SampleJPGImage_100kbmb.jpg'), null)).buffer
34+
const data = new Uint32Array(input)
35+
36+
const decoder = createDecoder()
37+
let count = 0
38+
39+
const packets: Uint32Array[] = []
40+
41+
// Encode the data
42+
for (const block of encodeFountain(data, 1000)) {
43+
count += 1
44+
if (count > 1000)
45+
break
46+
packets.push(blockToBinary(block))
47+
}
48+
49+
// Dissupt the order of packets
50+
packets.sort(() => Math.random() - 0.5)
51+
// Simulate 50% of packet loss
52+
packets.length = Math.floor(packets.length * 0.5)
53+
54+
count = 0
55+
// Decode the data
56+
for (const packet of packets) {
57+
count += 1
58+
if (count > 500)
59+
throw new Error('Too many blocks')
60+
61+
// Use the binary to transfer
62+
const back = binaryToBlock(packet)
63+
const result = decoder.addBlock([back])
64+
if (result)
65+
break
66+
}
67+
68+
const result = decoder.getDecoded()!
69+
expect(result).toBeDefined()
70+
expect(result).toBeInstanceOf(Uint32Array)
71+
expect(result.length).toBe(data.length)
72+
})
2973
})

0 commit comments

Comments
 (0)