Export 0.2.1
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / gzip-js / node_modules / crc32 / test / test.js
1 (function () {
2         var fs = require('fs'),
3                 path = require('path'),
4                 crc32 = require('../lib/crc32'),
5                 testDir = './testFiles',
6                 checkFile,
7                 checkValues,
8                 usage = [
9                         'Usage:',
10                         '',
11                         'node test.js checkFile.json [/path/to/testFiles]'
12                 ].join('\n'),
13                 failed = false;
14
15         checkFile = process.argv[2];
16         if (process.argv.length === 4) {
17                 testDir = process.argv[3];
18         }
19
20         if (!checkFile) {
21                 console.log(usage);
22                 return;
23         }
24
25         try {
26                 checkValues = fs.readFileSync(checkFile, 'utf8');
27         } catch (e) {
28                 console.error('Unable to read ' + checkFile);
29                 return;
30         }
31
32         try {
33                 checkValues = JSON.parse(checkValues);
34                 Object.keys(checkValues).forEach(function (key) {
35                         checkValues[key] = parseInt(checkValues[key]).toString(16);
36                 });
37         } catch (e) {
38                 console.error('Unable to parse contents of ' + checkFile + ' as JSON.');
39                 console.error(checkValues);
40                 return;
41         }
42         
43         fs.readdirSync(testDir).forEach(function (file) {
44                 var data = fs.readFileSync(path.join(testDir, file)),
45                         tableRes = crc32(data),
46                         directRes = crc32(data, true),
47                         appendRes,
48                         arr;
49
50                 if (tableRes !== directRes) {
51                         console.log(file + ':',  'FAILED', '-', 'Results for table mode and direct mode');
52                         failed = true;
53                         return;
54                 }
55
56                 if (file in checkValues) {
57                         if (tableRes !== checkValues[file]) {
58                                 failed = true;
59                                 console.log(file + ':', 'FAILED', '-', 'Results do not match {val = ' + tableRes + ', actual = ' + checkValues[file] + '}');
60                                 return;
61                         }
62                 } else {
63                         console.warn('No check value for ' + file);
64                 }
65
66                 // run append test
67
68                 // clear any previous data
69                 crc32.table();
70
71                 // convert Buffer to byte array
72                 arr = Array.prototype.map.call(data, function (byte) {
73                         return byte;
74                 });
75
76                 // run in append mode in 10 byte chunks
77                 while (arr.length) {
78                         appendRes = (crc32.table(arr.splice(0, 10), true) >>> 0).toString(16);
79                 }
80
81                 if (appendRes !== tableRes) {
82                         console.log(file + ':', 'FAILED', '-', 'Append mode output not correct');
83                         console.log(appendRes, tableRes);
84                         return;
85                 }
86
87                 console.log(file + ':', 'PASSED');
88         });
89
90         console.log();
91         console.log(failed ? 'Tests failed =\'(' : 'All tests passed!! =D');
92 }());