Skip to content

Commit 2df7386

Browse files
committed
Use constants for the number of test values.
1 parent d55fec9 commit 2df7386

File tree

5 files changed

+83
-70
lines changed

5 files changed

+83
-70
lines changed

test/test-avl-tree.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2828
#include "avl-tree.h"
2929
#include "compare-int.h"
3030

31-
int test_array[1000];
31+
#define NUM_TEST_VALUES 1000
32+
33+
int test_array[NUM_TEST_VALUES];
3234

3335
#if 0
3436
/* Tree print function - useful for debugging. */
@@ -161,7 +163,7 @@ AVLTree *create_tree(void)
161163

162164
tree = avl_tree_new((AVLTreeCompareFunc) int_compare);
163165

164-
for (i=0; i<1000; ++i) {
166+
for (i=0; i<NUM_TEST_VALUES; ++i) {
165167
test_array[i] = i;
166168
avl_tree_insert(tree, &test_array[i], &test_array[i]);
167169
}
@@ -203,7 +205,7 @@ void test_avl_tree_insert_lookup(void)
203205

204206
tree = avl_tree_new((AVLTreeCompareFunc) int_compare);
205207

206-
for (i=0; i<1000; ++i) {
208+
for (i=0; i<NUM_TEST_VALUES; ++i) {
207209
test_array[i] = i;
208210
avl_tree_insert(tree, &test_array[i], &test_array[i]);
209211

@@ -215,7 +217,7 @@ void test_avl_tree_insert_lookup(void)
215217

216218
/* Check that all values can be read back again */
217219

218-
for (i=0; i<1000; ++i) {
220+
for (i=0; i<NUM_TEST_VALUES; ++i) {
219221
node = avl_tree_lookup_node(tree, &i);
220222
assert(node != NULL);
221223
value = avl_tree_node_key(node);
@@ -228,7 +230,7 @@ void test_avl_tree_insert_lookup(void)
228230

229231
i = -1;
230232
assert(avl_tree_lookup_node(tree, &i) == NULL);
231-
i = 100000;
233+
i = NUM_TEST_VALUES + 100;
232234
assert(avl_tree_lookup_node(tree, &i) == NULL);
233235

234236
avl_tree_free(tree);
@@ -327,7 +329,7 @@ void test_avl_tree_lookup(void)
327329

328330
tree = create_tree();
329331

330-
for (i=0; i<1000; ++i) {
332+
for (i=0; i<NUM_TEST_VALUES; ++i) {
331333
value = avl_tree_lookup(tree, &i);
332334

333335
assert(value != NULL);
@@ -338,7 +340,7 @@ void test_avl_tree_lookup(void)
338340

339341
i = -1;
340342
assert(avl_tree_lookup(tree, &i) == NULL);
341-
i = 1001;
343+
i = NUM_TEST_VALUES + 1;
342344
assert(avl_tree_lookup(tree, &i) == NULL);
343345
i = 8724897;
344346
assert(avl_tree_lookup(tree, &i) == NULL);
@@ -358,14 +360,14 @@ void test_avl_tree_remove(void)
358360

359361
/* Try removing invalid entries */
360362

361-
i = 100000;
363+
i = NUM_TEST_VALUES + 100;
362364
assert(avl_tree_remove(tree, &i) == 0);
363365
i = -1;
364366
assert(avl_tree_remove(tree, &i) == 0);
365367

366368
/* Delete the nodes from the tree */
367369

368-
expected_entries = 1000;
370+
expected_entries = NUM_TEST_VALUES;
369371

370372
/* This looping arrangement causes nodes to be removed in a
371373
* randomish fashion from all over the tree. */

test/test-binary-heap.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,16 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2727
#include "binary-heap.h"
2828
#include "compare-int.h"
2929

30-
int test_array[1000];
30+
#define NUM_TEST_VALUES 10000
31+
32+
int test_array[NUM_TEST_VALUES];
3133

3234
void test_binary_heap_new_free(void)
3335
{
3436
BinaryHeap *heap;
3537
int i;
3638

37-
for (i=0; i<1000; ++i) {
39+
for (i=0; i<NUM_TEST_VALUES; ++i) {
3840
heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare);
3941
binary_heap_free(heap);
4042
}
@@ -57,12 +59,12 @@ void test_binary_heap_insert(void)
5759

5860
heap = binary_heap_new(BINARY_HEAP_TYPE_MIN, int_compare);
5961

60-
for (i=0; i<1000; ++i) {
62+
for (i=0; i<NUM_TEST_VALUES; ++i) {
6163
test_array[i] = i;
6264
assert(binary_heap_insert(heap, &test_array[i]) != 0);
6365
}
6466

65-
assert(binary_heap_num_entries(heap) == 1000);
67+
assert(binary_heap_num_entries(heap) == NUM_TEST_VALUES);
6668

6769
binary_heap_free(heap);
6870
}
@@ -77,7 +79,7 @@ void test_min_heap(void)
7779

7880
/* Push a load of values onto the heap */
7981

80-
for (i=0; i<1000; ++i) {
82+
for (i=0; i<NUM_TEST_VALUES; ++i) {
8183
test_array[i] = i;
8284
assert(binary_heap_insert(heap, &test_array[i]) != 0);
8385
}
@@ -110,14 +112,14 @@ void test_max_heap(void)
110112

111113
/* Push a load of values onto the heap */
112114

113-
for (i=0; i<1000; ++i) {
115+
for (i=0; i<NUM_TEST_VALUES; ++i) {
114116
test_array[i] = i;
115117
assert(binary_heap_insert(heap, &test_array[i]) != 0);
116118
}
117119

118120
/* Pop values off the heap and check they are in order */
119121

120-
i = 1000;
122+
i = NUM_TEST_VALUES;
121123
while (binary_heap_num_entries(heap) > 0) {
122124
val = (int *) binary_heap_pop(heap);
123125

test/test-binomial-heap.c

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -27,23 +27,25 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2727
#include "binomial-heap.h"
2828
#include "compare-int.h"
2929

30-
int test_array[1000];
30+
#define NUM_TEST_VALUES 10000
31+
32+
int test_array[NUM_TEST_VALUES];
3133

3234
void test_binomial_heap_new_free(void)
3335
{
3436
BinomialHeap *heap;
3537
int i;
3638

37-
for (i=0; i<1000; ++i) {
39+
for (i=0; i<NUM_TEST_VALUES; ++i) {
3840
heap = binomial_heap_new(BINOMIAL_HEAP_TYPE_MIN, int_compare);
3941
binomial_heap_free(heap);
4042
}
4143

42-
/* Test for out of memory */
44+
/* Test for out of memory */
4345

44-
alloc_test_set_limit(0);
46+
alloc_test_set_limit(0);
4547

46-
assert(binomial_heap_new(BINOMIAL_HEAP_TYPE_MIN, int_compare) == NULL);
48+
assert(binomial_heap_new(BINOMIAL_HEAP_TYPE_MIN, int_compare) == NULL);
4749
}
4850

4951
void test_binomial_heap_insert(void)
@@ -53,16 +55,16 @@ void test_binomial_heap_insert(void)
5355

5456
heap = binomial_heap_new(BINOMIAL_HEAP_TYPE_MIN, int_compare);
5557

56-
for (i=0; i<1000; ++i) {
58+
for (i=0; i<NUM_TEST_VALUES; ++i) {
5759
test_array[i] = i;
5860
assert(binomial_heap_insert(heap, &test_array[i]) != 0);
5961
}
60-
assert(binomial_heap_num_entries(heap) == 1000);
62+
assert(binomial_heap_num_entries(heap) == NUM_TEST_VALUES);
6163

62-
/* Test for out of memory */
64+
/* Test for out of memory */
6365

64-
alloc_test_set_limit(0);
65-
assert(binomial_heap_insert(heap, &i) == 0);
66+
alloc_test_set_limit(0);
67+
assert(binomial_heap_insert(heap, &i) == 0);
6668

6769
binomial_heap_free(heap);
6870
}
@@ -77,7 +79,7 @@ void test_min_heap(void)
7779

7880
/* Push a load of values onto the heap */
7981

80-
for (i=0; i<1000; ++i) {
82+
for (i=0; i<NUM_TEST_VALUES; ++i) {
8183
test_array[i] = i;
8284
assert(binomial_heap_insert(heap, &test_array[i]) != 0);
8385
}
@@ -92,10 +94,10 @@ void test_min_heap(void)
9294
i = *val;
9395
}
9496

95-
/* Test pop on an empty heap */
97+
/* Test pop on an empty heap */
9698

97-
val = (int *) binomial_heap_pop(heap);
98-
assert(val == NULL);
99+
val = (int *) binomial_heap_pop(heap);
100+
assert(val == NULL);
99101

100102
binomial_heap_free(heap);
101103
}
@@ -110,29 +112,31 @@ void test_max_heap(void)
110112

111113
/* Push a load of values onto the heap */
112114

113-
for (i=0; i<1000; ++i) {
115+
for (i=0; i<NUM_TEST_VALUES; ++i) {
114116
test_array[i] = i;
115117
assert(binomial_heap_insert(heap, &test_array[i]) != 0);
116118
}
117119

118120
/* Pop values off the heap and check they are in order */
119121

120-
i = 1000;
122+
i = NUM_TEST_VALUES;
121123
while (binomial_heap_num_entries(heap) > 0) {
122124
val = (int *) binomial_heap_pop(heap);
123125

124126
assert(*val == i - 1);
125127
i = *val;
126128
}
127129

128-
/* Test pop on an empty heap */
130+
/* Test pop on an empty heap */
129131

130-
val = (int *) binomial_heap_pop(heap);
131-
assert(val == NULL);
132+
val = (int *) binomial_heap_pop(heap);
133+
assert(val == NULL);
132134

133135
binomial_heap_free(heap);
134136
}
135137

138+
#define TEST_VALUE (NUM_TEST_VALUES / 2)
139+
136140
static BinomialHeap *generate_heap(void)
137141
{
138142
BinomialHeap *heap;
@@ -142,9 +146,9 @@ static BinomialHeap *generate_heap(void)
142146

143147
/* Push a load of values onto the heap */
144148

145-
for (i=0; i<1000; ++i) {
149+
for (i=0; i<NUM_TEST_VALUES; ++i) {
146150
test_array[i] = i;
147-
if (i != 400) {
151+
if (i != TEST_VALUE) {
148152
assert(binomial_heap_insert(heap, &test_array[i]) != 0);
149153
}
150154
}
@@ -162,10 +166,10 @@ static void verify_heap(BinomialHeap *heap)
162166
int i;
163167

164168
numvals = binomial_heap_num_entries(heap);
165-
assert(numvals == 999);
169+
assert(numvals == NUM_TEST_VALUES - 1);
166170

167-
for (i=0; i<1000; ++i) {
168-
if (i == 400) {
171+
for (i=0; i<NUM_TEST_VALUES; ++i) {
172+
if (i == TEST_VALUE) {
169173
continue;
170174
}
171175

@@ -197,8 +201,9 @@ static void test_insert_out_of_memory(void)
197201
/* Insert should fail */
198202

199203
alloc_test_set_limit(i);
200-
test_array[400] = 400;
201-
assert(binomial_heap_insert(heap, &test_array[400]) == 0);
204+
test_array[TEST_VALUE] = TEST_VALUE;
205+
assert(binomial_heap_insert(heap,
206+
&test_array[TEST_VALUE]) == 0);
202207
alloc_test_set_limit(-1);
203208

204209
/* Check that the heap is unharmed */

test/test-hash-functions.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -29,41 +29,43 @@ CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
2929
#include "hash-int.h"
3030
#include "hash-string.h"
3131

32+
#define NUM_TEST_VALUES 200
33+
3234
void test_pointer_hash(void)
3335
{
34-
int array[10];
36+
int array[NUM_TEST_VALUES];
3537
int i, j;
3638

3739
/* Initialise the array to all zeros */
3840

39-
for (i=0; i<10; ++i) {
41+
for (i=0; i<NUM_TEST_VALUES; ++i) {
4042
array[i] = 0;
4143
}
4244

4345
/* Check hashes are never the same */
4446

45-
for (i=0; i<10; ++i) {
46-
for (j=i+1; j<10; ++j) {
47+
for (i=0; i<NUM_TEST_VALUES; ++i) {
48+
for (j=i+1; j<NUM_TEST_VALUES; ++j) {
4749
assert(pointer_hash(&array[i]) != pointer_hash(&array[j]));
4850
}
4951
}
5052
}
5153

5254
void test_int_hash(void)
5355
{
54-
int array[100];
56+
int array[NUM_TEST_VALUES];
5557
int i, j;
5658

5759
/* Initialise all entries in the array */
5860

59-
for (i=0; i<100; ++i) {
61+
for (i=0; i<NUM_TEST_VALUES; ++i) {
6062
array[i] = i;
6163
}
6264

6365
/* Check hashes are never the same */
6466

65-
for (i=0; i<100; ++i) {
66-
for (j=i+1; j<100; ++j) {
67+
for (i=0; i<NUM_TEST_VALUES; ++i) {
68+
for (j=i+1; j<NUM_TEST_VALUES; ++j) {
6769
assert(int_hash(&array[i]) != int_hash(&array[j]));
6870
}
6971
}

0 commit comments

Comments
 (0)