]> git.saurik.com Git - redis.git/blame - deps/jemalloc/test/bitmap.c
redis-check-dump now understands dumps produced by Redis 2.6
[redis.git] / deps / jemalloc / test / bitmap.c
CommitLineData
a78e148b 1#define JEMALLOC_MANGLE
2#include "jemalloc_test.h"
3
a78e148b 4#if (LG_BITMAP_MAXBITS > 12)
5# define MAXBITS 4500
6#else
7# define MAXBITS (1U << LG_BITMAP_MAXBITS)
8#endif
9
10static void
11test_bitmap_size(void)
12{
13 size_t i, prev_size;
14
15 prev_size = 0;
16 for (i = 1; i <= MAXBITS; i++) {
17 size_t size = bitmap_size(i);
18 assert(size >= prev_size);
19 prev_size = size;
20 }
21}
22
23static void
24test_bitmap_init(void)
25{
26 size_t i;
27
28 for (i = 1; i <= MAXBITS; i++) {
29 bitmap_info_t binfo;
30 bitmap_info_init(&binfo, i);
31 {
32 size_t j;
ad4c0b41 33 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
34 bitmap_info_ngroups(&binfo));
a78e148b 35 bitmap_init(bitmap, &binfo);
36
37 for (j = 0; j < i; j++)
38 assert(bitmap_get(bitmap, &binfo, j) == false);
ad4c0b41 39 free(bitmap);
a78e148b 40
41 }
42 }
43}
44
45static void
46test_bitmap_set(void)
47{
48 size_t i;
49
50 for (i = 1; i <= MAXBITS; i++) {
51 bitmap_info_t binfo;
52 bitmap_info_init(&binfo, i);
53 {
54 size_t j;
ad4c0b41 55 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
56 bitmap_info_ngroups(&binfo));
a78e148b 57 bitmap_init(bitmap, &binfo);
58
59 for (j = 0; j < i; j++)
60 bitmap_set(bitmap, &binfo, j);
61 assert(bitmap_full(bitmap, &binfo));
ad4c0b41 62 free(bitmap);
a78e148b 63 }
64 }
65}
66
67static void
68test_bitmap_unset(void)
69{
70 size_t i;
71
72 for (i = 1; i <= MAXBITS; i++) {
73 bitmap_info_t binfo;
74 bitmap_info_init(&binfo, i);
75 {
76 size_t j;
ad4c0b41 77 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
78 bitmap_info_ngroups(&binfo));
a78e148b 79 bitmap_init(bitmap, &binfo);
80
81 for (j = 0; j < i; j++)
82 bitmap_set(bitmap, &binfo, j);
83 assert(bitmap_full(bitmap, &binfo));
84 for (j = 0; j < i; j++)
85 bitmap_unset(bitmap, &binfo, j);
86 for (j = 0; j < i; j++)
87 bitmap_set(bitmap, &binfo, j);
88 assert(bitmap_full(bitmap, &binfo));
ad4c0b41 89 free(bitmap);
a78e148b 90 }
91 }
92}
93
94static void
95test_bitmap_sfu(void)
96{
97 size_t i;
98
99 for (i = 1; i <= MAXBITS; i++) {
100 bitmap_info_t binfo;
101 bitmap_info_init(&binfo, i);
102 {
103 ssize_t j;
ad4c0b41 104 bitmap_t *bitmap = malloc(sizeof(bitmap_t) *
105 bitmap_info_ngroups(&binfo));
a78e148b 106 bitmap_init(bitmap, &binfo);
107
108 /* Iteratively set bits starting at the beginning. */
109 for (j = 0; j < i; j++)
110 assert(bitmap_sfu(bitmap, &binfo) == j);
111 assert(bitmap_full(bitmap, &binfo));
112
113 /*
114 * Iteratively unset bits starting at the end, and
115 * verify that bitmap_sfu() reaches the unset bits.
116 */
117 for (j = i - 1; j >= 0; j--) {
118 bitmap_unset(bitmap, &binfo, j);
119 assert(bitmap_sfu(bitmap, &binfo) == j);
120 bitmap_unset(bitmap, &binfo, j);
121 }
122 assert(bitmap_get(bitmap, &binfo, 0) == false);
123
124 /*
125 * Iteratively set bits starting at the beginning, and
126 * verify that bitmap_sfu() looks past them.
127 */
128 for (j = 1; j < i; j++) {
129 bitmap_set(bitmap, &binfo, j - 1);
130 assert(bitmap_sfu(bitmap, &binfo) == j);
131 bitmap_unset(bitmap, &binfo, j);
132 }
133 assert(bitmap_sfu(bitmap, &binfo) == i - 1);
134 assert(bitmap_full(bitmap, &binfo));
ad4c0b41 135 free(bitmap);
a78e148b 136 }
137 }
138}
139
140int
141main(void)
142{
ad4c0b41 143 malloc_printf("Test begin\n");
a78e148b 144
145 test_bitmap_size();
146 test_bitmap_init();
147 test_bitmap_set();
148 test_bitmap_unset();
149 test_bitmap_sfu();
150
ad4c0b41 151 malloc_printf("Test end\n");
a78e148b 152 return (0);
153}