2 * This testing program makes sure the bitops functions work
4 * Copyright (C) 2001 by Theodore Ts'o.
7 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
20 #include <sys/types.h>
25 #include <sys/resource.h>
30 unsigned char bitarray[] = {
31 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
35 7, 12, 13, 14,15, 22, 30, 68, 77, -1,
38 #define BIG_TEST_BIT (((unsigned) 1 << 31) + 42)
41 int main(int argc, char **argv)
44 unsigned char testarray[12];
45 unsigned char *bigarray;
47 size = sizeof(bitarray)*8;
49 i = ext2fs_find_first_bit_set(bitarray, size);
51 printf("Bit set: %d\n", i);
52 i = ext2fs_find_next_bit_set(bitarray, size, i+1);
57 for (i=0,j=0; i < size; i++) {
58 if (ext2fs_test_bit(i, bitarray)) {
59 if (bits_list[j] == i) {
62 printf("Bit %d set, not expected\n", i);
66 if (bits_list[j] == i) {
67 printf("Expected bit %d to be clear.\n", i);
72 printf("ext2fs_test_bit appears to be correct\n");
74 /* Test ext2fs_set_bit */
75 memset(testarray, 0, sizeof(testarray));
76 for (i=0; bits_list[i] > 0; i++) {
77 ext2fs_set_bit(bits_list[i], testarray);
79 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
80 printf("ext2fs_set_bit test succeeded.\n");
82 printf("ext2fs_set_bit test failed.\n");
83 for (i=0; i < sizeof(testarray); i++) {
84 printf("%02x ", testarray[i]);
89 for (i=0; bits_list[i] > 0; i++) {
90 ext2fs_clear_bit(bits_list[i], testarray);
92 for (i=0; i < sizeof(testarray); i++) {
94 printf("ext2fs_clear_bit failed, "
95 "testarray[%d] is %d\n", i, testarray[i]);
99 printf("ext2fs_clear_bit test succeed.\n");
102 /* Do bigarray test */
103 bigarray = malloc(1 << 29);
105 fprintf(stderr, "Failed to allocate scratch memory!\n");
109 bigarray[BIG_TEST_BIT >> 3] = 0;
111 ext2fs_set_bit(BIG_TEST_BIT, bigarray);
112 printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT,
113 bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7)));
114 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
117 ext2fs_clear_bit(BIG_TEST_BIT, bigarray);
119 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
120 bigarray[BIG_TEST_BIT >> 3]);
121 if (bigarray[BIG_TEST_BIT >> 3] != 0)
124 printf("ext2fs_set_bit big_test successful\n");
127 /* Now test ext2fs_fast_set_bit */
128 memset(testarray, 0, sizeof(testarray));
129 for (i=0; bits_list[i] > 0; i++) {
130 ext2fs_fast_set_bit(bits_list[i], testarray);
132 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
133 printf("ext2fs_fast_set_bit test succeeded.\n");
135 printf("ext2fs_fast_set_bit test failed.\n");
136 for (i=0; i < sizeof(testarray); i++) {
137 printf("%02x ", testarray[i]);
142 for (i=0; bits_list[i] > 0; i++) {
143 ext2fs_clear_bit(bits_list[i], testarray);
145 for (i=0; i < sizeof(testarray); i++) {
147 printf("ext2fs_clear_bit failed, "
148 "testarray[%d] is %d\n", i, testarray[i]);
152 printf("ext2fs_clear_bit test succeed.\n");
155 bigarray[BIG_TEST_BIT >> 3] = 0;
157 ext2fs_fast_set_bit(BIG_TEST_BIT, bigarray);
158 printf("big bit number (%u) test: %d, expected %d\n", BIG_TEST_BIT,
159 bigarray[BIG_TEST_BIT >> 3], (1 << (BIG_TEST_BIT & 7)));
160 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
163 ext2fs_fast_clear_bit(BIG_TEST_BIT, bigarray);
165 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
166 bigarray[BIG_TEST_BIT >> 3]);
167 if (bigarray[BIG_TEST_BIT >> 3] != 0)
170 printf("ext2fs_fast_set_bit big_test successful\n");
172 /* Repeat foregoing tests for 64-bit bitops */
175 for (i=0,j=0; i < size; i++) {
176 if (ext2fs_test_bit64(i, bitarray)) {
177 if (bits_list[j] == i) {
180 printf("64-bit: Bit %d set, not expected\n",
185 if (bits_list[j] == i) {
187 "Expected bit %d to be clear.\n", i);
192 printf("64-bit: ext2fs_test_bit appears to be correct\n");
194 /* Test ext2fs_set_bit */
195 memset(testarray, 0, sizeof(testarray));
196 for (i=0; bits_list[i] > 0; i++) {
197 ext2fs_set_bit64(bits_list[i], testarray);
199 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
200 printf("64-bit: ext2fs_set_bit test succeeded.\n");
202 printf("64-bit: ext2fs_set_bit test failed.\n");
203 for (i=0; i < sizeof(testarray); i++) {
204 printf("%02x ", testarray[i]);
209 for (i=0; bits_list[i] > 0; i++) {
210 ext2fs_clear_bit64(bits_list[i], testarray);
212 for (i=0; i < sizeof(testarray); i++) {
214 printf("64-bit: ext2fs_clear_bit failed, "
215 "testarray[%d] is %d\n", i, testarray[i]);
219 printf("64-bit: ext2fs_clear_bit test succeed.\n");
221 /* Do bigarray test */
222 bigarray = malloc(1 << 29);
224 fprintf(stderr, "Failed to allocate scratch memory!\n");
228 bigarray[BIG_TEST_BIT >> 3] = 0;
230 ext2fs_set_bit64(BIG_TEST_BIT, bigarray);
231 printf("64-bit: big bit number (%u) test: %d, expected %d\n",
232 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
233 (1 << (BIG_TEST_BIT & 7)));
234 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
237 ext2fs_clear_bit64(BIG_TEST_BIT, bigarray);
239 printf("64-bit: big bit number (%u) test: %d, expected 0\n",
241 bigarray[BIG_TEST_BIT >> 3]);
242 if (bigarray[BIG_TEST_BIT >> 3] != 0)
245 printf("64-bit: ext2fs_set_bit big_test successful\n");
247 /* Now test ext2fs_fast_set_bit */
248 memset(testarray, 0, sizeof(testarray));
249 for (i=0; bits_list[i] > 0; i++) {
250 ext2fs_fast_set_bit64(bits_list[i], testarray);
252 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
253 printf("64-bit: ext2fs_fast_set_bit test succeeded.\n");
255 printf("64-bit: ext2fs_fast_set_bit test failed.\n");
256 for (i=0; i < sizeof(testarray); i++) {
257 printf("%02x ", testarray[i]);
262 for (i=0; bits_list[i] > 0; i++) {
263 ext2fs_clear_bit64(bits_list[i], testarray);
265 for (i=0; i < sizeof(testarray); i++) {
267 printf("64-bit: ext2fs_clear_bit failed, "
268 "testarray[%d] is %d\n", i, testarray[i]);
272 printf("64-bit: ext2fs_clear_bit test succeed.\n");
274 bigarray[BIG_TEST_BIT >> 3] = 0;
276 ext2fs_fast_set_bit64(BIG_TEST_BIT, bigarray);
277 printf("64-bit: big bit number (%u) test: %d, expected %d\n",
278 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
279 (1 << (BIG_TEST_BIT & 7)));
280 if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
283 ext2fs_fast_clear_bit64(BIG_TEST_BIT, bigarray);
285 printf("64-bit: big bit number (%u) test: %d, expected 0\n",
286 BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3]);
287 if (bigarray[BIG_TEST_BIT >> 3] != 0)
290 printf("64-bit: ext2fs_fast_set_bit big_test successful\n");