2 var fs = require('fs'),
3 path = require('path'),
4 crc32 = require('../lib/crc32'),
5 testDir = './testFiles',
11 'node test.js checkFile.json [/path/to/testFiles]'
15 checkFile = process.argv[2];
16 if (process.argv.length === 4) {
17 testDir = process.argv[3];
26 checkValues = fs.readFileSync(checkFile, 'utf8');
28 console.error('Unable to read ' + checkFile);
33 checkValues = JSON.parse(checkValues);
34 Object.keys(checkValues).forEach(function (key) {
35 checkValues[key] = parseInt(checkValues[key]).toString(16);
38 console.error('Unable to parse contents of ' + checkFile + ' as JSON.');
39 console.error(checkValues);
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),
50 if (tableRes !== directRes) {
51 console.log(file + ':', 'FAILED', '-', 'Results for table mode and direct mode');
56 if (file in checkValues) {
57 if (tableRes !== checkValues[file]) {
59 console.log(file + ':', 'FAILED', '-', 'Results do not match {val = ' + tableRes + ', actual = ' + checkValues[file] + '}');
63 console.warn('No check value for ' + file);
68 // clear any previous data
71 // convert Buffer to byte array
72 arr = Array.prototype.map.call(data, function (byte) {
76 // run in append mode in 10 byte chunks
78 appendRes = (crc32.table(arr.splice(0, 10), true) >>> 0).toString(16);
81 if (appendRes !== tableRes) {
82 console.log(file + ':', 'FAILED', '-', 'Append mode output not correct');
83 console.log(appendRes, tableRes);
87 console.log(file + ':', 'PASSED');
91 console.log(failed ? 'Tests failed =\'(' : 'All tests passed!! =D');