blob: f57a2e46c33f0c25dafc99813d2a078a2f4cdfb9 [file] [log] [blame]
Theodore Ts'oac493822001-07-04 14:04:58 -04001/*
2 * This testing program makes sure the bitops functions work
3 *
4 * Copyright (C) 2001 by Theodore Ts'o.
Theodore Ts'oefc6f622008-08-27 23:07:54 -04005 *
Theodore Ts'oac493822001-07-04 14:04:58 -04006 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04007 * This file may be redistributed under the terms of the GNU Library
8 * General Public License, version 2.
Theodore Ts'oac493822001-07-04 14:04:58 -04009 * %End-Header%
10 */
11
Theodore Ts'oac493822001-07-04 14:04:58 -040012#include <stdio.h>
13#include <string.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17#include <fcntl.h>
18#include <time.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#if HAVE_ERRNO_H
22#include <errno.h>
23#endif
Theodore Ts'of9bcce32006-03-25 13:42:45 -050024#include <sys/time.h>
25#include <sys/resource.h>
Theodore Ts'oac493822001-07-04 14:04:58 -040026
27#include "ext2_fs.h"
28#include "ext2fs.h"
29
30unsigned char bitarray[] = {
31 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
32 };
33
Theodore Ts'of9bcce32006-03-25 13:42:45 -050034int bits_list[] = {
35 7, 12, 13, 14,15, 22, 30, 68, 77, -1,
36};
37
38#define BIG_TEST_BIT (((unsigned) 1 << 31) + 42)
39
40
Andreas Dilgerde8f3a72007-05-25 11:18:11 -040041int main(int argc, char **argv)
Theodore Ts'oac493822001-07-04 14:04:58 -040042{
Theodore Ts'of9bcce32006-03-25 13:42:45 -050043 int i, j, size;
44 unsigned char testarray[12];
45 unsigned char *bigarray;
Theodore Ts'oac493822001-07-04 14:04:58 -040046
47 size = sizeof(bitarray)*8;
Theodore Ts'of9bcce32006-03-25 13:42:45 -050048#if 0
Theodore Ts'oac493822001-07-04 14:04:58 -040049 i = ext2fs_find_first_bit_set(bitarray, size);
50 while (i < size) {
51 printf("Bit set: %d\n", i);
52 i = ext2fs_find_next_bit_set(bitarray, size, i+1);
53 }
Theodore Ts'of9bcce32006-03-25 13:42:45 -050054#endif
55
56 /* Test test_bit */
57 for (i=0,j=0; i < size; i++) {
58 if (ext2fs_test_bit(i, bitarray)) {
59 if (bits_list[j] == i) {
60 j++;
61 } else {
62 printf("Bit %d set, not expected\n", i);
63 exit(1);
64 }
65 } else {
66 if (bits_list[j] == i) {
67 printf("Expected bit %d to be clear.\n", i);
68 exit(1);
69 }
70 }
71 }
72 printf("ext2fs_test_bit appears to be correct\n");
73
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);
78 }
79 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
80 printf("ext2fs_set_bit test succeeded.\n");
81 } else {
82 printf("ext2fs_set_bit test failed.\n");
83 for (i=0; i < sizeof(testarray); i++) {
84 printf("%02x ", testarray[i]);
85 }
86 printf("\n");
87 exit(1);
88 }
89 for (i=0; bits_list[i] > 0; i++) {
90 ext2fs_clear_bit(bits_list[i], testarray);
91 }
92 for (i=0; i < sizeof(testarray); i++) {
93 if (testarray[i]) {
94 printf("ext2fs_clear_bit failed, "
95 "testarray[%d] is %d\n", i, testarray[i]);
96 exit(1);
97 }
98 }
99 printf("ext2fs_clear_bit test succeed.\n");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400100
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500101
102 /* Do bigarray test */
103 bigarray = malloc(1 << 29);
104 if (!bigarray) {
105 fprintf(stderr, "Failed to allocate scratch memory!\n");
106 exit(1);
107 }
108
109 bigarray[BIG_TEST_BIT >> 3] = 0;
110
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)))
115 exit(1);
116
117 ext2fs_clear_bit(BIG_TEST_BIT, bigarray);
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400118
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500119 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400120 bigarray[BIG_TEST_BIT >> 3]);
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500121 if (bigarray[BIG_TEST_BIT >> 3] != 0)
122 exit(1);
123
124 printf("ext2fs_set_bit big_test successful\n");
125
126
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);
131 }
132 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) {
133 printf("ext2fs_fast_set_bit test succeeded.\n");
134 } else {
135 printf("ext2fs_fast_set_bit test failed.\n");
136 for (i=0; i < sizeof(testarray); i++) {
137 printf("%02x ", testarray[i]);
138 }
139 printf("\n");
140 exit(1);
141 }
142 for (i=0; bits_list[i] > 0; i++) {
143 ext2fs_clear_bit(bits_list[i], testarray);
144 }
145 for (i=0; i < sizeof(testarray); i++) {
146 if (testarray[i]) {
147 printf("ext2fs_clear_bit failed, "
148 "testarray[%d] is %d\n", i, testarray[i]);
149 exit(1);
150 }
151 }
152 printf("ext2fs_clear_bit test succeed.\n");
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400153
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500154
155 bigarray[BIG_TEST_BIT >> 3] = 0;
156
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)))
161 exit(1);
162
163 ext2fs_fast_clear_bit(BIG_TEST_BIT, bigarray);
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400164
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500165 printf("big bit number (%u) test: %d, expected 0\n", BIG_TEST_BIT,
Andreas Dilgerde8f3a72007-05-25 11:18:11 -0400166 bigarray[BIG_TEST_BIT >> 3]);
Theodore Ts'of9bcce32006-03-25 13:42:45 -0500167 if (bigarray[BIG_TEST_BIT >> 3] != 0)
168 exit(1);
169
170 printf("ext2fs_fast_set_bit big_test successful\n");
171
172 exit(0);
Theodore Ts'oac493822001-07-04 14:04:58 -0400173}