New upstream version 17.11.1
[deb_dpdk.git] / test / test / test_bitmap.c
1 /*
2  *   BSD LICENSE
3  *
4  *   Copyright (C) Cavium, Inc. 2017.
5  *
6  *   Redistribution and use in source and binary forms, with or without
7  *   modification, are permitted provided that the following conditions
8  *   are met:
9  *
10  *     * Redistributions of source code must retain the above copyright
11  *       notice, this list of conditions and the following disclaimer.
12  *     * Redistributions in binary form must reproduce the above copyright
13  *       notice, this list of conditions and the following disclaimer in
14  *       the documentation and/or other materials provided with the
15  *       distribution.
16  *     * Neither the name of Cavium, Inc nor the names of its
17  *       contributors may be used to endorse or promote products derived
18  *       from this software without specific prior written permission.
19  *
20  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32
33 #include <stdio.h>
34 #include <inttypes.h>
35
36 #include <rte_common.h>
37 #include <rte_bitmap.h>
38 #include <rte_malloc.h>
39
40 #include "test.h"
41
42 #define MAX_BITS 1000
43
44 static int
45 test_bitmap_scan_operations(struct rte_bitmap *bmp)
46 {
47         uint32_t pos = 0;
48         uint64_t slab1_magic = 0xBADC0FFEEBADF00D;
49         uint64_t slab2_magic = 0xFEEDDEADDEADF00D;
50         uint64_t out_slab = 0;
51
52         rte_bitmap_reset(bmp);
53
54         rte_bitmap_set_slab(bmp, pos, slab1_magic);
55         rte_bitmap_set_slab(bmp, pos + RTE_BITMAP_SLAB_BIT_SIZE, slab2_magic);
56
57         if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
58                 printf("Failed to get slab from bitmap.\n");
59                 return TEST_FAILED;
60         }
61
62         if (slab1_magic != out_slab) {
63                 printf("Scan operation sanity failed.\n");
64                 return TEST_FAILED;
65         }
66
67         if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
68                 printf("Failed to get slab from bitmap.\n");
69                 return TEST_FAILED;
70         }
71
72         if (slab2_magic != out_slab) {
73                 printf("Scan operation sanity failed.\n");
74                 return TEST_FAILED;
75         }
76
77         /* Wrap around */
78         if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
79                 printf("Failed to get slab from bitmap.\n");
80                 return TEST_FAILED;
81         }
82
83         if (slab1_magic != out_slab) {
84                 printf("Scan operation wrap around failed.\n");
85                 return TEST_FAILED;
86         }
87
88         /* Scan reset check. */
89         __rte_bitmap_scan_init(bmp);
90
91         if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
92                 printf("Failed to get slab from bitmap.\n");
93                 return TEST_FAILED;
94         }
95
96         if (slab1_magic != out_slab) {
97                 printf("Scan reset operation failed.\n");
98                 return TEST_FAILED;
99         }
100
101         return TEST_SUCCESS;
102 }
103
104 static int
105 test_bitmap_slab_set_get(struct rte_bitmap *bmp)
106 {
107         uint32_t pos = 0;
108         uint64_t slab_magic = 0xBADC0FFEEBADF00D;
109         uint64_t out_slab = 0;
110
111         rte_bitmap_reset(bmp);
112         rte_bitmap_set_slab(bmp, pos, slab_magic);
113
114         if (!rte_bitmap_scan(bmp, &pos, &out_slab)) {
115                 printf("Failed to get slab from bitmap.\n");
116                 return TEST_FAILED;
117         }
118
119
120         if (slab_magic != out_slab) {
121                 printf("Invalid slab in bitmap.\n");
122                 return TEST_FAILED;
123         }
124
125
126         return TEST_SUCCESS;
127 }
128
129 static int
130 test_bitmap_set_get_clear(struct rte_bitmap *bmp)
131 {
132         int i;
133
134         rte_bitmap_reset(bmp);
135         for (i = 0; i < MAX_BITS; i++)
136                 rte_bitmap_set(bmp, i);
137
138         for (i = 0; i < MAX_BITS; i++) {
139                 if (!rte_bitmap_get(bmp, i)) {
140                         printf("Failed to get set bit.\n");
141                         return TEST_FAILED;
142                 }
143         }
144
145         for (i = 0; i < MAX_BITS; i++)
146                 rte_bitmap_clear(bmp, i);
147
148         for (i = 0; i < MAX_BITS; i++) {
149                 if (rte_bitmap_get(bmp, i)) {
150                         printf("Failed to clear set bit.\n");
151                         return TEST_FAILED;
152                 }
153         }
154
155         return TEST_SUCCESS;
156 }
157
158 static int
159 test_bitmap(void)
160 {
161         void *mem;
162         uint32_t bmp_size;
163         struct rte_bitmap *bmp;
164
165         bmp_size =
166                 rte_bitmap_get_memory_footprint(MAX_BITS);
167
168         mem = rte_zmalloc("test_bmap", bmp_size, RTE_CACHE_LINE_SIZE);
169         if (mem == NULL) {
170                 printf("Failed to allocate memory for bitmap\n");
171                 return TEST_FAILED;
172         }
173
174         bmp = rte_bitmap_init(MAX_BITS, mem, bmp_size);
175         if (bmp == NULL) {
176                 printf("Failed to init bitmap\n");
177                 return TEST_FAILED;
178         }
179
180         if (test_bitmap_set_get_clear(bmp) < 0)
181                 return TEST_FAILED;
182
183         if (test_bitmap_slab_set_get(bmp) < 0)
184                 return TEST_FAILED;
185
186         if (test_bitmap_scan_operations(bmp) < 0)
187                 return TEST_FAILED;
188
189         rte_bitmap_free(bmp);
190         rte_free(mem);
191
192         return TEST_SUCCESS;
193 }
194
195 REGISTER_TEST_COMMAND(bitmap_test, test_bitmap);