Permalink
Fetching contributors…
Cannot retrieve contributors at this time
920 lines (665 sloc) 24 KB
#include "config.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include "boro.h"
/* FIXME */
void boro_cmd_get(boro_t *b, off_t r, char *k, off_t c, struct boro_mr *s, boro_type_t *t);
/* total number of tests and oks */
int tests = 0;
int oks = 0;
/* failed tests messages */
char *failed_msgs[5000];
int i_failed_msgs = 0;
int verbose = 0;
void _do_test(char *desc, int res, int src_line)
{
tests++;
if (res) {
oks++;
// printf("OK\n");
if (verbose)
printf("stress.c:%d: #%d %s: OK\n", src_line, tests, desc);
}
else {
printf("stress.c:%d: #%d error: %s *** Failed ***\n", src_line, tests, desc);
}
}
int test_summary(void)
{
printf("\n*** Total tests passed: %d/%d\n", oks, tests);
if (oks == tests)
printf("*** ALL TESTS PASSED\n");
else {
printf("*** %d %s\n", tests - oks, "TESTS ---FAILED---");
/* printf("\nFailed tests:\n\n");
for (n = 0; n < i_failed_msgs; n++)
printf("%s", failed_msgs[n]);*/
}
return oks == tests ? 0 : 1;
}
#define do_test(d, r) _do_test(d, r, __LINE__)
double diff_time(struct timeval *st, struct timeval *et)
{
double t = 0.0;
if (et == NULL)
gettimeofday(st, NULL);
else {
gettimeofday(et, NULL);
t = et->tv_sec - st->tv_sec + (et->tv_usec - st->tv_usec) / 1000000.0;
}
return t;
}
void boro_benchmark(char *filename)
{
}
int _do_benchmarks = 0;
off_t countries(boro_t *b)
{
off_t r, s1, s2;
r = 0;
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "Madrid"), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "40º26'N 3º42W"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "47265321"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "Europe"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_TRUE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "Barcelona"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Valencia"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Sevilla"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "Spain", s1, BORO_DICT);
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "Brussels"), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "50º51'N 4º21'E"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "11035948"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "Europe"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_TRUE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "Antwerpen"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Brugge"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Gent"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "Belgium", s1, BORO_DICT);
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "Paris"), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "48º51.4'N 2º21.05'E"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "65350000"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "Europe"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_TRUE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "Lyon"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Bordeaux"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Toulouse"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "France", s1, BORO_DICT);
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "London"), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "51º30'N 0º7W"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "63181775"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "Europe"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_FALSE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "Glasgow"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Cardiff"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Edinburgh"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "United Kingdom", s1, BORO_DICT);
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "Washington D.C."), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "38º53'N 77º01'W"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "316345000"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "America"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_TRUE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "New York"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Chicago"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Los Angeles"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "United States of America", s1, BORO_DICT);
s1 = 0;
s1 = boro_k_set(b, s1, "capital", boro_new_s(b, "Tokyo"), BORO_STRING);
s1 = boro_k_set(b, s1, "capital_coords", boro_new_s(b, "35º41'N 139º46'E"), BORO_STRING);
s1 = boro_k_set(b, s1, "population", boro_new_s(b, "126659683"), BORO_NUMBER);
s1 = boro_k_set(b, s1, "continent", boro_new_s(b, "Asia"), BORO_STRING);
s1 = boro_k_set(b, s1, "drives_on_the_right", 0, BORO_FALSE);
s2 = 0;
s2 = boro_append(b, s2, boro_new_s(b, "Kyoto"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Osaka"), BORO_STRING);
s2 = boro_append(b, s2, boro_new_s(b, "Sapporo"), BORO_STRING);
s1 = boro_k_set(b, s1, "cities", s2, BORO_LIST);
r = boro_k_set(b, r, "Japan", s1, BORO_DICT);
return r;
}
void print_tree(boro_t *b, off_t r, boro_type_t pt)
{
struct boro_mr s;
boro_mr_init(&s, b, boro_mr_json);
s.farg[0] = stdout;
boro_mr(&s, r, pt);
printf("\n");
}
FILE *jsfile(char *json)
{
FILE *f;
if ((f = fopen("qq.json", "w")) != NULL) {
fprintf(f, "%s", json);
fclose(f);
f = fopen("qq.json", "r");
}
return f;
}
void test_basic(char *filename)
{
boro_t b;
off_t r, i, i2, t, r2;
boro_type_t fl;
struct boro_node *n;
struct boro_mr s1, s2, s3;
FILE *f;
int c;
if (filename != NULL)
unlink(filename);
do_test("boro_open", boro_open(&b, filename));
r = boro_begin_rw(&b);
i = boro_new(&b, "key", boro_new_s(&b, "value"), BORO_STRING, 0, 0);
n = boro_node(&b, i);
do_test("n->n 1", n->n == 0);
do_test("n->c 1", n->c == 1);
r = boro_new(&b, "key2", boro_new_s(&b, "value2"), BORO_STRING, 0, i);
n = boro_node(&b, i);
do_test("n->n 2", n->n == 1);
do_test("n->c 2", n->c == 1);
n = boro_node(&b, r);
do_test("n->n 2", n->n == 0);
do_test("n->c 2", n->c == 2);
boro_end_rw(&b, r);
n = boro_node(&b, r);
do_test("n->n 2", n->n == 1);
do_test("n->c 2", n->c == 2);
do_test("b->h->dq 1", b.h->dq == 0);
r = boro_begin_rw(&b);
i = boro_new(&b, "key3", boro_new_s(&b, "value3"), BORO_STRING, 0, 0);
boro_end_rw(&b, i);
/* if (b.f != -1)
do_test("b->h->dq 2", b.h->dq == r);
*/
boro_gc(&b);
boro_gc(&b);
boro_gc(&b);
r = boro_begin_rw(&b);
i = boro_new(&b, "key4", boro_new_s(&b, "value4"), BORO_STRING, 0, 0);
boro_end_rw(&b, i);
boro_gc(&b);
r = boro_begin_rw(&b);
r = boro_k_set(&b, r, "key5", boro_new_s(&b, "value5"), BORO_STRING);
boro_end_rw(&b, r);
r = boro_begin_rw(&b);
do_test("boro_k_get before boro_k_delete", boro_k_get(&b, r, "key5") != 0);
r = boro_k_delete(&b, r, "key5");
do_test("boro_k_get after boro_k_delete", boro_k_get(&b, r, "key5") == 0);
boro_end_rw(&b, r);
boro_close(&b);
if (filename != NULL)
unlink(filename);
do_test("boro_open", boro_open(&b, filename));
r = boro_begin_rw(&b);
r = boro_append(&b, r, boro_new_s(&b, "this"), BORO_STRING);
r = boro_append(&b, r, boro_new_s(&b, "is"), BORO_STRING);
r = boro_append(&b, r, boro_new_s(&b, "a"), BORO_STRING);
r = boro_append(&b, r, boro_new_s(&b, "test"), BORO_STRING);
boro_end_rw(&b, r);
r = boro_begin_ro(&b);
if (verbose) {
boro_dump_as_tree(&b, r, 0, 0, 20);
printf("\n");
boro_dump_as_csv(&b, r);
}
i = boro_i_get(&b, r, 0);
n = boro_node(&b, i);
do_test("i_append & i_get 0", strcmp("this", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 1);
n = boro_node(&b, i);
do_test("i_append & i_get 1", strcmp("is", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 2);
n = boro_node(&b, i);
do_test("i_append & i_get 2", strcmp("a", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 3);
n = boro_node(&b, i);
do_test("i_append & i_get 3", strcmp("test", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 4);
do_test("i_get 4", i == 0);
boro_end_ro(&b, r);
boro_close(&b);
if (filename != NULL)
unlink(filename);
do_test("boro_open", boro_open(&b, filename));
r = boro_begin_rw(&b);
r = boro_sort(&b, r, boro_new_s(&b, "c"), BORO_STRING, 1);
r = boro_sort(&b, r, boro_new_s(&b, "d"), BORO_STRING, 1);
r = boro_sort(&b, r, boro_new_s(&b, "a"), BORO_STRING, 1);
r = boro_sort(&b, r, boro_new_s(&b, "b"), BORO_STRING, 1);
boro_end_rw(&b, r);
r = boro_begin_ro(&b);
if (verbose) {
boro_dump_as_tree(&b, r, 0, 0, 20);
printf("\n");
boro_dump_as_csv(&b, r);
}
while (boro_gc(&b));
if (verbose) {
boro_dump_as_tree(&b, r, 0, 0, 20);
printf("\n");
boro_dump_as_csv(&b, r);
}
i = boro_i_get(&b, r, 0);
n = boro_node(&b, i);
do_test("i_sort & i_get 0", strcmp("a", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 1);
n = boro_node(&b, i);
do_test("i_sort & i_get 1", strcmp("b", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 2);
n = boro_node(&b, i);
do_test("i_sort & i_get 2", strcmp("c", boro_str(&b, n->v, n->t)) == 0);
i = boro_i_get(&b, r, 3);
n = boro_node(&b, i);
do_test("i_sort & i_get 3", strcmp("d", boro_str(&b, n->v, n->t)) == 0);
boro_end_ro(&b, r);
if (verbose) {
r = boro_begin_ro(&b);
boro_mr_init(&s1, &b, boro_mr_json);
s1.farg[0] = stdout;
boro_mr(&s1, r, BORO_LIST);
printf("\n");
boro_mr_init(&s1, &b, boro_mr_slice);
s1.iarg[0] = 1;
s1.iarg[1] = 2;
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
boro_mr(&s1, r, BORO_LIST);
printf("\n");
boro_end_ro(&b, r);
}
r = boro_begin_ro(&b);
boro_mr_init(&s1, &b, boro_mr_slice);
s1.iarg[0] = 1;
s1.iarg[1] = 2;
boro_mr_init(&s2, &b, boro_mr_store);
boro_mr_chain(&s1, &s2);
boro_mr(&s1, r, BORO_LIST);
t = s2.r;
boro_ref(&b, t);
i = boro_i_get(&b, t, 0);
n = boro_node(&b, i);
do_test("slice + store 1", n && strcmp(boro_str(&b, n->v, n->t), "b") == 0);
i = boro_i_get(&b, t, 1);
n = boro_node(&b, i);
do_test("slice + store 2", n && strcmp(boro_str(&b, n->v, n->t), "c") == 0);
boro_unref(&b, t);
boro_end_ro(&b, r);
while (boro_gc(&b));
r = boro_begin_rw(&b);
r = countries(&b);
r = boro_k_set(&b, 0, "countries", r, BORO_DICT);
boro_end_rw(&b, r);
if (verbose) {
r = boro_begin_ro(&b);
boro_mr_init(&s1, &b, boro_mr_json);
s1.farg[0] = stdout;
boro_mr(&s1, r, BORO_DICT);
printf("\n");
boro_end_ro(&b, r);
}
r = boro_begin_ro(&b);
i = boro_k_get(&b, r, "countries");
n = boro_node(&b, i);
t = n->v;
boro_mr_init(&s1, &b, boro_mr_sort_by);
s1.sarg[0] = "capital";
boro_mr(&s1, t, 0);
boro_ref(&b, s1.r);
i = boro_i_get(&b, s1.r, 0);
n = boro_node(&b, i);
i = boro_k_get(&b, n->v, "capital");
n = boro_node(&b, i);
do_test("sort_by 1", n && strcmp(boro_str(&b, n->v, n->t), "Brussels") == 0);
i = boro_i_get(&b, s1.r, 5);
n = boro_node(&b, i);
i = boro_k_get(&b, n->v, "capital");
n = boro_node(&b, i);
do_test("sort_by 2", n && strcmp(boro_str(&b, n->v, n->t), "Washington D.C.") == 0);
if (verbose) {
printf("sort_by capital:\n");
print_tree(&b, s1.r, BORO_LIST);
}
boro_unref(&b, s1.r);
i = boro_k_get(&b, r, "countries");
n = boro_node(&b, i);
t = n->v;
boro_mr_init(&s1, &b, boro_mr_sort_by);
s1.sarg[0] = "capital";
boro_mr_init(&s2, &b, boro_mr_slice);
s2.iarg[0] = 0;
s2.iarg[1] = 0;
boro_mr_init(&s3, &b, boro_mr_store);
boro_mr_chain(&s1, &s2);
boro_mr_chain(&s1, &s3);
boro_mr(&s1, t, BORO_LIST);
boro_ref(&b, s3.r);
if (verbose) {
printf("sort_by capital + slice(0, 0):\n");
print_tree(&b, s3.r, BORO_LIST);
}
do_test("sort_by + slice + store", boro_count(&b, s3.r) == 1);
boro_unref(&b, s3.r);
boro_mr_init(&s1, &b, boro_mr_group_by);
s1.sarg[0] = "continent";
boro_mr(&s1, t, BORO_DICT);
boro_ref(&b, s1.r);
if (verbose) {
printf("group_by:\n");
print_tree(&b, s1.r, BORO_DICT);
}
i = boro_k_get(&b, s1.r, "Asia");
n = boro_node(&b, i);
do_test("group_by 1", n && boro_count(&b, n->v) == 1);
i = boro_k_get(&b, s1.r, "America");
n = boro_node(&b, i);
do_test("group_by 2", n && boro_count(&b, n->v) == 1);
i = boro_k_get(&b, s1.r, "Europe");
n = boro_node(&b, i);
do_test("group_by 3", n && boro_count(&b, n->v) == 4);
boro_unref(&b, s1.r);
while (boro_gc(&b));
/* having */
t = boro_k_set(&b, 0, "drives_on_the_right", 0, BORO_TRUE);
boro_ref(&b, t);
boro_mr_init(&s1, &b, boro_mr_having);
s1.r = t;
boro_mr_init(&s2, &b, boro_mr_store);
boro_mr_chain(&s1, &s2);
i = boro_k_get(&b, r, "countries");
n = boro_node(&b, i);
r2 = n->v;
boro_mr(&s1, r2, 0);
boro_ref(&b, s2.r);
do_test("having 1", boro_k_get(&b, s2.r, "Spain") != 0);
do_test("having 2", boro_k_get(&b, s2.r, "Belgium") != 0);
do_test("having 3", boro_k_get(&b, s2.r, "France") != 0);
do_test("having 4", boro_k_get(&b, s2.r, "United States of America") != 0);
do_test("having 5", boro_k_get(&b, s2.r, "Japan") == 0);
do_test("having 6", boro_k_get(&b, s2.r, "United Kingdom") == 0);
if (verbose) {
printf("having {drives_on_the_right: true}\n");
print_tree(&b, s2.r, BORO_LIST);
}
boro_unref(&b, s2.r);
boro_unref(&b, t);
boro_end_ro(&b, r);
while (boro_gc(&b));
/* import */
r = boro_begin_rw(&b);
fl = 0;
f = jsfile("[]");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 1 []", fl == BORO_LIST);
fl = 0;
f = jsfile("{}");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 2 {}", fl == BORO_DICT);
fl = 0;
f = jsfile("{\"a\":1}");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 3", fl == BORO_DICT);
fl = 0;
f = jsfile("{\"a\":\"c\",\"b\":[1,\"d\",3],\"e1\":{\"e1\":1.0,\"e2\":12.53}}");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 4", fl == BORO_DICT);
fl = 0;
f = jsfile("[\"a\",\"c\",\"b\",[1,\"d\",3],\"e1\",{\"e1\":1.0,\"e2\":12.53},null,true,false,12345.2345,\"no more\"]");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 5", fl == BORO_LIST);
fl = 0;
f = jsfile("[1");
t = boro_import_json(&b, &fl, f);
fclose(f);
do_test("import json 6.1 (error)", fl == 0);
fl = 0;
f = jsfile("[1,");
t = boro_import_json(&b, &fl, f);
fclose(f);
do_test("import json 6.2 (error)", fl == 0);
fl = 0;
f = jsfile("[1,2");
t = boro_import_json(&b, &fl, f);
fclose(f);
do_test("import json 6.3 (error)", fl == 0);
fl = 0;
f = jsfile("[1,2,3");
t = boro_import_json(&b, &fl, f);
fclose(f);
do_test("import json 6.4 (error)", fl == 0);
fl = 0;
f = jsfile("[1,true,2,false,[],{},null,4.567] garbage at the end");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 7", fl == BORO_LIST);
fl = 0;
f = jsfile("{\"a\":{\"b\":{\"c\":[1,2,3]}}}");
t = boro_import_json(&b, &fl, f);
fclose(f);
if (verbose)
print_tree(&b, t, fl);
do_test("import json 8", fl == BORO_DICT);
fl = 0;
t = boro_import_json_s(&b, &fl,
"{\"a\":\"c\",\"b\":[1,\"d\",3],\"e1\":{\"e1\":1.0,\"e2\":12.53}}");
if (verbose)
print_tree(&b, t, fl);
do_test("import json_s 1", fl == BORO_DICT);
while (boro_gc(&b));
boro_end_rw(&b, r);
while (boro_gc(&b));
/* iterators */
if (verbose)
printf("Iterating countries:\n");
struct boro_iter it;
r = boro_begin_ro(&b);
i = boro_k_get(&b, r, "countries");
n = boro_node(&b, i);
t = n->v;
c = 0;
boro_iter_init(&it, &b, t, 1);
while ((n = boro_iter_next(&it))) {
if (verbose)
printf("%s\n", n->k);
c++;
}
do_test("iterator 1", c == 6);
if (verbose)
printf("Iterator for France key/values (non-scalar values not shown):\n");
i = boro_k_get(&b, t, "France");
n = boro_node(&b, i);
t = n->v;
boro_iter_init(&it, &b, t, 1);
while ((n = boro_iter_next(&it))) {
if (verbose)
printf("%s: %s\n", n->k, boro_str(&b, n->v, n->t));
}
i = boro_k_get(&b, r, "countries");
n = boro_node(&b, i);
t = n->v;
if (verbose)
printf("Inverse iterator for France key/values (non-scalar values not shown):\n");
i = boro_k_get(&b, t, "France");
n = boro_node(&b, i);
t = n->v;
boro_iter_init(&it, &b, t, -1);
while ((n = boro_iter_next(&it))) {
if (verbose)
printf("%s: %s\n", n->k, boro_str(&b, n->v, n->t));
}
/* cmd_get */
boro_mr_init(&s1, &b, boro_mr_crc32);
if (verbose) {
printf("boro_cmd_get countries/Spain:\n");
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
}
fl = BORO_DICT;
boro_cmd_get(&b, r, "countries/Spain", 0, &s1, &fl);
if (verbose)
printf("\ncrc32: %08x\n", s1.iarg[0]);
do_test("boro_cmd_get countries/Spain", s1.iarg[0] == 0x5ec5188d);
boro_mr_init(&s1, &b, boro_mr_crc32);
if (verbose) {
printf("boro_cmd_get countries/France:\n");
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
}
fl = BORO_DICT;
boro_cmd_get(&b, r, "countries/France", 0, &s1, &fl);
if (verbose)
printf("\ncrc32: %08x\n", s1.iarg[0]);
do_test("boro_cmd_get countries/France", s1.iarg[0] == 0xc47c0a33);
boro_mr_init(&s1, &b, boro_mr_crc32);
t = boro_import_json_s(&b, &fl, "[[\"having\", {\"continent\":\"Asia\"}]]");
boro_ref(&b, t);
if (verbose) {
printf("boro_cmd_get countries (having continent: Asia):\n");
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
}
fl = BORO_DICT;
boro_cmd_get(&b, r, "countries", t, &s1, &fl);
if (verbose)
printf("\ncrc32: %08x\n", s1.iarg[0]);
do_test("boro_cmd_get countries (having continent: Asia)", s1.iarg[0] == 0xfe375820);
boro_unref(&b, t);
boro_mr_init(&s1, &b, boro_mr_crc32);
t = boro_import_json_s(&b, &fl, "[[\"sort_by\", \"capital\"]]");
boro_ref(&b, t);
if (verbose) {
printf("boro_cmd_get countries (sort_by capital):\n");
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
}
fl = BORO_DICT;
boro_cmd_get(&b, r, "countries", t, &s1, &fl);
if (verbose)
printf("\ncrc32: %08x\n", s1.iarg[0]);
do_test("boro_cmd_get countries (sort_by capital)", s1.iarg[0] == 0xd01ce6b4);
boro_unref(&b, t);
boro_mr_init(&s1, &b, boro_mr_crc32);
t = boro_import_json_s(&b, &fl, "[[\"sort_by\", \"population\"]]");
boro_ref(&b, t);
if (verbose) {
printf("boro_cmd_get countries (sort_by population):\n");
boro_mr_init(&s2, &b, boro_mr_json);
s2.farg[0] = stdout;
boro_mr_chain(&s1, &s2);
}
fl = BORO_DICT;
boro_cmd_get(&b, r, "countries", t, &s1, &fl);
if (verbose)
printf("\ncrc32: %08x\n", s1.iarg[0]);
do_test("boro_cmd_get countries (sort_by population)", s1.iarg[0] == 0x2e5af057);
boro_unref(&b, t);
i = boro_p_get(&b, r, BORO_DICT, "countries/Spain/capital");
n = boro_node(&b, i);
do_test("boro_p_get 1", strcmp(boro_str(&b, n->v, n->t), "Madrid") == 0);
i = boro_p_get(&b, r, BORO_DICT, "countries/Spain/cities/0");
n = boro_node(&b, i);
do_test("boro_p_get 2", strcmp(boro_str(&b, n->v, n->t), "Barcelona") == 0);
i = boro_p_get(&b, r, BORO_DICT, "countries/Spain/cities/2");
n = boro_node(&b, i);
do_test("boro_p_get 3", strcmp(boro_str(&b, n->v, n->t), "Sevilla") == 0);
i = boro_p_get(&b, r, BORO_DICT, "countries/Spain/cities/3");
do_test("boro_p_get 4", i == 0);
boro_end_ro(&b, r);
r = boro_begin_rw(&b);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal", 0, BORO_DICT);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/capital", boro_new_s(&b, "Lisboa"), BORO_STRING);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/cities", 0, BORO_LIST);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/cities/", boro_new_s(&b, "Porto"), BORO_STRING);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/cities/", boro_new_s(&b, "Coimbra"), BORO_STRING);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/cities/", boro_new_s(&b, "Braga"), BORO_STRING);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/drives_on_the_right", 0, BORO_TRUE);
r = boro_p_set(&b, r, BORO_DICT, "countries/Portugal/continent", boro_new_s(&b, "Europe"), BORO_STRING);
i = boro_p_get(&b, r, BORO_DICT, "countries/Portugal/capital");
n = boro_node(&b, i);
do_test("boro_p_set 1", strcmp(boro_str(&b, n->v, n->t), "Lisboa") == 0);
boro_end_rw(&b, r);
r = boro_begin_ro(&b);
if (verbose) {
printf("After adding Portugal:\n");
print_tree(&b, r, BORO_DICT);
}
boro_end_ro(&b, r);
if (verbose)
printf("Creating a list and a dict...\n");
r = boro_begin_rw(&b);
char tmp[32];
i = i2 = 0;
for (c = 0; c < 10; c++) {
sprintf(tmp, "%d", c);
i = boro_append_p(&b, i, boro_new_s(&b, tmp), BORO_NUMBER);
i2 = boro_k_set_p(&b, i2, tmp, boro_new_s(&b, tmp), BORO_NUMBER);
}
r = boro_p_set(&b, r, BORO_DICT, "list", i, BORO_LIST);
r = boro_p_set(&b, r, BORO_DICT, "dict", i2, BORO_DICT);
boro_end_rw(&b, r);
if (verbose)
printf("Done.\n");
boro_close(&b);
}
int main(int argc, char *argv[])
{
int n;
printf("BorodinDB %s stress tests\n", VERSION);
for (n = 1; n < argc; n++) {
if (strcmp(argv[n], "-b") == 0)
_do_benchmarks = 1;
if (strcmp(argv[n], "-v") == 0)
verbose = 1;
}
if (verbose)
printf("\nIn-memory trees---------------------\n");
test_basic(NULL);
if (verbose)
printf("\nFile-backed trees------------------------\n");
test_basic("qq.boro");
if (_do_benchmarks) {
printf("\nIn-memory trees---------------------\n");
boro_benchmark(NULL);
printf("\nFile-backed trees------------------------\n");
boro_benchmark("qq.boro");
}
else
printf("(Run with -b to do benchmarks)\n");
return test_summary();
}