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 <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 24 #include <sys/time.h> 25 #include <sys/resource.h> 26 27 #include "ext2_fs.h" 28 #include "ext2fs.h" 29 30 unsigned char bitarray[] = { 31 0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00 32 }; 33 34 int 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 41 int main(int argc, char **argv) 42 { 43 int i, j, size; 44 unsigned char testarray[12]; 45 unsigned char *bigarray; 46 47 size = sizeof(bitarray)*8; 48 #if 0 49 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 } 54 #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"); 100 101 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); 118 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) 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"); 153 154 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); 164 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) 168 exit(1); 169 170 printf("ext2fs_fast_set_bit big_test successful\n"); 171 172 /* Repeat foregoing tests for 64-bit bitops */ 173 174 /* Test test_bit */ 175 for (i=0,j=0; i < size; i++) { 176 if (ext2fs_test_bit64(i, bitarray)) { 177 if (bits_list[j] == i) { 178 j++; 179 } else { 180 printf("64-bit: Bit %d set, not expected\n", 181 i); 182 exit(1); 183 } 184 } else { 185 if (bits_list[j] == i) { 186 printf("64-bit: " 187 "Expected bit %d to be clear.\n", i); 188 exit(1); 189 } 190 } 191 } 192 printf("64-bit: ext2fs_test_bit appears to be correct\n"); 193 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); 198 } 199 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) { 200 printf("64-bit: ext2fs_set_bit test succeeded.\n"); 201 } else { 202 printf("64-bit: ext2fs_set_bit test failed.\n"); 203 for (i=0; i < sizeof(testarray); i++) { 204 printf("%02x ", testarray[i]); 205 } 206 printf("\n"); 207 exit(1); 208 } 209 for (i=0; bits_list[i] > 0; i++) { 210 ext2fs_clear_bit64(bits_list[i], testarray); 211 } 212 for (i=0; i < sizeof(testarray); i++) { 213 if (testarray[i]) { 214 printf("64-bit: ext2fs_clear_bit failed, " 215 "testarray[%d] is %d\n", i, testarray[i]); 216 exit(1); 217 } 218 } 219 printf("64-bit: ext2fs_clear_bit test succeed.\n"); 220 221 /* Do bigarray test */ 222 bigarray = malloc(1 << 29); 223 if (!bigarray) { 224 fprintf(stderr, "Failed to allocate scratch memory!\n"); 225 exit(1); 226 } 227 228 bigarray[BIG_TEST_BIT >> 3] = 0; 229 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))) 235 exit(1); 236 237 ext2fs_clear_bit64(BIG_TEST_BIT, bigarray); 238 239 printf("64-bit: big bit number (%u) test: %d, expected 0\n", 240 BIG_TEST_BIT, 241 bigarray[BIG_TEST_BIT >> 3]); 242 if (bigarray[BIG_TEST_BIT >> 3] != 0) 243 exit(1); 244 245 printf("64-bit: ext2fs_set_bit big_test successful\n"); 246 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); 251 } 252 if (memcmp(testarray, bitarray, sizeof(testarray)) == 0) { 253 printf("64-bit: ext2fs_fast_set_bit test succeeded.\n"); 254 } else { 255 printf("64-bit: ext2fs_fast_set_bit test failed.\n"); 256 for (i=0; i < sizeof(testarray); i++) { 257 printf("%02x ", testarray[i]); 258 } 259 printf("\n"); 260 exit(1); 261 } 262 for (i=0; bits_list[i] > 0; i++) { 263 ext2fs_clear_bit64(bits_list[i], testarray); 264 } 265 for (i=0; i < sizeof(testarray); i++) { 266 if (testarray[i]) { 267 printf("64-bit: ext2fs_clear_bit failed, " 268 "testarray[%d] is %d\n", i, testarray[i]); 269 exit(1); 270 } 271 } 272 printf("64-bit: ext2fs_clear_bit test succeed.\n"); 273 274 bigarray[BIG_TEST_BIT >> 3] = 0; 275 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))) 281 exit(1); 282 283 ext2fs_fast_clear_bit64(BIG_TEST_BIT, bigarray); 284 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) 288 exit(1); 289 290 printf("64-bit: ext2fs_fast_set_bit big_test successful\n"); 291 292 exit(0); 293 } 294