tst_bitops.c revision d1154eb460efe588eaed3d439c1caaca149fa362
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
31unsigned char bitarray[] = {
32	0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
33	};
34
35int 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
42int 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 = malloc(1 << 29);
224	if (!bigarray) {
225		fprintf(stderr, "Failed to allocate scratch memory!\n");
226		exit(1);
227	}
228
229        bigarray[BIG_TEST_BIT >> 3] = 0;
230
231	ext2fs_set_bit64(BIG_TEST_BIT, bigarray);
232	printf("64-bit: big bit number (%u) test: %d, expected %d\n",
233	       BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
234	       (1 << (BIG_TEST_BIT & 7)));
235	if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
236		exit(1);
237
238	ext2fs_clear_bit64(BIG_TEST_BIT, bigarray);
239
240	printf("64-bit: big bit number (%u) test: %d, expected 0\n",
241	       BIG_TEST_BIT,
242	       bigarray[BIG_TEST_BIT >> 3]);
243	if (bigarray[BIG_TEST_BIT >> 3] != 0)
244		exit(1);
245
246	printf("64-bit: ext2fs_set_bit big_test successful\n");
247
248	/* Now test ext2fs_fast_set_bit */
249	memset(testarray, 0, sizeof(testarray));
250	for (i=0; bits_list[i] > 0; i++) {
251		ext2fs_fast_set_bit64(bits_list[i], testarray);
252	}
253	if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
254		printf("64-bit: ext2fs_fast_set_bit test succeeded.\n");
255	} else {
256		printf("64-bit: ext2fs_fast_set_bit test failed.\n");
257		for (i=0; i < sizeof(testarray); i++) {
258			printf("%02x ", testarray[i]);
259		}
260		printf("\n");
261		exit(1);
262	}
263	for (i=0; bits_list[i] > 0; i++) {
264		ext2fs_clear_bit64(bits_list[i], testarray);
265	}
266	for (i=0; i < sizeof(testarray); i++) {
267		if (testarray[i]) {
268			printf("64-bit: ext2fs_clear_bit failed, "
269			       "testarray[%d] is %d\n", i, testarray[i]);
270			exit(1);
271		}
272	}
273	printf("64-bit: ext2fs_clear_bit test succeed.\n");
274
275        bigarray[BIG_TEST_BIT >> 3] = 0;
276
277	ext2fs_fast_set_bit64(BIG_TEST_BIT, bigarray);
278	printf("64-bit: big bit number (%u) test: %d, expected %d\n",
279	       BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3],
280	       (1 << (BIG_TEST_BIT & 7)));
281	if (bigarray[BIG_TEST_BIT >> 3] != (1 << (BIG_TEST_BIT & 7)))
282		exit(1);
283
284	ext2fs_fast_clear_bit64(BIG_TEST_BIT, bigarray);
285
286	printf("64-bit: big bit number (%u) test: %d, expected 0\n",
287	       BIG_TEST_BIT, bigarray[BIG_TEST_BIT >> 3]);
288	if (bigarray[BIG_TEST_BIT >> 3] != 0)
289		exit(1);
290
291	printf("64-bit: ext2fs_fast_set_bit big_test successful\n");
292
293	exit(0);
294}
295