authorgravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2018-11-21 18:47:32-05:00
committergravatar for thejoshwolfe@gmail.comJosh Wolfe <thejoshwolfe@gmail.com> 2018-11-24 22:25:21-05:00
logeed7b48fe3e02670b3d276e09a2dd376348baf68
tree08c0589dcf137deb58aea43d08682fab8951c494
parent49b49618d2db657b22c24a4c78d6516df0fd7e45

test lots of types


1 files changed, 35 insertions(+), 2 deletions(-)

std/rand/index.zig+35-2
......@@ -60,7 +60,8 @@ pub const Random = struct {
6060 /// Constant-time implementation off ::uintLessThan.
6161 /// The results of this function may be biased.
6262 pub fn uintLessThanBiased(r: *Random, comptime T: type, less_than: T) T {
63 assert(T.is_signed == false);
63 comptime assert(T.is_signed == false);
64 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
6465 assert(0 < less_than);
6566 // Small is typically u32
6667 const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32);
......@@ -83,7 +84,8 @@ pub const Random = struct {
8384 /// this function is guaranteed to return.
8485 /// If you need deterministic runtime bounds, use `::uintLessThanBiased`.
8586 pub fn uintLessThan(r: *Random, comptime T: type, less_than: T) T {
86 assert(T.is_signed == false);
87 comptime assert(T.is_signed == false);
88 comptime assert(T.bit_count <= 64); // TODO: workaround: LLVM ERROR: Unsupported library call operation!
8789 assert(0 < less_than);
8890 // Small is typically u32
8991 const Small = @IntType(false, @divTrunc(T.bit_count + 31, 32) * 32);
......@@ -431,6 +433,37 @@ fn testRandomIntAtMost() void {
431433 assert(r.random.uintAtMost(u0, 0) == 0);
432434}
433435
436test "Random Biased" {
437 var r = DefaultPrng.init(0);
438 // Not thoroughly checking the logic here.
439 // Just want to execute all the paths with different types.
440
441 assert(r.random.uintLessThanBiased(u1, 1) == 0);
442 assert(r.random.uintLessThanBiased(u32, 10) < 10);
443 assert(r.random.uintLessThanBiased(u64, 20) < 20);
444
445 assert(r.random.uintAtMostBiased(u0, 0) == 0);
446 assert(r.random.uintAtMostBiased(u1, 0) <= 0);
447 assert(r.random.uintAtMostBiased(u32, 10) <= 10);
448 assert(r.random.uintAtMostBiased(u64, 20) <= 20);
449
450 assert(r.random.intRangeLessThanBiased(u1, 0, 1) == 0);
451 assert(r.random.intRangeLessThanBiased(i1, -1, 0) == -1);
452 assert(r.random.intRangeLessThanBiased(u32, 10, 20) >= 10);
453 assert(r.random.intRangeLessThanBiased(i32, 10, 20) >= 10);
454 assert(r.random.intRangeLessThanBiased(u64, 20, 40) >= 20);
455 assert(r.random.intRangeLessThanBiased(i64, 20, 40) >= 20);
456
457 // uncomment for broken module error:
458 //assert(r.random.intRangeAtMostBiased(u0, 0, 0) == 0);
459 assert(r.random.intRangeAtMostBiased(u1, 0, 1) >= 0);
460 assert(r.random.intRangeAtMostBiased(i1, -1, 0) >= -1);
461 assert(r.random.intRangeAtMostBiased(u32, 10, 20) >= 10);
462 assert(r.random.intRangeAtMostBiased(i32, 10, 20) >= 10);
463 assert(r.random.intRangeAtMostBiased(u64, 20, 40) >= 20);
464 assert(r.random.intRangeAtMostBiased(i64, 20, 40) >= 20);
465}
466
434467// Generator to extend 64-bit seed values into longer sequences.
435468//
436469// The number of cycles is thus limited to 64-bits regardless of the engine, but this