Initial commit
[yaffs-website] / node_modules / uuid / test / test.js
1 var assert = require('assert');
2
3 var uuid = require('../');
4
5 // Verify ordering of v1 ids created with explicit times
6 var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
7
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('-');
13     }
14     ids = ids.sort();
15     var sorted = ([].concat(ids)).sort();
16
17     assert(sorted.toString() == ids.toString(), name + ' have expected order');
18   });
19 }
20
21 // Verify ordering of v1 ids created using default behavior
22 compare('uuids with current time', [
23   uuid.v1(),
24   uuid.v1(),
25   uuid.v1(),
26   uuid.v1(),
27   uuid.v1()
28 ]);
29
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})
37 ]);
38
39 test('msec', function() {
40   assert(
41     uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
42     'IDs created at same msec are different'
43   );
44 });
45
46 test('exception thrown when > 10k ids created in 1ms', function() {
47   // Verify throw if too many ids created
48   var thrown = false;
49   try {
50     uuid.v1({msecs: TIME, nsecs: 10000});
51   } catch (e) {
52     thrown = true;
53   }
54   assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
55 });
56
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});
61   assert(
62     parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
63     'Clock regression by msec increments the clockseq'
64   );
65 });
66
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});
71   assert(
72     parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
73     'Clock regression by nsec increments the clockseq'
74   );
75 });
76
77 test('explicit options product expected id', function() {
78   // Verify explicit options produce expected id
79   var id = uuid.v1({
80     msecs: 1321651533573,
81     nsecs: 5432,
82     clockseq: 0x385c,
83     node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
84   });
85   assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
86 });
87
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});
92
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');
96 });
97
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');
104 });
105