17 #include <libfocunit.h>
18 #include <sys/resource.h>
20 static PGresult *capture_update(PGconn *connection,
const char *query);
21 static void capture_clear(PGresult *result);
22 static void capture_heart(
int items);
27 #define main adj2nest_main
28 #define PQexec capture_update
29 #define PQclear capture_clear
30 #define fo_scheduler_heart capture_heart
31 #include "../../agent/adj2nest.c"
32 #undef fo_scheduler_heart
38 extern volatile int alive;
49 static unsigned char *endpoints;
50 static PGresult *command_result;
51 static long update_count;
52 static long clear_count;
53 static long heart_count;
54 static long expected_count;
55 static struct rlimit saved_stack_limit;
56 static int stack_limited;
66 update_count = clear_count = heart_count = expected_count = 0;
75 FO_ASSERT(setrlimit(RLIMIT_STACK, &saved_stack_limit) == 0);
78 PQclear(command_result);
79 command_result = NULL;
90 static void make_tree(
long count)
93 Tree = calloc(count,
sizeof(*Tree));
94 actual = calloc(count,
sizeof(*actual));
95 expected = calloc(count,
sizeof(*expected));
96 endpoints = calloc(2 * count + 1,
sizeof(*endpoints));
98 command_result = PQmakeEmptyPGresult(NULL, PGRES_COMMAND_OK);
99 FO_ASSERT_PTR_NOT_NULL_FATAL(Tree);
100 FO_ASSERT_PTR_NOT_NULL_FATAL(actual);
101 FO_ASSERT_PTR_NOT_NULL_FATAL(expected);
102 FO_ASSERT_PTR_NOT_NULL_FATAL(endpoints);
103 FO_ASSERT_PTR_NOT_NULL_FATAL(command_result);
104 for (
long i = 0; i < count; ++i)
113 static PGresult *capture_update(PGconn *connection,
const char *query)
117 FO_ASSERT_PTR_EQUAL(connection,
pgConn);
118 FO_ASSERT_FATAL(update_count < TreeSize);
120 FO_ASSERT(clear_count == update_count);
121 FO_ASSERT(heart_count == update_count);
123 int fields = sscanf(query,
124 "UPDATE %127s SET lft='%ld', rgt='%ld' WHERE uploadtree_pk='%ld'%n",
125 table, &row->lft, &row->rgt, &row->pk, &end);
126 FO_ASSERT_EQUAL_FATAL(fields, 4);
127 FO_ASSERT(query[end] ==
'\0');
130 return command_result;
133 static void capture_clear(PGresult *result)
135 FO_ASSERT_PTR_EQUAL(result, command_result);
136 FO_ASSERT(clear_count + 1 == update_count);
137 FO_ASSERT(heart_count + 1 == update_count);
142 static void capture_heart(
int items)
144 FO_ASSERT_EQUAL(items, 1);
145 FO_ASSERT(clear_count == update_count);
146 FO_ASSERT(heart_count + 1 == update_count);
153 fo_scheduler_heart(items);
154 FO_ASSERT_TRUE(
alive);
158 static int compare_pk(
const void *a,
const void *b)
162 return (left > right) - (left < right);
168 return bsearch(&key, actual, TreeSize,
sizeof(*actual), compare_pk);
171 static void assert_range(
long index,
long lft,
long rgt)
174 FO_ASSERT_PTR_NOT_NULL_FATAL(row);
175 FO_ASSERT(row->lft == lft);
176 FO_ASSERT(row->rgt == rgt);
179 static void run_walk(
void)
182 FO_ASSERT_FATAL(update_count == TreeSize);
183 FO_ASSERT(clear_count == TreeSize);
184 FO_ASSERT(heart_count == TreeSize);
186 FO_ASSERT(
SetNum == 2 * TreeSize);
187 qsort(actual, TreeSize,
sizeof(*actual), compare_pk);
189 for (
long i = 0; i < TreeSize; ++i)
192 FO_ASSERT_FATAL(row->lft >= 1 && row->rgt <= 2 * TreeSize);
193 FO_ASSERT_FATAL(row->lft < row->rgt);
194 FO_ASSERT_FALSE(endpoints[row->lft]);
195 FO_ASSERT_FALSE(endpoints[row->rgt]);
196 endpoints[row->lft] = endpoints[row->rgt] = 1;
198 for (
long i = 1; i <= 2 * TreeSize; ++i)
200 FO_ASSERT_TRUE(endpoints[i]);
202 for (
long i = 0; i < TreeSize; ++i)
205 FO_ASSERT_PTR_NOT_NULL_FATAL(parent);
206 for (
long child = Tree[i].Child; child >= 0; child = Tree[child].
Sibling)
209 FO_ASSERT_PTR_NOT_NULL_FATAL(row);
210 FO_ASSERT(parent->lft < row->lft);
211 FO_ASSERT(row->rgt < parent->rgt);
214 assert_range(0, 1, 2 * TreeSize);
223 long left = (*number)++;
224 if (Tree[index].Child > -1)
229 expected[expected_count++] =
231 if (Tree[index].Sibling > -1)
238 static void assert_recursive_parity(
void)
242 FO_ASSERT_FATAL(expected_count == TreeSize);
243 qsort(expected, TreeSize,
sizeof(*expected), compare_pk);
245 FO_ASSERT(
SetNum == number);
246 for (
long i = 0; i < TreeSize; ++i)
248 FO_ASSERT(actual[i].pk == expected[i].pk);
249 FO_ASSERT(actual[i].lft == expected[i].lft);
250 FO_ASSERT(actual[i].rgt == expected[i].rgt);
254 static void test_single_node(
void)
258 assert_recursive_parity();
259 assert_range(0, 1, 2);
262 static void test_siblings(
void)
268 assert_recursive_parity();
269 assert_range(0, 1, 8);
270 assert_range(1, 2, 3);
271 assert_range(2, 4, 5);
272 assert_range(3, 6, 7);
275 static void test_mixed_tree(
void)
286 assert_recursive_parity();
288 assert_range(0, 1, 16);
289 assert_range(1, 2, 11);
290 assert_range(2, 12, 15);
291 assert_range(3, 3, 4);
292 assert_range(4, 5, 10);
293 assert_range(5, 13, 14);
294 assert_range(6, 6, 7);
295 assert_range(7, 8, 9);
298 static void test_deep_tree(
void)
301 for (
long i = 0; i + 1 < TreeSize; ++i)
303 Tree[i].
Child = i + 1;
305 assert_recursive_parity();
306 for (
long i = 0; i < TreeSize; ++i)
308 assert_range(i, i + 1, 2 * TreeSize - i);
312 static void test_generated_tree(
void)
314 guint32 seed = 0xc0ffee;
316 for (
long i = 1; i < TreeSize; ++i)
319 seed = seed * 1664525U + 1013904223U;
320 long parent = seed % i;
321 long *link = &Tree[parent].
Child;
328 assert_recursive_parity();
331 static void test_wide_tree(
void)
336 for (
long i = 1; i + 1 < TreeSize; ++i)
344 FO_ASSERT_FATAL(getrlimit(RLIMIT_STACK, &saved_stack_limit) == 0);
345 struct rlimit limit = saved_stack_limit;
346 if (limit.rlim_cur == RLIM_INFINITY || limit.rlim_cur > 256 * 1024)
348 limit.rlim_cur = 256 * 1024;
349 FO_ASSERT_FATAL(setrlimit(RLIMIT_STACK, &limit) == 0);
353 for (
long i = 1; i < TreeSize; ++i)
355 assert_range(i, 2 * i, 2 * i + 1);
359 static CU_TestInfo walk_tree_tests[] = {
360 {
"single node", test_single_node},
361 {
"siblings", test_siblings},
362 {
"mixed tree", test_mixed_tree},
363 {
"deep tree", test_deep_tree},
364 {
"generated tree", test_generated_tree},
365 {
"100000 siblings with bounded stack", test_wide_tree},
369 static CU_SuiteInfo suites[] = {
374 int main(
int argc,
char **argv)
376 return focunit_main(argc, argv,
"adj2nest", suites);
PGconn * pgConn
Database connection.
void WalkTree(long Index, long Depth)
char * uploadtree_tablename
upload.uploadtree_tablename
The main FOSSology C library.
int agent_verbose
Common verbose flags for the agents, this is used so that the scheduler can change the verbose level ...
void set_up(void)
Since the fossscheduler library depends on reading and writing very specific data to stdin and stdout...
void tear_down(void)
This function closes the pipes created in the setup function and returns stdin and stdout to their or...
volatile int alive
If the agent has updated with a hearbeat.
volatile gint items_processed
The number of items processed by the agent.
static void walk_recursive(long index, long *number)