7579afbe0804f91524eb437b25c87eddbfc0eada
2 #include "cgds/Vector.h"
8 Vector
* v
= vector_new(int);
9 lu_assert(vector_empty(v
));
23 lu_assert(vector_empty(v
));
30 Vector
* v
= vector_new(int);
31 lu_assert(vector_empty(v
));
36 lu_assert_int_eq(vector_size(v
), 3);
40 lu_assert_int_eq(vector_size(v
), 5);
45 lu_assert_int_eq(vector_size(v
), 8);
50 void t_vector_push_pop_basic()
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
);
59 while (vectorI_has_data(vi
))
63 lu_assert_dbl_eq(d
, ckValue
);
65 vectorI_move_next(vi
);
68 // Same, from end to beginning
70 vectorI_reset_end(vi
);
71 while (vectorI_has_data(vi
))
75 lu_assert_dbl_eq(d
, ckValue
);
77 vectorI_move_prev(vi
);
83 void t_vector_push_pop_evolved()
87 Vector
* v
= vector_new(StructTest1
);
88 StructTest1
* st1
= (StructTest1
*) malloc(n
* sizeof (StructTest1
));
89 for (int i
= 0; i
< n
; i
++)
91 st1
[i
].a
= random() % 42;
92 st1
[i
].b
= (double) random() / RAND_MAX
;
93 vector_push(v
, *(st1
+ i
));
95 for (int i
= 0; i
< n
; i
++)
97 // Another way to access elements
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
);
106 v
= vector_new(StructTest2
*);
107 StructTest2
* st2
= (StructTest2
*) malloc(n
* sizeof (StructTest2
));
108 for (int i
= 0; i
< n
; i
++)
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
);
116 for (int i
= 0; i
< n
; i
++)
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
);
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
);
138 lu_assert_int_eq(v
->size
, vc
->size
);
140 for (int i
= 0; i
< n
; i
++)
143 vector_get(vc
, i
, b
);
144 lu_assert_int_eq(a
, b
);