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.
21 #include <sys/types.h>
26 #include <sys/resource.h>
31 unsigned char bitarray[] = {
32 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
36 7, 12, 13, 14,15, 22, 30, 68, 77, -1,
39 #define BIG_TEST_BIT (((unsigned) 1 << 31) + 42)
42 int main(int argc, char **argv)
45 unsigned char testarray[12];
46 unsigned char *bigarray;
48 size = sizeof(bitarray)*8;
50 i = ext2fs_find_first_bit_set(bitarray, size);
52 printf("Bit set: %d\n", i);
53 i = ext2fs_find_next_bit_set(bitarray, size, i+1);
58 for (i=0,j=0; i < size; i++) {
59 if (ext2fs_test_bit(i, bitarray)) {
60 if (bits_list[j] == i) {
63 printf("Bit %d set, not expected\n", i);
67 if (bits_list[j] == i) {
68 printf("Expected bit %d to be clear.\n", i);
73 printf("ext2fs_test_bit appears to be correct\n");
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);
80 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
81 printf("ext2fs_set_bit test succeeded.\n");
83 printf("ext2fs_set_bit test failed.\n");
84 for (i=0; i < sizeof(testarray); i++) {
85 printf("%02x ", testarray[i]);
90 for (i=0; bits_list[i] > 0; i++) {
91 ext2fs_clear_bit(bits_list[i], testarray);
93 for (i=0; i < sizeof(testarray); i++) {
95 printf("ext2fs_clear_bit failed, "
96 "testarray[%d] is %d\n", i, testarray[i]);
100 printf("ext2fs_clear_bit test succeed.\n");
103 /* Do bigarray test */
104 bigarray = malloc(1 << 29);
106 fprintf(stderr, "Failed to allocate scratch memory!\n");
110 bigarray[BIG_TEST_BIT >> 3] = 0;
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)))
118 ext2fs_clear_bit(BIG_TEST_BIT, bigarray);
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)
125 printf("ext2fs_set_bit big_test successful\n");
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);
133 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
134 printf("ext2fs_fast_set_bit test succeeded.\n");
136 printf("ext2fs_fast_set_bit test failed.\n");
137 for (i=0; i < sizeof(testarray); i++) {
138 printf("%02x ", testarray[i]);
143 for (i=0; bits_list[i] > 0; i++) {
144 ext2fs_clear_bit(bits_list[i], testarray);
146 for (i=0; i < sizeof(testarray); i++) {
148 printf("ext2fs_clear_bit failed, "
149 "testarray[%d] is %d\n", i, testarray[i]);
153 printf("ext2fs_clear_bit test succeed.\n");
156 bigarray[BIG_TEST_BIT >> 3] = 0;
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)))
164 ext2fs_fast_clear_bit(BIG_TEST_BIT, bigarray);
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)
171 printf("ext2fs_fast_set_bit big_test successful\n");
173 /* Repeat foregoing tests for 64-bit bitops */
176 for (i=0,j=0; i < size; i++) {
177 if (ext2fs_test_bit64(i, bitarray)) {
178 if (bits_list[j] == i) {
181 printf("64-bit: Bit %d set, not expected\n",
186 if (bits_list[j] == i) {
188 "Expected bit %d to be clear.\n", i);
193 printf("64-bit: ext2fs_test_bit appears to be correct\n");
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);
200 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
201 printf("64-bit: ext2fs_set_bit test succeeded.\n");
203 printf("64-bit: ext2fs_set_bit test failed.\n");
204 for (i=0; i < sizeof(testarray); i++) {
205 printf("%02x ", testarray[i]);
210 for (i=0; bits_list[i] > 0; i++) {
211 ext2fs_clear_bit64(bits_list[i], testarray);
213 for (i=0; i < sizeof(testarray); i++) {
215 printf("64-bit: ext2fs_clear_bit failed, "
216 "testarray[%d] is %d\n", i, testarray[i]);
220 printf("64-bit: ext2fs_clear_bit test succeed.\n");
222 /* Do bigarray test */
223 bigarray[BIG_TEST_BIT >> 3] = 0;
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)))
232 ext2fs_clear_bit64(BIG_TEST_BIT, bigarray);
234 printf("64-bit: big bit number (%u) test: %d, expected 0\n",
236 bigarray[BIG_TEST_BIT >> 3]);
237 if (bigarray[BIG_TEST_BIT >> 3] != 0)
240 printf("64-bit: ext2fs_set_bit big_test successful\n");
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);
247 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
248 printf("64-bit: ext2fs_fast_set_bit test succeeded.\n");
250 printf("64-bit: ext2fs_fast_set_bit test failed.\n");
251 for (i=0; i < sizeof(testarray); i++) {
252 printf("%02x ", testarray[i]);
257 for (i=0; bits_list[i] > 0; i++) {
258 ext2fs_clear_bit64(bits_list[i], testarray);
260 for (i=0; i < sizeof(testarray); i++) {
262 printf("64-bit: ext2fs_clear_bit failed, "
263 "testarray[%d] is %d\n", i, testarray[i]);
267 printf("64-bit: ext2fs_clear_bit test succeed.\n");
269 bigarray[BIG_TEST_BIT >> 3] = 0;
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)))
278 ext2fs_fast_clear_bit64(BIG_TEST_BIT, bigarray);
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)
285 printf("64-bit: ext2fs_fast_set_bit big_test successful\n");