Some fixes + improvements (Vector) + code reformatting
[cgds.git] / test / t.Vector.c
... / ...
CommitLineData
1#include <stdlib.h>
2#include "cgds/Vector.h"
3#include "helpers.h"
4#include "lut.h"
5
6void t_vector_clear()
7{
8 Vector* v = vector_new(int);
9 lu_assert(vector_empty(v));
10
11 vector_push(v, 0);
12 vector_push(v, 0);
13 vector_push(v, 0);
14
15 vector_destroy(v);
16 v = vector_new(int);
17
18 vector_push(v, 0);
19 vector_push(v, 0);
20 vector_push(v, 0);
21
22 vector_clear(v);
23 lu_assert(vector_empty(v));
24
25 vector_destroy(v);
26}
27
28void t_vector_size()
29{
30 Vector* v = vector_new(int);
31 lu_assert(vector_empty(v));
32
33 vector_push(v, 0);
34 vector_push(v, 0);
35 vector_push(v, 0);
36 lu_assert_int_eq(vector_size(v), 3);
37
38 vector_push(v, 0);
39 vector_push(v, 0);
40 lu_assert_int_eq(vector_size(v), 5);
41
42 vector_push(v, 0);
43 vector_push(v, 0);
44 vector_push(v, 0);
45 lu_assert_int_eq(vector_size(v), 8);
46
47 vector_destroy(v);
48}
49
50void t_vector_push_pop_basic()
51{
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);
81}
82
83void t_vector_push_pop_evolved()
84{
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);
127}
128
129void t_vector_copy()
130{
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);
148}