You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

89 lines
2.1 KiB

  1. var crc32 = require('..');
  2. var test = require('tap').test;
  3. test('simple crc32 is no problem', function (t) {
  4. var input = Buffer('hey sup bros');
  5. var expected = Buffer([0x47, 0xfa, 0x55, 0x70]);
  6. t.same(crc32(input), expected);
  7. t.end();
  8. });
  9. test('another simple one', function (t) {
  10. var input = Buffer('IEND');
  11. var expected = Buffer([0xae, 0x42, 0x60, 0x82]);
  12. t.same(crc32(input), expected);
  13. t.end();
  14. });
  15. test('slightly more complex', function (t) {
  16. var input = Buffer([0x00, 0x00, 0x00]);
  17. var expected = Buffer([0xff, 0x41, 0xd9, 0x12]);
  18. t.same(crc32(input), expected);
  19. t.end();
  20. });
  21. test('complex crc32 gets calculated like a champ', function (t) {
  22. var input = Buffer('शीर्षक');
  23. var expected = Buffer([0x17, 0xb8, 0xaf, 0xf1]);
  24. t.same(crc32(input), expected);
  25. t.end();
  26. });
  27. test('casts to buffer if necessary', function (t) {
  28. var input = 'शीर्षक';
  29. var expected = Buffer([0x17, 0xb8, 0xaf, 0xf1]);
  30. t.same(crc32(input), expected);
  31. t.end();
  32. });
  33. test('can do signed', function (t) {
  34. var input = 'ham sandwich';
  35. var expected = -1891873021;
  36. t.same(crc32.signed(input), expected);
  37. t.end();
  38. });
  39. test('can do unsigned', function (t) {
  40. var input = 'bear sandwich';
  41. var expected = 3711466352;
  42. t.same(crc32.unsigned(input), expected);
  43. t.end();
  44. });
  45. test('simple crc32 in append mode', function (t) {
  46. var input = [Buffer('hey'), Buffer(' '), Buffer('sup'), Buffer(' '), Buffer('bros')];
  47. var expected = Buffer([0x47, 0xfa, 0x55, 0x70]);
  48. for (var crc = 0, i = 0; i < input.length; i++) {
  49. crc = crc32(input[i], crc);
  50. }
  51. t.same(crc, expected);
  52. t.end();
  53. });
  54. test('can do signed in append mode', function (t) {
  55. var input1 = 'ham';
  56. var input2 = ' ';
  57. var input3 = 'sandwich';
  58. var expected = -1891873021;
  59. var crc = crc32.signed(input1);
  60. crc = crc32.signed(input2, crc);
  61. crc = crc32.signed(input3, crc);
  62. t.same(crc, expected);
  63. t.end();
  64. });
  65. test('can do unsigned in append mode', function (t) {
  66. var input1 = 'bear san';
  67. var input2 = 'dwich';
  68. var expected = 3711466352;
  69. var crc = crc32.unsigned(input1);
  70. crc = crc32.unsigned(input2, crc);
  71. t.same(crc, expected);
  72. t.end();
  73. });