Skip to contentSkip to conversation

lib/test*.c: Use prandom_u32_max()

RFC PATCH In review George Spelvin 2 participants 2 messages Mar 28, 2020 Library code

lore
Share & tools
lore
#0 lore raw

RFC PATCH 32/50 lib/test*.c: Use prandom_u32_max()

lib/reed_solomon/test_rslib.c alreasy uses prandom_u32(); lib/test_hexdump.c and lib/test-string_helpers.c were using get_random_int() % range, which is needlessly expensive for test code.

Cc Ferdinand Blomqvist <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
---
 lib/reed_solomon/test_rslib.c |  4 ++--
 lib/test-string_helpers.c     |  2 +-
 lib/test_hexdump.c            | 10 +++++-----
 3 files changed, 8 insertions(+), 8 deletions(-)
lib/reed_solomon/test_rslib.c +2 -2
index 4eb29f365ece0..58e767c142ef8 100644
@@ -183,7 +183,7 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
 
 		do {
 			/* Must not choose the same location twice */
-			errloc = prandom_u32() % len;
+			errloc = prandom_u32_max(len);
 		} while (errlocs[errloc] != 0);
 
 		errlocs[errloc] = 1;
@@ -194,7 +194,7 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
 	for (i = 0; i < eras; i++) {
 		do {
 			/* Must not choose the same location twice */
-			errloc = prandom_u32() % len;
+			errloc = prandom_u32_max(len);
 		} while (errlocs[errloc] != 0);
 
 		derrlocs[i] = errloc;
lib/test-string_helpers.c +1 -1
index 25b5cbfb7615b..3349f3ddc528c 100644
@@ -398,7 +398,7 @@ static int __init test_string_helpers_init(void)
 	for (i = 0; i < UNESCAPE_ANY + 1; i++)
 		test_string_unescape("unescape", i, false);
 	test_string_unescape("unescape inplace",
-			     get_random_int() % (UNESCAPE_ANY + 1), true);
+			     prandom_u32_max(UNESCAPE_ANY + 1), true);
 
 	/* Without dictionary */
 	for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
