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