mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-24 02:12:09 -05:00
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
This commit is contained in:
parent
230aa1404c
commit
4c068ba921
1 changed files with 7 additions and 10 deletions
|
@ -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<u32>::max());
|
||||
}
|
||||
|
||||
u32 max = (bits == 32)
|
||||
? NumericLimits<u32>::max()
|
||||
: ((u64)1 << bits) - 1;
|
||||
u32 max = ((u64)1 << bits) - 1;
|
||||
return unsigned_int(max);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue