axmap: ensure that overlaps are handled strictly sequential
[fio.git] / t / axmap.c
CommitLineData
ad1f90aa
JA
1#include <stdio.h>
2#include <stdlib.h>
ad1f90aa
JA
3#include <inttypes.h>
4
5#include "../lib/lfsr.h"
32bbd3ab 6#include "../lib/axmap.h"
ad1f90aa 7
a7448122 8static int test_regular(size_t size, int seed)
ad1f90aa
JA
9{
10 struct fio_lfsr lfsr;
ad1f90aa 11 struct axmap *map;
a7448122 12 size_t osize;
32bbd3ab 13 uint64_t ff;
a7448122 14 int err;
ad1f90aa 15
a7448122
JA
16 printf("Using %llu entries...", (unsigned long long) size);
17 fflush(stdout);
ad1f90aa 18
d474cbc9 19 lfsr_init(&lfsr, size, seed, seed & 0xF);
ad1f90aa 20 map = axmap_new(size);
32bbd3ab 21 osize = size;
a7448122 22 err = 0;
ad1f90aa
JA
23
24 while (size--) {
25 uint64_t val;
26
dd9bd2b2 27 if (lfsr_next(&lfsr, &val)) {
32bbd3ab 28 printf("lfsr: short loop\n");
a7448122 29 err = 1;
32bbd3ab
JA
30 break;
31 }
ab3379bc
JA
32 if (axmap_isset(map, val)) {
33 printf("bit already set\n");
a7448122 34 err = 1;
ab3379bc
JA
35 break;
36 }
ad1f90aa 37 axmap_set(map, val);
ab3379bc
JA
38 if (!axmap_isset(map, val)) {
39 printf("bit not set\n");
a7448122 40 err = 1;
ab3379bc
JA
41 break;
42 }
ad1f90aa
JA
43 }
44
a7448122
JA
45 if (err)
46 return err;
47
32bbd3ab
JA
48 ff = axmap_next_free(map, osize);
49 if (ff != (uint64_t) -1ULL) {
50 printf("axmap_next_free broken: got %llu\n", (unsigned long long) ff);
51 return 1;
52 }
53
a7448122
JA
54 printf("pass!\n");
55 axmap_free(map);
56 return 0;
57}
58
59static int test_multi(size_t size, unsigned int bit_off)
60{
61 unsigned int map_size = size;
62 struct axmap *map;
63 uint64_t val = bit_off;
64 int i, err;
65
66 printf("Test multi %llu entries %u offset...", (unsigned long long) size, bit_off);
67 fflush(stdout);
68
69 map = axmap_new(map_size);
70 while (val + 128 <= map_size) {
71 err = 0;
72 for (i = val; i < val + 128; i++) {
73 if (axmap_isset(map, val + i)) {
74 printf("bit already set\n");
75 err = 1;
76 break;
77 }
78 }
79
80 if (err)
81 break;
82
83 err = axmap_set_nr(map, val, 128);
84 if (err != 128) {
85 printf("only set %u bits\n", err);
86 break;
87 }
88
89 err = 0;
90 for (i = 0; i < 128; i++) {
91 if (!axmap_isset(map, val + i)) {
92 printf("bit not set: %llu\n", (unsigned long long) val + i);
93 err = 1;
94 break;
95 }
96 }
97
98 val += 128;
99 if (err)
100 break;
101 }
102
103 if (!err)
104 printf("pass!\n");
105
106 axmap_free(map);
107 return err;
108}
109
9a5950bf
JA
110static int test_overlap(void)
111{
112 struct axmap *map;
113 int ret;
114
115 printf("Test overlaps...");
116 fflush(stdout);
117
118 map = axmap_new(200);
119
120 ret = axmap_set_nr(map, 102, 1);
121 if (ret != 1) {
122 printf("fail 102 1: %d\n", ret);
123 return 1;
124 }
125
126 ret = axmap_set_nr(map, 101, 3);
127 if (ret != 1) {
128 printf("fail 102 1: %d\n", ret);
129 return 1;
130 }
131
132 ret = axmap_set_nr(map, 106, 4);
133 if (ret != 4) {
134 printf("fail 106 4: %d\n", ret);
135 return 1;
136 }
137
138 ret = axmap_set_nr(map, 105, 3);
139 if (ret != 1) {
140 printf("fail 105 3: %d\n", ret);
141 return 1;
142 }
143
144 ret = axmap_set_nr(map, 120, 4);
145 if (ret != 4) {
146 printf("fail 120 4: %d\n", ret);
147 return 1;
148 }
149
150 ret = axmap_set_nr(map, 118, 2);
151 if (ret != 2) {
152 printf("fail 118 2: %d\n", ret);
153 return 1;
154 }
155
156 ret = axmap_set_nr(map, 118, 2);
157 if (ret != 0) {
158 printf("fail 118 2: %d\n", ret);
159 return 1;
160 }
161
162 printf("pass!\n");
163 axmap_free(map);
164 return 0;
165}
166
a7448122
JA
167int main(int argc, char *argv[])
168{
169 size_t size = (1UL << 23) - 200;
170 int seed = 1;
171
172 if (argc > 1) {
173 size = strtoul(argv[1], NULL, 10);
174 if (argc > 2)
175 seed = strtoul(argv[2], NULL, 10);
176 }
177
178 if (test_regular(size, seed))
179 return 1;
180 if (test_multi(size, 0))
181 return 2;
182 if (test_multi(size, 17))
183 return 3;
9a5950bf
JA
184 if (test_overlap())
185 return 4;
a7448122 186
ad1f90aa
JA
187 return 0;
188}