/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
using IntMap = js::HashMap<uint32_t, uint32_t, js::DefaultHasher<uint32_t>,
js::SystemAllocPolicy>; using IntSet =
js::HashSet<uint32_t, js::DefaultHasher<uint32_t>, js::SystemAllocPolicy>;
/* * The rekeying test as conducted by adding only keys masked with 0x0000FFFF * that are unique. We rekey by shifting left 16 bits.
*/ #ifdef FUZZ const size_t TestSize = 0x0000FFFF / 2; const size_t TestIterations = SIZE_MAX; #else const size_t TestSize = 10000; const size_t TestIterations = 10; #endif
staticbool MapsAreEqual(IntMap& am, IntMap& bm) { bool equal = true; if (am.count() != bm.count()) {
equal = false;
fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(),
bm.count());
} for (auto iter = am.iter(); !iter.done(); iter.next()) { if (!bm.has(iter.get().key())) {
equal = false;
fprintf(stderr, "B does not have %x which is in A\n", iter.get().key());
}
} for (auto iter = bm.iter(); !iter.done(); iter.next()) { if (!am.has(iter.get().key())) {
equal = false;
fprintf(stderr, "A does not have %x which is in B\n", iter.get().key());
}
} return equal;
}
staticbool SetsAreEqual(IntSet& am, IntSet& bm) { bool equal = true; if (am.count() != bm.count()) {
equal = false;
fprintf(stderr, "A.count() == %u and B.count() == %u\n", am.count(),
bm.count());
} for (auto iter = am.iter(); !iter.done(); iter.next()) { if (!bm.has(iter.get())) {
equal = false;
fprintf(stderr, "B does not have %x which is in A\n", iter.get());
}
} for (auto iter = bm.iter(); !iter.done(); iter.next()) { if (!am.has(iter.get())) {
equal = false;
fprintf(stderr, "A does not have %x which is in B\n", iter.get());
}
} return equal;
}
staticbool AddLowKeys(IntMap* am, IntMap* bm, int seed) {
size_t i = 0;
srand(seed); while (i < TestSize) {
uint32_t n = rand() & 0x0000FFFF; if (!am->has(n)) { if (bm->has(n)) { returnfalse;
}
staticbool AddLowKeys(IntSet* as, IntSet* bs, int seed) {
size_t i = 0;
srand(seed); while (i < TestSize) {
uint32_t n = rand() & 0x0000FFFF; if (!as->has(n)) { if (bs->has(n)) { returnfalse;
} if (!as->putNew(n) || !bs->putNew(n)) { returnfalse;
}
i++;
}
} returntrue;
}
template <class NewKeyFunction> staticbool SlowRekey(IntMap* m) {
IntMap tmp;
for (auto iter = m->iter(); !iter.done(); iter.next()) { if (NewKeyFunction::shouldBeRemoved(iter.get().key())) { continue;
}
uint32_t hi = NewKeyFunction::rekey(iter.get().key()); if (tmp.has(hi)) { returnfalse;
} if (!tmp.putNew(hi, iter.get().value())) { returnfalse;
}
}
m->clear(); for (auto iter = tmp.iter(); !iter.done(); iter.next()) { if (!m->putNew(iter.get().key(), iter.get().value())) { returnfalse;
}
}
for (auto iter = s->iter(); !iter.done(); iter.next()) { if (NewKeyFunction::shouldBeRemoved(iter.get())) { continue;
}
uint32_t hi = NewKeyFunction::rekey(iter.get()); if (tmp.has(hi)) { returnfalse;
} if (!tmp.putNew(hi)) { returnfalse;
}
}
s->clear(); for (auto iter = tmp.iter(); !iter.done(); iter.next()) { if (!s->putNew(iter.get())) { returnfalse;
}
}
returntrue;
}
BEGIN_TEST(testHashRekeyManual) {
IntMap am, bm; for (size_t i = 0; i < TestIterations; ++i) { #ifdef FUZZ
fprintf(stderr, "map1: %lu\n", i); #endif
CHECK(AddLowKeys(&am, &bm, i));
CHECK(MapsAreEqual(am, bm));
for (auto iter = am.modIter(); !iter.done(); iter.next()) {
uint32_t tmp = LowToHigh::rekey(iter.get().key()); if (tmp != iter.get().key()) {
iter.rekey(tmp);
}
}
CHECK(SlowRekey<LowToHigh>(&bm));
BEGIN_TEST(testHashLazyStorage) { // The following code depends on the current capacity computation, which // could change in the future.
uint32_t defaultCap = 32;
uint32_t minCap = 4;
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.