2019-03-02 13:46:04 -08:00
|
|
|
const std = @import("std.zig");
|
2020-04-29 04:40:52 -07:00
|
|
|
const warn = std.debug.warn;
|
2019-02-08 15:18:47 -08:00
|
|
|
|
2020-01-29 19:22:01 -08:00
|
|
|
pub const LeakCountAllocator = @import("testing/leak_count_allocator.zig").LeakCountAllocator;
|
|
|
|
pub const FailingAllocator = @import("testing/failing_allocator.zig").FailingAllocator;
|
|
|
|
|
2020-01-29 10:21:29 -08:00
|
|
|
/// This should only be used in temporary test programs.
|
|
|
|
pub const allocator = &allocator_instance.allocator;
|
2020-01-29 19:22:01 -08:00
|
|
|
pub var allocator_instance = LeakCountAllocator.init(&base_allocator_instance.allocator);
|
2020-01-29 10:21:29 -08:00
|
|
|
|
2020-04-07 05:24:49 -07:00
|
|
|
pub const failing_allocator = &failing_allocator_instance.allocator;
|
|
|
|
pub var failing_allocator_instance = FailingAllocator.init(&base_allocator_instance.allocator, 0);
|
2020-01-29 11:18:04 -08:00
|
|
|
|
2020-01-29 19:22:01 -08:00
|
|
|
pub var base_allocator_instance = std.heap.ThreadSafeFixedBufferAllocator.init(allocator_mem[0..]);
|
2020-05-01 03:15:58 -07:00
|
|
|
var allocator_mem: [2 * 1024 * 1024]u8 = undefined;
|
2020-01-29 11:18:04 -08:00
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
/// 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.
|
|
|
|
pub fn expectError(expected_error: anyerror, actual_error_union: var) void {
|
|
|
|
if (actual_error_union) |actual_payload| {
|
2019-12-09 13:21:16 -08:00
|
|
|
std.debug.panic("expected error.{}, found {}", .{ @errorName(expected_error), actual_payload });
|
2019-02-08 15:18:47 -08:00
|
|
|
} else |actual_error| {
|
|
|
|
if (expected_error != actual_error) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected error.{}, found error.{}", .{
|
|
|
|
@errorName(expected_error),
|
|
|
|
@errorName(actual_error),
|
|
|
|
});
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is intended to be used only in tests. When the two values are not
|
|
|
|
/// equal, prints diagnostics to stderr to show exactly how they are not equal,
|
|
|
|
/// then aborts.
|
|
|
|
/// The types must match exactly.
|
2019-12-09 12:56:19 -08:00
|
|
|
pub fn expectEqual(expected: var, actual: @TypeOf(expected)) void {
|
|
|
|
switch (@typeInfo(@TypeOf(actual))) {
|
2019-07-21 16:56:37 -07:00
|
|
|
.NoReturn,
|
|
|
|
.BoundFn,
|
|
|
|
.Opaque,
|
|
|
|
.Frame,
|
2019-07-26 16:52:35 -07:00
|
|
|
.AnyFrame,
|
2019-12-09 12:56:19 -08:00
|
|
|
=> @compileError("value of type " ++ @typeName(@TypeOf(actual)) ++ " encountered"),
|
2019-02-08 15:18:47 -08:00
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Undefined,
|
|
|
|
.Null,
|
|
|
|
.Void,
|
2019-02-08 15:18:47 -08:00
|
|
|
=> return,
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Type,
|
|
|
|
.Bool,
|
|
|
|
.Int,
|
|
|
|
.Float,
|
|
|
|
.ComptimeFloat,
|
|
|
|
.ComptimeInt,
|
|
|
|
.EnumLiteral,
|
|
|
|
.Enum,
|
|
|
|
.Fn,
|
|
|
|
.ErrorSet,
|
2019-02-08 15:18:47 -08:00
|
|
|
=> {
|
|
|
|
if (actual != expected) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected {}, found {}", .{ expected, actual });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Pointer => |pointer| {
|
2019-02-08 15:18:47 -08:00
|
|
|
switch (pointer.size) {
|
2020-02-25 12:29:56 -08:00
|
|
|
.One, .Many, .C => {
|
2019-02-08 15:18:47 -08:00
|
|
|
if (actual != expected) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected {*}, found {*}", .{ expected, actual });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
},
|
2020-02-25 22:18:23 -08:00
|
|
|
.Slice => {
|
2019-02-08 15:18:47 -08:00
|
|
|
if (actual.ptr != expected.ptr) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected slice ptr {}, found {}", .{ expected.ptr, actual.ptr });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
if (actual.len != expected.len) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected slice len {}, found {}", .{ expected.len, actual.len });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Array => |array| expectEqualSlices(array.child, &expected, &actual),
|
2019-02-08 15:18:47 -08:00
|
|
|
|
2020-01-07 06:14:37 -08:00
|
|
|
.Vector => |vectorType| {
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < vectorType.len) : (i += 1) {
|
|
|
|
if (!std.meta.eql(expected[i], actual[i])) {
|
|
|
|
std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Struct => |structType| {
|
2019-05-20 07:29:46 -07:00
|
|
|
inline for (structType.fields) |field| {
|
|
|
|
expectEqual(@field(expected, field.name), @field(actual, field.name));
|
|
|
|
}
|
2019-02-08 15:18:47 -08:00
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Union => |union_info| {
|
2019-02-08 15:18:47 -08:00
|
|
|
if (union_info.tag_type == null) {
|
|
|
|
@compileError("Unable to compare untagged union values");
|
|
|
|
}
|
2019-11-27 13:35:32 -08:00
|
|
|
|
2019-12-09 12:56:19 -08:00
|
|
|
const TagType = @TagType(@TypeOf(expected));
|
2019-11-27 13:35:32 -08:00
|
|
|
|
|
|
|
const expectedTag = @as(TagType, expected);
|
|
|
|
const actualTag = @as(TagType, actual);
|
|
|
|
|
|
|
|
expectEqual(expectedTag, actualTag);
|
|
|
|
|
|
|
|
// we only reach this loop if the tags are equal
|
2019-12-09 12:56:19 -08:00
|
|
|
inline for (std.meta.fields(@TypeOf(actual))) |fld| {
|
2019-11-27 13:35:32 -08:00
|
|
|
if (std.mem.eql(u8, fld.name, @tagName(actualTag))) {
|
|
|
|
expectEqual(@field(expected, fld.name), @field(actual, fld.name));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we iterate over *all* union fields
|
2019-12-08 19:53:51 -08:00
|
|
|
// => we should never get here as the loop above is
|
2019-11-27 13:35:32 -08:00
|
|
|
// including all possible values.
|
|
|
|
unreachable;
|
2019-02-08 15:18:47 -08:00
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.Optional => {
|
2019-02-08 15:18:47 -08:00
|
|
|
if (expected) |expected_payload| {
|
|
|
|
if (actual) |actual_payload| {
|
|
|
|
expectEqual(expected_payload, actual_payload);
|
|
|
|
} else {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected {}, found null", .{expected_payload});
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (actual) |actual_payload| {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected null, found {}", .{actual_payload});
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2019-07-21 16:56:37 -07:00
|
|
|
.ErrorUnion => {
|
2019-02-08 15:18:47 -08:00
|
|
|
if (expected) |expected_payload| {
|
|
|
|
if (actual) |actual_payload| {
|
|
|
|
expectEqual(expected_payload, actual_payload);
|
|
|
|
} else |actual_err| {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected {}, found {}", .{ expected_payload, actual_err });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
} else |expected_err| {
|
|
|
|
if (actual) |actual_payload| {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("expected {}, found {}", .{ expected_err, actual_payload });
|
2019-02-08 15:18:47 -08:00
|
|
|
} else |actual_err| {
|
|
|
|
expectEqual(expected_err, actual_err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-08 19:53:51 -08:00
|
|
|
test "expectEqual.union(enum)" {
|
2019-11-27 13:35:32 -08:00
|
|
|
const T = union(enum) {
|
|
|
|
a: i32,
|
|
|
|
b: f32,
|
|
|
|
};
|
|
|
|
|
2019-12-08 19:53:51 -08:00
|
|
|
const a10 = T{ .a = 10 };
|
|
|
|
const a20 = T{ .a = 20 };
|
2019-11-27 13:35:32 -08:00
|
|
|
|
|
|
|
expectEqual(a10, a10);
|
|
|
|
}
|
|
|
|
|
2019-02-08 15:18:47 -08:00
|
|
|
/// This function is intended to be used only in tests. When the two slices are not
|
|
|
|
/// equal, prints diagnostics to stderr to show exactly how they are not equal,
|
|
|
|
/// then aborts.
|
|
|
|
pub fn expectEqualSlices(comptime T: type, expected: []const T, actual: []const T) void {
|
|
|
|
// TODO better printing of the difference
|
|
|
|
// If the arrays are small enough we could print the whole thing
|
|
|
|
// If the child type is u8 and no weird bytes, we could print it as strings
|
|
|
|
// Even for the length difference, it would be useful to see the values of the slices probably.
|
|
|
|
if (expected.len != actual.len) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("slice lengths differ. expected {}, found {}", .{ expected.len, actual.len });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
var i: usize = 0;
|
|
|
|
while (i < expected.len) : (i += 1) {
|
2019-12-21 17:37:25 -08:00
|
|
|
if (!std.meta.eql(expected[i], actual[i])) {
|
2019-12-08 19:53:51 -08:00
|
|
|
std.debug.panic("index {} incorrect. expected {}, found {}", .{ i, expected[i], actual[i] });
|
2019-02-08 15:18:47 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// This function is intended to be used only in tests. When `ok` is false, the test fails.
|
|
|
|
/// A message is printed to stderr and then abort is called.
|
|
|
|
pub fn expect(ok: bool) void {
|
|
|
|
if (!ok) @panic("test failure");
|
|
|
|
}
|
2019-12-21 17:37:25 -08:00
|
|
|
|
2020-04-27 15:26:59 -07:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-18 08:10:02 -07:00
|
|
|
fn getCwdOrWasiPreopen() std.fs.Dir {
|
|
|
|
if (@import("builtin").os.tag == .wasi) {
|
|
|
|
var preopens = std.fs.wasi.PreopenList.init(allocator);
|
|
|
|
defer preopens.deinit();
|
|
|
|
preopens.populate() catch
|
|
|
|
@panic("unable to make tmp dir for testing: unable to populate preopens");
|
|
|
|
const preopen = preopens.find(".") orelse
|
|
|
|
@panic("unable to make tmp dir for testing: didn't find '.' in the preopens");
|
|
|
|
|
|
|
|
return std.fs.Dir{ .fd = preopen.fd };
|
|
|
|
} else {
|
|
|
|
return std.fs.cwd();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 15:26:59 -07:00
|
|
|
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);
|
|
|
|
|
2020-05-18 08:10:02 -07:00
|
|
|
var cwd = getCwdOrWasiPreopen();
|
|
|
|
var cache_dir = cwd.makeOpenPath("zig-cache", .{}) catch
|
2020-04-27 15:26:59 -07:00
|
|
|
@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,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2019-12-21 17:37:25 -08:00
|
|
|
test "expectEqual nested array" {
|
|
|
|
const a = [2][2]f32{
|
|
|
|
[_]f32{ 1.0, 0.0 },
|
|
|
|
[_]f32{ 0.0, 1.0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
const b = [2][2]f32{
|
|
|
|
[_]f32{ 1.0, 0.0 },
|
|
|
|
[_]f32{ 0.0, 1.0 },
|
|
|
|
};
|
|
|
|
|
|
|
|
expectEqual(a, b);
|
|
|
|
}
|
2020-01-07 06:14:37 -08:00
|
|
|
|
|
|
|
test "expectEqual vector" {
|
|
|
|
var a = @splat(4, @as(u32, 4));
|
|
|
|
var b = @splat(4, @as(u32, 4));
|
|
|
|
|
|
|
|
expectEqual(a, b);
|
|
|
|
}
|
2020-04-29 04:40:52 -07:00
|
|
|
|
|
|
|
pub fn expectEqualStrings(expected: []const u8, actual: []const u8) void {
|
|
|
|
if (std.mem.indexOfDiff(u8, actual, expected)) |diff_index| {
|
|
|
|
warn("\n====== expected this output: =========\n", .{});
|
|
|
|
printWithVisibleNewlines(expected);
|
|
|
|
warn("\n======== instead found this: =========\n", .{});
|
|
|
|
printWithVisibleNewlines(actual);
|
|
|
|
warn("\n======================================\n", .{});
|
|
|
|
|
|
|
|
var diff_line_number: usize = 1;
|
|
|
|
for (expected[0..diff_index]) |value| {
|
|
|
|
if (value == '\n') diff_line_number += 1;
|
|
|
|
}
|
|
|
|
warn("First difference occurs on line {}:\n", .{diff_line_number});
|
|
|
|
|
|
|
|
warn("expected:\n", .{});
|
|
|
|
printIndicatorLine(expected, diff_index);
|
|
|
|
|
|
|
|
warn("found:\n", .{});
|
|
|
|
printIndicatorLine(actual, diff_index);
|
|
|
|
|
|
|
|
@panic("test failure");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn printIndicatorLine(source: []const u8, indicator_index: usize) void {
|
|
|
|
const line_begin_index = if (std.mem.lastIndexOfScalar(u8, source[0..indicator_index], '\n')) |line_begin|
|
|
|
|
line_begin + 1
|
|
|
|
else
|
|
|
|
0;
|
|
|
|
const line_end_index = if (std.mem.indexOfScalar(u8, source[indicator_index..], '\n')) |line_end|
|
|
|
|
(indicator_index + line_end)
|
|
|
|
else
|
|
|
|
source.len;
|
|
|
|
|
|
|
|
printLine(source[line_begin_index..line_end_index]);
|
|
|
|
{
|
|
|
|
var i: usize = line_begin_index;
|
|
|
|
while (i < indicator_index) : (i += 1)
|
|
|
|
warn(" ", .{});
|
|
|
|
}
|
|
|
|
warn("^\n", .{});
|
|
|
|
}
|
|
|
|
|
|
|
|
fn printWithVisibleNewlines(source: []const u8) void {
|
|
|
|
var i: usize = 0;
|
|
|
|
while (std.mem.indexOf(u8, source[i..], "\n")) |nl| : (i += nl + 1) {
|
|
|
|
printLine(source[i .. i + nl]);
|
|
|
|
}
|
|
|
|
warn("{}␃\n", .{source[i..]}); // End of Text symbol (ETX)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn printLine(line: []const u8) void {
|
2020-05-21 21:27:51 -07:00
|
|
|
if (line.len != 0) switch (line[line.len - 1]) {
|
2020-04-29 04:40:52 -07:00
|
|
|
' ', '\t' => warn("{}⏎\n", .{line}), // Carriage return symbol,
|
2020-05-21 21:27:51 -07:00
|
|
|
else => {},
|
|
|
|
};
|
|
|
|
warn("{}\n", .{line});
|
2020-04-29 04:40:52 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
test "" {
|
|
|
|
expectEqualStrings("foo", "foo");
|
|
|
|
}
|