aboutsummaryrefslogtreecommitdiff
path: root/lib/std/testing.zig
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2020-05-02 01:53:24 -0400
committerAndrew Kelley <andrew@ziglang.org>2020-05-02 01:53:24 -0400
commitbeebcbb677350b86c9f36a850a8b8853b7a71769 (patch)
treecc9c09cecdc0c702d1f1f34ae87967dcc98c4089 /lib/std/testing.zig
parent43f7856bac5fd1a93970d7cebd20289de2367691 (diff)
parent3dbe02e2d8cda1fe0072e71ae7630170c270419e (diff)
downloadzig-beebcbb677350b86c9f36a850a8b8853b7a71769.tar.gz
zig-beebcbb677350b86c9f36a850a8b8853b7a71769.zip
Merge remote-tracking branch 'origin/master' into FireFox317-windows-evented-io
Diffstat (limited to 'lib/std/testing.zig')
-rw-r--r--lib/std/testing.zig40
1 files changed, 39 insertions, 1 deletions
diff --git a/lib/std/testing.zig b/lib/std/testing.zig
index fed2b15bf5..0f6cefb787 100644
--- a/lib/std/testing.zig
+++ b/lib/std/testing.zig
@@ -12,7 +12,7 @@ pub const failing_allocator = &failing_allocator_instance.allocator;
pub var failing_allocator_instance = FailingAllocator.init(&base_allocator_instance.allocator, 0);
pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
-var allocator_mem: [1024 * 1024]u8 = undefined;
+var allocator_mem: [2 * 1024 * 1024]u8 = undefined;
/// This function is intended to be used only in tests. It prints diagnostics to stderr
/// and then aborts when actual_error_union is not expected_error.
@@ -193,6 +193,44 @@ pub fn expect(ok: bool) void {
if (!ok) @panic("test failure");
}
+pub const TmpDir = struct {
+ dir: std.fs.Dir,
+ parent_dir: std.fs.Dir,
+ sub_path: [sub_path_len]u8,
+
+ const random_bytes_count = 12;
+ const sub_path_len = std.base64.Base64Encoder.calcSize(random_bytes_count);
+
+ pub fn cleanup(self: *TmpDir) void {
+ self.dir.close();
+ self.parent_dir.deleteTree(&self.sub_path) catch {};
+ self.parent_dir.close();
+ self.* = undefined;
+ }
+};
+
+pub fn tmpDir(opts: std.fs.Dir.OpenDirOptions) TmpDir {
+ var random_bytes: [TmpDir.random_bytes_count]u8 = undefined;
+ std.crypto.randomBytes(&random_bytes) catch
+ @panic("unable to make tmp dir for testing: unable to get random bytes");
+ var sub_path: [TmpDir.sub_path_len]u8 = undefined;
+ std.fs.base64_encoder.encode(&sub_path, &random_bytes);
+
+ var cache_dir = std.fs.cwd().makeOpenPath("zig-cache", .{}) catch
+ @panic("unable to make tmp dir for testing: unable to make and open zig-cache dir");
+ defer cache_dir.close();
+ var parent_dir = cache_dir.makeOpenPath("tmp", .{}) catch
+ @panic("unable to make tmp dir for testing: unable to make and open zig-cache/tmp dir");
+ var dir = parent_dir.makeOpenPath(&sub_path, opts) catch
+ @panic("unable to make tmp dir for testing: unable to make and open the tmp dir");
+
+ return .{
+ .dir = dir,
+ .parent_dir = parent_dir,
+ .sub_path = sub_path,
+ };
+}
+
test "expectEqual nested array" {
const a = [2][2]f32{
[_]f32{ 1.0, 0.0 },