File: WeakSetTest.cpp

package info (click to toggle)
storm-lang 0.7.4-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 52,004 kB
  • sloc: ansic: 261,462; cpp: 140,405; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (171 lines) | stat: -rw-r--r-- 3,285 bytes parent folder | download | duplicates (4)
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
#include "stdafx.h"
#include "Core/WeakSet.h"
#include "Core/Str.h"
#include "Compiler/Debug.h"

using debug::PtrKey;

static bool setFind(WeakSet<PtrKey> *in, PtrKey *elem) {
	WeakSet<PtrKey>::Iter i = in->iter();
	while (TObject *o = i.next())
		if (o == elem)
			return true;

	return false;
}

static nat setCount(WeakSet<PtrKey> *in) {
	nat c = 0;
	WeakSet<PtrKey>::Iter i = in->iter();
	while (i.next())
		c++;

	return c;
}

BEGIN_TEST(WeakSetTest, Core) {
	Engine &e = gEngine();

	WeakSet<PtrKey> *set = new (e) WeakSet<PtrKey>();

	PtrKey *lone = new (e) PtrKey();
	set->put(lone);

	for (nat i = 0; i < 100; i++)
		set->put(new (e) PtrKey());

	CHECK(set->any());
	CHECK(set->has(lone));
	CHECK(setFind(set, lone));

	// Try to make the gc collect the weak references in the set.
	e.gc.collect();

	for (nat i = 0; i < 1000; i++)
		new (e) PtrKey();

	e.gc.collect();

	CHECK(setFind(set, lone));
	// Should have forgot some of the elements by now.
	CHECK(setCount(set) < 100);

} 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;
}

static bool validate(Array<PtrKey *> *k, WeakSet<PtrKey> *set) {
	for (nat i = 0; i < k->count(); i++) {
		if (!set->has(k->at(i))) {
			PLN(L"Missing object " << (void *)k->at(i));
			return false;
		}
	}

	return true;
}


BEGIN_TEST(WeakSetStress, Stress) {
	Engine &e = gEngine();
	const nat count = 500;
	const nat times = 1000;

	Array<PtrKey *> *k = new (e) Array<PtrKey *>();
	for (nat i = 0; i < count; i++)
		k->push(new (e) PtrKey());

	WeakSet<PtrKey> *set = new (e) WeakSet<PtrKey>();

	for (nat i = 0; i < count; i++) {
		set->put(k->at(i));
	}

	for (nat i = 0; i < times; i++) {
		moveObjects(k);

		// See if we have any garbage.
		{
			nat count = 0;
			WeakSet<PtrKey>::Iter iter = set->iter();
			while (iter.next())
				count++;

			CHECK_GTE(count, k->count());
			CHECK_LTE(count, 5*k->count());
		}

		{
			// Swap some elements.
			for (nat j = count/2; j < count; j++)
				k->at(j) = new (e) PtrKey();
			for (nat j = 0; j < count; j++)
				set->put(k->at(j));
		}

		CHECK(validate(k, set));

		// Otherwise, something is very wrong!
		CHECK_LTE(set->capacity(), 100*count);
	}

} END_TEST


BEGIN_TEST(WeakSetRemoveStress, Stress) {
	Engine &e = gEngine();
	const nat count = 500;
	const nat times = 1000;

	Array<PtrKey *> *k = new (e) Array<PtrKey *>();
	for (nat i = 0; i < count; i++)
		k->push(new (e) PtrKey());

	WeakSet<PtrKey> *set = new (e) WeakSet<PtrKey>();

	for (nat i = 0; i < count; i++) {
		set->put(k->at(i));
	}

	for (nat i = 0; i < times; i++) {
		moveObjects(k);

		// See if we have any garbage.
		{
			nat count = 0;
			WeakSet<PtrKey>::Iter iter = set->iter();
			while (iter.next())
				count++;

			CHECK_GTE(count, k->count());
			CHECK_LTE(count, 5*k->count());
		}

		{
			// Swap some elements.
			for (nat j = count/2; j < count; j++) {
				set->remove(k->at(j));
				k->at(j) = new (e) PtrKey();
			}
			for (nat j = 0; j < count; j++)
				set->put(k->at(j));
		}

		CHECK(validate(k, set));

		// Otherwise, something is very wrong!
		CHECK_LTE(set->capacity(), 100*count);
	}

} END_TEST