Commit | Line | Data |
---|---|---|
a7868768 BA |
1 | #include <stdlib.h> |
2 | #include "cgds/Vector.h" | |
aef8a996 BA |
3 | #include "helpers.h" |
4 | #include "lut.h" | |
a7868768 | 5 | |
4029c991 | 6 | void t_vector_clear() |
a7868768 | 7 | { |
e45132ac BA |
8 | Vector* v = vector_new(int); |
9 | lu_assert(vector_empty(v)); | |
a7868768 | 10 | |
e45132ac BA |
11 | vector_push(v, 0); |
12 | vector_push(v, 0); | |
13 | vector_push(v, 0); | |
a7868768 | 14 | |
e45132ac BA |
15 | vector_destroy(v); |
16 | v = vector_new(int); | |
a7868768 | 17 | |
e45132ac BA |
18 | vector_push(v, 0); |
19 | vector_push(v, 0); | |
20 | vector_push(v, 0); | |
a7868768 | 21 | |
e45132ac BA |
22 | vector_clear(v); |
23 | lu_assert(vector_empty(v)); | |
a7868768 | 24 | |
e45132ac | 25 | vector_destroy(v); |
a7868768 BA |
26 | } |
27 | ||
4029c991 | 28 | void t_vector_size() |
a7868768 | 29 | { |
e45132ac BA |
30 | Vector* v = vector_new(int); |
31 | lu_assert(vector_empty(v)); | |
a7868768 | 32 | |
e45132ac BA |
33 | vector_push(v, 0); |
34 | vector_push(v, 0); | |
35 | vector_push(v, 0); | |
36 | lu_assert_int_eq(vector_size(v), 3); | |
a7868768 | 37 | |
e45132ac BA |
38 | vector_push(v, 0); |
39 | vector_push(v, 0); | |
40 | lu_assert_int_eq(vector_size(v), 5); | |
a7868768 | 41 | |
e45132ac BA |
42 | vector_push(v, 0); |
43 | vector_push(v, 0); | |
44 | vector_push(v, 0); | |
45 | lu_assert_int_eq(vector_size(v), 8); | |
a7868768 | 46 | |
e45132ac | 47 | vector_destroy(v); |
a7868768 BA |
48 | } |
49 | ||
4029c991 | 50 | void t_vector_push_pop_basic() |
a7868768 | 51 | { |
e45132ac BA |
52 | int n = 10; |
53 | ||
54 | Vector* v = vector_new(double); | |
55 | for (int i = 0; i < n; i++) vector_push(v, (double) i); | |
56 | // iterate and check values | |
57 | VectorIterator* vi = vector_get_iterator(v); | |
58 | double ckValue = 0.0; | |
59 | while (vectorI_has_data(vi)) | |
60 | { | |
61 | double d; | |
62 | vectorI_get(vi, d); | |
63 | lu_assert_dbl_eq(d, ckValue); | |
64 | ckValue += 1.0; | |
65 | vectorI_move_next(vi); | |
66 | } | |
67 | ||
68 | // same, from end to beginning | |
69 | ckValue = n - 1; | |
70 | vectorI_reset_end(vi); | |
71 | while (vectorI_has_data(vi)) | |
72 | { | |
73 | double d; | |
74 | vectorI_get(vi, d); | |
75 | lu_assert_dbl_eq(d, ckValue); | |
76 | ckValue -= 1.0; | |
77 | vectorI_move_prev(vi); | |
78 | } | |
79 | vector_destroy(v); | |
80 | vectorI_destroy(vi); | |
a7868768 BA |
81 | } |
82 | ||
4029c991 | 83 | void t_vector_push_pop_evolved() |
a7868768 | 84 | { |
e45132ac BA |
85 | int n = 10; |
86 | ||
87 | Vector* v = vector_new(StructTest1); | |
88 | StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); | |
89 | for (int i = 0; i < n; i++) | |
90 | { | |
91 | st1[i].a = random() % 42; | |
92 | st1[i].b = (double) random() / RAND_MAX; | |
93 | vector_push(v, *(st1 + i)); | |
94 | } | |
95 | for (int i = 0; i < n; i++) | |
96 | { | |
97 | //another way to access elements | |
98 | StructTest1 st1Cell; | |
99 | vector_get(v, i, st1Cell); | |
100 | lu_assert_int_eq(st1Cell.a, st1[i].a); | |
101 | lu_assert_dbl_eq(st1Cell.b, st1[i].b); | |
102 | } | |
103 | safe_free(st1); | |
104 | vector_destroy(v); | |
105 | ||
106 | v = vector_new(StructTest2*); | |
107 | StructTest2* st2 = (StructTest2*) malloc(n * sizeof (StructTest2)); | |
108 | for (int i = 0; i < n; i++) | |
109 | { | |
110 | st2[i].a = (float) random() / RAND_MAX; | |
111 | st2[i].b = (StructTest1*) malloc(sizeof (StructTest1)); | |
112 | st2[i].b->a = random() % 42; | |
113 | st2[i].b->b = (double) random() / RAND_MAX; | |
114 | vector_push(v, st2 + i); | |
115 | } | |
116 | for (int i = 0; i < n; i++) | |
117 | { | |
118 | StructTest2* st2Cell; | |
119 | vector_get(v, i, st2Cell); | |
120 | lu_assert_dbl_eq(st2Cell->a, st2[i].a); | |
121 | lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); | |
122 | lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); | |
123 | safe_free(st2Cell->b); | |
124 | } | |
125 | safe_free(st2); | |
126 | vector_destroy(v); | |
a7868768 BA |
127 | } |
128 | ||
4029c991 | 129 | void t_vector_copy() |
a7868768 | 130 | { |
e45132ac BA |
131 | int n = 10; |
132 | ||
133 | Vector* v = vector_new(int); | |
134 | for (int i = 0; i < n; i++) | |
135 | vector_push(v, random() % 42); | |
136 | Vector* vc = vector_copy(v); | |
137 | ||
138 | lu_assert_int_eq(v->size, vc->size); | |
139 | int a, b; | |
140 | for (int i = 0; i < n; i++) | |
141 | { | |
142 | vector_get(v, i, a); | |
143 | vector_get(vc, i, b); | |
144 | lu_assert_int_eq(a, b); | |
145 | } | |
146 | vector_destroy(v); | |
147 | vector_destroy(vc); | |
a7868768 | 148 | } |