Commit | Line | Data |
---|---|---|
a7868768 BA |
1 | #include <stdlib.h> |
2 | #include "cgds/List.h" | |
aef8a996 BA |
3 | #include "helpers.h" |
4 | #include "lut.h" | |
a7868768 | 5 | |
4029c991 | 6 | void t_list_clear() |
a7868768 | 7 | { |
e45132ac | 8 | List* L = list_new(int); |
a7868768 | 9 | |
e45132ac BA |
10 | list_insert_front(L, 0); |
11 | list_insert_back(L, 0); | |
12 | list_insert_after(L, L->head, 0); | |
a7868768 | 13 | |
e45132ac BA |
14 | list_clear(L); |
15 | lu_assert(list_empty(L)); | |
a7868768 | 16 | |
e45132ac | 17 | list_destroy(L); |
a7868768 BA |
18 | } |
19 | ||
4029c991 | 20 | void t_list_size() |
a7868768 | 21 | { |
e45132ac BA |
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); | |
a7868768 BA |
52 | } |
53 | ||
4029c991 | 54 | void t_list_push_pop_basic() |
a7868768 | 55 | { |
e45132ac BA |
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); | |
a7868768 BA |
85 | } |
86 | ||
4029c991 | 87 | void t_list_push_pop_evolved() |
a7868768 | 88 | { |
e45132ac BA |
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); | |
a7868768 BA |
135 | } |
136 | ||
4029c991 | 137 | void t_list_copy() |
a7868768 | 138 | { |
e45132ac BA |
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); | |
a7868768 | 160 | } |