1 /*
2 * Testcase for bitmap
3 */
4
5 #include "opal_config.h"
6
7 #include <stdio.h>
8 #include "support.h"
9
10 #include "opal/class/opal_bitmap.h"
11 #include "opal/constants.h"
12
13 #define BSIZE 26
14 #define OPAL_INVALID_BIT -1
15 #define ERR_CODE -2
16
17 #define PRINT_VALID_ERR \
18 fprintf(error_out, "================================ \n"); \
19 fprintf(error_out, "This is suppossed to throw error \n"); \
20 fprintf(error_out, "================================ \n")
21
22 static void test_bitmap_set(opal_bitmap_t *bm);
23 static void test_bitmap_clear(opal_bitmap_t *bm);
24 static void test_bitmap_is_set(opal_bitmap_t *bm);
25 static void test_bitmap_clear_all(opal_bitmap_t *bm);
26 static void test_bitmap_set_all(opal_bitmap_t *bm);
27 static void test_bitmap_find_and_set(opal_bitmap_t *bm);
28
29
30 static int set_bit(opal_bitmap_t *bm, int bit);
31 static int clear_bit(opal_bitmap_t *bm, int bit);
32 static int is_set_bit(opal_bitmap_t *bm, int bit);
33 static int clear_all(opal_bitmap_t *bm);
34 static int set_all(opal_bitmap_t *bm);
35 static int find_and_set(opal_bitmap_t *bm, int bit);
36
37 #define WANT_PRINT_BITMAP 0
38 #if WANT_PRINT_BITMAP
39 static void print_bitmap(opal_bitmap_t *bm);
40 #endif
41
42 static FILE *error_out=NULL;
43
main(int argc,char * argv[])44 int main(int argc, char *argv[])
45 {
46 /* Local variables */
47 opal_bitmap_t bm;
48 int err;
49
50 /* Perform overall test initialization */
51 test_init("opal_bitmap_t");
52
53 #ifdef STANDALONE
54 error_out = stderr;
55 #else
56 error_out = fopen( "./opal_bitmap_test_out.txt", "w" );
57 if( error_out == NULL ) error_out = stderr;
58 #endif
59
60 /* Initialize bitmap */
61 OBJ_CONSTRUCT(&bm, opal_bitmap_t);
62 PRINT_VALID_ERR;
63 err = opal_bitmap_init(NULL, 2);
64 if (err == OPAL_ERR_BAD_PARAM)
65 fprintf(error_out, "ERROR: Initialization of bitmap failed\n\n");
66
67 PRINT_VALID_ERR;
68 err = opal_bitmap_init(&bm, -1);
69 if (err == OPAL_ERR_BAD_PARAM)
70 fprintf(error_out, "ERROR: Initialization of bitmap failed \n\n");
71
72 err = opal_bitmap_init(&bm, BSIZE);
73 if (0 > err) {
74 fprintf(error_out, "Error in bitmap create -- aborting \n");
75 exit(-1);
76 }
77
78 fprintf(error_out, "\nTesting bitmap set... \n");
79 test_bitmap_set(&bm);
80
81 fprintf(error_out, "\nTesting bitmap clear ... \n");
82 test_bitmap_clear(&bm);
83
84 fprintf(error_out, "\nTesting bitmap is_set ... \n");
85 test_bitmap_is_set(&bm);
86
87 fprintf(error_out, "\nTesting bitmap clear_all... \n");
88 test_bitmap_clear_all(&bm);
89
90 fprintf(error_out, "\nTesting bitmap set_all... \n");
91 test_bitmap_set_all(&bm);
92
93 fprintf(error_out, "\nTesting bitmap find_and_set... \n");
94 test_bitmap_find_and_set(&bm);
95
96 fprintf(error_out, "\n~~~~~~ Testing complete ~~~~~~ \n\n");
97
98 test_finalize();
99 #ifndef STANDALONE
100 fclose(error_out);
101 #endif
102
103 return 0;
104 }
105
106
107
test_bitmap_set(opal_bitmap_t * bm)108 void test_bitmap_set(opal_bitmap_t *bm) {
109 int result=0;
110
111 /* start of bitmap and boundaries */
112 set_bit(bm, 0);
113 set_bit(bm, 1);
114 set_bit(bm, 7);
115 set_bit(bm, 8);
116 /* middle of bitmap */
117 set_bit(bm, 24);
118
119 /* end of bitmap initial size */
120 set_bit(bm, 31);
121 set_bit(bm, 32);
122
123 /* beyond bitmap -- this is valid */
124 set_bit(bm, 44);
125 set_bit(bm, 82);
126
127 /* invalid bit */
128 PRINT_VALID_ERR;
129 result = set_bit(bm, -1);
130 TEST_AND_REPORT(result, ERR_CODE,"opal_bitmap_set_bit");
131 }
132
133
test_bitmap_clear(opal_bitmap_t * bm)134 void test_bitmap_clear(opal_bitmap_t *bm) {
135 int result=0;
136
137 /* Valid set bits */
138 clear_bit(bm, 29);
139 clear_bit(bm, 31);
140 clear_bit(bm, 33);
141 clear_bit(bm, 32);
142 clear_bit(bm, 0);
143
144 /* invalid bit */
145 PRINT_VALID_ERR;
146 result = clear_bit(bm, -1);
147 TEST_AND_REPORT(result, ERR_CODE,"opal_bitmap_clear_bit");
148 PRINT_VALID_ERR;
149 result = clear_bit(bm, 142);
150 TEST_AND_REPORT(result, ERR_CODE,"opal_bitmap_clear_bit");
151 }
152
153
test_bitmap_is_set(opal_bitmap_t * bm)154 void test_bitmap_is_set(opal_bitmap_t *bm)
155 {
156 int result=0;
157
158 /* First set some bits */
159 test_bitmap_set(bm);
160 is_set_bit(bm, 0);
161 is_set_bit(bm, 1);
162 is_set_bit(bm, 31);
163 is_set_bit(bm, 32);
164
165 result = is_set_bit(bm, 1122);
166 TEST_AND_REPORT(result,0,"opal_bitmap_is_set_bit");
167 is_set_bit(bm, -33);
168 TEST_AND_REPORT(result,0,"opal_bitmap_is_set_bit");
169 is_set_bit(bm, -1);
170 TEST_AND_REPORT(result,0,"opal_bitmap_is_set_bit");
171 }
172
173
test_bitmap_find_and_set(opal_bitmap_t * bm)174 void test_bitmap_find_and_set(opal_bitmap_t *bm)
175 {
176 int bsize;
177 int result=0;
178
179 opal_bitmap_clear_all_bits(bm);
180 result = find_and_set(bm, 0);
181 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
182 result = find_and_set(bm, 1);
183 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
184 result = find_and_set(bm, 2);
185 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
186 result = find_and_set(bm, 3);
187 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
188
189 result = opal_bitmap_set_bit(bm, 5);
190 result = find_and_set(bm, 4);
191 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
192
193 result = opal_bitmap_set_bit(bm, 6);
194 result = opal_bitmap_set_bit(bm, 7);
195
196 /* Setting beyond a char boundary */
197 result = find_and_set(bm, 8);
198 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
199 opal_bitmap_set_bit(bm, 9);
200 result = find_and_set(bm, 10);
201 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
202
203 /* Setting beyond the current size of bitmap */
204 opal_bitmap_set_all_bits(bm);
205 bsize = opal_bitmap_size(bm);
206 result = find_and_set(bm, bsize);
207 TEST_AND_REPORT(result, 0, "opal_bitmap_find_and_set_first_unset_bit");
208 }
209
test_bitmap_clear_all(opal_bitmap_t * bm)210 void test_bitmap_clear_all(opal_bitmap_t *bm)
211 {
212 int result = clear_all(bm);
213 TEST_AND_REPORT(result, 0, " error in opal_bitmap_clear_all_bits");
214 }
215
216
test_bitmap_set_all(opal_bitmap_t * bm)217 void test_bitmap_set_all(opal_bitmap_t *bm)
218 {
219 int result = set_all(bm);
220 TEST_AND_REPORT(result, 0, " error in opal_bitmap_set_ala_bitsl");
221 }
222
set_bit(opal_bitmap_t * bm,int bit)223 int set_bit(opal_bitmap_t *bm, int bit)
224 {
225 int err = opal_bitmap_set_bit(bm, bit);
226 if (err != 0 || !opal_bitmap_is_set_bit(bm, bit)) {
227 fprintf(error_out, "ERROR: set_bit for bit = %d\n\n", bit);
228 return ERR_CODE;
229 }
230 return 0;
231 }
232
233
clear_bit(opal_bitmap_t * bm,int bit)234 int clear_bit(opal_bitmap_t *bm, int bit)
235 {
236 int err = opal_bitmap_clear_bit(bm, bit);
237 if ((err != 0) || opal_bitmap_is_set_bit(bm, bit)) {
238 fprintf(error_out, "ERROR: clear_bit for bit = %d \n\n", bit);
239 return ERR_CODE;
240 }
241
242 return 0;
243 }
244
245
is_set_bit(opal_bitmap_t * bm,int bit)246 int is_set_bit(opal_bitmap_t *bm, int bit)
247 {
248 bool result = opal_bitmap_is_set_bit(bm, bit);
249
250 if (result) {
251 if (bit < 0) {
252 fprintf(error_out, "ERROR: is_set_bit for bit = %d \n\n",bit);
253 return ERR_CODE;
254 }
255 return 0;
256 }
257
258 if (!result) {
259 if (0 <= bit && bit <= bm->array_size && !opal_bitmap_is_set_bit(bm, bit)) {
260 fprintf(error_out, "ERROR: is_set_bit for bit = %d \n\n",bit);
261 return ERR_CODE;
262 }
263 return 0;
264 }
265
266 return 0;
267 }
268
269
find_and_set(opal_bitmap_t * bm,int bit)270 int find_and_set(opal_bitmap_t *bm, int bit)
271 {
272 int ret, pos;
273 /* bit here is the bit that should be found and set, in the top
274 level stub, this function will be called in sequence to test */
275
276 ret = opal_bitmap_find_and_set_first_unset_bit(bm, &pos);
277 if (ret != OPAL_SUCCESS) return ret;
278
279 if (pos != bit) {
280 fprintf(error_out, "ERROR: find_and_set: expected to find_and_set %d\n\n",
281 bit);
282 return ERR_CODE;
283 }
284
285 return 0;
286 }
287
288
clear_all(opal_bitmap_t * bm)289 int clear_all(opal_bitmap_t *bm)
290 {
291 int i;
292 if (OPAL_SUCCESS != opal_bitmap_clear_all_bits(bm)) {
293 return ERR_CODE;
294 }
295 for (i = 0; i < bm->array_size; ++i)
296 if (bm->bitmap[i] != 0) {
297 fprintf(error_out, "ERROR: clear_all for bitmap array entry %d\n\n",
298 i);
299 return ERR_CODE;
300 }
301 return 0;
302 }
303
304
set_all(opal_bitmap_t * bm)305 int set_all(opal_bitmap_t *bm)
306 {
307 int i;
308 if (OPAL_SUCCESS != opal_bitmap_set_all_bits(bm)) {
309 return ERR_CODE;
310 }
311 for (i = 0; i < bm->array_size; ++i)
312 if (bm->bitmap[i] != 0xffffffffffffffffUL) {
313 fprintf(error_out, "ERROR: set_all for bitmap arry entry %d\n\n", i);
314 return ERR_CODE;
315 }
316 return 0;
317 }
318
319
320 #if WANT_PRINT_BITMAP
print_bitmap(opal_bitmap_t * bm)321 void print_bitmap(opal_bitmap_t *bm)
322 {
323 /* Accessing the fields within the structure, since its not an
324 opaque structure */
325
326 int i;
327 for (i = 0; i < bm->array_size; ++i) {
328 fprintf(error_out, "---\n bitmap[%d] = %x \n---\n\n", i,
329 (bm->bitmap[i] & 0xff));
330 }
331 fprintf(error_out, "========================= \n");
332 return;
333 }
334 #endif
335