6 UInt
getHash_int(void* item
, size_t hashSize
) {
7 return *((int*)item
) % hashSize
;
12 Set
* s
= set_new(int, 16, getHash_int
);
13 lu_assert(set_empty(s
));
20 s
= set_new(int, 8, getHash_int
);
27 lu_assert(set_empty(s
));
34 Set
* s
= set_new(int, 16, getHash_int
);
35 lu_assert(set_empty(s
));
40 lu_assert_int_eq(set_size(s
), 3);
44 lu_assert_int_eq(set_size(s
), 5);
49 lu_assert_int_eq(set_size(s
), 8);
54 void t_set_add_remove_basic()
58 Set
* s
= set_new(double, 4, NULL
);
59 for (double i
= 0.0; i
< n
; i
++)
61 lu_assert_int_eq(set_size(s
), n
);
64 for (double i
= 0.0; i
< n
; i
++)
65 lu_assert(set_has(s
, &i
));
68 for (double i
= 0.0; i
< n
; i
++)
70 lu_assert_int_eq(set_size(s
), 0);
75 void t_set_getnull_modify()
79 Set
* s
= set_new(StructTest1
, 4, NULL
);
80 // NOTE: using calloc() because probably StructTest1 is 4 + 8 bytes,
81 // aligned to 8 + 8 ==> 4 are left uninitialized ==> memcpy compares
82 // some junk values. TODO: custom equality function instead.
83 StructTest1
* st1
= (StructTest1
*) calloc(n
, sizeof(StructTest1
));
84 for (int i
= 0; i
< n
; i
++)
86 st1
[i
].a
= random() % 42;
87 st1
[i
].b
= (double) random() / RAND_MAX
;
88 set_add(s
, *(st1
+ i
));
90 for (int i
= 0; i
< n
; i
++)
92 // Another way to access elements
93 StructTest1
* st1Cell
= (StructTest1
*) calloc(1, sizeof(StructTest1
));
94 st1Cell
->a
= st1
[7].a
;
95 st1Cell
->b
= st1
[7].b
;
96 lu_assert(set_has(s
, st1Cell
));
101 StructTest1
* stmp
= (StructTest1
*) calloc(1, sizeof(StructTest1
));
104 lu_assert(!set_has(s
, stmp
));
106 lu_assert(set_has(s
, st1
+ 4));
116 Set
* s
= set_new(int, 8, getHash_int
);
117 for (int i
= 0; i
< n
; i
++)
119 Set
* sc
= set_copy(s
);
121 lu_assert_int_eq(s
->size
, sc
->size
);
123 for (int i
= 0; i
< n
; i
++) {
125 lu_assert(set_has(sc
, &item
));
135 Set
* s
= set_new(int, 8, getHash_int
);
136 for (int i
= 0; i
< n
; i
++)
139 Vector
* v
= set_to_vector(s
);
140 lu_assert(vector_size(v
) == set_size(s
));