test_keys.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  1. /**
  2. * Any copyright is dedicated to the Public Domain.
  3. * http://creativecommons.org/publicdomain/zero/1.0/
  4. */
  5. var testGenerator = testSteps();
  6. function testSteps()
  7. {
  8. const dbname = this.window ? window.location.pathname : "Splendid Test";
  9. const RW = "readwrite"
  10. let c1 = 1;
  11. let c2 = 1;
  12. let openRequest = indexedDB.open(dbname, 1);
  13. openRequest.onerror = errorHandler;
  14. openRequest.onupgradeneeded = grabEventAndContinueHandler;
  15. openRequest.onsuccess = unexpectedSuccessHandler;
  16. let event = yield undefined;
  17. let db = event.target.result;
  18. let trans = event.target.transaction;
  19. // Create test stores
  20. let store = db.createObjectStore("store");
  21. // Test simple inserts
  22. var keys = [
  23. -1/0,
  24. -1.7e308,
  25. -10000,
  26. -2,
  27. -1.5,
  28. -1,
  29. -1.00001e-200,
  30. -1e-200,
  31. 0,
  32. 1e-200,
  33. 1.00001e-200,
  34. 1,
  35. 2,
  36. 10000,
  37. 1.7e308,
  38. 1/0,
  39. new Date("1750-01-02"),
  40. new Date("1800-12-31T12:34:56.001"),
  41. new Date(-1000),
  42. new Date(-10),
  43. new Date(-1),
  44. new Date(0),
  45. new Date(1),
  46. new Date(2),
  47. new Date(1000),
  48. new Date("1971-01-01"),
  49. new Date("1971-01-01T01:01:01Z"),
  50. new Date("1971-01-01T01:01:01.001Z"),
  51. new Date("1971-01-01T01:01:01.01Z"),
  52. new Date("1971-01-01T01:01:01.1Z"),
  53. new Date("1980-02-02"),
  54. new Date("3333-03-19T03:33:33.333"),
  55. "",
  56. "\x00",
  57. "\x00\x00",
  58. "\x00\x01",
  59. "\x01",
  60. "\x02",
  61. "\x03",
  62. "\x04",
  63. "\x07",
  64. "\x08",
  65. "\x0F",
  66. "\x10",
  67. "\x1F",
  68. "\x20",
  69. "01234",
  70. "\x3F",
  71. "\x40",
  72. "A",
  73. "A\x00",
  74. "A1",
  75. "ZZZZ",
  76. "a",
  77. "a\x00",
  78. "aa",
  79. "azz",
  80. "}",
  81. "\x7E",
  82. "\x7F",
  83. "\x80",
  84. "\xFF",
  85. "\u0100",
  86. "\u01FF",
  87. "\u0200",
  88. "\u03FF",
  89. "\u0400",
  90. "\u07FF",
  91. "\u0800",
  92. "\u0FFF",
  93. "\u1000",
  94. "\u1FFF",
  95. "\u2000",
  96. "\u3FFF",
  97. "\u4000",
  98. "\u7FFF",
  99. "\u8000",
  100. "\uD800",
  101. "\uD800a",
  102. "\uD800\uDC01",
  103. "\uDBFF",
  104. "\uDC00",
  105. "\uDFFF\uD800",
  106. "\uFFFE",
  107. "\uFFFF",
  108. "\uFFFF\x00",
  109. "\uFFFFZZZ",
  110. [],
  111. [-1/0],
  112. [-1],
  113. [0],
  114. [1],
  115. [1, "a"],
  116. [1, []],
  117. [1, [""]],
  118. [2, 3],
  119. [2, 3.0000000000001],
  120. [12, [[]]],
  121. [12, [[[]]]],
  122. [12, [[[""]]]],
  123. [12, [[["foo"]]]],
  124. [12, [[[[[3]]]]]],
  125. [12, [[[[[[3]]]]]]],
  126. [new Date(-1)],
  127. [new Date(1)],
  128. [""],
  129. ["", [[]]],
  130. ["", [[[]]]],
  131. ["abc"],
  132. ["abc", "def"],
  133. ["abc\x00"],
  134. ["abc\x00", "\x00\x01"],
  135. ["abc\x00", "\x00def"],
  136. ["abc\x00\x00def"],
  137. ["x", [[]]],
  138. ["x", [[[]]]],
  139. [[]],
  140. [[],"foo"],
  141. [[],[]],
  142. [[[]]],
  143. [[[]], []],
  144. [[[]], [[]]],
  145. [[[]], [[1]]],
  146. [[[]], [[[]]]],
  147. [[[1]]],
  148. [[[[]], []]],
  149. ];
  150. for (var i = 0; i < keys.length; ++i) {
  151. let keyI = keys[i];
  152. is(indexedDB.cmp(keyI, keyI), 0, i + " compared to self");
  153. function doCompare(keyI) {
  154. for (var j = i-1; j >= i-10 && j >= 0; --j) {
  155. is(indexedDB.cmp(keyI, keys[j]), 1, i + " compared to " + j);
  156. is(indexedDB.cmp(keys[j], keyI), -1, j + " compared to " + i);
  157. }
  158. }
  159. doCompare(keyI);
  160. store.add(i, keyI).onsuccess = function(e) {
  161. is(indexedDB.cmp(e.target.result, keyI), 0,
  162. "Returned key should cmp as equal");
  163. ok(compareKeys(e.target.result, keyI),
  164. "Returned key should actually be equal");
  165. };
  166. // Test that -0 compares the same as 0
  167. if (keyI === 0) {
  168. doCompare(-0);
  169. let req = store.add(i, -0);
  170. req.addEventListener("error", new ExpectError("ConstraintError", true));
  171. req.onsuccess = unexpectedSuccessHandler;
  172. yield undefined;
  173. }
  174. else if (Array.isArray(keyI) && keyI.length === 1 && keyI[0] === 0) {
  175. doCompare([-0]);
  176. let req = store.add(i, [-0]);
  177. req.addEventListener("error", new ExpectError("ConstraintError", true));
  178. req.onsuccess = unexpectedSuccessHandler;
  179. yield undefined;
  180. }
  181. }
  182. store.openCursor().onsuccess = grabEventAndContinueHandler;
  183. for (i = 0; i < keys.length; ++i) {
  184. event = yield undefined;
  185. let cursor = event.target.result;
  186. is(indexedDB.cmp(cursor.key, keys[i]), 0,
  187. "Read back key should cmp as equal");
  188. ok(compareKeys(cursor.key, keys[i]),
  189. "Read back key should actually be equal");
  190. is(cursor.value, i, "Stored with right value");
  191. cursor.continue();
  192. }
  193. event = yield undefined;
  194. is(event.target.result, null, "no more results expected");
  195. var nan = 0/0;
  196. var invalidKeys = [
  197. nan,
  198. undefined,
  199. null,
  200. /x/,
  201. {},
  202. new Date(NaN),
  203. new Date("foopy"),
  204. [nan],
  205. [undefined],
  206. [null],
  207. [/x/],
  208. [{}],
  209. [new Date(NaN)],
  210. [1, nan],
  211. [1, undefined],
  212. [1, null],
  213. [1, /x/],
  214. [1, {}],
  215. [1, [nan]],
  216. [1, [undefined]],
  217. [1, [null]],
  218. [1, [/x/]],
  219. [1, [{}]],
  220. ];
  221. for (i = 0; i < invalidKeys.length; ++i) {
  222. try {
  223. indexedDB.cmp(invalidKeys[i], 1);
  224. ok(false, "didn't throw");
  225. }
  226. catch(ex) {
  227. ok(ex instanceof DOMException, "Threw DOMException");
  228. is(ex.name, "DataError", "Threw right DOMException");
  229. is(ex.code, 0, "Threw with right code");
  230. }
  231. try {
  232. indexedDB.cmp(1, invalidKeys[i]);
  233. ok(false, "didn't throw2");
  234. }
  235. catch(ex) {
  236. ok(ex instanceof DOMException, "Threw DOMException2");
  237. is(ex.name, "DataError", "Threw right DOMException2");
  238. is(ex.code, 0, "Threw with right code2");
  239. }
  240. try {
  241. store.put(1, invalidKeys[i]);
  242. ok(false, "didn't throw3");
  243. }
  244. catch(ex) {
  245. ok(ex instanceof DOMException, "Threw DOMException3");
  246. is(ex.name, "DataError", "Threw right DOMException3");
  247. is(ex.code, 0, "Threw with right code3");
  248. }
  249. }
  250. openRequest.onsuccess = grabEventAndContinueHandler;
  251. yield undefined;
  252. finishTest();
  253. yield undefined;
  254. }