lib/test_hexdump.c +5 -5
index 5144899d3c6b8..54e4efb28b974 100644
@@ -149,7 +149,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 static void __init test_hexdump_set(int rowsize, bool ascii)
 {
 	size_t d = min_t(size_t, sizeof(data_b), rowsize);
-	size_t len = get_random_int() % d + 1;
+	size_t len = prandom_u32_max(d) + 1;
 
 	test_hexdump(len, rowsize, 4, ascii);
 	test_hexdump(len, rowsize, 2, ascii);
@@ -208,11 +208,11 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
 {
 	unsigned int i = 0;
-	int rs = (get_random_int() % 2 + 1) * 16;
+	int rs = (prandom_u32() % 2 + 1) * 16;
 
 	do {
 		int gs = 1 << i;
-		size_t len = get_random_int() % rs + gs;
+		size_t len = prandom_u32_max(rs) + gs;
 
 		test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
 	} while (i++ < 3);
@@ -223,11 +223,11 @@ static int __init test_hexdump_init(void)
 	unsigned int i;
 	int rowsize;
 
-	rowsize = (get_random_int() % 2 + 1) * 16;
+	rowsize = (prandom_u32() % 2 + 1) * 16;
 	for (i = 0; i < 16; i++)
 		test_hexdump_set(rowsize, false);
 
-	rowsize = (get_random_int() % 2 + 1) * 16;
+	rowsize = (prandom_u32() % 2 + 1) * 16;
 	for (i = 0; i < 16; i++)
 		test_hexdump_set(rowsize, true);
 
Signature · 1 line
2.26.0
Reviewed-by on v1 32/50
#1 lore raw
Quoted 6 lines · George Spelvin

lib/reed_solomon/test_rslib.c alreasy uses prandom_u32(); lib/test_hexdump.c and lib/test-string_helpers.c were using get_random_int() % range, which is needlessly expensive for test code.

Fine with me, FWIW,

Quoted 90 lines
Cc Ferdinand Blomqvist <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Andy Shevchenko <[email protected]>
Cc Vitaly Kuznetsov <[email protected]>
---
 lib/reed_solomon/test_rslib.c |  4 ++--
 lib/test-string_helpers.c     |  2 +-
 lib/test_hexdump.c            | 10 +++++-----
 3 files changed, 8 insertions(+), 8 deletions(-)
lib/reed_solomon/test_rslib.c +2 -2
index 4eb29f365ece0..58e767c142ef8 100644
@@ -183,7 +183,7 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
 
                do {
                        /* Must not choose the same location twice */
-                       errloc = prandom_u32() % len;
+                       errloc = prandom_u32_max(len);
                } while (errlocs[errloc] != 0);
 
                errlocs[errloc] = 1;
@@ -194,7 +194,7 @@ static int get_rcw_we(struct rs_control *rs, struct wspace *ws,
        for (i = 0; i < eras; i++) {
                do {
                        /* Must not choose the same location twice */
-                       errloc = prandom_u32() % len;
+                       errloc = prandom_u32_max(len);
                } while (errlocs[errloc] != 0);
 
                derrlocs[i] = errloc;
lib/test-string_helpers.c +1 -1
index 25b5cbfb7615b..3349f3ddc528c 100644
@@ -398,7 +398,7 @@ static int __init test_string_helpers_init(void)
        for (i = 0; i < UNESCAPE_ANY + 1; i++)
                test_string_unescape("unescape", i, false);
        test_string_unescape("unescape inplace",
-                            get_random_int() % (UNESCAPE_ANY + 1), true);
+                            prandom_u32_max(UNESCAPE_ANY + 1), true);
 
        /* Without dictionary */
        for (i = 0; i < (ESCAPE_ANY_NP | ESCAPE_HEX) + 1; i++)
lib/test_hexdump.c +5 -6
index 5144899d3c6b8..54e4efb28b974 100644
@@ -149,7 +149,7 @@ static void __init test_hexdump(size_t len, int rowsize, int groupsize,
 static void __init test_hexdump_set(int rowsize, bool ascii)
 {
        size_t d = min_t(size_t, sizeof(data_b), rowsize);
-       size_t len = get_random_int() % d + 1;
+       size_t len = prandom_u32_max(d) + 1;
 
        test_hexdump(len, rowsize, 4, ascii);
        test_hexdump(len, rowsize, 2, ascii);
@@ -208,11 +208,11 @@ static void __init test_hexdump_overflow(size_t buflen, size_t len,
 static void __init test_hexdump_overflow_set(size_t buflen, bool ascii)
 {
        unsigned int i = 0;
-       int rs = (get_random_int() % 2 + 1) * 16;
+       int rs = (prandom_u32() % 2 + 1) * 16;
 
        do {
                int gs = 1 << i;
-               size_t len = get_random_int() % rs + gs;
+               size_t len = prandom_u32_max(rs) + gs;
 
                test_hexdump_overflow(buflen, rounddown(len, gs), rs, gs, ascii);
        } while (i++ < 3);
@@ -223,11 +223,11 @@ static int __init test_hexdump_init(void)
        unsigned int i;
        int rowsize;
 
-       rowsize = (get_random_int() % 2 + 1) * 16;
+       rowsize = (prandom_u32() % 2 + 1) * 16;
        for (i = 0; i < 16; i++)
                test_hexdump_set(rowsize, false);
 
-       rowsize = (get_random_int() % 2 + 1) * 16;
+       rowsize = (prandom_u32() % 2 + 1) * 16;
        for (i = 0; i < 16; i++)
                test_hexdump_set(rowsize, true);
 
--

2.26.0

Signature · 2 lines
With Best Regards,
Andy Shevchenko