]>
Commit | Line | Data |
---|---|---|
f52e9790 NL |
1 | /* eslint-disable no-console */ |
2 | const expect = chai.expect; | |
3 | ||
4 | import { inflateInit, inflate } from "../vendor/pako/lib/zlib/inflate.js"; | |
5 | import ZStream from "../vendor/pako/lib/zlib/zstream.js"; | |
6 | import Deflator from "../core/deflator.js"; | |
7 | ||
8 | function _inflator(compText, expected) { | |
9 | let strm = new ZStream(); | |
10 | let chunkSize = 1024 * 10 * 10; | |
11 | strm.output = new Uint8Array(chunkSize); | |
12 | ||
13 | inflateInit(strm, 5); | |
14 | ||
15 | if (expected > chunkSize) { | |
16 | chunkSize = expected; | |
17 | strm.output = new Uint8Array(chunkSize); | |
18 | } | |
19 | ||
20 | strm.input = compText; | |
21 | strm.avail_in = strm.input.length; | |
22 | strm.next_in = 0; | |
23 | ||
24 | strm.next_out = 0; | |
25 | strm.avail_out = expected.length; | |
26 | ||
27 | let ret = inflate(strm, 0); | |
28 | ||
29 | // Check that return code is not an error | |
30 | expect(ret).to.be.greaterThan(-1); | |
31 | ||
32 | return new Uint8Array(strm.output.buffer, 0, strm.next_out); | |
33 | } | |
34 | ||
35 | describe('Deflate data', function () { | |
36 | ||
37 | it('should be able to deflate messages', function () { | |
38 | let deflator = new Deflator(); | |
39 | ||
40 | let text = "123asdf"; | |
41 | let preText = new Uint8Array(text.length); | |
42 | for (let i = 0; i < preText.length; i++) { | |
43 | preText[i] = text.charCodeAt(i); | |
44 | } | |
45 | ||
46 | let compText = deflator.deflate(preText); | |
47 | ||
48 | let inflatedText = _inflator(compText, text.length); | |
49 | expect(inflatedText).to.array.equal(preText); | |
50 | ||
51 | }); | |
52 | ||
53 | it('should be able to deflate large messages', function () { | |
54 | let deflator = new Deflator(); | |
55 | ||
56 | /* Generate a big string with random characters. Used because | |
57 | repetition of letters might be deflated more effectively than | |
58 | random ones. */ | |
59 | let text = ""; | |
60 | let characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; | |
61 | for (let i = 0; i < 300000; i++) { | |
62 | text += characters.charAt(Math.floor(Math.random() * characters.length)); | |
63 | } | |
64 | ||
65 | let preText = new Uint8Array(text.length); | |
66 | for (let i = 0; i < preText.length; i++) { | |
67 | preText[i] = text.charCodeAt(i); | |
68 | } | |
69 | ||
70 | let compText = deflator.deflate(preText); | |
71 | ||
72 | //Check that the compressed size is expected size | |
73 | expect(compText.length).to.be.greaterThan((1024 * 10 * 10) * 2); | |
74 | ||
75 | let inflatedText = _inflator(compText, text.length); | |
76 | ||
77 | expect(inflatedText).to.array.equal(preText); | |
78 | ||
79 | }); | |
80 | }); |