react / wstein / node_modules / jest-cli / node_modules / jsdom / node_modules / request / node_modules / node-uuid / test / test.js
81146 viewsif (!this.uuid) {1// node.js2uuid = require('../uuid');3}45//6// x-platform log/assert shims7//89function _log(msg, type) {10type = type || 'log';1112if (typeof(document) != 'undefined') {13document.write('<div class="' + type + '">' + msg.replace(/\n/g, '<br />') + '</div>');14}15if (typeof(console) != 'undefined') {16var color = {17log: '\033[39m',18warn: '\033[33m',19error: '\033[31m'20};21console[type](color[type] + msg + color.log);22}23}2425function log(msg) {_log(msg, 'log');}26function warn(msg) {_log(msg, 'warn');}27function error(msg) {_log(msg, 'error');}2829function assert(res, msg) {30if (!res) {31error('FAIL: ' + msg);32} else {33log('Pass: ' + msg);34}35}3637//38// Unit tests39//4041// Verify ordering of v1 ids created with explicit times42var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:004344function compare(name, ids) {45ids = ids.map(function(id) {46return id.split('-').reverse().join('-');47}).sort();48var sorted = ([].concat(ids)).sort();4950assert(sorted.toString() == ids.toString(), name + ' have expected order');51}5253// Verify ordering of v1 ids created using default behavior54compare('uuids with current time', [55uuid.v1(),56uuid.v1(),57uuid.v1(),58uuid.v1(),59uuid.v1()60]);6162// Verify ordering of v1 ids created with explicit times63compare('uuids with time option', [64uuid.v1({msecs: TIME - 10*3600*1000}),65uuid.v1({msecs: TIME - 1}),66uuid.v1({msecs: TIME}),67uuid.v1({msecs: TIME + 1}),68uuid.v1({msecs: TIME + 28*24*3600*1000})69]);7071assert(72uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),73'IDs created at same msec are different'74);7576// Verify throw if too many ids created77var thrown = false;78try {79uuid.v1({msecs: TIME, nsecs: 10000});80} catch (e) {81thrown = true;82}83assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');8485// Verify clock regression bumps clockseq86var uidt = uuid.v1({msecs: TIME});87var uidtb = uuid.v1({msecs: TIME - 1});88assert(89parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,90'Clock regression by msec increments the clockseq'91);9293// Verify clock regression bumps clockseq94var uidtn = uuid.v1({msecs: TIME, nsecs: 10});95var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});96assert(97parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,98'Clock regression by nsec increments the clockseq'99);100101// Verify explicit options produce expected id102var id = uuid.v1({103msecs: 1321651533573,104nsecs: 5432,105clockseq: 0x385c,106node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]107});108assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');109110// Verify adjacent ids across a msec boundary are 1 time unit apart111var u0 = uuid.v1({msecs: TIME, nsecs: 9999});112var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});113114var before = u0.split('-')[0], after = u1.split('-')[0];115var dt = parseInt(after, 16) - parseInt(before, 16);116assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');117118//119// Test parse/unparse120//121122id = '00112233445566778899aabbccddeeff';123assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==124'00112233-4400-0000-0000-000000000000', 'Short parse');125assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==126'00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');127128//129// Perf tests130//131132var generators = {133v1: uuid.v1,134v4: uuid.v4135};136137var UUID_FORMAT = {138v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i,139v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i140};141142var N = 1e4;143144// Get %'age an actual value differs from the ideal value145function divergence(actual, ideal) {146return Math.round(100*100*(actual - ideal)/ideal)/100;147}148149function rate(msg, t) {150log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second');151}152153for (var version in generators) {154var counts = {}, max = 0;155var generator = generators[version];156var format = UUID_FORMAT[version];157158log('\nSanity check ' + N + ' ' + version + ' uuids');159for (var i = 0, ok = 0; i < N; i++) {160id = generator();161if (!format.test(id)) {162throw Error(id + ' is not a valid UUID string');163}164165if (id != uuid.unparse(uuid.parse(id))) {166assert(fail, id + ' is not a valid id');167}168169// Count digits for our randomness check170if (version == 'v4') {171var digits = id.replace(/-/g, '').split('');172for (var j = digits.length-1; j >= 0; j--) {173var c = digits[j];174max = Math.max(max, counts[c] = (counts[c] || 0) + 1);175}176}177}178179// Check randomness for v4 UUIDs180if (version == 'v4') {181// Limit that we get worried about randomness. (Purely empirical choice, this!)182var limit = 2*100*Math.sqrt(1/N);183184log('\nChecking v4 randomness. Distribution of Hex Digits (% deviation from ideal)');185186for (var i = 0; i < 16; i++) {187var c = i.toString(16);188var bar = '', n = counts[c], p = Math.round(n/max*100|0);189190// 1-3,5-8, and D-F: 1:16 odds over 30 digits191var ideal = N*30/16;192if (i == 4) {193// 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits194ideal = N*(1 + 30/16);195} else if (i >= 8 && i <= 11) {196// 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits197ideal = N*(1/4 + 30/16);198} else {199// Otherwise: 1:16 odds on 30 digits200ideal = N*30/16;201}202var d = divergence(n, ideal);203204// Draw bar using UTF squares (just for grins)205var s = n/max*50 | 0;206while (s--) bar += '=';207208assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)');209}210}211}212213// Perf tests214for (var version in generators) {215log('\nPerformance testing ' + version + ' UUIDs');216var generator = generators[version];217var buf = new uuid.BufferClass(16);218219for (var i = 0, t = Date.now(); i < N; i++) generator();220rate('uuid.' + version + '()', t);221222for (var i = 0, t = Date.now(); i < N; i++) generator('binary');223rate('uuid.' + version + '(\'binary\')', t);224225for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf);226rate('uuid.' + version + '(\'binary\', buffer)', t);227}228229230