3 #include "test/helpers.h"
8 List
* L
= list_new(int);
10 list_insert_front(L
, 0);
11 list_insert_back(L
, 0);
12 list_insert_after(L
, L
->head
, 0);
15 lu_assert(list_empty(L
));
22 List
* L
= list_new(double);
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);
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);
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);
38 list_set(L
, L
->head
->next
, 42);
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
);
48 lu_assert_int_eq(a
, 32);
49 lu_assert_int_eq(list_size(L
), 5);
54 void t_list_push_pop_basic()
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
);
63 while (listI_has_data(li
))
67 lu_assert_dbl_eq(d
, ckValue
);
72 // same, from end to beginning
75 while (listI_has_data(li
))
79 lu_assert_dbl_eq(d
, ckValue
);
87 void t_list_push_pop_evolved()
91 List
* L
= list_new(StructTest1
);
92 StructTest1
* st1
= (StructTest1
*) malloc(n
* sizeof (StructTest1
));
93 for (int i
= 0; i
< n
; i
++)
95 st1
[i
].a
= rand() % 42;
96 st1
[i
].b
= (double) rand() / RAND_MAX
;
97 list_insert_back(L
, *(st1
+ i
));
99 ListCell
* lc
= L
->head
;
100 for (int i
= 0; i
< n
; i
++)
102 //another way to access elements
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
);
112 L
= list_new(StructTest2
*);
113 StructTest2
* st2
= (StructTest2
*) malloc(n
* sizeof (StructTest2
));
114 for (int i
= 0; i
< n
; i
++)
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
);
123 for (int i
= 0; i
< n
; i
++)
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
);
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
);
146 lu_assert_int_eq(L
->size
, Lc
->size
);
147 ListCell
* lCell
= L
->head
;
148 ListCell
* lcCell
= Lc
->head
;
150 for (int i
= 0; i
< n
; i
++)
154 lu_assert_int_eq(a
, b
);
156 lcCell
= lcCell
->next
;