test.js 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. var assert = require('assert');
  2. var uuid = require('../');
  3. // Verify ordering of v1 ids created with explicit times
  4. var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
  5. function compare(name, ids) {
  6. test(name, function() {
  7. // avoid .map for older browsers
  8. for (var i=0 ; i<ids.length ; ++i) {
  9. ids[i] = ids[i].split('-').reverse().join('-');
  10. }
  11. ids = ids.sort();
  12. var sorted = ([].concat(ids)).sort();
  13. assert(sorted.toString() == ids.toString(), name + ' have expected order');
  14. });
  15. }
  16. // Verify ordering of v1 ids created using default behavior
  17. compare('uuids with current time', [
  18. uuid.v1(),
  19. uuid.v1(),
  20. uuid.v1(),
  21. uuid.v1(),
  22. uuid.v1()
  23. ]);
  24. // Verify ordering of v1 ids created with explicit times
  25. compare('uuids with time option', [
  26. uuid.v1({msecs: TIME - 10*3600*1000}),
  27. uuid.v1({msecs: TIME - 1}),
  28. uuid.v1({msecs: TIME}),
  29. uuid.v1({msecs: TIME + 1}),
  30. uuid.v1({msecs: TIME + 28*24*3600*1000})
  31. ]);
  32. test('msec', function() {
  33. assert(
  34. uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
  35. 'IDs created at same msec are different'
  36. );
  37. });
  38. test('exception thrown when > 10k ids created in 1ms', function() {
  39. // Verify throw if too many ids created
  40. var thrown = false;
  41. try {
  42. uuid.v1({msecs: TIME, nsecs: 10000});
  43. } catch (e) {
  44. thrown = true;
  45. }
  46. assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
  47. });
  48. test('clock regression by msec', function() {
  49. // Verify clock regression bumps clockseq
  50. var uidt = uuid.v1({msecs: TIME});
  51. var uidtb = uuid.v1({msecs: TIME - 1});
  52. assert(
  53. parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
  54. 'Clock regression by msec increments the clockseq'
  55. );
  56. });
  57. test('clock regression by nsec', function() {
  58. // Verify clock regression bumps clockseq
  59. var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
  60. var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
  61. assert(
  62. parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
  63. 'Clock regression by nsec increments the clockseq'
  64. );
  65. });
  66. test('explicit options product expected id', function() {
  67. // Verify explicit options produce expected id
  68. var id = uuid.v1({
  69. msecs: 1321651533573,
  70. nsecs: 5432,
  71. clockseq: 0x385c,
  72. node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
  73. });
  74. assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
  75. });
  76. test('ids spanning 1ms boundary are 100ns apart', function() {
  77. // Verify adjacent ids across a msec boundary are 1 time unit apart
  78. var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
  79. var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
  80. var before = u0.split('-')[0], after = u1.split('-')[0];
  81. var dt = parseInt(after, 16) - parseInt(before, 16);
  82. assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
  83. });
  84. test('parse/unparse', function() {
  85. var id = '00112233445566778899aabbccddeeff';
  86. assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
  87. '00112233-4400-0000-0000-000000000000', 'Short parse');
  88. assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
  89. '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');
  90. });