UNPKG

1.86 kBJavaScriptView Raw
1var uuid = require('../uuid'),
2 assert = require('assert');
3
4function compare(name, ids) {
5 ids = ids.map(function(id) {
6 return id.split('-').reverse().join('-');
7 }).sort();
8 var sorted = ([].concat(ids)).sort();
9
10 console.log('Verify ' + name + ' have expected order.');
11 assert.equal(sorted.toString(), ids.toString(), '... failed!');
12 console.log('... verified');
13}
14
15// Verify ordering of ids created using default behavior
16compare('uuids with current time', [
17 uuid.v1(),
18 uuid.v1(),
19 uuid.v1(),
20 uuid.v1(),
21 uuid.v1()
22]);
23
24// Verify ordering of ids created with explicit times
25var t = 1321644961388; // "Fri Nov 18 2011 11:36:01.388 GMT-0800 (PST)"
26compare('uuids with time option', [
27 uuid.v1({msecs: t - 10*3600*1000}),
28 uuid.v1({msecs: t - 1}),
29 uuid.v1({msecs: t}),
30 uuid.v1({msecs: t + 1}),
31 uuid.v1({msecs: t + 28*24*3600*1000}),
32]);
33
34console.log('Verify explicit options produce expected id');
35var id = uuid.v1({
36 msecs: 1321651533573,
37 nsecs: 5432,
38 clockseq: 0x385c,
39 node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
40});
41assert.equal(id, 'd9428888-122b-11e1-b85c-61cd3cbb3210', '... failed!');
42console.log('... verified');
43
44// Check that there is exactly 1 tick between lastUUID and firstUUID of the
45// next millisecond interval (this test is sloppy since it fails if time_mid
46// or time_hi change when we changed the time by one ms. If we want to include
47// that case, we cannot use parseInt() since our integers become
48// > 32bit):
49console.log('Verify 1ns separation between adjacent uuids');
50var u0 = uuid.v1({msecs: t, nsecs: 9999});
51var u1 = uuid.v1({msecs: t + 1});
52
53var before = u0.split('-')[0], after = u1.split('-')[0];
54var dt = parseInt(after, 16) - parseInt(before, 16);
55assert.strictEqual(dt, 1, 'Not exactly one tick between last and nextFirst');
56console.log('... verified');
57
58console.log('\nTests complete');