29bd7779037ec042988de3f27b2a8bd983bc0de3.svn-base 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. if (!dojo._hasResource["dojox.uuid.tests.uuid"]) { // _hasResource checks added
  2. // by build. Do not use
  3. // _hasResource directly in
  4. // your code.
  5. dojo._hasResource["dojox.uuid.tests.uuid"] = true;
  6. dojo.provide("dojox.uuid.tests.uuid");
  7. dojo.require("dojox.uuid");
  8. dojo.require("dojox.uuid.Uuid");
  9. dojo.require("dojox.uuid.generateRandomUuid");
  10. dojo.require("dojox.uuid.generateTimeBasedUuid");
  11. dojox.uuid.tests.uuid.checkValidityOfUuidString = function(
  12. /* String */ uuidString) {
  13. // summary:
  14. // A helper function that's used by the registered test functions
  15. var NIL_UUID = "00000000-0000-0000-0000-000000000000";
  16. if (uuidString == NIL_UUID) {
  17. // We'll consider the Nil UUID to be valid, so now
  18. // we can just return, with not further checks.
  19. return;
  20. }
  21. doh.assertTrue(uuidString.length == 36); // UUIDs have 36 characters
  22. var validCharacters = "0123456789abcedfABCDEF-";
  23. var character;
  24. var position;
  25. for (var i = 0; i < 36; ++i) {
  26. character = uuidString.charAt(i);
  27. position = validCharacters.indexOf(character);
  28. doh.assertTrue(position != -1); // UUIDs have only valid characters
  29. }
  30. var arrayOfParts = uuidString.split("-");
  31. doh.assertTrue(arrayOfParts.length == 5); // UUIDs have 5 sections
  32. // separated by 4 hyphens
  33. doh.assertTrue(arrayOfParts[0].length == 8); // Section 0 has 8
  34. // characters
  35. doh.assertTrue(arrayOfParts[1].length == 4); // Section 1 has 4
  36. // characters
  37. doh.assertTrue(arrayOfParts[2].length == 4); // Section 2 has 4
  38. // characters
  39. doh.assertTrue(arrayOfParts[3].length == 4); // Section 3 has 4
  40. // characters
  41. doh.assertTrue(arrayOfParts[4].length == 12); // Section 4 has 8
  42. // characters
  43. // check to see that the "UUID variant code" starts with the binary bits
  44. // '10'
  45. var section3 = arrayOfParts[3];
  46. var HEX_RADIX = 16;
  47. var hex3 = parseInt(section3, HEX_RADIX);
  48. var binaryString = hex3.toString(2);
  49. // alert("section3 = " + section3 + "\n binaryString = " +
  50. // binaryString);
  51. doh.assertTrue(binaryString.length == 16); // section 3 has 16 bits
  52. doh.assertTrue(binaryString.charAt(0) == '1'); // first bit of section
  53. // 3 is 1
  54. doh.assertTrue(binaryString.charAt(1) == '0'); // second bit of section
  55. // 3 is 0
  56. }
  57. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString = function(
  58. /* String */ uuidString) {
  59. // summary:
  60. // A helper function that's used by the registered test functions
  61. dojox.uuid.tests.uuid.checkValidityOfUuidString(uuidString);
  62. var arrayOfParts = uuidString.split("-");
  63. var section2 = arrayOfParts[2];
  64. doh.assertTrue(section2.charAt(0) == "1"); // Section 2 starts with a 1
  65. }
  66. dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString = function(
  67. /* String */ uuidString) {
  68. // summary:
  69. // A helper function that's used by the registered test functions
  70. var arrayOfParts = uuidString.split("-");
  71. var section4 = arrayOfParts[4];
  72. var firstChar = section4.charAt(0);
  73. var HEX_RADIX = 16;
  74. var hexFirstChar = parseInt(firstChar, HEX_RADIX);
  75. var binaryString = hexFirstChar.toString(2);
  76. var firstBit;
  77. if (binaryString.length == 4) {
  78. firstBit = binaryString.charAt(0);
  79. } else {
  80. firstBit = '0';
  81. }
  82. doh.assertTrue(firstBit == '1'); // first bit of section 4 is 1
  83. }
  84. doh.register("dojox.uuid.tests.uuid", [
  85. /*
  86. * function test_uuid_performance(){ var start = allGetServerTime(); var
  87. * startMS = start.valueOf(); var nowMS = startMS; var i; var now;
  88. * var numTrials = 100000;
  89. *
  90. * while(nowMS == startMS){ now = allGetServerTime(); nowMS = now.valueOf(); }
  91. *
  92. * startMS = nowMS; for(i = 0; i < numTrials; ++i){ var a =
  93. * dojox.uuid.LightweightGenerator.generate(); } now = allGetServerTime();
  94. * nowMS = now.valueOf(); var elapsedMS = nowMS - startMS; //
  95. * dojo.log.debug("created " + numTrials + " UUIDs in " + elapsedMS + "
  96. * milliseconds"); },
  97. */
  98. function test_uuid_capitalization() {
  99. var randomLowercaseString = "3b12f1df-5232-4804-897e-917bf397618a";
  100. var randomUppercaseString = "3B12F1DF-5232-4804-897E-917BF397618A";
  101. var timebasedLowercaseString = "b4308fb0-86cd-11da-a72b-0800200c9a66";
  102. var timebasedUppercaseString = "B4308FB0-86CD-11DA-A72B-0800200C9A66";
  103. var uuidRL = new dojox.uuid.Uuid(randomLowercaseString);
  104. var uuidRU = new dojox.uuid.Uuid(randomUppercaseString);
  105. var uuidTL = new dojox.uuid.Uuid(timebasedLowercaseString);
  106. var uuidTU = new dojox.uuid.Uuid(timebasedUppercaseString);
  107. doh.assertTrue(uuidRL.isEqual(uuidRU));
  108. doh.assertTrue(uuidRU.isEqual(uuidRL));
  109. doh.assertTrue(uuidTL.isEqual(uuidTU));
  110. doh.assertTrue(uuidTU.isEqual(uuidTL));
  111. },
  112. function test_uuid_constructor() {
  113. var uuid, uuidToo;
  114. var nilUuid = '00000000-0000-0000-0000-000000000000';
  115. uuid = new dojox.uuid.Uuid();
  116. doh.assertTrue(uuid == nilUuid); // 'new dojox.uuid.Uuid()' returns
  117. // the Nil UUID
  118. var randomUuidString = "3b12f1df-5232-4804-897e-917bf397618a";
  119. uuid = new dojox.uuid.Uuid(randomUuidString);
  120. doh.assertTrue(uuid.isValid());
  121. doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
  122. doh.assertTrue(uuid.getVersion() == dojox.uuid.version.RANDOM);
  123. uuidToo = new dojox.uuid.Uuid(new String(randomUuidString));
  124. doh.assertTrue(uuid.isEqual(uuidToo));
  125. var timeBasedUuidString = "b4308fb0-86cd-11da-a72b-0800200c9a66";
  126. uuid = new dojox.uuid.Uuid(timeBasedUuidString);
  127. doh.assertTrue(uuid.isValid());
  128. doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
  129. doh.assertTrue(uuid.getVersion() == dojox.uuid.version.TIME_BASED);
  130. doh.assertTrue(uuid.getNode() == "0800200c9a66");
  131. var timestamp = uuid.getTimestamp();
  132. var date = uuid.getTimestamp(Date);
  133. var dateString = uuid.getTimestamp(String);
  134. var hexString = uuid.getTimestamp("hex");
  135. var now = allGetServerTime();
  136. doh.assertTrue(timestamp.valueOf() == date.valueOf());
  137. doh.assertTrue(hexString == "1da86cdb4308fb0");
  138. doh.assertTrue(timestamp < now);
  139. },
  140. function test_uuid_generators() {
  141. var generators = [dojox.uuid.generateNilUuid,
  142. dojox.uuid.generateRandomUuid, dojox.uuid.generateTimeBasedUuid];
  143. for (var i in generators) {
  144. var generator = generators[i];
  145. var uuidString = generator();
  146. doh.assertTrue((typeof uuidString) == 'string');
  147. dojox.uuid.tests.uuid.checkValidityOfUuidString(uuidString);
  148. var uuid = new dojox.uuid.Uuid(uuidString);
  149. if (generator != dojox.uuid.generateNilUuid) {
  150. doh.assertTrue(uuid.getVariant() == dojox.uuid.variant.DCE);
  151. }
  152. doh.assertTrue(uuid.isEqual(uuid));
  153. doh.assertTrue(uuid.compare(uuid) == 0);
  154. doh.assertTrue(dojox.uuid.Uuid.compare(uuid, uuid) == 0);
  155. dojox.uuid.tests.uuid.checkValidityOfUuidString(uuid.toString());
  156. doh.assertTrue(uuid.toString().length == 36);
  157. if (generator != dojox.uuid.generateNilUuid) {
  158. var uuidStringOne = generator();
  159. var uuidStringTwo = generator();
  160. doh.assertTrue(uuidStringOne != uuidStringTwo);
  161. dojox.uuid.Uuid.setGenerator(generator);
  162. var uuidOne = new dojox.uuid.Uuid();
  163. var uuidTwo = new dojox.uuid.Uuid();
  164. doh.assertTrue(generator === dojox.uuid.Uuid.getGenerator());
  165. dojox.uuid.Uuid.setGenerator(null);
  166. doh.assertTrue(uuidOne != uuidTwo);
  167. doh.assertTrue(!uuidOne.isEqual(uuidTwo));
  168. doh.assertTrue(!uuidTwo.isEqual(uuidOne));
  169. var oneVsTwo = dojox.uuid.Uuid.compare(uuidOne, uuidTwo); // either
  170. // 1 or
  171. // -1
  172. var twoVsOne = dojox.uuid.Uuid.compare(uuidTwo, uuidOne); // either
  173. // -1
  174. // or 1
  175. doh.assertTrue(oneVsTwo + twoVsOne == 0);
  176. doh.assertTrue(oneVsTwo != 0);
  177. doh.assertTrue(twoVsOne != 0);
  178. doh.assertTrue(!uuidTwo.isEqual(uuidOne));
  179. }
  180. if (generator == dojox.uuid.generateRandomUuid) {
  181. doh.assertTrue(uuid.getVersion() == dojox.uuid.version.RANDOM);
  182. }
  183. if (generator == dojox.uuid.generateTimeBasedUuid) {
  184. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid
  185. .toString());
  186. doh
  187. .assertTrue(uuid.getVersion() == dojox.uuid.version.TIME_BASED);
  188. doh.assertTrue(dojo.isString(uuid.getNode()));
  189. doh.assertTrue(uuid.getNode().length == 12);
  190. var timestamp = uuid.getTimestamp();
  191. var date = uuid.getTimestamp(Date);
  192. var dateString = uuid.getTimestamp(String);
  193. var hexString = uuid.getTimestamp("hex");
  194. doh.assertTrue(date instanceof Date);
  195. doh.assertTrue(timestamp.valueOf() == date.valueOf());
  196. doh.assertTrue(hexString.length == 15);
  197. }
  198. }
  199. },
  200. function test_uuid_nilGenerator() {
  201. var nilUuidString = '00000000-0000-0000-0000-000000000000';
  202. var uuidString = dojox.uuid.generateNilUuid();
  203. doh.assertTrue(uuidString == nilUuidString);
  204. },
  205. function test_uuid_timeBasedGenerator() {
  206. var uuid; // an instance of dojox.uuid.Uuid
  207. var string; // a simple string literal
  208. var generator = dojox.uuid.generateTimeBasedUuid;
  209. var string1 = generator();
  210. var uuid2 = new dojox.uuid.Uuid(generator());
  211. var string3 = generator("017bf397618a"); // hardwareNode
  212. var string4 = generator("f17bf397618a"); // pseudoNode
  213. var string5 = generator(new String("017BF397618A"));
  214. dojox.uuid.generateTimeBasedUuid.setNode("017bf397618a");
  215. var string6 = generator(); // the generated UUID has node ==
  216. // "017bf397618a"
  217. var uuid7 = new dojox.uuid.Uuid(generator()); // the generated UUID
  218. // has node ==
  219. // "017bf397618a"
  220. var returnedNode = dojox.uuid.generateTimeBasedUuid.getNode();
  221. doh.assertTrue(returnedNode == "017bf397618a");
  222. function getNode(string) {
  223. var arrayOfStrings = string.split('-');
  224. return arrayOfStrings[4];
  225. }
  226. dojox.uuid.tests.uuid
  227. .checkForPseudoNodeBitInTimeBasedUuidString(string1);
  228. dojox.uuid.tests.uuid.checkForPseudoNodeBitInTimeBasedUuidString(uuid2
  229. .toString());
  230. dojox.uuid.tests.uuid
  231. .checkForPseudoNodeBitInTimeBasedUuidString(string4);
  232. doh.assertTrue(getNode(string3) == "017bf397618a");
  233. doh.assertTrue(getNode(string4) == "f17bf397618a");
  234. doh.assertTrue(getNode(string5) == "017bf397618a");
  235. doh.assertTrue(getNode(string6) == "017bf397618a");
  236. doh.assertTrue(uuid7.getNode() == "017bf397618a");
  237. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string1);
  238. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid2
  239. .toString());
  240. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string3);
  241. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string4);
  242. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string5);
  243. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(string6);
  244. dojox.uuid.tests.uuid.checkValidityOfTimeBasedUuidString(uuid7
  245. .toString());
  246. },
  247. function test_uuid_invalidUuids() {
  248. var uuidStrings = [];
  249. uuidStrings.push("Hello world!"); // not a UUID
  250. uuidStrings.push("3B12F1DF-5232-1804-897E-917BF39761"); // too short
  251. uuidStrings.push("3B12F1DF-5232-1804-897E-917BF39761-8A"); // extra '-'
  252. uuidStrings.push("3B12F1DF-5232-1804-897E917BF39761-8A"); // last '-'
  253. // in wrong
  254. // place
  255. uuidStrings.push("HB12F1DF-5232-1804-897E-917BF397618A"); // "HB12F1DF"
  256. // is not a
  257. // hex
  258. // string
  259. var numberOfFailures = 0;
  260. for (var i in uuidStrings) {
  261. var uuidString = uuidStrings[i];
  262. try {
  263. new dojox.uuid.Uuid(uuidString);
  264. } catch (e) {
  265. ++numberOfFailures;
  266. }
  267. }
  268. doh.assertTrue(numberOfFailures == uuidStrings.length);
  269. }]);
  270. /*
  271. * function test_uuid_get64bitArrayFromFloat(){ // summary: // This is a
  272. * test we'd like to be able to run, but we can't run it // because it tests
  273. * a function which is private in generateTimeBasedUuid var x = Math.pow(2,
  274. * 63) + Math.pow(2, 15); var result =
  275. * dojox.uuid.generateTimeBasedUuid._get64bitArrayFromFloat(x);
  276. * doh.assertTrue(result[0] === 0x8000); doh.assertTrue(result[1] ===
  277. * 0x0000); doh.assertTrue(result[2] === 0x0000); doh.assertTrue(result[3]
  278. * === 0x8000);
  279. *
  280. * var date = allGetServerTime(); x = date.valueOf(); result =
  281. * dojox.uuid.generateTimeBasedUuid._get64bitArrayFromFloat(x); var
  282. * reconstructedFloat = result[0]; reconstructedFloat *= 0x10000;
  283. * reconstructedFloat += result[1]; reconstructedFloat *= 0x10000;
  284. * reconstructedFloat += result[2]; reconstructedFloat *= 0x10000;
  285. * reconstructedFloat += result[3];
  286. *
  287. * doh.assertTrue(reconstructedFloat === x); }
  288. *
  289. * function test_uuid_addTwo64bitArrays(){ // summary: // This is a test
  290. * we'd like to be able to run, but we can't run it // because it tests a
  291. * function which is private in generateTimeBasedUuid var a = [0x0000,
  292. * 0x0000, 0x0000, 0x0001]; var b = [0x0FFF, 0xFFFF, 0xFFFF, 0xFFFF]; var
  293. * result = dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a, b);
  294. * doh.assertTrue(result[0] === 0x1000); doh.assertTrue(result[1] ===
  295. * 0x0000); doh.assertTrue(result[2] === 0x0000); doh.assertTrue(result[3]
  296. * === 0x0000);
  297. *
  298. * a = [0x4000, 0x8000, 0x8000, 0x8000]; b = [0x8000, 0x8000, 0x8000,
  299. * 0x8000]; result = dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a,
  300. * b); doh.assertTrue(result[0] === 0xC001); doh.assertTrue(result[1] ===
  301. * 0x0001); doh.assertTrue(result[2] === 0x0001); doh.assertTrue(result[3]
  302. * === 0x0000);
  303. *
  304. * a = [7, 6, 2, 5]; b = [1, 0, 3, 4]; result =
  305. * dojox.uuid.generateTimeBasedUuid._addTwo64bitArrays(a, b);
  306. * doh.assertTrue(result[0] === 8); doh.assertTrue(result[1] === 6);
  307. * doh.assertTrue(result[2] === 5); doh.assertTrue(result[3] === 9); }
  308. *
  309. * function test_uuid_multiplyTwo64bitArrays(){ // summary: // This is a
  310. * test we'd like to be able to run, but we can't run it // because it tests
  311. * a function which is private in generateTimeBasedUuid var a = [ 0, 0x0000,
  312. * 0x0000, 0x0003]; var b = [0x1111, 0x1234, 0x0000, 0xFFFF]; var result =
  313. * dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
  314. * doh.assertTrue(result[0] === 0x3333); doh.assertTrue(result[1] ===
  315. * 0x369C); doh.assertTrue(result[2] === 0x0002); doh.assertTrue(result[3]
  316. * === 0xFFFD);
  317. *
  318. * a = [0, 0, 0, 5]; b = [0, 0, 0, 4]; result =
  319. * dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
  320. * doh.assertTrue(result[0] === 0); doh.assertTrue(result[1] === 0);
  321. * doh.assertTrue(result[2] === 0); doh.assertTrue(result[3] === 20);
  322. *
  323. * a = [0, 0, 2, 5]; b = [0, 0, 3, 4]; result =
  324. * dojox.uuid.generateTimeBasedUuid._multiplyTwo64bitArrays(a, b);
  325. * doh.assertTrue(result[0] === 0); doh.assertTrue(result[1] === 6);
  326. * doh.assertTrue(result[2] === 23); doh.assertTrue(result[3] === 20); }
  327. */
  328. }