1 var assert = require('assert');
3 var uuid = require('../');
5 // Verify ordering of v1 ids created with explicit times
6 var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
8 function compare(name, ids) {
9 test(name, function() {
10 // avoid .map for older browsers
11 for (var i=0 ; i<ids.length ; ++i) {
12 ids[i] = ids[i].split('-').reverse().join('-');
15 var sorted = ([].concat(ids)).sort();
17 assert(sorted.toString() == ids.toString(), name + ' have expected order');
21 // Verify ordering of v1 ids created using default behavior
22 compare('uuids with current time', [
30 // Verify ordering of v1 ids created with explicit times
31 compare('uuids with time option', [
32 uuid.v1({msecs: TIME - 10*3600*1000}),
33 uuid.v1({msecs: TIME - 1}),
34 uuid.v1({msecs: TIME}),
35 uuid.v1({msecs: TIME + 1}),
36 uuid.v1({msecs: TIME + 28*24*3600*1000})
39 test('msec', function() {
41 uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
42 'IDs created at same msec are different'
46 test('exception thrown when > 10k ids created in 1ms', function() {
47 // Verify throw if too many ids created
50 uuid.v1({msecs: TIME, nsecs: 10000});
54 assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
57 test('clock regression by msec', function() {
58 // Verify clock regression bumps clockseq
59 var uidt = uuid.v1({msecs: TIME});
60 var uidtb = uuid.v1({msecs: TIME - 1});
62 parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
63 'Clock regression by msec increments the clockseq'
67 test('clock regression by nsec', function() {
68 // Verify clock regression bumps clockseq
69 var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
70 var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
72 parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
73 'Clock regression by nsec increments the clockseq'
77 test('explicit options product expected id', function() {
78 // Verify explicit options produce expected id
83 node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
85 assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
88 test('ids spanning 1ms boundary are 100ns apart', function() {
89 // Verify adjacent ids across a msec boundary are 1 time unit apart
90 var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
91 var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
93 var before = u0.split('-')[0], after = u1.split('-')[0];
94 var dt = parseInt(after, 16) - parseInt(before, 16);
95 assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
98 test('parse/unparse', function() {
99 var id = '00112233445566778899aabbccddeeff';
100 assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
101 '00112233-4400-0000-0000-000000000000', 'Short parse');
102 assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
103 '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');