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"
9#include "../lib/axmap.h"
10
11void *smalloc(size_t size)
12{
13	return malloc(size);
14}
15
16void sfree(void *ptr)
17{
18	free(ptr);
19}
20
21static int test_regular(size_t size, int seed)
22{
23	struct fio_lfsr lfsr;
24	struct axmap *map;
25	size_t osize;
26	uint64_t ff;
27	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;
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
141	return 0;
142}
143