Home | History | Annotate | Download | only in ext2fs
      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 	exit(0);
    173 }
    174