]>
git.proxmox.com Git - mirror_novnc.git/blob - tests/test.deflator.js
12e8a46bfa58e1b071e90ade57e0f26b13ddd7c8
1 /* eslint-disable no-console */
2 const expect
= chai
.expect
;
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";
8 function _inflator(compText
, expected
) {
9 let strm
= new ZStream();
10 let chunkSize
= 1024 * 10 * 10;
11 strm
.output
= new Uint8Array(chunkSize
);
15 if (expected
> chunkSize
) {
17 strm
.output
= new Uint8Array(chunkSize
);
20 /* eslint-disable camelcase */
21 strm
.input
= compText
;
22 strm
.avail_in
= strm
.input
.length
;
26 strm
.avail_out
= expected
.length
;
27 /* eslint-enable camelcase */
29 let ret
= inflate(strm
, 0);
31 // Check that return code is not an error
32 expect(ret
).to
.be
.greaterThan(-1);
34 return new Uint8Array(strm
.output
.buffer
, 0, strm
.next_out
);
37 describe('Deflate data', function () {
39 it('should be able to deflate messages', function () {
40 let deflator
= new Deflator();
43 let preText
= new Uint8Array(text
.length
);
44 for (let i
= 0; i
< preText
.length
; i
++) {
45 preText
[i
] = text
.charCodeAt(i
);
48 let compText
= deflator
.deflate(preText
);
50 let inflatedText
= _inflator(compText
, text
.length
);
51 expect(inflatedText
).to
.array
.equal(preText
);
55 it('should be able to deflate large messages', function () {
56 let deflator
= new Deflator();
58 /* Generate a big string with random characters. Used because
59 repetition of letters might be deflated more effectively than
62 let characters
= 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
63 for (let i
= 0; i
< 300000; i
++) {
64 text
+= characters
.charAt(Math
.floor(Math
.random() * characters
.length
));
67 let preText
= new Uint8Array(text
.length
);
68 for (let i
= 0; i
< preText
.length
; i
++) {
69 preText
[i
] = text
.charCodeAt(i
);
72 let compText
= deflator
.deflate(preText
);
74 //Check that the compressed size is expected size
75 expect(compText
.length
).to
.be
.greaterThan((1024 * 10 * 10) * 2);
77 let inflatedText
= _inflator(compText
, text
.length
);
79 expect(inflatedText
).to
.array
.equal(preText
);