-
Notifications
You must be signed in to change notification settings - Fork 2
/
test_quadtree_insert_query_rand.c
76 lines (60 loc) · 1.54 KB
/
test_quadtree_insert_query_rand.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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <time.h>
#include "quadtree.h"
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 main(int argc, char **argv) {
int random[64], i, j;
unsigned int s;
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 number
assert(quadtree_insert(ref, 0, random[i]));
}
for (i = 0; i < 64; i++) {
// Check retrieval
assert(quadtree_query(ref, 0, random[i]));
}
for (i = 0; i < 64; i++) {
if (!random[i]) continue;
// Check retrieval
assert(!quadtree_query(ref, random[i], random[i]));
}
return 0;
}