| 1 | #include <stdlib.h> |
| 2 | #include "cgds/List.h" |
| 3 | #include "helpers.h" |
| 4 | #include "lut.h" |
| 5 | |
| 6 | void t_list_clear() |
| 7 | { |
| 8 | List* L = list_new(int); |
| 9 | |
| 10 | list_insert_front(L, 0); |
| 11 | list_insert_back(L, 0); |
| 12 | list_insert_after(L, L->head, 0); |
| 13 | |
| 14 | list_clear(L); |
| 15 | lu_assert(list_empty(L)); |
| 16 | |
| 17 | list_destroy(L); |
| 18 | } |
| 19 | |
| 20 | void t_list_size() |
| 21 | { |
| 22 | List* L = list_new(double); |
| 23 | |
| 24 | list_insert_front(L, 0.0); |
| 25 | list_insert_back(L, 0.0); |
| 26 | list_insert_front(L, 0.0); |
| 27 | lu_assert_int_eq(list_size(L), 3); |
| 28 | |
| 29 | list_insert_back(L, 0.0); |
| 30 | list_insert_before(L, L->tail, 0.0); |
| 31 | lu_assert_int_eq(list_size(L), 5); |
| 32 | |
| 33 | list_insert_after(L, L->head->next, 0.0); |
| 34 | list_insert_before(L, L->tail->prev, 0.0); |
| 35 | list_insert_after(L, L->head, 0.0); |
| 36 | lu_assert_int_eq(list_size(L), 8); |
| 37 | |
| 38 | list_set(L, L->head->next, 42); |
| 39 | list_remove_front(L); |
| 40 | int a; |
| 41 | list_get(L->head, a); |
| 42 | lu_assert_int_eq(a, 42); |
| 43 | list_remove(L, L->head->next); |
| 44 | lu_assert_int_eq(list_size(L), 6); |
| 45 | list_set(L, L->tail, 32); |
| 46 | list_remove(L, L->tail->prev); |
| 47 | list_get(L->tail, a); |
| 48 | lu_assert_int_eq(a, 32); |
| 49 | lu_assert_int_eq(list_size(L), 5); |
| 50 | |
| 51 | list_destroy(L); |
| 52 | } |
| 53 | |
| 54 | void t_list_push_pop_basic() |
| 55 | { |
| 56 | int n = 10; |
| 57 | |
| 58 | List* L = list_new(double); |
| 59 | for (int i = 0; i < n; i++) list_insert_back(L, (double) i); |
| 60 | // iterate and check values |
| 61 | ListIterator* li = list_get_iterator(L); |
| 62 | double ckValue = 0.0; |
| 63 | while (listI_has_data(li)) |
| 64 | { |
| 65 | double d; |
| 66 | listI_get(li, d); |
| 67 | lu_assert_dbl_eq(d, ckValue); |
| 68 | ckValue += 1.0; |
| 69 | listI_move_next(li); |
| 70 | } |
| 71 | |
| 72 | // same, from end to beginning |
| 73 | ckValue = n - 1; |
| 74 | listI_reset_tail(li); |
| 75 | while (listI_has_data(li)) |
| 76 | { |
| 77 | double d; |
| 78 | listI_get(li, d); |
| 79 | lu_assert_dbl_eq(d, ckValue); |
| 80 | ckValue -= 1.0; |
| 81 | listI_move_prev(li); |
| 82 | } |
| 83 | list_destroy(L); |
| 84 | listI_destroy(li); |
| 85 | } |
| 86 | |
| 87 | void t_list_push_pop_evolved() |
| 88 | { |
| 89 | int n = 10; |
| 90 | |
| 91 | List* L = list_new(StructTest1); |
| 92 | StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); |
| 93 | for (int i = 0; i < n; i++) |
| 94 | { |
| 95 | st1[i].a = rand() % 42; |
| 96 | st1[i].b = (double) rand() / RAND_MAX; |
| 97 | list_insert_back(L, *(st1 + i)); |
| 98 | } |
| 99 | ListCell* lc = L->head; |
| 100 | for (int i = 0; i < n; i++) |
| 101 | { |
| 102 | //another way to access elements |
| 103 | StructTest1 st1Cell; |
| 104 | list_get(lc, st1Cell); |
| 105 | lu_assert_int_eq(st1Cell.a, st1[i].a); |
| 106 | lu_assert_dbl_eq(st1Cell.b, st1[i].b); |
| 107 | lc = lc->next; |
| 108 | } |
| 109 | safe_free(st1); |
| 110 | list_destroy(L); |
| 111 | |
| 112 | L = list_new(StructTest2*); |
| 113 | StructTest2* st2 = (StructTest2*) malloc(n * sizeof (StructTest2)); |
| 114 | for (int i = 0; i < n; i++) |
| 115 | { |
| 116 | st2[i].a = (float) rand() / RAND_MAX; |
| 117 | st2[i].b = (StructTest1*) malloc(sizeof (StructTest1)); |
| 118 | st2[i].b->a = rand() % 42; |
| 119 | st2[i].b->b = (double) rand() / RAND_MAX; |
| 120 | list_insert_back(L, st2 + i); |
| 121 | } |
| 122 | lc = L->head; |
| 123 | for (int i = 0; i < n; i++) |
| 124 | { |
| 125 | StructTest2* st2Cell; |
| 126 | list_get(lc, st2Cell); |
| 127 | lu_assert_dbl_eq(st2Cell->a, st2[i].a); |
| 128 | lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); |
| 129 | lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); |
| 130 | safe_free(st2Cell->b); |
| 131 | lc = lc->next; |
| 132 | } |
| 133 | safe_free(st2); |
| 134 | list_destroy(L); |
| 135 | } |
| 136 | |
| 137 | void t_list_copy() |
| 138 | { |
| 139 | int n = 10; |
| 140 | |
| 141 | List* L = list_new(int); |
| 142 | for (int i = 0; i < n; i++) |
| 143 | list_insert_front(L, rand() % 42); |
| 144 | List* Lc = list_copy(L); |
| 145 | |
| 146 | lu_assert_int_eq(L->size, Lc->size); |
| 147 | ListCell* lCell = L->head; |
| 148 | ListCell* lcCell = Lc->head; |
| 149 | int a, b; |
| 150 | for (int i = 0; i < n; i++) |
| 151 | { |
| 152 | list_get(lCell, a); |
| 153 | list_get(lcCell, b); |
| 154 | lu_assert_int_eq(a, b); |
| 155 | lCell = lCell->next; |
| 156 | lcCell = lcCell->next; |
| 157 | } |
| 158 | list_destroy(L); |
| 159 | list_destroy(Lc); |
| 160 | } |