From 4c068ba921233e1c93d4f08c25e45d22c1651316 Mon Sep 17 00:00:00 2001 From: Martin Janiczek Date: Fri, 27 Oct 2023 11:41:38 +0200 Subject: [PATCH] LibTest: Minimize footprint of Gen::unsigned_int, simplify code unsigned_int(0) doesn't need to draw bits from RandomnessSource. An expression for getting INT_MAX for u32 didn't need to be special-cased over the general formula. This is a follow-up on a few comments --- .../Libraries/LibTest/Randomized/Generator.h | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/Userland/Libraries/LibTest/Randomized/Generator.h b/Userland/Libraries/LibTest/Randomized/Generator.h index 444ba8406c..b6f11dbb36 100644 --- a/Userland/Libraries/LibTest/Randomized/Generator.h +++ b/Userland/Libraries/LibTest/Randomized/Generator.h @@ -45,6 +45,9 @@ namespace Gen { // Shrinks towards 0. inline u32 unsigned_int(u32 max) { + if (max == 0) + return 0; + u32 random = Test::randomness_source().draw_value(max, [&]() { return AK::get_random_uniform(max + 1); }); @@ -58,19 +61,15 @@ inline u32 unsigned_int(u32 max) // -> value 10, RandomRun [7] // etc. // -// In case `min == max`, the RandomRun footprint will be smaller, as we'll -// switch to a `constant` and won't need any randomness to generate that -// value: +// In case `min == max`, the RandomRun footprint will be smaller: no randomness +// is needed. // // Gen::unsigned_int(3,3) -> value 3, RandomRun [] (always) // -// Shrinks towards the smaller argument. +// Shrinks towards the minimum. inline u32 unsigned_int(u32 min, u32 max) { VERIFY(max >= min); - if (min == max) { - return min; - } return unsigned_int(max - min) + min; } @@ -174,9 +173,7 @@ inline u32 unsigned_int() NumericLimits::max()); } - u32 max = (bits == 32) - ? NumericLimits::max() - : ((u64)1 << bits) - 1; + u32 max = ((u64)1 << bits) - 1; return unsigned_int(max); }