-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_quadtree_integrity.c
154 lines (132 loc) · 4.71 KB
/
test_quadtree_integrity.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
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
#include <assert.h>
#include <stdint.h>
#include <sqlite3.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "quadtree.h"
const unsigned int DOCUMENT_IDENTIFIER_COUNT = 1021206;
const unsigned int MAX_LABEL_COUNT = 13;
const unsigned int MAX_DOCUMENT_IDENTIFIER = 6741414;
struct verification_t {
uint64_t *arr;
uint64_t last_identifier;
uint64_t last_offset;
uint64_t last_label_offset;
};
unsigned int nextpow2(unsigned int x) {
unsigned int ret;
for (ret = 1; ret < x; ret *= 2);
return ret;
}
int create_tree_callback(void *arg, int argc, char **argv, char **col) {
uint64_t identifier, label;
QUADTREE *tree = (QUADTREE *)arg;
identifier = strtoul(argv[0], NULL, 10);
label = strtoul(argv[1], NULL, 10);
assert(quadtree_insert(tree, identifier, label));
return 0;
}
int create_validation_callback(void *arg, int argc, char **argv, char **col) {
uint64_t identifier, label, i, j, off;
struct verification_t *val = (struct verification_t *)arg;
identifier = strtoul(argv[0], NULL, 10);
label = strtoul(argv[1], NULL, 10);
if (val->last_offset && (val->last_identifier == identifier)) {
off = ((val->last_offset-1) * (MAX_LABEL_COUNT + 1));
off += val->last_label_offset;
val->last_label_offset++;
}
else {
off = (val->last_offset * (MAX_LABEL_COUNT + 1));
*(val->arr + off) = identifier;
val->last_label_offset = 1;
val->last_identifier = identifier;
off++;
val->last_offset++;
}
*(val->arr + off) = label;
return 0;
}
int main(int argc, char **argv) {
const char * const db_location = "cluster.sqlite";
const char * const select_query = "SELECT document_identifier, label FROM temporary_label_clustering ORDER BY document_identifier";
QUADTREE *tree = NULL;
sqlite3 *db = NULL;
char *zErrMsg = NULL;
int rc = 0;
uint64_t *validation, *verification;
unsigned int label_buf[MAX_LABEL_COUNT];
struct verification_t v;
// Allocate some memory for those things
validation = calloc(sizeof(uint64_t), DOCUMENT_IDENTIFIER_COUNT * (MAX_LABEL_COUNT + 1));
verification = calloc(sizeof(uint64_t), DOCUMENT_IDENTIFIER_COUNT * (MAX_LABEL_COUNT + 1));
if (validation == NULL || verification == NULL) {
fprintf(stderr, "Allocation error!\n");
return 1;
}
v.arr = validation;
v.last_offset = 0;
// Open the database
rc = sqlite3_open(db_location, &db);
if (rc) {
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
sqlite3_close(db);
return 1;
}
// Instantiate the quadtree
assert(!quadtree_init(&tree, nextpow2(MAX_DOCUMENT_IDENTIFIER)-1, nextpow2(MAX_DOCUMENT_IDENTIFIER)-1));
// Select the data out of the database
fprintf(stderr, "Creating quadtree...\n");
rc = sqlite3_exec(db, select_query, create_tree_callback, tree, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
return 1;
}
// Create the validation structure
fprintf(stderr, "Creating validation structure...\n");
rc = sqlite3_exec(db, select_query, create_validation_callback, &v, &zErrMsg);
if (rc != SQLITE_OK) {
fprintf(stderr, "SQL error: %s\n", zErrMsg);
sqlite3_free(zErrMsg);
sqlite3_close(db);
return 1;
}
// Verify the result
fprintf(stderr, "Verifying (stage 1)...\n");
for (int i = 0; i < DOCUMENT_IDENTIFIER_COUNT; i++) {
uint64_t *off = validation + (i * (MAX_LABEL_COUNT + 1));
uint64_t identifier = *off;
unsigned int out = 0;
assert(!quadtree_scan_x(tree, identifier, label_buf, &out, MAX_LABEL_COUNT));
for (int j = 0; j < out; j++) {
unsigned int label = label_buf[j];
for (int k = 1; k < MAX_LABEL_COUNT; k++) {
if (*(off + k) == label) {
*(off + k) = 0;
}
}
}
}
fprintf(stderr, "Verifying (stage 2)...\n");
for (int i = 0; i < DOCUMENT_IDENTIFIER_COUNT; i++) {
uint64_t *off = validation + (i * (MAX_LABEL_COUNT + 1));
uint64_t passed = 1;
for (int j = 1; j < MAX_LABEL_COUNT; j++) {
uint64_t *suboff = off + j;
if (*suboff) {
passed = 0;
fprintf(stderr, "%d/%d/%d/%d\n", *off, *suboff, i, j);
assert(0);
}
}
if (passed) {
*off = 0;
}
}
fprintf(stderr, "Verifying (stage 3)...\n");
assert(!memcmp(validation, verification, DOCUMENT_IDENTIFIER_COUNT * (MAX_LABEL_COUNT + 1) * sizeof(uint64_t)));
return 0;
}