Add bitarray test. Fixes and add dump function.
[bertos.git] / bertos / struct / bitarray_test.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2010 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Bitarray test
34  *
35  * \author Daniele Basile <asterix@develer.com>
36  */
37
38 #include <struct/bitarray.h>
39
40 #include <cfg/compiler.h>
41 #include <cfg/test.h>
42 #include <cfg/debug.h>
43
44 #include <string.h>
45
46 ALLOC_BITARRAY(test1, 31);
47 BitArray ctx;
48
49 int bitarray_testSetup(void)
50 {
51         kdbg_init();
52         init_bitarray(&ctx, test1, sizeof(test1));
53         return 0;
54 }
55
56 int bitarray_testRun(void)
57 {
58         memset(test1, 0xaa, sizeof(test1));
59         bitarray_dump(&ctx);
60
61         for (int i = 0; i < bitarray_size(&ctx); i++)
62         {
63                 if (!((bool)(i % 2) == bitarray_check(&ctx, i)))
64                 {
65                         kprintf("Error!\n");
66                         return -1;
67                 }
68         }
69
70         memset(test1, 0, sizeof(test1));
71         for (int i = 0; i < bitarray_size(&ctx); i++)
72         {
73                 if ((i % 2) == 0)
74                         bitarray_clear(&ctx, i);
75                 else
76                         bitarray_set(&ctx, i);
77         }
78
79         bitarray_dump(&ctx);
80         for (int i = 0; i < bitarray_size(&ctx); i++)
81         {
82                 if (!((bool)(i % 2) == bitarray_check(&ctx, i)))
83                 {
84                         kprintf("Error!\n");
85                         return -1;
86                 }
87         }
88
89         memset(test1, 0, sizeof(test1));
90         bitarray_set(&ctx, 0);
91         bitarray_dump(&ctx);
92         if (!bitarray_check(&ctx, 0))
93         {
94                 kprintf("Error!\n");
95                 return -1;
96         }
97
98         memset(test1, 0, sizeof(test1));
99         bitarray_set(&ctx, bitarray_size(&ctx));
100         bitarray_dump(&ctx);
101         if (!bitarray_check(&ctx, bitarray_size(&ctx)))
102         {
103                 kprintf("Error!\n");
104                 return -1;
105         }
106
107         return 0;
108 }
109
110 int bitarray_testTearDown(void)
111 {
112         return 0;
113 }
114
115 TEST_MAIN(bitarray);