#include #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; spa *spap = NULL; prodecor *prod = NULL; NUM input_len = 10; /* check 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))) { if (crf_add_node(crfp, p)) { fleprintf0("fail to add node\n"); goto cleanup; } else { printf("Successfully add node (0, 1)\n"); } } if (!(crf_find_node(crfp, p))) { fleprintf0("Fail to find node\n"); goto cleanup; } else { printf("Successfully find node for (0, 1)\n"); } pair4 p4 = { 0 }; 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, 0)\n"); } p4.u = 1; 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"); } 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"); } 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(g, input_len); if (spap == NULL) { fleprintf0("fail to create new spa\n"); goto cleanup; } pair3 p3 = (pair3) { 0, 1, 9 }; if (spa_insert(spap, p3)) { fleprintf0("Fail to insert\n"); goto cleanup; } printf("Successfully inserted into SPA\n"); if (spa_belong(spap, p3)) { printf("Successfully found p3\n"); } else { fleprintf0("Fail to find p3\n"); goto cleanup; } pair2 first_two = (pair2) { 0, 1 }; spa_map(spap, first_two, print_label3); /* check prodecor */ BOOL error = 0; pr = new_procr(g, input_len, &error); pu = new_procu(g, input_len, &error); printf("Successfully created procr and procu\n"); p4 = (pair4) { 0 }; p4.x = 1; if (desc_add(pr, pu, 2, p4)) { fleprintf0("Fail to add descriptor\n"); goto cleanup; } printf("Successfully added to procr and procu\n"); print_procr(pr); NUM grade = 0; prod = pop_procr(pr, pu, &grade); if (prod == NULL) { printf("The first pop failed. " "But maybe it occurs due to some misses in the minimal " "weight.\n"); printf("So we shall pop again\n"); prod = pop_procr(pr, pu, &grade); if (prod == NULL) { fleprintf0("Fail to pop\n"); goto cleanup; } } printf("popped a process descriptor: (%ld, %ld, %ld, %ld, %ld)\n", prod->x, prod->y, prod->z, prod->u, grade); cleanup: 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 (prod) free(prod); return 0; }