Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
1 importScripts("resource://gre/modules/workers/require.js");
2 importScripts("resource://gre/modules/osfile.jsm");
5 function do_print(x) {
6 //self.postMessage({kind: "do_print", args: [x]});
7 dump("TEST-INFO: " + x + "\n");
8 }
10 function do_check_true(x) {
11 self.postMessage({kind: "do_check_true", args: [!!x]});
12 if (x) {
13 dump("TEST-PASS: " + x + "\n");
14 } else {
15 throw new Error("do_check_true failed");
16 }
17 }
19 function do_check_eq(a, b) {
20 let result = a == b;
21 self.postMessage({kind: "do_check_true", args: [result]});
22 if (!result) {
23 throw new Error("do_check_eq failed " + a + " != " + b);
24 }
25 }
27 function do_test_complete() {
28 self.postMessage({kind: "do_test_complete", args:[]});
29 }
31 self.onmessage = function() {
32 try {
33 run_test();
34 } catch (ex) {
35 let {message, moduleStack, moduleName, lineNumber} = ex;
36 let error = new Error(message, moduleName, lineNumber);
37 error.stack = moduleStack;
38 dump("Uncaught error: " + error + "\n");
39 dump("Full stack: " + moduleStack + "\n");
40 throw error;
41 }
42 };
44 let Lz4;
45 let Internals;
46 function test_import() {
47 Lz4 = require("resource://gre/modules/workers/lz4.js");
48 Internals = require("resource://gre/modules/workers/lz4_internal.js");
49 }
51 function test_bound() {
52 for (let k of ["compress", "decompress", "maxCompressedSize"]) {
53 try {
54 do_print("Checking the existence of " + k + "\n");
55 do_check_true(!!Internals[k]);
56 do_print(k + " exists");
57 } catch (ex) {
58 // Ignore errors
59 do_print(k + " doesn't exist!");
60 }
61 }
62 }
64 function test_reference_file() {
65 do_print("Decompress reference file");
66 let path = OS.Path.join("data", "compression.lz");
67 let data = OS.File.read(path);
68 let decompressed = Lz4.decompressFileContent(data);
69 let text = (new TextDecoder()).decode(decompressed);
70 do_check_eq(text, "Hello, lz4");
71 }
73 function compare_arrays(a, b) {
74 return Array.prototype.join.call(a) == Array.prototype.join.call(a);
75 }
77 function run_rawcompression(name, array) {
78 do_print("Raw compression test " + name);
79 let length = array.byteLength;
80 let compressedArray = new Uint8Array(Internals.maxCompressedSize(length));
81 let compressedBytes = Internals.compress(array, length, compressedArray);
82 compressedArray = new Uint8Array(compressedArray.buffer, 0, compressedBytes);
83 do_print("Raw compressed: " + length + " into " + compressedBytes);
85 let decompressedArray = new Uint8Array(length);
86 let decompressedBytes = new ctypes.size_t();
87 let success = Internals.decompress(compressedArray, compressedBytes,
88 decompressedArray, length,
89 decompressedBytes.address());
90 do_print("Raw decompression success? " + success);
91 do_print("Raw decompression size: " + decompressedBytes.value);
92 do_check_true(compare_arrays(array, decompressedArray));
93 }
95 function run_filecompression(name, array) {
96 do_print("File compression test " + name);
97 let compressed = Lz4.compressFileContent(array);
98 do_print("Compressed " + array.byteLength + " bytes into " + compressed.byteLength);
100 let decompressed = Lz4.decompressFileContent(compressed);
101 do_print("Decompressed " + compressed.byteLength + " bytes into " + decompressed.byteLength);
102 do_check_true(compare_arrays(array, decompressed));
103 }
105 function run_faileddecompression(name, array) {
106 do_print("invalid decompression test " + name);
108 // Ensure that raw decompression doesn't segfault
109 let length = 1 << 14;
110 let decompressedArray = new Uint8Array(length);
111 let decompressedBytes = new ctypes.size_t();
112 Internals.decompress(array, array.byteLength,
113 decompressedArray, length,
114 decompressedBytes.address());
116 // File decompression should fail with an acceptable exception
117 let exn = null;
118 try {
119 Lz4.decompressFileContent(array);
120 } catch (ex) {
121 exn = ex;
122 }
123 do_check_true(exn);
124 if (array.byteLength < 10) {
125 do_check_true(exn.becauseLZNoHeader);
126 } else {
127 do_check_true(exn.becauseLZWrongMagicNumber);
128 }
129 }
131 function run_test() {
132 test_import();
133 test_bound();
134 test_reference_file();
135 for (let length of [0, 1, 1024]) {
136 let array = new Uint8Array(length);
137 for (let i = 0; i < length; ++i) {
138 array[i] = i % 256;
139 }
140 let name = length + " bytes";
141 run_rawcompression(name, array);
142 run_filecompression(name, array);
143 run_faileddecompression(name, array);
144 }
145 do_test_complete();
146 }