Blame view
node_modules/uuid/test/test.js
2.77 KB
f7563de62
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
var assert = require('assert'); var uuid = require('../'); // Verify ordering of v1 ids created with explicit times var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00 function compare(name, ids) { test(name, function() { // avoid .map for older browsers for (var i=0 ; i<ids.length ; ++i) { ids[i] = ids[i].split('-').reverse().join('-'); } ids = ids.sort(); var sorted = ([].concat(ids)).sort(); assert(sorted.toString() == ids.toString(), name + ' have expected order'); }); } // Verify ordering of v1 ids created using default behavior compare('uuids with current time', [ uuid.v1(), uuid.v1(), uuid.v1(), uuid.v1(), uuid.v1() ]); // Verify ordering of v1 ids created with explicit times compare('uuids with time option', [ uuid.v1({msecs: TIME - 10*3600*1000}), uuid.v1({msecs: TIME - 1}), uuid.v1({msecs: TIME}), uuid.v1({msecs: TIME + 1}), uuid.v1({msecs: TIME + 28*24*3600*1000}) ]); test('msec', function() { assert( uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}), 'IDs created at same msec are different' ); }); test('exception thrown when > 10k ids created in 1ms', function() { // Verify throw if too many ids created var thrown = false; try { uuid.v1({msecs: TIME, nsecs: 10000}); } catch (e) { thrown = true; } assert(thrown, 'Exception thrown when > 10K ids created in 1 ms'); }); test('clock regression by msec', function() { // Verify clock regression bumps clockseq var uidt = uuid.v1({msecs: TIME}); var uidtb = uuid.v1({msecs: TIME - 1}); assert( parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1, 'Clock regression by msec increments the clockseq' ); }); test('clock regression by nsec', function() { // Verify clock regression bumps clockseq var uidtn = uuid.v1({msecs: TIME, nsecs: 10}); var uidtnb = uuid.v1({msecs: TIME, nsecs: 9}); assert( parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1, 'Clock regression by nsec increments the clockseq' ); }); test('explicit options product expected id', function() { // Verify explicit options produce expected id var id = uuid.v1({ msecs: 1321651533573, nsecs: 5432, clockseq: 0x385c, node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ] }); assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id'); }); test('ids spanning 1ms boundary are 100ns apart', function() { // Verify adjacent ids across a msec boundary are 1 time unit apart var u0 = uuid.v1({msecs: TIME, nsecs: 9999}); var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0}); var before = u0.split('-')[0], after = u1.split('-')[0]; var dt = parseInt(after, 16) - parseInt(before, 16); assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart'); }); |