summaryrefslogtreecommitdiff
path: root/src/test/check_list.c
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-11-08 16:37:57 +0800
committerJSDurand <mmemmew@gmail.com>2021-11-08 16:37:57 +0800
commit9594210f02572681ed581c5197ace4c207db0917 (patch)
tree08bf1bf079d111c64cf3128dd68323abdce78228 /src/test/check_list.c
initial commit
Now the rough framework is established and the grammar class is sort of ready. It remains to write a general input reading mechanism.
Diffstat (limited to 'src/test/check_list.c')
-rw-r--r--src/test/check_list.c217
1 files changed, 217 insertions, 0 deletions
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;
+}