Linux Audio

Check our new training course

Loading...
v4.10.11
 
  1/*
  2 * Test cases for printf facility.
  3 */
  4
  5#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  6
  7#include <linux/bitmap.h>
  8#include <linux/init.h>
  9#include <linux/kernel.h>
 10#include <linux/module.h>
 11#include <linux/printk.h>
 12#include <linux/slab.h>
 13#include <linux/string.h>
 
 
 
 14
 15static unsigned total_tests __initdata;
 16static unsigned failed_tests __initdata;
 17
 18static char pbl_buffer[PAGE_SIZE] __initdata;
 19
 20
 21static bool __init
 22__check_eq_uint(const char *srcfile, unsigned int line,
 23		const unsigned int exp_uint, unsigned int x)
 24{
 25	if (exp_uint != x) {
 26		pr_warn("[%s:%u] expected %u, got %u\n",
 27			srcfile, line, exp_uint, x);
 28		return false;
 29	}
 30	return true;
 31}
 32
 33
 34static bool __init
 35__check_eq_bitmap(const char *srcfile, unsigned int line,
 36		  const unsigned long *exp_bmap, unsigned int exp_nbits,
 37		  const unsigned long *bmap, unsigned int nbits)
 38{
 39	if (exp_nbits != nbits) {
 40		pr_warn("[%s:%u] bitmap length mismatch: expected %u, got %u\n",
 41			srcfile, line, exp_nbits, nbits);
 42		return false;
 43	}
 44
 45	if (!bitmap_equal(exp_bmap, bmap, nbits)) {
 46		pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
 47			srcfile, line,
 48			exp_nbits, exp_bmap, nbits, bmap);
 49		return false;
 50	}
 51	return true;
 52}
 53
 54static bool __init
 55__check_eq_pbl(const char *srcfile, unsigned int line,
 56	       const char *expected_pbl,
 57	       const unsigned long *bitmap, unsigned int nbits)
 58{
 59	snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
 60	if (strcmp(expected_pbl, pbl_buffer)) {
 61		pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
 62			srcfile, line,
 63			expected_pbl, pbl_buffer);
 64		return false;
 65	}
 66	return true;
 67}
 68
 69static bool __init
 70__check_eq_u32_array(const char *srcfile, unsigned int line,
 71		     const u32 *exp_arr, unsigned int exp_len,
 
 
 
 
 72		     const u32 *arr, unsigned int len)
 73{
 74	if (exp_len != len) {
 75		pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
 76			srcfile, line,
 77			exp_len, len);
 78		return false;
 79	}
 80
 81	if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
 82		pr_warn("[%s:%u] array contents differ\n", srcfile, line);
 83		print_hex_dump(KERN_WARNING, "  exp:  ", DUMP_PREFIX_OFFSET,
 84			       32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
 85		print_hex_dump(KERN_WARNING, "  got:  ", DUMP_PREFIX_OFFSET,
 86			       32, 4, arr, len*sizeof(*arr), false);
 87		return false;
 88	}
 89
 90	return true;
 91}
 92
 93#define __expect_eq(suffix, ...)					\
 94	({								\
 95		int result = 0;						\
 96		total_tests++;						\
 97		if (!__check_eq_ ## suffix(__FILE__, __LINE__,		\
 98					   ##__VA_ARGS__)) {		\
 99			failed_tests++;					\
100			result = 1;					\
101		}							\
102		result;							\
103	})
104
105#define expect_eq_uint(...)		__expect_eq(uint, ##__VA_ARGS__)
106#define expect_eq_bitmap(...)		__expect_eq(bitmap, ##__VA_ARGS__)
107#define expect_eq_pbl(...)		__expect_eq(pbl, ##__VA_ARGS__)
108#define expect_eq_u32_array(...)	__expect_eq(u32_array, ##__VA_ARGS__)
109
110static void __init test_zero_fill_copy(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
111{
112	DECLARE_BITMAP(bmap1, 1024);
113	DECLARE_BITMAP(bmap2, 1024);
114
115	bitmap_zero(bmap1, 1024);
116	bitmap_zero(bmap2, 1024);
117
118	/* single-word bitmaps */
119	expect_eq_pbl("", bmap1, 23);
120
121	bitmap_fill(bmap1, 19);
122	expect_eq_pbl("0-18", bmap1, 1024);
123
124	bitmap_copy(bmap2, bmap1, 23);
125	expect_eq_pbl("0-18", bmap2, 1024);
126
127	bitmap_fill(bmap2, 23);
128	expect_eq_pbl("0-22", bmap2, 1024);
129
130	bitmap_copy(bmap2, bmap1, 23);
131	expect_eq_pbl("0-18", bmap2, 1024);
132
133	bitmap_zero(bmap1, 23);
134	expect_eq_pbl("", bmap1, 1024);
135
136	/* multi-word bitmaps */
137	bitmap_zero(bmap1, 1024);
138	expect_eq_pbl("", bmap1, 1024);
139
140	bitmap_fill(bmap1, 109);
141	expect_eq_pbl("0-108", bmap1, 1024);
142
143	bitmap_copy(bmap2, bmap1, 1024);
144	expect_eq_pbl("0-108", bmap2, 1024);
145
146	bitmap_fill(bmap2, 1024);
147	expect_eq_pbl("0-1023", bmap2, 1024);
148
149	bitmap_copy(bmap2, bmap1, 1024);
150	expect_eq_pbl("0-108", bmap2, 1024);
151
152	/* the following tests assume a 32- or 64-bit arch (even 128b
153	 * if we care)
154	 */
155
156	bitmap_fill(bmap2, 1024);
157	bitmap_copy(bmap2, bmap1, 109);  /* ... but 0-padded til word length */
158	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
159
160	bitmap_fill(bmap2, 1024);
161	bitmap_copy(bmap2, bmap1, 97);  /* ... but aligned on word length */
162	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
163
164	bitmap_zero(bmap2, 97);  /* ... but 0-padded til word length */
165	expect_eq_pbl("128-1023", bmap2, 1024);
166}
167
168static void __init test_bitmap_u32_array_conversions(void)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
169{
170	DECLARE_BITMAP(bmap1, 1024);
171	DECLARE_BITMAP(bmap2, 1024);
172	u32 exp_arr[32], arr[32];
173	unsigned nbits;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
174
175	for (nbits = 0 ; nbits < 257 ; ++nbits) {
176		const unsigned int used_u32s = DIV_ROUND_UP(nbits, 32);
177		unsigned int i, rv;
178
179		bitmap_zero(bmap1, nbits);
180		bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
181
182		memset(arr, 0xff, sizeof(arr));
183		rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
184		expect_eq_uint(nbits, rv);
185
186		memset(exp_arr, 0xff, sizeof(exp_arr));
187		memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
188		expect_eq_u32_array(exp_arr, 32, arr, 32);
189
190		bitmap_fill(bmap2, 1024);
191		rv = bitmap_from_u32array(bmap2, nbits, arr, used_u32s);
192		expect_eq_uint(nbits, rv);
193		expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
194
195		for (i = 0 ; i < nbits ; ++i) {
196			/*
197			 * test conversion bitmap -> u32[]
198			 */
199
200			bitmap_zero(bmap1, 1024);
201			__set_bit(i, bmap1);
202			bitmap_set(bmap1, nbits, 1024 - nbits);  /* garbage */
203
204			memset(arr, 0xff, sizeof(arr));
205			rv = bitmap_to_u32array(arr, used_u32s, bmap1, nbits);
206			expect_eq_uint(nbits, rv);
207
208			/* 1st used u32 words contain expected bit set, the
209			 * remaining words are left unchanged (0xff)
210			 */
211			memset(exp_arr, 0xff, sizeof(exp_arr));
212			memset(exp_arr, 0, used_u32s*sizeof(*exp_arr));
213			exp_arr[i/32] = (1U<<(i%32));
214			expect_eq_u32_array(exp_arr, 32, arr, 32);
215
216
217			/* same, with longer array to fill
218			 */
219			memset(arr, 0xff, sizeof(arr));
220			rv = bitmap_to_u32array(arr, 32, bmap1, nbits);
221			expect_eq_uint(nbits, rv);
222
223			/* 1st used u32 words contain expected bit set, the
224			 * remaining words are all 0s
225			 */
226			memset(exp_arr, 0, sizeof(exp_arr));
227			exp_arr[i/32] = (1U<<(i%32));
228			expect_eq_u32_array(exp_arr, 32, arr, 32);
229
230			/*
231			 * test conversion u32[] -> bitmap
232			 */
233
234			/* the 1st nbits of bmap2 are identical to
235			 * bmap1, the remaining bits of bmap2 are left
236			 * unchanged (all 1s)
237			 */
238			bitmap_fill(bmap2, 1024);
239			rv = bitmap_from_u32array(bmap2, nbits,
240						  exp_arr, used_u32s);
241			expect_eq_uint(nbits, rv);
242
243			expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
244
245			/* same, with more bits to fill
246			 */
247			memset(arr, 0xff, sizeof(arr));  /* garbage */
248			memset(arr, 0, used_u32s*sizeof(u32));
249			arr[i/32] = (1U<<(i%32));
250
251			bitmap_fill(bmap2, 1024);
252			rv = bitmap_from_u32array(bmap2, 1024, arr, used_u32s);
253			expect_eq_uint(used_u32s*32, rv);
254
255			/* the 1st nbits of bmap2 are identical to
256			 * bmap1, the remaining bits of bmap2 are cleared
257			 */
258			bitmap_zero(bmap1, 1024);
259			__set_bit(i, bmap1);
260			expect_eq_bitmap(bmap1, 1024, bmap2, 1024);
261
262
263			/*
264			 * test short conversion bitmap -> u32[] (1
265			 * word too short)
266			 */
267			if (used_u32s > 1) {
268				bitmap_zero(bmap1, 1024);
269				__set_bit(i, bmap1);
270				bitmap_set(bmap1, nbits,
271					   1024 - nbits);  /* garbage */
272				memset(arr, 0xff, sizeof(arr));
273
274				rv = bitmap_to_u32array(arr, used_u32s - 1,
275							bmap1, nbits);
276				expect_eq_uint((used_u32s - 1)*32, rv);
277
278				/* 1st used u32 words contain expected
279				 * bit set, the remaining words are
280				 * left unchanged (0xff)
281				 */
282				memset(exp_arr, 0xff, sizeof(exp_arr));
283				memset(exp_arr, 0,
284				       (used_u32s-1)*sizeof(*exp_arr));
285				if ((i/32) < (used_u32s - 1))
286					exp_arr[i/32] = (1U<<(i%32));
287				expect_eq_u32_array(exp_arr, 32, arr, 32);
288			}
289
290			/*
291			 * test short conversion u32[] -> bitmap (3
292			 * bits too short)
293			 */
294			if (nbits > 3) {
295				memset(arr, 0xff, sizeof(arr));  /* garbage */
296				memset(arr, 0, used_u32s*sizeof(*arr));
297				arr[i/32] = (1U<<(i%32));
298
299				bitmap_zero(bmap1, 1024);
300				rv = bitmap_from_u32array(bmap1, nbits - 3,
301							  arr, used_u32s);
302				expect_eq_uint(nbits - 3, rv);
303
304				/* we are expecting the bit < nbits -
305				 * 3 (none otherwise), and the rest of
306				 * bmap1 unchanged (0-filled)
307				 */
308				bitmap_zero(bmap2, 1024);
309				if (i < nbits - 3)
310					__set_bit(i, bmap2);
311				expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
312
313				/* do the same with bmap1 initially
314				 * 1-filled
315				 */
316
317				bitmap_fill(bmap1, 1024);
318				rv = bitmap_from_u32array(bmap1, nbits - 3,
319							 arr, used_u32s);
320				expect_eq_uint(nbits - 3, rv);
321
322				/* we are expecting the bit < nbits -
323				 * 3 (none otherwise), and the rest of
324				 * bmap1 unchanged (1-filled)
325				 */
326				bitmap_zero(bmap2, 1024);
327				if (i < nbits - 3)
328					__set_bit(i, bmap2);
329				bitmap_set(bmap2, nbits-3, 1024 - nbits + 3);
330				expect_eq_bitmap(bmap2, 1024, bmap1, 1024);
331			}
332		}
 
 
 
 
333	}
334}
335
336static int __init test_bitmap_init(void)
 
 
 
 
 
337{
338	test_zero_fill_copy();
339	test_bitmap_u32_array_conversions();
340
341	if (failed_tests == 0)
342		pr_info("all %u tests passed\n", total_tests);
343	else
344		pr_warn("failed %u out of %u tests\n",
345			failed_tests, total_tests);
346
347	return failed_tests ? -EINVAL : 0;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
348}
349
350static void __exit test_bitmap_cleanup(void)
351{
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
352}
353
354module_init(test_bitmap_init);
355module_exit(test_bitmap_cleanup);
 
 
 
 
 
 
 
 
356
 
357MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
358MODULE_LICENSE("GPL");
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * Test cases for printf facility.
  4 */
  5
  6#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7
  8#include <linux/bitmap.h>
  9#include <linux/init.h>
 10#include <linux/kernel.h>
 11#include <linux/module.h>
 12#include <linux/printk.h>
 13#include <linux/slab.h>
 14#include <linux/string.h>
 15#include <linux/uaccess.h>
 16
 17#include "../tools/testing/selftests/kselftest_module.h"
 18
 19static unsigned total_tests __initdata;
 20static unsigned failed_tests __initdata;
 21
 22static char pbl_buffer[PAGE_SIZE] __initdata;
 23
 24
 25static bool __init
 26__check_eq_uint(const char *srcfile, unsigned int line,
 27		const unsigned int exp_uint, unsigned int x)
 28{
 29	if (exp_uint != x) {
 30		pr_err("[%s:%u] expected %u, got %u\n",
 31			srcfile, line, exp_uint, x);
 32		return false;
 33	}
 34	return true;
 35}
 36
 37
 38static bool __init
 39__check_eq_bitmap(const char *srcfile, unsigned int line,
 40		  const unsigned long *exp_bmap, const unsigned long *bmap,
 41		  unsigned int nbits)
 42{
 
 
 
 
 
 
 43	if (!bitmap_equal(exp_bmap, bmap, nbits)) {
 44		pr_warn("[%s:%u] bitmaps contents differ: expected \"%*pbl\", got \"%*pbl\"\n",
 45			srcfile, line,
 46			nbits, exp_bmap, nbits, bmap);
 47		return false;
 48	}
 49	return true;
 50}
 51
 52static bool __init
 53__check_eq_pbl(const char *srcfile, unsigned int line,
 54	       const char *expected_pbl,
 55	       const unsigned long *bitmap, unsigned int nbits)
 56{
 57	snprintf(pbl_buffer, sizeof(pbl_buffer), "%*pbl", nbits, bitmap);
 58	if (strcmp(expected_pbl, pbl_buffer)) {
 59		pr_warn("[%s:%u] expected \"%s\", got \"%s\"\n",
 60			srcfile, line,
 61			expected_pbl, pbl_buffer);
 62		return false;
 63	}
 64	return true;
 65}
 66
 67static bool __init
 68__check_eq_u32_array(const char *srcfile, unsigned int line,
 69		     const u32 *exp_arr, unsigned int exp_len,
 70		     const u32 *arr, unsigned int len) __used;
 71static bool __init
 72__check_eq_u32_array(const char *srcfile, unsigned int line,
 73		     const u32 *exp_arr, unsigned int exp_len,
 74		     const u32 *arr, unsigned int len)
 75{
 76	if (exp_len != len) {
 77		pr_warn("[%s:%u] array length differ: expected %u, got %u\n",
 78			srcfile, line,
 79			exp_len, len);
 80		return false;
 81	}
 82
 83	if (memcmp(exp_arr, arr, len*sizeof(*arr))) {
 84		pr_warn("[%s:%u] array contents differ\n", srcfile, line);
 85		print_hex_dump(KERN_WARNING, "  exp:  ", DUMP_PREFIX_OFFSET,
 86			       32, 4, exp_arr, exp_len*sizeof(*exp_arr), false);
 87		print_hex_dump(KERN_WARNING, "  got:  ", DUMP_PREFIX_OFFSET,
 88			       32, 4, arr, len*sizeof(*arr), false);
 89		return false;
 90	}
 91
 92	return true;
 93}
 94
 95#define __expect_eq(suffix, ...)					\
 96	({								\
 97		int result = 0;						\
 98		total_tests++;						\
 99		if (!__check_eq_ ## suffix(__FILE__, __LINE__,		\
100					   ##__VA_ARGS__)) {		\
101			failed_tests++;					\
102			result = 1;					\
103		}							\
104		result;							\
105	})
106
107#define expect_eq_uint(...)		__expect_eq(uint, ##__VA_ARGS__)
108#define expect_eq_bitmap(...)		__expect_eq(bitmap, ##__VA_ARGS__)
109#define expect_eq_pbl(...)		__expect_eq(pbl, ##__VA_ARGS__)
110#define expect_eq_u32_array(...)	__expect_eq(u32_array, ##__VA_ARGS__)
111
112static void __init test_zero_clear(void)
113{
114	DECLARE_BITMAP(bmap, 1024);
115
116	/* Known way to set all bits */
117	memset(bmap, 0xff, 128);
118
119	expect_eq_pbl("0-22", bmap, 23);
120	expect_eq_pbl("0-1023", bmap, 1024);
121
122	/* single-word bitmaps */
123	bitmap_clear(bmap, 0, 9);
124	expect_eq_pbl("9-1023", bmap, 1024);
125
126	bitmap_zero(bmap, 35);
127	expect_eq_pbl("64-1023", bmap, 1024);
128
129	/* cross boundaries operations */
130	bitmap_clear(bmap, 79, 19);
131	expect_eq_pbl("64-78,98-1023", bmap, 1024);
132
133	bitmap_zero(bmap, 115);
134	expect_eq_pbl("128-1023", bmap, 1024);
135
136	/* Zeroing entire area */
137	bitmap_zero(bmap, 1024);
138	expect_eq_pbl("", bmap, 1024);
139}
140
141static void __init test_fill_set(void)
142{
143	DECLARE_BITMAP(bmap, 1024);
144
145	/* Known way to clear all bits */
146	memset(bmap, 0x00, 128);
147
148	expect_eq_pbl("", bmap, 23);
149	expect_eq_pbl("", bmap, 1024);
150
151	/* single-word bitmaps */
152	bitmap_set(bmap, 0, 9);
153	expect_eq_pbl("0-8", bmap, 1024);
154
155	bitmap_fill(bmap, 35);
156	expect_eq_pbl("0-63", bmap, 1024);
157
158	/* cross boundaries operations */
159	bitmap_set(bmap, 79, 19);
160	expect_eq_pbl("0-63,79-97", bmap, 1024);
161
162	bitmap_fill(bmap, 115);
163	expect_eq_pbl("0-127", bmap, 1024);
164
165	/* Zeroing entire area */
166	bitmap_fill(bmap, 1024);
167	expect_eq_pbl("0-1023", bmap, 1024);
168}
169
170static void __init test_copy(void)
171{
172	DECLARE_BITMAP(bmap1, 1024);
173	DECLARE_BITMAP(bmap2, 1024);
174
175	bitmap_zero(bmap1, 1024);
176	bitmap_zero(bmap2, 1024);
177
178	/* single-word bitmaps */
179	bitmap_set(bmap1, 0, 19);
 
 
 
 
180	bitmap_copy(bmap2, bmap1, 23);
181	expect_eq_pbl("0-18", bmap2, 1024);
182
183	bitmap_set(bmap2, 0, 23);
 
 
184	bitmap_copy(bmap2, bmap1, 23);
185	expect_eq_pbl("0-18", bmap2, 1024);
186
 
 
 
187	/* multi-word bitmaps */
188	bitmap_set(bmap1, 0, 109);
 
 
 
 
 
189	bitmap_copy(bmap2, bmap1, 1024);
190	expect_eq_pbl("0-108", bmap2, 1024);
191
192	bitmap_fill(bmap2, 1024);
 
 
193	bitmap_copy(bmap2, bmap1, 1024);
194	expect_eq_pbl("0-108", bmap2, 1024);
195
196	/* the following tests assume a 32- or 64-bit arch (even 128b
197	 * if we care)
198	 */
199
200	bitmap_fill(bmap2, 1024);
201	bitmap_copy(bmap2, bmap1, 109);  /* ... but 0-padded til word length */
202	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
203
204	bitmap_fill(bmap2, 1024);
205	bitmap_copy(bmap2, bmap1, 97);  /* ... but aligned on word length */
206	expect_eq_pbl("0-108,128-1023", bmap2, 1024);
 
 
 
207}
208
209#define PARSE_TIME 0x1
210
211struct test_bitmap_parselist{
212	const int errno;
213	const char *in;
214	const unsigned long *expected;
215	const int nbits;
216	const int flags;
217};
218
219static const unsigned long exp[] __initconst = {
220	BITMAP_FROM_U64(1),
221	BITMAP_FROM_U64(2),
222	BITMAP_FROM_U64(0x0000ffff),
223	BITMAP_FROM_U64(0xffff0000),
224	BITMAP_FROM_U64(0x55555555),
225	BITMAP_FROM_U64(0xaaaaaaaa),
226	BITMAP_FROM_U64(0x11111111),
227	BITMAP_FROM_U64(0x22222222),
228	BITMAP_FROM_U64(0xffffffff),
229	BITMAP_FROM_U64(0xfffffffe),
230	BITMAP_FROM_U64(0x3333333311111111ULL),
231	BITMAP_FROM_U64(0xffffffff77777777ULL),
232	BITMAP_FROM_U64(0),
233};
234
235static const unsigned long exp2[] __initconst = {
236	BITMAP_FROM_U64(0x3333333311111111ULL),
237	BITMAP_FROM_U64(0xffffffff77777777ULL)
238};
239
240static const struct test_bitmap_parselist parselist_tests[] __initconst = {
241#define step (sizeof(u64) / sizeof(unsigned long))
242
243	{0, "0",			&exp[0], 8, 0},
244	{0, "1",			&exp[1 * step], 8, 0},
245	{0, "0-15",			&exp[2 * step], 32, 0},
246	{0, "16-31",			&exp[3 * step], 32, 0},
247	{0, "0-31:1/2",			&exp[4 * step], 32, 0},
248	{0, "1-31:1/2",			&exp[5 * step], 32, 0},
249	{0, "0-31:1/4",			&exp[6 * step], 32, 0},
250	{0, "1-31:1/4",			&exp[7 * step], 32, 0},
251	{0, "0-31:4/4",			&exp[8 * step], 32, 0},
252	{0, "1-31:4/4",			&exp[9 * step], 32, 0},
253	{0, "0-31:1/4,32-63:2/4",	&exp[10 * step], 64, 0},
254	{0, "0-31:3/4,32-63:4/4",	&exp[11 * step], 64, 0},
255	{0, "  ,,  0-31:3/4  ,, 32-63:4/4  ,,  ",	&exp[11 * step], 64, 0},
256
257	{0, "0-31:1/4,32-63:2/4,64-95:3/4,96-127:4/4",	exp2, 128, 0},
258
259	{0, "0-2047:128/256", NULL, 2048, PARSE_TIME},
260
261	{0, "",				&exp[12 * step], 8, 0},
262	{0, "\n",			&exp[12 * step], 8, 0},
263	{0, ",,  ,,  , ,  ,",		&exp[12 * step], 8, 0},
264	{0, " ,  ,,  , ,   ",		&exp[12 * step], 8, 0},
265	{0, " ,  ,,  , ,   \n",		&exp[12 * step], 8, 0},
266
267	{-EINVAL, "-1",	NULL, 8, 0},
268	{-EINVAL, "-0",	NULL, 8, 0},
269	{-EINVAL, "10-1", NULL, 8, 0},
270	{-EINVAL, "0-31:", NULL, 8, 0},
271	{-EINVAL, "0-31:0", NULL, 8, 0},
272	{-EINVAL, "0-31:0/", NULL, 8, 0},
273	{-EINVAL, "0-31:0/0", NULL, 8, 0},
274	{-EINVAL, "0-31:1/0", NULL, 8, 0},
275	{-EINVAL, "0-31:10/1", NULL, 8, 0},
276	{-EOVERFLOW, "0-98765432123456789:10/1", NULL, 8, 0},
277
278	{-EINVAL, "a-31", NULL, 8, 0},
279	{-EINVAL, "0-a1", NULL, 8, 0},
280	{-EINVAL, "a-31:10/1", NULL, 8, 0},
281	{-EINVAL, "0-31:a/1", NULL, 8, 0},
282	{-EINVAL, "0-\n", NULL, 8, 0},
283};
284
285static void __init __test_bitmap_parselist(int is_user)
286{
287	int i;
288	int err;
289	ktime_t time;
290	DECLARE_BITMAP(bmap, 2048);
291	char *mode = is_user ? "_user"  : "";
292
293	for (i = 0; i < ARRAY_SIZE(parselist_tests); i++) {
294#define ptest parselist_tests[i]
295
296		if (is_user) {
297			mm_segment_t orig_fs = get_fs();
298			size_t len = strlen(ptest.in);
299
300			set_fs(KERNEL_DS);
301			time = ktime_get();
302			err = bitmap_parselist_user(ptest.in, len,
303						    bmap, ptest.nbits);
304			time = ktime_get() - time;
305			set_fs(orig_fs);
306		} else {
307			time = ktime_get();
308			err = bitmap_parselist(ptest.in, bmap, ptest.nbits);
309			time = ktime_get() - time;
310		}
311
312		if (err != ptest.errno) {
313			pr_err("parselist%s: %d: input is %s, errno is %d, expected %d\n",
314					mode, i, ptest.in, err, ptest.errno);
315			continue;
316		}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
317
318		if (!err && ptest.expected
319			 && !__bitmap_equal(bmap, ptest.expected, ptest.nbits)) {
320			pr_err("parselist%s: %d: input is %s, result is 0x%lx, expected 0x%lx\n",
321					mode, i, ptest.in, bmap[0],
322					*ptest.expected);
323			continue;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
324		}
325
326		if (ptest.flags & PARSE_TIME)
327			pr_err("parselist%s: %d: input is '%s' OK, Time: %llu\n",
328					mode, i, ptest.in, time);
329	}
330}
331
332static void __init test_bitmap_parselist(void)
333{
334	__test_bitmap_parselist(0);
335}
336
337static void __init test_bitmap_parselist_user(void)
338{
339	__test_bitmap_parselist(1);
340}
341
342#define EXP_BYTES	(sizeof(exp) * 8)
 
 
 
 
343
344static void __init test_bitmap_arr32(void)
345{
346	unsigned int nbits, next_bit;
347	u32 arr[sizeof(exp) / 4];
348	DECLARE_BITMAP(bmap2, EXP_BYTES);
349
350	memset(arr, 0xa5, sizeof(arr));
351
352	for (nbits = 0; nbits < EXP_BYTES; ++nbits) {
353		bitmap_to_arr32(arr, exp, nbits);
354		bitmap_from_arr32(bmap2, arr, nbits);
355		expect_eq_bitmap(bmap2, exp, nbits);
356
357		next_bit = find_next_bit(bmap2,
358				round_up(nbits, BITS_PER_LONG), nbits);
359		if (next_bit < round_up(nbits, BITS_PER_LONG))
360			pr_err("bitmap_copy_arr32(nbits == %d:"
361				" tail is not safely cleared: %d\n",
362				nbits, next_bit);
363
364		if (nbits < EXP_BYTES - 32)
365			expect_eq_uint(arr[DIV_ROUND_UP(nbits, 32)],
366								0xa5a5a5a5);
367	}
368}
369
370static void noinline __init test_mem_optimisations(void)
371{
372	DECLARE_BITMAP(bmap1, 1024);
373	DECLARE_BITMAP(bmap2, 1024);
374	unsigned int start, nbits;
375
376	for (start = 0; start < 1024; start += 8) {
377		for (nbits = 0; nbits < 1024 - start; nbits += 8) {
378			memset(bmap1, 0x5a, sizeof(bmap1));
379			memset(bmap2, 0x5a, sizeof(bmap2));
380
381			bitmap_set(bmap1, start, nbits);
382			__bitmap_set(bmap2, start, nbits);
383			if (!bitmap_equal(bmap1, bmap2, 1024)) {
384				printk("set not equal %d %d\n", start, nbits);
385				failed_tests++;
386			}
387			if (!__bitmap_equal(bmap1, bmap2, 1024)) {
388				printk("set not __equal %d %d\n", start, nbits);
389				failed_tests++;
390			}
391
392			bitmap_clear(bmap1, start, nbits);
393			__bitmap_clear(bmap2, start, nbits);
394			if (!bitmap_equal(bmap1, bmap2, 1024)) {
395				printk("clear not equal %d %d\n", start, nbits);
396				failed_tests++;
397			}
398			if (!__bitmap_equal(bmap1, bmap2, 1024)) {
399				printk("clear not __equal %d %d\n", start,
400									nbits);
401				failed_tests++;
402			}
403		}
404	}
405}
406
407static void __init selftest(void)
408{
409	test_zero_clear();
410	test_fill_set();
411	test_copy();
412	test_bitmap_arr32();
413	test_bitmap_parselist();
414	test_bitmap_parselist_user();
415	test_mem_optimisations();
416}
417
418KSTM_MODULE_LOADERS(test_bitmap);
419MODULE_AUTHOR("david decotigny <david.decotigny@googlers.com>");
420MODULE_LICENSE("GPL");