aboutsummaryrefslogtreecommitdiff
path: root/includes/js/dojox/uuid/tests/uuid.js
blob: 9cc89e0694e64f25e7723b6120aa5599440b32ce (plain)
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
if(!dojo._hasResource["dojox.uuid.tests.uuid"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
dojo._hasResource["dojox.uuid.tests.uuid"] = true;
dojo.provide("dojox.uuid.tests.uuid");
dojo.require("dojox.uuid");
dojo.require("dojox.uuid.Uuid");
dojo.require("dojox.uuid.generateRandomUuid");
dojo.require("dojox.uuid.generateTimeBasedUuid");

dojox.uuid.tests.uuid.checkValidityOfUuidString = function(/*String*/uuidString){
	// summary:
	//		A helper function that's used by the registered test functions
	var NIL_UUID = "00000000-0000-0000-0000-000000000000";
	if (uuidString == NIL_UUID) {
		// We'll consider the Nil UUID to be valid, so now 
		// we can just return, with not further checks.
		return;
	}
	
	doh.assertTrue(uuidString.length == 36); // UUIDs have 36 characters

	var validCharacters = "0123456789abcedfABCDEF-";
	var character;
	var position;
	for(var i = 0; i < 36; ++i){
		character = uuidString.charAt(i);
		position = validCharacters.indexOf(character);
		doh.assertTrue(position != -1); // UUIDs have only valid characters
	}

	var arrayOfParts = uuidString.split("-");
	doh.assertTrue(arrayOfParts.length == 5); // UUIDs have 5 sections separated by 4 hyphens
	doh.assertTrue(arrayOfParts[0].length == 8); // Section 0 has 8 characters
	doh.assertTrue(arrayOfParts[1].length == 4); // Section 1 has 4 characters
	doh.assertTrue(arrayOfParts[2].length == 4); // Section 2 has 4 characters
	doh.assertTrue(arrayOfParts[3].length == 4); // Section 3 has 4 characters
	doh.assertTrue(arrayOfParts[4].length == 12); // Section 4 has 8 characters

	// check to see that the "UUID variant code" starts with the binary bits '10'
	var section3 = arrayOfParts[3];
	var HEX_RADIX = 16;
	var hex3 = parseInt(section3, HEX_RADIX);
	var binaryString = hex3.toString(2);
	// alert("section3 = " + section3 + "\n binaryString = " + binaryString);
	doh.assertTrue(binaryString.length == 16); // section 3 has 16 bits
	doh.assertTrue(binaryString.charAt(0) == '1'); // first bit of section 3 is 1
	doh.assertTrue(binaryString.charAt(1) == '0'); // second bit of section 3 is 0
}

dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString = function(/*String*/uuidString){
	// summary:
	//		A helper function that's used by the registered test functions
	dojox.uuid.tests.uuid.checkValidityOfUuidString(uuidString);
	var arrayOfParts = uuidString.split("-");
	var section2 = arrayOfParts[2];
	doh.assertTrue(section2.charAt(0) == "1"); // Section 2 starts with a 1
}

dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString = function(/*String*/uuidString){
	// summary:
	//		A helper function that's used by the registered test functions
	var arrayOfParts = uuidString.split("-");
	var section4 = arrayOfParts[4];
	var firstChar = section4.charAt(0);
	var HEX_RADIX = 16;
	var hexFirstChar = parseInt(firstChar, HEX_RADIX);
	var binaryString = hexFirstChar.toString(2);
	var firstBit;
	if(binaryString.length == 4){
		firstBit = binaryString.charAt(0);
	}else{
		firstBit = '0';
	}
	doh.assertTrue(firstBit == '1'); // first bit of section 4 is 1
}

doh.register("dojox.uuid.tests.uuid", 
	[
		/*
		function test_uuid_performance(){
			var start = new Date();
			var startMS = start.valueOf();
			var nowMS = startMS;
			var i;
			var now;
			var numTrials = 100000;
		
			while(nowMS == startMS){
				now = new Date();
				nowMS = now.valueOf();
			}
			
			startMS = nowMS;
			for(i = 0; i < numTrials; ++i){
				var a = dojox.uuid.LightweightGenerator.generate();
			}
			now = new Date();
			nowMS = now.valueOf();
			var elapsedMS = nowMS - startMS;
			// dojo.log.debug("created " + numTrials + " UUIDs in " + elapsedMS + " milliseconds");
		},
		*/

		function test_uuid_capitalization(){
			var randomLowercaseString = "3b12f1df-5232-4804-897e-917bf397618a";
			var randomUppercaseString = "3B12F1DF-5232-4804-897E-917BF397618A";
			
			var timebasedLowercaseString = "b4308fb0-86cd-11da-a72b-0800200c9a66";
			var timebasedUppercaseString = "B4308FB0-86CD-11DA-A72B-0800200C9A66";
			
			var uuidRL = new dojox.uuid.Uuid(randomLowercaseString);
			var uuidRU = new dojox.uuid.Uuid(randomUppercaseString);
			
			var uuidTL = new dojox.uuid.Uuid(timebasedLowercaseString);
			var uuidTU = new dojox.uuid.Uuid(timebasedUppercaseString);
			
			doh.assertTrue(uuidRL.isEqual(uuidRU));
			doh.assertTrue(uuidRU.isEqual(uuidRL));
			
			doh.assertTrue(uuidTL.isEqual(uuidTU));
			doh.assertTrue(uuidTU.isEqual(uuidTL));
		},
	
		function test_uuid_constructor(){
			var uuid, uuidToo;
			
			var nilUuid = '00000000-0000-0000-0000-000000000000';
			uuid = new dojox.uuid.Uuid();
			doh.assertTrue(uuid == nilUuid); // 'new dojox.uuid.Uuid()' returns the Nil UUID
			
			var randomUuidString = "3b12f1df-5232-4804-897e-917bf397618a";
			uuid = new dojox.uuid.Uuid(randomUuidString);
			doh.assertTrue(uuid.isValid());
			doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
			doh.assertTrue(uuid.getVersion() == dojox.uuid.version.RANDOM);
			uuidToo = new dojox.uuid.Uuid(new String(randomUuidString));
			doh.assertTrue(uuid.isEqual(uuidToo));	
		
			var timeBasedUuidString = "b4308fb0-86cd-11da-a72b-0800200c9a66";
			uuid = new dojox.uuid.Uuid(timeBasedUuidString);
			doh.assertTrue(uuid.isValid());
			doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
			doh.assertTrue(uuid.getVersion() == dojox.uuid.version.TIME_BASED);
			doh.assertTrue(uuid.getNode() == "0800200c9a66");
			var timestamp = uuid.getTimestamp();
			var date = uuid.getTimestamp(Date);
			var dateString = uuid.getTimestamp(String);
			var hexString = uuid.getTimestamp("hex");
			var now = new Date();
			doh.assertTrue(timestamp.valueOf() == date.valueOf());
			doh.assertTrue(hexString == "1da86cdb4308fb0");
			doh.assertTrue(timestamp < now);
		},
		
		function test_uuid_generators(){
			var generators = [
				dojox.uuid.generateNilUuid,
				dojox.uuid.generateRandomUuid,
				dojox.uuid.generateTimeBasedUuid
			];
			
			for(var i in generators){
				var generator = generators[i];
				var uuidString = generator();

				doh.assertTrue((typeof uuidString) == 'string');
				dojox.uuid.tests.uuid.checkValidityOfUuidString(uuidString);

				var uuid = new dojox.uuid.Uuid(uuidString);
				if(generator != dojox.uuid.generateNilUuid){
					doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
				}

				doh.assertTrue(uuid.isEqual(uuid));
				doh.assertTrue(uuid.compare(uuid) == 0);
				doh.assertTrue(dojox.uuid.Uuid.compare(uuid, uuid) == 0);
				dojox.uuid.tests.uuid.checkValidityOfUuidString(uuid.toString());
				doh.assertTrue(uuid.toString().length == 36);
		
				if(generator != dojox.uuid.generateNilUuid){
					var uuidStringOne = generator();
					var uuidStringTwo = generator();
					doh.assertTrue(uuidStringOne != uuidStringTwo);
					
					dojox.uuid.Uuid.setGenerator(generator);
					var uuidOne = new dojox.uuid.Uuid();
					var uuidTwo = new dojox.uuid.Uuid();
					doh.assertTrue(generator === dojox.uuid.Uuid.getGenerator());
					dojox.uuid.Uuid.setGenerator(null);
					doh.assertTrue(uuidOne != uuidTwo);
					doh.assertTrue(!uuidOne.isEqual(uuidTwo));
					doh.assertTrue(!uuidTwo.isEqual(uuidOne));
					
					var oneVsTwo = dojox.uuid.Uuid.compare(uuidOne, uuidTwo); // either 1 or -1
					var twoVsOne = dojox.uuid.Uuid.compare(uuidTwo, uuidOne); // either -1 or 1
					doh.assertTrue(oneVsTwo + twoVsOne == 0);
					doh.assertTrue(oneVsTwo != 0);
					doh.assertTrue(twoVsOne != 0);
					
					doh.assertTrue(!uuidTwo.isEqual(uuidOne));
				}
				
				if(generator == dojox.uuid.generateRandomUuid){
					doh.assertTrue(uuid.getVersion() == dojox.uuid.version.RANDOM);
				}
				
				if(generator == dojox.uuid.generateTimeBasedUuid){
					dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid.toString());
					doh.assertTrue(uuid.getVersion() == dojox.uuid.version.TIME_BASED);
					doh.assertTrue(dojo.isString(uuid.getNode()));
					doh.assertTrue(uuid.getNode().length == 12);
					var timestamp = uuid.getTimestamp();
					var date = uuid.getTimestamp(Date);
					var dateString = uuid.getTimestamp(String);
					var hexString = uuid.getTimestamp("hex");
					doh.assertTrue(date instanceof Date);
					doh.assertTrue(timestamp.valueOf() == date.valueOf());
					doh.assertTrue(hexString.length == 15);
				}
			}
		},
		
		function test_uuid_nilGenerator(){
			var nilUuidString = '00000000-0000-0000-0000-000000000000';
			var uuidString = dojox.uuid.generateNilUuid();
			doh.assertTrue(uuidString == nilUuidString);
		},
		
		function test_uuid_timeBasedGenerator(){
			var uuid;   // an instance of dojox.uuid.Uuid
			var string; // a simple string literal
			var generator = dojox.uuid.generateTimeBasedUuid;

			var string1 = generator();
			var uuid2    = new dojox.uuid.Uuid(generator());
			var string3 = generator("017bf397618a");         // hardwareNode
			var string4 = generator("f17bf397618a");         // pseudoNode
			var string5 = generator(new String("017BF397618A"));
			
			dojox.uuid.generateTimeBasedUuid.setNode("017bf397618a");
			var string6 = generator(); // the generated UUID has node == "017bf397618a"
			var uuid7   = new dojox.uuid.Uuid(generator()); // the generated UUID has node == "017bf397618a"
			var returnedNode = dojox.uuid.generateTimeBasedUuid.getNode();
			doh.assertTrue(returnedNode == "017bf397618a");
		
			function getNode(string){
				var arrayOfStrings = string.split('-');
				return arrayOfStrings[4];
			}
			dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString(string1);
			dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString(uuid2.toString());
			dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString(string4);
			
			doh.assertTrue(getNode(string3) == "017bf397618a");
			doh.assertTrue(getNode(string4) == "f17bf397618a");
			doh.assertTrue(getNode(string5) == "017bf397618a");
			doh.assertTrue(getNode(string6) == "017bf397618a");
			doh.assertTrue(uuid7.getNode() == "017bf397618a");
			
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string1);
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid2.toString());
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string3);
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string4);
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string5);
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string6);
			dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid7.toString());
		},

		function test_uuid_invalidUuids(){
			var uuidStrings = [];
			uuidStrings.push("Hello world!");                          // not a UUID
			uuidStrings.push("3B12F1DF-5232-1804-897E-917BF39761");    // too short
			uuidStrings.push("3B12F1DF-5232-1804-897E-917BF39761-8A"); // extra '-'
			uuidStrings.push("3B12F1DF-5232-1804-897E917BF39761-8A");  // last '-' in wrong place
			uuidStrings.push("HB12F1DF-5232-1804-897E-917BF397618A");  // "HB12F1DF" is not a hex string
		
			var numberOfFailures = 0;
			for(var i in uuidStrings){
				var uuidString = uuidStrings[i];
				try{
					new dojox.uuid.Uuid(uuidString);
				}catch (e){
					++numberOfFailures;
				}
			}
			doh.assertTrue(numberOfFailures == uuidStrings.length);
		}
	]
);



