-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_quadtree_scan_x.c
125 lines (101 loc) · 3.01 KB
/
test_quadtree_scan_x.c
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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include "quadtree.h"
int quadtree_scan_x(QUADTREE *tree, unsigned int x, unsigned int *out, unsigned int *p, size_t arr_size);
unsigned int nextpow2(int of) {
unsigned int ret;
for (ret = 1; ret < of; ret *= 2);
return ret;
}
int max(int *arr, int len) {
int i, max;
for (i = 0, max = 0; i < len; i++) {
if (*(arr + i) > max) {
max = *(arr + i);
}
}
return max;
}
int comp(int *arg1, unsigned int *arg2, int length1, int length2) {
// Compare the original random array with the one scanned
// Return 1 if they contain the same elements, 0 otherwise
int i, j;
for (i = 0; i < length1; i++) {
int icur = arg1[i];
for (j = 0; j < length2; j++) {
int jcur = arg2[j];
if (jcur == icur) {
arg2[j] = 0;
}
}
}
for (i = 0; i < length2; i++) {
if (arg2[i]) {
return 0;
}
}
return 1;
}
int main(int argc, char **argv) {
int random[64], i, j;
unsigned int s, p, scan[64];
QUADTREE *ref = NULL;
// Configure random numbers
srand(time(NULL));
// Generate 64 of your finest random numbers!
for (i = 0; i < 64; i++) {
int dup = 1;
do {
random[i] = rand();
// Check this one isn't a duplicate
for (j = 0, dup = 0; j < i; j++) {
if (random[j] == random[i]) {
dup = 1; continue;
}
}
}
while(dup);
}
// Get the maximum random number generated
s = max(random, 64);
// Get the nearest 2 power
s = nextpow2(s);
// Generate the tree
assert(!quadtree_init(&ref, s, s));
for (i = 0; i < 64; i++) {
// Insert the random numbers at a constant x value
assert(quadtree_insert(ref, 13, random[i]));
}
// Expect a scan along an x-offset with no values
// will result in a non-incremented pointer
p = 0;
assert(!quadtree_scan_x(ref, 15, scan, &p, 64));
assert(p == 0);
// Expect a scan along the correct offset without
// sufficient memory to return error code 1, and
// a partially-filled search array
p = 0;
assert(quadtree_scan_x(ref, 13, scan, &p, 32) == 1);
assert(comp(random, scan, 64, 32));
// Check for a successful scan
p = 0;
assert(!quadtree_scan_x(ref, 13, scan, &p, 64));
assert(comp(random, scan, 64, 64));
for (i = 0; i < 64; i++) {
// Insert the random numbers at a constant x value
assert(quadtree_insert(ref, 0, random[i]));
}
p = 0;
assert(!quadtree_scan_x(ref, 0, scan, &p, 64));
assert(comp(random, scan, 64, 64));
for (i = 0; i < 64; i++) {
// Insert the random numbers at a constant x value
assert(quadtree_insert(ref, s-1, random[i]));
}
p = 0;
assert(!quadtree_scan_x(ref, s-1, scan, &p, 64));
assert(comp(random, scan, 64, 64));
return 0;
}