Deconding query string when we get a key value from it.
[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 #define TEST3_LEN   16
49 #define TEST4_LEN   23
50 #define TEST5_LEN   72
51
52 BITARRAY_ALLOC(test1, TEST1_LEN);
53 BITARRAY_ALLOC(test2, TEST2_LEN);
54 BITARRAY_ALLOC(test3, TEST3_LEN);
55 BITARRAY_ALLOC(test4, TEST4_LEN);
56 BITARRAY_ALLOC(test5, TEST5_LEN);
57
58 BitArray bitx1;
59 BitArray bitx2;
60 BitArray bitx3;
61 BitArray bitx4;
62 BitArray bitx5;
63
64 int bitarray_testSetup(void)
65 {
66         kdbg_init();
67         bitarray_init(&bitx1, TEST1_LEN, test1, sizeof(test1));
68         bitarray_init(&bitx2, TEST2_LEN, test2, sizeof(test2));
69         bitarray_init(&bitx3, TEST3_LEN, test3, sizeof(test3));
70         bitarray_init(&bitx4, TEST4_LEN, test4, sizeof(test4));
71         bitarray_init(&bitx5, TEST5_LEN, test5, sizeof(test5));
72         return 0;
73 }
74
75 int bitarray_testRun(void)
76 {
77         memset(test1, 0xaa, sizeof(test1));
78
79         bitarray_dump(&bitx1);
80         for (size_t i = 0; i < TEST1_LEN; i++)
81         {
82                 if (!((bool)(i % 2) == bitarray_test(&bitx1,i)))
83                         goto error;
84         }
85
86         memset(test1, 0, sizeof(test1));
87         for (size_t i = 0; i < TEST1_LEN; i++)
88         {
89                 if ((i % 2) == 0)
90                         bitarray_clear(&bitx1,i);
91                 else
92                         bitarray_set(&bitx1, i);
93         }
94
95         bitarray_dump(&bitx1);
96         for (size_t i = 0; i < TEST1_LEN; i++)
97         {
98                 if (!((bool)(i % 2) == bitarray_test(&bitx1, i)))
99                 goto error;
100         }
101
102         memset(test1, 0, sizeof(test1));
103         bitarray_set(&bitx1, 0);
104         bitarray_dump(&bitx1);
105         if (!bitarray_test(&bitx1, 0))
106                 goto error;
107
108         memset(test1, 0, sizeof(test1));
109         bitarray_set(&bitx1, TEST1_LEN);
110         bitarray_dump(&bitx1);
111         if (!bitarray_test(&bitx1, TEST1_LEN))
112                 goto error;
113
114         kprintf("Test 2\n");
115         memset(test2, 0xFF, sizeof(test2));
116         bitarray_dump(&bitx2);
117         if (!bitarray_isFull(&bitx2))
118                 goto error;
119
120         memset(test2, 0xFF, sizeof(test2));
121         bitarray_clear(&bitx2, 5);
122         bitarray_dump(&bitx2);
123         if (bitarray_isFull(&bitx2))
124                 goto error;
125
126         memset(test2, 0xFF, sizeof(test2));
127         bitarray_clear(&bitx2, 13);
128         bitarray_dump(&bitx2);
129         if (bitarray_isFull(&bitx2))
130                 goto error;
131
132         kprintf("Test 3\n");
133         bitarray_set(&bitx3, 12);
134         bitarray_dump(&bitx3);
135         int pos = 0;
136         pos = bitarray_firstSetBit(&bitx3);
137         if (pos != 12)
138                 goto error;
139
140         kprintf("Test 4\n");
141         bitarray_set(&bitx4, TEST4_LEN);
142         bitarray_dump(&bitx4);
143         pos = 0;
144         pos = bitarray_firstSetBit(&bitx4);
145         if (pos != 23)
146                 goto error;
147
148         kprintf("Test 5\n");
149         bitarray_set(&bitx5, 71);
150         bitarray_dump(&bitx5);
151         pos = 0;
152         pos = bitarray_firstSetBit(&bitx5);
153         kprintf("pos %d\n", pos);
154         if (pos != 71)
155                 goto error;
156
157         kprintf("Test 6\n");
158         bitarray_clear(&bitx5, 71);
159         bitarray_set(&bitx5, 5);
160         bitarray_dump(&bitx5);
161         pos = 0;
162         pos = bitarray_firstSetBit(&bitx5);
163         if (pos != 5)
164                 goto error;
165
166         return 0;
167
168 error:
169         kprintf("Error!\n");
170         return -1;
171 }
172
173 int bitarray_testTearDown(void)
174 {
175         return 0;
176 }
177
178 TEST_MAIN(bitarray);