diff options
| author | Josh Wolfe <thejoshwolfe@gmail.com> | 2018-11-21 18:47:32 -0500 |
|---|---|---|
| committer | Josh Wolfe <thejoshwolfe@gmail.com> | 2018-11-24 22:25:21 -0500 |
| commit | eed7b48fe3e02670b3d276e09a2dd376348baf68 (patch) | |
| tree | 08c0589dcf137deb58aea43d08682fab8951c494 /std/rand | |
| parent | 49b49618d2db657b22c24a4c78d6516df0fd7e45 (diff) | |
| download | zig-eed7b48fe3e02670b3d276e09a2dd376348baf68.tar.gz zig-eed7b48fe3e02670b3d276e09a2dd376348baf68.zip | |
test lots of types
Diffstat (limited to 'std/rand')
| -rw-r--r-- | std/rand/index.zig | 37 |
1 files changed, 35 insertions, 2 deletions
diff --git a/std/rand/index.zig b/std/rand/index.zig index 43fc43330b..055565ad65 100644 --- a/std/rand/index.zig +++ b/std/rand/index.zig @@ -60,7 +60,8 @@ pub const Random = struct { /// Constant-time implementation off ::uintLessThan. /// The results of this function may be biased. pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T { - assert(T.is_signed == false); + comptime assert(T.is_signed == false); + comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! assert(0 < less_than); // Small is typically u32 const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32); @@ -83,7 +84,8 @@ pub const Random = struct { /// this function is guaranteed to return. /// If you need deterministic runtime bounds, use `::uintLessThanBiased`. pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T { - assert(T.is_signed == false); + comptime assert(T.is_signed == false); + comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation! assert(0 < less_than); // Small is typically u32 const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32); @@ -431,6 +433,37 @@ fn testRandomIntAtMost() void { assert(r.random.uintAtMost(u0, 0) == 0); } +test "Random Biased" { + var r = DefaultPrng.init(0); + // Not thoroughly checking the logic here. + // Just want to execute all the paths with different types. + + assert(r.random.uintLessThanBiased(u1, 1) == 0); + assert(r.random.uintLessThanBiased(u32, 10) < 10); + assert(r.random.uintLessThanBiased(u64, 20) < 20); + + assert(r.random.uintAtMostBiased(u0, 0) == 0); + assert(r.random.uintAtMostBiased(u1, 0) <= 0); + assert(r.random.uintAtMostBiased(u32, 10) <= 10); + assert(r.random.uintAtMostBiased(u64, 20) <= 20); + + assert(r.random.intRangeLessThanBiased(u1, 0, 1) == 0); + assert(r.random.intRangeLessThanBiased(i1, -1, 0) == -1); + assert(r.random.intRangeLessThanBiased(u32, 10, 20) >= 10); + assert(r.random.intRangeLessThanBiased(i32, 10, 20) >= 10); + assert(r.random.intRangeLessThanBiased(u64, 20, 40) >= 20); + assert(r.random.intRangeLessThanBiased(i64, 20, 40) >= 20); + + // uncomment for broken module error: + //assert(r.random.intRangeAtMostBiased(u0, 0, 0) == 0); + assert(r.random.intRangeAtMostBiased(u1, 0, 1) >= 0); + assert(r.random.intRangeAtMostBiased(i1, -1, 0) >= -1); + assert(r.random.intRangeAtMostBiased(u32, 10, 20) >= 10); + assert(r.random.intRangeAtMostBiased(i32, 10, 20) >= 10); + assert(r.random.intRangeAtMostBiased(u64, 20, 40) >= 20); + assert(r.random.intRangeAtMostBiased(i64, 20, 40) >= 20); +} + // Generator to extend 64-bit seed values into longer sequences. // // The number of cycles is thus limited to 64-bits regardless of the engine, but this |
