diff options
Diffstat (limited to 'lib/test_bitmap.c')
| -rw-r--r-- | lib/test_bitmap.c | 114 | 
1 files changed, 108 insertions, 6 deletions
diff --git a/lib/test_bitmap.c b/lib/test_bitmap.c index e14a15ac250b..61ed71c1daba 100644 --- a/lib/test_bitmap.c +++ b/lib/test_bitmap.c @@ -275,26 +275,28 @@ static void __init test_copy(void)  static void __init test_replace(void)  {  	unsigned int nbits = 64; +	unsigned int nlongs = DIV_ROUND_UP(nbits, BITS_PER_LONG);  	DECLARE_BITMAP(bmap, 1024);  	bitmap_zero(bmap, 1024); -	bitmap_replace(bmap, &exp2[0], &exp2[1], exp2_to_exp3_mask, nbits); +	bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);  	expect_eq_bitmap(bmap, exp3_0_1, nbits);  	bitmap_zero(bmap, 1024); -	bitmap_replace(bmap, &exp2[1], &exp2[0], exp2_to_exp3_mask, nbits); +	bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);  	expect_eq_bitmap(bmap, exp3_1_0, nbits);  	bitmap_fill(bmap, 1024); -	bitmap_replace(bmap, &exp2[0], &exp2[1], exp2_to_exp3_mask, nbits); +	bitmap_replace(bmap, &exp2[0 * nlongs], &exp2[1 * nlongs], exp2_to_exp3_mask, nbits);  	expect_eq_bitmap(bmap, exp3_0_1, nbits);  	bitmap_fill(bmap, 1024); -	bitmap_replace(bmap, &exp2[1], &exp2[0], exp2_to_exp3_mask, nbits); +	bitmap_replace(bmap, &exp2[1 * nlongs], &exp2[0 * nlongs], exp2_to_exp3_mask, nbits);  	expect_eq_bitmap(bmap, exp3_1_0, nbits);  } -#define PARSE_TIME 0x1 +#define PARSE_TIME	0x1 +#define NO_LEN		0x2  struct test_bitmap_parselist{  	const int errno; @@ -348,7 +350,6 @@ static const struct test_bitmap_parselist parselist_tests[] __initconst = {  	{-EINVAL, "0-31:a/1", NULL, 8, 0},  	{-EINVAL, "0-\n", NULL, 8, 0}, -#undef step  };  static void __init __test_bitmap_parselist(int is_user) @@ -400,6 +401,95 @@ static void __init __test_bitmap_parselist(int is_user)  	}  } +static const unsigned long parse_test[] __initconst = { +	BITMAP_FROM_U64(0), +	BITMAP_FROM_U64(1), +	BITMAP_FROM_U64(0xdeadbeef), +	BITMAP_FROM_U64(0x100000000ULL), +}; + +static const unsigned long parse_test2[] __initconst = { +	BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xdeadbeef), +	BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0xbaadf00ddeadbeef), +	BITMAP_FROM_U64(0x100000000ULL), BITMAP_FROM_U64(0x0badf00ddeadbeef), +}; + +static const struct test_bitmap_parselist parse_tests[] __initconst = { +	{0, "",				&parse_test[0 * step], 32, 0}, +	{0, " ",			&parse_test[0 * step], 32, 0}, +	{0, "0",			&parse_test[0 * step], 32, 0}, +	{0, "0\n",			&parse_test[0 * step], 32, 0}, +	{0, "1",			&parse_test[1 * step], 32, 0}, +	{0, "deadbeef",			&parse_test[2 * step], 32, 0}, +	{0, "1,0",			&parse_test[3 * step], 33, 0}, +	{0, "deadbeef,\n,0,1",		&parse_test[2 * step], 96, 0}, + +	{0, "deadbeef,1,0",		&parse_test2[0 * 2 * step], 96, 0}, +	{0, "baadf00d,deadbeef,1,0",	&parse_test2[1 * 2 * step], 128, 0}, +	{0, "badf00d,deadbeef,1,0",	&parse_test2[2 * 2 * step], 124, 0}, +	{0, "badf00d,deadbeef,1,0",	&parse_test2[2 * 2 * step], 124, NO_LEN}, +	{0, "  badf00d,deadbeef,1,0  ",	&parse_test2[2 * 2 * step], 124, 0}, +	{0, " , badf00d,deadbeef,1,0 , ",	&parse_test2[2 * 2 * step], 124, 0}, +	{0, " , badf00d, ,, ,,deadbeef,1,0 , ",	&parse_test2[2 * 2 * step], 124, 0}, + +	{-EINVAL,    "goodfood,deadbeef,1,0",	NULL, 128, 0}, +	{-EOVERFLOW, "3,0",			NULL, 33, 0}, +	{-EOVERFLOW, "123badf00d,deadbeef,1,0",	NULL, 128, 0}, +	{-EOVERFLOW, "badf00d,deadbeef,1,0",	NULL, 90, 0}, +	{-EOVERFLOW, "fbadf00d,deadbeef,1,0",	NULL, 95, 0}, +	{-EOVERFLOW, "badf00d,deadbeef,1,0",	NULL, 100, 0}, +#undef step +}; + +static void __init __test_bitmap_parse(int is_user) +{ +	int i; +	int err; +	ktime_t time; +	DECLARE_BITMAP(bmap, 2048); +	char *mode = is_user ? "_user"  : ""; + +	for (i = 0; i < ARRAY_SIZE(parse_tests); i++) { +		struct test_bitmap_parselist test = parse_tests[i]; + +		if (is_user) { +			size_t len = strlen(test.in); +			mm_segment_t orig_fs = get_fs(); + +			set_fs(KERNEL_DS); +			time = ktime_get(); +			err = bitmap_parse_user((__force const char __user *)test.in, len, +						bmap, test.nbits); +			time = ktime_get() - time; +			set_fs(orig_fs); +		} else { +			size_t len = test.flags & NO_LEN ? +				UINT_MAX : strlen(test.in); +			time = ktime_get(); +			err = bitmap_parse(test.in, len, bmap, test.nbits); +			time = ktime_get() - time; +		} + +		if (err != test.errno) { +			pr_err("parse%s: %d: input is %s, errno is %d, expected %d\n", +					mode, i, test.in, err, test.errno); +			continue; +		} + +		if (!err && test.expected +			 && !__bitmap_equal(bmap, test.expected, test.nbits)) { +			pr_err("parse%s: %d: input is %s, result is 0x%lx, expected 0x%lx\n", +					mode, i, test.in, bmap[0], +					*test.expected); +			continue; +		} + +		if (test.flags & PARSE_TIME) +			pr_err("parse%s: %d: input is '%s' OK, Time: %llu\n", +					mode, i, test.in, time); +	} +} +  static void __init test_bitmap_parselist(void)  {  	__test_bitmap_parselist(0); @@ -410,6 +500,16 @@ static void __init test_bitmap_parselist_user(void)  	__test_bitmap_parselist(1);  } +static void __init test_bitmap_parse(void) +{ +	__test_bitmap_parse(0); +} + +static void __init test_bitmap_parse_user(void) +{ +	__test_bitmap_parse(1); +} +  #define EXP1_IN_BITS	(sizeof(exp1) * 8)  static void __init test_bitmap_arr32(void) @@ -515,6 +615,8 @@ static void __init selftest(void)  	test_copy();  	test_replace();  	test_bitmap_arr32(); +	test_bitmap_parse(); +	test_bitmap_parse_user();  	test_bitmap_parselist();  	test_bitmap_parselist_user();  	test_mem_optimisations();  | 
