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