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