~ubuntu-branches/ubuntu/edgy/e2fsprogs/edgy-updates

« back to all changes in this revision

Viewing changes to lib/ext2fs/tst_bitops.c

  • Committer: Bazaar Package Importer
  • Author(s): Theodore Y. Ts'o
  • Date: 2006-05-29 11:07:53 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20060529110753-uvkuaxqr96y31kqy
Tags: 1.39-1
* New upstream version
* Fix debugfs's dump_unused command so it will not core dump on
  filesystems with a 64k blocksize
* Clarified and improved man pages, including spelling errors
  (Closes: #368392, #368393, #368394, #368179)
* New filesystems are now created with directory indexing and
  on-line resizing enabled by default
* Fix previously mangled wording in an older Debian changelog entry
* Fix doc-base pointer to the top-level html file (Closes: #362544, #362970)

Show diffs side-by-side

added added

removed removed

Lines of Context:
9
9
 * %End-Header%
10
10
 */
11
11
 
12
 
/* #define _EXT2_USE_C_VERSIONS_ */
13
 
 
14
12
#include <stdio.h>
15
13
#include <string.h>
16
14
#if HAVE_UNISTD_H
23
21
#if HAVE_ERRNO_H
24
22
#include <errno.h>
25
23
#endif
 
24
#include <sys/time.h>
 
25
#include <sys/resource.h>
26
26
 
27
27
#include "ext2_fs.h"
28
28
#include "ext2fs.h"
31
31
        0x80, 0xF0, 0x40, 0x40, 0x0, 0x0, 0x0, 0x0, 0x10, 0x20, 0x00, 0x00
32
32
        };
33
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
 
34
41
main(int argc, char **argv)
35
42
{
36
 
        int     i, size;
 
43
        int     i, j, size;
 
44
        unsigned char testarray[12];
 
45
        unsigned char *bigarray;
37
46
 
38
47
        size = sizeof(bitarray)*8;
 
48
#if 0
39
49
        i = ext2fs_find_first_bit_set(bitarray, size);
40
50
        while (i < size) {
41
51
                printf("Bit set: %d\n", i);
42
52
                i = ext2fs_find_next_bit_set(bitarray, size, i+1);
43
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], 0);
 
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], 0);
 
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);
44
173
}