tst_bitops: don't stop testing on low memory
[e2fsprogs.git] / lib / ext2fs / tst_bitops.c
blobadef12dacd765f41fac698c432305b6e7e6e2889
1 /*
2 * This testing program makes sure the bitops functions work
4 * Copyright (C) 2001 by Theodore Ts'o.
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%
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>
28 #include "ext2_fs.h"
29 #include "ext2fs.h"
31 unsigned char bitarray[] = {
32 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
35 int bits_list[] = {
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)
44 int i, j, size;
45 unsigned char testarray[12];
46 unsigned char *bigarray;
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);
55 #endif
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);
66 } else {
67 if (bits_list[j] == i) {
68 printf("Expected bit %d to be clear.\n", i);
69 exit(1);
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");
82 } else {
83 printf("ext2fs_set_bit test failed.\n");
84 for (i=0; i < sizeof(testarray); i++) {
85 printf("%02x ", testarray[i]);
87 printf("\n");
88 exit(1);
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++) {
94 if (testarray[i]) {
95 printf("ext2fs_clear_bit failed, "
96 "testarray[%d] is %d\n", i, testarray[i]);
97 exit(1);
100 printf("ext2fs_clear_bit test succeed.\n");
103 /* Do bigarray test */
104 bigarray = malloc(1 << 29);
105 if (!bigarray) {
106 fprintf(stderr, "Failed to allocate scratch memory!\n");
107 exit(0);
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)))
116 exit(1);
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)
123 exit(1);
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");
135 } else {
136 printf("ext2fs_fast_set_bit test failed.\n");
137 for (i=0; i < sizeof(testarray); i++) {
138 printf("%02x ", testarray[i]);
140 printf("\n");
141 exit(1);
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++) {
147 if (testarray[i]) {
148 printf("ext2fs_clear_bit failed, "
149 "testarray[%d] is %d\n", i, testarray[i]);
150 exit(1);
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)))
162 exit(1);
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)
169 exit(1);
171 printf("ext2fs_fast_set_bit big_test successful\n");
173 /* Repeat foregoing tests for 64-bit bitops */
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",
183 exit(1);
185 } else {
186 if (bits_list[j] == i) {
187 printf("64-bit: "
188 "Expected bit %d to be clear.\n", i);
189 exit(1);
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");
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]);
207 printf("\n");
208 exit(1);
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++) {
214 if (testarray[i]) {
215 printf("64-bit: ext2fs_clear_bit failed, "
216 "testarray[%d] is %d\n", i, testarray[i]);
217 exit(1);
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)))
230 exit(1);
232 ext2fs_clear_bit64(BIG_TEST_BIT, bigarray);
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);
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");
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]);
254 printf("\n");
255 exit(1);
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++) {
261 if (testarray[i]) {
262 printf("64-bit: ext2fs_clear_bit failed, "
263 "testarray[%d] is %d\n", i, testarray[i]);
264 exit(1);
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)))
276 exit(1);
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)
283 exit(1);
285 printf("64-bit: ext2fs_fast_set_bit big_test successful\n");
286 free(bigarray);
287 exit(0);