summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/IndexedDB/keygenerator-explicit.html
blob: e2d65e14b6e6ca594cbaa52f6b141960cf8f5f39 (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
<!DOCTYPE html>
<meta charset="utf-8">
<title>Key Generator behavior with explicit keys generator overflow</title>
<link rel=help href="https://w3c.github.io/IndexedDB/#key-generator-construct">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="resources/support.js"></script>
<script>

function big_key_test(key, description) {
  indexeddb_test(
    (t, db) => {
      assert_equals(indexedDB.cmp(key, key), 0, 'Key is valid');

      db.createObjectStore('store', {autoIncrement: true});
    },
    (t, db) => {
      const tx = db.transaction('store', 'readwrite', {durability: 'relaxed'});
      const store = tx.objectStore('store');
      const value = 0;
      let request;

      request = store.put(value);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 1,
                      'Key generator should initially be 1');
      });

      request = store.put(value);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 2,
                      'Key generator should increment');
      });

      request = store.put(value, 1000);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 1000,
                      'Explicit key should be used');
      });

      request = store.put(value);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 1001,
                      'Key generator should have updated');
      });

      request = store.put(value, key);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, key,
                      'Explicit key should be used');
      });

      if (key >= 0) {
        // Large positive values will max out the key generator, so it
        // can no longer produce keys.
        request = store.put(value);
        request.onsuccess = t.unreached_func('put should fail');
        request.onerror = t.step_func(e => {
          e.preventDefault();
          assert_equals(e.target.error.name, 'ConstraintError',
                        'Key generator should have returned failure');
        });
      } else {
        // Large negative values are always lower than the key generator's
        // current number, so have no effect on the generator.
        request = store.put(value);
        request.onerror = t.unreached_func('put should succeed');
        request.onsuccess = t.step_func(e => {
          assert_equals(e.target.result, 1002,
                        'Key generator should have updated');
        });
      }

      request = store.put(value, 2000);
      request.onerror = t.unreached_func('put should succeed');
      request.onsuccess = t.step_func(e => {
        assert_equals(e.target.result, 2000,
                      'Explicit key should be used');
      });

      tx.onabort = t.step_func(() => {
        assert_unreached(`Transaction aborted: ${tx.error.message}`);
      });
      tx.oncomplete = t.step_func(() => { t.done(); });
    },
    description);
}

[
  {
    key: Number.MAX_SAFE_INTEGER + 1,
    description: '53 bits'
  },
  {
    key: Math.pow(2, 60),
    description: 'greater than 53 bits, less than 64 bits'
  },
  {
    key: -Math.pow(2, 60),
    description: 'greater than 53 bits, less than 64 bits (negative)'
  },
  {
    key: Math.pow(2, 63),
    description: '63 bits'
  },
  {
    key: -Math.pow(2, 63),
    description: '63 bits (negative)'
  },
  {
    key: Math.pow(2, 64),
    description: '64 bits'
  },
  {
    key: -Math.pow(2, 64),
    description: '64 bits (negative)'
  },
  {
    key: Math.pow(2, 70),
    description: 'greater than 64 bits, but still finite'
  },
  {
    key: -Math.pow(2, 70),
    description: 'greater than 64 bits, but still finite (negative)'
  },
  {
    key: Infinity,
    description: 'equal to Infinity'
  },
  {
    key: -Infinity,
    description: 'equal to -Infinity'
  }
].forEach(function(testCase) {
  big_key_test(testCase.key,
               `Key generator vs. explicit key ${testCase.description}`);
});



</script>