Commit | Line | Data |
---|---|---|
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 | 8 | static 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 | ||
59 | static 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 | ||
110 | int 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 | ||
ad1f90aa JA |
128 | return 0; |
129 | } |