3 uuid = require('../uuid');
4 if (!/_rb/.test(uuid._rng.toString())) {
5 throw new Error("should use crypto for node.js");
10 // x-platform log/assert shims
13 function _log(msg, type) {
16 if (typeof(document) != 'undefined') {
17 document.write('<div class="' + type + '">' + msg.replace(/\n/g, '<br />') + '</div>');
19 if (typeof(console) != 'undefined') {
25 console[type](color[type] + msg + color.log);
29 function log(msg) {_log(msg, 'log');}
30 function warn(msg) {_log(msg, 'warn');}
31 function error(msg) {_log(msg, 'error');}
33 function assert(res, msg) {
35 error('FAIL: ' + msg);
45 // Verify ordering of v1 ids created with explicit times
46 var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
48 function compare(name, ids) {
49 ids = ids.map(function(id) {
50 return id.split('-').reverse().join('-');
52 var sorted = ([].concat(ids)).sort();
54 assert(sorted.toString() == ids.toString(), name + ' have expected order');
57 // Verify ordering of v1 ids created using default behavior
58 compare('uuids with current time', [
66 // Verify ordering of v1 ids created with explicit times
67 compare('uuids with time option', [
68 uuid.v1({msecs: TIME - 10*3600*1000}),
69 uuid.v1({msecs: TIME - 1}),
70 uuid.v1({msecs: TIME}),
71 uuid.v1({msecs: TIME + 1}),
72 uuid.v1({msecs: TIME + 28*24*3600*1000})
76 uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
77 'IDs created at same msec are different'
80 // Verify throw if too many ids created
83 uuid.v1({msecs: TIME, nsecs: 10000});
87 assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
89 // Verify clock regression bumps clockseq
90 var uidt = uuid.v1({msecs: TIME});
91 var uidtb = uuid.v1({msecs: TIME - 1});
93 parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
94 'Clock regression by msec increments the clockseq'
97 // Verify clock regression bumps clockseq
98 var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
99 var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
101 parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
102 'Clock regression by nsec increments the clockseq'
105 // Verify explicit options produce expected id
107 msecs: 1321651533573,
110 node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
112 assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
114 // Verify adjacent ids across a msec boundary are 1 time unit apart
115 var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
116 var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
118 var before = u0.split('-')[0], after = u1.split('-')[0];
119 var dt = parseInt(after, 16) - parseInt(before, 16);
120 assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
123 // Test parse/unparse
126 id = '00112233445566778899aabbccddeeff';
127 assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
128 '00112233-4400-0000-0000-000000000000', 'Short parse');
129 assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
130 '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');
142 v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i,
143 v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i
148 // Get %'age an actual value differs from the ideal value
149 function divergence(actual, ideal) {
150 return Math.round(100*100*(actual - ideal)/ideal)/100;
153 function rate(msg, t) {
154 log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second');
157 for (var version in generators) {
158 var counts = {}, max = 0;
159 var generator = generators[version];
160 var format = UUID_FORMAT[version];
162 log('\nSanity check ' + N + ' ' + version + ' uuids');
163 for (var i = 0, ok = 0; i < N; i++) {
165 if (!format.test(id)) {
166 throw Error(id + ' is not a valid UUID string');
169 if (id != uuid.unparse(uuid.parse(id))) {
170 assert(fail, id + ' is not a valid id');
173 // Count digits for our randomness check
174 if (version == 'v4') {
175 var digits = id.replace(/-/g, '').split('');
176 for (var j = digits.length-1; j >= 0; j--) {
178 max = Math.max(max, counts[c] = (counts[c] || 0) + 1);
183 // Check randomness for v4 UUIDs
184 if (version == 'v4') {
185 // Limit that we get worried about randomness. (Purely empirical choice, this!)
186 var limit = 2*100*Math.sqrt(1/N);
188 log('\nChecking v4 randomness. Distribution of Hex Digits (% deviation from ideal)');
190 for (var i = 0; i < 16; i++) {
191 var c = i.toString(16);
192 var bar = '', n = counts[c], p = Math.round(n/max*100|0);
194 // 1-3,5-8, and D-F: 1:16 odds over 30 digits
197 // 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits
198 ideal = N*(1 + 30/16);
199 } else if (i >= 8 && i <= 11) {
200 // 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits
201 ideal = N*(1/4 + 30/16);
203 // Otherwise: 1:16 odds on 30 digits
206 var d = divergence(n, ideal);
208 // Draw bar using UTF squares (just for grins)
209 var s = n/max*50 | 0;
210 while (s--) bar += '=';
212 assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)');
218 for (var version in generators) {
219 log('\nPerformance testing ' + version + ' UUIDs');
220 var generator = generators[version];
221 var buf = new uuid.BufferClass(16);
223 for (var i = 0, t = Date.now(); i < N; i++) generator();
224 rate('uuid.' + version + '()', t);
226 for (var i = 0, t = Date.now(); i < N; i++) generator('binary');
227 rate('uuid.' + version + '(\'binary\')', t);
229 for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf);
230 rate('uuid.' + version + '(\'binary\', buffer)', t);