blob: 57c585b6798ae9a3d28b17f5a49a25062590ded4 [file] [log] [blame]
Jens Axboead1f90a2012-11-28 21:29:14 +01001#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"
Jens Axboe32bbd3a2013-01-23 11:02:07 -07009#include "../lib/axmap.h"
Jens Axboead1f90a2012-11-28 21:29:14 +010010
11void *smalloc(size_t size)
12{
13 return malloc(size);
14}
15
16void sfree(void *ptr)
17{
18 free(ptr);
19}
20
Jens Axboea7448122013-03-29 09:14:23 -060021static int test_regular(size_t size, int seed)
Jens Axboead1f90a2012-11-28 21:29:14 +010022{
23 struct fio_lfsr lfsr;
Jens Axboead1f90a2012-11-28 21:29:14 +010024 struct axmap *map;
Jens Axboea7448122013-03-29 09:14:23 -060025 size_t osize;
Jens Axboe32bbd3a2013-01-23 11:02:07 -070026 uint64_t ff;
Jens Axboea7448122013-03-29 09:14:23 -060027 int err;
28
29 printf("Using %llu entries...", (unsigned long long) size);
30 fflush(stdout);
31
32 lfsr_init(&lfsr, size, seed, seed & 0xF);
33 map = axmap_new(size);
34 osize = size;
35 err = 0;
36
37 while (size--) {
38 uint64_t val;
39
40 if (lfsr_next(&lfsr, &val, osize)) {
41 printf("lfsr: short loop\n");
42 err = 1;
43 break;
44 }
45 if (axmap_isset(map, val)) {
46 printf("bit already set\n");
47 err = 1;
48 break;
49 }
50 axmap_set(map, val);
51 if (!axmap_isset(map, val)) {
52 printf("bit not set\n");
53 err = 1;
54 break;
55 }
56 }
57
58 if (err)
59 return err;
60
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
67 printf("pass!\n");
68 axmap_free(map);
69 return 0;
70}
71
72static 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
123int main(int argc, char *argv[])
124{
125 size_t size = (1UL << 23) - 200;
Jens Axboe4e590172012-12-21 21:32:50 +0100126 int seed = 1;
Jens Axboead1f90a2012-11-28 21:29:14 +0100127
Jens Axboe4e590172012-12-21 21:32:50 +0100128 if (argc > 1) {
Jens Axboead1f90a2012-11-28 21:29:14 +0100129 size = strtoul(argv[1], NULL, 10);
Jens Axboe4e590172012-12-21 21:32:50 +0100130 if (argc > 2)
131 seed = strtoul(argv[2], NULL, 10);
132 }
Jens Axboead1f90a2012-11-28 21:29:14 +0100133
Jens Axboea7448122013-03-29 09:14:23 -0600134 if (test_regular(size, seed))
Jens Axboe32bbd3a2013-01-23 11:02:07 -0700135 return 1;
Jens Axboea7448122013-03-29 09:14:23 -0600136 if (test_multi(size, 0))
137 return 2;
138 if (test_multi(size, 17))
139 return 3;
Jens Axboe32bbd3a2013-01-23 11:02:07 -0700140
Jens Axboead1f90a2012-11-28 21:29:14 +0100141 return 0;
142}