summaryrefslogtreecommitdiff
path: root/src/test/check_crf.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/check_crf.c')
-rw-r--r--src/test/check_crf.c195
1 files changed, 159 insertions, 36 deletions
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;