diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/check_bsr.c | 30 | ||||
-rw-r--r-- | src/test/check_cnp.c | 41 | ||||
-rw-r--r-- | src/test/check_crf.c | 195 | ||||
-rw-r--r-- | src/test/check_ht.c | 6 | ||||
-rw-r--r-- | src/test/check_splist.c | 62 | ||||
-rw-r--r-- | src/test/check_tuple.c | 244 |
6 files changed, 509 insertions, 69 deletions
diff --git a/src/test/check_bsr.c b/src/test/check_bsr.c index 81a84a7..7f729ca 100644 --- a/src/test/check_bsr.c +++ b/src/test/check_bsr.c @@ -114,23 +114,39 @@ main(int UNUSED argc, char ** UNUSED argv) BOOL error = 0; - bsr *bsrp = new_bsr(&error, g); + bsr *bsrp = new_bsr(g, 10, &error); if (error) goto cleanup; - bsr_add(g, bsrp, 0, 0, 1, 0, 1, 2); - bsr_add(g, bsrp, 0, 1, 1, 0, 1, 2); - bsr_add(g, bsrp, 0, 1, 2, 0, 1, 2); - + if (bsr_add(g, bsrp, (pair6) { 0, 0, 1, 0, 1, 2 })) { + fleprintf0("Fail to add to BSR\n"); + goto cleanup; + } + + if (bsr_add(g, bsrp, (pair6) { 0, 1, 1, 0, 1, 2 })) { + fleprintf0("Fail to add to BSR\n"); + goto cleanup; + } + + if (bsr_add(g, bsrp, (pair6) { 0, 1, 2, 0, 1, 2 })) { + fleprintf0("Fail to add to BSR\n"); + goto cleanup; + } + + if (bsr_add(g, bsrp, (pair6) { 0, 1, 3, 0, 1, 3 })) { + fleprintf0("Fail to add to BSR\n"); + goto cleanup; + } + if (bsr_find(bsrp, g, &error, - 0, 0, 1, 0, 1, 2)) { + (pair6) { 0, 0, 1, 0, 1, 2 })) { printf("Successfully found 0, 0, 1, 0, 1, 2\n"); } else { fleprintf0("Fail to find 0, 0, 1, 0, 1, 2\n"); } if (bsr_find(bsrp, g, &error, - 0, 0, 1, 0, 1, 3)) { + (pair6) { 0, 0, 1, 0, 1, 3 })) { fleprintf0("Accidentally found 0, 0, 1, 0, 1, 3\n"); } else { printf("Indeed cannot find 0, 0, 1, 0, 1, 3\n"); diff --git a/src/test/check_cnp.c b/src/test/check_cnp.c index 91952ef..a21d74e 100644 --- a/src/test/check_cnp.c +++ b/src/test/check_cnp.c @@ -1,9 +1,16 @@ #include "time.h" #include "../cnp.h" -#define TIC do { \ - clock_gettime(CLOCK_MONOTONIC_RAW, &tic); \ - } while (0) +UNUSED +static void +print_label6(pair6 label) +{ + printf("(%ld, %ld, %ld, %ld, %ld, %ld)\n", + label.x, label.y, label.z, label.u, label.v, label.w); +} + +#define TIC struct timespec tic, toc; \ + do { clock_gettime(CLOCK_MONOTONIC_RAW, &tic); } while (0) #define TOC do { \ clock_gettime(CLOCK_MONOTONIC_RAW, &toc); \ @@ -175,13 +182,13 @@ main(int UNUSED argc, char ** UNUSED argv) string = new_utf8("aab", 3); break; case 2: - string = new_utf8("[+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><][+-[.,]><]", 100); + string = new_utf8("[[[[[[[[[[[[[[[[[[[[[[[++[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]]--]]]]]]]]]]]]]]]]]]]]]]]", 204); break; case 3: - string = new_utf8("bbbbb", 5); + string = new_utf8("bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb", 100); break; case 4: - string = new_utf8("aaaaaaaaaaaa", 12); + string = new_utf8("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 48); break; default: fleprintf0("Forgot to assign input!\n"); @@ -190,7 +197,6 @@ main(int UNUSED argc, char ** UNUSED argv) printf("\nPrinting the input...\n%s\n", get_data((str *) string)); - struct timespec tic, toc; TIC; Environment *env = cnp_parse(g, (str *) string); @@ -199,33 +205,22 @@ main(int UNUSED argc, char ** UNUSED argv) if (env) { if (!(env_error_p(env))) { - BOOL error = 0; - ht *result = bsr_lookup - (env_bsrp(env), &error, 0, 0, str_length((str *) string)); - - if (error) { - printf("There are errors!\n"); - goto destroy; - } + BOOL result = bsr_lookup + (env_bsrp(env), 0, 0, str_length((str *) string)); - if (result && ht_size(result)) { + if (result) { printf("\nSuccessfully parsed the input!\n"); - for (NUM i = 0; i < ht_size(result); i++) - printf("(%d, %ld, %d, %ld, %ld)\n", - 0, (*((pair2 **) ht_keys(result)+i))->x, - 0, (*((pair2 **) ht_keys(result)+i))->y, - str_length((str *) string)); } else { printf("\nThe input does not parse!\n"); } printf("\nAll BSRs follow:\n\n"); - bsr_print(env_bsrp(env), env_grammar(env), 1); + if (argc == 1) + bsr_print(env_bsrp(env), env_grammar(env), 1); } else { printf("There are errors!\n"); } - destroy: destroy_env(env); } diff --git a/src/test/check_crf.c b/src/test/check_crf.c index d99a838..72931f8 100644 --- a/src/test/check_crf.c +++ b/src/test/check_crf.c @@ -2,9 +2,116 @@ #include "../util.h" #include "../crf.h" +#define READ_INTO_CPA(N, U, I, VA, VI, CP) do { \ + U = new_utf8(N, 1); \ + I = get_info((str *)U, 0); \ + VA = MYALLOC(NUM, 1); \ + VI = 0; \ + for (NUM index = 0; \ + I.value >= 0 && index < str_length((str *) U); \ + index += I.step, VI++) { \ + I = get_info((str *)U, index); \ + *(VA+VI) = I.value; \ + } \ + CP = MYALLOC(cpa, 1); \ + CP->array = VA; \ + CP->size = VI; \ + add_to_list(names, CP); \ + destroy_str((str *)U, 1); \ + } while (0) + +#define READ_TNT_STRING(LEFT, FORMAT, LEN, ...) do { \ + tnt_string = new_tnt_string(FORMAT, LEN, __VA_ARGS__); \ + if (!tnt_string) { \ + fleprintf("left = %d, f = %s, l = %d, " \ + "cannot create tnt string\n", \ + LEFT, FORMAT, LEN); \ + map_list(rules, destroy_rule_and_free_all); \ + destroy_list(rules, 0); \ + map_list(names, destroy_cpa_and_free_all); \ + destroy_list(names, 0); \ + return 1; \ + } \ + rule = new_rule(LEFT, tnt_string); \ + add_to_list(rules, rule); \ + } while (0) + +static void +print_label3(pair3 label) +{ + printf("(%ld, %ld, %ld)\n", label.x, label.y, label.z); +} + int main(int UNUSED argc, char ** UNUSED argv) { + /* Have a grammar for testing */ + + List *tnt_string = NULL; + Rule *rule = NULL; + List *rules = new_list(); + List *names = new_list(); + + char *name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'E'; + + utf8* uname = NULL; + + str_info info = EMPTY_STR_INFO; + + NUM *varray = NULL; + NUM vindex = 0; + cpa *cpap = NULL; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'T'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'F'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + name = MYALLOC(char, 2); + *(name+1) = 0; + *name = 'D'; + + READ_INTO_CPA(name, uname, info, varray, vindex, cpap); + + READ_TNT_STRING(0, "n", 1, (NT) 1); + READ_TNT_STRING(0, "ntn", 3, (NT) 1, (T) 43, (NT) 1); + + READ_TNT_STRING(1, "n", 1, (NT) 2); + READ_TNT_STRING(1, "ntn", 3, (NT) 2, (T) 42, (NT) 2); + + READ_TNT_STRING(2, "n", 1, (NT) 3); + READ_TNT_STRING(2, "tnt", 3, (T) 40, (NT) 0, (T) 41); + + READ_TNT_STRING(3, "tn", 2, (T) 48, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 49, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 50, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 51, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 52, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 53, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 54, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 55, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 56, (NT) 3); + READ_TNT_STRING(3, "tn", 2, (T) 57, (NT) 3); + rule = new_rule(3, new_list()); + add_to_list(rules, rule); + + Grammar *g = new_grammar(); + + build_grammar(g, rules, names, NULL); + + print_grammar(g); + procr *pr = NULL; procu *pu = NULL; @@ -12,14 +119,21 @@ main(int UNUSED argc, char ** UNUSED argv) prodecor *prod = NULL; + NUM input_len = 10; + /* check crf */ - crf *crfp = new_crf(); + crf *crfp = new_crf(g, input_len); + if (crfp == NULL) { + fleprintf0("Fail to create new CRF\n"); + goto cleanup; + } + pair2 p = { 0 }; p.x = 0; p.y = 1; - if (crf_find_node(crfp, p) == NULL) { + if (!(crf_find_node(crfp, p))) { if (crf_add_node(crfp, p)) { fleprintf0("fail to add node\n"); goto cleanup; @@ -28,7 +142,7 @@ main(int UNUSED argc, char ** UNUSED argv) } } - if (crf_find_node(crfp, p) == NULL) { + if (!(crf_find_node(crfp, p))) { fleprintf0("Fail to find node\n"); goto cleanup; } else { @@ -41,39 +155,46 @@ main(int UNUSED argc, char ** UNUSED argv) fleprintf0("Fail to add edge\n"); goto cleanup; } else { - printf("Successfully add edge from (0, 1) to zero\n"); + printf("Successfully add edge from (0, 1) to (0, 0, 0, 0)\n"); } - if (crf_find_node(crfp, p)) { - printf("Printing edges for (0, 1)...\n"); + p4.u = 1; -#define TABLE (crf_find_node(crfp, p)) + if (crf_add_edge(crfp, p, p4)) { + fleprintf0("Fail to add edge\n"); + goto cleanup; + } else { + printf("Successfully add edge from (0, 1) to (0, 0, 0, 1)\n"); + } - for (NUM i = 0; i < ht_size(TABLE); i++) { -#define KEY (*((pair4**) ht_keys(TABLE)+i)) - printf("(%ld, %ld, %ld, %ld)", - KEY->x, KEY->y, KEY->z, KEY->w); - if (i+1<ht_size(TABLE)) printf(", "); - else printf("\n"); - } + p4.u = 9; + + if (crf_add_edge(crfp, p, p4)) { + fleprintf0("Fail to add edge\n"); + goto cleanup; + } else { + printf("Successfully add edge from (0, 1) to (0, 0, 0, 9)\n"); } -#undef TABLE -#undef KEY + if (crf_find_edge(crfp, p, p4)) { + printf("Successfully found edge from (0, 1) to (0, 0, 0, 9)\n"); + } else { + fleprintf0("Fail to find edge from (0, 1) to (0, 0, 0, 9)\n"); + goto cleanup; + } + + crf_print(crfp); /* check spa */ - spap = new_spa(); + spap = new_spa(g, input_len); if (spap == NULL) { - fleprintf0("fail to have new spa\n"); + fleprintf0("fail to create new spa\n"); goto cleanup; } - pair3 p3 = (pair3) { 0 }; - p3.x = 0; - p3.y = 1; - p3.z = 10; + pair3 p3 = (pair3) { 0, 1, 9 }; if (spa_insert(spap, p3)) { fleprintf0("Fail to insert\n"); @@ -89,23 +210,18 @@ main(int UNUSED argc, char ** UNUSED argv) goto cleanup; } - pair2 first_two = (pair2) { 0 }; - first_two.x = 0; - first_two.y = 1; + pair2 first_two = (pair2) { 0, 1 }; - if (spa_find(spap, first_two)) { - printf("Successfully found p2\n"); - } else { - fleprintf0("Fail to find p2\n"); - goto cleanup; - } + spa_map(spap, first_two, print_label3); /* check prodecor */ BOOL error = 0; - pr = new_procr(10, &error); - pu = new_procu(10, &error); + pr = new_procr(g, input_len, &error); + pu = new_procu(g, input_len, &error); + + printf("Successfully created procr and procu\n"); p4 = (pair4) { 0 }; @@ -116,6 +232,10 @@ main(int UNUSED argc, char ** UNUSED argv) goto cleanup; } + printf("Successfully added to procr and procu\n"); + + print_procr(pr); + NUM grade = 0; prod = pop_procr(pr, pu, &grade); @@ -134,15 +254,18 @@ main(int UNUSED argc, char ** UNUSED argv) } printf("popped a process descriptor: (%ld, %ld, %ld, %ld, %ld)\n", - prod->x, prod->y, prod->z, prod->w, grade); + prod->x, prod->y, prod->z, prod->u, grade); cleanup: - destroy_crf(crfp); + if (crfp) destroy_crf(crfp); + if (spap) destroy_spa(spap); + + destroy_grammar(g, 1); + destroy_list(rules, 1); if (pr) destroy_procr(pr); if (pu) destroy_procu(pu); - if (spap) destroy_spa(spap); if (prod) free(prod); return 0; diff --git a/src/test/check_ht.c b/src/test/check_ht.c index 62a7bee..dd94eab 100644 --- a/src/test/check_ht.c +++ b/src/test/check_ht.c @@ -192,7 +192,7 @@ int main(int UNUSED argc, char ** UNUSED argv) printf("Success!\n"); - pair4 testk4 = { .x = 0, .y = 0, .z = 0, .w = 1 }; + pair4 testk4 = { .x = 0, .y = 0, .z = 0, .u = 1 }; if (ht_find(htp, &testk4) == NULL) { fleprintf0("Fail to find zero pair\n"); @@ -206,7 +206,7 @@ int main(int UNUSED argc, char ** UNUSED argv) testk4.x = 1; testk4.y = 2; testk4.z = -1; - testk4.w = 2; + testk4.u = 2; if (ht_find(htp, &testk4) == NULL) { fleprintf0("Fail to find one two minus one pair\n"); @@ -220,7 +220,7 @@ int main(int UNUSED argc, char ** UNUSED argv) testk4.x = 1; testk4.y = 2; testk4.z = 1; - testk4.w = 2; + testk4.u = 2; if (ht_find(htp, &testk4)) { fleprintf0("Accidentally find one two one two pair\n"); diff --git a/src/test/check_splist.c b/src/test/check_splist.c new file mode 100644 index 0000000..335b383 --- /dev/null +++ b/src/test/check_splist.c @@ -0,0 +1,62 @@ +#include "../splist.h" +#include <stdio.h> + +int +main(U_ATTR int argc, U_ATTR char **argv) +{ + splist *s = new_splist(); + + if (!s) { + eprintf("failed to create a splist!\n"); + exit(1); + } + + unsigned char result = 0; + + result = init_splist(s, 10); + + if (result) { + eprintf("failed to init splist\n"); + exit(1); + } + + if (add_to_splist(s, 2) || + add_to_splist(s, 4) || + add_to_splist(s, 8)) { + eprintf("failed to add to splist!\n"); + exit(1); + } + + print_splist(s); + + /* eprintf("Successfully printed splist\n"); */ + + if (splist_is_member(s, 8)) + eprintf("8 is indeed a member\n"); + else { + eprintf("8 should be a member!\n"); + exit(1); + } + + if (splist_is_member(s, 1)) { + eprintf("1 should not be a member\n"); + exit(1); + } else { + eprintf("1 indeed is not a member!\n"); + } + + reset_splist(s); + + if (splist_is_member(s, 8)) { + eprintf("8 should not be a member now!\n"); + exit(1); + } else { + eprintf("8 is indeed not anymore a member!\n"); + } + + destroy_splist(s); + + eprintf("Successfully destroyed splist\n"); + + return 0; +} diff --git a/src/test/check_tuple.c b/src/test/check_tuple.c new file mode 100644 index 0000000..74a60b2 --- /dev/null +++ b/src/test/check_tuple.c @@ -0,0 +1,244 @@ +#include <stdio.h> +#include "../util.h" +#include "../tuple.h" + +void print_label3(pair3 label) +{ + printf("(%ld, %ld, %ld)\n", label.x, label.y, label.z); +} + +void print_label5(pair5 label) +{ + printf("(%ld, %ld, %ld, %ld, %ld)\n", + label.x, label.y, label.z, label.u, label.v); +} + +void print_label6(pair6 label) +{ + printf("(%ld, %ld, %ld, %ld, %ld, %ld)\n", + label.x, label.y, label.z, label.u, label.v, label.w); +} + +int +main(int UNUSED argc, char ** UNUSED argv) +{ + NUM *lengths = NULL; + + SAFE_MALLOC(NUM, lengths, 3, return 1;); + + *lengths = 2; + *(lengths+1) = 2; + *(lengths+2) = 6; + + luple3 *l3 = new_luple3(lengths); + + printf("Successfully created 3 tuple!\n"); + + if (add_to_luple3(l3, (pair3) { 0 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + if (add_to_luple3(l3, (pair3) { 0, 0, 1 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + if (add_to_luple3(l3, (pair3) { 0, 0, 5 }, 1)) { + fleprintf0("Fail to add to L3\n"); + destroy_luple3(l3); + goto cleanup; + } + + printf("Successfully added to L3\n"); + + NUM *result = luple3_find(l3, (pair3) { 0 }); + + if (result) { + printf("Successfully found value %ld for (0, 0, 0)\n", + *result); + } else { + fleprintf0("fail to find value for (0, 0, 0)\n"); + destroy_luple3(l3); + goto cleanup; + } + + /* the first is one and the rest are all zero */ + result = luple3_find(l3, (pair3) { 1 }); + + if (result) { + fleprintf("Accidentally found value %ld for (1, 0, 0)\n", + *result); + destroy_luple3(l3); + goto cleanup; + } else { + printf("Fail to find value for (1, 0, 0), as expected\n"); + } + + printf("Printing values starting from (0, 0)...\n"); + luple3_map_2(l3, (pair2) { 0 }, print_label3); + + destroy_luple3(l3); + + SAFE_MALLOC(NUM, lengths, 5, goto cleanup;); + + for (int i = 0; i < 4;) *(lengths+i++) = 2; + *(lengths+4) = 10; + + luple5 *l5 = new_luple5(lengths); + + printf("Successfully created 5 tuple!\n"); + + pair5 p5 = (pair5) { 1 }; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + p5.v = 2; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + p5.v = 9; + + if (add_to_luple5(l5, p5, 1)) { + fleprintf0("Fail to add to L5\n"); + destroy_luple5(l5); + goto cleanup; + } + + printf("Successfully added to L5\n"); + + printf("Printing values starting from (1, 0, 0)...\n"); + luple5_map_3(l5, (pair3) { 1 }, print_label5); + + destroy_luple5(l5); + + SAFE_MALLOC(NUM, lengths, 6, return 1;); + + for (int i = 0; i < 5;) *(lengths+i++) = 2; + *(lengths+5) = 10; + + luple6 *l6 = new_luple6(lengths); + + printf("Successfully created 6 tuple!\n"); + + pair6 p6 = (pair6) { 1, 0, 1 }; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 2; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 9; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + p6.w = 7; + + if (add_to_luple6(l6, p6, 1)) { + fleprintf0("Fail to add to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Successfully added to L6\n"); + + if (add_to_luple6_pt_2(l6, (pair2) { 0 })) { + fleprintf0("Fail to add partially to L6\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Successfully added partially to L6\n"); + + result = luple6_find(l6, p6); + + if (result) { + printf("Successfully found value %ld for " + "(1, 0, 1, 0, 0, 0)\n", + *result); + } else { + fleprintf0("fail to find value for " + "(1, 0, 1, 0, 0, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + result = luple6_find(l6, (pair6) { 1, 4 }); + + if (result) { + fleprintf("Accidentally found value %ld for " + "(1, 4, 0, 0, 0, 0)\n", + *result); + destroy_luple6(l6); + goto cleanup; + } else { + printf("Fail to find value for " + "(1, 4, 0, 0, 0, 0), as expected\n"); + } + + if (luple6_pf_2(l6, (pair2) { 1 })) { + printf("Successfully detected existence of (1, 0)\n"); + } else { + fleprintf0("fail to detect existence of (1, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + if (luple6_pf_2(l6, (pair2) { 1, 1 })) { + fleprintf0("Accidentally detected existence of (1, 1)\n"); + destroy_luple6(l6); + goto cleanup; + } else { + printf("Fail to detect existence of (1, 0), as expected\n"); + } + + if (luple6_pf_2(l6, (pair2) { 0 })) { + printf("Successfully detected partial existence of (0, 0)\n"); + } else { + fleprintf0("fail to detect partial existence of (0, 0)\n"); + destroy_luple6(l6); + goto cleanup; + } + + printf("Printing values starting from (1, 0) under 7...\n"); + luple6_map_2(l6, (pair2) { 1 }, 7, print_label6); + + printf("Printing values starting from (1, 0) under 2...\n"); + luple6_map_2(l6, (pair2) { 1 }, 2, print_label6); + + printf("Printing values starting from (1, 0) under 9...\n"); + luple6_map_2(l6, (pair2) { 1 }, 9, print_label6); + + printf("Printing values starting from (1, 0, 1, 0, 0)...\n"); + luple6_map_5(l6, (pair5) { 1, 0, 1 }, print_label6); + + destroy_luple6(l6); + + return 0; + + cleanup: + return 1; +} |