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
|
#include "stdafx.h"
#include "Core/Set.h"
#include "Core/Str.h"
#include "Core/GcBitset.h"
#include "Compiler/Debug.h"
using debug::PtrKey;
BEGIN_TEST(SetTest, Core) {
Engine &e = gEngine();
// Basic operation:
{
Set<Str *> *set = new (e) Set<Str *>();
set->put(new (e) Str(L"A"));
set->put(new (e) Str(L"B"));
set->put(new (e) Str(L"A"));
set->put(new (e) Str(L"E"));
CHECK_EQ(set->count(), 3);
CHECK(set->has(new (e) Str(L"A")));
set->remove(new (e) Str(L"A"));
CHECK_EQ(set->count(), 2);
CHECK(set->has(new (e) Str(L"B")));
CHECK(set->has(new (e) Str(L"E")));
CHECK(!set->has(new (e) Str(L"A")));
}
// TODO: More tests here!
} END_TEST
static bool moveObjects(Array<PtrKey *> *k) {
// Try a few times...
for (nat i = 0; i < 10; i++) {
gEngine().gc.collect();
for (nat i = 0; i < k->count(); i++)
if (k->at(i)->moved())
return true;
}
return false;
}
BEGIN_TEST(SetTestMove, Core) {
// Do we handle moving objects properly?
Engine &e = gEngine();
const nat count = 10;
// Store these in arrays so the GC can move them properly.
Array<PtrKey *> *k = new (e) Array<PtrKey *>();
for (nat i = 0; i < count; i++) {
k->push(new (e) PtrKey());
// Make it less likely that we pin all objects.
runtime::allocArray<Byte>(e, &byteArrayType, 1000);
}
Set<PtrKey *> *set = new (e) Set<PtrKey *>();
for (nat i = 0; i < count; i++) {
set->put(k->at(i));
k->at(i)->reset();
}
// Try to force objects to be moved around!
// Note: if we're using a non-moving collector, the following tests will fail.
CHECK(moveObjects(k));
// Try to find all objects in the set:
for (nat i = 0; i < count; i++) {
CHECK_EQ(set->get(k->at(i)), k->at(i));
}
// Move the objects once more, so we can try 'remove' properly.
CHECK(moveObjects(k));
// Try to remove an object.
for (nat i = 0; i < count; i++) {
set->remove(k->at(i));
CHECK_EQ(set->count(), count - i - 1);
// Validate the others.
for (nat j = 0; j < count; j++) {
if (j <= i) {
CHECK(!set->has(k->at(j)));
} else {
CHECK(set->has(k->at(j)));
}
}
}
} END_TEST
BEGIN_TEST(GcBitsetTest, Core) {
Engine &e = gEngine();
GcBitset *s = allocBitset(e, 10);
CHECK_EQ(s->count(), 10);
for (nat i = 0; i < s->count(); i++)
CHECK(!s->has(i));
s->set(5, true);
CHECK(s->has(5));
CHECK(!s->has(4));
CHECK(!s->has(6));
} END_TEST
|