#include #include #include "../list.h" static 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); } eprintf("Successfully created LS2!\n"); 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); /* 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; }