test.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. if (!this.uuid) {
  2. // node.js
  3. uuid = require('../uuid');
  4. if (!/_rb/.test(uuid._rng.toString())) {
  5. throw new Error("should use crypto for node.js");
  6. }
  7. }
  8. //
  9. // x-platform log/assert shims
  10. //
  11. function _log(msg, type) {
  12. type = type || 'log';
  13. if (typeof(document) != 'undefined') {
  14. document.write('<div class="' + type + '">' + msg.replace(/\n/g, '<br />') + '</div>');
  15. }
  16. if (typeof(console) != 'undefined') {
  17. var color = {
  18. log: '\033[39m',
  19. warn: '\033[33m',
  20. error: '\033[31m'
  21. };
  22. console[type](color[type] + msg + color.log);
  23. }
  24. }
  25. function log(msg) {_log(msg, 'log');}
  26. function warn(msg) {_log(msg, 'warn');}
  27. function error(msg) {_log(msg, 'error');}
  28. function assert(res, msg) {
  29. if (!res) {
  30. error('FAIL: ' + msg);
  31. } else {
  32. log('Pass: ' + msg);
  33. }
  34. }
  35. //
  36. // Unit tests
  37. //
  38. // Verify ordering of v1 ids created with explicit times
  39. var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
  40. function compare(name, ids) {
  41. ids = ids.map(function(id) {
  42. return id.split('-').reverse().join('-');
  43. }).sort();
  44. var sorted = ([].concat(ids)).sort();
  45. assert(sorted.toString() == ids.toString(), name + ' have expected order');
  46. }
  47. // Verify ordering of v1 ids created using default behavior
  48. compare('uuids with current time', [
  49. uuid.v1(),
  50. uuid.v1(),
  51. uuid.v1(),
  52. uuid.v1(),
  53. uuid.v1()
  54. ]);
  55. // Verify ordering of v1 ids created with explicit times
  56. compare('uuids with time option', [
  57. uuid.v1({msecs: TIME - 10*3600*1000}),
  58. uuid.v1({msecs: TIME - 1}),
  59. uuid.v1({msecs: TIME}),
  60. uuid.v1({msecs: TIME + 1}),
  61. uuid.v1({msecs: TIME + 28*24*3600*1000})
  62. ]);
  63. assert(
  64. uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
  65. 'IDs created at same msec are different'
  66. );
  67. // Verify throw if too many ids created
  68. var thrown = false;
  69. try {
  70. uuid.v1({msecs: TIME, nsecs: 10000});
  71. } catch (e) {
  72. thrown = true;
  73. }
  74. assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
  75. // Verify clock regression bumps clockseq
  76. var uidt = uuid.v1({msecs: TIME});
  77. var uidtb = uuid.v1({msecs: TIME - 1});
  78. assert(
  79. parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
  80. 'Clock regression by msec increments the clockseq'
  81. );
  82. // Verify clock regression bumps clockseq
  83. var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
  84. var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
  85. assert(
  86. parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
  87. 'Clock regression by nsec increments the clockseq'
  88. );
  89. // Verify explicit options produce expected id
  90. var id = uuid.v1({
  91. msecs: 1321651533573,
  92. nsecs: 5432,
  93. clockseq: 0x385c,
  94. node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
  95. });
  96. assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
  97. // Verify adjacent ids across a msec boundary are 1 time unit apart
  98. var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
  99. var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
  100. var before = u0.split('-')[0], after = u1.split('-')[0];
  101. var dt = parseInt(after, 16) - parseInt(before, 16);
  102. assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
  103. //
  104. // Test parse/unparse
  105. //
  106. id = '00112233445566778899aabbccddeeff';
  107. assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
  108. '00112233-4400-0000-0000-000000000000', 'Short parse');
  109. assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
  110. '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');
  111. //
  112. // Perf tests
  113. //
  114. var generators = {
  115. v1: uuid.v1,
  116. v4: uuid.v4
  117. };
  118. var UUID_FORMAT = {
  119. v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i,
  120. v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i
  121. };
  122. var N = 1e4;
  123. // Get %'age an actual value differs from the ideal value
  124. function divergence(actual, ideal) {
  125. return Math.round(100*100*(actual - ideal)/ideal)/100;
  126. }
  127. function rate(msg, t) {
  128. log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second');
  129. }
  130. for (var version in generators) {
  131. var counts = {}, max = 0;
  132. var generator = generators[version];
  133. var format = UUID_FORMAT[version];
  134. log('\nSanity check ' + N + ' ' + version + ' uuids');
  135. for (var i = 0, ok = 0; i < N; i++) {
  136. id = generator();
  137. if (!format.test(id)) {
  138. throw Error(id + ' is not a valid UUID string');
  139. }
  140. if (id != uuid.unparse(uuid.parse(id))) {
  141. assert(fail, id + ' is not a valid id');
  142. }
  143. // Count digits for our randomness check
  144. if (version == 'v4') {
  145. var digits = id.replace(/-/g, '').split('');
  146. for (var j = digits.length-1; j >= 0; j--) {
  147. var c = digits[j];
  148. max = Math.max(max, counts[c] = (counts[c] || 0) + 1);
  149. }
  150. }
  151. }
  152. // Check randomness for v4 UUIDs
  153. if (version == 'v4') {
  154. // Limit that we get worried about randomness. (Purely empirical choice, this!)
  155. var limit = 2*100*Math.sqrt(1/N);
  156. log('\nChecking v4 randomness. Distribution of Hex Digits (% deviation from ideal)');
  157. for (var i = 0; i < 16; i++) {
  158. var c = i.toString(16);
  159. var bar = '', n = counts[c], p = Math.round(n/max*100|0);
  160. // 1-3,5-8, and D-F: 1:16 odds over 30 digits
  161. var ideal = N*30/16;
  162. if (i == 4) {
  163. // 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits
  164. ideal = N*(1 + 30/16);
  165. } else if (i >= 8 && i <= 11) {
  166. // 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits
  167. ideal = N*(1/4 + 30/16);
  168. } else {
  169. // Otherwise: 1:16 odds on 30 digits
  170. ideal = N*30/16;
  171. }
  172. var d = divergence(n, ideal);
  173. // Draw bar using UTF squares (just for grins)
  174. var s = n/max*50 | 0;
  175. while (s--) bar += '=';
  176. assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)');
  177. }
  178. }
  179. }
  180. // Perf tests
  181. for (var version in generators) {
  182. log('\nPerformance testing ' + version + ' UUIDs');
  183. var generator = generators[version];
  184. var buf = new uuid.BufferClass(16);
  185. for (var i = 0, t = Date.now(); i < N; i++) generator();
  186. rate('uuid.' + version + '()', t);
  187. for (var i = 0, t = Date.now(); i < N; i++) generator('binary');
  188. rate('uuid.' + version + '(\'binary\')', t);
  189. for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf);
  190. rate('uuid.' + version + '(\'binary\', buffer)', t);
  191. }