]> gerrit.simantics Code Review - simantics/district.git/blob - org.simantics.maps.server/node/node-v4.8.0-win-x64/node_modules/npm/node_modules/request/node_modules/node-uuid/test/test.js
Adding integrated tile server
[simantics/district.git] / org.simantics.maps.server / node / node-v4.8.0-win-x64 / node_modules / npm / node_modules / request / node_modules / node-uuid / test / test.js
1 if (!this.uuid) {
2   // node.js
3   uuid = require('../uuid');
4   if (!/_rb/.test(uuid._rng.toString())) {
5     throw new Error("should use crypto for node.js");
6   }
7 }
8
9 //
10 // x-platform log/assert shims
11 //
12
13 function _log(msg, type) {
14   type = type || 'log';
15
16   if (typeof(document) != 'undefined') {
17     document.write('<div class="' + type + '">' + msg.replace(/\n/g, '<br />') + '</div>');
18   }
19   if (typeof(console) != 'undefined') {
20     var color = {
21       log: '\033[39m',
22       warn: '\033[33m',
23       error: '\033[31m'
24     };
25     console[type](color[type] + msg + color.log);
26   }
27 }
28
29 function log(msg) {_log(msg, 'log');}
30 function warn(msg) {_log(msg, 'warn');}
31 function error(msg) {_log(msg, 'error');}
32
33 function assert(res, msg) {
34   if (!res) {
35     error('FAIL: ' + msg);
36   } else {
37     log('Pass: ' + msg);
38   }
39 }
40
41 //
42 // Unit tests
43 //
44
45 // Verify ordering of v1 ids created with explicit times
46 var TIME = 1321644961388; // 2011-11-18 11:36:01.388-08:00
47
48 function compare(name, ids) {
49   ids = ids.map(function(id) {
50     return id.split('-').reverse().join('-');
51   }).sort();
52   var sorted = ([].concat(ids)).sort();
53
54   assert(sorted.toString() == ids.toString(), name + ' have expected order');
55 }
56
57 // Verify ordering of v1 ids created using default behavior
58 compare('uuids with current time', [
59   uuid.v1(),
60   uuid.v1(),
61   uuid.v1(),
62   uuid.v1(),
63   uuid.v1()
64 ]);
65
66 // Verify ordering of v1 ids created with explicit times
67 compare('uuids with time option', [
68   uuid.v1({msecs: TIME - 10*3600*1000}),
69   uuid.v1({msecs: TIME - 1}),
70   uuid.v1({msecs: TIME}),
71   uuid.v1({msecs: TIME + 1}),
72   uuid.v1({msecs: TIME + 28*24*3600*1000})
73 ]);
74
75 assert(
76   uuid.v1({msecs: TIME}) != uuid.v1({msecs: TIME}),
77   'IDs created at same msec are different'
78 );
79
80 // Verify throw if too many ids created
81 var thrown = false;
82 try {
83   uuid.v1({msecs: TIME, nsecs: 10000});
84 } catch (e) {
85   thrown = true;
86 }
87 assert(thrown, 'Exception thrown when > 10K ids created in 1 ms');
88
89 // Verify clock regression bumps clockseq
90 var uidt = uuid.v1({msecs: TIME});
91 var uidtb = uuid.v1({msecs: TIME - 1});
92 assert(
93   parseInt(uidtb.split('-')[3], 16) - parseInt(uidt.split('-')[3], 16) === 1,
94   'Clock regression by msec increments the clockseq'
95 );
96
97 // Verify clock regression bumps clockseq
98 var uidtn = uuid.v1({msecs: TIME, nsecs: 10});
99 var uidtnb = uuid.v1({msecs: TIME, nsecs: 9});
100 assert(
101   parseInt(uidtnb.split('-')[3], 16) - parseInt(uidtn.split('-')[3], 16) === 1,
102   'Clock regression by nsec increments the clockseq'
103 );
104
105 // Verify explicit options produce expected id
106 var id = uuid.v1({
107   msecs: 1321651533573,
108   nsecs: 5432,
109   clockseq: 0x385c,
110   node: [ 0x61, 0xcd, 0x3c, 0xbb, 0x32, 0x10 ]
111 });
112 assert(id == 'd9428888-122b-11e1-b85c-61cd3cbb3210', 'Explicit options produce expected id');
113
114 // Verify adjacent ids across a msec boundary are 1 time unit apart
115 var u0 = uuid.v1({msecs: TIME, nsecs: 9999});
116 var u1 = uuid.v1({msecs: TIME + 1, nsecs: 0});
117
118 var before = u0.split('-')[0], after = u1.split('-')[0];
119 var dt = parseInt(after, 16) - parseInt(before, 16);
120 assert(dt === 1, 'Ids spanning 1ms boundary are 100ns apart');
121
122 //
123 // Test parse/unparse
124 //
125
126 id = '00112233445566778899aabbccddeeff';
127 assert(uuid.unparse(uuid.parse(id.substr(0,10))) ==
128   '00112233-4400-0000-0000-000000000000', 'Short parse');
129 assert(uuid.unparse(uuid.parse('(this is the uuid -> ' + id + id)) ==
130   '00112233-4455-6677-8899-aabbccddeeff', 'Dirty parse');
131
132 //
133 // Perf tests
134 //
135
136 var generators = {
137   v1: uuid.v1,
138   v4: uuid.v4
139 };
140
141 var UUID_FORMAT = {
142   v1: /[0-9a-f]{8}-[0-9a-f]{4}-1[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i,
143   v4: /[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}/i
144 };
145
146 var N = 1e4;
147
148 // Get %'age an actual value differs from the ideal value
149 function divergence(actual, ideal) {
150   return Math.round(100*100*(actual - ideal)/ideal)/100;
151 }
152
153 function rate(msg, t) {
154   log(msg + ': ' + (N / (Date.now() - t) * 1e3 | 0) + ' uuids\/second');
155 }
156
157 for (var version in generators) {
158   var counts = {}, max = 0;
159   var generator = generators[version];
160   var format = UUID_FORMAT[version];
161
162   log('\nSanity check ' + N + ' ' + version + ' uuids');
163   for (var i = 0, ok = 0; i < N; i++) {
164     id = generator();
165     if (!format.test(id)) {
166       throw Error(id + ' is not a valid UUID string');
167     }
168
169     if (id != uuid.unparse(uuid.parse(id))) {
170       assert(fail, id + ' is not a valid id');
171     }
172
173     // Count digits for our randomness check
174     if (version == 'v4') {
175       var digits = id.replace(/-/g, '').split('');
176       for (var j = digits.length-1; j >= 0; j--) {
177         var c = digits[j];
178         max = Math.max(max, counts[c] = (counts[c] || 0) + 1);
179       }
180     }
181   }
182
183   // Check randomness for v4 UUIDs
184   if (version == 'v4') {
185     // Limit that we get worried about randomness. (Purely empirical choice, this!)
186     var limit = 2*100*Math.sqrt(1/N);
187
188     log('\nChecking v4 randomness.  Distribution of Hex Digits (% deviation from ideal)');
189
190     for (var i = 0; i < 16; i++) {
191       var c = i.toString(16);
192       var bar = '', n = counts[c], p = Math.round(n/max*100|0);
193
194       // 1-3,5-8, and D-F: 1:16 odds over 30 digits
195       var ideal = N*30/16;
196       if (i == 4) {
197         // 4: 1:1 odds on 1 digit, plus 1:16 odds on 30 digits
198         ideal = N*(1 + 30/16);
199       } else if (i >= 8 && i <= 11) {
200         // 8-B: 1:4 odds on 1 digit, plus 1:16 odds on 30 digits
201         ideal = N*(1/4 + 30/16);
202       } else {
203         // Otherwise: 1:16 odds on 30 digits
204         ideal = N*30/16;
205       }
206       var d = divergence(n, ideal);
207
208       // Draw bar using UTF squares (just for grins)
209       var s = n/max*50 | 0;
210       while (s--) bar += '=';
211
212       assert(Math.abs(d) < limit, c + ' |' + bar + '| ' + counts[c] + ' (' + d + '% < ' + limit + '%)');
213     }
214   }
215 }
216
217 // Perf tests
218 for (var version in generators) {
219   log('\nPerformance testing ' + version + ' UUIDs');
220   var generator = generators[version];
221   var buf = new uuid.BufferClass(16);
222
223   for (var i = 0, t = Date.now(); i < N; i++) generator();
224   rate('uuid.' + version + '()', t);
225
226   for (var i = 0, t = Date.now(); i < N; i++) generator('binary');
227   rate('uuid.' + version + '(\'binary\')', t);
228
229   for (var i = 0, t = Date.now(); i < N; i++) generator('binary', buf);
230   rate('uuid.' + version + '(\'binary\', buffer)', t);
231 }