a803ce47bf3386308427c8ebf202ed8df8461625
[fio.git] / t / axmap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <fcntl.h>
4 #include <string.h>
5 #include <unistd.h>
6 #include <inttypes.h>
7
8 #include "../lib/lfsr.h"
9 #include "../lib/axmap.h"
10
11 static int test_regular(size_t size, int seed)
12 {
13         struct fio_lfsr lfsr;
14         struct axmap *map;
15         size_t osize;
16         uint64_t ff;
17         int err;
18
19         printf("Using %llu entries...", (unsigned long long) size);
20         fflush(stdout);
21
22         lfsr_init(&lfsr, size, seed, seed & 0xF);
23         map = axmap_new(size);
24         osize = size;
25         err = 0;
26
27         while (size--) {
28                 uint64_t val;
29
30                 if (lfsr_next(&lfsr, &val)) {
31                         printf("lfsr: short loop\n");
32                         err = 1;
33                         break;
34                 }
35                 if (axmap_isset(map, val)) {
36                         printf("bit already set\n");
37                         err = 1;
38                         break;
39                 }
40                 axmap_set(map, val);
41                 if (!axmap_isset(map, val)) {
42                         printf("bit not set\n");
43                         err = 1;
44                         break;
45                 }
46         }
47
48         if (err)
49                 return err;
50
51         ff = axmap_next_free(map, osize);
52         if (ff != (uint64_t) -1ULL) {
53                 printf("axmap_next_free broken: got %llu\n", (unsigned long long) ff);
54                 return 1;
55         }
56
57         printf("pass!\n");
58         axmap_free(map);
59         return 0;
60 }
61
62 static int test_multi(size_t size, unsigned int bit_off)
63 {
64         unsigned int map_size = size;
65         struct axmap *map;
66         uint64_t val = bit_off;
67         int i, err;
68
69         printf("Test multi %llu entries %u offset...", (unsigned long long) size, bit_off);
70         fflush(stdout);
71
72         map = axmap_new(map_size);
73         while (val + 128 <= map_size) {
74                 err = 0;
75                 for (i = val; i < val + 128; i++) {
76                         if (axmap_isset(map, val + i)) {
77                                 printf("bit already set\n");
78                                 err = 1;
79                                 break;
80                         }
81                 }
82
83                 if (err)
84                         break;
85
86                 err = axmap_set_nr(map, val, 128);
87                 if (err != 128) {
88                         printf("only set %u bits\n", err);
89                         break;
90                 }
91
92                 err = 0;
93                 for (i = 0; i < 128; i++) {
94                         if (!axmap_isset(map, val + i)) {
95                                 printf("bit not set: %llu\n", (unsigned long long) val + i);
96                                 err = 1;
97                                 break;
98                         }
99                 }
100
101                 val += 128;
102                 if (err)
103                         break;
104         }
105
106         if (!err)
107                 printf("pass!\n");
108
109         axmap_free(map);
110         return err;
111 }
112
113 int main(int argc, char *argv[])
114 {
115         size_t size = (1UL << 23) - 200;
116         int seed = 1;
117
118         if (argc > 1) {
119                 size = strtoul(argv[1], NULL, 10);
120                 if (argc > 2)
121                         seed = strtoul(argv[2], NULL, 10);
122         }
123
124         if (test_regular(size, seed))
125                 return 1;
126         if (test_multi(size, 0))
127                 return 2;
128         if (test_multi(size, 17))
129                 return 3;
130
131         return 0;
132 }