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