replace typeof with __typeof__
[fio.git] / t / axmap.c
... / ...
CommitLineData
1#include <stdio.h>
2#include <stdlib.h>
3#include <inttypes.h>
4
5#include "../lib/lfsr.h"
6#include "../lib/axmap.h"
7
8static int test_regular(size_t size, int seed)
9{
10 struct fio_lfsr lfsr;
11 struct axmap *map;
12 size_t osize;
13 uint64_t ff;
14 int err;
15
16 printf("Using %llu entries...", (unsigned long long) size);
17 fflush(stdout);
18
19 lfsr_init(&lfsr, size, seed, seed & 0xF);
20 map = axmap_new(size);
21 osize = size;
22 err = 0;
23
24 while (size--) {
25 uint64_t val;
26
27 if (lfsr_next(&lfsr, &val)) {
28 printf("lfsr: short loop\n");
29 err = 1;
30 break;
31 }
32 if (axmap_isset(map, val)) {
33 printf("bit already set\n");
34 err = 1;
35 break;
36 }
37 axmap_set(map, val);
38 if (!axmap_isset(map, val)) {
39 printf("bit not set\n");
40 err = 1;
41 break;
42 }
43 }
44
45 if (err)
46 return err;
47
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
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
110int main(int argc, char *argv[])
111{
112 size_t size = (1UL << 23) - 200;
113 int seed = 1;
114
115 if (argc > 1) {
116 size = strtoul(argv[1], NULL, 10);
117 if (argc > 2)
118 seed = strtoul(argv[2], NULL, 10);
119 }
120
121 if (test_regular(size, seed))
122 return 1;
123 if (test_multi(size, 0))
124 return 2;
125 if (test_multi(size, 17))
126 return 3;
127
128 return 0;
129}