Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #include "js/HashTable.h" |
michael@0 | 6 | #include "jsapi-tests/tests.h" |
michael@0 | 7 | |
michael@0 | 8 | //#define FUZZ |
michael@0 | 9 | |
michael@0 | 10 | typedef js::HashMap<uint32_t, uint32_t, js::DefaultHasher<uint32_t>, js::SystemAllocPolicy> IntMap; |
michael@0 | 11 | typedef js::HashSet<uint32_t, js::DefaultHasher<uint32_t>, js::SystemAllocPolicy> IntSet; |
michael@0 | 12 | |
michael@0 | 13 | /* |
michael@0 | 14 | * The rekeying test as conducted by adding only keys masked with 0x0000FFFF |
michael@0 | 15 | * that are unique. We rekey by shifting left 16 bits. |
michael@0 | 16 | */ |
michael@0 | 17 | #ifdef FUZZ |
michael@0 | 18 | const size_t TestSize = 0x0000FFFF / 2; |
michael@0 | 19 | const size_t TestIterations = SIZE_MAX; |
michael@0 | 20 | #else |
michael@0 | 21 | const size_t TestSize = 10000; |
michael@0 | 22 | const size_t TestIterations = 10; |
michael@0 | 23 | #endif |
michael@0 | 24 | |
michael@0 | 25 | JS_STATIC_ASSERT(TestSize <= 0x0000FFFF / 2); |
michael@0 | 26 | |
michael@0 | 27 | struct LowToHigh |
michael@0 | 28 | { |
michael@0 | 29 | static uint32_t rekey(uint32_t initial) { |
michael@0 | 30 | if (initial > uint32_t(0x0000FFFF)) |
michael@0 | 31 | return initial; |
michael@0 | 32 | return initial << 16; |
michael@0 | 33 | } |
michael@0 | 34 | |
michael@0 | 35 | static bool shouldBeRemoved(uint32_t initial) { |
michael@0 | 36 | return false; |
michael@0 | 37 | } |
michael@0 | 38 | }; |
michael@0 | 39 | |
michael@0 | 40 | struct LowToHighWithRemoval |
michael@0 | 41 | { |
michael@0 | 42 | static uint32_t rekey(uint32_t initial) { |
michael@0 | 43 | if (initial > uint32_t(0x0000FFFF)) |
michael@0 | 44 | return initial; |
michael@0 | 45 | return initial << 16; |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | static bool shouldBeRemoved(uint32_t initial) { |
michael@0 | 49 | if (initial >= 0x00010000) |
michael@0 | 50 | return (initial >> 16) % 2 == 0; |
michael@0 | 51 | return initial % 2 == 0; |
michael@0 | 52 | } |
michael@0 | 53 | }; |
michael@0 | 54 | |
michael@0 | 55 | static bool |
michael@0 | 56 | MapsAreEqual(IntMap &am, IntMap &bm) |
michael@0 | 57 | { |
michael@0 | 58 | bool equal = true; |
michael@0 | 59 | if (am.count() != bm.count()) { |
michael@0 | 60 | equal = false; |
michael@0 | 61 | fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(), bm.count()); |
michael@0 | 62 | } |
michael@0 | 63 | for (IntMap::Range r = am.all(); !r.empty(); r.popFront()) { |
michael@0 | 64 | if (!bm.has(r.front().key())) { |
michael@0 | 65 | equal = false; |
michael@0 | 66 | fprintf(stderr, "B does not have %x which is in A\n", r.front().key()); |
michael@0 | 67 | } |
michael@0 | 68 | } |
michael@0 | 69 | for (IntMap::Range r = bm.all(); !r.empty(); r.popFront()) { |
michael@0 | 70 | if (!am.has(r.front().key())) { |
michael@0 | 71 | equal = false; |
michael@0 | 72 | fprintf(stderr, "A does not have %x which is in B\n", r.front().key()); |
michael@0 | 73 | } |
michael@0 | 74 | } |
michael@0 | 75 | return equal; |
michael@0 | 76 | } |
michael@0 | 77 | |
michael@0 | 78 | static bool |
michael@0 | 79 | SetsAreEqual(IntSet &am, IntSet &bm) |
michael@0 | 80 | { |
michael@0 | 81 | bool equal = true; |
michael@0 | 82 | if (am.count() != bm.count()) { |
michael@0 | 83 | equal = false; |
michael@0 | 84 | fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(), bm.count()); |
michael@0 | 85 | } |
michael@0 | 86 | for (IntSet::Range r = am.all(); !r.empty(); r.popFront()) { |
michael@0 | 87 | if (!bm.has(r.front())) { |
michael@0 | 88 | equal = false; |
michael@0 | 89 | fprintf(stderr, "B does not have %x which is in A\n", r.front()); |
michael@0 | 90 | } |
michael@0 | 91 | } |
michael@0 | 92 | for (IntSet::Range r = bm.all(); !r.empty(); r.popFront()) { |
michael@0 | 93 | if (!am.has(r.front())) { |
michael@0 | 94 | equal = false; |
michael@0 | 95 | fprintf(stderr, "A does not have %x which is in B\n", r.front()); |
michael@0 | 96 | } |
michael@0 | 97 | } |
michael@0 | 98 | return equal; |
michael@0 | 99 | } |
michael@0 | 100 | |
michael@0 | 101 | static bool |
michael@0 | 102 | AddLowKeys(IntMap *am, IntMap *bm, int seed) |
michael@0 | 103 | { |
michael@0 | 104 | size_t i = 0; |
michael@0 | 105 | srand(seed); |
michael@0 | 106 | while (i < TestSize) { |
michael@0 | 107 | uint32_t n = rand() & 0x0000FFFF; |
michael@0 | 108 | if (!am->has(n)) { |
michael@0 | 109 | if (bm->has(n)) |
michael@0 | 110 | return false; |
michael@0 | 111 | am->putNew(n, n); |
michael@0 | 112 | bm->putNew(n, n); |
michael@0 | 113 | i++; |
michael@0 | 114 | } |
michael@0 | 115 | } |
michael@0 | 116 | return true; |
michael@0 | 117 | } |
michael@0 | 118 | |
michael@0 | 119 | static bool |
michael@0 | 120 | AddLowKeys(IntSet *as, IntSet *bs, int seed) |
michael@0 | 121 | { |
michael@0 | 122 | size_t i = 0; |
michael@0 | 123 | srand(seed); |
michael@0 | 124 | while (i < TestSize) { |
michael@0 | 125 | uint32_t n = rand() & 0x0000FFFF; |
michael@0 | 126 | if (!as->has(n)) { |
michael@0 | 127 | if (bs->has(n)) |
michael@0 | 128 | return false; |
michael@0 | 129 | as->putNew(n); |
michael@0 | 130 | bs->putNew(n); |
michael@0 | 131 | i++; |
michael@0 | 132 | } |
michael@0 | 133 | } |
michael@0 | 134 | return true; |
michael@0 | 135 | } |
michael@0 | 136 | |
michael@0 | 137 | template <class NewKeyFunction> |
michael@0 | 138 | static bool |
michael@0 | 139 | SlowRekey(IntMap *m) { |
michael@0 | 140 | IntMap tmp; |
michael@0 | 141 | tmp.init(); |
michael@0 | 142 | |
michael@0 | 143 | for (IntMap::Range r = m->all(); !r.empty(); r.popFront()) { |
michael@0 | 144 | if (NewKeyFunction::shouldBeRemoved(r.front().key())) |
michael@0 | 145 | continue; |
michael@0 | 146 | uint32_t hi = NewKeyFunction::rekey(r.front().key()); |
michael@0 | 147 | if (tmp.has(hi)) |
michael@0 | 148 | return false; |
michael@0 | 149 | tmp.putNew(hi, r.front().value()); |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | m->clear(); |
michael@0 | 153 | for (IntMap::Range r = tmp.all(); !r.empty(); r.popFront()) { |
michael@0 | 154 | m->putNew(r.front().key(), r.front().value()); |
michael@0 | 155 | } |
michael@0 | 156 | |
michael@0 | 157 | return true; |
michael@0 | 158 | } |
michael@0 | 159 | |
michael@0 | 160 | template <class NewKeyFunction> |
michael@0 | 161 | static bool |
michael@0 | 162 | SlowRekey(IntSet *s) { |
michael@0 | 163 | IntSet tmp; |
michael@0 | 164 | tmp.init(); |
michael@0 | 165 | |
michael@0 | 166 | for (IntSet::Range r = s->all(); !r.empty(); r.popFront()) { |
michael@0 | 167 | if (NewKeyFunction::shouldBeRemoved(r.front())) |
michael@0 | 168 | continue; |
michael@0 | 169 | uint32_t hi = NewKeyFunction::rekey(r.front()); |
michael@0 | 170 | if (tmp.has(hi)) |
michael@0 | 171 | return false; |
michael@0 | 172 | tmp.putNew(hi); |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | s->clear(); |
michael@0 | 176 | for (IntSet::Range r = tmp.all(); !r.empty(); r.popFront()) { |
michael@0 | 177 | s->putNew(r.front()); |
michael@0 | 178 | } |
michael@0 | 179 | |
michael@0 | 180 | return true; |
michael@0 | 181 | } |
michael@0 | 182 | |
michael@0 | 183 | BEGIN_TEST(testHashRekeyManual) |
michael@0 | 184 | { |
michael@0 | 185 | IntMap am, bm; |
michael@0 | 186 | am.init(); |
michael@0 | 187 | bm.init(); |
michael@0 | 188 | for (size_t i = 0; i < TestIterations; ++i) { |
michael@0 | 189 | #ifdef FUZZ |
michael@0 | 190 | fprintf(stderr, "map1: %lu\n", i); |
michael@0 | 191 | #endif |
michael@0 | 192 | CHECK(AddLowKeys(&am, &bm, i)); |
michael@0 | 193 | CHECK(MapsAreEqual(am, bm)); |
michael@0 | 194 | |
michael@0 | 195 | for (IntMap::Enum e(am); !e.empty(); e.popFront()) { |
michael@0 | 196 | uint32_t tmp = LowToHigh::rekey(e.front().key()); |
michael@0 | 197 | if (tmp != e.front().key()) |
michael@0 | 198 | e.rekeyFront(tmp); |
michael@0 | 199 | } |
michael@0 | 200 | CHECK(SlowRekey<LowToHigh>(&bm)); |
michael@0 | 201 | |
michael@0 | 202 | CHECK(MapsAreEqual(am, bm)); |
michael@0 | 203 | am.clear(); |
michael@0 | 204 | bm.clear(); |
michael@0 | 205 | } |
michael@0 | 206 | |
michael@0 | 207 | IntSet as, bs; |
michael@0 | 208 | as.init(); |
michael@0 | 209 | bs.init(); |
michael@0 | 210 | for (size_t i = 0; i < TestIterations; ++i) { |
michael@0 | 211 | #ifdef FUZZ |
michael@0 | 212 | fprintf(stderr, "set1: %lu\n", i); |
michael@0 | 213 | #endif |
michael@0 | 214 | CHECK(AddLowKeys(&as, &bs, i)); |
michael@0 | 215 | CHECK(SetsAreEqual(as, bs)); |
michael@0 | 216 | |
michael@0 | 217 | for (IntSet::Enum e(as); !e.empty(); e.popFront()) { |
michael@0 | 218 | uint32_t tmp = LowToHigh::rekey(e.front()); |
michael@0 | 219 | if (tmp != e.front()) |
michael@0 | 220 | e.rekeyFront(tmp); |
michael@0 | 221 | } |
michael@0 | 222 | CHECK(SlowRekey<LowToHigh>(&bs)); |
michael@0 | 223 | |
michael@0 | 224 | CHECK(SetsAreEqual(as, bs)); |
michael@0 | 225 | as.clear(); |
michael@0 | 226 | bs.clear(); |
michael@0 | 227 | } |
michael@0 | 228 | |
michael@0 | 229 | return true; |
michael@0 | 230 | } |
michael@0 | 231 | END_TEST(testHashRekeyManual) |
michael@0 | 232 | |
michael@0 | 233 | BEGIN_TEST(testHashRekeyManualRemoval) |
michael@0 | 234 | { |
michael@0 | 235 | IntMap am, bm; |
michael@0 | 236 | am.init(); |
michael@0 | 237 | bm.init(); |
michael@0 | 238 | for (size_t i = 0; i < TestIterations; ++i) { |
michael@0 | 239 | #ifdef FUZZ |
michael@0 | 240 | fprintf(stderr, "map2: %lu\n", i); |
michael@0 | 241 | #endif |
michael@0 | 242 | CHECK(AddLowKeys(&am, &bm, i)); |
michael@0 | 243 | CHECK(MapsAreEqual(am, bm)); |
michael@0 | 244 | |
michael@0 | 245 | for (IntMap::Enum e(am); !e.empty(); e.popFront()) { |
michael@0 | 246 | if (LowToHighWithRemoval::shouldBeRemoved(e.front().key())) { |
michael@0 | 247 | e.removeFront(); |
michael@0 | 248 | } else { |
michael@0 | 249 | uint32_t tmp = LowToHighWithRemoval::rekey(e.front().key()); |
michael@0 | 250 | if (tmp != e.front().key()) |
michael@0 | 251 | e.rekeyFront(tmp); |
michael@0 | 252 | } |
michael@0 | 253 | } |
michael@0 | 254 | CHECK(SlowRekey<LowToHighWithRemoval>(&bm)); |
michael@0 | 255 | |
michael@0 | 256 | CHECK(MapsAreEqual(am, bm)); |
michael@0 | 257 | am.clear(); |
michael@0 | 258 | bm.clear(); |
michael@0 | 259 | } |
michael@0 | 260 | |
michael@0 | 261 | IntSet as, bs; |
michael@0 | 262 | as.init(); |
michael@0 | 263 | bs.init(); |
michael@0 | 264 | for (size_t i = 0; i < TestIterations; ++i) { |
michael@0 | 265 | #ifdef FUZZ |
michael@0 | 266 | fprintf(stderr, "set1: %lu\n", i); |
michael@0 | 267 | #endif |
michael@0 | 268 | CHECK(AddLowKeys(&as, &bs, i)); |
michael@0 | 269 | CHECK(SetsAreEqual(as, bs)); |
michael@0 | 270 | |
michael@0 | 271 | for (IntSet::Enum e(as); !e.empty(); e.popFront()) { |
michael@0 | 272 | if (LowToHighWithRemoval::shouldBeRemoved(e.front())) { |
michael@0 | 273 | e.removeFront(); |
michael@0 | 274 | } else { |
michael@0 | 275 | uint32_t tmp = LowToHighWithRemoval::rekey(e.front()); |
michael@0 | 276 | if (tmp != e.front()) |
michael@0 | 277 | e.rekeyFront(tmp); |
michael@0 | 278 | } |
michael@0 | 279 | } |
michael@0 | 280 | CHECK(SlowRekey<LowToHighWithRemoval>(&bs)); |
michael@0 | 281 | |
michael@0 | 282 | CHECK(SetsAreEqual(as, bs)); |
michael@0 | 283 | as.clear(); |
michael@0 | 284 | bs.clear(); |
michael@0 | 285 | } |
michael@0 | 286 | |
michael@0 | 287 | return true; |
michael@0 | 288 | } |
michael@0 | 289 | END_TEST(testHashRekeyManualRemoval) |