diff options
Diffstat (limited to 'src/test')
-rw-r--r-- | src/test/check_grammar.c | 35 | ||||
-rw-r--r-- | src/test/check_list.c | 217 |
2 files changed, 252 insertions, 0 deletions
diff --git a/src/test/check_grammar.c b/src/test/check_grammar.c new file mode 100644 index 0000000..1fe27dd --- /dev/null +++ b/src/test/check_grammar.c @@ -0,0 +1,35 @@ +#include <stdio.h> +#include <stdlib.h> +#include "../grammar.h" + +int main(U_ATTR int argc, U_ATTR char **argv) +{ + /* check new_tnt and print it */ + TNT *tnt = new_tnt(1, 12); + + printf("Print a TNT value of type NT: "); + print_tnt(tnt); + printf("\n"); + + free(tnt); + + /* check new_tnt_string */ + + List *tnt_string = new_tnt_string("tntnt", 5, + (T) 1, (NT) 2, (T) 3, (NT) 4, (T) 15); + + if (!tnt_string) { + eprintf("error!\n"); + return 1; + } + + /* check new_rule, print_rule, and destroy_rule. */ + + Rule *rule = new_rule(1, tnt_string); + + print_rule(rule); + + destroy_rule(rule); + + return 0; +} diff --git a/src/test/check_list.c b/src/test/check_list.c new file mode 100644 index 0000000..8677cd3 --- /dev/null +++ b/src/test/check_list.c @@ -0,0 +1,217 @@ +#include <stdio.h> +#include <stdlib.h> +#include "../list.h" + +void +print_int(void *p) +{ + printf("%d\n", *((int *)p)); +} + +int +main(U_ATTR int argc, U_ATTR char **argv) +{ + List *ls = new_list(); + + if (!ls) { + eprintf("%s:%d, failed to create a new list\n", + __FILE__, __LINE__); + exit(1); + } + + unsigned char result = 0; + + int x[] = { 1, 2, 3, 4, 1 }; + + for (size_t i = 0; i < sizeof(x) / sizeof(*x); i++) { + if (add_to_list(ls, x+i)) { + eprintf("failed to add %ld-th element to LS\n", + i); + exit(1); + } + } + + print_list(ls, print_int); + + NUM expected_ls_len = sizeof(x) / sizeof(*x); + + if (list_length(ls) != expected_ls_len) { + eprintf("The length of the list is wrong!\n"); + exit(1); + } + + eprintf("The length is correct!\n"); + + NUM array_len = 0; + int *array = list_to_array(ls, sizeof(*array), &array_len); + + if (!array) { + eprintf("%s:%d, failed to convert the list to an array\n", + __FILE__, __LINE__); + exit(1); + } + + if (array_len != expected_ls_len) { + eprintf("The length of the array is wrong!\n"); + exit(1); + } + + eprintf("The length of the array is correct!\n"); + + for (NUM i = 0; i < expected_ls_len; i++) { + if (*(array+i) != *(x+i)) { + eprintf("The %lu-th element of array = %d, x = %d\n", + i, *(array+i), *(x+i)); + exit(1); + } + } + + eprintf("Every element of the array is as expected!\n"); + + for (NUM i = 0; i < expected_ls_len; i++) { + int *pointer = pop_from_list(ls); + + if (!pointer) { + eprintf("%s:%d, i = %ld, Popping returns NULL\n", + __FILE__, __LINE__, i); + exit(1); + } + + if (*pointer != *(x+expected_ls_len-i-1)) { + eprintf("i = %ld, *pointer = %d, and x = %d\n", + i, *pointer, *(x+expected_ls_len-i-1)); + exit(1); + } + } + + eprintf("pop_from_list works as expected.\n"); + + print_list(ls, print_int); + + expected_ls_len = 5; + + for (NUM i = 0; i < expected_ls_len; i++) { + int *pointer = malloc(sizeof *pointer * 1); + + if (!pointer) { + eprintf("%s:%d, i = %ld, failed to malloc\n", + __FILE__, __LINE__, i); + exit(1); + } + + *pointer = expected_ls_len-1-i; + if (add_to_list(ls, pointer)) { + eprintf("%s:%d, i = %ld, failed to add element\n", + __FILE__, __LINE__, i); + exit(1); + } + } + + print_list(ls, print_int); + + destroy_list(ls, 1); + + eprintf("Successfully destroyed the list and freed every element\n"); + + free(array); + + ls = new_list(); + + if (!ls) { + eprintf("%s:%d, failed to create a new list\n", + __FILE__, __LINE__); + exit(1); + } + + if (list_assure_size(ls, 513)) { + eprintf("%s:%d, failed to assure size\n", + __FILE__, __LINE__); + exit(1); + } + + if (list_set_length(ls, 513)) { + eprintf("%s:%d, failed to set length\n", + __FILE__, __LINE__); + exit(1); + } + + print_list(ls, print_int); + + destroy_list(ls, 2); + + eprintf("Successfully destroyed the list\n"); + + if (!(ls = new_list())) { + eprintf("%s:%d, failed to create a new list\n", + __FILE__, __LINE__); + exit(1); + } + + List *ls2 = new_list(); + + if (!ls2) { + eprintf("%s:%d, failed to create a new list\n", + __FILE__, __LINE__); + exit(1); + } + + for (NUM i = 0; i < 5; i++) { + int *pointer = malloc(sizeof *pointer * 1); + + if (!pointer) { + eprintf("%s:%d, failed to malloc\n", + __FILE__, __LINE__); + exit(1); + } + + *pointer = 4-i; + + if (add_to_list(ls2, pointer)) { + eprintf("%s:%d, i = %ld, failed to add to list\n", + __FILE__, __LINE__, i); + exit(1); + } + } + + print_list(ls2, print_int); + + result = copy_list(ls, ls2, copy_num); + + if (result) { + eprintf("%s:%d, failed to copy list\n", + __FILE__, __LINE__); + exit(1); + } + + destroy_list(ls2, 1); + destroy_list(ls, 1); + + eprintf("Successfully destroyed lists!\n"); + + /* test a list with pointers in an array */ + + if (!(ls = new_list())) { + eprintf("%s:%d, failed to create a new list\n", + __FILE__, __LINE__); + exit(1); + } + + NUM arr[10]; + + for (NUM i = 0; i < 10; i++) { + arr[i] = i*i-i+1; + if (add_to_list(ls, arr+i)) { + eprintf("%s:%d, i = %ld, failed to add element\n", + __FILE__, __LINE__, i); + exit(1); + } + } + + print_list(ls, print_int); + + destroy_list(ls, 0); + + printf("Every test is successful!\n"); + + return 0; +} |