From 55dc897da6e81f2a26cfc7e66ac942824773498b Mon Sep 17 00:00:00 2001 From: JSDurand Date: Tue, 4 Jan 2022 11:51:58 +0800 Subject: temporary commit Now we can read grammars from a file. But we need to check if it works for reading strings still. --- src/test/check_grammar.c | 36 +++++++++++++++++++++++++++++------- src/test/check_list.c | 29 ++++++++++++++++++++++++++--- src/test/check_reader.c | 32 ++++++++++++++++++++++---------- 3 files changed, 77 insertions(+), 20 deletions(-) (limited to 'src/test') diff --git a/src/test/check_grammar.c b/src/test/check_grammar.c index 39f66ee..98a07cd 100644 --- a/src/test/check_grammar.c +++ b/src/test/check_grammar.c @@ -37,8 +37,7 @@ main(U_ATTR int argc, U_ATTR char **argv) List *rules = new_list(); List *names = new_list(); - - for (int i = 0; i < 3; i++) { + for (int i = 0; i < 4; i++) { tnt_string = new_tnt_string("ntn", 3, (NT) 3*(i+1), (T) 3*(i+1)*(i+1), @@ -48,18 +47,41 @@ main(U_ATTR int argc, U_ATTR char **argv) eprintf("i = %d, cannot create tnt string\n", i); map_list(rules, destroy_rule_and_free_all); - destroy_list(rules, 0); - destroy_list(names, 1); + + map_list(names, destroy_cpa_and_free_all); + destroy_list(names, 0); return 1; } - rule = new_rule(i, tnt_string); + rule = new_rule(i%3, tnt_string); add_to_list(rules, rule); char *name = MYALLOC(char, 7); snprintf(name, 7, "Rule %d", i); - add_to_list(names, name); + + utf8* uname = new_utf8(name, 6); + + str_info info = get_info((str *)uname, 0); + + NUM *varray = MYALLOC(NUM, 6); + NUM vindex = 0; + + for (NUM index = 0; + info.value >= 0 && index < str_length((str *) uname); + index += info.step, vindex++) { + info = get_info((str *)uname, index); + + *(varray+vindex) = info.value; + } + + destroy_str((str *) uname, 1); + + cpa *cpap = MYALLOC(cpa, 1); + cpap->array = varray; + cpap->size = vindex; + + add_to_list(names, cpap); } Grammar *g = new_grammar(); @@ -70,7 +92,7 @@ main(U_ATTR int argc, U_ATTR char **argv) destroy_grammar(g, 1); - free(g); + destroy_list(rules, 1); return 0; } diff --git a/src/test/check_list.c b/src/test/check_list.c index 8677cd3..fe23b2b 100644 --- a/src/test/check_list.c +++ b/src/test/check_list.c @@ -2,7 +2,7 @@ #include #include "../list.h" -void +static void print_int(void *p) { printf("%d\n", *((int *)p)); @@ -155,6 +155,8 @@ main(U_ATTR int argc, U_ATTR char **argv) exit(1); } + eprintf("Successfully created LS2!\n"); + for (NUM i = 0; i < 5; i++) { int *pointer = malloc(sizeof *pointer * 1); @@ -197,7 +199,7 @@ main(U_ATTR int argc, U_ATTR char **argv) } NUM arr[10]; - + for (NUM i = 0; i < 10; i++) { arr[i] = i*i-i+1; if (add_to_list(ls, arr+i)) { @@ -211,7 +213,28 @@ main(U_ATTR int argc, U_ATTR char **argv) destroy_list(ls, 0); + /* test array_to_list */ + int *arr3 = MYALLOC(int, 20); + + void **arr2 = NULL; + + for (NUM i = 0; i < 20; i++) { + *(arr3+i) = i*i+i+1; + } + + arr2 = MYALLOC(void *, 20); + + for (NUM i = 0; i < 20; i++) *(arr2+i) = arr3+i; + + ls = array_to_list(arr2, 20); + + printf("Printing a list from an array\n"); + + map_list(ls, print_int); + + destroy_list(ls, 2); + printf("Every test is successful!\n"); - + return 0; } diff --git a/src/test/check_reader.c b/src/test/check_reader.c index 1d73b3f..8769cd7 100644 --- a/src/test/check_reader.c +++ b/src/test/check_reader.c @@ -6,17 +6,29 @@ #include "../grammar.h" #include "../reader.h" -/* The test of reading grammars should be postponed till later. */ - -/* Grammar * - * read_grammar(List *args) - * { - * - * } */ - - +/* TODO: check string */ int main(U_ATTR int argc, U_ATTR char **argv) { - return 77; + /* return 77; */ + + char *file_name = "test.txt"; + char *buffer = MYALLOC(char, 512); + NUM buffer_size = 0; + + if (read_entire_file(file_name, &buffer, &buffer_size)) { + fleprintf("%s:%d, Cannot read file %s", file_name); + free(buffer); + return 1; + } + + utf8 *s = new_utf8(buffer, buffer_size); + + Grammar *g = read_grammar_from_bnf((str *) s); + + print_grammar(g); + destroy_grammar(g, 2); + destroy_str((str *)s, 1); + + return 0; } -- cgit v1.2.3-18-g5258