test.js 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. checkFile = process.argv[2];
  15. if (process.argv.length === 4) {
  16. testDir = process.argv[3];
  17. }
  18. if (!checkFile) {
  19. console.log(usage);
  20. return;
  21. }
  22. try {
  23. checkValues = fs.readFileSync(checkFile, 'utf8');
  24. } catch (e) {
  25. console.error('Unable to read ' + checkFile);
  26. return;
  27. }
  28. try {
  29. checkValues = JSON.parse(checkValues);
  30. Object.keys(checkValues).forEach(function (key) {
  31. checkValues[key] = parseInt(checkValues[key]).toString(16);
  32. });
  33. } catch (e) {
  34. console.error('Unable to parse contents of ' + checkFile + ' as JSON.');
  35. console.error(checkValues);
  36. return;
  37. }
  38. fs.readdirSync(testDir).forEach(function (file) {
  39. var data = fs.readFileSync(path.join(testDir, file)),
  40. tableRes = crc32(data),
  41. directRes = crc32(data, true),
  42. appendRes,
  43. arr;
  44. if (tableRes !== directRes) {
  45. console.log(file + ':', 'FAILED', '-', 'Results for table mode and direct mode');
  46. failed = true;
  47. return;
  48. }
  49. if (file in checkValues) {
  50. if (tableRes !== checkValues[file]) {
  51. failed = true;
  52. console.log(file + ':', 'FAILED', '-', 'Results do not match {val = ' + tableRes + ', actual = ' + checkValues[file] + '}');
  53. return;
  54. }
  55. } else {
  56. console.warn('No check value for ' + file);
  57. }
  58. // run append test
  59. // clear any previous data
  60. crc32.table();
  61. // convert Buffer to byte array
  62. arr = Array.prototype.map.call(data, function (byte) {
  63. return byte;
  64. });
  65. // run in append mode in 10 byte chunks
  66. while (arr.length) {
  67. appendRes = (crc32.table(arr.splice(0, 10), true) >>> 0).toString(16);
  68. }
  69. if (appendRes !== tableRes) {
  70. console.log(file + ':', 'FAILED', '-', 'Append mode output not correct');
  71. console.log(appendRes, tableRes);
  72. return;
  73. }
  74. console.log(file + ':', 'PASSED');
  75. });
  76. console.log();
  77. console.log(failed ? 'Tests failed =\'(' : 'All tests passed!! =D');
  78. }());