/*
function test_uuid_get64bitArrayFromFloat(){
	// summary:
	//		This is a test we'd like to be able to run, but we can't run it
	//		because it tests a function which is private in generateTimeBasedUuid
	var x = Math.pow(2, 63) + Math.pow(2, 15);
	var result = dojox.uuid.generateTimeBasedUuid._get64bitArrayFromFloat(x);
	doh.assertTrue(result[0] === 0x8000);
	doh.assertTrue(result[1] === 0x0000);
	doh.assertTrue(result[2] === 0x0000);
	doh.assertTrue(result[3] === 0x8000);

	var date = new Date();
	x = date.valueOf();
	result = dojox.uuid.generateTimeBasedUuid._get64bitArrayFromFloat(x);
	var reconstructedFloat = result[0];
	reconstructedFloat *= 0x10000;
	reconstructedFloat += result[1];
	reconstructedFloat *= 0x10000;
	reconstructedFloat += result[2];
	reconstructedFloat *= 0x10000;
	reconstructedFloat += result[3];

	doh.assertTrue(reconstructedFloat === x);
}

function test_uuid_addTwo64bitArrays(){
	// summary:
	//		This is a test we'd like to be able to run, but we can't run it
	//		because it tests a function which is private in generateTimeBasedUuid
	var a = [0x0000, 0x0000, 0x0000, 0x0001];
	var b = [0x0FFF, 0xFFFF, 0xFFFF, 0xFFFF];
	var result = dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 0x1000);
	doh.assertTrue(result[1] === 0x0000);
	doh.assertTrue(result[2] === 0x0000);
	doh.assertTrue(result[3] === 0x0000);

	a = [0x4000, 0x8000, 0x8000, 0x8000];
	b = [0x8000, 0x8000, 0x8000, 0x8000];
	result = dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 0xC001);
	doh.assertTrue(result[1] === 0x0001);
	doh.assertTrue(result[2] === 0x0001);
	doh.assertTrue(result[3] === 0x0000);

	a = [7, 6, 2, 5];
	b = [1, 0, 3, 4];
	result = dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 8);
	doh.assertTrue(result[1] === 6);
	doh.assertTrue(result[2] === 5);
	doh.assertTrue(result[3] === 9);
}

function test_uuid_multiplyTwo64bitArrays(){
	// summary:
	//		This is a test we'd like to be able to run, but we can't run it
	//		because it tests a function which is private in generateTimeBasedUuid
	var a = [     0, 0x0000, 0x0000, 0x0003];
	var b = [0x1111, 0x1234, 0x0000, 0xFFFF];
	var result = dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 0x3333);
	doh.assertTrue(result[1] === 0x369C);
	doh.assertTrue(result[2] === 0x0002);
	doh.assertTrue(result[3] === 0xFFFD);

	a = [0, 0, 0, 5];
	b = [0, 0, 0, 4];
	result = dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 0);
	doh.assertTrue(result[1] === 0);
	doh.assertTrue(result[2] === 0);
	doh.assertTrue(result[3] === 20);

	a = [0, 0, 2, 5];
	b = [0, 0, 3, 4];
	result = dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
	doh.assertTrue(result[0] === 0);
	doh.assertTrue(result[1] === 6);
	doh.assertTrue(result[2] === 23);
	doh.assertTrue(result[3] === 20);
}
*/

}