1 /*
2 * This testing program makes sure the bitops functions work
3 *
4 * Copyright (C) 2001 by Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
9 * %End-Header%
10 */
11
12 #include "config.h"
13 #include <stdio.h>
14 #include <string.h>
15 #if HAVE_UNISTD_H
16 #include <unistd.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #include <sys/stat.h>
21 #include <sys/types.h>
22 #if HAVE_ERRNO_H
23 #include <errno.h>
24 #endif
25 #include <sys/time.h>
26 #include <sys/resource.h>
27
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
30
31 unsigned char bitarray[] = {
32 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
33 };
34
35 int bits_list[] = {
36 7, 12, 13, 14,15, 22, 30, 68, 77, -1,
37 };
38
39 #define BIG_TEST_BIT (((unsigned) 1 << 31) + 42)
40
41
main(int argc,char ** argv)42 int main(int argc, char **argv)
43 {
44 int i, j, size;
45 unsigned char testarray[12];
46 unsigned char *bigarray;
47
48 size = sizeof(bitarray)*8;
49 #if 0
50 i = ext2fs_find_first_bit_set(bitarray, size);
51 while (i < size) {
52 printf("Bit set: %d\n", i);
53 i = ext2fs_find_next_bit_set(bitarray, size, i+1);
54 }
55 #endif
56
57 /* Test test_bit */
58 for (i=0,j=0; i < size; i++) {
59 if (ext2fs_test_bit(i, bitarray)) {
60 if (bits_list[j] == i) {
61 j++;
62 } else {
63 printf("Bit %d set, not expected\n", i);
64 exit(1);
65 }
66 } else {
67 if (bits_list[j] == i) {
68 printf("Expected bit %d to be clear.\n", i);
69 exit(1);
70 }
71 }
72 }
73 printf("ext2fs_test_bit appears to be correct\n");
74
75 /* Test ext2fs_set_bit */
76 memset(testarray, 0, sizeof(testarray));
77 for (i=0; bits_list[i] > 0; i++) {
78 ext2fs_set_bit(bits_list[i], testarray);
79 }
80 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
81 printf("ext2fs_set_bit test succeeded.\n");
82 } else {
83 printf("ext2fs_set_bit test failed.\n");
84 for (i=0; i < sizeof(testarray); i++) {
85 printf("%02x ", testarray[i]);
86 }
87 printf("\n");
88 exit(1);
89 }
90 for (i=0; bits_list[i] > 0; i++) {
91 ext2fs_clear_bit(bits_list[i], testarray);
92 }
93 for (i=0; i < sizeof(testarray); i++) {
94 if (testarray[i]) {
95 printf("ext2fs_clear_bit failed, "
96 "testarray[%d] is %d\n", i, testarray[i]);
97 exit(1);
98 }
99 }
100 printf("ext2fs_clear_bit test succeed.\n");
101
102
103 /* Do bigarray test */
104 bigarray = malloc(1 << 29);
105 if (!bigarray) {
106 fprintf(stderr, "Failed to allocate scratch memory!\n");
107 exit(1);
108 }
109
110 bigarray[BIG_TEST_BIT >> 3] = 0;
111
112 ext2fs_set_bit(BIG_TEST_BIT, bigarray);
113 printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT,
114 bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7)));
115 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
116 exit(1);
117
118 ext2fs_clear_bit(BIG_TEST_BIT, bigarray);
119
120 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
121 bigarray[BIG_TEST_BIT >> 3]);
122 if (bigarray[BIG_TEST_BIT >> 3] != 0)
123 exit(1);
124
125 printf("ext2fs_set_bit big_test successful\n");
126
127
128 /* Now test ext2fs_fast_set_bit */
129 memset(testarray, 0, sizeof(testarray));
130 for (i=0; bits_list[i] > 0; i++) {
131 ext2fs_fast_set_bit(bits_list[i], testarray);
132 }
133 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
134 printf("ext2fs_fast_set_bit test succeeded.\n");
135 } else {
136 printf("ext2fs_fast_set_bit test failed.\n");
137 for (i=0; i < sizeof(testarray); i++) {
138 printf("%02x ", testarray[i]);
139 }
140 printf("\n");
141 exit(1);
142 }
143 for (i=0; bits_list[i] > 0; i++) {
144 ext2fs_clear_bit(bits_list[i], testarray);
145 }
146 for (i=0; i < sizeof(testarray); i++) {
147 if (testarray[i]) {
148 printf("ext2fs_clear_bit failed, "
149 "testarray[%d] is %d\n", i, testarray[i]);
150 exit(1);
151 }
152 }
153 printf("ext2fs_clear_bit test succeed.\n");
154
155
156 bigarray[BIG_TEST_BIT >> 3] = 0;
157
158 ext2fs_fast_set_bit(BIG_TEST_BIT, bigarray);
159 printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT,
160 bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7)));
161 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
162 exit(1);
163
164 ext2fs_fast_clear_bit(BIG_TEST_BIT, bigarray);
165
166 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
167 bigarray[BIG_TEST_BIT >> 3]);
168 if (bigarray[BIG_TEST_BIT >> 3] != 0)
169 exit(1);
170
171 printf("ext2fs_fast_set_bit big_test successful\n");
172
173 /* Repeat foregoing tests for 64-bit bitops */
174
175 /* Test test_bit */
176 for (i=0,j=0; i < size; i++) {
177 if (ext2fs_test_bit64(i, bitarray)) {
178 if (bits_list[j] == i) {
179 j++;
180 } else {
181 printf("64-bit: Bit %d set, not expected\n",
182 i);
183 exit(1);
184 }
185 } else {
186 if (bits_list[j] == i) {
187 printf("64-bit: "
188 "Expected bit %d to be clear.\n", i);
189 exit(1);
190 }
191 }
192 }
193 printf("64-bit: ext2fs_test_bit appears to be correct\n");
194
195 /* Test ext2fs_set_bit */
196 memset(testarray, 0, sizeof(testarray));
197 for (i=0; bits_list[i] > 0; i++) {
198 ext2fs_set_bit64(bits_list[i], testarray);
199 }
200 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
201 printf("64-bit: ext2fs_set_bit test succeeded.\n");
202 } else {
203 printf("64-bit: ext2fs_set_bit test failed.\n");
204 for (i=0; i < sizeof(testarray); i++) {
205 printf("%02x ", testarray[i]);
206 }
207 printf("\n");
208 exit(1);
209 }
210 for (i=0; bits_list[i] > 0; i++) {
211 ext2fs_clear_bit64(bits_list[i], testarray);
212 }
213 for (i=0; i < sizeof(testarray); i++) {
214 if (testarray[i]) {
215 printf("64-bit: ext2fs_clear_bit failed, "
216 "testarray[%d] is %d\n", i, testarray[i]);
217 exit(1);
218 }
219 }
220 printf("64-bit: ext2fs_clear_bit test succeed.\n");
221
222 /* Do bigarray test */
223 bigarray[BIG_TEST_BIT >> 3] = 0;
224
225 ext2fs_set_bit64(BIG_TEST_BIT, bigarray);
226 printf("64-bit: big bit number (%u) test: %d, expected %d\n",
227 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
228 (1 << (BIG_TEST_BIT & 7)));
229 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
230 exit(1);
231
232 ext2fs_clear_bit64(BIG_TEST_BIT, bigarray);
233
234 printf("64-bit: big bit number (%u) test: %d, expected 0\n",
235 BIG_TEST_BIT,
236 bigarray[BIG_TEST_BIT >> 3]);
237 if (bigarray[BIG_TEST_BIT >> 3] != 0)
238 exit(1);
239
240 printf("64-bit: ext2fs_set_bit big_test successful\n");
241
242 /* Now test ext2fs_fast_set_bit */
243 memset(testarray, 0, sizeof(testarray));
244 for (i=0; bits_list[i] > 0; i++) {
245 ext2fs_fast_set_bit64(bits_list[i], testarray);
246 }
247 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
248 printf("64-bit: ext2fs_fast_set_bit test succeeded.\n");
249 } else {
250 printf("64-bit: ext2fs_fast_set_bit test failed.\n");
251 for (i=0; i < sizeof(testarray); i++) {
252 printf("%02x ", testarray[i]);
253 }
254 printf("\n");
255 exit(1);
256 }
257 for (i=0; bits_list[i] > 0; i++) {
258 ext2fs_clear_bit64(bits_list[i], testarray);
259 }
260 for (i=0; i < sizeof(testarray); i++) {
261 if (testarray[i]) {
262 printf("64-bit: ext2fs_clear_bit failed, "
263 "testarray[%d] is %d\n", i, testarray[i]);
264 exit(1);
265 }
266 }
267 printf("64-bit: ext2fs_clear_bit test succeed.\n");
268
269 bigarray[BIG_TEST_BIT >> 3] = 0;
270
271 ext2fs_fast_set_bit64(BIG_TEST_BIT, bigarray);
272 printf("64-bit: big bit number (%u) test: %d, expected %d\n",
273 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
274 (1 << (BIG_TEST_BIT & 7)));
275 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
276 exit(1);
277
278 ext2fs_fast_clear_bit64(BIG_TEST_BIT, bigarray);
279
280 printf("64-bit: big bit number (%u) test: %d, expected 0\n",
281 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3]);
282 if (bigarray[BIG_TEST_BIT >> 3] != 0)
283 exit(1);
284
285 printf("64-bit: ext2fs_fast_set_bit big_test successful\n");
286 free(bigarray);
287 exit(0);
288 }
289