e65aa146a1e6b7ba4d372e1b2ae0fa346eca4608
[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 #define TEST1_LEN   31
47 #define TEST2_LEN   17
48
49 ALLOC_BITARRAY(test1, TEST1_LEN);
50 ALLOC_BITARRAY(test2, TEST2_LEN);
51
52 BitArray bitx1;
53 BitArray bitx2;
54
55 int bitarray_testSetup(void)
56 {
57         kdbg_init();
58         init_bitarray(&bitx1, TEST1_LEN, test1, sizeof(test1));
59         init_bitarray(&bitx2, TEST2_LEN, test2, sizeof(test2));
60         return 0;
61 }
62
63 int bitarray_testRun(void)
64 {
65         memset(test1, 0xaa, sizeof(test1));
66
67         bitarray_dump(&bitx1);
68         for (size_t i = 0; i < TEST1_LEN; i++)
69         {
70                 if (!((bool)(i % 2) == bitarray_check(&bitx1,i)))
71                         goto error;
72         }
73
74         memset(test1, 0, sizeof(test1));
75         for (size_t i = 0; i < TEST1_LEN; i++)
76         {
77                 if ((i % 2) == 0)
78                         bitarray_clear(&bitx1,i);
79                 else
80                         bitarray_set(&bitx1, i);
81         }
82
83         bitarray_dump(&bitx1);
84         for (size_t i = 0; i < TEST1_LEN; i++)
85         {
86                 if (!((bool)(i % 2) == bitarray_check(&bitx1, i)))
87                 goto error;
88         }
89
90         memset(test1, 0, sizeof(test1));
91         bitarray_set(&bitx1, 0);
92         bitarray_dump(&bitx1);
93         if (!bitarray_check(&bitx1, 0))
94                 goto error;
95
96         memset(test1, 0, sizeof(test1));
97         bitarray_set(&bitx1, TEST1_LEN);
98         bitarray_dump(&bitx1);
99         if (!bitarray_check(&bitx1, TEST1_LEN))
100                 goto error;
101
102         kprintf("Test 2\n");
103         memset(test2, 0xFF, sizeof(test2));
104         bitarray_dump(&bitx2);
105         if (!bitarray_full(&bitx2))
106                 goto error;
107
108         memset(test2, 0xFF, sizeof(test2));
109         bitarray_clear(&bitx2, 5);
110         bitarray_dump(&bitx2);
111         if (bitarray_full(&bitx2))
112                 goto error;
113
114         memset(test2, 0xFF, sizeof(test2));
115         bitarray_clear(&bitx2, 13);
116         bitarray_dump(&bitx2);
117         if (bitarray_full(&bitx2))
118                 goto error;
119
120         return 0;
121
122 error:
123         kprintf("Error!\n");
124         return -1;
125 }
126
127 int bitarray_testTearDown(void)
128 {
129         return 0;
130 }
131
132 TEST_MAIN(bitarray);