Skip to content

Commit

Permalink
add std.testing.random_seed
Browse files Browse the repository at this point in the history
closes #17609
  • Loading branch information
andrewrk committed Jul 22, 2024
1 parent eac7fd4 commit 72695a5
Show file tree
Hide file tree
Showing 13 changed files with 35 additions and 18 deletions.
5 changes: 2 additions & 3 deletions lib/compiler/build_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,6 @@ pub fn main() !void {
var max_rss: u64 = 0;
var skip_oom_steps = false;
var color: Color = .auto;
var seed: u32 = 0;
var prominent_compile_errors = false;
var help_menu = false;
var steps_menu = false;
Expand Down Expand Up @@ -188,7 +187,7 @@ pub fn main() !void {
} else if (mem.eql(u8, arg, "--seed")) {
const next_arg = nextArg(args, &arg_idx) orelse
fatalWithHint("expected u32 after '{s}'", .{arg});
seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| {
graph.random_seed = std.fmt.parseUnsigned(u32, next_arg, 0) catch |err| {
fatal("unable to parse seed '{s}' as unsigned 32-bit integer: {s}\n", .{
next_arg, @errorName(err),
});
Expand Down Expand Up @@ -371,7 +370,7 @@ pub fn main() !void {
}

const gpa = arena;
prepare(gpa, arena, builder, targets.items, &run, seed) catch |err| switch (err) {
prepare(gpa, arena, builder, targets.items, &run, graph.random_seed) catch |err| switch (err) {
error.UncleanExit => process.exit(1),
else => return err,
};
Expand Down
3 changes: 3 additions & 0 deletions lib/compiler/test_runner.zig
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ pub fn main() void {
for (args[1..]) |arg| {
if (std.mem.eql(u8, arg, "--listen=-")) {
listen = true;
} else if (std.mem.startsWith(u8, arg, "--seed=")) {
std.testing.random_seed = std.fmt.parseUnsigned(u32, arg["--seed=".len..], 0) catch
@panic("unable to parse --seed command line argument");
} else {
@panic("unrecognized command line argument");
}
Expand Down
1 change: 1 addition & 0 deletions lib/std/Build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ pub const Graph = struct {
/// Information about the native target. Computed before build() is invoked.
host: ResolvedTarget,
incremental: ?bool = null,
random_seed: u32 = 0,
};

const AvailableDeps = []const struct { []const u8, []const u8 };
Expand Down
7 changes: 6 additions & 1 deletion lib/std/Build/Step/Run.zig
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,13 @@ pub fn setName(run: *Run, name: []const u8) void {
}

pub fn enableTestRunnerMode(run: *Run) void {
const b = run.step.owner;
const arena = b.allocator;
run.stdio = .zig_test;
run.addArgs(&.{"--listen=-"});
run.addArgs(&.{
std.fmt.allocPrint(arena, "--seed=0x{x}", .{b.graph.random_seed}) catch @panic("OOM"),
"--listen=-",
});
}

pub fn addArtifactArg(run: *Run, artifact: *Step.Compile) void {
Expand Down
4 changes: 3 additions & 1 deletion lib/std/Random/ziggurat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,9 @@ pub fn norm_zero_case(random: Random, u: f64) f64 {
}
}

test "normal dist sanity" {
test "normal dist smoke test" {
// Hardcode 0 as the seed because it's possible a seed exists that fails
// this test.
var prng = Random.DefaultPrng.init(0);
const random = prng.random();

Expand Down
4 changes: 2 additions & 2 deletions lib/std/hash_map.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1935,7 +1935,7 @@ test "put and remove loop in random order" {
while (i < size) : (i += 1) {
try keys.append(i);
}
var prng = std.Random.DefaultPrng.init(0);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();

while (i < iterations) : (i += 1) {
Expand Down Expand Up @@ -1967,7 +1967,7 @@ test "remove one million elements in random order" {
keys.append(i) catch unreachable;
}

var prng = std.Random.DefaultPrng.init(0);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();
random.shuffle(u32, keys.items);

Expand Down
2 changes: 1 addition & 1 deletion lib/std/heap/arena_allocator.zig
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ test "reset with preheating" {
var arena_allocator = ArenaAllocator.init(std.testing.allocator);
defer arena_allocator.deinit();
// provides some variance in the allocated data
var rng_src = std.Random.DefaultPrng.init(19930913);
var rng_src = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = rng_src.random();
var rounds: usize = 25;
while (rounds > 0) {
Expand Down
2 changes: 1 addition & 1 deletion lib/std/math/big/rational.zig
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ test "toFloat" {
test "set/to Float round-trip" {
var a = try Rational.init(testing.allocator);
defer a.deinit();
var prng = std.Random.DefaultPrng.init(0x5EED);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();
var i: usize = 0;
while (i < 512) : (i += 1) {
Expand Down
4 changes: 2 additions & 2 deletions lib/std/os/linux/IoUring.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3995,7 +3995,7 @@ test "ring mapped buffers recv" {
defer fds.close();

// for random user_data in sqe/cqe
var Rnd = std.Random.DefaultPrng.init(0);
var Rnd = std.Random.DefaultPrng.init(std.testing.random_seed);
var rnd = Rnd.random();

var round: usize = 4; // repeat send/recv cycle round times
Expand Down Expand Up @@ -4081,7 +4081,7 @@ test "ring mapped buffers multishot recv" {
defer fds.close();

// for random user_data in sqe/cqe
var Rnd = std.Random.DefaultPrng.init(0);
var Rnd = std.Random.DefaultPrng.init(std.testing.random_seed);
var rnd = Rnd.random();

var round: usize = 4; // repeat send/recv cycle round times
Expand Down
6 changes: 3 additions & 3 deletions lib/std/priority_dequeue.zig
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,7 @@ test "shrinkAndFree" {
}

test "fuzz testing min" {
var prng = std.Random.DefaultPrng.init(0x12345678);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();

const test_case_count = 100;
Expand Down Expand Up @@ -895,7 +895,7 @@ fn fuzzTestMin(rng: std.Random, comptime queue_size: usize) !void {
}

test "fuzz testing max" {
var prng = std.Random.DefaultPrng.init(0x87654321);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();

const test_case_count = 100;
Expand Down Expand Up @@ -924,7 +924,7 @@ fn fuzzTestMax(rng: std.Random, queue_size: usize) !void {
}

test "fuzz testing min and max" {
var prng = std.Random.DefaultPrng.init(0x87654321);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();

const test_case_count = 100;
Expand Down
4 changes: 2 additions & 2 deletions lib/std/sort.zig
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ test "stable sort" {
}

test "stable sort fuzz testing" {
var prng = std.Random.DefaultPrng.init(0x12345678);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();
const test_case_count = 10;

Expand Down Expand Up @@ -408,7 +408,7 @@ test "sort with context in the middle of a slice" {
}

test "sort fuzz testing" {
var prng = std.Random.DefaultPrng.init(0x12345678);
var prng = std.Random.DefaultPrng.init(std.testing.random_seed);
const random = prng.random();
const test_case_count = 10;

Expand Down
6 changes: 5 additions & 1 deletion lib/std/testing.zig
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const std = @import("std.zig");
const builtin = @import("builtin");

const assert = std.debug.assert;
const math = std.math;

/// Provides deterministic randomness in unit tests.
/// Initialized on startup. Read-only after that.
pub var random_seed: u32 = 0;

pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator;

/// This should only be used in temporary test programs.
Expand Down
5 changes: 4 additions & 1 deletion src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4261,7 +4261,10 @@ fn runOrTest(
defer argv.deinit();

if (test_exec_args.len == 0) {
try argv.append(exe_path);
try argv.appendSlice(&.{
exe_path,
try std.fmt.allocPrint(arena, "--seed=0x{x}", .{std.crypto.random.int(u32)}),
});
} else {
for (test_exec_args) |arg| {
try argv.append(arg orelse exe_path);
Expand Down

0 comments on commit 72695a5

Please sign in to comment.