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 | ||
12bde369 | 8 | #include "../mutex.h" |
ad1f90aa | 9 | #include "../lib/lfsr.h" |
32bbd3ab | 10 | #include "../lib/axmap.h" |
ad1f90aa JA |
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 | ||
a7448122 | 22 | static int test_regular(size_t size, int seed) |
ad1f90aa JA |
23 | { |
24 | struct fio_lfsr lfsr; | |
ad1f90aa | 25 | struct axmap *map; |
a7448122 | 26 | size_t osize; |
32bbd3ab | 27 | uint64_t ff; |
a7448122 | 28 | int err; |
ad1f90aa | 29 | |
a7448122 JA |
30 | printf("Using %llu entries...", (unsigned long long) size); |
31 | fflush(stdout); | |
ad1f90aa | 32 | |
d474cbc9 | 33 | lfsr_init(&lfsr, size, seed, seed & 0xF); |
ad1f90aa | 34 | map = axmap_new(size); |
32bbd3ab | 35 | osize = size; |
a7448122 | 36 | err = 0; |
ad1f90aa JA |
37 | |
38 | while (size--) { | |
39 | uint64_t val; | |
40 | ||
dd9bd2b2 | 41 | if (lfsr_next(&lfsr, &val)) { |
32bbd3ab | 42 | printf("lfsr: short loop\n"); |
a7448122 | 43 | err = 1; |
32bbd3ab JA |
44 | break; |
45 | } | |
ab3379bc JA |
46 | if (axmap_isset(map, val)) { |
47 | printf("bit already set\n"); | |
a7448122 | 48 | err = 1; |
ab3379bc JA |
49 | break; |
50 | } | |
ad1f90aa | 51 | axmap_set(map, val); |
ab3379bc JA |
52 | if (!axmap_isset(map, val)) { |
53 | printf("bit not set\n"); | |
a7448122 | 54 | err = 1; |
ab3379bc JA |
55 | break; |
56 | } | |
ad1f90aa JA |
57 | } |
58 | ||
a7448122 JA |
59 | if (err) |
60 | return err; | |
61 | ||
32bbd3ab JA |
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 | ||
a7448122 JA |
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 | ||
ad1f90aa JA |
142 | return 0; |
143 | } |