From 9b8e23934bc87f1fd6a42cdfdd551212994b6e58 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 1 Feb 2019 17:49:29 -0500 Subject: introduce --single-threaded build option closes #1764 This adds another boolean to the test matrix; hopefully it does not inflate the time too much. std.event.Loop does not work with this option yet. See #1908 --- std/atomic/stack.zig | 82 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 56 insertions(+), 26 deletions(-) (limited to 'std/atomic/stack.zig') diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index b69a93733c..1e4981353b 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -4,10 +4,13 @@ const AtomicOrder = builtin.AtomicOrder; /// Many reader, many writer, non-allocating, thread-safe /// Uses a spinlock to protect push() and pop() +/// When building in single threaded mode, this is a simple linked list. pub fn Stack(comptime T: type) type { return struct { root: ?*Node, - lock: u8, + lock: @typeOf(lock_init), + + const lock_init = if (builtin.single_threaded) {} else u8(0); pub const Self = @This(); @@ -19,7 +22,7 @@ pub fn Stack(comptime T: type) type { pub fn init() Self { return Self{ .root = null, - .lock = 0, + .lock = lock_init, }; } @@ -31,20 +34,31 @@ pub fn Stack(comptime T: type) type { } pub fn push(self: *Self, node: *Node) void { - while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {} - defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1); - - node.next = self.root; - self.root = node; + if (builtin.single_threaded) { + node.next = self.root; + self.root = node; + } else { + while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {} + defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1); + + node.next = self.root; + self.root = node; + } } pub fn pop(self: *Self) ?*Node { - while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {} - defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1); - - const root = self.root orelse return null; - self.root = root.next; - return root; + if (builtin.single_threaded) { + const root = self.root orelse return null; + self.root = root.next; + return root; + } else { + while (@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst) != 0) {} + defer assert(@atomicRmw(u8, &self.lock, builtin.AtomicRmwOp.Xchg, 0, AtomicOrder.SeqCst) == 1); + + const root = self.root orelse return null; + self.root = root.next; + return root; + } } pub fn isEmpty(self: *Self) bool { @@ -90,20 +104,36 @@ test "std.atomic.stack" { .get_count = 0, }; - var putters: [put_thread_count]*std.os.Thread = undefined; - for (putters) |*t| { - t.* = try std.os.spawnThread(&context, startPuts); - } - var getters: [put_thread_count]*std.os.Thread = undefined; - for (getters) |*t| { - t.* = try std.os.spawnThread(&context, startGets); - } + if (builtin.single_threaded) { + { + var i: usize = 0; + while (i < put_thread_count) : (i += 1) { + std.debug.assertOrPanic(startPuts(&context) == 0); + } + } + context.puts_done = 1; + { + var i: usize = 0; + while (i < put_thread_count) : (i += 1) { + std.debug.assertOrPanic(startGets(&context) == 0); + } + } + } else { + var putters: [put_thread_count]*std.os.Thread = undefined; + for (putters) |*t| { + t.* = try std.os.spawnThread(&context, startPuts); + } + var getters: [put_thread_count]*std.os.Thread = undefined; + for (getters) |*t| { + t.* = try std.os.spawnThread(&context, startGets); + } - for (putters) |t| - t.wait(); - _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst); - for (getters) |t| - t.wait(); + for (putters) |t| + t.wait(); + _ = @atomicRmw(u8, &context.puts_done, builtin.AtomicRmwOp.Xchg, 1, AtomicOrder.SeqCst); + for (getters) |t| + t.wait(); + } if (context.put_sum != context.get_sum) { std.debug.panic("failure\nput_sum:{} != get_sum:{}", context.put_sum, context.get_sum); -- cgit v1.2.3 From dfbc063f79dc1358208216b466c1bf8c44baa430 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Sun, 3 Feb 2019 16:13:28 -0500 Subject: `std.mem.Allocator.create` replaced with better API `std.mem.Allocator.createOne` is renamed to `std.mem.Allocator.create`. The problem with the previous API is that even after copy elision, the initalization value passed as a parameter would always be a copy. With the new API, once copy elision is done, initialization functions can directly initialize allocated memory in place. Related: * #1872 * #1873 --- src-self-hosted/compilation.zig | 72 ++++---- src-self-hosted/errmsg.zig | 20 ++- src-self-hosted/ir.zig | 15 +- src-self-hosted/main.zig | 5 +- src-self-hosted/package.zig | 6 +- src-self-hosted/scope.zig | 18 +- src-self-hosted/type.zig | 17 +- src-self-hosted/value.zig | 35 ++-- std/atomic/queue.zig | 5 +- std/atomic/stack.zig | 5 +- std/build.zig | 56 +++--- std/debug/index.zig | 7 +- std/event/channel.zig | 5 +- std/event/fs.zig | 8 +- std/event/group.zig | 5 +- std/heap.zig | 3 +- std/io.zig | 5 +- std/linked_list.zig | 2 +- std/mem.zig | 13 +- std/os/child_process.zig | 5 +- std/os/index.zig | 5 +- std/zig/parse.zig | 376 ++++++++++++++++++++++++---------------- test/tests.zig | 75 ++++---- 23 files changed, 451 insertions(+), 312 deletions(-) (limited to 'std/atomic/stack.zig') diff --git a/src-self-hosted/compilation.zig b/src-self-hosted/compilation.zig index c00c7c1d41..d60892432e 100644 --- a/src-self-hosted/compilation.zig +++ b/src-self-hosted/compilation.zig @@ -83,10 +83,11 @@ pub const ZigCompiler = struct { const context_ref = c.LLVMContextCreate() orelse return error.OutOfMemory; errdefer c.LLVMContextDispose(context_ref); - const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node{ + const node = try self.loop.allocator.create(std.atomic.Stack(llvm.ContextRef).Node); + node.* = std.atomic.Stack(llvm.ContextRef).Node{ .next = undefined, .data = context_ref, - }); + }; errdefer self.loop.allocator.destroy(node); return LlvmHandle{ .node = node }; @@ -596,7 +597,8 @@ pub const Compilation = struct { } fn initTypes(comp: *Compilation) !void { - comp.meta_type = try comp.arena().create(Type.MetaType{ + comp.meta_type = try comp.arena().create(Type.MetaType); + comp.meta_type.* = Type.MetaType{ .base = Type{ .name = "type", .base = Value{ @@ -608,12 +610,13 @@ pub const Compilation = struct { .abi_alignment = Type.AbiAlignment.init(comp.loop), }, .value = undefined, - }); + }; comp.meta_type.value = &comp.meta_type.base; comp.meta_type.base.base.typ = &comp.meta_type.base; assert((try comp.primitive_type_table.put(comp.meta_type.base.name, &comp.meta_type.base)) == null); - comp.void_type = try comp.arena().create(Type.Void{ + comp.void_type = try comp.arena().create(Type.Void); + comp.void_type.* = Type.Void{ .base = Type{ .name = "void", .base = Value{ @@ -624,10 +627,11 @@ pub const Compilation = struct { .id = builtin.TypeId.Void, .abi_alignment = Type.AbiAlignment.init(comp.loop), }, - }); + }; assert((try comp.primitive_type_table.put(comp.void_type.base.name, &comp.void_type.base)) == null); - comp.noreturn_type = try comp.arena().create(Type.NoReturn{ + comp.noreturn_type = try comp.arena().create(Type.NoReturn); + comp.noreturn_type.* = Type.NoReturn{ .base = Type{ .name = "noreturn", .base = Value{ @@ -638,10 +642,11 @@ pub const Compilation = struct { .id = builtin.TypeId.NoReturn, .abi_alignment = Type.AbiAlignment.init(comp.loop), }, - }); + }; assert((try comp.primitive_type_table.put(comp.noreturn_type.base.name, &comp.noreturn_type.base)) == null); - comp.comptime_int_type = try comp.arena().create(Type.ComptimeInt{ + comp.comptime_int_type = try comp.arena().create(Type.ComptimeInt); + comp.comptime_int_type.* = Type.ComptimeInt{ .base = Type{ .name = "comptime_int", .base = Value{ @@ -652,10 +657,11 @@ pub const Compilation = struct { .id = builtin.TypeId.ComptimeInt, .abi_alignment = Type.AbiAlignment.init(comp.loop), }, - }); + }; assert((try comp.primitive_type_table.put(comp.comptime_int_type.base.name, &comp.comptime_int_type.base)) == null); - comp.bool_type = try comp.arena().create(Type.Bool{ + comp.bool_type = try comp.arena().create(Type.Bool); + comp.bool_type.* = Type.Bool{ .base = Type{ .name = "bool", .base = Value{ @@ -666,45 +672,50 @@ pub const Compilation = struct { .id = builtin.TypeId.Bool, .abi_alignment = Type.AbiAlignment.init(comp.loop), }, - }); + }; assert((try comp.primitive_type_table.put(comp.bool_type.base.name, &comp.bool_type.base)) == null); - comp.void_value = try comp.arena().create(Value.Void{ + comp.void_value = try comp.arena().create(Value.Void); + comp.void_value.* = Value.Void{ .base = Value{ .id = Value.Id.Void, .typ = &Type.Void.get(comp).base, .ref_count = std.atomic.Int(usize).init(1), }, - }); + }; - comp.true_value = try comp.arena().create(Value.Bool{ + comp.true_value = try comp.arena().create(Value.Bool); + comp.true_value.* = Value.Bool{ .base = Value{ .id = Value.Id.Bool, .typ = &Type.Bool.get(comp).base, .ref_count = std.atomic.Int(usize).init(1), }, .x = true, - }); + }; - comp.false_value = try comp.arena().create(Value.Bool{ + comp.false_value = try comp.arena().create(Value.Bool); + comp.false_value.* = Value.Bool{ .base = Value{ .id = Value.Id.Bool, .typ = &Type.Bool.get(comp).base, .ref_count = std.atomic.Int(usize).init(1), }, .x = false, - }); + }; - comp.noreturn_value = try comp.arena().create(Value.NoReturn{ + comp.noreturn_value = try comp.arena().create(Value.NoReturn); + comp.noreturn_value.* = Value.NoReturn{ .base = Value{ .id = Value.Id.NoReturn, .typ = &Type.NoReturn.get(comp).base, .ref_count = std.atomic.Int(usize).init(1), }, - }); + }; for (CInt.list) |cint, i| { - const c_int_type = try comp.arena().create(Type.Int{ + const c_int_type = try comp.arena().create(Type.Int); + c_int_type.* = Type.Int{ .base = Type{ .name = cint.zig_name, .base = Value{ @@ -720,11 +731,12 @@ pub const Compilation = struct { .bit_count = comp.target.cIntTypeSizeInBits(cint.id), }, .garbage_node = undefined, - }); + }; comp.c_int_types[i] = c_int_type; assert((try comp.primitive_type_table.put(cint.zig_name, &c_int_type.base)) == null); } - comp.u8_type = try comp.arena().create(Type.Int{ + comp.u8_type = try comp.arena().create(Type.Int); + comp.u8_type.* = Type.Int{ .base = Type{ .name = "u8", .base = Value{ @@ -740,7 +752,7 @@ pub const Compilation = struct { .bit_count = 8, }, .garbage_node = undefined, - }); + }; assert((try comp.primitive_type_table.put(comp.u8_type.base.name, &comp.u8_type.base)) == null); } @@ -829,7 +841,7 @@ pub const Compilation = struct { }; errdefer self.gpa().free(source_code); - const tree = try self.gpa().createOne(ast.Tree); + const tree = try self.gpa().create(ast.Tree); tree.* = try std.zig.parse(self.gpa(), source_code); errdefer { tree.deinit(); @@ -925,7 +937,8 @@ pub const Compilation = struct { } } else { // add new decl - const fn_decl = try self.gpa().create(Decl.Fn{ + const fn_decl = try self.gpa().create(Decl.Fn); + fn_decl.* = Decl.Fn{ .base = Decl{ .id = Decl.Id.Fn, .name = name, @@ -936,7 +949,7 @@ pub const Compilation = struct { }, .value = Decl.Fn.Val{ .Unresolved = {} }, .fn_proto = fn_proto, - }); + }; tree_scope.base.ref(); errdefer self.gpa().destroy(fn_decl); @@ -1140,12 +1153,13 @@ pub const Compilation = struct { } } - const link_lib = try self.gpa().create(LinkLib{ + const link_lib = try self.gpa().create(LinkLib); + link_lib.* = LinkLib{ .name = name, .path = null, .provided_explicitly = provided_explicitly, .symbols = ArrayList([]u8).init(self.gpa()), - }); + }; try self.link_libs_list.append(link_lib); if (is_libc) { self.libc_link_lib = link_lib; diff --git a/src-self-hosted/errmsg.zig b/src-self-hosted/errmsg.zig index 0e552fde7e..fc49fad410 100644 --- a/src-self-hosted/errmsg.zig +++ b/src-self-hosted/errmsg.zig @@ -118,7 +118,8 @@ pub const Msg = struct { const realpath = try mem.dupe(comp.gpa(), u8, tree_scope.root().realpath); errdefer comp.gpa().free(realpath); - const msg = try comp.gpa().create(Msg{ + const msg = try comp.gpa().create(Msg); + msg.* = Msg{ .text = text, .realpath = realpath, .data = Data{ @@ -128,7 +129,7 @@ pub const Msg = struct { .span = span, }, }, - }); + }; tree_scope.base.ref(); return msg; } @@ -139,13 +140,14 @@ pub const Msg = struct { const realpath_copy = try mem.dupe(comp.gpa(), u8, realpath); errdefer comp.gpa().free(realpath_copy); - const msg = try comp.gpa().create(Msg{ + const msg = try comp.gpa().create(Msg); + msg.* = Msg{ .text = text, .realpath = realpath_copy, .data = Data{ .Cli = Cli{ .allocator = comp.gpa() }, }, - }); + }; return msg; } @@ -164,7 +166,8 @@ pub const Msg = struct { var out_stream = &std.io.BufferOutStream.init(&text_buf).stream; try parse_error.render(&tree_scope.tree.tokens, out_stream); - const msg = try comp.gpa().create(Msg{ + const msg = try comp.gpa().create(Msg); + msg.* = Msg{ .text = undefined, .realpath = realpath_copy, .data = Data{ @@ -177,7 +180,7 @@ pub const Msg = struct { }, }, }, - }); + }; tree_scope.base.ref(); msg.text = text_buf.toOwnedSlice(); return msg; @@ -203,7 +206,8 @@ pub const Msg = struct { var out_stream = &std.io.BufferOutStream.init(&text_buf).stream; try parse_error.render(&tree.tokens, out_stream); - const msg = try allocator.create(Msg{ + const msg = try allocator.create(Msg); + msg.* = Msg{ .text = undefined, .realpath = realpath_copy, .data = Data{ @@ -216,7 +220,7 @@ pub const Msg = struct { }, }, }, - }); + }; msg.text = text_buf.toOwnedSlice(); errdefer allocator.destroy(msg); diff --git a/src-self-hosted/ir.zig b/src-self-hosted/ir.zig index 562765b354..0362bb4ef8 100644 --- a/src-self-hosted/ir.zig +++ b/src-self-hosted/ir.zig @@ -1021,12 +1021,13 @@ pub const Builder = struct { pub const Error = Analyze.Error; pub fn init(comp: *Compilation, tree_scope: *Scope.AstTree, begin_scope: ?*Scope) !Builder { - const code = try comp.gpa().create(Code{ + const code = try comp.gpa().create(Code); + code.* = Code{ .basic_block_list = undefined, .arena = std.heap.ArenaAllocator.init(comp.gpa()), .return_type = null, .tree_scope = tree_scope, - }); + }; code.basic_block_list = std.ArrayList(*BasicBlock).init(&code.arena.allocator); errdefer code.destroy(comp.gpa()); @@ -1052,7 +1053,8 @@ pub const Builder = struct { /// No need to clean up resources thanks to the arena allocator. pub fn createBasicBlock(self: *Builder, scope: *Scope, name_hint: [*]const u8) !*BasicBlock { - const basic_block = try self.arena().create(BasicBlock{ + const basic_block = try self.arena().create(BasicBlock); + basic_block.* = BasicBlock{ .ref_count = 0, .name_hint = name_hint, .debug_id = self.next_debug_id, @@ -1063,7 +1065,7 @@ pub const Builder = struct { .ref_instruction = null, .llvm_block = undefined, .llvm_exit_block = undefined, - }); + }; self.next_debug_id += 1; return basic_block; } @@ -1774,7 +1776,8 @@ pub const Builder = struct { params: I.Params, is_generated: bool, ) !*Inst { - const inst = try self.arena().create(I{ + const inst = try self.arena().create(I); + inst.* = I{ .base = Inst{ .id = Inst.typeToId(I), .is_generated = is_generated, @@ -1793,7 +1796,7 @@ pub const Builder = struct { .owner_bb = self.current_basic_block, }, .params = params, - }); + }; // Look at the params and ref() other instructions comptime var i = 0; diff --git a/src-self-hosted/main.zig b/src-self-hosted/main.zig index 0742cbfe65..1403ab860d 100644 --- a/src-self-hosted/main.zig +++ b/src-self-hosted/main.zig @@ -944,12 +944,13 @@ const CliPkg = struct { parent: ?*CliPkg, pub fn init(allocator: *mem.Allocator, name: []const u8, path: []const u8, parent: ?*CliPkg) !*CliPkg { - var pkg = try allocator.create(CliPkg{ + var pkg = try allocator.create(CliPkg); + pkg.* = CliPkg{ .name = name, .path = path, .children = ArrayList(*CliPkg).init(allocator), .parent = parent, - }); + }; return pkg; } diff --git a/src-self-hosted/package.zig b/src-self-hosted/package.zig index 720b279651..0d31731b55 100644 --- a/src-self-hosted/package.zig +++ b/src-self-hosted/package.zig @@ -15,11 +15,13 @@ pub const Package = struct { /// makes internal copies of root_src_dir and root_src_path /// allocator should be an arena allocator because Package never frees anything pub fn create(allocator: *mem.Allocator, root_src_dir: []const u8, root_src_path: []const u8) !*Package { - return allocator.create(Package{ + const ptr = try allocator.create(Package); + ptr.* = Package{ .root_src_dir = try Buffer.init(allocator, root_src_dir), .root_src_path = try Buffer.init(allocator, root_src_path), .table = Table.init(allocator), - }); + }; + return ptr; } pub fn add(self: *Package, name: []const u8, package: *Package) !void { diff --git a/src-self-hosted/scope.zig b/src-self-hosted/scope.zig index 43d3b5a784..b14c073a9e 100644 --- a/src-self-hosted/scope.zig +++ b/src-self-hosted/scope.zig @@ -120,7 +120,7 @@ pub const Scope = struct { /// Creates a Root scope with 1 reference /// Takes ownership of realpath pub fn create(comp: *Compilation, realpath: []u8) !*Root { - const self = try comp.gpa().createOne(Root); + const self = try comp.gpa().create(Root); self.* = Root{ .base = Scope{ .id = Id.Root, @@ -150,7 +150,7 @@ pub const Scope = struct { /// Creates a scope with 1 reference /// Takes ownership of tree, will deinit and destroy when done. pub fn create(comp: *Compilation, tree: *ast.Tree, root_scope: *Root) !*AstTree { - const self = try comp.gpa().createOne(AstTree); + const self = try comp.gpa().create(AstTree); self.* = AstTree{ .base = undefined, .tree = tree, @@ -182,7 +182,7 @@ pub const Scope = struct { /// Creates a Decls scope with 1 reference pub fn create(comp: *Compilation, parent: *Scope) !*Decls { - const self = try comp.gpa().createOne(Decls); + const self = try comp.gpa().create(Decls); self.* = Decls{ .base = undefined, .table = event.RwLocked(Decl.Table).init(comp.loop, Decl.Table.init(comp.gpa())), @@ -235,7 +235,7 @@ pub const Scope = struct { /// Creates a Block scope with 1 reference pub fn create(comp: *Compilation, parent: *Scope) !*Block { - const self = try comp.gpa().createOne(Block); + const self = try comp.gpa().create(Block); self.* = Block{ .base = undefined, .incoming_values = undefined, @@ -262,7 +262,7 @@ pub const Scope = struct { /// Creates a FnDef scope with 1 reference /// Must set the fn_val later pub fn create(comp: *Compilation, parent: *Scope) !*FnDef { - const self = try comp.gpa().createOne(FnDef); + const self = try comp.gpa().create(FnDef); self.* = FnDef{ .base = undefined, .fn_val = null, @@ -281,7 +281,7 @@ pub const Scope = struct { /// Creates a CompTime scope with 1 reference pub fn create(comp: *Compilation, parent: *Scope) !*CompTime { - const self = try comp.gpa().createOne(CompTime); + const self = try comp.gpa().create(CompTime); self.* = CompTime{ .base = undefined }; self.base.init(Id.CompTime, parent); return self; @@ -309,7 +309,7 @@ pub const Scope = struct { kind: Kind, defer_expr_scope: *DeferExpr, ) !*Defer { - const self = try comp.gpa().createOne(Defer); + const self = try comp.gpa().create(Defer); self.* = Defer{ .base = undefined, .defer_expr_scope = defer_expr_scope, @@ -333,7 +333,7 @@ pub const Scope = struct { /// Creates a DeferExpr scope with 1 reference pub fn create(comp: *Compilation, parent: *Scope, expr_node: *ast.Node) !*DeferExpr { - const self = try comp.gpa().createOne(DeferExpr); + const self = try comp.gpa().create(DeferExpr); self.* = DeferExpr{ .base = undefined, .expr_node = expr_node, @@ -398,7 +398,7 @@ pub const Scope = struct { } fn create(comp: *Compilation, parent: *Scope, name: []const u8, src_node: *ast.Node) !*Var { - const self = try comp.gpa().createOne(Var); + const self = try comp.gpa().create(Var); self.* = Var{ .base = undefined, .name = name, diff --git a/src-self-hosted/type.zig b/src-self-hosted/type.zig index fa31343902..8a05594b30 100644 --- a/src-self-hosted/type.zig +++ b/src-self-hosted/type.zig @@ -413,7 +413,7 @@ pub const Type = struct { key.ref(); errdefer key.deref(comp); - const self = try comp.gpa().createOne(Fn); + const self = try comp.gpa().create(Fn); self.* = Fn{ .base = undefined, .key = key, @@ -615,11 +615,12 @@ pub const Type = struct { } } - const self = try comp.gpa().create(Int{ + const self = try comp.gpa().create(Int); + self.* = Int{ .base = undefined, .key = key, .garbage_node = undefined, - }); + }; errdefer comp.gpa().destroy(self); const u_or_i = "ui"[@boolToInt(key.is_signed)]; @@ -781,11 +782,12 @@ pub const Type = struct { } } - const self = try comp.gpa().create(Pointer{ + const self = try comp.gpa().create(Pointer); + self.* = Pointer{ .base = undefined, .key = normal_key, .garbage_node = undefined, - }); + }; errdefer comp.gpa().destroy(self); const size_str = switch (self.key.size) { @@ -879,11 +881,12 @@ pub const Type = struct { } } - const self = try comp.gpa().create(Array{ + const self = try comp.gpa().create(Array); + self.* = Array{ .base = undefined, .key = key, .garbage_node = undefined, - }); + }; errdefer comp.gpa().destroy(self); const name = try std.fmt.allocPrint(comp.gpa(), "[{}]{}", key.len, key.elem_type.name); diff --git a/src-self-hosted/value.zig b/src-self-hosted/value.zig index e6dca4eff7..9431c614b9 100644 --- a/src-self-hosted/value.zig +++ b/src-self-hosted/value.zig @@ -135,14 +135,15 @@ pub const Value = struct { symbol_name: Buffer, pub fn create(comp: *Compilation, fn_type: *Type.Fn, symbol_name: Buffer) !*FnProto { - const self = try comp.gpa().create(FnProto{ + const self = try comp.gpa().create(FnProto); + self.* = FnProto{ .base = Value{ .id = Value.Id.FnProto, .typ = &fn_type.base, .ref_count = std.atomic.Int(usize).init(1), }, .symbol_name = symbol_name, - }); + }; fn_type.base.base.ref(); return self; } @@ -190,14 +191,16 @@ pub const Value = struct { /// Creates a Fn value with 1 ref /// Takes ownership of symbol_name pub fn create(comp: *Compilation, fn_type: *Type.Fn, fndef_scope: *Scope.FnDef, symbol_name: Buffer) !*Fn { - const link_set_node = try comp.gpa().create(Compilation.FnLinkSet.Node{ + const link_set_node = try comp.gpa().create(Compilation.FnLinkSet.Node); + link_set_node.* = Compilation.FnLinkSet.Node{ .data = null, .next = undefined, .prev = undefined, - }); + }; errdefer comp.gpa().destroy(link_set_node); - const self = try comp.gpa().create(Fn{ + const self = try comp.gpa().create(Fn); + self.* = Fn{ .base = Value{ .id = Value.Id.Fn, .typ = &fn_type.base, @@ -209,7 +212,7 @@ pub const Value = struct { .symbol_name = symbol_name, .containing_object = Buffer.initNull(comp.gpa()), .link_set_node = link_set_node, - }); + }; fn_type.base.base.ref(); fndef_scope.fn_val = self; fndef_scope.base.ref(); @@ -353,7 +356,8 @@ pub const Value = struct { var ptr_type_consumed = false; errdefer if (!ptr_type_consumed) ptr_type.base.base.deref(comp); - const self = try comp.gpa().create(Value.Ptr{ + const self = try comp.gpa().create(Value.Ptr); + self.* = Value.Ptr{ .base = Value{ .id = Value.Id.Ptr, .typ = &ptr_type.base, @@ -366,7 +370,7 @@ pub const Value = struct { }, }, .mut = Mut.CompTimeConst, - }); + }; ptr_type_consumed = true; errdefer comp.gpa().destroy(self); @@ -430,14 +434,15 @@ pub const Value = struct { }) catch unreachable); errdefer array_type.base.base.deref(comp); - const self = try comp.gpa().create(Value.Array{ + const self = try comp.gpa().create(Value.Array); + self.* = Value.Array{ .base = Value{ .id = Value.Id.Array, .typ = &array_type.base, .ref_count = std.atomic.Int(usize).init(1), }, .special = Special{ .OwnedBuffer = buffer }, - }); + }; errdefer comp.gpa().destroy(self); return self; @@ -509,14 +514,15 @@ pub const Value = struct { big_int: std.math.big.Int, pub fn createFromString(comp: *Compilation, typ: *Type, base: u8, value: []const u8) !*Int { - const self = try comp.gpa().create(Value.Int{ + const self = try comp.gpa().create(Value.Int); + self.* = Value.Int{ .base = Value{ .id = Value.Id.Int, .typ = typ, .ref_count = std.atomic.Int(usize).init(1), }, .big_int = undefined, - }); + }; typ.base.ref(); errdefer comp.gpa().destroy(self); @@ -557,14 +563,15 @@ pub const Value = struct { old.base.typ.base.ref(); errdefer old.base.typ.base.deref(comp); - const new = try comp.gpa().create(Value.Int{ + const new = try comp.gpa().create(Value.Int); + new.* = Value.Int{ .base = Value{ .id = Value.Id.Int, .typ = old.base.typ, .ref_count = std.atomic.Int(usize).init(1), }, .big_int = undefined, - }); + }; errdefer comp.gpa().destroy(new); new.big_int = try old.big_int.clone(); diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 6c61bcc048..183c434dc5 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -221,11 +221,12 @@ fn startPuts(ctx: *Context) u8 { while (put_count != 0) : (put_count -= 1) { std.os.time.sleep(1); // let the os scheduler be our fuzz const x = @bitCast(i32, r.random.scalar(u32)); - const node = ctx.allocator.create(Queue(i32).Node{ + const node = ctx.allocator.create(Queue(i32).Node) catch unreachable; + node.* = Queue(i32).Node{ .prev = undefined, .next = undefined, .data = x, - }) catch unreachable; + }; ctx.queue.put(node); _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst); } diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 1e4981353b..503fa0c0ce 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -155,10 +155,11 @@ fn startPuts(ctx: *Context) u8 { while (put_count != 0) : (put_count -= 1) { std.os.time.sleep(1); // let the os scheduler be our fuzz const x = @bitCast(i32, r.random.scalar(u32)); - const node = ctx.allocator.create(Stack(i32).Node{ + const node = ctx.allocator.create(Stack(i32).Node) catch unreachable; + node.* = Stack(i32).Node{ .next = undefined, .data = x, - }) catch unreachable; + }; ctx.stack.push(node); _ = @atomicRmw(isize, &ctx.put_sum, builtin.AtomicRmwOp.Add, x, AtomicOrder.SeqCst); } diff --git a/std/build.zig b/std/build.zig index 90f5bec656..6f58594190 100644 --- a/std/build.zig +++ b/std/build.zig @@ -89,7 +89,7 @@ pub const Builder = struct { }; pub fn init(allocator: *Allocator, zig_exe: []const u8, build_root: []const u8, cache_root: []const u8) Builder { - const env_map = allocator.createOne(BufMap) catch unreachable; + const env_map = allocator.create(BufMap) catch unreachable; env_map.* = os.getEnvMap(allocator) catch unreachable; var self = Builder{ .zig_exe = zig_exe, @@ -170,7 +170,8 @@ pub const Builder = struct { } pub fn addTest(self: *Builder, root_src: []const u8) *TestStep { - const test_step = self.allocator.create(TestStep.init(self, root_src)) catch unreachable; + const test_step = self.allocator.create(TestStep) catch unreachable; + test_step.* = TestStep.init(self, root_src); return test_step; } @@ -202,18 +203,21 @@ pub const Builder = struct { } pub fn addWriteFile(self: *Builder, file_path: []const u8, data: []const u8) *WriteFileStep { - const write_file_step = self.allocator.create(WriteFileStep.init(self, file_path, data)) catch unreachable; + const write_file_step = self.allocator.create(WriteFileStep) catch unreachable; + write_file_step.* = WriteFileStep.init(self, file_path, data); return write_file_step; } pub fn addLog(self: *Builder, comptime format: []const u8, args: ...) *LogStep { const data = self.fmt(format, args); - const log_step = self.allocator.create(LogStep.init(self, data)) catch unreachable; + const log_step = self.allocator.create(LogStep) catch unreachable; + log_step.* = LogStep.init(self, data); return log_step; } pub fn addRemoveDirTree(self: *Builder, dir_path: []const u8) *RemoveDirStep { - const remove_dir_step = self.allocator.create(RemoveDirStep.init(self, dir_path)) catch unreachable; + const remove_dir_step = self.allocator.create(RemoveDirStep) catch unreachable; + remove_dir_step.* = RemoveDirStep.init(self, dir_path); return remove_dir_step; } @@ -414,10 +418,11 @@ pub const Builder = struct { } pub fn step(self: *Builder, name: []const u8, description: []const u8) *Step { - const step_info = self.allocator.create(TopLevelStep{ + const step_info = self.allocator.create(TopLevelStep) catch unreachable; + step_info.* = TopLevelStep{ .step = Step.initNoOp(name, self.allocator), .description = description, - }) catch unreachable; + }; self.top_level_steps.append(step_info) catch unreachable; return &step_info.step; } @@ -616,7 +621,8 @@ pub const Builder = struct { const full_dest_path = os.path.resolve(self.allocator, self.prefix, dest_rel_path) catch unreachable; self.pushInstalledFile(full_dest_path); - const install_step = self.allocator.create(InstallFileStep.init(self, src_path, full_dest_path)) catch unreachable; + const install_step = self.allocator.create(InstallFileStep) catch unreachable; + install_step.* = InstallFileStep.init(self, src_path, full_dest_path); return install_step; } @@ -865,43 +871,51 @@ pub const LibExeObjStep = struct { }; pub fn createSharedLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8, ver: Version) *LibExeObjStep { - const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, false, ver)) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initExtraArgs(builder, name, root_src, Kind.Lib, false, ver); return self; } pub fn createCSharedLibrary(builder: *Builder, name: []const u8, version: Version) *LibExeObjStep { - const self = builder.allocator.create(initC(builder, name, Kind.Lib, version, false)) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initC(builder, name, Kind.Lib, version, false); return self; } pub fn createStaticLibrary(builder: *Builder, name: []const u8, root_src: ?[]const u8) *LibExeObjStep { - const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0))) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initExtraArgs(builder, name, root_src, Kind.Lib, true, builder.version(0, 0, 0)); return self; } pub fn createCStaticLibrary(builder: *Builder, name: []const u8) *LibExeObjStep { - const self = builder.allocator.create(initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true)) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initC(builder, name, Kind.Lib, builder.version(0, 0, 0), true); return self; } pub fn createObject(builder: *Builder, name: []const u8, root_src: []const u8) *LibExeObjStep { - const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0))) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initExtraArgs(builder, name, root_src, Kind.Obj, false, builder.version(0, 0, 0)); return self; } pub fn createCObject(builder: *Builder, name: []const u8, src: []const u8) *LibExeObjStep { - const self = builder.allocator.create(initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false)) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initC(builder, name, Kind.Obj, builder.version(0, 0, 0), false); self.object_src = src; return self; } pub fn createExecutable(builder: *Builder, name: []const u8, root_src: ?[]const u8, static: bool) *LibExeObjStep { - const self = builder.allocator.create(initExtraArgs(builder, name, root_src, Kind.Exe, static, builder.version(0, 0, 0))) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initExtraArgs(builder, name, root_src, Kind.Exe, static, builder.version(0, 0, 0)); return self; } pub fn createCExecutable(builder: *Builder, name: []const u8) *LibExeObjStep { - const self = builder.allocator.create(initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false)) catch unreachable; + const self = builder.allocator.create(LibExeObjStep) catch unreachable; + self.* = initC(builder, name, Kind.Exe, builder.version(0, 0, 0), false); return self; } @@ -1914,13 +1928,14 @@ pub const CommandStep = struct { /// ::argv is copied. pub fn create(builder: *Builder, cwd: ?[]const u8, env_map: *const BufMap, argv: []const []const u8) *CommandStep { - const self = builder.allocator.create(CommandStep{ + const self = builder.allocator.create(CommandStep) catch unreachable; + self.* = CommandStep{ .builder = builder, .step = Step.init(argv[0], builder.allocator, make), .argv = builder.allocator.alloc([]u8, argv.len) catch unreachable, .cwd = cwd, .env_map = env_map, - }) catch unreachable; + }; mem.copy([]const u8, self.argv, argv); self.step.name = self.argv[0]; @@ -1949,12 +1964,13 @@ const InstallArtifactStep = struct { LibExeObjStep.Kind.Exe => builder.exe_dir, LibExeObjStep.Kind.Lib => builder.lib_dir, }; - const self = builder.allocator.create(Self{ + const self = builder.allocator.create(Self) catch unreachable; + self.* = Self{ .builder = builder, .step = Step.init(builder.fmt("install {}", artifact.step.name), builder.allocator, make), .artifact = artifact, .dest_file = os.path.join(builder.allocator, dest_dir, artifact.out_filename) catch unreachable, - }) catch unreachable; + }; self.step.dependOn(&artifact.step); builder.pushInstalledFile(self.dest_file); if (self.artifact.kind == LibExeObjStep.Kind.Lib and !self.artifact.static) { diff --git a/std/debug/index.zig b/std/debug/index.zig index 445f943594..838bd0c166 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -751,7 +751,7 @@ fn openSelfDebugInfoWindows(allocator: *mem.Allocator) !DebugInfo { const self_file = try os.openSelfExe(); defer self_file.close(); - const coff_obj = try allocator.createOne(coff.Coff); + const coff_obj = try allocator.create(coff.Coff); coff_obj.* = coff.Coff{ .in_file = self_file, .allocator = allocator, @@ -1036,7 +1036,7 @@ fn openSelfDebugInfoMacOs(allocator: *mem.Allocator) !DebugInfo { } } } - const sentinel = try allocator.createOne(macho.nlist_64); + const sentinel = try allocator.create(macho.nlist_64); sentinel.* = macho.nlist_64{ .n_strx = 0, .n_type = 36, @@ -1949,7 +1949,8 @@ fn scanAllCompileUnits(di: *DwarfInfo) !void { try di.dwarf_seekable_stream.seekTo(compile_unit_pos); - const compile_unit_die = try di.allocator().create(try parseDie(di, abbrev_table, is_64)); + const compile_unit_die = try di.allocator().create(Die); + compile_unit_die.* = try parseDie(di, abbrev_table, is_64); if (compile_unit_die.tag_id != DW.TAG_compile_unit) return error.InvalidDebugInfo; diff --git a/std/event/channel.zig b/std/event/channel.zig index f04ad1604e..f8cdae6208 100644 --- a/std/event/channel.zig +++ b/std/event/channel.zig @@ -54,7 +54,8 @@ pub fn Channel(comptime T: type) type { const buffer_nodes = try loop.allocator.alloc(T, capacity); errdefer loop.allocator.free(buffer_nodes); - const self = try loop.allocator.create(SelfChannel{ + const self = try loop.allocator.create(SelfChannel); + self.* = SelfChannel{ .loop = loop, .buffer_len = 0, .buffer_nodes = buffer_nodes, @@ -66,7 +67,7 @@ pub fn Channel(comptime T: type) type { .or_null_queue = std.atomic.Queue(*std.atomic.Queue(GetNode).Node).init(), .get_count = 0, .put_count = 0, - }); + }; errdefer loop.allocator.destroy(self); return self; diff --git a/std/event/fs.zig b/std/event/fs.zig index 7e77b3e6e2..097f2beddc 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -495,7 +495,7 @@ pub const CloseOperation = struct { }; pub fn start(loop: *Loop) (error{OutOfMemory}!*CloseOperation) { - const self = try loop.allocator.createOne(CloseOperation); + const self = try loop.allocator.create(CloseOperation); self.* = CloseOperation{ .loop = loop, .os_data = switch (builtin.os) { @@ -787,7 +787,7 @@ pub fn Watch(comptime V: type) type { }, builtin.Os.windows => { - const self = try loop.allocator.createOne(Self); + const self = try loop.allocator.create(Self); errdefer loop.allocator.destroy(self); self.* = Self{ .channel = channel, @@ -802,7 +802,7 @@ pub fn Watch(comptime V: type) type { }, builtin.Os.macosx, builtin.Os.freebsd => { - const self = try loop.allocator.createOne(Self); + const self = try loop.allocator.create(Self); errdefer loop.allocator.destroy(self); self.* = Self{ @@ -1068,7 +1068,7 @@ pub fn Watch(comptime V: type) type { } } else { errdefer _ = self.os_data.dir_table.remove(dirname); - const dir = try self.channel.loop.allocator.createOne(OsData.Dir); + const dir = try self.channel.loop.allocator.create(OsData.Dir); errdefer self.channel.loop.allocator.destroy(dir); dir.* = OsData.Dir{ diff --git a/std/event/group.zig b/std/event/group.zig index 0e9c2d9655..7f6b5d953b 100644 --- a/std/event/group.zig +++ b/std/event/group.zig @@ -42,10 +42,11 @@ pub fn Group(comptime ReturnType: type) type { /// Add a promise to the group. Thread-safe. pub fn add(self: *Self, handle: promise->ReturnType) (error{OutOfMemory}!void) { - const node = try self.lock.loop.allocator.create(Stack.Node{ + const node = try self.lock.loop.allocator.create(Stack.Node); + node.* = Stack.Node{ .next = undefined, .data = handle, - }); + }; self.alloc_stack.push(node); } diff --git a/std/heap.zig b/std/heap.zig index 46b247fa7e..fd2ce1e965 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -518,7 +518,8 @@ fn testAllocator(allocator: *mem.Allocator) !void { var slice = try allocator.alloc(*i32, 100); assert(slice.len == 100); for (slice) |*item, i| { - item.* = try allocator.create(@intCast(i32, i)); + item.* = try allocator.create(i32); + item.*.* = @intCast(i32, i); } slice = try allocator.realloc(*i32, slice, 20000); diff --git a/std/io.zig b/std/io.zig index 46625cd34b..c8701aeda6 100644 --- a/std/io.zig +++ b/std/io.zig @@ -944,12 +944,13 @@ pub const BufferedAtomicFile = struct { pub fn create(allocator: *mem.Allocator, dest_path: []const u8) !*BufferedAtomicFile { // TODO with well defined copy elision we don't need this allocation - var self = try allocator.create(BufferedAtomicFile{ + var self = try allocator.create(BufferedAtomicFile); + self.* = BufferedAtomicFile{ .atomic_file = undefined, .file_stream = undefined, .buffered_stream = undefined, .allocator = allocator, - }); + }; errdefer allocator.destroy(self); self.atomic_file = try os.AtomicFile.init(dest_path, os.File.default_mode); diff --git a/std/linked_list.zig b/std/linked_list.zig index c3db55b5a6..7021cac707 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -190,7 +190,7 @@ pub fn LinkedList(comptime T: type) type { /// Returns: /// A pointer to the new node. pub fn allocateNode(list: *Self, allocator: *Allocator) !*Node { - return allocator.create(Node(undefined)); + return allocator.create(Node); } /// Deallocate a node. diff --git a/std/mem.zig b/std/mem.zig index fb5f6fd5da..a403d80453 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -36,20 +36,9 @@ pub const Allocator = struct { /// Guaranteed: `old_mem.len` is the same as what was returned from `allocFn` or `reallocFn` freeFn: fn (self: *Allocator, old_mem: []u8) void, - /// Call `destroy` with the result - /// TODO this is deprecated. use createOne instead - pub fn create(self: *Allocator, init: var) Error!*@typeOf(init) { - const T = @typeOf(init); - if (@sizeOf(T) == 0) return &(T{}); - const slice = try self.alloc(T, 1); - const ptr = &slice[0]; - ptr.* = init; - return ptr; - } - /// Call `destroy` with the result. /// Returns undefined memory. - pub fn createOne(self: *Allocator, comptime T: type) Error!*T { + pub fn create(self: *Allocator, comptime T: type) Error!*T { if (@sizeOf(T) == 0) return &(T{}); const slice = try self.alloc(T, 1); return &slice[0]; diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 0aa896ff1b..9f33bee905 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -88,7 +88,8 @@ pub const ChildProcess = struct { /// First argument in argv is the executable. /// On success must call deinit. pub fn init(argv: []const []const u8, allocator: *mem.Allocator) !*ChildProcess { - const child = try allocator.create(ChildProcess{ + const child = try allocator.create(ChildProcess); + child.* = ChildProcess{ .allocator = allocator, .argv = argv, .pid = undefined, @@ -109,7 +110,7 @@ pub const ChildProcess = struct { .stdin_behavior = StdIo.Inherit, .stdout_behavior = StdIo.Inherit, .stderr_behavior = StdIo.Inherit, - }); + }; errdefer allocator.destroy(child); return child; } diff --git a/std/os/index.zig b/std/os/index.zig index 75abe3bbde..0d0e07bfa3 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -3047,7 +3047,8 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread const bytes_ptr = windows.HeapAlloc(heap_handle, 0, byte_count) orelse return SpawnThreadError.OutOfMemory; errdefer assert(windows.HeapFree(heap_handle, 0, bytes_ptr) != 0); const bytes = @ptrCast([*]u8, bytes_ptr)[0..byte_count]; - const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext{ + const outer_context = std.heap.FixedBufferAllocator.init(bytes).allocator.create(WinThread.OuterContext) catch unreachable; + outer_context.* = WinThread.OuterContext{ .thread = Thread{ .data = Thread.Data{ .heap_handle = heap_handle, @@ -3056,7 +3057,7 @@ pub fn spawnThread(context: var, comptime startFn: var) SpawnThreadError!*Thread }, }, .inner = context, - }) catch unreachable; + }; const parameter = if (@sizeOf(Context) == 0) null else @ptrCast(*c_void, &outer_context.inner); outer_context.thread.data.handle = windows.CreateThread(null, default_stack_size, WinThread.threadMain, parameter, 0, null) orelse { diff --git a/std/zig/parse.zig b/std/zig/parse.zig index a216484d7d..783464c620 100644 --- a/std/zig/parse.zig +++ b/std/zig/parse.zig @@ -17,14 +17,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { defer stack.deinit(); const arena = &tree_arena.allocator; - const root_node = try arena.create(ast.Node.Root{ + const root_node = try arena.create(ast.Node.Root); + root_node.* = ast.Node.Root{ .base = ast.Node{ .id = ast.Node.Id.Root }, .decls = ast.Node.Root.DeclList.init(arena), .doc_comments = null, .shebang = null, // initialized when we get the eof token .eof_token = undefined, - }); + }; var tree = ast.Tree{ .source = source, @@ -75,20 +76,22 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { Token.Id.Keyword_test => { stack.append(State.TopLevel) catch unreachable; - const block = try arena.create(ast.Node.Block{ + const block = try arena.create(ast.Node.Block); + block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = null, .lbrace = undefined, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); - const test_node = try arena.create(ast.Node.TestDecl{ + }; + const test_node = try arena.create(ast.Node.TestDecl); + test_node.* = ast.Node.TestDecl{ .base = ast.Node{ .id = ast.Node.Id.TestDecl }, .doc_comments = comments, .test_token = token_index, .name = undefined, .body_node = &block.base, - }); + }; try root_node.decls.push(&test_node.base); try stack.append(State{ .Block = block }); try stack.append(State{ @@ -119,19 +122,21 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_comptime => { - const block = try arena.create(ast.Node.Block{ + const block = try arena.create(ast.Node.Block); + block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = null, .lbrace = undefined, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); - const node = try arena.create(ast.Node.Comptime{ + }; + const node = try arena.create(ast.Node.Comptime); + node.* = ast.Node.Comptime{ .base = ast.Node{ .id = ast.Node.Id.Comptime }, .comptime_token = token_index, .expr = &block.base, .doc_comments = comments, - }); + }; try root_node.decls.push(&node.base); stack.append(State.TopLevel) catch unreachable; @@ -235,14 +240,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { return tree; } - const node = try arena.create(ast.Node.Use{ + const node = try arena.create(ast.Node.Use); + node.* = ast.Node.Use{ .base = ast.Node{ .id = ast.Node.Id.Use }, .use_token = token_index, .visib_token = ctx.visib_token, .expr = undefined, .semicolon_token = undefined, .doc_comments = ctx.comments, - }); + }; try ctx.decls.push(&node.base); stack.append(State{ @@ -276,7 +282,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_fn, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc, Token.Id.Keyword_async => { - const fn_proto = try arena.create(ast.Node.FnProto{ + const fn_proto = try arena.create(ast.Node.FnProto); + fn_proto.* = ast.Node.FnProto{ .base = ast.Node{ .id = ast.Node.Id.FnProto }, .doc_comments = ctx.comments, .visib_token = ctx.visib_token, @@ -292,7 +299,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .lib_name = ctx.lib_name, .align_expr = null, .section_expr = null, - }); + }; try ctx.decls.push(&fn_proto.base); stack.append(State{ .FnDef = fn_proto }) catch unreachable; try stack.append(State{ .FnProto = fn_proto }); @@ -309,12 +316,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_async => { - const async_node = try arena.create(ast.Node.AsyncAttribute{ + const async_node = try arena.create(ast.Node.AsyncAttribute); + async_node.* = ast.Node.AsyncAttribute{ .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute }, .async_token = token_index, .allocator_type = null, .rangle_bracket = null, - }); + }; fn_proto.async_attr = async_node; try stack.append(State{ @@ -341,13 +349,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { }, State.TopLevelExternOrField => |ctx| { if (eatToken(&tok_it, &tree, Token.Id.Identifier)) |identifier| { - const node = try arena.create(ast.Node.StructField{ + const node = try arena.create(ast.Node.StructField); + node.* = ast.Node.StructField{ .base = ast.Node{ .id = ast.Node.Id.StructField }, .doc_comments = ctx.comments, .visib_token = ctx.visib_token, .name_token = identifier, .type_expr = undefined, - }); + }; const node_ptr = try ctx.container_decl.fields_and_decls.addOne(); node_ptr.* = &node.base; @@ -391,7 +400,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token = nextToken(&tok_it, &tree); const token_index = token.index; const token_ptr = token.ptr; - const node = try arena.create(ast.Node.ContainerDecl{ + const node = try arena.create(ast.Node.ContainerDecl); + node.* = ast.Node.ContainerDecl{ .base = ast.Node{ .id = ast.Node.Id.ContainerDecl }, .layout_token = ctx.layout_token, .kind_token = switch (token_ptr.id) { @@ -405,7 +415,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .fields_and_decls = ast.Node.ContainerDecl.DeclList.init(arena), .lbrace_token = undefined, .rbrace_token = undefined, - }); + }; ctx.opt_ctx.store(&node.base); stack.append(State{ .ContainerDecl = node }) catch unreachable; @@ -464,13 +474,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { Token.Id.Identifier => { switch (tree.tokens.at(container_decl.kind_token).id) { Token.Id.Keyword_struct => { - const node = try arena.create(ast.Node.StructField{ + const node = try arena.create(ast.Node.StructField); + node.* = ast.Node.StructField{ .base = ast.Node{ .id = ast.Node.Id.StructField }, .doc_comments = comments, .visib_token = null, .name_token = token_index, .type_expr = undefined, - }); + }; const node_ptr = try container_decl.fields_and_decls.addOne(); node_ptr.* = &node.base; @@ -485,13 +496,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_union => { - const node = try arena.create(ast.Node.UnionTag{ + const node = try arena.create(ast.Node.UnionTag); + node.* = ast.Node.UnionTag{ .base = ast.Node{ .id = ast.Node.Id.UnionTag }, .name_token = token_index, .type_expr = null, .value_expr = null, .doc_comments = comments, - }); + }; try container_decl.fields_and_decls.push(&node.base); try stack.append(State{ @@ -506,12 +518,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_enum => { - const node = try arena.create(ast.Node.EnumTag{ + const node = try arena.create(ast.Node.EnumTag); + node.* = ast.Node.EnumTag{ .base = ast.Node{ .id = ast.Node.Id.EnumTag }, .name_token = token_index, .value = null, .doc_comments = comments, - }); + }; try container_decl.fields_and_decls.push(&node.base); try stack.append(State{ @@ -593,7 +606,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { }, State.VarDecl => |ctx| { - const var_decl = try arena.create(ast.Node.VarDecl{ + const var_decl = try arena.create(ast.Node.VarDecl); + var_decl.* = ast.Node.VarDecl{ .base = ast.Node{ .id = ast.Node.Id.VarDecl }, .doc_comments = ctx.comments, .visib_token = ctx.visib_token, @@ -609,7 +623,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .name_token = undefined, .eq_token = undefined, .semicolon_token = undefined, - }); + }; try ctx.list.push(&var_decl.base); try stack.append(State{ .VarDeclAlign = var_decl }); @@ -708,13 +722,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_ptr = token.ptr; switch (token_ptr.id) { Token.Id.LBrace => { - const block = try arena.create(ast.Node.Block{ + const block = try arena.create(ast.Node.Block); + block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = null, .lbrace = token_index, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); + }; fn_proto.body_node = &block.base; stack.append(State{ .Block = block }) catch unreachable; continue; @@ -770,10 +785,11 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { // TODO: this is a special case. Remove this when #760 is fixed if (token_ptr.id == Token.Id.Keyword_anyerror) { if (tok_it.peek().?.id == Token.Id.LBrace) { - const error_type_node = try arena.create(ast.Node.ErrorType{ + const error_type_node = try arena.create(ast.Node.ErrorType); + error_type_node.* = ast.Node.ErrorType{ .base = ast.Node{ .id = ast.Node.Id.ErrorType }, .token = token_index, - }); + }; fn_proto.return_type = ast.Node.FnProto.ReturnType{ .Explicit = &error_type_node.base }; continue; } @@ -791,14 +807,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { if (eatToken(&tok_it, &tree, Token.Id.RParen)) |_| { continue; } - const param_decl = try arena.create(ast.Node.ParamDecl{ + const param_decl = try arena.create(ast.Node.ParamDecl); + param_decl.* = ast.Node.ParamDecl{ .base = ast.Node{ .id = ast.Node.Id.ParamDecl }, .comptime_token = null, .noalias_token = null, .name_token = null, .type_node = undefined, .var_args_token = null, - }); + }; try fn_proto.params.push(¶m_decl.base); stack.append(State{ @@ -877,13 +894,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_ptr = token.ptr; switch (token_ptr.id) { Token.Id.LBrace => { - const block = try arena.create(ast.Node.Block{ + const block = try arena.create(ast.Node.Block); + block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = ctx.label, .lbrace = token_index, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); + }; ctx.opt_ctx.store(&block.base); stack.append(State{ .Block = block }) catch unreachable; continue; @@ -970,7 +988,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { } }, State.While => |ctx| { - const node = try arena.create(ast.Node.While{ + const node = try arena.create(ast.Node.While); + node.* = ast.Node.While{ .base = ast.Node{ .id = ast.Node.Id.While }, .label = ctx.label, .inline_token = ctx.inline_token, @@ -980,7 +999,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .continue_expr = null, .body = undefined, .@"else" = null, - }); + }; ctx.opt_ctx.store(&node.base); stack.append(State{ .Else = &node.@"else" }) catch unreachable; try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }); @@ -999,7 +1018,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, State.For => |ctx| { - const node = try arena.create(ast.Node.For{ + const node = try arena.create(ast.Node.For); + node.* = ast.Node.For{ .base = ast.Node{ .id = ast.Node.Id.For }, .label = ctx.label, .inline_token = ctx.inline_token, @@ -1008,7 +1028,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .payload = null, .body = undefined, .@"else" = null, - }); + }; ctx.opt_ctx.store(&node.base); stack.append(State{ .Else = &node.@"else" }) catch unreachable; try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }); @@ -1020,12 +1040,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { }, State.Else => |dest| { if (eatToken(&tok_it, &tree, Token.Id.Keyword_else)) |else_token| { - const node = try arena.create(ast.Node.Else{ + const node = try arena.create(ast.Node.Else); + node.* = ast.Node.Else{ .base = ast.Node{ .id = ast.Node.Id.Else }, .else_token = else_token, .payload = null, .body = undefined, - }); + }; dest.* = node; stack.append(State{ .Expression = OptionalCtx{ .Required = &node.body } }) catch unreachable; @@ -1083,11 +1104,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_defer, Token.Id.Keyword_errdefer => { - const node = try arena.create(ast.Node.Defer{ + const node = try arena.create(ast.Node.Defer); + node.* = ast.Node.Defer{ .base = ast.Node{ .id = ast.Node.Id.Defer }, .defer_token = token_index, .expr = undefined, - }); + }; const node_ptr = try block.statements.addOne(); node_ptr.* = &node.base; @@ -1096,13 +1118,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.LBrace => { - const inner_block = try arena.create(ast.Node.Block{ + const inner_block = try arena.create(ast.Node.Block); + inner_block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = null, .lbrace = token_index, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); + }; try block.statements.push(&inner_block.base); stack.append(State{ .Block = inner_block }) catch unreachable; @@ -1164,14 +1187,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.AsmOutput{ + const node = try arena.create(ast.Node.AsmOutput); + node.* = ast.Node.AsmOutput{ .base = ast.Node{ .id = ast.Node.Id.AsmOutput }, .lbracket = lbracket_index, .symbolic_name = undefined, .constraint = undefined, .kind = undefined, .rparen = undefined, - }); + }; try items.push(node); stack.append(State{ .AsmOutputItems = items }) catch unreachable; @@ -1218,14 +1242,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.AsmInput{ + const node = try arena.create(ast.Node.AsmInput); + node.* = ast.Node.AsmInput{ .base = ast.Node{ .id = ast.Node.Id.AsmInput }, .lbracket = lbracket_index, .symbolic_name = undefined, .constraint = undefined, .expr = undefined, .rparen = undefined, - }); + }; try items.push(node); stack.append(State{ .AsmInputItems = items }) catch unreachable; @@ -1283,12 +1308,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.FieldInitializer{ + const node = try arena.create(ast.Node.FieldInitializer); + node.* = ast.Node.FieldInitializer{ .base = ast.Node{ .id = ast.Node.Id.FieldInitializer }, .period_token = undefined, .name_token = undefined, .expr = undefined, - }); + }; try list_state.list.push(&node.base); stack.append(State{ .FieldInitListCommaOrEnd = list_state }) catch unreachable; @@ -1390,13 +1416,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { } const comments = try eatDocComments(arena, &tok_it, &tree); - const node = try arena.create(ast.Node.SwitchCase{ + const node = try arena.create(ast.Node.SwitchCase); + node.* = ast.Node.SwitchCase{ .base = ast.Node{ .id = ast.Node.Id.SwitchCase }, .items = ast.Node.SwitchCase.ItemList.init(arena), .payload = null, .expr = undefined, .arrow_token = undefined, - }); + }; try list_state.list.push(&node.base); try stack.append(State{ .SwitchCaseCommaOrEnd = list_state }); try stack.append(State{ .AssignmentExpressionBegin = OptionalCtx{ .Required = &node.expr } }); @@ -1427,10 +1454,11 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (token_ptr.id == Token.Id.Keyword_else) { - const else_node = try arena.create(ast.Node.SwitchElse{ + const else_node = try arena.create(ast.Node.SwitchElse); + else_node.* = ast.Node.SwitchElse{ .base = ast.Node{ .id = ast.Node.Id.SwitchElse }, .token = token_index, - }); + }; try switch_case.items.push(&else_node.base); try stack.append(State{ @@ -1537,7 +1565,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { State.ExternType => |ctx| { if (eatToken(&tok_it, &tree, Token.Id.Keyword_fn)) |fn_token| { - const fn_proto = try arena.create(ast.Node.FnProto{ + const fn_proto = try arena.create(ast.Node.FnProto); + fn_proto.* = ast.Node.FnProto{ .base = ast.Node{ .id = ast.Node.Id.FnProto }, .doc_comments = ctx.comments, .visib_token = null, @@ -1553,7 +1582,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .lib_name = null, .align_expr = null, .section_expr = null, - }); + }; ctx.opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; continue; @@ -1711,12 +1740,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.Payload{ + const node = try arena.create(ast.Node.Payload); + node.* = ast.Node.Payload{ .base = ast.Node{ .id = ast.Node.Id.Payload }, .lpipe = token_index, .error_symbol = undefined, .rpipe = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ @@ -1747,13 +1777,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.PointerPayload{ + const node = try arena.create(ast.Node.PointerPayload); + node.* = ast.Node.PointerPayload{ .base = ast.Node{ .id = ast.Node.Id.PointerPayload }, .lpipe = token_index, .ptr_token = null, .value_symbol = undefined, .rpipe = undefined, - }); + }; opt_ctx.store(&node.base); try stack.append(State{ @@ -1790,14 +1821,15 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.PointerIndexPayload{ + const node = try arena.create(ast.Node.PointerIndexPayload); + node.* = ast.Node.PointerIndexPayload{ .base = ast.Node{ .id = ast.Node.Id.PointerIndexPayload }, .lpipe = token_index, .ptr_token = null, .value_symbol = undefined, .index_symbol = null, .rpipe = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ @@ -1824,12 +1856,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_ptr = token.ptr; switch (token_ptr.id) { Token.Id.Keyword_return, Token.Id.Keyword_break, Token.Id.Keyword_continue => { - const node = try arena.create(ast.Node.ControlFlowExpression{ + const node = try arena.create(ast.Node.ControlFlowExpression); + node.* = ast.Node.ControlFlowExpression{ .base = ast.Node{ .id = ast.Node.Id.ControlFlowExpression }, .ltoken = token_index, .kind = undefined, .rhs = null, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .Expression = OptionalCtx{ .Optional = &node.rhs } }) catch unreachable; @@ -1853,7 +1886,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_try, Token.Id.Keyword_cancel, Token.Id.Keyword_resume => { - const node = try arena.create(ast.Node.PrefixOp{ + const node = try arena.create(ast.Node.PrefixOp); + node.* = ast.Node.PrefixOp{ .base = ast.Node{ .id = ast.Node.Id.PrefixOp }, .op_token = token_index, .op = switch (token_ptr.id) { @@ -1863,7 +1897,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { else => unreachable, }, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable; @@ -1887,13 +1921,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Ellipsis3)) |ellipsis3| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = ellipsis3, .op = ast.Node.InfixOp.Op.Range, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }) catch unreachable; continue; @@ -1912,13 +1947,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToAssignment(token_ptr.id)) |ass_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = ass_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .AssignmentExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.rhs } }); @@ -1942,13 +1978,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToUnwrapExpr(token_ptr.id)) |unwrap_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = unwrap_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .UnwrapExpressionEnd = opt_ctx.toRequired() }) catch unreachable; @@ -1974,13 +2011,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Keyword_or)) |or_token| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = or_token, .op = ast.Node.InfixOp.Op.BoolOr, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BoolOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .BoolAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -1998,13 +2036,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Keyword_and)) |and_token| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = and_token, .op = ast.Node.InfixOp.Op.BoolAnd, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BoolAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .ComparisonExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2025,13 +2064,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToComparison(token_ptr.id)) |comp_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = comp_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .ComparisonExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .BinaryOrExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2052,13 +2092,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Pipe)) |pipe| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = pipe, .op = ast.Node.InfixOp.Op.BitOr, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BinaryOrExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .BinaryXorExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2076,13 +2117,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Caret)) |caret| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = caret, .op = ast.Node.InfixOp.Op.BitXor, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BinaryXorExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .BinaryAndExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2100,13 +2142,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Ampersand)) |ampersand| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = ampersand, .op = ast.Node.InfixOp.Op.BitAnd, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BinaryAndExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .BitShiftExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2127,13 +2170,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToBitShift(token_ptr.id)) |bitshift_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = bitshift_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .BitShiftExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .AdditionExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2157,13 +2201,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToAddition(token_ptr.id)) |add_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = add_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .AdditionExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .MultiplyExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2187,13 +2232,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToMultiply(token_ptr.id)) |mult_id| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = mult_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .MultiplyExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .CurlySuffixExpressionBegin = OptionalCtx{ .Required = &node.rhs } }); @@ -2215,12 +2261,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (tok_it.peek().?.id == Token.Id.Period) { - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op{ .StructInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) }, .rtoken = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable; @@ -2234,12 +2281,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op{ .ArrayInitializer = ast.Node.SuffixOp.Op.InitList.init(arena) }, .rtoken = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .CurlySuffixExpressionEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .IfToken = Token.Id.LBrace }); @@ -2263,13 +2311,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const lhs = opt_ctx.get() orelse continue; if (eatToken(&tok_it, &tree, Token.Id.Bang)) |bang| { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = bang, .op = ast.Node.InfixOp.Op.ErrorUnion, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .TypeExprEnd = opt_ctx.toRequired() }) catch unreachable; try stack.append(State{ .PrefixOpExpression = OptionalCtx{ .Required = &node.rhs } }); @@ -2282,22 +2331,24 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_index = token.index; const token_ptr = token.ptr; if (tokenIdToPrefixOp(token_ptr.id)) |prefix_id| { - var node = try arena.create(ast.Node.PrefixOp{ + var node = try arena.create(ast.Node.PrefixOp); + node.* = ast.Node.PrefixOp{ .base = ast.Node{ .id = ast.Node.Id.PrefixOp }, .op_token = token_index, .op = prefix_id, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); // Treat '**' token as two pointer types if (token_ptr.id == Token.Id.AsteriskAsterisk) { - const child = try arena.create(ast.Node.PrefixOp{ + const child = try arena.create(ast.Node.PrefixOp); + child.* = ast.Node.PrefixOp{ .base = ast.Node{ .id = ast.Node.Id.PrefixOp }, .op_token = token_index, .op = prefix_id, .rhs = undefined, - }); + }; node.rhs = &child.base; node = child; } @@ -2316,12 +2367,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { State.SuffixOpExpressionBegin => |opt_ctx| { if (eatToken(&tok_it, &tree, Token.Id.Keyword_async)) |async_token| { - const async_node = try arena.create(ast.Node.AsyncAttribute{ + const async_node = try arena.create(ast.Node.AsyncAttribute); + async_node.* = ast.Node.AsyncAttribute{ .base = ast.Node{ .id = ast.Node.Id.AsyncAttribute }, .async_token = async_token, .allocator_type = null, .rangle_bracket = null, - }); + }; stack.append(State{ .AsyncEnd = AsyncEndCtx{ .ctx = opt_ctx, @@ -2347,7 +2399,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { const token_ptr = token.ptr; switch (token_ptr.id) { Token.Id.LParen => { - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op{ @@ -2357,7 +2410,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { }, }, .rtoken = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; @@ -2371,12 +2424,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.LBracket => { - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op{ .ArrayAccess = undefined }, .rtoken = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; @@ -2386,34 +2440,37 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { }, Token.Id.Period => { if (eatToken(&tok_it, &tree, Token.Id.Asterisk)) |asterisk_token| { - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op.Deref, .rtoken = asterisk_token, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; continue; } if (eatToken(&tok_it, &tree, Token.Id.QuestionMark)) |question_token| { - const node = try arena.create(ast.Node.SuffixOp{ + const node = try arena.create(ast.Node.SuffixOp); + node.* = ast.Node.SuffixOp{ .base = ast.Node{ .id = ast.Node.Id.SuffixOp }, .lhs = lhs, .op = ast.Node.SuffixOp.Op.UnwrapOptional, .rtoken = question_token, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; continue; } - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = lhs, .op_token = token_index, .op = ast.Node.InfixOp.Op.Period, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SuffixOpExpressionEnd = opt_ctx.toRequired() }) catch unreachable; @@ -2467,11 +2524,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_promise => { - const node = try arena.create(ast.Node.PromiseType{ + const node = try arena.create(ast.Node.PromiseType); + node.* = ast.Node.PromiseType{ .base = ast.Node{ .id = ast.Node.Id.PromiseType }, .promise_token = token.index, .result = null, - }); + }; opt_ctx.store(&node.base); const next_token = nextToken(&tok_it, &tree); const next_token_index = next_token.index; @@ -2493,12 +2551,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.LParen => { - const node = try arena.create(ast.Node.GroupedExpression{ + const node = try arena.create(ast.Node.GroupedExpression); + node.* = ast.Node.GroupedExpression{ .base = ast.Node{ .id = ast.Node.Id.GroupedExpression }, .lparen = token.index, .expr = undefined, .rparen = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ @@ -2511,12 +2570,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Builtin => { - const node = try arena.create(ast.Node.BuiltinCall{ + const node = try arena.create(ast.Node.BuiltinCall); + node.* = ast.Node.BuiltinCall{ .base = ast.Node{ .id = ast.Node.Id.BuiltinCall }, .builtin_token = token.index, .params = ast.Node.BuiltinCall.ParamList.init(arena), .rparen_token = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ @@ -2530,12 +2590,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.LBracket => { - const node = try arena.create(ast.Node.PrefixOp{ + const node = try arena.create(ast.Node.PrefixOp); + node.* = ast.Node.PrefixOp{ .base = ast.Node{ .id = ast.Node.Id.PrefixOp }, .op_token = token.index, .op = undefined, .rhs = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ .SliceOrArrayType = node }) catch unreachable; @@ -2593,7 +2654,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_fn => { - const fn_proto = try arena.create(ast.Node.FnProto{ + const fn_proto = try arena.create(ast.Node.FnProto); + fn_proto.* = ast.Node.FnProto{ .base = ast.Node{ .id = ast.Node.Id.FnProto }, .doc_comments = null, .visib_token = null, @@ -2609,13 +2671,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .lib_name = null, .align_expr = null, .section_expr = null, - }); + }; opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; continue; }, Token.Id.Keyword_nakedcc, Token.Id.Keyword_stdcallcc => { - const fn_proto = try arena.create(ast.Node.FnProto{ + const fn_proto = try arena.create(ast.Node.FnProto); + fn_proto.* = ast.Node.FnProto{ .base = ast.Node{ .id = ast.Node.Id.FnProto }, .doc_comments = null, .visib_token = null, @@ -2631,7 +2694,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .lib_name = null, .align_expr = null, .section_expr = null, - }); + }; opt_ctx.store(&fn_proto.base); stack.append(State{ .FnProto = fn_proto }) catch unreachable; try stack.append(State{ @@ -2643,7 +2706,8 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; }, Token.Id.Keyword_asm => { - const node = try arena.create(ast.Node.Asm{ + const node = try arena.create(ast.Node.Asm); + node.* = ast.Node.Asm{ .base = ast.Node{ .id = ast.Node.Id.Asm }, .asm_token = token.index, .volatile_token = null, @@ -2652,7 +2716,7 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { .inputs = ast.Node.Asm.InputList.init(arena), .clobbers = ast.Node.Asm.ClobberList.init(arena), .rparen = undefined, - }); + }; opt_ctx.store(&node.base); stack.append(State{ @@ -2701,13 +2765,14 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { State.ErrorTypeOrSetDecl => |ctx| { if (eatToken(&tok_it, &tree, Token.Id.LBrace) == null) { - const node = try arena.create(ast.Node.InfixOp{ + const node = try arena.create(ast.Node.InfixOp); + node.* = ast.Node.InfixOp{ .base = ast.Node{ .id = ast.Node.Id.InfixOp }, .lhs = &(try createLiteral(arena, ast.Node.ErrorType, ctx.error_token)).base, .op_token = undefined, .op = ast.Node.InfixOp.Op.Period, .rhs = undefined, - }); + }; ctx.opt_ctx.store(&node.base); stack.append(State{ .Identifier = OptionalCtx{ .Required = &node.rhs } }) catch unreachable; try stack.append(State{ @@ -2719,12 +2784,13 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { continue; } - const node = try arena.create(ast.Node.ErrorSetDecl{ + const node = try arena.create(ast.Node.ErrorSetDecl); + node.* = ast.Node.ErrorSetDecl{ .base = ast.Node{ .id = ast.Node.Id.ErrorSetDecl }, .error_token = ctx.error_token, .decls = ast.Node.ErrorSetDecl.DeclList.init(arena), .rbrace_token = undefined, - }); + }; ctx.opt_ctx.store(&node.base); stack.append(State{ @@ -2785,11 +2851,12 @@ pub fn parse(allocator: *mem.Allocator, source: []const u8) !ast.Tree { return tree; } - const node = try arena.create(ast.Node.ErrorTag{ + const node = try arena.create(ast.Node.ErrorTag); + node.* = ast.Node.ErrorTag{ .base = ast.Node{ .id = ast.Node.Id.ErrorTag }, .doc_comments = comments, .name_token = ident_token_index, - }); + }; node_ptr.* = &node.base; continue; }, @@ -3129,10 +3196,11 @@ fn pushDocComment(arena: *mem.Allocator, line_comment: TokenIndex, result: *?*as if (result.*) |comment_node| { break :blk comment_node; } else { - const comment_node = try arena.create(ast.Node.DocComment{ + const comment_node = try arena.create(ast.Node.DocComment); + comment_node.* = ast.Node.DocComment{ .base = ast.Node{ .id = ast.Node.Id.DocComment }, .lines = ast.Node.DocComment.LineList.init(arena), - }); + }; result.* = comment_node; break :blk comment_node; } @@ -3158,10 +3226,11 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato return &(try createLiteral(arena, ast.Node.StringLiteral, token_index)).base; }, Token.Id.MultilineStringLiteralLine => { - const node = try arena.create(ast.Node.MultilineStringLiteral{ + const node = try arena.create(ast.Node.MultilineStringLiteral); + node.* = ast.Node.MultilineStringLiteral{ .base = ast.Node{ .id = ast.Node.Id.MultilineStringLiteral }, .lines = ast.Node.MultilineStringLiteral.LineList.init(arena), - }); + }; try node.lines.push(token_index); while (true) { const multiline_str = nextToken(tok_it, tree); @@ -3186,25 +3255,27 @@ fn parseStringLiteral(arena: *mem.Allocator, tok_it: *ast.Tree.TokenList.Iterato fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: OptionalCtx, token_ptr: Token, token_index: TokenIndex) !bool { switch (token_ptr.id) { Token.Id.Keyword_suspend => { - const node = try arena.create(ast.Node.Suspend{ + const node = try arena.create(ast.Node.Suspend); + node.* = ast.Node.Suspend{ .base = ast.Node{ .id = ast.Node.Id.Suspend }, .suspend_token = token_index, .body = null, - }); + }; ctx.store(&node.base); stack.append(State{ .SuspendBody = node }) catch unreachable; return true; }, Token.Id.Keyword_if => { - const node = try arena.create(ast.Node.If{ + const node = try arena.create(ast.Node.If); + node.* = ast.Node.If{ .base = ast.Node{ .id = ast.Node.Id.If }, .if_token = token_index, .condition = undefined, .payload = null, .body = undefined, .@"else" = null, - }); + }; ctx.store(&node.base); stack.append(State{ .Else = &node.@"else" }) catch unreachable; @@ -3238,13 +3309,14 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: Opti return true; }, Token.Id.Keyword_switch => { - const node = try arena.create(ast.Node.Switch{ + const node = try arena.create(ast.Node.Switch); + node.* = ast.Node.Switch{ .base = ast.Node{ .id = ast.Node.Id.Switch }, .switch_token = token_index, .expr = undefined, .cases = ast.Node.Switch.CaseList.init(arena), .rbrace = undefined, - }); + }; ctx.store(&node.base); stack.append(State{ @@ -3260,25 +3332,27 @@ fn parseBlockExpr(stack: *std.ArrayList(State), arena: *mem.Allocator, ctx: Opti return true; }, Token.Id.Keyword_comptime => { - const node = try arena.create(ast.Node.Comptime{ + const node = try arena.create(ast.Node.Comptime); + node.* = ast.Node.Comptime{ .base = ast.Node{ .id = ast.Node.Id.Comptime }, .comptime_token = token_index, .expr = undefined, .doc_comments = null, - }); + }; ctx.store(&node.base); try stack.append(State{ .Expression = OptionalCtx{ .Required = &node.expr } }); return true; }, Token.Id.LBrace => { - const block = try arena.create(ast.Node.Block{ + const block = try arena.create(ast.Node.Block); + block.* = ast.Node.Block{ .base = ast.Node{ .id = ast.Node.Id.Block }, .label = null, .lbrace = token_index, .statements = ast.Node.Block.StatementList.init(arena), .rbrace = undefined, - }); + }; ctx.store(&block.base); stack.append(State{ .Block = block }) catch unreachable; return true; @@ -3412,10 +3486,12 @@ fn tokenIdToPrefixOp(id: Token.Id) ?ast.Node.PrefixOp.Op { } fn createLiteral(arena: *mem.Allocator, comptime T: type, token_index: TokenIndex) !*T { - return arena.create(T{ + const result = try arena.create(T); + result.* = T{ .base = ast.Node{ .id = ast.Node.typeToId(T) }, .token = token_index, - }); + }; + return result; } fn createToCtxLiteral(arena: *mem.Allocator, opt_ctx: OptionalCtx, comptime T: type, token_index: TokenIndex) !*T { diff --git a/test/tests.zig b/test/tests.zig index 73d4644d18..548496fa2f 100644 --- a/test/tests.zig +++ b/test/tests.zig @@ -48,13 +48,14 @@ const test_targets = []TestTarget{ const max_stdout_size = 1 * 1024 * 1024; // 1 MB pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(CompareOutputContext{ + const cases = b.allocator.create(CompareOutputContext) catch unreachable; + cases.* = CompareOutputContext{ .b = b, .step = b.step("test-compare-output", "Run the compare output tests"), .test_index = 0, .test_filter = test_filter, .modes = modes, - }) catch unreachable; + }; compare_output.addCases(cases); @@ -62,13 +63,14 @@ pub fn addCompareOutputTests(b: *build.Builder, test_filter: ?[]const u8, modes: } pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(CompareOutputContext{ + const cases = b.allocator.create(CompareOutputContext) catch unreachable; + cases.* = CompareOutputContext{ .b = b, .step = b.step("test-runtime-safety", "Run the runtime safety tests"), .test_index = 0, .test_filter = test_filter, .modes = modes, - }) catch unreachable; + }; runtime_safety.addCases(cases); @@ -76,13 +78,14 @@ pub fn addRuntimeSafetyTests(b: *build.Builder, test_filter: ?[]const u8, modes: } pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(CompileErrorContext{ + const cases = b.allocator.create(CompileErrorContext) catch unreachable; + cases.* = CompileErrorContext{ .b = b, .step = b.step("test-compile-errors", "Run the compile error tests"), .test_index = 0, .test_filter = test_filter, .modes = modes, - }) catch unreachable; + }; compile_errors.addCases(cases); @@ -90,13 +93,14 @@ pub fn addCompileErrorTests(b: *build.Builder, test_filter: ?[]const u8, modes: } pub fn addBuildExampleTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(BuildExamplesContext{ + const cases = b.allocator.create(BuildExamplesContext) catch unreachable; + cases.* = BuildExamplesContext{ .b = b, .step = b.step("test-build-examples", "Build the examples"), .test_index = 0, .test_filter = test_filter, .modes = modes, - }) catch unreachable; + }; build_examples.addCases(cases); @@ -119,13 +123,14 @@ pub fn addCliTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const M } pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, modes: []const Mode) *build.Step { - const cases = b.allocator.create(CompareOutputContext{ + const cases = b.allocator.create(CompareOutputContext) catch unreachable; + cases.* = CompareOutputContext{ .b = b, .step = b.step("test-asm-link", "Run the assemble and link tests"), .test_index = 0, .test_filter = test_filter, .modes = modes, - }) catch unreachable; + }; assemble_and_link.addCases(cases); @@ -133,12 +138,13 @@ pub fn addAssembleAndLinkTests(b: *build.Builder, test_filter: ?[]const u8, mode } pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step { - const cases = b.allocator.create(TranslateCContext{ + const cases = b.allocator.create(TranslateCContext) catch unreachable; + cases.* = TranslateCContext{ .b = b, .step = b.step("test-translate-c", "Run the C transation tests"), .test_index = 0, .test_filter = test_filter, - }) catch unreachable; + }; translate_c.addCases(cases); @@ -146,12 +152,13 @@ pub fn addTranslateCTests(b: *build.Builder, test_filter: ?[]const u8) *build.St } pub fn addGenHTests(b: *build.Builder, test_filter: ?[]const u8) *build.Step { - const cases = b.allocator.create(GenHContext{ + const cases = b.allocator.create(GenHContext) catch unreachable; + cases.* = GenHContext{ .b = b, .step = b.step("test-gen-h", "Run the C header file generation tests"), .test_index = 0, .test_filter = test_filter, - }) catch unreachable; + }; gen_h.addCases(cases); @@ -244,7 +251,8 @@ pub const CompareOutputContext = struct { pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8, expected_output: []const u8, cli_args: []const []const u8) *RunCompareOutputStep { const allocator = context.b.allocator; - const ptr = allocator.create(RunCompareOutputStep{ + const ptr = allocator.create(RunCompareOutputStep) catch unreachable; + ptr.* = RunCompareOutputStep{ .context = context, .exe_path = exe_path, .name = name, @@ -252,7 +260,7 @@ pub const CompareOutputContext = struct { .test_index = context.test_index, .step = build.Step.init("RunCompareOutput", allocator, make), .cli_args = cli_args, - }) catch unreachable; + }; context.test_index += 1; return ptr; } @@ -331,13 +339,14 @@ pub const CompareOutputContext = struct { pub fn create(context: *CompareOutputContext, exe_path: []const u8, name: []const u8) *RuntimeSafetyRunStep { const allocator = context.b.allocator; - const ptr = allocator.create(RuntimeSafetyRunStep{ + const ptr = allocator.create(RuntimeSafetyRunStep) catch unreachable; + ptr.* = RuntimeSafetyRunStep{ .context = context, .exe_path = exe_path, .name = name, .test_index = context.test_index, .step = build.Step.init("RuntimeSafetyRun", allocator, make), - }) catch unreachable; + }; context.test_index += 1; return ptr; @@ -542,14 +551,15 @@ pub const CompileErrorContext = struct { pub fn create(context: *CompileErrorContext, name: []const u8, case: *const TestCase, build_mode: Mode) *CompileCmpOutputStep { const allocator = context.b.allocator; - const ptr = allocator.create(CompileCmpOutputStep{ + const ptr = allocator.create(CompileCmpOutputStep) catch unreachable; + ptr.* = CompileCmpOutputStep{ .step = build.Step.init("CompileCmpOutput", allocator, make), .context = context, .name = name, .test_index = context.test_index, .case = case, .build_mode = build_mode, - }) catch unreachable; + }; context.test_index += 1; return ptr; @@ -661,13 +671,14 @@ pub const CompileErrorContext = struct { } pub fn create(self: *CompileErrorContext, name: []const u8, source: []const u8, expected_lines: ...) *TestCase { - const tc = self.b.allocator.create(TestCase{ + const tc = self.b.allocator.create(TestCase) catch unreachable; + tc.* = TestCase{ .name = name, .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), .expected_errors = ArrayList([]const u8).init(self.b.allocator), .link_libc = false, .is_exe = false, - }) catch unreachable; + }; tc.addSourceFile(".tmp_source.zig", source); comptime var arg_i = 0; @@ -821,13 +832,14 @@ pub const TranslateCContext = struct { pub fn create(context: *TranslateCContext, name: []const u8, case: *const TestCase) *TranslateCCmpOutputStep { const allocator = context.b.allocator; - const ptr = allocator.create(TranslateCCmpOutputStep{ + const ptr = allocator.create(TranslateCCmpOutputStep) catch unreachable; + ptr.* = TranslateCCmpOutputStep{ .step = build.Step.init("ParseCCmpOutput", allocator, make), .context = context, .name = name, .test_index = context.test_index, .case = case, - }) catch unreachable; + }; context.test_index += 1; return ptr; @@ -928,12 +940,13 @@ pub const TranslateCContext = struct { } pub fn create(self: *TranslateCContext, allow_warnings: bool, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase { - const tc = self.b.allocator.create(TestCase{ + const tc = self.b.allocator.create(TestCase) catch unreachable; + tc.* = TestCase{ .name = name, .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), .expected_lines = ArrayList([]const u8).init(self.b.allocator), .allow_warnings = allow_warnings, - }) catch unreachable; + }; tc.addSourceFile(filename, source); comptime var arg_i = 0; @@ -1015,14 +1028,15 @@ pub const GenHContext = struct { pub fn create(context: *GenHContext, h_path: []const u8, name: []const u8, case: *const TestCase) *GenHCmpOutputStep { const allocator = context.b.allocator; - const ptr = allocator.create(GenHCmpOutputStep{ + const ptr = allocator.create(GenHCmpOutputStep) catch unreachable; + ptr.* = GenHCmpOutputStep{ .step = build.Step.init("ParseCCmpOutput", allocator, make), .context = context, .h_path = h_path, .name = name, .test_index = context.test_index, .case = case, - }) catch unreachable; + }; context.test_index += 1; return ptr; @@ -1062,11 +1076,12 @@ pub const GenHContext = struct { } pub fn create(self: *GenHContext, filename: []const u8, name: []const u8, source: []const u8, expected_lines: ...) *TestCase { - const tc = self.b.allocator.create(TestCase{ + const tc = self.b.allocator.create(TestCase) catch unreachable; + tc.* = TestCase{ .name = name, .sources = ArrayList(TestCase.SourceFile).init(self.b.allocator), .expected_lines = ArrayList([]const u8).init(self.b.allocator), - }) catch unreachable; + }; tc.addSourceFile(filename, source); comptime var arg_i = 0; -- cgit v1.2.3 From c2db077574be841da586fa62d67619c901dd535d Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Fri, 8 Feb 2019 18:18:47 -0500 Subject: std.debug.assert: remove special case for test builds Previously, std.debug.assert would `@panic` in test builds, if the assertion failed. Now, it's always `unreachable`. This makes release mode test builds more accurately test the actual code that will be run. However this requires tests to call `std.testing.expect` rather than `std.debug.assert` to make sure output is correct. Here is the explanation of when to use either one, copied from the assert doc comments: Inside a test block, it is best to use the `std.testing` module rather than assert, because assert may not detect a test failure in ReleaseFast and ReleaseSafe mode. Outside of a test block, assert is the correct function to use. closes #1304 --- CMakeLists.txt | 1 + doc/docgen.zig | 4 +- src-self-hosted/arg.zig | 23 +- src-self-hosted/test.zig | 4 +- src/ir.cpp | 24 +- std/array_list.zig | 106 +++--- std/atomic/queue.zig | 23 +- std/atomic/stack.zig | 5 +- std/base64.zig | 13 +- std/buf_map.zig | 16 +- std/buf_set.zig | 6 +- std/buffer.zig | 15 +- std/crypto/chacha20.zig | 15 +- std/crypto/poly1305.zig | 2 +- std/crypto/test.zig | 9 +- std/crypto/x25519.zig | 24 +- std/cstr.zig | 6 +- std/debug/index.zig | 38 +-- std/dynamic_library.zig | 3 +- std/event/channel.zig | 9 +- std/event/fs.zig | 9 +- std/event/future.zig | 3 +- std/event/group.zig | 6 +- std/event/lock.zig | 3 +- std/event/loop.zig | 5 +- std/event/net.zig | 4 +- std/event/rwlock.zig | 7 +- std/fmt/index.zig | 158 ++++----- std/hash/adler.zig | 12 +- std/hash/crc.zig | 25 +- std/hash/fnv.zig | 18 +- std/hash/siphash.zig | 15 +- std/hash_map.zig | 59 ++-- std/heap.zig | 55 +-- std/index.zig | 15 +- std/io.zig | 17 +- std/io_test.zig | 202 ++++++------ std/json.zig | 17 +- std/json_test.zig | 6 +- std/lazy_init.zig | 9 +- std/linked_list.zig | 29 +- std/math/acos.zig | 38 +-- std/math/acosh.zig | 30 +- std/math/asin.zig | 46 +-- std/math/asinh.zig | 54 +-- std/math/atan.zig | 42 +-- std/math/atan2.zig | 110 +++--- std/math/atanh.zig | 38 +-- std/math/big/int.zig | 367 ++++++++++----------- std/math/cbrt.zig | 50 +-- std/math/ceil.zig | 38 +-- std/math/complex/abs.zig | 4 +- std/math/complex/acos.zig | 6 +- std/math/complex/acosh.zig | 6 +- std/math/complex/arg.zig | 4 +- std/math/complex/asin.zig | 6 +- std/math/complex/asinh.zig | 6 +- std/math/complex/atan.zig | 10 +- std/math/complex/atanh.zig | 6 +- std/math/complex/conj.zig | 4 +- std/math/complex/cos.zig | 6 +- std/math/complex/cosh.zig | 10 +- std/math/complex/exp.zig | 10 +- std/math/complex/index.zig | 16 +- std/math/complex/log.zig | 6 +- std/math/complex/pow.zig | 6 +- std/math/complex/proj.zig | 4 +- std/math/complex/sin.zig | 6 +- std/math/complex/sinh.zig | 10 +- std/math/complex/sqrt.zig | 10 +- std/math/complex/tan.zig | 6 +- std/math/complex/tanh.zig | 10 +- std/math/copysign.zig | 32 +- std/math/cos.zig | 42 +-- std/math/cosh.zig | 42 +-- std/math/exp2.zig | 32 +- std/math/expm1.zig | 38 +-- std/math/fabs.zig | 38 +-- std/math/floor.zig | 56 ++-- std/math/fma.zig | 34 +- std/math/frexp.zig | 34 +- std/math/hypot.zig | 58 ++-- std/math/ilogb.zig | 46 +-- std/math/index.zig | 343 +++++++++---------- std/math/isfinite.zig | 26 +- std/math/isinf.zig | 74 ++--- std/math/isnan.zig | 14 +- std/math/isnormal.zig | 20 +- std/math/ln.zig | 46 +-- std/math/log.zig | 26 +- std/math/log10.zig | 46 +-- std/math/log1p.zig | 58 ++-- std/math/log2.zig | 42 +-- std/math/modf.zig | 58 ++-- std/math/pow.zig | 96 +++--- std/math/powi.zig | 138 ++++---- std/math/round.zig | 42 +-- std/math/scalbn.zig | 10 +- std/math/signbit.zig | 20 +- std/math/sin.zig | 52 +-- std/math/sinh.zig | 42 +-- std/math/sqrt.zig | 104 +++--- std/math/tan.zig | 50 +-- std/math/tanh.zig | 46 +-- std/math/trunc.zig | 38 +-- std/mem.zig | 319 +++++++++--------- std/meta/index.zig | 151 ++++----- std/meta/trait.zig | 133 ++++---- std/mutex.zig | 6 +- std/os/child_process.zig | 1 - std/os/index.zig | 9 +- std/os/linux/test.zig | 12 +- std/os/path.zig | 115 +++---- std/os/test.zig | 16 +- std/os/time.zig | 9 +- std/rand/index.zig | 161 ++++----- std/rb.zig | 5 +- std/segmented_list.zig | 31 +- std/sort.zig | 17 +- std/special/compiler_rt/divti3_test.zig | 4 +- std/special/compiler_rt/extendXfYf2_test.zig | 1 - std/special/compiler_rt/fixdfdi_test.zig | 4 +- std/special/compiler_rt/fixdfsi_test.zig | 4 +- std/special/compiler_rt/fixdfti_test.zig | 4 +- std/special/compiler_rt/fixint_test.zig | 4 +- std/special/compiler_rt/fixsfdi_test.zig | 4 +- std/special/compiler_rt/fixsfsi_test.zig | 4 +- std/special/compiler_rt/fixsfti_test.zig | 4 +- std/special/compiler_rt/fixtfdi_test.zig | 4 +- std/special/compiler_rt/fixtfsi_test.zig | 4 +- std/special/compiler_rt/fixtfti_test.zig | 4 +- std/special/compiler_rt/fixunsdfdi_test.zig | 4 +- std/special/compiler_rt/fixunsdfsi_test.zig | 4 +- std/special/compiler_rt/fixunsdfti_test.zig | 4 +- std/special/compiler_rt/fixunssfdi_test.zig | 4 +- std/special/compiler_rt/fixunssfsi_test.zig | 4 +- std/special/compiler_rt/fixunssfti_test.zig | 4 +- std/special/compiler_rt/fixunstfdi_test.zig | 4 +- std/special/compiler_rt/fixunstfsi_test.zig | 4 +- std/special/compiler_rt/fixunstfti_test.zig | 4 +- std/special/compiler_rt/floattidf_test.zig | 4 +- std/special/compiler_rt/floattisf_test.zig | 4 +- std/special/compiler_rt/floattitf_test.zig | 4 +- std/special/compiler_rt/floatunditf_test.zig | 1 - std/special/compiler_rt/floatunsitf_test.zig | 1 - std/special/compiler_rt/floatuntidf_test.zig | 4 +- std/special/compiler_rt/floatuntisf_test.zig | 4 +- std/special/compiler_rt/floatuntitf_test.zig | 4 +- std/special/compiler_rt/index.zig | 5 +- std/special/compiler_rt/muloti4_test.zig | 4 +- std/special/compiler_rt/multi3_test.zig | 4 +- std/special/compiler_rt/udivmoddi4_test.zig | 6 +- std/special/compiler_rt/udivmodti4_test.zig | 6 +- std/special/init-lib/src/main.zig | 4 +- std/statically_initialized_mutex.zig | 5 +- std/testing.zig | 152 +++++++++ std/unicode.zig | 119 +++---- std/zig/ast.zig | 3 +- std/zig/parser_test.zig | 2 +- std/zig/tokenizer.zig | 2 +- test/cli.zig | 10 +- test/stage1/behavior/align.zig | 72 ++-- test/stage1/behavior/alignof.zig | 6 +- test/stage1/behavior/array.zig | 110 +++--- test/stage1/behavior/asm.zig | 4 +- test/stage1/behavior/atomics.zig | 32 +- test/stage1/behavior/bit_shifting.zig | 10 +- test/stage1/behavior/bitcast.zig | 8 +- test/stage1/behavior/bitreverse.zig | 86 ++--- test/stage1/behavior/bool.zig | 20 +- test/stage1/behavior/bswap.zig | 42 +-- test/stage1/behavior/bugs/1076.zig | 4 +- test/stage1/behavior/bugs/1277.zig | 2 +- test/stage1/behavior/bugs/1322.zig | 4 +- test/stage1/behavior/bugs/1381.zig | 2 +- test/stage1/behavior/bugs/1421.zig | 4 +- test/stage1/behavior/bugs/1442.zig | 2 +- test/stage1/behavior/bugs/1486.zig | 6 +- test/stage1/behavior/bugs/394.zig | 4 +- test/stage1/behavior/bugs/655.zig | 4 +- test/stage1/behavior/bugs/656.zig | 4 +- test/stage1/behavior/bugs/726.zig | 6 +- test/stage1/behavior/bugs/920.zig | 2 +- test/stage1/behavior/byval_arg_var.zig | 2 +- test/stage1/behavior/cancel.zig | 14 +- test/stage1/behavior/cast.zig | 158 ++++----- test/stage1/behavior/const_slice_child.zig | 13 +- test/stage1/behavior/coroutine_await_struct.zig | 6 +- test/stage1/behavior/coroutines.zig | 36 +- test/stage1/behavior/defer.zig | 24 +- test/stage1/behavior/enum.zig | 74 ++--- test/stage1/behavior/enum_with_members.zig | 10 +- test/stage1/behavior/error.zig | 62 ++-- test/stage1/behavior/eval.zig | 232 ++++++------- test/stage1/behavior/field_parent_ptr.zig | 14 +- test/stage1/behavior/fn.zig | 38 +-- test/stage1/behavior/fn_in_struct_in_comptime.zig | 4 +- test/stage1/behavior/for.zig | 10 +- test/stage1/behavior/generics.zig | 46 +-- test/stage1/behavior/if.zig | 4 +- test/stage1/behavior/import.zig | 6 +- .../behavior/incomplete_struct_param_tld.zig | 4 +- test/stage1/behavior/inttoptr.zig | 2 +- test/stage1/behavior/ir_block_deps.zig | 6 +- test/stage1/behavior/math.zig | 262 +++++++-------- test/stage1/behavior/misc.zig | 276 ++++++++-------- .../namespace_depends_on_compile_var/index.zig | 6 +- test/stage1/behavior/new_stack_call.zig | 10 +- test/stage1/behavior/null.zig | 34 +- test/stage1/behavior/optional.zig | 28 +- test/stage1/behavior/pointers.zig | 24 +- test/stage1/behavior/popcount.zig | 10 +- test/stage1/behavior/ptrcast.zig | 8 +- test/stage1/behavior/pub_enum/index.zig | 6 +- .../ref_var_in_if_after_if_2nd_switch_prong.zig | 10 +- test/stage1/behavior/reflection.zig | 78 ++--- test/stage1/behavior/sizeof_and_typeof.zig | 60 ++-- test/stage1/behavior/slice.zig | 16 +- test/stage1/behavior/struct.zig | 184 +++++------ .../behavior/struct_contains_null_ptr_itself.zig | 4 +- .../behavior/struct_contains_slice_of_itself.zig | 26 +- test/stage1/behavior/switch.zig | 66 ++-- test/stage1/behavior/switch_prong_err_enum.zig | 6 +- .../stage1/behavior/switch_prong_implicit_cast.zig | 4 +- test/stage1/behavior/this.zig | 8 +- test/stage1/behavior/truncate.zig | 4 +- test/stage1/behavior/try.zig | 10 +- test/stage1/behavior/type_info.zig | 182 +++++----- test/stage1/behavior/undefined.zig | 28 +- test/stage1/behavior/underscore.zig | 2 +- test/stage1/behavior/union.zig | 76 ++--- test/stage1/behavior/var_args.zig | 34 +- test/stage1/behavior/vector.zig | 8 +- test/stage1/behavior/void.zig | 8 +- test/stage1/behavior/while.zig | 44 +-- test/stage1/behavior/widening.zig | 8 +- test/stage1/c_abi/main.zig | 82 ++--- test/standalone/brace_expansion/main.zig | 9 +- test/standalone/issue_794/main.zig | 4 +- 239 files changed, 4106 insertions(+), 3960 deletions(-) create mode 100644 std/testing.zig (limited to 'std/atomic/stack.zig') diff --git a/CMakeLists.txt b/CMakeLists.txt index d6bd8a6c2e..ed79f99901 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -658,6 +658,7 @@ set(ZIG_STD_FILES "special/test_runner.zig" "spinlock.zig" "statically_initialized_mutex.zig" + "testing.zig" "unicode.zig" "zig/ast.zig" "zig/index.zig" diff --git a/doc/docgen.zig b/doc/docgen.zig index 14e4700553..7aaf5ebdc7 100644 --- a/doc/docgen.zig +++ b/doc/docgen.zig @@ -4,7 +4,7 @@ const io = std.io; const os = std.os; const warn = std.debug.warn; const mem = std.mem; -const assert = std.debug.assert; +const testing = std.testing; const max_doc_file_size = 10 * 1024 * 1024; @@ -620,7 +620,7 @@ const TermState = enum { test "term color" { const input_bytes = "A\x1b[32;1mgreen\x1b[0mB"; const result = try termColor(std.debug.global_allocator, input_bytes); - assert(mem.eql(u8, result, "AgreenB")); + testing.expectEqualSlices(u8, "AgreenB", result)); } fn termColor(allocator: *mem.Allocator, input: []const u8) ![]u8 { diff --git a/src-self-hosted/arg.zig b/src-self-hosted/arg.zig index 99e6ecc17a..7bbd233a75 100644 --- a/src-self-hosted/arg.zig +++ b/src-self-hosted/arg.zig @@ -1,5 +1,6 @@ const std = @import("std"); const debug = std.debug; +const testing = std.testing; const mem = std.mem; const Allocator = mem.Allocator; @@ -272,21 +273,21 @@ test "parse arguments" { var args = try Args.parse(std.debug.global_allocator, spec1, cliargs); - debug.assert(args.present("help")); - debug.assert(!args.present("help2")); - debug.assert(!args.present("init")); + testing.expect(args.present("help")); + testing.expect(!args.present("help2")); + testing.expect(!args.present("init")); - debug.assert(mem.eql(u8, args.single("build-file").?, "build.zig")); - debug.assert(mem.eql(u8, args.single("color").?, "on")); + testing.expect(mem.eql(u8, args.single("build-file").?, "build.zig")); + testing.expect(mem.eql(u8, args.single("color").?, "on")); const objects = args.many("object").?; - debug.assert(mem.eql(u8, objects[0], "obj1")); - debug.assert(mem.eql(u8, objects[1], "obj2")); + testing.expect(mem.eql(u8, objects[0], "obj1")); + testing.expect(mem.eql(u8, objects[1], "obj2")); - debug.assert(mem.eql(u8, args.single("library").?, "lib2")); + testing.expect(mem.eql(u8, args.single("library").?, "lib2")); const pos = args.positionals.toSliceConst(); - debug.assert(mem.eql(u8, pos[0], "build")); - debug.assert(mem.eql(u8, pos[1], "pos1")); - debug.assert(mem.eql(u8, pos[2], "pos2")); + testing.expect(mem.eql(u8, pos[0], "build")); + testing.expect(mem.eql(u8, pos[1], "pos1")); + testing.expect(mem.eql(u8, pos[2], "pos2")); } diff --git a/src-self-hosted/test.zig b/src-self-hosted/test.zig index de551cf7f7..4be6d53932 100644 --- a/src-self-hosted/test.zig +++ b/src-self-hosted/test.zig @@ -4,7 +4,7 @@ const builtin = @import("builtin"); const Target = @import("target.zig").Target; const Compilation = @import("compilation.zig").Compilation; const introspect = @import("introspect.zig"); -const assertOrPanic = std.debug.assertOrPanic; +const testing = std.testing; const errmsg = @import("errmsg.zig"); const ZigCompiler = @import("compilation.zig").ZigCompiler; @@ -210,7 +210,7 @@ pub const TestContext = struct { @panic("build incorrectly failed"); }, Compilation.Event.Fail => |msgs| { - assertOrPanic(msgs.len != 0); + testing.expect(msgs.len != 0); for (msgs) |msg| { if (mem.endsWith(u8, msg.realpath, path) and mem.eql(u8, msg.text, text)) { const span = msg.getSpan(); diff --git a/src/ir.cpp b/src/ir.cpp index 00d358552a..efda2b321b 100644 --- a/src/ir.cpp +++ b/src/ir.cpp @@ -17543,21 +17543,16 @@ static void make_enum_field_val(IrAnalyze *ira, ConstExprValue *enum_field_val, enum_field_val->data.x_struct.fields = inner_fields; } -static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstExprValue **out) { +static Error ir_make_type_info_value(IrAnalyze *ira, AstNode *source_node, ZigType *type_entry, ConstExprValue **out) { Error err; assert(type_entry != nullptr); assert(!type_is_invalid(type_entry)); - if ((err = ensure_complete_type(ira->codegen, type_entry))) + if ((err = type_resolve(ira->codegen, type_entry, ResolveStatusSizeKnown))) return err; - if (type_entry == ira->codegen->builtin_types.entry_global_error_set) { - zig_panic("TODO implement @typeInfo for global error set"); - } - ConstExprValue *result = nullptr; - switch (type_entry->id) - { + switch (type_entry->id) { case ZigTypeIdInvalid: zig_unreachable(); case ZigTypeIdMetaType: @@ -17778,6 +17773,15 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE ensure_field_index(result->type, "errors", 0); ZigType *type_info_error_type = ir_type_info_get_type(ira, "Error", nullptr); + if (!resolve_inferred_error_set(ira->codegen, type_entry, source_node)) { + return ErrorSemanticAnalyzeFail; + } + if (type_is_global_error_set(type_entry)) { + ir_add_error_node(ira, source_node, + buf_sprintf("TODO: compiler bug: implement @typeInfo support for anyerror. https://github.com/ziglang/zig/issues/1936")); + return ErrorSemanticAnalyzeFail; + } + uint32_t error_count = type_entry->data.error_set.err_count; ConstExprValue *error_array = create_const_vals(1); error_array->special = ConstValSpecialStatic; @@ -18103,7 +18107,7 @@ static Error ir_make_type_info_value(IrAnalyze *ira, ZigType *type_entry, ConstE { ZigType *fn_type = type_entry->data.bound_fn.fn_type; assert(fn_type->id == ZigTypeIdFn); - if ((err = ir_make_type_info_value(ira, fn_type, &result))) + if ((err = ir_make_type_info_value(ira, source_node, fn_type, &result))) return err; break; @@ -18128,7 +18132,7 @@ static IrInstruction *ir_analyze_instruction_type_info(IrAnalyze *ira, ZigType *result_type = ir_type_info_get_type(ira, nullptr, nullptr); ConstExprValue *payload; - if ((err = ir_make_type_info_value(ira, type_entry, &payload))) + if ((err = ir_make_type_info_value(ira, instruction->base.source_node, type_entry, &payload))) return ira->codegen->invalid_instruction; IrInstruction *result = ir_const(ira, &instruction->base, result_type); diff --git a/std/array_list.zig b/std/array_list.zig index ddad9c989c..e2535d6393 100644 --- a/std/array_list.zig +++ b/std/array_list.zig @@ -1,7 +1,7 @@ const std = @import("index.zig"); const debug = std.debug; const assert = debug.assert; -const assertError = debug.assertError; +const testing = std.testing; const mem = std.mem; const Allocator = mem.Allocator; @@ -212,8 +212,8 @@ test "std.ArrayList.init" { var list = ArrayList(i32).init(allocator); defer list.deinit(); - assert(list.count() == 0); - assert(list.capacity() == 0); + testing.expect(list.count() == 0); + testing.expect(list.capacity() == 0); } test "std.ArrayList.basic" { @@ -224,7 +224,7 @@ test "std.ArrayList.basic" { defer list.deinit(); // setting on empty list is out of bounds - assertError(list.setOrError(0, 1), error.OutOfBounds); + testing.expectError(error.OutOfBounds, list.setOrError(0, 1)); { var i: usize = 0; @@ -236,44 +236,44 @@ test "std.ArrayList.basic" { { var i: usize = 0; while (i < 10) : (i += 1) { - assert(list.items[i] == @intCast(i32, i + 1)); + testing.expect(list.items[i] == @intCast(i32, i + 1)); } } for (list.toSlice()) |v, i| { - assert(v == @intCast(i32, i + 1)); + testing.expect(v == @intCast(i32, i + 1)); } for (list.toSliceConst()) |v, i| { - assert(v == @intCast(i32, i + 1)); + testing.expect(v == @intCast(i32, i + 1)); } - assert(list.pop() == 10); - assert(list.len == 9); + testing.expect(list.pop() == 10); + testing.expect(list.len == 9); list.appendSlice([]const i32{ 1, 2, 3, }) catch unreachable; - assert(list.len == 12); - assert(list.pop() == 3); - assert(list.pop() == 2); - assert(list.pop() == 1); - assert(list.len == 9); + testing.expect(list.len == 12); + testing.expect(list.pop() == 3); + testing.expect(list.pop() == 2); + testing.expect(list.pop() == 1); + testing.expect(list.len == 9); list.appendSlice([]const i32{}) catch unreachable; - assert(list.len == 9); + testing.expect(list.len == 9); // can only set on indices < self.len list.set(7, 33); list.set(8, 42); - assertError(list.setOrError(9, 99), error.OutOfBounds); - assertError(list.setOrError(10, 123), error.OutOfBounds); + testing.expectError(error.OutOfBounds, list.setOrError(9, 99)); + testing.expectError(error.OutOfBounds, list.setOrError(10, 123)); - assert(list.pop() == 42); - assert(list.pop() == 33); + testing.expect(list.pop() == 42); + testing.expect(list.pop() == 33); } test "std.ArrayList.swapRemove" { @@ -289,18 +289,18 @@ test "std.ArrayList.swapRemove" { try list.append(7); //remove from middle - assert(list.swapRemove(3) == 4); - assert(list.at(3) == 7); - assert(list.len == 6); + testing.expect(list.swapRemove(3) == 4); + testing.expect(list.at(3) == 7); + testing.expect(list.len == 6); //remove from end - assert(list.swapRemove(5) == 6); - assert(list.len == 5); + testing.expect(list.swapRemove(5) == 6); + testing.expect(list.len == 5); //remove from front - assert(list.swapRemove(0) == 1); - assert(list.at(0) == 5); - assert(list.len == 4); + testing.expect(list.swapRemove(0) == 1); + testing.expect(list.at(0) == 5); + testing.expect(list.len == 4); } test "std.ArrayList.swapRemoveOrError" { @@ -308,27 +308,27 @@ test "std.ArrayList.swapRemoveOrError" { defer list.deinit(); // Test just after initialization - assertError(list.swapRemoveOrError(0), error.OutOfBounds); + testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0)); // Test after adding one item and remote it try list.append(1); - assert((try list.swapRemoveOrError(0)) == 1); - assertError(list.swapRemoveOrError(0), error.OutOfBounds); + testing.expect((try list.swapRemoveOrError(0)) == 1); + testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0)); // Test after adding two items and remote both try list.append(1); try list.append(2); - assert((try list.swapRemoveOrError(1)) == 2); - assert((try list.swapRemoveOrError(0)) == 1); - assertError(list.swapRemoveOrError(0), error.OutOfBounds); + testing.expect((try list.swapRemoveOrError(1)) == 2); + testing.expect((try list.swapRemoveOrError(0)) == 1); + testing.expectError(error.OutOfBounds, list.swapRemoveOrError(0)); // Test out of bounds with one item try list.append(1); - assertError(list.swapRemoveOrError(1), error.OutOfBounds); + testing.expectError(error.OutOfBounds, list.swapRemoveOrError(1)); // Test out of bounds with two items try list.append(2); - assertError(list.swapRemoveOrError(2), error.OutOfBounds); + testing.expectError(error.OutOfBounds, list.swapRemoveOrError(2)); } test "std.ArrayList.iterator" { @@ -342,22 +342,22 @@ test "std.ArrayList.iterator" { var count: i32 = 0; var it = list.iterator(); while (it.next()) |next| { - assert(next == count + 1); + testing.expect(next == count + 1); count += 1; } - assert(count == 3); - assert(it.next() == null); + testing.expect(count == 3); + testing.expect(it.next() == null); it.reset(); count = 0; while (it.next()) |next| { - assert(next == count + 1); + testing.expect(next == count + 1); count += 1; if (count == 2) break; } it.reset(); - assert(it.next().? == 1); + testing.expect(it.next().? == 1); } test "std.ArrayList.insert" { @@ -368,10 +368,10 @@ test "std.ArrayList.insert" { try list.append(2); try list.append(3); try list.insert(0, 5); - assert(list.items[0] == 5); - assert(list.items[1] == 1); - assert(list.items[2] == 2); - assert(list.items[3] == 3); + testing.expect(list.items[0] == 5); + testing.expect(list.items[1] == 1); + testing.expect(list.items[2] == 2); + testing.expect(list.items[3] == 3); } test "std.ArrayList.insertSlice" { @@ -386,17 +386,17 @@ test "std.ArrayList.insertSlice" { 9, 8, }); - assert(list.items[0] == 1); - assert(list.items[1] == 9); - assert(list.items[2] == 8); - assert(list.items[3] == 2); - assert(list.items[4] == 3); - assert(list.items[5] == 4); + testing.expect(list.items[0] == 1); + testing.expect(list.items[1] == 9); + testing.expect(list.items[2] == 8); + testing.expect(list.items[3] == 2); + testing.expect(list.items[4] == 3); + testing.expect(list.items[5] == 4); const items = []const i32{1}; try list.insertSlice(0, items[0..0]); - assert(list.len == 6); - assert(list.items[0] == 1); + testing.expect(list.len == 6); + testing.expect(list.items[0] == 1); } const Item = struct { @@ -407,5 +407,5 @@ const Item = struct { test "std.ArrayList: ArrayList(T) of struct T" { var root = Item{ .integer = 1, .sub_items = ArrayList(Item).init(debug.global_allocator) }; try root.sub_items.append( Item{ .integer = 42, .sub_items = ArrayList(Item).init(debug.global_allocator) } ); - assert(root.sub_items.items[0].integer == 42); + testing.expect(root.sub_items.items[0].integer == 42); } diff --git a/std/atomic/queue.zig b/std/atomic/queue.zig index 183c434dc5..bdc86c0981 100644 --- a/std/atomic/queue.zig +++ b/std/atomic/queue.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const AtomicOrder = builtin.AtomicOrder; const AtomicRmwOp = builtin.AtomicRmwOp; const assert = std.debug.assert; +const expect = std.testing.expect; /// Many producer, many consumer, non-allocating, thread-safe. /// Uses a mutex to protect access. @@ -174,14 +175,14 @@ test "std.atomic.Queue" { { var i: usize = 0; while (i < put_thread_count) : (i += 1) { - std.debug.assertOrPanic(startPuts(&context) == 0); + expect(startPuts(&context) == 0); } } context.puts_done = 1; { var i: usize = 0; while (i < put_thread_count) : (i += 1) { - std.debug.assertOrPanic(startGets(&context) == 0); + expect(startGets(&context) == 0); } } } else { @@ -264,7 +265,7 @@ test "std.atomic.Queue single-threaded" { }; queue.put(&node_1); - assert(queue.get().?.data == 0); + expect(queue.get().?.data == 0); var node_2 = Queue(i32).Node{ .data = 2, @@ -280,9 +281,9 @@ test "std.atomic.Queue single-threaded" { }; queue.put(&node_3); - assert(queue.get().?.data == 1); + expect(queue.get().?.data == 1); - assert(queue.get().?.data == 2); + expect(queue.get().?.data == 2); var node_4 = Queue(i32).Node{ .data = 4, @@ -291,12 +292,12 @@ test "std.atomic.Queue single-threaded" { }; queue.put(&node_4); - assert(queue.get().?.data == 3); + expect(queue.get().?.data == 3); node_3.next = null; - assert(queue.get().?.data == 4); + expect(queue.get().?.data == 4); - assert(queue.get() == null); + expect(queue.get() == null); } test "std.atomic.Queue dump" { @@ -311,7 +312,7 @@ test "std.atomic.Queue dump" { // Test empty stream sos.reset(); try queue.dumpToStream(SliceOutStream.Error, &sos.stream); - assert(mem.eql(u8, buffer[0..sos.pos], + expect(mem.eql(u8, buffer[0..sos.pos], \\head: (null) \\tail: (null) \\ @@ -335,7 +336,7 @@ test "std.atomic.Queue dump" { \\ (null) \\ , @ptrToInt(queue.head), @ptrToInt(queue.tail)); - assert(mem.eql(u8, buffer[0..sos.pos], expected)); + expect(mem.eql(u8, buffer[0..sos.pos], expected)); // Test a stream with two elements var node_1 = Queue(i32).Node{ @@ -356,5 +357,5 @@ test "std.atomic.Queue dump" { \\ (null) \\ , @ptrToInt(queue.head), @ptrToInt(queue.head.?.next), @ptrToInt(queue.tail)); - assert(mem.eql(u8, buffer[0..sos.pos], expected)); + expect(mem.eql(u8, buffer[0..sos.pos], expected)); } diff --git a/std/atomic/stack.zig b/std/atomic/stack.zig index 503fa0c0ce..4d0d5075e0 100644 --- a/std/atomic/stack.zig +++ b/std/atomic/stack.zig @@ -1,6 +1,7 @@ const assert = std.debug.assert; const builtin = @import("builtin"); const AtomicOrder = builtin.AtomicOrder; +const expect = std.testing.expect; /// Many reader, many writer, non-allocating, thread-safe /// Uses a spinlock to protect push() and pop() @@ -108,14 +109,14 @@ test "std.atomic.stack" { { var i: usize = 0; while (i < put_thread_count) : (i += 1) { - std.debug.assertOrPanic(startPuts(&context) == 0); + expect(startPuts(&context) == 0); } } context.puts_done = 1; { var i: usize = 0; while (i < put_thread_count) : (i += 1) { - std.debug.assertOrPanic(startGets(&context) == 0); + expect(startGets(&context) == 0); } } } else { diff --git a/std/base64.zig b/std/base64.zig index bc0bdb1bd3..cfe8ea95f8 100644 --- a/std/base64.zig +++ b/std/base64.zig @@ -1,5 +1,6 @@ const std = @import("index.zig"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; pub const standard_alphabet_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; @@ -394,7 +395,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void var buffer: [0x100]u8 = undefined; var encoded = buffer[0..Base64Encoder.calcSize(expected_decoded.len)]; standard_encoder.encode(encoded, expected_decoded); - assert(mem.eql(u8, encoded, expected_encoded)); + testing.expectEqualSlices(u8, expected_encoded, encoded); } // Base64Decoder @@ -402,7 +403,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void var buffer: [0x100]u8 = undefined; var decoded = buffer[0..try standard_decoder.calcSize(expected_encoded)]; try standard_decoder.decode(decoded, expected_encoded); - assert(mem.eql(u8, decoded, expected_decoded)); + testing.expectEqualSlices(u8, expected_decoded, decoded); } // Base64DecoderWithIgnore @@ -411,8 +412,8 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void var buffer: [0x100]u8 = undefined; var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(expected_encoded.len)]; var written = try standard_decoder_ignore_nothing.decode(decoded, expected_encoded); - assert(written <= decoded.len); - assert(mem.eql(u8, decoded[0..written], expected_decoded)); + testing.expect(written <= decoded.len); + testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); } // Base64DecoderUnsafe @@ -420,7 +421,7 @@ fn testAllApis(expected_decoded: []const u8, expected_encoded: []const u8) !void var buffer: [0x100]u8 = undefined; var decoded = buffer[0..standard_decoder_unsafe.calcSize(expected_encoded)]; standard_decoder_unsafe.decode(decoded, expected_encoded); - assert(mem.eql(u8, decoded, expected_decoded)); + testing.expectEqualSlices(u8, expected_decoded, decoded); } } @@ -429,7 +430,7 @@ fn testDecodeIgnoreSpace(expected_decoded: []const u8, encoded: []const u8) !voi var buffer: [0x100]u8 = undefined; var decoded = buffer[0..Base64DecoderWithIgnore.calcSizeUpperBound(encoded.len)]; var written = try standard_decoder_ignore_space.decode(decoded, encoded); - assert(mem.eql(u8, decoded[0..written], expected_decoded)); + testing.expectEqualSlices(u8, expected_decoded, decoded[0..written]); } fn testError(encoded: []const u8, expected_err: anyerror) !void { diff --git a/std/buf_map.zig b/std/buf_map.zig index 6de0d20cdb..f8d3d5e04d 100644 --- a/std/buf_map.zig +++ b/std/buf_map.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const HashMap = std.HashMap; const mem = std.mem; const Allocator = mem.Allocator; -const assert = std.debug.assert; +const testing = std.testing; /// BufMap copies keys and values before they go into the map, and /// frees them when they get removed. @@ -90,17 +90,17 @@ test "BufMap" { defer bufmap.deinit(); try bufmap.set("x", "1"); - assert(mem.eql(u8, bufmap.get("x").?, "1")); - assert(1 == bufmap.count()); + testing.expect(mem.eql(u8, bufmap.get("x").?, "1")); + testing.expect(1 == bufmap.count()); try bufmap.set("x", "2"); - assert(mem.eql(u8, bufmap.get("x").?, "2")); - assert(1 == bufmap.count()); + testing.expect(mem.eql(u8, bufmap.get("x").?, "2")); + testing.expect(1 == bufmap.count()); try bufmap.set("x", "3"); - assert(mem.eql(u8, bufmap.get("x").?, "3")); - assert(1 == bufmap.count()); + testing.expect(mem.eql(u8, bufmap.get("x").?, "3")); + testing.expect(1 == bufmap.count()); bufmap.delete("x"); - assert(0 == bufmap.count()); + testing.expect(0 == bufmap.count()); } diff --git a/std/buf_set.zig b/std/buf_set.zig index ab2d8e7c34..7ccd94c179 100644 --- a/std/buf_set.zig +++ b/std/buf_set.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const HashMap = @import("hash_map.zig").HashMap; const mem = @import("mem.zig"); const Allocator = mem.Allocator; -const assert = std.debug.assert; +const testing = std.testing; pub const BufSet = struct { hash_map: BufSetHashMap, @@ -68,9 +68,9 @@ test "BufSet" { defer bufset.deinit(); try bufset.put("x"); - assert(bufset.count() == 1); + testing.expect(bufset.count() == 1); bufset.delete("x"); - assert(bufset.count() == 0); + testing.expect(bufset.count() == 0); try bufset.put("x"); try bufset.put("y"); diff --git a/std/buffer.zig b/std/buffer.zig index 2b71c26749..371655f1e5 100644 --- a/std/buffer.zig +++ b/std/buffer.zig @@ -3,6 +3,7 @@ const debug = std.debug; const mem = std.mem; const Allocator = mem.Allocator; const assert = debug.assert; +const testing = std.testing; const ArrayList = std.ArrayList; /// A buffer that allocates memory and maintains a null byte at the end. @@ -141,19 +142,19 @@ test "simple Buffer" { const cstr = @import("cstr.zig"); var buf = try Buffer.init(debug.global_allocator, ""); - assert(buf.len() == 0); + testing.expect(buf.len() == 0); try buf.append("hello"); try buf.append(" "); try buf.append("world"); - assert(buf.eql("hello world")); - assert(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst())); + testing.expect(buf.eql("hello world")); + testing.expect(mem.eql(u8, cstr.toSliceConst(buf.toSliceConst().ptr), buf.toSliceConst())); var buf2 = try Buffer.initFromBuffer(buf); - assert(buf.eql(buf2.toSliceConst())); + testing.expect(buf.eql(buf2.toSliceConst())); - assert(buf.startsWith("hell")); - assert(buf.endsWith("orld")); + testing.expect(buf.startsWith("hell")); + testing.expect(buf.endsWith("orld")); try buf2.resize(4); - assert(buf.startsWith(buf2.toSlice())); + testing.expect(buf.startsWith(buf2.toSlice())); } diff --git a/std/crypto/chacha20.zig b/std/crypto/chacha20.zig index 5ec1e79756..d964f4c03d 100644 --- a/std/crypto/chacha20.zig +++ b/std/crypto/chacha20.zig @@ -4,6 +4,7 @@ const std = @import("../index.zig"); const mem = std.mem; const endian = std.endian; const assert = std.debug.assert; +const testing = std.testing; const builtin = @import("builtin"); const maxInt = std.math.maxInt; @@ -216,12 +217,12 @@ test "crypto.chacha20 test vector sunscreen" { }; chaCha20IETF(result[0..], input[0..], 1, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); // Chacha20 is self-reversing. var plaintext: [114]u8 = undefined; chaCha20IETF(plaintext[0..], result[0..], 1, key, nonce); - assert(mem.compare(u8, input, plaintext) == mem.Compare.Equal); + testing.expect(mem.compare(u8, input, plaintext) == mem.Compare.Equal); } // https://tools.ietf.org/html/draft-agl-tls-chacha20poly1305-04#section-7 @@ -256,7 +257,7 @@ test "crypto.chacha20 test vector 1" { const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 }; chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); } test "crypto.chacha20 test vector 2" { @@ -290,7 +291,7 @@ test "crypto.chacha20 test vector 2" { const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 0 }; chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); } test "crypto.chacha20 test vector 3" { @@ -324,7 +325,7 @@ test "crypto.chacha20 test vector 3" { const nonce = []u8{ 0, 0, 0, 0, 0, 0, 0, 1 }; chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); } test "crypto.chacha20 test vector 4" { @@ -358,7 +359,7 @@ test "crypto.chacha20 test vector 4" { const nonce = []u8{ 1, 0, 0, 0, 0, 0, 0, 0 }; chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); } test "crypto.chacha20 test vector 5" { @@ -430,5 +431,5 @@ test "crypto.chacha20 test vector 5" { }; chaCha20With64BitNonce(result[0..], input[0..], 0, key, nonce); - assert(mem.eql(u8, expected_result, result)); + testing.expectEqualSlices(u8, expected_result, result); } diff --git a/std/crypto/poly1305.zig b/std/crypto/poly1305.zig index 0d7a4d672d..64adb17c45 100644 --- a/std/crypto/poly1305.zig +++ b/std/crypto/poly1305.zig @@ -230,5 +230,5 @@ test "poly1305 rfc7439 vector1" { var mac: [16]u8 = undefined; Poly1305.create(mac[0..], msg, key); - std.debug.assert(std.mem.eql(u8, mac, expected_mac)); + std.testing.expectEqualSlices(u8, expected_mac, mac); } diff --git a/std/crypto/test.zig b/std/crypto/test.zig index 3fa24272e5..258cdf7320 100644 --- a/std/crypto/test.zig +++ b/std/crypto/test.zig @@ -1,6 +1,7 @@ -const debug = @import("../debug/index.zig"); -const mem = @import("../mem.zig"); -const fmt = @import("../fmt/index.zig"); +const std = @import("../index.zig"); +const testing = std.testing; +const mem = std.mem; +const fmt = std.fmt; // Hash using the specified hasher `H` asserting `expected == H(input)`. pub fn assertEqualHash(comptime Hasher: var, comptime expected: []const u8, input: []const u8) void { @@ -17,5 +18,5 @@ pub fn assertEqual(comptime expected: []const u8, input: []const u8) void { r.* = fmt.parseInt(u8, expected[2 * i .. 2 * i + 2], 16) catch unreachable; } - debug.assert(mem.eql(u8, expected_bytes, input)); + testing.expectEqualSlices(u8, expected_bytes, input); } diff --git a/std/crypto/x25519.zig b/std/crypto/x25519.zig index daccb56808..9349569f97 100644 --- a/std/crypto/x25519.zig +++ b/std/crypto/x25519.zig @@ -581,8 +581,8 @@ test "x25519 public key calculation from secret key" { var pk_calculated: [32]u8 = undefined; try fmt.hexToBytes(sk[0..], "8052030376d47112be7f73ed7a019293dd12ad910b654455798b4667d73de166"); try fmt.hexToBytes(pk_expected[0..], "f1814f0e8ff1043d8a44d25babff3cedcae6c22c3edaa48f857ae70de2baae50"); - std.debug.assert(X25519.createPublicKey(pk_calculated[0..], sk)); - std.debug.assert(std.mem.eql(u8, pk_calculated, pk_expected)); + std.testing.expect(X25519.createPublicKey(pk_calculated[0..], sk)); + std.testing.expect(std.mem.eql(u8, pk_calculated, pk_expected)); } test "x25519 rfc7748 vector1" { @@ -593,8 +593,8 @@ test "x25519 rfc7748 vector1" { var output: [32]u8 = undefined; - std.debug.assert(X25519.create(output[0..], secret_key, public_key)); - std.debug.assert(std.mem.eql(u8, output, expected_output)); + std.testing.expect(X25519.create(output[0..], secret_key, public_key)); + std.testing.expect(std.mem.eql(u8, output, expected_output)); } test "x25519 rfc7748 vector2" { @@ -605,8 +605,8 @@ test "x25519 rfc7748 vector2" { var output: [32]u8 = undefined; - std.debug.assert(X25519.create(output[0..], secret_key, public_key)); - std.debug.assert(std.mem.eql(u8, output, expected_output)); + std.testing.expect(X25519.create(output[0..], secret_key, public_key)); + std.testing.expect(std.mem.eql(u8, output, expected_output)); } test "x25519 rfc7748 one iteration" { @@ -619,13 +619,13 @@ test "x25519 rfc7748 one iteration" { var i: usize = 0; while (i < 1) : (i += 1) { var output: [32]u8 = undefined; - std.debug.assert(X25519.create(output[0..], k, u)); + std.testing.expect(X25519.create(output[0..], k, u)); std.mem.copy(u8, u[0..], k[0..]); std.mem.copy(u8, k[0..], output[0..]); } - std.debug.assert(std.mem.eql(u8, k[0..], expected_output)); + std.testing.expect(std.mem.eql(u8, k[0..], expected_output)); } test "x25519 rfc7748 1,000 iterations" { @@ -643,13 +643,13 @@ test "x25519 rfc7748 1,000 iterations" { var i: usize = 0; while (i < 1000) : (i += 1) { var output: [32]u8 = undefined; - std.debug.assert(X25519.create(output[0..], k, u)); + std.testing.expect(X25519.create(output[0..], k, u)); std.mem.copy(u8, u[0..], k[0..]); std.mem.copy(u8, k[0..], output[0..]); } - std.debug.assert(std.mem.eql(u8, k[0..], expected_output)); + std.testing.expect(std.mem.eql(u8, k[0..], expected_output)); } test "x25519 rfc7748 1,000,000 iterations" { @@ -666,11 +666,11 @@ test "x25519 rfc7748 1,000,000 iterations" { var i: usize = 0; while (i < 1000000) : (i += 1) { var output: [32]u8 = undefined; - std.debug.assert(X25519.create(output[0..], k, u)); + std.testing.expect(X25519.create(output[0..], k, u)); std.mem.copy(u8, u[0..], k[0..]); std.mem.copy(u8, k[0..], output[0..]); } - std.debug.assert(std.mem.eql(u8, k[0..], expected_output)); + std.testing.expect(std.mem.eql(u8, k[0..], expected_output)); } diff --git a/std/cstr.zig b/std/cstr.zig index a8aaf21279..abd2eac48f 100644 --- a/std/cstr.zig +++ b/std/cstr.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const debug = std.debug; const mem = std.mem; -const assert = debug.assert; +const testing = std.testing; pub const line_sep = switch (builtin.os) { builtin.Os.windows => "\r\n", @@ -42,8 +42,8 @@ test "cstr fns" { } fn testCStrFnsImpl() void { - assert(cmp(c"aoeu", c"aoez") == -1); - assert(len(c"123456789") == 9); + testing.expect(cmp(c"aoeu", c"aoez") == -1); + testing.expect(len(c"123456789") == 9); } /// Returns a mutable slice with 1 more byte of length which is a null byte. diff --git a/std/debug/index.zig b/std/debug/index.zig index a1e2747df5..7e5be9acef 100644 --- a/std/debug/index.zig +++ b/std/debug/index.zig @@ -107,37 +107,15 @@ pub fn dumpStackTrace(stack_trace: *const builtin.StackTrace) void { /// This function invokes undefined behavior when `ok` is `false`. /// In Debug and ReleaseSafe modes, calls to this function are always /// generated, and the `unreachable` statement triggers a panic. -/// In ReleaseFast and ReleaseSmall modes, calls to this function can be -/// optimized away. +/// In ReleaseFast and ReleaseSmall modes, calls to this function are +/// optimized away, and in fact the optimizer is able to use the assertion +/// in its heuristics. +/// Inside a test block, it is best to use the `std.testing` module rather +/// than this function, because this function may not detect a test failure +/// in ReleaseFast and ReleaseSafe mode. Outside of a test block, this assert +/// function is the correct function to use. pub fn assert(ok: bool) void { - if (!ok) { - // In ReleaseFast test mode, we still want assert(false) to crash, so - // we insert an explicit call to @panic instead of unreachable. - // TODO we should use `assertOrPanic` in tests and remove this logic. - if (builtin.is_test) { - @panic("assertion failure"); - } else { - unreachable; // assertion failure - } - } -} - -/// TODO: add `==` operator for `error_union == error_set`, and then -/// remove this function -pub fn assertError(value: var, expected_error: anyerror) void { - if (value) { - @panic("expected error"); - } else |actual_error| { - assert(actual_error == expected_error); - } -} - -/// Call this function when you want to panic if the condition is not true. -/// If `ok` is `false`, this function will panic in every release mode. -pub fn assertOrPanic(ok: bool) void { - if (!ok) { - @panic("assertion failure"); - } + if (!ok) unreachable; // assertion failure } pub fn panic(comptime format: []const u8, args: ...) noreturn { diff --git a/std/dynamic_library.zig b/std/dynamic_library.zig index 4d19951318..66026b1f29 100644 --- a/std/dynamic_library.zig +++ b/std/dynamic_library.zig @@ -6,6 +6,7 @@ const mem = std.mem; const cstr = std.cstr; const os = std.os; const assert = std.debug.assert; +const testing = std.testing; const elf = std.elf; const linux = os.linux; const windows = os.windows; @@ -206,7 +207,7 @@ test "dynamic_library" { }; const dynlib = DynLib.open(std.debug.global_allocator, libname) catch |err| { - assert(err == error.FileNotFound); + testing.expect(err == error.FileNotFound); return; }; @panic("Expected error from function"); diff --git a/std/event/channel.zig b/std/event/channel.zig index f8cdae6208..4af9bf612b 100644 --- a/std/event/channel.zig +++ b/std/event/channel.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const testing = std.testing; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; const Loop = std.event.Loop; @@ -350,19 +351,19 @@ async fn testChannelGetter(loop: *Loop, channel: *Channel(i32)) void { const value1_promise = try async channel.get(); const value1 = await value1_promise; - assert(value1 == 1234); + testing.expect(value1 == 1234); const value2_promise = try async channel.get(); const value2 = await value2_promise; - assert(value2 == 4567); + testing.expect(value2 == 4567); const value3_promise = try async channel.getOrNull(); const value3 = await value3_promise; - assert(value3 == null); + testing.expect(value3 == null); const last_put = try async testPut(channel, 4444); const value4 = await try async channel.getOrNull(); - assert(value4.? == 4444); + testing.expect(value4.? == 4444); await last_put; } diff --git a/std/event/fs.zig b/std/event/fs.zig index fd0fe434cb..0e7482ec60 100644 --- a/std/event/fs.zig +++ b/std/event/fs.zig @@ -2,6 +2,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const event = std.event; const assert = std.debug.assert; +const testing = std.testing; const os = std.os; const mem = std.mem; const posix = os.posix; @@ -1349,13 +1350,13 @@ async fn testFsWatch(loop: *Loop) !void { try await try async writeFile(loop, file_path, contents); const read_contents = try await try async readFile(loop, file_path, 1024 * 1024); - assert(mem.eql(u8, read_contents, contents)); + testing.expectEqualSlices(u8, contents, read_contents); // now watch the file var watch = try Watch(void).create(loop, 0); defer watch.destroy(); - assert((try await try async watch.addFile(file_path, {})) == null); + testing.expect((try await try async watch.addFile(file_path, {})) == null); const ev = try async watch.channel.get(); var ev_consumed = false; @@ -1375,10 +1376,10 @@ async fn testFsWatch(loop: *Loop) !void { WatchEventId.Delete => @panic("wrong event"), } const contents_updated = try await try async readFile(loop, file_path, 1024 * 1024); - assert(mem.eql(u8, contents_updated, + testing.expectEqualSlices(u8, \\line 1 \\lorem ipsum - )); + , contents_updated); // TODO test deleting the file and then re-adding it. we should get events for both } diff --git a/std/event/future.zig b/std/event/future.zig index 55ed01046d..66acac5ad7 100644 --- a/std/event/future.zig +++ b/std/event/future.zig @@ -1,5 +1,6 @@ const std = @import("../index.zig"); const assert = std.debug.assert; +const testing = std.testing; const builtin = @import("builtin"); const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -114,7 +115,7 @@ async fn testFuture(loop: *Loop) void { const result = (await a) + (await b); cancel c; - assert(result == 12); + testing.expect(result == 12); } async fn waitOnFuture(future: *Future(i32)) i32 { diff --git a/std/event/group.zig b/std/event/group.zig index 7f6b5d953b..25e79640cb 100644 --- a/std/event/group.zig +++ b/std/event/group.zig @@ -4,7 +4,7 @@ const Lock = std.event.Lock; const Loop = std.event.Loop; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; -const assert = std.debug.assert; +const testing = std.testing; /// ReturnType must be `void` or `E!void` pub fn Group(comptime ReturnType: type) type { @@ -146,12 +146,12 @@ async fn testGroup(loop: *Loop) void { group.add(async sleepALittle(&count) catch @panic("memory")) catch @panic("memory"); group.call(increaseByTen, &count) catch @panic("memory"); await (async group.wait() catch @panic("memory")); - assert(count == 11); + testing.expect(count == 11); var another = Group(anyerror!void).init(loop); another.add(async somethingElse() catch @panic("memory")) catch @panic("memory"); another.call(doSomethingThatFails) catch @panic("memory"); - std.debug.assertError(await (async another.wait() catch @panic("memory")), error.ItBroke); + testing.expectError(error.ItBroke, await (async another.wait() catch @panic("memory"))); } async fn sleepALittle(count: *usize) void { diff --git a/std/event/lock.zig b/std/event/lock.zig index 01978e1909..d6a246cee5 100644 --- a/std/event/lock.zig +++ b/std/event/lock.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -141,7 +142,7 @@ test "std.event.Lock" { defer cancel handle; loop.run(); - assert(mem.eql(i32, shared_test_data, [1]i32{3 * @intCast(i32, shared_test_data.len)} ** shared_test_data.len)); + testing.expectEqualSlices(i32, [1]i32{3 * @intCast(i32, shared_test_data.len)} ** shared_test_data.len, shared_test_data); } async fn testLock(loop: *Loop, lock: *Lock) void { diff --git a/std/event/loop.zig b/std/event/loop.zig index d5228db604..b92bf41982 100644 --- a/std/event/loop.zig +++ b/std/event/loop.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -896,7 +897,7 @@ test "std.event.Loop - call" { loop.run(); - assert(did_it); + testing.expect(did_it); } async fn testEventLoop() i32 { @@ -905,6 +906,6 @@ async fn testEventLoop() i32 { async fn testEventLoop2(h: promise->i32, did_it: *bool) void { const value = await h; - assert(value == 1234); + testing.expect(value == 1234); did_it.* = true; } diff --git a/std/event/net.zig b/std/event/net.zig index 9dac6aa566..48461c3d81 100644 --- a/std/event/net.zig +++ b/std/event/net.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); -const assert = std.debug.assert; +const testing = std.testing; const event = std.event; const mem = std.mem; const os = std.os; @@ -326,7 +326,7 @@ async fn doAsyncTest(loop: *Loop, address: *const std.net.Address, server: *Serv var buf: [512]u8 = undefined; const amt_read = try socket_file.read(buf[0..]); const msg = buf[0..amt_read]; - assert(mem.eql(u8, msg, "hello from server\n")); + testing.expect(mem.eql(u8, msg, "hello from server\n")); server.close(); } diff --git a/std/event/rwlock.zig b/std/event/rwlock.zig index f272ac71ea..26ccd12b73 100644 --- a/std/event/rwlock.zig +++ b/std/event/rwlock.zig @@ -1,6 +1,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -231,7 +232,7 @@ test "std.event.RwLock" { loop.run(); const expected_result = [1]i32{shared_it_count * @intCast(i32, shared_test_data.len)} ** shared_test_data.len; - assert(mem.eql(i32, shared_test_data, expected_result)); + testing.expectEqualSlices(i32, expected_result, shared_test_data); } async fn testLock(loop: *Loop, lock: *RwLock) void { @@ -293,7 +294,7 @@ async fn readRunner(lock: *RwLock) void { const handle = await lock_promise; defer handle.release(); - assert(shared_test_index == 0); - assert(shared_test_data[i] == @intCast(i32, shared_count)); + testing.expect(shared_test_index == 0); + testing.expect(shared_test_data[i] == @intCast(i32, shared_count)); } } diff --git a/std/fmt/index.zig b/std/fmt/index.zig index 6097a12c23..05b028112f 100644 --- a/std/fmt/index.zig +++ b/std/fmt/index.zig @@ -2,7 +2,7 @@ const std = @import("../index.zig"); const math = std.math; const debug = std.debug; const assert = debug.assert; -const assertError = debug.assertError; +const testing = std.testing; const mem = std.mem; const builtin = @import("builtin"); const errol = @import("errol/index.zig"); @@ -588,7 +588,7 @@ pub fn formatFloatDecimal( } // Remaining fractional portion, zero-padding if insufficient. - debug.assert(precision >= printed); + assert(precision >= printed); if (num_digits_whole_no_pad + precision - printed < float_decimal.digits.len) { try output(context, float_decimal.digits[num_digits_whole_no_pad .. num_digits_whole_no_pad + precision - printed]); return; @@ -798,13 +798,13 @@ pub fn parseInt(comptime T: type, buf: []const u8, radix: u8) !T { } test "fmt.parseInt" { - assert((parseInt(i32, "-10", 10) catch unreachable) == -10); - assert((parseInt(i32, "+10", 10) catch unreachable) == 10); - assert(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter); - assert(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter); - assert(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter); - assert((parseInt(u8, "255", 10) catch unreachable) == 255); - assert(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow); + testing.expect((parseInt(i32, "-10", 10) catch unreachable) == -10); + testing.expect((parseInt(i32, "+10", 10) catch unreachable) == 10); + testing.expect(if (parseInt(i32, " 10", 10)) |_| false else |err| err == error.InvalidCharacter); + testing.expect(if (parseInt(i32, "10 ", 10)) |_| false else |err| err == error.InvalidCharacter); + testing.expect(if (parseInt(u32, "-10", 10)) |_| false else |err| err == error.InvalidCharacter); + testing.expect((parseInt(u8, "255", 10) catch unreachable) == 255); + testing.expect(if (parseInt(u8, "256", 10)) |_| false else |err| err == error.Overflow); } const ParseUnsignedError = error{ @@ -829,30 +829,30 @@ pub fn parseUnsigned(comptime T: type, buf: []const u8, radix: u8) ParseUnsigned } test "parseUnsigned" { - assert((try parseUnsigned(u16, "050124", 10)) == 50124); - assert((try parseUnsigned(u16, "65535", 10)) == 65535); - assertError(parseUnsigned(u16, "65536", 10), error.Overflow); + testing.expect((try parseUnsigned(u16, "050124", 10)) == 50124); + testing.expect((try parseUnsigned(u16, "65535", 10)) == 65535); + testing.expectError(error.Overflow, parseUnsigned(u16, "65536", 10)); - assert((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); - assertError(parseUnsigned(u64, "10000000000000000", 16), error.Overflow); + testing.expect((try parseUnsigned(u64, "0ffffffffffffffff", 16)) == 0xffffffffffffffff); + testing.expectError(error.Overflow, parseUnsigned(u64, "10000000000000000", 16)); - assert((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); + testing.expect((try parseUnsigned(u32, "DeadBeef", 16)) == 0xDEADBEEF); - assert((try parseUnsigned(u7, "1", 10)) == 1); - assert((try parseUnsigned(u7, "1000", 2)) == 8); + testing.expect((try parseUnsigned(u7, "1", 10)) == 1); + testing.expect((try parseUnsigned(u7, "1000", 2)) == 8); - assertError(parseUnsigned(u32, "f", 10), error.InvalidCharacter); - assertError(parseUnsigned(u8, "109", 8), error.InvalidCharacter); + testing.expectError(error.InvalidCharacter, parseUnsigned(u32, "f", 10)); + testing.expectError(error.InvalidCharacter, parseUnsigned(u8, "109", 8)); - assert((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); + testing.expect((try parseUnsigned(u32, "NUMBER", 36)) == 1442151747); // these numbers should fit even though the radix itself doesn't fit in the destination type - assert((try parseUnsigned(u1, "0", 10)) == 0); - assert((try parseUnsigned(u1, "1", 10)) == 1); - assertError(parseUnsigned(u1, "2", 10), error.Overflow); - assert((try parseUnsigned(u1, "001", 16)) == 1); - assert((try parseUnsigned(u2, "3", 16)) == 3); - assertError(parseUnsigned(u2, "4", 16), error.Overflow); + testing.expect((try parseUnsigned(u1, "0", 10)) == 0); + testing.expect((try parseUnsigned(u1, "1", 10)) == 1); + testing.expectError(error.Overflow, parseUnsigned(u1, "2", 10)); + testing.expect((try parseUnsigned(u1, "001", 16)) == 1); + testing.expect((try parseUnsigned(u2, "3", 16)) == 3); + testing.expectError(error.Overflow, parseUnsigned(u2, "4", 16)); } pub fn charToDigit(c: u8, radix: u8) (error{InvalidCharacter}!u8) { @@ -910,19 +910,19 @@ fn countSize(size: *usize, bytes: []const u8) (error{}!void) { test "buf print int" { var buffer: [max_int_digits]u8 = undefined; const buf = buffer[0..]; - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 2, false, 0), "-101111000110000101001110")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 10, false, 0), "-12345678")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, false, 0), "-bc614e")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-12345678), 16, true, 0), "-BC614E")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(12345678), 10, true, 0), "12345678")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(666), 10, false, 6), "000666")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 6), "001234")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, u32(0x1234), 16, false, 1), "1234")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42")); - assert(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(42), 10, false, 3), "+42")); + testing.expect(mem.eql(u8, bufPrintIntToSlice(buf, i32(-42), 10, false, 3), "-42")); } fn bufPrintIntToSlice(buf: []u8, value: var, base: u8, uppercase: bool, width: usize) []u8 { @@ -939,7 +939,7 @@ test "parse u64 digit too big" { test "parse unsigned comptime" { comptime { - assert((try parseUnsigned(usize, "2", 10)) == 2); + testing.expect((try parseUnsigned(usize, "2", 10)) == 2); } } @@ -977,17 +977,17 @@ test "fmt.format" { var context = BufPrintContext{ .remaining = buf1[0..] }; try formatType(1234, "", &context, error{BufferTooSmall}, bufPrintWrite); var res = buf1[0 .. buf1.len - context.remaining.len]; - assert(mem.eql(u8, res, "1234")); + testing.expect(mem.eql(u8, res, "1234")); context = BufPrintContext{ .remaining = buf1[0..] }; try formatType('a', "c", &context, error{BufferTooSmall}, bufPrintWrite); res = buf1[0 .. buf1.len - context.remaining.len]; - assert(mem.eql(u8, res, "a")); + testing.expect(mem.eql(u8, res, "a")); context = BufPrintContext{ .remaining = buf1[0..] }; try formatType(0b1100, "b", &context, error{BufferTooSmall}, bufPrintWrite); res = buf1[0 .. buf1.len - context.remaining.len]; - assert(mem.eql(u8, res, "1100")); + testing.expect(mem.eql(u8, res, "1100")); } { const value: [3]u8 = "abc"; @@ -1053,19 +1053,19 @@ test "fmt.format" { var buf1: [32]u8 = undefined; const value: f32 = 1.34; const result = try bufPrint(buf1[0..], "f32: {e}\n", value); - assert(mem.eql(u8, result, "f32: 1.34000003e+00\n")); + testing.expect(mem.eql(u8, result, "f32: 1.34000003e+00\n")); } { var buf1: [32]u8 = undefined; const value: f32 = 12.34; const result = try bufPrint(buf1[0..], "f32: {e}\n", value); - assert(mem.eql(u8, result, "f32: 1.23400001e+01\n")); + testing.expect(mem.eql(u8, result, "f32: 1.23400001e+01\n")); } { var buf1: [32]u8 = undefined; const value: f64 = -12.34e10; const result = try bufPrint(buf1[0..], "f64: {e}\n", value); - assert(mem.eql(u8, result, "f64: -1.234e+11\n")); + testing.expect(mem.eql(u8, result, "f64: -1.234e+11\n")); } { // This fails on release due to a minor rounding difference. @@ -1075,26 +1075,26 @@ test "fmt.format" { var buf1: [32]u8 = undefined; const value: f64 = 9.999960e-40; const result = try bufPrint(buf1[0..], "f64: {e}\n", value); - assert(mem.eql(u8, result, "f64: 9.99996e-40\n")); + testing.expect(mem.eql(u8, result, "f64: 9.99996e-40\n")); } } { var buf1: [32]u8 = undefined; const value: f64 = 1.409706e-42; const result = try bufPrint(buf1[0..], "f64: {e5}\n", value); - assert(mem.eql(u8, result, "f64: 1.40971e-42\n")); + testing.expect(mem.eql(u8, result, "f64: 1.40971e-42\n")); } { var buf1: [32]u8 = undefined; const value: f64 = @bitCast(f32, u32(814313563)); const result = try bufPrint(buf1[0..], "f64: {e5}\n", value); - assert(mem.eql(u8, result, "f64: 1.00000e-09\n")); + testing.expect(mem.eql(u8, result, "f64: 1.00000e-09\n")); } { var buf1: [32]u8 = undefined; const value: f64 = @bitCast(f32, u32(1006632960)); const result = try bufPrint(buf1[0..], "f64: {e5}\n", value); - assert(mem.eql(u8, result, "f64: 7.81250e-03\n")); + testing.expect(mem.eql(u8, result, "f64: 7.81250e-03\n")); } { // libc rounds 1.000005e+05 to 1.00000e+05 but zig does 1.00001e+05. @@ -1102,47 +1102,47 @@ test "fmt.format" { var buf1: [32]u8 = undefined; const value: f64 = @bitCast(f32, u32(1203982400)); const result = try bufPrint(buf1[0..], "f64: {e5}\n", value); - assert(mem.eql(u8, result, "f64: 1.00001e+05\n")); + testing.expect(mem.eql(u8, result, "f64: 1.00001e+05\n")); } { var buf1: [32]u8 = undefined; const result = try bufPrint(buf1[0..], "f64: {}\n", math.nan_f64); - assert(mem.eql(u8, result, "f64: nan\n")); + testing.expect(mem.eql(u8, result, "f64: nan\n")); } if (builtin.arch != builtin.Arch.armv8) { // negative nan is not defined by IEE 754, // and ARM thus normalizes it to positive nan var buf1: [32]u8 = undefined; const result = try bufPrint(buf1[0..], "f64: {}\n", -math.nan_f64); - assert(mem.eql(u8, result, "f64: -nan\n")); + testing.expect(mem.eql(u8, result, "f64: -nan\n")); } { var buf1: [32]u8 = undefined; const result = try bufPrint(buf1[0..], "f64: {}\n", math.inf_f64); - assert(mem.eql(u8, result, "f64: inf\n")); + testing.expect(mem.eql(u8, result, "f64: inf\n")); } { var buf1: [32]u8 = undefined; const result = try bufPrint(buf1[0..], "f64: {}\n", -math.inf_f64); - assert(mem.eql(u8, result, "f64: -inf\n")); + testing.expect(mem.eql(u8, result, "f64: -inf\n")); } { var buf1: [64]u8 = undefined; const value: f64 = 1.52314e+29; const result = try bufPrint(buf1[0..], "f64: {.}\n", value); - assert(mem.eql(u8, result, "f64: 152314000000000000000000000000\n")); + testing.expect(mem.eql(u8, result, "f64: 152314000000000000000000000000\n")); } { var buf1: [32]u8 = undefined; const value: f32 = 1.1234; const result = try bufPrint(buf1[0..], "f32: {.1}\n", value); - assert(mem.eql(u8, result, "f32: 1.1\n")); + testing.expect(mem.eql(u8, result, "f32: 1.1\n")); } { var buf1: [32]u8 = undefined; const value: f32 = 1234.567; const result = try bufPrint(buf1[0..], "f32: {.2}\n", value); - assert(mem.eql(u8, result, "f32: 1234.57\n")); + testing.expect(mem.eql(u8, result, "f32: 1234.57\n")); } { var buf1: [32]u8 = undefined; @@ -1150,92 +1150,92 @@ test "fmt.format" { const result = try bufPrint(buf1[0..], "f32: {.4}\n", value); // -11.1234 is converted to f64 -11.12339... internally (errol3() function takes f64). // -11.12339... is rounded back up to -11.1234 - assert(mem.eql(u8, result, "f32: -11.1234\n")); + testing.expect(mem.eql(u8, result, "f32: -11.1234\n")); } { var buf1: [32]u8 = undefined; const value: f32 = 91.12345; const result = try bufPrint(buf1[0..], "f32: {.5}\n", value); - assert(mem.eql(u8, result, "f32: 91.12345\n")); + testing.expect(mem.eql(u8, result, "f32: 91.12345\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 91.12345678901235; const result = try bufPrint(buf1[0..], "f64: {.10}\n", value); - assert(mem.eql(u8, result, "f64: 91.1234567890\n")); + testing.expect(mem.eql(u8, result, "f64: 91.1234567890\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 0.0; const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 5.700; const result = try bufPrint(buf1[0..], "f64: {.0}\n", value); - assert(mem.eql(u8, result, "f64: 6\n")); + testing.expect(mem.eql(u8, result, "f64: 6\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 9.999; const result = try bufPrint(buf1[0..], "f64: {.1}\n", value); - assert(mem.eql(u8, result, "f64: 10.0\n")); + testing.expect(mem.eql(u8, result, "f64: 10.0\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 1.0; const result = try bufPrint(buf1[0..], "f64: {.3}\n", value); - assert(mem.eql(u8, result, "f64: 1.000\n")); + testing.expect(mem.eql(u8, result, "f64: 1.000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 0.0003; const result = try bufPrint(buf1[0..], "f64: {.8}\n", value); - assert(mem.eql(u8, result, "f64: 0.00030000\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00030000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 1.40130e-45; const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = 9.999960e-40; const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00000\n")); } // libc checks { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(916964781))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.00001\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00001\n")); } { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(925353389))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.00001\n")); + testing.expect(mem.eql(u8, result, "f64: 0.00001\n")); } { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(1036831278))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.10000\n")); + testing.expect(mem.eql(u8, result, "f64: 0.10000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(1065353133))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 1.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 1.00000\n")); } { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(1092616192))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 10.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 10.00000\n")); } // libc differences { @@ -1245,7 +1245,7 @@ test "fmt.format" { // floats of the form x.yyyy25 on a precision point. const value: f64 = f64(@bitCast(f32, u32(1015021568))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 0.01563\n")); + testing.expect(mem.eql(u8, result, "f64: 0.01563\n")); } // std-windows-x86_64-Debug-bare test case fails { @@ -1255,7 +1255,7 @@ test "fmt.format" { var buf1: [32]u8 = undefined; const value: f64 = f64(@bitCast(f32, u32(1518338049))); const result = try bufPrint(buf1[0..], "f64: {.5}\n", value); - assert(mem.eql(u8, result, "f64: 18014400656965630.00000\n")); + testing.expect(mem.eql(u8, result, "f64: 18014400656965630.00000\n")); } //custom type format { @@ -1336,10 +1336,10 @@ test "fmt.format" { var buf: [100]u8 = undefined; const uu_result = try bufPrint(buf[0..], "{}", uu_inst); - debug.assert(mem.eql(u8, uu_result[0..3], "UU@")); + testing.expect(mem.eql(u8, uu_result[0..3], "UU@")); const eu_result = try bufPrint(buf[0..], "{}", eu_inst); - debug.assert(mem.eql(u8, uu_result[0..3], "EU@")); + testing.expect(mem.eql(u8, uu_result[0..3], "EU@")); } //enum format { @@ -1398,11 +1398,11 @@ pub fn trim(buf: []const u8) []const u8 { } test "fmt.trim" { - assert(mem.eql(u8, "abc", trim("\n abc \t"))); - assert(mem.eql(u8, "", trim(" "))); - assert(mem.eql(u8, "", trim(""))); - assert(mem.eql(u8, "abc", trim(" abc"))); - assert(mem.eql(u8, "abc", trim("abc "))); + testing.expect(mem.eql(u8, "abc", trim("\n abc \t"))); + testing.expect(mem.eql(u8, "", trim(" "))); + testing.expect(mem.eql(u8, "", trim(""))); + testing.expect(mem.eql(u8, "abc", trim(" abc"))); + testing.expect(mem.eql(u8, "abc", trim("abc "))); } pub fn isWhiteSpace(byte: u8) bool { diff --git a/std/hash/adler.zig b/std/hash/adler.zig index 9c5966f89b..78f960367a 100644 --- a/std/hash/adler.zig +++ b/std/hash/adler.zig @@ -4,7 +4,7 @@ // https://github.com/madler/zlib/blob/master/adler32.c const std = @import("../index.zig"); -const debug = std.debug; +const testing = std.testing; pub const Adler32 = struct { const base = 65521; @@ -89,19 +89,19 @@ pub const Adler32 = struct { }; test "adler32 sanity" { - debug.assert(Adler32.hash("a") == 0x620062); - debug.assert(Adler32.hash("example") == 0xbc002ed); + testing.expect(Adler32.hash("a") == 0x620062); + testing.expect(Adler32.hash("example") == 0xbc002ed); } test "adler32 long" { const long1 = []u8{1} ** 1024; - debug.assert(Adler32.hash(long1[0..]) == 0x06780401); + testing.expect(Adler32.hash(long1[0..]) == 0x06780401); const long2 = []u8{1} ** 1025; - debug.assert(Adler32.hash(long2[0..]) == 0x0a7a0402); + testing.expect(Adler32.hash(long2[0..]) == 0x0a7a0402); } test "adler32 very long" { const long = []u8{1} ** 5553; - debug.assert(Adler32.hash(long[0..]) == 0x707f15b2); + testing.expect(Adler32.hash(long[0..]) == 0x707f15b2); } diff --git a/std/hash/crc.zig b/std/hash/crc.zig index c4bd92884a..9bea358bf1 100644 --- a/std/hash/crc.zig +++ b/std/hash/crc.zig @@ -7,6 +7,7 @@ const std = @import("../index.zig"); const debug = std.debug; +const testing = std.testing; pub const Polynomial = struct { const IEEE = 0xedb88320; @@ -101,17 +102,17 @@ pub fn Crc32WithPoly(comptime poly: u32) type { test "crc32 ieee" { const Crc32Ieee = Crc32WithPoly(Polynomial.IEEE); - debug.assert(Crc32Ieee.hash("") == 0x00000000); - debug.assert(Crc32Ieee.hash("a") == 0xe8b7be43); - debug.assert(Crc32Ieee.hash("abc") == 0x352441c2); + testing.expect(Crc32Ieee.hash("") == 0x00000000); + testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); + testing.expect(Crc32Ieee.hash("abc") == 0x352441c2); } test "crc32 castagnoli" { const Crc32Castagnoli = Crc32WithPoly(Polynomial.Castagnoli); - debug.assert(Crc32Castagnoli.hash("") == 0x00000000); - debug.assert(Crc32Castagnoli.hash("a") == 0xc1d04330); - debug.assert(Crc32Castagnoli.hash("abc") == 0x364b3fb7); + testing.expect(Crc32Castagnoli.hash("") == 0x00000000); + testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); + testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7); } // half-byte lookup table implementation. @@ -165,15 +166,15 @@ pub fn Crc32SmallWithPoly(comptime poly: u32) type { test "small crc32 ieee" { const Crc32Ieee = Crc32SmallWithPoly(Polynomial.IEEE); - debug.assert(Crc32Ieee.hash("") == 0x00000000); - debug.assert(Crc32Ieee.hash("a") == 0xe8b7be43); - debug.assert(Crc32Ieee.hash("abc") == 0x352441c2); + testing.expect(Crc32Ieee.hash("") == 0x00000000); + testing.expect(Crc32Ieee.hash("a") == 0xe8b7be43); + testing.expect(Crc32Ieee.hash("abc") == 0x352441c2); } test "small crc32 castagnoli" { const Crc32Castagnoli = Crc32SmallWithPoly(Polynomial.Castagnoli); - debug.assert(Crc32Castagnoli.hash("") == 0x00000000); - debug.assert(Crc32Castagnoli.hash("a") == 0xc1d04330); - debug.assert(Crc32Castagnoli.hash("abc") == 0x364b3fb7); + testing.expect(Crc32Castagnoli.hash("") == 0x00000000); + testing.expect(Crc32Castagnoli.hash("a") == 0xc1d04330); + testing.expect(Crc32Castagnoli.hash("abc") == 0x364b3fb7); } diff --git a/std/hash/fnv.zig b/std/hash/fnv.zig index 9bb18f14b3..6876b636f6 100644 --- a/std/hash/fnv.zig +++ b/std/hash/fnv.zig @@ -5,7 +5,7 @@ // https://tools.ietf.org/html/draft-eastlake-fnv-14 const std = @import("../index.zig"); -const debug = std.debug; +const testing = std.testing; pub const Fnv1a_32 = Fnv1a(u32, 0x01000193, 0x811c9dc5); pub const Fnv1a_64 = Fnv1a(u64, 0x100000001b3, 0xcbf29ce484222325); @@ -41,18 +41,18 @@ fn Fnv1a(comptime T: type, comptime prime: T, comptime offset: T) type { } test "fnv1a-32" { - debug.assert(Fnv1a_32.hash("") == 0x811c9dc5); - debug.assert(Fnv1a_32.hash("a") == 0xe40c292c); - debug.assert(Fnv1a_32.hash("foobar") == 0xbf9cf968); + testing.expect(Fnv1a_32.hash("") == 0x811c9dc5); + testing.expect(Fnv1a_32.hash("a") == 0xe40c292c); + testing.expect(Fnv1a_32.hash("foobar") == 0xbf9cf968); } test "fnv1a-64" { - debug.assert(Fnv1a_64.hash("") == 0xcbf29ce484222325); - debug.assert(Fnv1a_64.hash("a") == 0xaf63dc4c8601ec8c); - debug.assert(Fnv1a_64.hash("foobar") == 0x85944171f73967e8); + testing.expect(Fnv1a_64.hash("") == 0xcbf29ce484222325); + testing.expect(Fnv1a_64.hash("a") == 0xaf63dc4c8601ec8c); + testing.expect(Fnv1a_64.hash("foobar") == 0x85944171f73967e8); } test "fnv1a-128" { - debug.assert(Fnv1a_128.hash("") == 0x6c62272e07bb014262b821756295c58d); - debug.assert(Fnv1a_128.hash("a") == 0xd228cb696f1a8caf78912b704e4a8964); + testing.expect(Fnv1a_128.hash("") == 0x6c62272e07bb014262b821756295c58d); + testing.expect(Fnv1a_128.hash("a") == 0xd228cb696f1a8caf78912b704e4a8964); } diff --git a/std/hash/siphash.zig b/std/hash/siphash.zig index ee26950272..c9a6128d35 100644 --- a/std/hash/siphash.zig +++ b/std/hash/siphash.zig @@ -6,7 +6,8 @@ // https://131002.net/siphash/ const std = @import("../index.zig"); -const debug = std.debug; +const assert = std.debug.assert; +const testing = std.testing; const math = std.math; const mem = std.mem; @@ -21,8 +22,8 @@ pub fn SipHash128(comptime c_rounds: usize, comptime d_rounds: usize) type { } fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) type { - debug.assert(T == u64 or T == u128); - debug.assert(c_rounds > 0 and d_rounds > 0); + assert(T == u64 or T == u128); + assert(c_rounds > 0 and d_rounds > 0); return struct { const Self = @This(); @@ -40,7 +41,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) msg_len: u8, pub fn init(key: []const u8) Self { - debug.assert(key.len >= 16); + assert(key.len >= 16); const k0 = mem.readIntSliceLittle(u64, key[0..8]); const k1 = mem.readIntSliceLittle(u64, key[8..16]); @@ -119,7 +120,7 @@ fn SipHash(comptime T: type, comptime c_rounds: usize, comptime d_rounds: usize) } fn round(d: *Self, b: []const u8) void { - debug.assert(b.len == 8); + assert(b.len == 8); const m = mem.readIntSliceLittle(u64, b[0..]); d.v3 ^= m; @@ -236,7 +237,7 @@ test "siphash64-2-4 sanity" { buffer[i] = @intCast(u8, i); const expected = mem.readIntLittle(u64, &vector); - debug.assert(siphash.hash(test_key, buffer[0..i]) == expected); + testing.expect(siphash.hash(test_key, buffer[0..i]) == expected); } } @@ -315,6 +316,6 @@ test "siphash128-2-4 sanity" { buffer[i] = @intCast(u8, i); const expected = mem.readIntLittle(u128, &vector); - debug.assert(siphash.hash(test_key, buffer[0..i]) == expected); + testing.expect(siphash.hash(test_key, buffer[0..i]) == expected); } } diff --git a/std/hash_map.zig b/std/hash_map.zig index a63a549814..716f04ff34 100644 --- a/std/hash_map.zig +++ b/std/hash_map.zig @@ -1,6 +1,7 @@ const std = @import("index.zig"); const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const math = std.math; const mem = std.mem; const Allocator = mem.Allocator; @@ -342,37 +343,37 @@ test "basic hash map usage" { var map = AutoHashMap(i32, i32).init(&direct_allocator.allocator); defer map.deinit(); - assert((try map.put(1, 11)) == null); - assert((try map.put(2, 22)) == null); - assert((try map.put(3, 33)) == null); - assert((try map.put(4, 44)) == null); - assert((try map.put(5, 55)) == null); + testing.expect((try map.put(1, 11)) == null); + testing.expect((try map.put(2, 22)) == null); + testing.expect((try map.put(3, 33)) == null); + testing.expect((try map.put(4, 44)) == null); + testing.expect((try map.put(5, 55)) == null); - assert((try map.put(5, 66)).?.value == 55); - assert((try map.put(5, 55)).?.value == 66); + testing.expect((try map.put(5, 66)).?.value == 55); + testing.expect((try map.put(5, 55)).?.value == 66); const gop1 = try map.getOrPut(5); - assert(gop1.found_existing == true); - assert(gop1.kv.value == 55); + testing.expect(gop1.found_existing == true); + testing.expect(gop1.kv.value == 55); gop1.kv.value = 77; - assert(map.get(5).?.value == 77); + testing.expect(map.get(5).?.value == 77); const gop2 = try map.getOrPut(99); - assert(gop2.found_existing == false); + testing.expect(gop2.found_existing == false); gop2.kv.value = 42; - assert(map.get(99).?.value == 42); + testing.expect(map.get(99).?.value == 42); const gop3 = try map.getOrPutValue(5, 5); - assert(gop3.value == 77); + testing.expect(gop3.value == 77); const gop4 = try map.getOrPutValue(100, 41); - assert(gop4.value == 41); + testing.expect(gop4.value == 41); - assert(map.contains(2)); - assert(map.get(2).?.value == 22); + testing.expect(map.contains(2)); + testing.expect(map.get(2).?.value == 22); _ = map.remove(2); - assert(map.remove(2) == null); - assert(map.get(2) == null); + testing.expect(map.remove(2) == null); + testing.expect(map.get(2) == null); } test "iterator hash map" { @@ -382,9 +383,9 @@ test "iterator hash map" { var reset_map = AutoHashMap(i32, i32).init(&direct_allocator.allocator); defer reset_map.deinit(); - assert((try reset_map.put(1, 11)) == null); - assert((try reset_map.put(2, 22)) == null); - assert((try reset_map.put(3, 33)) == null); + testing.expect((try reset_map.put(1, 11)) == null); + testing.expect((try reset_map.put(2, 22)) == null); + testing.expect((try reset_map.put(3, 33)) == null); var keys = []i32{ 3, @@ -400,26 +401,26 @@ test "iterator hash map" { var it = reset_map.iterator(); var count: usize = 0; while (it.next()) |next| { - assert(next.key == keys[count]); - assert(next.value == values[count]); + testing.expect(next.key == keys[count]); + testing.expect(next.value == values[count]); count += 1; } - assert(count == 3); - assert(it.next() == null); + testing.expect(count == 3); + testing.expect(it.next() == null); it.reset(); count = 0; while (it.next()) |next| { - assert(next.key == keys[count]); - assert(next.value == values[count]); + testing.expect(next.key == keys[count]); + testing.expect(next.value == values[count]); count += 1; if (count == 2) break; } it.reset(); var entry = it.next().?; - assert(entry.key == keys[0]); - assert(entry.value == values[0]); + testing.expect(entry.key == keys[0]); + testing.expect(entry.value == values[0]); } pub fn getHashPtrAddrFn(comptime K: type) (fn (K) u32) { diff --git a/std/heap.zig b/std/heap.zig index 8a5ebf134c..e7088150a8 100644 --- a/std/heap.zig +++ b/std/heap.zig @@ -1,6 +1,7 @@ const std = @import("index.zig"); const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); @@ -487,11 +488,11 @@ test "FixedBufferAllocator Reuse memory on realloc" { var fixed_buffer_allocator = FixedBufferAllocator.init(small_fixed_buffer[0..]); var slice0 = try fixed_buffer_allocator.allocator.alloc(u8, 5); - assert(slice0.len == 5); + testing.expect(slice0.len == 5); var slice1 = try fixed_buffer_allocator.allocator.realloc(u8, slice0, 10); - assert(slice1.ptr == slice0.ptr); - assert(slice1.len == 10); - debug.assertError(fixed_buffer_allocator.allocator.realloc(u8, slice1, 11), error.OutOfMemory); + testing.expect(slice1.ptr == slice0.ptr); + testing.expect(slice1.len == 10); + testing.expectError(error.OutOfMemory, fixed_buffer_allocator.allocator.realloc(u8, slice1, 11)); } // check that we don't re-use the memory if it's not the most recent block { @@ -502,10 +503,10 @@ test "FixedBufferAllocator Reuse memory on realloc" { slice0[1] = 2; var slice1 = try fixed_buffer_allocator.allocator.alloc(u8, 2); var slice2 = try fixed_buffer_allocator.allocator.realloc(u8, slice0, 4); - assert(slice0.ptr != slice2.ptr); - assert(slice1.ptr != slice2.ptr); - assert(slice2[0] == 1); - assert(slice2[1] == 2); + testing.expect(slice0.ptr != slice2.ptr); + testing.expect(slice1.ptr != slice2.ptr); + testing.expect(slice2[0] == 1); + testing.expect(slice2[1] == 2); } } @@ -519,28 +520,28 @@ test "ThreadSafeFixedBufferAllocator" { fn testAllocator(allocator: *mem.Allocator) !void { var slice = try allocator.alloc(*i32, 100); - assert(slice.len == 100); + testing.expect(slice.len == 100); for (slice) |*item, i| { item.* = try allocator.create(i32); item.*.* = @intCast(i32, i); } slice = try allocator.realloc(*i32, slice, 20000); - assert(slice.len == 20000); + testing.expect(slice.len == 20000); for (slice[0..100]) |item, i| { - assert(item.* == @intCast(i32, i)); + testing.expect(item.* == @intCast(i32, i)); allocator.destroy(item); } slice = try allocator.realloc(*i32, slice, 50); - assert(slice.len == 50); + testing.expect(slice.len == 50); slice = try allocator.realloc(*i32, slice, 25); - assert(slice.len == 25); + testing.expect(slice.len == 25); slice = try allocator.realloc(*i32, slice, 0); - assert(slice.len == 0); + testing.expect(slice.len == 0); slice = try allocator.realloc(*i32, slice, 10); - assert(slice.len == 10); + testing.expect(slice.len == 10); allocator.free(slice); } @@ -548,25 +549,25 @@ fn testAllocator(allocator: *mem.Allocator) !void { fn testAllocatorAligned(allocator: *mem.Allocator, comptime alignment: u29) !void { // initial var slice = try allocator.alignedAlloc(u8, alignment, 10); - assert(slice.len == 10); + testing.expect(slice.len == 10); // grow slice = try allocator.alignedRealloc(u8, alignment, slice, 100); - assert(slice.len == 100); + testing.expect(slice.len == 100); // shrink slice = try allocator.alignedRealloc(u8, alignment, slice, 10); - assert(slice.len == 10); + testing.expect(slice.len == 10); // go to zero slice = try allocator.alignedRealloc(u8, alignment, slice, 0); - assert(slice.len == 0); + testing.expect(slice.len == 0); // realloc from zero slice = try allocator.alignedRealloc(u8, alignment, slice, 100); - assert(slice.len == 100); + testing.expect(slice.len == 100); // shrink with shrink slice = allocator.alignedShrink(u8, alignment, slice, 10); - assert(slice.len == 10); + testing.expect(slice.len == 10); // shrink to zero slice = allocator.alignedShrink(u8, alignment, slice, 0); - assert(slice.len == 0); + testing.expect(slice.len == 0); } fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!void { @@ -581,19 +582,19 @@ fn testAllocatorLargeAlignment(allocator: *mem.Allocator) mem.Allocator.Error!vo _ = @shlWithOverflow(usize, ~usize(0), USizeShift(@ctz(large_align)), &align_mask); var slice = try allocator.allocFn(allocator, 500, large_align); - debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.reallocFn(allocator, slice, 100, large_align); - debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.reallocFn(allocator, slice, 5000, large_align); - debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.reallocFn(allocator, slice, 10, large_align); - debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); slice = try allocator.reallocFn(allocator, slice, 20000, large_align); - debug.assert(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); + testing.expect(@ptrToInt(slice.ptr) & align_mask == @ptrToInt(slice.ptr)); allocator.free(slice); } diff --git a/std/index.zig b/std/index.zig index 2a63244004..9d9b7ee8d6 100644 --- a/std/index.zig +++ b/std/index.zig @@ -31,6 +31,7 @@ pub const hash_map = @import("hash_map.zig"); pub const heap = @import("heap.zig"); pub const io = @import("io.zig"); pub const json = @import("json.zig"); +pub const lazyInit = @import("lazy_init.zig").lazyInit; pub const macho = @import("macho.zig"); pub const math = @import("math/index.zig"); pub const mem = @import("mem.zig"); @@ -41,11 +42,10 @@ pub const pdb = @import("pdb.zig"); pub const rand = @import("rand/index.zig"); pub const rb = @import("rb.zig"); pub const sort = @import("sort.zig"); +pub const testing = @import("testing.zig"); pub const unicode = @import("unicode.zig"); pub const zig = @import("zig/index.zig"); -pub const lazyInit = @import("lazy_init.zig").lazyInit; - test "std" { // run tests from these _ = @import("array_list.zig"); @@ -60,7 +60,6 @@ test "std" { _ = @import("segmented_list.zig"); _ = @import("spinlock.zig"); - _ = @import("dynamic_library.zig"); _ = @import("base64.zig"); _ = @import("build.zig"); _ = @import("c/index.zig"); @@ -69,24 +68,26 @@ test "std" { _ = @import("cstr.zig"); _ = @import("debug/index.zig"); _ = @import("dwarf.zig"); + _ = @import("dynamic_library.zig"); _ = @import("elf.zig"); _ = @import("empty.zig"); _ = @import("event.zig"); _ = @import("fmt/index.zig"); _ = @import("hash/index.zig"); + _ = @import("heap.zig"); _ = @import("io.zig"); _ = @import("json.zig"); + _ = @import("lazy_init.zig"); _ = @import("macho.zig"); _ = @import("math/index.zig"); - _ = @import("meta/index.zig"); _ = @import("mem.zig"); + _ = @import("meta/index.zig"); _ = @import("net.zig"); - _ = @import("heap.zig"); _ = @import("os/index.zig"); - _ = @import("rand/index.zig"); _ = @import("pdb.zig"); + _ = @import("rand/index.zig"); _ = @import("sort.zig"); + _ = @import("testing.zig"); _ = @import("unicode.zig"); _ = @import("zig/index.zig"); - _ = @import("lazy_init.zig"); } diff --git a/std/io.zig b/std/io.zig index 81d90def6e..d7e8507f9b 100644 --- a/std/io.zig +++ b/std/io.zig @@ -13,6 +13,7 @@ const trait = meta.trait; const Buffer = std.Buffer; const fmt = std.fmt; const File = std.os.File; +const testing = std.testing; const is_posix = builtin.os != builtin.Os.windows; const is_windows = builtin.os == builtin.Os.windows; @@ -664,7 +665,7 @@ test "io.SliceOutStream" { const stream = &slice_stream.stream; try stream.print("{}{}!", "Hello", "World"); - debug.assertOrPanic(mem.eql(u8, "HelloWorld!", slice_stream.getWritten())); + testing.expectEqualSlices(u8, "HelloWorld!", slice_stream.getWritten()); } var null_out_stream_state = NullOutStream.init(); @@ -726,7 +727,7 @@ test "io.CountingOutStream" { const bytes = "yay" ** 10000; stream.write(bytes) catch unreachable; - debug.assertOrPanic(counting_stream.bytes_written == bytes.len); + testing.expect(counting_stream.bytes_written == bytes.len); } pub fn BufferedOutStream(comptime Error: type) type { @@ -1014,10 +1015,10 @@ test "io.readLineFrom" { ); const stream = &mem_stream.stream; - debug.assertOrPanic(mem.eql(u8, "Line 1", try readLineFrom(stream, &buf))); - debug.assertOrPanic(mem.eql(u8, "Line 22", try readLineFrom(stream, &buf))); - debug.assertError(readLineFrom(stream, &buf), error.EndOfStream); - debug.assertOrPanic(mem.eql(u8, buf.toSlice(), "Line 1Line 22Line 333")); + testing.expectEqualSlices(u8, "Line 1", try readLineFrom(stream, &buf)); + testing.expectEqualSlices(u8, "Line 22", try readLineFrom(stream, &buf)); + testing.expectError(error.EndOfStream, readLineFrom(stream, &buf)); + testing.expectEqualSlices(u8, "Line 1Line 22Line 333", buf.toSlice()); } pub fn readLineSlice(slice: []u8) ![]u8 { @@ -1045,8 +1046,8 @@ test "io.readLineSliceFrom" { ); const stream = &mem_stream.stream; - debug.assertOrPanic(mem.eql(u8, "Line 1", try readLineSliceFrom(stream, buf[0..]))); - debug.assertError(readLineSliceFrom(stream, buf[0..]), error.OutOfMemory); + testing.expectEqualSlices(u8, "Line 1", try readLineSliceFrom(stream, buf[0..])); + testing.expectError(error.OutOfMemory, readLineSliceFrom(stream, buf[0..])); } /// Creates a deserializer that deserializes types from any stream. diff --git a/std/io_test.zig b/std/io_test.zig index 9a0687ec69..fb6e0ae7e9 100644 --- a/std/io_test.zig +++ b/std/io_test.zig @@ -3,8 +3,8 @@ const io = std.io; const meta = std.meta; const trait = std.trait; const DefaultPrng = std.rand.DefaultPrng; -const assert = std.debug.assert; -const assertError = std.debug.assertError; +const expect = std.testing.expect; +const expectError = std.testing.expectError; const mem = std.mem; const os = std.os; const builtin = @import("builtin"); @@ -35,7 +35,7 @@ test "write a file, read it, then delete it" { const file_size = try file.getEndPos(); const expected_file_size = "begin".len + data.len + "end".len; - assert(file_size == expected_file_size); + expect(file_size == expected_file_size); var file_in_stream = file.inStream(); var buf_stream = io.BufferedInStream(os.File.ReadError).init(&file_in_stream.stream); @@ -43,9 +43,9 @@ test "write a file, read it, then delete it" { const contents = try st.readAllAlloc(allocator, 2 * 1024); defer allocator.free(contents); - assert(mem.eql(u8, contents[0.."begin".len], "begin")); - assert(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data)); - assert(mem.eql(u8, contents[contents.len - "end".len ..], "end")); + expect(mem.eql(u8, contents[0.."begin".len], "begin")); + expect(mem.eql(u8, contents["begin".len .. contents.len - "end".len], data)); + expect(mem.eql(u8, contents[contents.len - "end".len ..], "end")); } try os.deleteFile(tmp_file_name); } @@ -61,7 +61,7 @@ test "BufferOutStream" { const y: i32 = 1234; try buf_stream.print("x: {}\ny: {}\n", x, y); - assert(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n")); + expect(mem.eql(u8, buffer.toSlice(), "x: 42\ny: 1234\n")); } test "SliceInStream" { @@ -71,15 +71,15 @@ test "SliceInStream" { var dest: [4]u8 = undefined; var read = try ss.stream.read(dest[0..4]); - assert(read == 4); - assert(mem.eql(u8, dest[0..4], bytes[0..4])); + expect(read == 4); + expect(mem.eql(u8, dest[0..4], bytes[0..4])); read = try ss.stream.read(dest[0..4]); - assert(read == 3); - assert(mem.eql(u8, dest[0..3], bytes[4..7])); + expect(read == 3); + expect(mem.eql(u8, dest[0..3], bytes[4..7])); read = try ss.stream.read(dest[0..4]); - assert(read == 0); + expect(read == 0); } test "PeekStream" { @@ -93,26 +93,26 @@ test "PeekStream" { ps.putBackByte(10); var read = try ps.stream.read(dest[0..4]); - assert(read == 4); - assert(dest[0] == 10); - assert(dest[1] == 9); - assert(mem.eql(u8, dest[2..4], bytes[0..2])); + expect(read == 4); + expect(dest[0] == 10); + expect(dest[1] == 9); + expect(mem.eql(u8, dest[2..4], bytes[0..2])); read = try ps.stream.read(dest[0..4]); - assert(read == 4); - assert(mem.eql(u8, dest[0..4], bytes[2..6])); + expect(read == 4); + expect(mem.eql(u8, dest[0..4], bytes[2..6])); read = try ps.stream.read(dest[0..4]); - assert(read == 2); - assert(mem.eql(u8, dest[0..2], bytes[6..8])); + expect(read == 2); + expect(mem.eql(u8, dest[0..2], bytes[6..8])); ps.putBackByte(11); ps.putBackByte(12); read = try ps.stream.read(dest[0..4]); - assert(read == 2); - assert(dest[0] == 12); - assert(dest[1] == 11); + expect(read == 2); + expect(dest[0] == 12); + expect(dest[1] == 11); } test "SliceOutStream" { @@ -120,19 +120,19 @@ test "SliceOutStream" { var ss = io.SliceOutStream.init(buffer[0..]); try ss.stream.write("Hello"); - assert(mem.eql(u8, ss.getWritten(), "Hello")); + expect(mem.eql(u8, ss.getWritten(), "Hello")); try ss.stream.write("world"); - assert(mem.eql(u8, ss.getWritten(), "Helloworld")); + expect(mem.eql(u8, ss.getWritten(), "Helloworld")); - assertError(ss.stream.write("!"), error.OutOfSpace); - assert(mem.eql(u8, ss.getWritten(), "Helloworld")); + expectError(error.OutOfSpace, ss.stream.write("!")); + expect(mem.eql(u8, ss.getWritten(), "Helloworld")); ss.reset(); - assert(ss.getWritten().len == 0); + expect(ss.getWritten().len == 0); - assertError(ss.stream.write("Hello world!"), error.OutOfSpace); - assert(mem.eql(u8, ss.getWritten(), "Hello worl")); + expectError(error.OutOfSpace, ss.stream.write("Hello world!")); + expect(mem.eql(u8, ss.getWritten(), "Hello worl")); } test "BitInStream" { @@ -145,66 +145,66 @@ test "BitInStream" { var out_bits: usize = undefined; - assert(1 == try bit_stream_be.readBits(u2, 1, &out_bits)); - assert(out_bits == 1); - assert(2 == try bit_stream_be.readBits(u5, 2, &out_bits)); - assert(out_bits == 2); - assert(3 == try bit_stream_be.readBits(u128, 3, &out_bits)); - assert(out_bits == 3); - assert(4 == try bit_stream_be.readBits(u8, 4, &out_bits)); - assert(out_bits == 4); - assert(5 == try bit_stream_be.readBits(u9, 5, &out_bits)); - assert(out_bits == 5); - assert(1 == try bit_stream_be.readBits(u1, 1, &out_bits)); - assert(out_bits == 1); + expect(1 == try bit_stream_be.readBits(u2, 1, &out_bits)); + expect(out_bits == 1); + expect(2 == try bit_stream_be.readBits(u5, 2, &out_bits)); + expect(out_bits == 2); + expect(3 == try bit_stream_be.readBits(u128, 3, &out_bits)); + expect(out_bits == 3); + expect(4 == try bit_stream_be.readBits(u8, 4, &out_bits)); + expect(out_bits == 4); + expect(5 == try bit_stream_be.readBits(u9, 5, &out_bits)); + expect(out_bits == 5); + expect(1 == try bit_stream_be.readBits(u1, 1, &out_bits)); + expect(out_bits == 1); mem_in_be.pos = 0; bit_stream_be.bit_count = 0; - assert(0b110011010000101 == try bit_stream_be.readBits(u15, 15, &out_bits)); - assert(out_bits == 15); + expect(0b110011010000101 == try bit_stream_be.readBits(u15, 15, &out_bits)); + expect(out_bits == 15); mem_in_be.pos = 0; bit_stream_be.bit_count = 0; - assert(0b1100110100001011 == try bit_stream_be.readBits(u16, 16, &out_bits)); - assert(out_bits == 16); + expect(0b1100110100001011 == try bit_stream_be.readBits(u16, 16, &out_bits)); + expect(out_bits == 16); _ = try bit_stream_be.readBits(u0, 0, &out_bits); - assert(0 == try bit_stream_be.readBits(u1, 1, &out_bits)); - assert(out_bits == 0); - assertError(bit_stream_be.readBitsNoEof(u1, 1), error.EndOfStream); + expect(0 == try bit_stream_be.readBits(u1, 1, &out_bits)); + expect(out_bits == 0); + expectError(error.EndOfStream, bit_stream_be.readBitsNoEof(u1, 1)); var mem_in_le = io.SliceInStream.init(mem_le[0..]); var bit_stream_le = io.BitInStream(builtin.Endian.Little, InError).init(&mem_in_le.stream); - assert(1 == try bit_stream_le.readBits(u2, 1, &out_bits)); - assert(out_bits == 1); - assert(2 == try bit_stream_le.readBits(u5, 2, &out_bits)); - assert(out_bits == 2); - assert(3 == try bit_stream_le.readBits(u128, 3, &out_bits)); - assert(out_bits == 3); - assert(4 == try bit_stream_le.readBits(u8, 4, &out_bits)); - assert(out_bits == 4); - assert(5 == try bit_stream_le.readBits(u9, 5, &out_bits)); - assert(out_bits == 5); - assert(1 == try bit_stream_le.readBits(u1, 1, &out_bits)); - assert(out_bits == 1); + expect(1 == try bit_stream_le.readBits(u2, 1, &out_bits)); + expect(out_bits == 1); + expect(2 == try bit_stream_le.readBits(u5, 2, &out_bits)); + expect(out_bits == 2); + expect(3 == try bit_stream_le.readBits(u128, 3, &out_bits)); + expect(out_bits == 3); + expect(4 == try bit_stream_le.readBits(u8, 4, &out_bits)); + expect(out_bits == 4); + expect(5 == try bit_stream_le.readBits(u9, 5, &out_bits)); + expect(out_bits == 5); + expect(1 == try bit_stream_le.readBits(u1, 1, &out_bits)); + expect(out_bits == 1); mem_in_le.pos = 0; bit_stream_le.bit_count = 0; - assert(0b001010100011101 == try bit_stream_le.readBits(u15, 15, &out_bits)); - assert(out_bits == 15); + expect(0b001010100011101 == try bit_stream_le.readBits(u15, 15, &out_bits)); + expect(out_bits == 15); mem_in_le.pos = 0; bit_stream_le.bit_count = 0; - assert(0b1001010100011101 == try bit_stream_le.readBits(u16, 16, &out_bits)); - assert(out_bits == 16); + expect(0b1001010100011101 == try bit_stream_le.readBits(u16, 16, &out_bits)); + expect(out_bits == 16); _ = try bit_stream_le.readBits(u0, 0, &out_bits); - assert(0 == try bit_stream_le.readBits(u1, 1, &out_bits)); - assert(out_bits == 0); - assertError(bit_stream_le.readBitsNoEof(u1, 1), error.EndOfStream); + expect(0 == try bit_stream_le.readBits(u1, 1, &out_bits)); + expect(out_bits == 0); + expectError(error.EndOfStream, bit_stream_le.readBitsNoEof(u1, 1)); } test "BitOutStream" { @@ -222,17 +222,17 @@ test "BitOutStream" { try bit_stream_be.writeBits(u9(5), 5); try bit_stream_be.writeBits(u1(1), 1); - assert(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011); + expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001011); mem_out_be.pos = 0; try bit_stream_be.writeBits(u15(0b110011010000101), 15); try bit_stream_be.flushBits(); - assert(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010); + expect(mem_be[0] == 0b11001101 and mem_be[1] == 0b00001010); mem_out_be.pos = 0; try bit_stream_be.writeBits(u32(0b110011010000101), 16); - assert(mem_be[0] == 0b01100110 and mem_be[1] == 0b10000101); + expect(mem_be[0] == 0b01100110 and mem_be[1] == 0b10000101); try bit_stream_be.writeBits(u0(0), 0); @@ -246,16 +246,16 @@ test "BitOutStream" { try bit_stream_le.writeBits(u9(5), 5); try bit_stream_le.writeBits(u1(1), 1); - assert(mem_le[0] == 0b00011101 and mem_le[1] == 0b10010101); + expect(mem_le[0] == 0b00011101 and mem_le[1] == 0b10010101); mem_out_le.pos = 0; try bit_stream_le.writeBits(u15(0b110011010000101), 15); try bit_stream_le.flushBits(); - assert(mem_le[0] == 0b10000101 and mem_le[1] == 0b01100110); + expect(mem_le[0] == 0b10000101 and mem_le[1] == 0b01100110); mem_out_le.pos = 0; try bit_stream_le.writeBits(u32(0b1100110100001011), 16); - assert(mem_le[0] == 0b00001011 and mem_le[1] == 0b11001101); + expect(mem_le[0] == 0b00001011 and mem_le[1] == 0b11001101); try bit_stream_le.writeBits(u0(0), 0); } @@ -290,20 +290,20 @@ test "BitStreams with File Stream" { var out_bits: usize = undefined; - assert(1 == try bit_stream.readBits(u2, 1, &out_bits)); - assert(out_bits == 1); - assert(2 == try bit_stream.readBits(u5, 2, &out_bits)); - assert(out_bits == 2); - assert(3 == try bit_stream.readBits(u128, 3, &out_bits)); - assert(out_bits == 3); - assert(4 == try bit_stream.readBits(u8, 4, &out_bits)); - assert(out_bits == 4); - assert(5 == try bit_stream.readBits(u9, 5, &out_bits)); - assert(out_bits == 5); - assert(1 == try bit_stream.readBits(u1, 1, &out_bits)); - assert(out_bits == 1); + expect(1 == try bit_stream.readBits(u2, 1, &out_bits)); + expect(out_bits == 1); + expect(2 == try bit_stream.readBits(u5, 2, &out_bits)); + expect(out_bits == 2); + expect(3 == try bit_stream.readBits(u128, 3, &out_bits)); + expect(out_bits == 3); + expect(4 == try bit_stream.readBits(u8, 4, &out_bits)); + expect(out_bits == 4); + expect(5 == try bit_stream.readBits(u9, 5, &out_bits)); + expect(out_bits == 5); + expect(1 == try bit_stream.readBits(u1, 1, &out_bits)); + expect(out_bits == 1); - assertError(bit_stream.readBitsNoEof(u1, 1), error.EndOfStream); + expectError(error.EndOfStream, bit_stream.readBitsNoEof(u1, 1)); } try os.deleteFile(tmp_file_name); } @@ -345,8 +345,8 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa const S = @IntType(true, i); const x = try deserializer.deserializeInt(U); const y = try deserializer.deserializeInt(S); - assert(x == U(i)); - if (i != 0) assert(y == S(-1)) else assert(y == 0); + expect(x == U(i)); + if (i != 0) expect(y == S(-1)) else expect(y == 0); } const u8_bit_count = comptime meta.bitCount(u8); @@ -356,7 +356,7 @@ fn testIntSerializerDeserializer(comptime endian: builtin.Endian, comptime is_pa const extra_packed_byte = @boolToInt(total_bits % u8_bit_count > 0); const total_packed_bytes = (total_bits / u8_bit_count) + extra_packed_byte; - assert(in.pos == if (is_packed) total_packed_bytes else total_bytes); + expect(in.pos == if (is_packed) total_packed_bytes else total_bytes); //Verify that empty error set works with serializer. //deserializer is covered by SliceInStream @@ -408,14 +408,14 @@ fn testIntSerializerDeserializerInfNaN(comptime endian: builtin.Endian, const inf_check_f64 = try deserializer.deserialize(f64); //const nan_check_f128 = try deserializer.deserialize(f128); //const inf_check_f128 = try deserializer.deserialize(f128); - assert(std.math.isNan(nan_check_f16)); - assert(std.math.isInf(inf_check_f16)); - assert(std.math.isNan(nan_check_f32)); - assert(std.math.isInf(inf_check_f32)); - assert(std.math.isNan(nan_check_f64)); - assert(std.math.isInf(inf_check_f64)); - //assert(std.math.isNan(nan_check_f128)); - //assert(std.math.isInf(inf_check_f128)); + expect(std.math.isNan(nan_check_f16)); + expect(std.math.isInf(inf_check_f16)); + expect(std.math.isNan(nan_check_f32)); + expect(std.math.isInf(inf_check_f32)); + expect(std.math.isNan(nan_check_f64)); + expect(std.math.isInf(inf_check_f64)); + //expect(std.math.isNan(nan_check_f128)); + //expect(std.math.isInf(inf_check_f128)); } test "Serializer/Deserializer Int: Inf/NaN" { @@ -528,7 +528,7 @@ fn testSerializerDeserializer(comptime endian: builtin.Endian, comptime is_packe try serializer.serialize(my_inst); const my_copy = try deserializer.deserialize(MyStruct); - assert(meta.eql(my_copy, my_inst)); + expect(meta.eql(my_copy, my_inst)); } test "Serializer/Deserializer generic" { @@ -565,11 +565,11 @@ fn testBadData(comptime endian: builtin.Endian, comptime is_packed: bool) !void var deserializer = io.Deserializer(endian, is_packed, InError).init(in_stream); try serializer.serialize(u14(3)); - assertError(deserializer.deserialize(A), error.InvalidEnumTag); + expectError(error.InvalidEnumTag, deserializer.deserialize(A)); out.pos = 0; try serializer.serialize(u14(3)); try serializer.serialize(u14(88)); - assertError(deserializer.deserialize(C), error.InvalidEnumTag); + expectError(error.InvalidEnumTag, deserializer.deserialize(C)); } test "Deserializer bad data" { diff --git a/std/json.zig b/std/json.zig index 4d07d7b89d..d8f28560e5 100644 --- a/std/json.zig +++ b/std/json.zig @@ -4,6 +4,7 @@ const std = @import("index.zig"); const debug = std.debug; +const testing = std.testing; const mem = std.mem; const maxInt = std.math.maxInt; @@ -960,7 +961,7 @@ test "json.token" { checkNext(&p, Token.Id.ObjectEnd); checkNext(&p, Token.Id.ObjectEnd); - debug.assert((try p.next()) == null); + testing.expect((try p.next()) == null); } // Validate a JSON string. This does not limit number precision so a decoder may not necessarily @@ -981,7 +982,7 @@ pub fn validate(s: []const u8) bool { } test "json.validate" { - debug.assert(validate("{}")); + testing.expect(validate("{}")); } const Allocator = std.mem.Allocator; @@ -1378,20 +1379,20 @@ test "json.parser.dynamic" { var image = root.Object.get("Image").?.value; const width = image.Object.get("Width").?.value; - debug.assert(width.Integer == 800); + testing.expect(width.Integer == 800); const height = image.Object.get("Height").?.value; - debug.assert(height.Integer == 600); + testing.expect(height.Integer == 600); const title = image.Object.get("Title").?.value; - debug.assert(mem.eql(u8, title.String, "View from 15th Floor")); + testing.expect(mem.eql(u8, title.String, "View from 15th Floor")); const animated = image.Object.get("Animated").?.value; - debug.assert(animated.Bool == false); + testing.expect(animated.Bool == false); const array_of_object = image.Object.get("ArrayOfObject").?.value; - debug.assert(array_of_object.Array.len == 1); + testing.expect(array_of_object.Array.len == 1); const obj0 = array_of_object.Array.at(0).Object.get("n").?.value; - debug.assert(mem.eql(u8, obj0.String, "m")); + testing.expect(mem.eql(u8, obj0.String, "m")); } diff --git a/std/json_test.zig b/std/json_test.zig index 9e19ec592a..edc50be8cb 100644 --- a/std/json_test.zig +++ b/std/json_test.zig @@ -6,15 +6,15 @@ const std = @import("index.zig"); fn ok(comptime s: []const u8) void { - std.debug.assert(std.json.validate(s)); + std.testing.expect(std.json.validate(s)); } fn err(comptime s: []const u8) void { - std.debug.assert(!std.json.validate(s)); + std.testing.expect(!std.json.validate(s)); } fn any(comptime s: []const u8) void { - std.debug.assert(true); + std.testing.expect(true); } //////////////////////////////////////////////////////////////////////////////////////////////////// diff --git a/std/lazy_init.zig b/std/lazy_init.zig index f08c01e874..a09168786b 100644 --- a/std/lazy_init.zig +++ b/std/lazy_init.zig @@ -1,6 +1,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const testing = std.testing; const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -63,12 +64,12 @@ test "std.lazyInit" { global_number.resolve(); } if (global_number.get()) |x| { - assert(x.* == 1234); + testing.expect(x.* == 1234); } else { @panic("bad"); } if (global_number.get()) |x| { - assert(x.* == 1234); + testing.expect(x.* == 1234); } else { @panic("bad"); } @@ -80,6 +81,6 @@ test "std.lazyInit(void)" { if (global_void.get()) |_| @panic("bad") else { global_void.resolve(); } - assert(global_void.get() != null); - assert(global_void.get() != null); + testing.expect(global_void.get() != null); + testing.expect(global_void.get() != null); } diff --git a/std/linked_list.zig b/std/linked_list.zig index 7021cac707..86e5cd056e 100644 --- a/std/linked_list.zig +++ b/std/linked_list.zig @@ -1,6 +1,7 @@ const std = @import("index.zig"); const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const mem = std.mem; const Allocator = mem.Allocator; @@ -246,7 +247,7 @@ test "basic linked list test" { var it = list.first; var index: u32 = 1; while (it) |node| : (it = node.next) { - assert(node.data == index); + testing.expect(node.data == index); index += 1; } } @@ -256,7 +257,7 @@ test "basic linked list test" { var it = list.last; var index: u32 = 1; while (it) |node| : (it = node.prev) { - assert(node.data == (6 - index)); + testing.expect(node.data == (6 - index)); index += 1; } } @@ -265,9 +266,9 @@ test "basic linked list test" { var last = list.pop(); // {2, 3, 4} list.remove(three); // {2, 4} - assert(list.first.?.data == 2); - assert(list.last.?.data == 4); - assert(list.len == 2); + testing.expect(list.first.?.data == 2); + testing.expect(list.last.?.data == 4); + testing.expect(list.len == 2); } test "linked list concatenation" { @@ -294,18 +295,18 @@ test "linked list concatenation" { list1.concatByMoving(&list2); - assert(list1.last == five); - assert(list1.len == 5); - assert(list2.first == null); - assert(list2.last == null); - assert(list2.len == 0); + testing.expect(list1.last == five); + testing.expect(list1.len == 5); + testing.expect(list2.first == null); + testing.expect(list2.last == null); + testing.expect(list2.len == 0); // Traverse forwards. { var it = list1.first; var index: u32 = 1; while (it) |node| : (it = node.next) { - assert(node.data == index); + testing.expect(node.data == index); index += 1; } } @@ -315,7 +316,7 @@ test "linked list concatenation" { var it = list1.last; var index: u32 = 1; while (it) |node| : (it = node.prev) { - assert(node.data == (6 - index)); + testing.expect(node.data == (6 - index)); index += 1; } } @@ -328,7 +329,7 @@ test "linked list concatenation" { var it = list2.first; var index: u32 = 1; while (it) |node| : (it = node.next) { - assert(node.data == index); + testing.expect(node.data == index); index += 1; } } @@ -338,7 +339,7 @@ test "linked list concatenation" { var it = list2.last; var index: u32 = 1; while (it) |node| : (it = node.prev) { - assert(node.data == (6 - index)); + testing.expect(node.data == (6 - index)); index += 1; } } diff --git a/std/math/acos.zig b/std/math/acos.zig index 54844e8f6e..734f7a8651 100644 --- a/std/math/acos.zig +++ b/std/math/acos.zig @@ -4,7 +4,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn acos(x: var) @typeOf(x) { const T = @typeOf(x); @@ -143,38 +143,38 @@ fn acos64(x: f64) f64 { } test "math.acos" { - assert(acos(f32(0.0)) == acos32(0.0)); - assert(acos(f64(0.0)) == acos64(0.0)); + expect(acos(f32(0.0)) == acos32(0.0)); + expect(acos(f64(0.0)) == acos64(0.0)); } test "math.acos32" { const epsilon = 0.000001; - assert(math.approxEq(f32, acos32(0.0), 1.570796, epsilon)); - assert(math.approxEq(f32, acos32(0.2), 1.369438, epsilon)); - assert(math.approxEq(f32, acos32(0.3434), 1.220262, epsilon)); - assert(math.approxEq(f32, acos32(0.5), 1.047198, epsilon)); - assert(math.approxEq(f32, acos32(0.8923), 0.468382, epsilon)); - assert(math.approxEq(f32, acos32(-0.2), 1.772154, epsilon)); + expect(math.approxEq(f32, acos32(0.0), 1.570796, epsilon)); + expect(math.approxEq(f32, acos32(0.2), 1.369438, epsilon)); + expect(math.approxEq(f32, acos32(0.3434), 1.220262, epsilon)); + expect(math.approxEq(f32, acos32(0.5), 1.047198, epsilon)); + expect(math.approxEq(f32, acos32(0.8923), 0.468382, epsilon)); + expect(math.approxEq(f32, acos32(-0.2), 1.772154, epsilon)); } test "math.acos64" { const epsilon = 0.000001; - assert(math.approxEq(f64, acos64(0.0), 1.570796, epsilon)); - assert(math.approxEq(f64, acos64(0.2), 1.369438, epsilon)); - assert(math.approxEq(f64, acos64(0.3434), 1.220262, epsilon)); - assert(math.approxEq(f64, acos64(0.5), 1.047198, epsilon)); - assert(math.approxEq(f64, acos64(0.8923), 0.468382, epsilon)); - assert(math.approxEq(f64, acos64(-0.2), 1.772154, epsilon)); + expect(math.approxEq(f64, acos64(0.0), 1.570796, epsilon)); + expect(math.approxEq(f64, acos64(0.2), 1.369438, epsilon)); + expect(math.approxEq(f64, acos64(0.3434), 1.220262, epsilon)); + expect(math.approxEq(f64, acos64(0.5), 1.047198, epsilon)); + expect(math.approxEq(f64, acos64(0.8923), 0.468382, epsilon)); + expect(math.approxEq(f64, acos64(-0.2), 1.772154, epsilon)); } test "math.acos32.special" { - assert(math.isNan(acos32(-2))); - assert(math.isNan(acos32(1.5))); + expect(math.isNan(acos32(-2))); + expect(math.isNan(acos32(1.5))); } test "math.acos64.special" { - assert(math.isNan(acos64(-2))); - assert(math.isNan(acos64(1.5))); + expect(math.isNan(acos64(-2))); + expect(math.isNan(acos64(1.5))); } diff --git a/std/math/acosh.zig b/std/math/acosh.zig index 9be323e1f6..a9c4a908ef 100644 --- a/std/math/acosh.zig +++ b/std/math/acosh.zig @@ -6,7 +6,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn acosh(x: var) @typeOf(x) { const T = @typeOf(x); @@ -55,34 +55,34 @@ fn acosh64(x: f64) f64 { } test "math.acosh" { - assert(acosh(f32(1.5)) == acosh32(1.5)); - assert(acosh(f64(1.5)) == acosh64(1.5)); + expect(acosh(f32(1.5)) == acosh32(1.5)); + expect(acosh(f64(1.5)) == acosh64(1.5)); } test "math.acosh32" { const epsilon = 0.000001; - assert(math.approxEq(f32, acosh32(1.5), 0.962424, epsilon)); - assert(math.approxEq(f32, acosh32(37.45), 4.315976, epsilon)); - assert(math.approxEq(f32, acosh32(89.123), 5.183133, epsilon)); - assert(math.approxEq(f32, acosh32(123123.234375), 12.414088, epsilon)); + expect(math.approxEq(f32, acosh32(1.5), 0.962424, epsilon)); + expect(math.approxEq(f32, acosh32(37.45), 4.315976, epsilon)); + expect(math.approxEq(f32, acosh32(89.123), 5.183133, epsilon)); + expect(math.approxEq(f32, acosh32(123123.234375), 12.414088, epsilon)); } test "math.acosh64" { const epsilon = 0.000001; - assert(math.approxEq(f64, acosh64(1.5), 0.962424, epsilon)); - assert(math.approxEq(f64, acosh64(37.45), 4.315976, epsilon)); - assert(math.approxEq(f64, acosh64(89.123), 5.183133, epsilon)); - assert(math.approxEq(f64, acosh64(123123.234375), 12.414088, epsilon)); + expect(math.approxEq(f64, acosh64(1.5), 0.962424, epsilon)); + expect(math.approxEq(f64, acosh64(37.45), 4.315976, epsilon)); + expect(math.approxEq(f64, acosh64(89.123), 5.183133, epsilon)); + expect(math.approxEq(f64, acosh64(123123.234375), 12.414088, epsilon)); } test "math.acosh32.special" { - assert(math.isNan(acosh32(math.nan(f32)))); - assert(math.isSignalNan(acosh32(0.5))); + expect(math.isNan(acosh32(math.nan(f32)))); + expect(math.isSignalNan(acosh32(0.5))); } test "math.acosh64.special" { - assert(math.isNan(acosh64(math.nan(f64)))); - assert(math.isSignalNan(acosh64(0.5))); + expect(math.isNan(acosh64(math.nan(f64)))); + expect(math.isSignalNan(acosh64(0.5))); } diff --git a/std/math/asin.zig b/std/math/asin.zig index 30b3a57e32..c9dbdf704f 100644 --- a/std/math/asin.zig +++ b/std/math/asin.zig @@ -5,7 +5,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn asin(x: var) @typeOf(x) { const T = @typeOf(x); @@ -136,42 +136,42 @@ fn asin64(x: f64) f64 { } test "math.asin" { - assert(asin(f32(0.0)) == asin32(0.0)); - assert(asin(f64(0.0)) == asin64(0.0)); + expect(asin(f32(0.0)) == asin32(0.0)); + expect(asin(f64(0.0)) == asin64(0.0)); } test "math.asin32" { const epsilon = 0.000001; - assert(math.approxEq(f32, asin32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, asin32(0.2), 0.201358, epsilon)); - assert(math.approxEq(f32, asin32(-0.2), -0.201358, epsilon)); - assert(math.approxEq(f32, asin32(0.3434), 0.350535, epsilon)); - assert(math.approxEq(f32, asin32(0.5), 0.523599, epsilon)); - assert(math.approxEq(f32, asin32(0.8923), 1.102415, epsilon)); + expect(math.approxEq(f32, asin32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, asin32(0.2), 0.201358, epsilon)); + expect(math.approxEq(f32, asin32(-0.2), -0.201358, epsilon)); + expect(math.approxEq(f32, asin32(0.3434), 0.350535, epsilon)); + expect(math.approxEq(f32, asin32(0.5), 0.523599, epsilon)); + expect(math.approxEq(f32, asin32(0.8923), 1.102415, epsilon)); } test "math.asin64" { const epsilon = 0.000001; - assert(math.approxEq(f64, asin64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, asin64(0.2), 0.201358, epsilon)); - assert(math.approxEq(f64, asin64(-0.2), -0.201358, epsilon)); - assert(math.approxEq(f64, asin64(0.3434), 0.350535, epsilon)); - assert(math.approxEq(f64, asin64(0.5), 0.523599, epsilon)); - assert(math.approxEq(f64, asin64(0.8923), 1.102415, epsilon)); + expect(math.approxEq(f64, asin64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, asin64(0.2), 0.201358, epsilon)); + expect(math.approxEq(f64, asin64(-0.2), -0.201358, epsilon)); + expect(math.approxEq(f64, asin64(0.3434), 0.350535, epsilon)); + expect(math.approxEq(f64, asin64(0.5), 0.523599, epsilon)); + expect(math.approxEq(f64, asin64(0.8923), 1.102415, epsilon)); } test "math.asin32.special" { - assert(asin32(0.0) == 0.0); - assert(asin32(-0.0) == -0.0); - assert(math.isNan(asin32(-2))); - assert(math.isNan(asin32(1.5))); + expect(asin32(0.0) == 0.0); + expect(asin32(-0.0) == -0.0); + expect(math.isNan(asin32(-2))); + expect(math.isNan(asin32(1.5))); } test "math.asin64.special" { - assert(asin64(0.0) == 0.0); - assert(asin64(-0.0) == -0.0); - assert(math.isNan(asin64(-2))); - assert(math.isNan(asin64(1.5))); + expect(asin64(0.0) == 0.0); + expect(asin64(-0.0) == -0.0); + expect(math.isNan(asin64(-2))); + expect(math.isNan(asin64(1.5))); } diff --git a/std/math/asinh.zig b/std/math/asinh.zig index 98892bcbcb..05bd8d008e 100644 --- a/std/math/asinh.zig +++ b/std/math/asinh.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn asinh(x: var) @typeOf(x) { @@ -83,46 +83,46 @@ fn asinh64(x: f64) f64 { } test "math.asinh" { - assert(asinh(f32(0.0)) == asinh32(0.0)); - assert(asinh(f64(0.0)) == asinh64(0.0)); + expect(asinh(f32(0.0)) == asinh32(0.0)); + expect(asinh(f64(0.0)) == asinh64(0.0)); } test "math.asinh32" { const epsilon = 0.000001; - assert(math.approxEq(f32, asinh32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, asinh32(0.2), 0.198690, epsilon)); - assert(math.approxEq(f32, asinh32(0.8923), 0.803133, epsilon)); - assert(math.approxEq(f32, asinh32(1.5), 1.194763, epsilon)); - assert(math.approxEq(f32, asinh32(37.45), 4.316332, epsilon)); - assert(math.approxEq(f32, asinh32(89.123), 5.183196, epsilon)); - assert(math.approxEq(f32, asinh32(123123.234375), 12.414088, epsilon)); + expect(math.approxEq(f32, asinh32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, asinh32(0.2), 0.198690, epsilon)); + expect(math.approxEq(f32, asinh32(0.8923), 0.803133, epsilon)); + expect(math.approxEq(f32, asinh32(1.5), 1.194763, epsilon)); + expect(math.approxEq(f32, asinh32(37.45), 4.316332, epsilon)); + expect(math.approxEq(f32, asinh32(89.123), 5.183196, epsilon)); + expect(math.approxEq(f32, asinh32(123123.234375), 12.414088, epsilon)); } test "math.asinh64" { const epsilon = 0.000001; - assert(math.approxEq(f64, asinh64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, asinh64(0.2), 0.198690, epsilon)); - assert(math.approxEq(f64, asinh64(0.8923), 0.803133, epsilon)); - assert(math.approxEq(f64, asinh64(1.5), 1.194763, epsilon)); - assert(math.approxEq(f64, asinh64(37.45), 4.316332, epsilon)); - assert(math.approxEq(f64, asinh64(89.123), 5.183196, epsilon)); - assert(math.approxEq(f64, asinh64(123123.234375), 12.414088, epsilon)); + expect(math.approxEq(f64, asinh64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, asinh64(0.2), 0.198690, epsilon)); + expect(math.approxEq(f64, asinh64(0.8923), 0.803133, epsilon)); + expect(math.approxEq(f64, asinh64(1.5), 1.194763, epsilon)); + expect(math.approxEq(f64, asinh64(37.45), 4.316332, epsilon)); + expect(math.approxEq(f64, asinh64(89.123), 5.183196, epsilon)); + expect(math.approxEq(f64, asinh64(123123.234375), 12.414088, epsilon)); } test "math.asinh32.special" { - assert(asinh32(0.0) == 0.0); - assert(asinh32(-0.0) == -0.0); - assert(math.isPositiveInf(asinh32(math.inf(f32)))); - assert(math.isNegativeInf(asinh32(-math.inf(f32)))); - assert(math.isNan(asinh32(math.nan(f32)))); + expect(asinh32(0.0) == 0.0); + expect(asinh32(-0.0) == -0.0); + expect(math.isPositiveInf(asinh32(math.inf(f32)))); + expect(math.isNegativeInf(asinh32(-math.inf(f32)))); + expect(math.isNan(asinh32(math.nan(f32)))); } test "math.asinh64.special" { - assert(asinh64(0.0) == 0.0); - assert(asinh64(-0.0) == -0.0); - assert(math.isPositiveInf(asinh64(math.inf(f64)))); - assert(math.isNegativeInf(asinh64(-math.inf(f64)))); - assert(math.isNan(asinh64(math.nan(f64)))); + expect(asinh64(0.0) == 0.0); + expect(asinh64(-0.0) == -0.0); + expect(math.isPositiveInf(asinh64(math.inf(f64)))); + expect(math.isNegativeInf(asinh64(-math.inf(f64)))); + expect(math.isNan(asinh64(math.nan(f64)))); } diff --git a/std/math/atan.zig b/std/math/atan.zig index 6ca94dd84a..72d17b4db2 100644 --- a/std/math/atan.zig +++ b/std/math/atan.zig @@ -5,7 +5,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn atan(x: var) @typeOf(x) { const T = @typeOf(x); @@ -206,44 +206,44 @@ fn atan64(x_: f64) f64 { } test "math.atan" { - assert(@bitCast(u32, atan(f32(0.2))) == @bitCast(u32, atan32(0.2))); - assert(atan(f64(0.2)) == atan64(0.2)); + expect(@bitCast(u32, atan(f32(0.2))) == @bitCast(u32, atan32(0.2))); + expect(atan(f64(0.2)) == atan64(0.2)); } test "math.atan32" { const epsilon = 0.000001; - assert(math.approxEq(f32, atan32(0.2), 0.197396, epsilon)); - assert(math.approxEq(f32, atan32(-0.2), -0.197396, epsilon)); - assert(math.approxEq(f32, atan32(0.3434), 0.330783, epsilon)); - assert(math.approxEq(f32, atan32(0.8923), 0.728545, epsilon)); - assert(math.approxEq(f32, atan32(1.5), 0.982794, epsilon)); + expect(math.approxEq(f32, atan32(0.2), 0.197396, epsilon)); + expect(math.approxEq(f32, atan32(-0.2), -0.197396, epsilon)); + expect(math.approxEq(f32, atan32(0.3434), 0.330783, epsilon)); + expect(math.approxEq(f32, atan32(0.8923), 0.728545, epsilon)); + expect(math.approxEq(f32, atan32(1.5), 0.982794, epsilon)); } test "math.atan64" { const epsilon = 0.000001; - assert(math.approxEq(f64, atan64(0.2), 0.197396, epsilon)); - assert(math.approxEq(f64, atan64(-0.2), -0.197396, epsilon)); - assert(math.approxEq(f64, atan64(0.3434), 0.330783, epsilon)); - assert(math.approxEq(f64, atan64(0.8923), 0.728545, epsilon)); - assert(math.approxEq(f64, atan64(1.5), 0.982794, epsilon)); + expect(math.approxEq(f64, atan64(0.2), 0.197396, epsilon)); + expect(math.approxEq(f64, atan64(-0.2), -0.197396, epsilon)); + expect(math.approxEq(f64, atan64(0.3434), 0.330783, epsilon)); + expect(math.approxEq(f64, atan64(0.8923), 0.728545, epsilon)); + expect(math.approxEq(f64, atan64(1.5), 0.982794, epsilon)); } test "math.atan32.special" { const epsilon = 0.000001; - assert(atan32(0.0) == 0.0); - assert(atan32(-0.0) == -0.0); - assert(math.approxEq(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon)); + expect(atan32(0.0) == 0.0); + expect(atan32(-0.0) == -0.0); + expect(math.approxEq(f32, atan32(math.inf(f32)), math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan32(-math.inf(f32)), -math.pi / 2.0, epsilon)); } test "math.atan64.special" { const epsilon = 0.000001; - assert(atan64(0.0) == 0.0); - assert(atan64(-0.0) == -0.0); - assert(math.approxEq(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon)); + expect(atan64(0.0) == 0.0); + expect(atan64(-0.0) == -0.0); + expect(math.approxEq(f64, atan64(math.inf(f64)), math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan64(-math.inf(f64)), -math.pi / 2.0, epsilon)); } diff --git a/std/math/atan2.zig b/std/math/atan2.zig index a7757132d7..6e1f67cfbb 100644 --- a/std/math/atan2.zig +++ b/std/math/atan2.zig @@ -20,7 +20,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn atan2(comptime T: type, y: T, x: T) T { return switch (T) { @@ -206,78 +206,78 @@ fn atan2_64(y: f64, x: f64) f64 { } test "math.atan2" { - assert(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21)); - assert(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21)); + expect(atan2(f32, 0.2, 0.21) == atan2_32(0.2, 0.21)); + expect(atan2(f64, 0.2, 0.21) == atan2_64(0.2, 0.21)); } test "math.atan2_32" { const epsilon = 0.000001; - assert(math.approxEq(f32, atan2_32(0.0, 0.0), 0.0, epsilon)); - assert(math.approxEq(f32, atan2_32(0.2, 0.2), 0.785398, epsilon)); - assert(math.approxEq(f32, atan2_32(-0.2, 0.2), -0.785398, epsilon)); - assert(math.approxEq(f32, atan2_32(0.2, -0.2), 2.356194, epsilon)); - assert(math.approxEq(f32, atan2_32(-0.2, -0.2), -2.356194, epsilon)); - assert(math.approxEq(f32, atan2_32(0.34, -0.4), 2.437099, epsilon)); - assert(math.approxEq(f32, atan2_32(0.34, 1.243), 0.267001, epsilon)); + expect(math.approxEq(f32, atan2_32(0.0, 0.0), 0.0, epsilon)); + expect(math.approxEq(f32, atan2_32(0.2, 0.2), 0.785398, epsilon)); + expect(math.approxEq(f32, atan2_32(-0.2, 0.2), -0.785398, epsilon)); + expect(math.approxEq(f32, atan2_32(0.2, -0.2), 2.356194, epsilon)); + expect(math.approxEq(f32, atan2_32(-0.2, -0.2), -2.356194, epsilon)); + expect(math.approxEq(f32, atan2_32(0.34, -0.4), 2.437099, epsilon)); + expect(math.approxEq(f32, atan2_32(0.34, 1.243), 0.267001, epsilon)); } test "math.atan2_64" { const epsilon = 0.000001; - assert(math.approxEq(f64, atan2_64(0.0, 0.0), 0.0, epsilon)); - assert(math.approxEq(f64, atan2_64(0.2, 0.2), 0.785398, epsilon)); - assert(math.approxEq(f64, atan2_64(-0.2, 0.2), -0.785398, epsilon)); - assert(math.approxEq(f64, atan2_64(0.2, -0.2), 2.356194, epsilon)); - assert(math.approxEq(f64, atan2_64(-0.2, -0.2), -2.356194, epsilon)); - assert(math.approxEq(f64, atan2_64(0.34, -0.4), 2.437099, epsilon)); - assert(math.approxEq(f64, atan2_64(0.34, 1.243), 0.267001, epsilon)); + expect(math.approxEq(f64, atan2_64(0.0, 0.0), 0.0, epsilon)); + expect(math.approxEq(f64, atan2_64(0.2, 0.2), 0.785398, epsilon)); + expect(math.approxEq(f64, atan2_64(-0.2, 0.2), -0.785398, epsilon)); + expect(math.approxEq(f64, atan2_64(0.2, -0.2), 2.356194, epsilon)); + expect(math.approxEq(f64, atan2_64(-0.2, -0.2), -2.356194, epsilon)); + expect(math.approxEq(f64, atan2_64(0.34, -0.4), 2.437099, epsilon)); + expect(math.approxEq(f64, atan2_64(0.34, 1.243), 0.267001, epsilon)); } test "math.atan2_32.special" { const epsilon = 0.000001; - assert(math.isNan(atan2_32(1.0, math.nan(f32)))); - assert(math.isNan(atan2_32(math.nan(f32), 1.0))); - assert(atan2_32(0.0, 5.0) == 0.0); - assert(atan2_32(-0.0, 5.0) == -0.0); - assert(math.approxEq(f32, atan2_32(0.0, -5.0), math.pi, epsilon)); - //assert(math.approxEq(f32, atan2_32(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero? - assert(math.approxEq(f32, atan2_32(1.0, 0.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan2_32(1.0, -0.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan2_32(-1.0, 0.0), -math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan2_32(-1.0, -0.0), -math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan2_32(math.inf(f32), math.inf(f32)), math.pi / 4.0, epsilon)); - assert(math.approxEq(f32, atan2_32(-math.inf(f32), math.inf(f32)), -math.pi / 4.0, epsilon)); - assert(math.approxEq(f32, atan2_32(math.inf(f32), -math.inf(f32)), 3.0 * math.pi / 4.0, epsilon)); - assert(math.approxEq(f32, atan2_32(-math.inf(f32), -math.inf(f32)), -3.0 * math.pi / 4.0, epsilon)); - assert(atan2_32(1.0, math.inf(f32)) == 0.0); - assert(math.approxEq(f32, atan2_32(1.0, -math.inf(f32)), math.pi, epsilon)); - assert(math.approxEq(f32, atan2_32(-1.0, -math.inf(f32)), -math.pi, epsilon)); - assert(math.approxEq(f32, atan2_32(math.inf(f32), 1.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f32, atan2_32(-math.inf(f32), 1.0), -math.pi / 2.0, epsilon)); + expect(math.isNan(atan2_32(1.0, math.nan(f32)))); + expect(math.isNan(atan2_32(math.nan(f32), 1.0))); + expect(atan2_32(0.0, 5.0) == 0.0); + expect(atan2_32(-0.0, 5.0) == -0.0); + expect(math.approxEq(f32, atan2_32(0.0, -5.0), math.pi, epsilon)); + //expect(math.approxEq(f32, atan2_32(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero? + expect(math.approxEq(f32, atan2_32(1.0, 0.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan2_32(1.0, -0.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan2_32(-1.0, 0.0), -math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan2_32(-1.0, -0.0), -math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan2_32(math.inf(f32), math.inf(f32)), math.pi / 4.0, epsilon)); + expect(math.approxEq(f32, atan2_32(-math.inf(f32), math.inf(f32)), -math.pi / 4.0, epsilon)); + expect(math.approxEq(f32, atan2_32(math.inf(f32), -math.inf(f32)), 3.0 * math.pi / 4.0, epsilon)); + expect(math.approxEq(f32, atan2_32(-math.inf(f32), -math.inf(f32)), -3.0 * math.pi / 4.0, epsilon)); + expect(atan2_32(1.0, math.inf(f32)) == 0.0); + expect(math.approxEq(f32, atan2_32(1.0, -math.inf(f32)), math.pi, epsilon)); + expect(math.approxEq(f32, atan2_32(-1.0, -math.inf(f32)), -math.pi, epsilon)); + expect(math.approxEq(f32, atan2_32(math.inf(f32), 1.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f32, atan2_32(-math.inf(f32), 1.0), -math.pi / 2.0, epsilon)); } test "math.atan2_64.special" { const epsilon = 0.000001; - assert(math.isNan(atan2_64(1.0, math.nan(f64)))); - assert(math.isNan(atan2_64(math.nan(f64), 1.0))); - assert(atan2_64(0.0, 5.0) == 0.0); - assert(atan2_64(-0.0, 5.0) == -0.0); - assert(math.approxEq(f64, atan2_64(0.0, -5.0), math.pi, epsilon)); - //assert(math.approxEq(f64, atan2_64(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero? - assert(math.approxEq(f64, atan2_64(1.0, 0.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan2_64(1.0, -0.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan2_64(-1.0, 0.0), -math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan2_64(-1.0, -0.0), -math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan2_64(math.inf(f64), math.inf(f64)), math.pi / 4.0, epsilon)); - assert(math.approxEq(f64, atan2_64(-math.inf(f64), math.inf(f64)), -math.pi / 4.0, epsilon)); - assert(math.approxEq(f64, atan2_64(math.inf(f64), -math.inf(f64)), 3.0 * math.pi / 4.0, epsilon)); - assert(math.approxEq(f64, atan2_64(-math.inf(f64), -math.inf(f64)), -3.0 * math.pi / 4.0, epsilon)); - assert(atan2_64(1.0, math.inf(f64)) == 0.0); - assert(math.approxEq(f64, atan2_64(1.0, -math.inf(f64)), math.pi, epsilon)); - assert(math.approxEq(f64, atan2_64(-1.0, -math.inf(f64)), -math.pi, epsilon)); - assert(math.approxEq(f64, atan2_64(math.inf(f64), 1.0), math.pi / 2.0, epsilon)); - assert(math.approxEq(f64, atan2_64(-math.inf(f64), 1.0), -math.pi / 2.0, epsilon)); + expect(math.isNan(atan2_64(1.0, math.nan(f64)))); + expect(math.isNan(atan2_64(math.nan(f64), 1.0))); + expect(atan2_64(0.0, 5.0) == 0.0); + expect(atan2_64(-0.0, 5.0) == -0.0); + expect(math.approxEq(f64, atan2_64(0.0, -5.0), math.pi, epsilon)); + //expect(math.approxEq(f64, atan2_64(-0.0, -5.0), -math.pi, epsilon)); TODO support negative zero? + expect(math.approxEq(f64, atan2_64(1.0, 0.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan2_64(1.0, -0.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan2_64(-1.0, 0.0), -math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan2_64(-1.0, -0.0), -math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan2_64(math.inf(f64), math.inf(f64)), math.pi / 4.0, epsilon)); + expect(math.approxEq(f64, atan2_64(-math.inf(f64), math.inf(f64)), -math.pi / 4.0, epsilon)); + expect(math.approxEq(f64, atan2_64(math.inf(f64), -math.inf(f64)), 3.0 * math.pi / 4.0, epsilon)); + expect(math.approxEq(f64, atan2_64(-math.inf(f64), -math.inf(f64)), -3.0 * math.pi / 4.0, epsilon)); + expect(atan2_64(1.0, math.inf(f64)) == 0.0); + expect(math.approxEq(f64, atan2_64(1.0, -math.inf(f64)), math.pi, epsilon)); + expect(math.approxEq(f64, atan2_64(-1.0, -math.inf(f64)), -math.pi, epsilon)); + expect(math.approxEq(f64, atan2_64(math.inf(f64), 1.0), math.pi / 2.0, epsilon)); + expect(math.approxEq(f64, atan2_64(-math.inf(f64), 1.0), -math.pi / 2.0, epsilon)); } diff --git a/std/math/atanh.zig b/std/math/atanh.zig index c97855c234..f2feab0207 100644 --- a/std/math/atanh.zig +++ b/std/math/atanh.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn atanh(x: var) @typeOf(x) { @@ -78,38 +78,38 @@ fn atanh_64(x: f64) f64 { } test "math.atanh" { - assert(atanh(f32(0.0)) == atanh_32(0.0)); - assert(atanh(f64(0.0)) == atanh_64(0.0)); + expect(atanh(f32(0.0)) == atanh_32(0.0)); + expect(atanh(f64(0.0)) == atanh_64(0.0)); } test "math.atanh_32" { const epsilon = 0.000001; - assert(math.approxEq(f32, atanh_32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, atanh_32(0.2), 0.202733, epsilon)); - assert(math.approxEq(f32, atanh_32(0.8923), 1.433099, epsilon)); + expect(math.approxEq(f32, atanh_32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, atanh_32(0.2), 0.202733, epsilon)); + expect(math.approxEq(f32, atanh_32(0.8923), 1.433099, epsilon)); } test "math.atanh_64" { const epsilon = 0.000001; - assert(math.approxEq(f64, atanh_64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, atanh_64(0.2), 0.202733, epsilon)); - assert(math.approxEq(f64, atanh_64(0.8923), 1.433099, epsilon)); + expect(math.approxEq(f64, atanh_64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, atanh_64(0.2), 0.202733, epsilon)); + expect(math.approxEq(f64, atanh_64(0.8923), 1.433099, epsilon)); } test "math.atanh32.special" { - assert(math.isPositiveInf(atanh_32(1))); - assert(math.isNegativeInf(atanh_32(-1))); - assert(math.isSignalNan(atanh_32(1.5))); - assert(math.isSignalNan(atanh_32(-1.5))); - assert(math.isNan(atanh_32(math.nan(f32)))); + expect(math.isPositiveInf(atanh_32(1))); + expect(math.isNegativeInf(atanh_32(-1))); + expect(math.isSignalNan(atanh_32(1.5))); + expect(math.isSignalNan(atanh_32(-1.5))); + expect(math.isNan(atanh_32(math.nan(f32)))); } test "math.atanh64.special" { - assert(math.isPositiveInf(atanh_64(1))); - assert(math.isNegativeInf(atanh_64(-1))); - assert(math.isSignalNan(atanh_64(1.5))); - assert(math.isSignalNan(atanh_64(-1.5))); - assert(math.isNan(atanh_64(math.nan(f64)))); + expect(math.isPositiveInf(atanh_64(1))); + expect(math.isNegativeInf(atanh_64(-1))); + expect(math.isSignalNan(atanh_64(1.5))); + expect(math.isSignalNan(atanh_64(-1.5))); + expect(math.isNan(atanh_64(math.nan(f64)))); } diff --git a/std/math/big/int.zig b/std/math/big/int.zig index cda2e1419f..f21e5df8aa 100644 --- a/std/math/big/int.zig +++ b/std/math/big/int.zig @@ -1,6 +1,7 @@ const std = @import("../../index.zig"); const builtin = @import("builtin"); const debug = std.debug; +const testing = std.testing; const math = std.math; const mem = std.mem; const Allocator = mem.Allocator; @@ -1086,44 +1087,40 @@ test "big.int comptime_int set" { const result = Limb(s & maxInt(Limb)); s >>= Limb.bit_count / 2; s >>= Limb.bit_count / 2; - debug.assert(a.limbs[i] == result); + testing.expect(a.limbs[i] == result); } } test "big.int comptime_int set negative" { var a = try Int.initSet(al, -10); - debug.assert(a.limbs[0] == 10); - debug.assert(a.positive == false); + testing.expect(a.limbs[0] == 10); + testing.expect(a.positive == false); } test "big.int int set unaligned small" { var a = try Int.initSet(al, u7(45)); - debug.assert(a.limbs[0] == 45); - debug.assert(a.positive == true); + testing.expect(a.limbs[0] == 45); + testing.expect(a.positive == true); } test "big.int comptime_int to" { const a = try Int.initSet(al, 0xefffffff00000001eeeeeeefaaaaaaab); - debug.assert((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab); + testing.expect((try a.to(u128)) == 0xefffffff00000001eeeeeeefaaaaaaab); } test "big.int sub-limb to" { const a = try Int.initSet(al, 10); - debug.assert((try a.to(u8)) == 10); + testing.expect((try a.to(u8)) == 10); } test "big.int to target too small error" { const a = try Int.initSet(al, 0xffffffff); - if (a.to(u8)) |_| { - unreachable; - } else |err| { - debug.assert(err == error.TargetTooSmall); - } + testing.expectError(error.TargetTooSmall, a.to(u8)); } test "big.int norm1" { @@ -1135,22 +1132,22 @@ test "big.int norm1" { a.limbs[2] = 3; a.limbs[3] = 0; a.norm1(4); - debug.assert(a.len == 3); + testing.expect(a.len == 3); a.limbs[0] = 1; a.limbs[1] = 2; a.limbs[2] = 3; a.norm1(3); - debug.assert(a.len == 3); + testing.expect(a.len == 3); a.limbs[0] = 0; a.limbs[1] = 0; a.norm1(2); - debug.assert(a.len == 1); + testing.expect(a.len == 1); a.limbs[0] = 0; a.norm1(1); - debug.assert(a.len == 1); + testing.expect(a.len == 1); } test "big.int normN" { @@ -1162,144 +1159,144 @@ test "big.int normN" { a.limbs[2] = 0; a.limbs[3] = 0; a.normN(4); - debug.assert(a.len == 2); + testing.expect(a.len == 2); a.limbs[0] = 1; a.limbs[1] = 2; a.limbs[2] = 3; a.normN(3); - debug.assert(a.len == 3); + testing.expect(a.len == 3); a.limbs[0] = 0; a.limbs[1] = 0; a.limbs[2] = 0; a.limbs[3] = 0; a.normN(4); - debug.assert(a.len == 1); + testing.expect(a.len == 1); a.limbs[0] = 0; a.normN(1); - debug.assert(a.len == 1); + testing.expect(a.len == 1); } test "big.int parity" { var a = try Int.init(al); try a.set(0); - debug.assert(a.isEven()); - debug.assert(!a.isOdd()); + testing.expect(a.isEven()); + testing.expect(!a.isOdd()); try a.set(7); - debug.assert(!a.isEven()); - debug.assert(a.isOdd()); + testing.expect(!a.isEven()); + testing.expect(a.isOdd()); } test "big.int bitcount + sizeInBase" { var a = try Int.init(al); try a.set(0b100); - debug.assert(a.bitCountAbs() == 3); - debug.assert(a.sizeInBase(2) >= 3); - debug.assert(a.sizeInBase(10) >= 1); + testing.expect(a.bitCountAbs() == 3); + testing.expect(a.sizeInBase(2) >= 3); + testing.expect(a.sizeInBase(10) >= 1); a.negate(); - debug.assert(a.bitCountAbs() == 3); - debug.assert(a.sizeInBase(2) >= 4); - debug.assert(a.sizeInBase(10) >= 2); + testing.expect(a.bitCountAbs() == 3); + testing.expect(a.sizeInBase(2) >= 4); + testing.expect(a.sizeInBase(10) >= 2); try a.set(0xffffffff); - debug.assert(a.bitCountAbs() == 32); - debug.assert(a.sizeInBase(2) >= 32); - debug.assert(a.sizeInBase(10) >= 10); + testing.expect(a.bitCountAbs() == 32); + testing.expect(a.sizeInBase(2) >= 32); + testing.expect(a.sizeInBase(10) >= 10); try a.shiftLeft(a, 5000); - debug.assert(a.bitCountAbs() == 5032); - debug.assert(a.sizeInBase(2) >= 5032); + testing.expect(a.bitCountAbs() == 5032); + testing.expect(a.sizeInBase(2) >= 5032); a.positive = false; - debug.assert(a.bitCountAbs() == 5032); - debug.assert(a.sizeInBase(2) >= 5033); + testing.expect(a.bitCountAbs() == 5032); + testing.expect(a.sizeInBase(2) >= 5033); } test "big.int bitcount/to" { var a = try Int.init(al); try a.set(0); - debug.assert(a.bitCountTwosComp() == 0); + testing.expect(a.bitCountTwosComp() == 0); // TODO: stack smashing - // debug.assert((try a.to(u0)) == 0); + // testing.expect((try a.to(u0)) == 0); // TODO: sigsegv - // debug.assert((try a.to(i0)) == 0); + // testing.expect((try a.to(i0)) == 0); try a.set(-1); - debug.assert(a.bitCountTwosComp() == 1); - debug.assert((try a.to(i1)) == -1); + testing.expect(a.bitCountTwosComp() == 1); + testing.expect((try a.to(i1)) == -1); try a.set(-8); - debug.assert(a.bitCountTwosComp() == 4); - debug.assert((try a.to(i4)) == -8); + testing.expect(a.bitCountTwosComp() == 4); + testing.expect((try a.to(i4)) == -8); try a.set(127); - debug.assert(a.bitCountTwosComp() == 7); - debug.assert((try a.to(u7)) == 127); + testing.expect(a.bitCountTwosComp() == 7); + testing.expect((try a.to(u7)) == 127); try a.set(-128); - debug.assert(a.bitCountTwosComp() == 8); - debug.assert((try a.to(i8)) == -128); + testing.expect(a.bitCountTwosComp() == 8); + testing.expect((try a.to(i8)) == -128); try a.set(-129); - debug.assert(a.bitCountTwosComp() == 9); - debug.assert((try a.to(i9)) == -129); + testing.expect(a.bitCountTwosComp() == 9); + testing.expect((try a.to(i9)) == -129); } test "big.int fits" { var a = try Int.init(al); try a.set(0); - debug.assert(a.fits(u0)); - debug.assert(a.fits(i0)); + testing.expect(a.fits(u0)); + testing.expect(a.fits(i0)); try a.set(255); - debug.assert(!a.fits(u0)); - debug.assert(!a.fits(u1)); - debug.assert(!a.fits(i8)); - debug.assert(a.fits(u8)); - debug.assert(a.fits(u9)); - debug.assert(a.fits(i9)); + testing.expect(!a.fits(u0)); + testing.expect(!a.fits(u1)); + testing.expect(!a.fits(i8)); + testing.expect(a.fits(u8)); + testing.expect(a.fits(u9)); + testing.expect(a.fits(i9)); try a.set(-128); - debug.assert(!a.fits(i7)); - debug.assert(a.fits(i8)); - debug.assert(a.fits(i9)); - debug.assert(!a.fits(u9)); + testing.expect(!a.fits(i7)); + testing.expect(a.fits(i8)); + testing.expect(a.fits(i9)); + testing.expect(!a.fits(u9)); try a.set(0x1ffffffffeeeeeeee); - debug.assert(!a.fits(u32)); - debug.assert(!a.fits(u64)); - debug.assert(a.fits(u65)); + testing.expect(!a.fits(u32)); + testing.expect(!a.fits(u64)); + testing.expect(a.fits(u65)); } test "big.int string set" { var a = try Int.init(al); try a.setString(10, "120317241209124781241290847124"); - debug.assert((try a.to(u128)) == 120317241209124781241290847124); + testing.expect((try a.to(u128)) == 120317241209124781241290847124); } test "big.int string negative" { var a = try Int.init(al); try a.setString(10, "-1023"); - debug.assert((try a.to(i32)) == -1023); + testing.expect((try a.to(i32)) == -1023); } test "big.int string set bad char error" { var a = try Int.init(al); - a.setString(10, "x") catch |err| debug.assert(err == error.InvalidCharForDigit); + testing.expectError(error.InvalidCharForDigit, a.setString(10, "x")); } test "big.int string set bad base error" { var a = try Int.init(al); - a.setString(45, "10") catch |err| debug.assert(err == error.InvalidBase); + testing.expectError(error.InvalidBase, a.setString(45, "10")); } test "big.int string to" { @@ -1308,17 +1305,13 @@ test "big.int string to" { const as = try a.toString(al, 10); const es = "120317241209124781241290847124"; - debug.assert(mem.eql(u8, as, es)); + testing.expect(mem.eql(u8, as, es)); } test "big.int string to base base error" { const a = try Int.initSet(al, 0xffffffff); - if (a.toString(al, 45)) |_| { - unreachable; - } else |err| { - debug.assert(err == error.InvalidBase); - } + testing.expectError(error.InvalidBase, a.toString(al, 45)); } test "big.int string to base 2" { @@ -1327,7 +1320,7 @@ test "big.int string to base 2" { const as = try a.toString(al, 2); const es = "-1011"; - debug.assert(mem.eql(u8, as, es)); + testing.expect(mem.eql(u8, as, es)); } test "big.int string to base 16" { @@ -1336,7 +1329,7 @@ test "big.int string to base 16" { const as = try a.toString(al, 16); const es = "efffffff00000001eeeeeeefaaaaaaab"; - debug.assert(mem.eql(u8, as, es)); + testing.expect(mem.eql(u8, as, es)); } test "big.int neg string to" { @@ -1345,7 +1338,7 @@ test "big.int neg string to" { const as = try a.toString(al, 10); const es = "-123907434"; - debug.assert(mem.eql(u8, as, es)); + testing.expect(mem.eql(u8, as, es)); } test "big.int zero string to" { @@ -1354,98 +1347,98 @@ test "big.int zero string to" { const as = try a.toString(al, 10); const es = "0"; - debug.assert(mem.eql(u8, as, es)); + testing.expect(mem.eql(u8, as, es)); } test "big.int clone" { var a = try Int.initSet(al, 1234); const b = try a.clone(); - debug.assert((try a.to(u32)) == 1234); - debug.assert((try b.to(u32)) == 1234); + testing.expect((try a.to(u32)) == 1234); + testing.expect((try b.to(u32)) == 1234); try a.set(77); - debug.assert((try a.to(u32)) == 77); - debug.assert((try b.to(u32)) == 1234); + testing.expect((try a.to(u32)) == 77); + testing.expect((try b.to(u32)) == 1234); } test "big.int swap" { var a = try Int.initSet(al, 1234); var b = try Int.initSet(al, 5678); - debug.assert((try a.to(u32)) == 1234); - debug.assert((try b.to(u32)) == 5678); + testing.expect((try a.to(u32)) == 1234); + testing.expect((try b.to(u32)) == 5678); a.swap(&b); - debug.assert((try a.to(u32)) == 5678); - debug.assert((try b.to(u32)) == 1234); + testing.expect((try a.to(u32)) == 5678); + testing.expect((try b.to(u32)) == 1234); } test "big.int to negative" { var a = try Int.initSet(al, -10); - debug.assert((try a.to(i32)) == -10); + testing.expect((try a.to(i32)) == -10); } test "big.int compare" { var a = try Int.initSet(al, -11); var b = try Int.initSet(al, 10); - debug.assert(a.cmpAbs(b) == 1); - debug.assert(a.cmp(b) == -1); + testing.expect(a.cmpAbs(b) == 1); + testing.expect(a.cmp(b) == -1); } test "big.int compare similar" { var a = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeee); var b = try Int.initSet(al, 0xffffffffeeeeeeeeffffffffeeeeeeef); - debug.assert(a.cmpAbs(b) == -1); - debug.assert(b.cmpAbs(a) == 1); + testing.expect(a.cmpAbs(b) == -1); + testing.expect(b.cmpAbs(a) == 1); } test "big.int compare different limb size" { var a = try Int.initSet(al, maxInt(Limb) + 1); var b = try Int.initSet(al, 1); - debug.assert(a.cmpAbs(b) == 1); - debug.assert(b.cmpAbs(a) == -1); + testing.expect(a.cmpAbs(b) == 1); + testing.expect(b.cmpAbs(a) == -1); } test "big.int compare multi-limb" { var a = try Int.initSet(al, -0x7777777799999999ffffeeeeffffeeeeffffeeeef); var b = try Int.initSet(al, 0x7777777799999999ffffeeeeffffeeeeffffeeeee); - debug.assert(a.cmpAbs(b) == 1); - debug.assert(a.cmp(b) == -1); + testing.expect(a.cmpAbs(b) == 1); + testing.expect(a.cmp(b) == -1); } test "big.int equality" { var a = try Int.initSet(al, 0xffffffff1); var b = try Int.initSet(al, -0xffffffff1); - debug.assert(a.eqAbs(b)); - debug.assert(!a.eq(b)); + testing.expect(a.eqAbs(b)); + testing.expect(!a.eq(b)); } test "big.int abs" { var a = try Int.initSet(al, -5); a.abs(); - debug.assert((try a.to(u32)) == 5); + testing.expect((try a.to(u32)) == 5); a.abs(); - debug.assert((try a.to(u32)) == 5); + testing.expect((try a.to(u32)) == 5); } test "big.int negate" { var a = try Int.initSet(al, 5); a.negate(); - debug.assert((try a.to(i32)) == -5); + testing.expect((try a.to(i32)) == -5); a.negate(); - debug.assert((try a.to(i32)) == 5); + testing.expect((try a.to(i32)) == 5); } test "big.int add single-single" { @@ -1455,7 +1448,7 @@ test "big.int add single-single" { var c = try Int.init(al); try c.add(a, b); - debug.assert((try c.to(u32)) == 55); + testing.expect((try c.to(u32)) == 55); } test "big.int add multi-single" { @@ -1465,10 +1458,10 @@ test "big.int add multi-single" { var c = try Int.init(al); try c.add(a, b); - debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2); + testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2); try c.add(b, a); - debug.assert((try c.to(DoubleLimb)) == maxInt(Limb) + 2); + testing.expect((try c.to(DoubleLimb)) == maxInt(Limb) + 2); } test "big.int add multi-multi" { @@ -1480,7 +1473,7 @@ test "big.int add multi-multi" { var c = try Int.init(al); try c.add(a, b); - debug.assert((try c.to(u128)) == op1 + op2); + testing.expect((try c.to(u128)) == op1 + op2); } test "big.int add zero-zero" { @@ -1490,7 +1483,7 @@ test "big.int add zero-zero" { var c = try Int.init(al); try c.add(a, b); - debug.assert((try c.to(u32)) == 0); + testing.expect((try c.to(u32)) == 0); } test "big.int add alias multi-limb nonzero-zero" { @@ -1500,7 +1493,7 @@ test "big.int add alias multi-limb nonzero-zero" { try a.add(a, b); - debug.assert((try a.to(u128)) == op1); + testing.expect((try a.to(u128)) == op1); } test "big.int add sign" { @@ -1512,16 +1505,16 @@ test "big.int add sign" { const neg_two = try Int.initSet(al, -2); try a.add(one, two); - debug.assert((try a.to(i32)) == 3); + testing.expect((try a.to(i32)) == 3); try a.add(neg_one, two); - debug.assert((try a.to(i32)) == 1); + testing.expect((try a.to(i32)) == 1); try a.add(one, neg_two); - debug.assert((try a.to(i32)) == -1); + testing.expect((try a.to(i32)) == -1); try a.add(neg_one, neg_two); - debug.assert((try a.to(i32)) == -3); + testing.expect((try a.to(i32)) == -3); } test "big.int sub single-single" { @@ -1531,7 +1524,7 @@ test "big.int sub single-single" { var c = try Int.init(al); try c.sub(a, b); - debug.assert((try c.to(u32)) == 45); + testing.expect((try c.to(u32)) == 45); } test "big.int sub multi-single" { @@ -1541,7 +1534,7 @@ test "big.int sub multi-single" { var c = try Int.init(al); try c.sub(a, b); - debug.assert((try c.to(Limb)) == maxInt(Limb)); + testing.expect((try c.to(Limb)) == maxInt(Limb)); } test "big.int sub multi-multi" { @@ -1554,7 +1547,7 @@ test "big.int sub multi-multi" { var c = try Int.init(al); try c.sub(a, b); - debug.assert((try c.to(u128)) == op1 - op2); + testing.expect((try c.to(u128)) == op1 - op2); } test "big.int sub equal" { @@ -1564,7 +1557,7 @@ test "big.int sub equal" { var c = try Int.init(al); try c.sub(a, b); - debug.assert((try c.to(u32)) == 0); + testing.expect((try c.to(u32)) == 0); } test "big.int sub sign" { @@ -1576,19 +1569,19 @@ test "big.int sub sign" { const neg_two = try Int.initSet(al, -2); try a.sub(one, two); - debug.assert((try a.to(i32)) == -1); + testing.expect((try a.to(i32)) == -1); try a.sub(neg_one, two); - debug.assert((try a.to(i32)) == -3); + testing.expect((try a.to(i32)) == -3); try a.sub(one, neg_two); - debug.assert((try a.to(i32)) == 3); + testing.expect((try a.to(i32)) == 3); try a.sub(neg_one, neg_two); - debug.assert((try a.to(i32)) == 1); + testing.expect((try a.to(i32)) == 1); try a.sub(neg_two, neg_one); - debug.assert((try a.to(i32)) == -1); + testing.expect((try a.to(i32)) == -1); } test "big.int mul single-single" { @@ -1598,7 +1591,7 @@ test "big.int mul single-single" { var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(u64)) == 250); + testing.expect((try c.to(u64)) == 250); } test "big.int mul multi-single" { @@ -1608,7 +1601,7 @@ test "big.int mul multi-single" { var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(DoubleLimb)) == 2 * maxInt(Limb)); + testing.expect((try c.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul multi-multi" { @@ -1620,7 +1613,7 @@ test "big.int mul multi-multi" { var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(u256)) == op1 * op2); + testing.expect((try c.to(u256)) == op1 * op2); } test "big.int mul alias r with a" { @@ -1629,7 +1622,7 @@ test "big.int mul alias r with a" { try a.mul(a, b); - debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); + testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul alias r with b" { @@ -1638,7 +1631,7 @@ test "big.int mul alias r with b" { try a.mul(b, a); - debug.assert((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); + testing.expect((try a.to(DoubleLimb)) == 2 * maxInt(Limb)); } test "big.int mul alias r with a and b" { @@ -1646,7 +1639,7 @@ test "big.int mul alias r with a and b" { try a.mul(a, a); - debug.assert((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb)); + testing.expect((try a.to(DoubleLimb)) == maxInt(Limb) * maxInt(Limb)); } test "big.int mul a*0" { @@ -1656,7 +1649,7 @@ test "big.int mul a*0" { var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(u32)) == 0); + testing.expect((try c.to(u32)) == 0); } test "big.int mul 0*0" { @@ -1666,7 +1659,7 @@ test "big.int mul 0*0" { var c = try Int.init(al); try c.mul(a, b); - debug.assert((try c.to(u32)) == 0); + testing.expect((try c.to(u32)) == 0); } test "big.int div single-single no rem" { @@ -1677,8 +1670,8 @@ test "big.int div single-single no rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u32)) == 10); - debug.assert((try r.to(u32)) == 0); + testing.expect((try q.to(u32)) == 10); + testing.expect((try r.to(u32)) == 0); } test "big.int div single-single with rem" { @@ -1689,8 +1682,8 @@ test "big.int div single-single with rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u32)) == 9); - debug.assert((try r.to(u32)) == 4); + testing.expect((try q.to(u32)) == 9); + testing.expect((try r.to(u32)) == 4); } test "big.int div multi-single no rem" { @@ -1704,8 +1697,8 @@ test "big.int div multi-single no rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u64)) == op1 / op2); - debug.assert((try r.to(u64)) == 0); + testing.expect((try q.to(u64)) == op1 / op2); + testing.expect((try r.to(u64)) == 0); } test "big.int div multi-single with rem" { @@ -1719,8 +1712,8 @@ test "big.int div multi-single with rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u64)) == op1 / op2); - debug.assert((try r.to(u64)) == 3); + testing.expect((try q.to(u64)) == op1 / op2); + testing.expect((try r.to(u64)) == 3); } test "big.int div multi>2-single" { @@ -1734,8 +1727,8 @@ test "big.int div multi>2-single" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == op1 / op2); - debug.assert((try r.to(u32)) == 0x3e4e); + testing.expect((try q.to(u128)) == op1 / op2); + testing.expect((try r.to(u32)) == 0x3e4e); } test "big.int div single-single q < r" { @@ -1746,8 +1739,8 @@ test "big.int div single-single q < r" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u64)) == 0); - debug.assert((try r.to(u64)) == 0x0078f432); + testing.expect((try q.to(u64)) == 0); + testing.expect((try r.to(u64)) == 0x0078f432); } test "big.int div single-single q == r" { @@ -1758,8 +1751,8 @@ test "big.int div single-single q == r" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u64)) == 1); - debug.assert((try r.to(u64)) == 0); + testing.expect((try q.to(u64)) == 1); + testing.expect((try r.to(u64)) == 0); } test "big.int div q=0 alias" { @@ -1768,8 +1761,8 @@ test "big.int div q=0 alias" { try Int.divTrunc(&a, &b, a, b); - debug.assert((try a.to(u64)) == 0); - debug.assert((try b.to(u64)) == 3); + testing.expect((try a.to(u64)) == 0); + testing.expect((try b.to(u64)) == 3); } test "big.int div multi-multi q < r" { @@ -1782,8 +1775,8 @@ test "big.int div multi-multi q < r" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == 0); - debug.assert((try r.to(u128)) == op1); + testing.expect((try q.to(u128)) == 0); + testing.expect((try r.to(u128)) == op1); } test "big.int div trunc single-single +/+" { @@ -1802,8 +1795,8 @@ test "big.int div trunc single-single +/+" { const eq = @divTrunc(u, v); const er = @mod(u, v); - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div trunc single-single -/+" { @@ -1822,8 +1815,8 @@ test "big.int div trunc single-single -/+" { const eq = -1; const er = -2; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div trunc single-single +/-" { @@ -1842,8 +1835,8 @@ test "big.int div trunc single-single +/-" { const eq = -1; const er = 2; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div trunc single-single -/-" { @@ -1862,8 +1855,8 @@ test "big.int div trunc single-single -/-" { const eq = 1; const er = -2; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div floor single-single +/+" { @@ -1882,8 +1875,8 @@ test "big.int div floor single-single +/+" { const eq = 1; const er = 2; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div floor single-single -/+" { @@ -1902,8 +1895,8 @@ test "big.int div floor single-single -/+" { const eq = -2; const er = 1; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div floor single-single +/-" { @@ -1922,8 +1915,8 @@ test "big.int div floor single-single +/-" { const eq = -2; const er = -1; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div floor single-single -/-" { @@ -1942,8 +1935,8 @@ test "big.int div floor single-single -/-" { const eq = 1; const er = -2; - debug.assert((try q.to(i32)) == eq); - debug.assert((try r.to(i32)) == er); + testing.expect((try q.to(i32)) == eq); + testing.expect((try r.to(i32)) == er); } test "big.int div multi-multi with rem" { @@ -1954,8 +1947,8 @@ test "big.int div multi-multi with rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b); - debug.assert((try r.to(u128)) == 0x28de0acacd806823638); + testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b); + testing.expect((try r.to(u128)) == 0x28de0acacd806823638); } test "big.int div multi-multi no rem" { @@ -1966,8 +1959,8 @@ test "big.int div multi-multi no rem" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b); - debug.assert((try r.to(u128)) == 0); + testing.expect((try q.to(u128)) == 0xe38f38e39161aaabd03f0f1b); + testing.expect((try r.to(u128)) == 0); } test "big.int div multi-multi (2 branch)" { @@ -1978,8 +1971,8 @@ test "big.int div multi-multi (2 branch)" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == 0x10000000000000000); - debug.assert((try r.to(u128)) == 0x44444443444444431111111111111111); + testing.expect((try q.to(u128)) == 0x10000000000000000); + testing.expect((try r.to(u128)) == 0x44444443444444431111111111111111); } test "big.int div multi-multi (3.1/3.3 branch)" { @@ -1990,53 +1983,53 @@ test "big.int div multi-multi (3.1/3.3 branch)" { var r = try Int.init(al); try Int.divTrunc(&q, &r, a, b); - debug.assert((try q.to(u128)) == 0xfffffffffffffffffff); - debug.assert((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282); + testing.expect((try q.to(u128)) == 0xfffffffffffffffffff); + testing.expect((try r.to(u256)) == 0x1111111111111111111110b12222222222222222282); } test "big.int shift-right single" { var a = try Int.initSet(al, 0xffff0000); try a.shiftRight(a, 16); - debug.assert((try a.to(u32)) == 0xffff); + testing.expect((try a.to(u32)) == 0xffff); } test "big.int shift-right multi" { var a = try Int.initSet(al, 0xffff0000eeee1111dddd2222cccc3333); try a.shiftRight(a, 67); - debug.assert((try a.to(u64)) == 0x1fffe0001dddc222); + testing.expect((try a.to(u64)) == 0x1fffe0001dddc222); } test "big.int shift-left single" { var a = try Int.initSet(al, 0xffff); try a.shiftLeft(a, 16); - debug.assert((try a.to(u64)) == 0xffff0000); + testing.expect((try a.to(u64)) == 0xffff0000); } test "big.int shift-left multi" { var a = try Int.initSet(al, 0x1fffe0001dddc222); try a.shiftLeft(a, 67); - debug.assert((try a.to(u128)) == 0xffff0000eeee11100000000000000000); + testing.expect((try a.to(u128)) == 0xffff0000eeee11100000000000000000); } test "big.int shift-right negative" { var a = try Int.init(al); try a.shiftRight(try Int.initSet(al, -20), 2); - debug.assert((try a.to(i32)) == -20 >> 2); + testing.expect((try a.to(i32)) == -20 >> 2); try a.shiftRight(try Int.initSet(al, -5), 10); - debug.assert((try a.to(i32)) == -5 >> 10); + testing.expect((try a.to(i32)) == -5 >> 10); } test "big.int shift-left negative" { var a = try Int.init(al); try a.shiftRight(try Int.initSet(al, -10), 1232); - debug.assert((try a.to(i32)) == -10 >> 1232); + testing.expect((try a.to(i32)) == -10 >> 1232); } test "big.int bitwise and simple" { @@ -2045,7 +2038,7 @@ test "big.int bitwise and simple" { try a.bitAnd(a, b); - debug.assert((try a.to(u64)) == 0xeeeeeeee00000000); + testing.expect((try a.to(u64)) == 0xeeeeeeee00000000); } test "big.int bitwise and multi-limb" { @@ -2054,7 +2047,7 @@ test "big.int bitwise and multi-limb" { try a.bitAnd(a, b); - debug.assert((try a.to(u128)) == 0); + testing.expect((try a.to(u128)) == 0); } test "big.int bitwise xor simple" { @@ -2063,7 +2056,7 @@ test "big.int bitwise xor simple" { try a.bitXor(a, b); - debug.assert((try a.to(u64)) == 0x1111111133333333); + testing.expect((try a.to(u64)) == 0x1111111133333333); } test "big.int bitwise xor multi-limb" { @@ -2072,7 +2065,7 @@ test "big.int bitwise xor multi-limb" { try a.bitXor(a, b); - debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb)); + testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) ^ maxInt(Limb)); } test "big.int bitwise or simple" { @@ -2081,7 +2074,7 @@ test "big.int bitwise or simple" { try a.bitOr(a, b); - debug.assert((try a.to(u64)) == 0xffffffff33333333); + testing.expect((try a.to(u64)) == 0xffffffff33333333); } test "big.int bitwise or multi-limb" { @@ -2091,15 +2084,15 @@ test "big.int bitwise or multi-limb" { try a.bitOr(a, b); // TODO: big.int.cpp or is wrong on multi-limb. - debug.assert((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb)); + testing.expect((try a.to(DoubleLimb)) == (maxInt(Limb) + 1) + maxInt(Limb)); } test "big.int var args" { var a = try Int.initSet(al, 5); try a.add(a, try Int.initSet(al, 6)); - debug.assert((try a.to(u64)) == 11); + testing.expect((try a.to(u64)) == 11); - debug.assert(a.cmp(try Int.initSet(al, 11)) == 0); - debug.assert(a.cmp(try Int.initSet(al, 14)) <= 0); + testing.expect(a.cmp(try Int.initSet(al, 11)) == 0); + testing.expect(a.cmp(try Int.initSet(al, 14)) <= 0); } diff --git a/std/math/cbrt.zig b/std/math/cbrt.zig index c067c5155a..957e026af4 100644 --- a/std/math/cbrt.zig +++ b/std/math/cbrt.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn cbrt(x: var) @typeOf(x) { const T = @typeOf(x); @@ -114,44 +114,44 @@ fn cbrt64(x: f64) f64 { } test "math.cbrt" { - assert(cbrt(f32(0.0)) == cbrt32(0.0)); - assert(cbrt(f64(0.0)) == cbrt64(0.0)); + expect(cbrt(f32(0.0)) == cbrt32(0.0)); + expect(cbrt(f64(0.0)) == cbrt64(0.0)); } test "math.cbrt32" { const epsilon = 0.000001; - assert(cbrt32(0.0) == 0.0); - assert(math.approxEq(f32, cbrt32(0.2), 0.584804, epsilon)); - assert(math.approxEq(f32, cbrt32(0.8923), 0.962728, epsilon)); - assert(math.approxEq(f32, cbrt32(1.5), 1.144714, epsilon)); - assert(math.approxEq(f32, cbrt32(37.45), 3.345676, epsilon)); - assert(math.approxEq(f32, cbrt32(123123.234375), 49.748501, epsilon)); + expect(cbrt32(0.0) == 0.0); + expect(math.approxEq(f32, cbrt32(0.2), 0.584804, epsilon)); + expect(math.approxEq(f32, cbrt32(0.8923), 0.962728, epsilon)); + expect(math.approxEq(f32, cbrt32(1.5), 1.144714, epsilon)); + expect(math.approxEq(f32, cbrt32(37.45), 3.345676, epsilon)); + expect(math.approxEq(f32, cbrt32(123123.234375), 49.748501, epsilon)); } test "math.cbrt64" { const epsilon = 0.000001; - assert(cbrt64(0.0) == 0.0); - assert(math.approxEq(f64, cbrt64(0.2), 0.584804, epsilon)); - assert(math.approxEq(f64, cbrt64(0.8923), 0.962728, epsilon)); - assert(math.approxEq(f64, cbrt64(1.5), 1.144714, epsilon)); - assert(math.approxEq(f64, cbrt64(37.45), 3.345676, epsilon)); - assert(math.approxEq(f64, cbrt64(123123.234375), 49.748501, epsilon)); + expect(cbrt64(0.0) == 0.0); + expect(math.approxEq(f64, cbrt64(0.2), 0.584804, epsilon)); + expect(math.approxEq(f64, cbrt64(0.8923), 0.962728, epsilon)); + expect(math.approxEq(f64, cbrt64(1.5), 1.144714, epsilon)); + expect(math.approxEq(f64, cbrt64(37.45), 3.345676, epsilon)); + expect(math.approxEq(f64, cbrt64(123123.234375), 49.748501, epsilon)); } test "math.cbrt.special" { - assert(cbrt32(0.0) == 0.0); - assert(cbrt32(-0.0) == -0.0); - assert(math.isPositiveInf(cbrt32(math.inf(f32)))); - assert(math.isNegativeInf(cbrt32(-math.inf(f32)))); - assert(math.isNan(cbrt32(math.nan(f32)))); + expect(cbrt32(0.0) == 0.0); + expect(cbrt32(-0.0) == -0.0); + expect(math.isPositiveInf(cbrt32(math.inf(f32)))); + expect(math.isNegativeInf(cbrt32(-math.inf(f32)))); + expect(math.isNan(cbrt32(math.nan(f32)))); } test "math.cbrt64.special" { - assert(cbrt64(0.0) == 0.0); - assert(cbrt64(-0.0) == -0.0); - assert(math.isPositiveInf(cbrt64(math.inf(f64)))); - assert(math.isNegativeInf(cbrt64(-math.inf(f64)))); - assert(math.isNan(cbrt64(math.nan(f64)))); + expect(cbrt64(0.0) == 0.0); + expect(cbrt64(-0.0) == -0.0); + expect(math.isPositiveInf(cbrt64(math.inf(f64)))); + expect(math.isNegativeInf(cbrt64(-math.inf(f64)))); + expect(math.isNan(cbrt64(math.nan(f64)))); } diff --git a/std/math/ceil.zig b/std/math/ceil.zig index 8a5221d862..5c6b98b2ca 100644 --- a/std/math/ceil.zig +++ b/std/math/ceil.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn ceil(x: var) @typeOf(x) { const T = @typeOf(x); @@ -81,34 +81,34 @@ fn ceil64(x: f64) f64 { } test "math.ceil" { - assert(ceil(f32(0.0)) == ceil32(0.0)); - assert(ceil(f64(0.0)) == ceil64(0.0)); + expect(ceil(f32(0.0)) == ceil32(0.0)); + expect(ceil(f64(0.0)) == ceil64(0.0)); } test "math.ceil32" { - assert(ceil32(1.3) == 2.0); - assert(ceil32(-1.3) == -1.0); - assert(ceil32(0.2) == 1.0); + expect(ceil32(1.3) == 2.0); + expect(ceil32(-1.3) == -1.0); + expect(ceil32(0.2) == 1.0); } test "math.ceil64" { - assert(ceil64(1.3) == 2.0); - assert(ceil64(-1.3) == -1.0); - assert(ceil64(0.2) == 1.0); + expect(ceil64(1.3) == 2.0); + expect(ceil64(-1.3) == -1.0); + expect(ceil64(0.2) == 1.0); } test "math.ceil32.special" { - assert(ceil32(0.0) == 0.0); - assert(ceil32(-0.0) == -0.0); - assert(math.isPositiveInf(ceil32(math.inf(f32)))); - assert(math.isNegativeInf(ceil32(-math.inf(f32)))); - assert(math.isNan(ceil32(math.nan(f32)))); + expect(ceil32(0.0) == 0.0); + expect(ceil32(-0.0) == -0.0); + expect(math.isPositiveInf(ceil32(math.inf(f32)))); + expect(math.isNegativeInf(ceil32(-math.inf(f32)))); + expect(math.isNan(ceil32(math.nan(f32)))); } test "math.ceil64.special" { - assert(ceil64(0.0) == 0.0); - assert(ceil64(-0.0) == -0.0); - assert(math.isPositiveInf(ceil64(math.inf(f64)))); - assert(math.isNegativeInf(ceil64(-math.inf(f64)))); - assert(math.isNan(ceil64(math.nan(f64)))); + expect(ceil64(0.0) == 0.0); + expect(ceil64(-0.0) == -0.0); + expect(math.isPositiveInf(ceil64(math.inf(f64)))); + expect(math.isNegativeInf(ceil64(-math.inf(f64)))); + expect(math.isNan(ceil64(math.nan(f64)))); } diff --git a/std/math/complex/abs.zig b/std/math/complex/abs.zig index 4cd095c46b..245d67d4c5 100644 --- a/std/math/complex/abs.zig +++ b/std/math/complex/abs.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -14,5 +14,5 @@ const epsilon = 0.0001; test "complex.cabs" { const a = Complex(f32).new(5, 3); const c = abs(a); - debug.assert(math.approxEq(f32, c, 5.83095, epsilon)); + testing.expect(math.approxEq(f32, c, 5.83095, epsilon)); } diff --git a/std/math/complex/acos.zig b/std/math/complex/acos.zig index a5760b4ace..1b314bc31a 100644 --- a/std/math/complex/acos.zig +++ b/std/math/complex/acos.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -16,6 +16,6 @@ test "complex.cacos" { const a = Complex(f32).new(5, 3); const c = acos(a); - debug.assert(math.approxEq(f32, c.re, 0.546975, epsilon)); - debug.assert(math.approxEq(f32, c.im, -2.452914, epsilon)); + testing.expect(math.approxEq(f32, c.re, 0.546975, epsilon)); + testing.expect(math.approxEq(f32, c.im, -2.452914, epsilon)); } diff --git a/std/math/complex/acosh.zig b/std/math/complex/acosh.zig index 8dd91b2836..0e4c0121f4 100644 --- a/std/math/complex/acosh.zig +++ b/std/math/complex/acosh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -16,6 +16,6 @@ test "complex.cacosh" { const a = Complex(f32).new(5, 3); const c = acosh(a); - debug.assert(math.approxEq(f32, c.re, 2.452914, epsilon)); - debug.assert(math.approxEq(f32, c.im, 0.546975, epsilon)); + testing.expect(math.approxEq(f32, c.re, 2.452914, epsilon)); + testing.expect(math.approxEq(f32, c.im, 0.546975, epsilon)); } diff --git a/std/math/complex/arg.zig b/std/math/complex/arg.zig index f24512ac73..be117a5940 100644 --- a/std/math/complex/arg.zig +++ b/std/math/complex/arg.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -14,5 +14,5 @@ const epsilon = 0.0001; test "complex.carg" { const a = Complex(f32).new(5, 3); const c = arg(a); - debug.assert(math.approxEq(f32, c, 0.540420, epsilon)); + testing.expect(math.approxEq(f32, c, 0.540420, epsilon)); } diff --git a/std/math/complex/asin.zig b/std/math/complex/asin.zig index 584a3a1a9b..cf802ea206 100644 --- a/std/math/complex/asin.zig +++ b/std/math/complex/asin.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -22,6 +22,6 @@ test "complex.casin" { const a = Complex(f32).new(5, 3); const c = asin(a); - debug.assert(math.approxEq(f32, c.re, 1.023822, epsilon)); - debug.assert(math.approxEq(f32, c.im, 2.452914, epsilon)); + testing.expect(math.approxEq(f32, c.re, 1.023822, epsilon)); + testing.expect(math.approxEq(f32, c.im, 2.452914, epsilon)); } diff --git a/std/math/complex/asinh.zig b/std/math/complex/asinh.zig index 0c4dc2b6e4..0386a636b0 100644 --- a/std/math/complex/asinh.zig +++ b/std/math/complex/asinh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -17,6 +17,6 @@ test "complex.casinh" { const a = Complex(f32).new(5, 3); const c = asinh(a); - debug.assert(math.approxEq(f32, c.re, 2.459831, epsilon)); - debug.assert(math.approxEq(f32, c.im, 0.533999, epsilon)); + testing.expect(math.approxEq(f32, c.re, 2.459831, epsilon)); + testing.expect(math.approxEq(f32, c.im, 0.533999, epsilon)); } diff --git a/std/math/complex/atan.zig b/std/math/complex/atan.zig index de60f2546d..8e60e58e43 100644 --- a/std/math/complex/atan.zig +++ b/std/math/complex/atan.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -117,14 +117,14 @@ test "complex.catan32" { const a = Complex(f32).new(5, 3); const c = atan(a); - debug.assert(math.approxEq(f32, c.re, 1.423679, epsilon)); - debug.assert(math.approxEq(f32, c.im, 0.086569, epsilon)); + testing.expect(math.approxEq(f32, c.re, 1.423679, epsilon)); + testing.expect(math.approxEq(f32, c.im, 0.086569, epsilon)); } test "complex.catan64" { const a = Complex(f64).new(5, 3); const c = atan(a); - debug.assert(math.approxEq(f64, c.re, 1.423679, epsilon)); - debug.assert(math.approxEq(f64, c.im, 0.086569, epsilon)); + testing.expect(math.approxEq(f64, c.re, 1.423679, epsilon)); + testing.expect(math.approxEq(f64, c.im, 0.086569, epsilon)); } diff --git a/std/math/complex/atanh.zig b/std/math/complex/atanh.zig index f70c741765..5b18fe1992 100644 --- a/std/math/complex/atanh.zig +++ b/std/math/complex/atanh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -17,6 +17,6 @@ test "complex.catanh" { const a = Complex(f32).new(5, 3); const c = atanh(a); - debug.assert(math.approxEq(f32, c.re, 0.146947, epsilon)); - debug.assert(math.approxEq(f32, c.im, 1.480870, epsilon)); + testing.expect(math.approxEq(f32, c.re, 0.146947, epsilon)); + testing.expect(math.approxEq(f32, c.im, 1.480870, epsilon)); } diff --git a/std/math/complex/conj.zig b/std/math/complex/conj.zig index ad3e8b5036..143543f9e7 100644 --- a/std/math/complex/conj.zig +++ b/std/math/complex/conj.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -13,5 +13,5 @@ test "complex.conj" { const a = Complex(f32).new(5, 3); const c = a.conjugate(); - debug.assert(c.re == 5 and c.im == -3); + testing.expect(c.re == 5 and c.im == -3); } diff --git a/std/math/complex/cos.zig b/std/math/complex/cos.zig index 96e4ffcdb0..658d19c3b6 100644 --- a/std/math/complex/cos.zig +++ b/std/math/complex/cos.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -16,6 +16,6 @@ test "complex.ccos" { const a = Complex(f32).new(5, 3); const c = cos(a); - debug.assert(math.approxEq(f32, c.re, 2.855815, epsilon)); - debug.assert(math.approxEq(f32, c.im, 9.606383, epsilon)); + testing.expect(math.approxEq(f32, c.re, 2.855815, epsilon)); + testing.expect(math.approxEq(f32, c.im, 9.606383, epsilon)); } diff --git a/std/math/complex/cosh.zig b/std/math/complex/cosh.zig index 91875a0c47..5ce10b03f8 100644 --- a/std/math/complex/cosh.zig +++ b/std/math/complex/cosh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -152,14 +152,14 @@ test "complex.ccosh32" { const a = Complex(f32).new(5, 3); const c = cosh(a); - debug.assert(math.approxEq(f32, c.re, -73.467300, epsilon)); - debug.assert(math.approxEq(f32, c.im, 10.471557, epsilon)); + testing.expect(math.approxEq(f32, c.re, -73.467300, epsilon)); + testing.expect(math.approxEq(f32, c.im, 10.471557, epsilon)); } test "complex.ccosh64" { const a = Complex(f64).new(5, 3); const c = cosh(a); - debug.assert(math.approxEq(f64, c.re, -73.467300, epsilon)); - debug.assert(math.approxEq(f64, c.im, 10.471557, epsilon)); + testing.expect(math.approxEq(f64, c.re, -73.467300, epsilon)); + testing.expect(math.approxEq(f64, c.im, 10.471557, epsilon)); } diff --git a/std/math/complex/exp.zig b/std/math/complex/exp.zig index 0473f653b6..9ded698d08 100644 --- a/std/math/complex/exp.zig +++ b/std/math/complex/exp.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -118,14 +118,14 @@ test "complex.cexp32" { const a = Complex(f32).new(5, 3); const c = exp(a); - debug.assert(math.approxEq(f32, c.re, -146.927917, epsilon)); - debug.assert(math.approxEq(f32, c.im, 20.944065, epsilon)); + testing.expect(math.approxEq(f32, c.re, -146.927917, epsilon)); + testing.expect(math.approxEq(f32, c.im, 20.944065, epsilon)); } test "complex.cexp64" { const a = Complex(f64).new(5, 3); const c = exp(a); - debug.assert(math.approxEq(f64, c.re, -146.927917, epsilon)); - debug.assert(math.approxEq(f64, c.im, 20.944065, epsilon)); + testing.expect(math.approxEq(f64, c.re, -146.927917, epsilon)); + testing.expect(math.approxEq(f64, c.im, 20.944065, epsilon)); } diff --git a/std/math/complex/index.zig b/std/math/complex/index.zig index 171c4c5829..ffbf14d83e 100644 --- a/std/math/complex/index.zig +++ b/std/math/complex/index.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; pub const abs = @import("abs.zig").abs; @@ -97,7 +97,7 @@ test "complex.add" { const b = Complex(f32).new(2, 7); const c = a.add(b); - debug.assert(c.re == 7 and c.im == 10); + testing.expect(c.re == 7 and c.im == 10); } test "complex.sub" { @@ -105,7 +105,7 @@ test "complex.sub" { const b = Complex(f32).new(2, 7); const c = a.sub(b); - debug.assert(c.re == 3 and c.im == -4); + testing.expect(c.re == 3 and c.im == -4); } test "complex.mul" { @@ -113,7 +113,7 @@ test "complex.mul" { const b = Complex(f32).new(2, 7); const c = a.mul(b); - debug.assert(c.re == -11 and c.im == 41); + testing.expect(c.re == -11 and c.im == 41); } test "complex.div" { @@ -121,7 +121,7 @@ test "complex.div" { const b = Complex(f32).new(2, 7); const c = a.div(b); - debug.assert(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and + testing.expect(math.approxEq(f32, c.re, f32(31) / 53, epsilon) and math.approxEq(f32, c.im, f32(-29) / 53, epsilon)); } @@ -129,14 +129,14 @@ test "complex.conjugate" { const a = Complex(f32).new(5, 3); const c = a.conjugate(); - debug.assert(c.re == 5 and c.im == -3); + testing.expect(c.re == 5 and c.im == -3); } test "complex.reciprocal" { const a = Complex(f32).new(5, 3); const c = a.reciprocal(); - debug.assert(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and + testing.expect(math.approxEq(f32, c.re, f32(5) / 34, epsilon) and math.approxEq(f32, c.im, f32(-3) / 34, epsilon)); } @@ -144,7 +144,7 @@ test "complex.magnitude" { const a = Complex(f32).new(5, 3); const c = a.magnitude(); - debug.assert(math.approxEq(f32, c, 5.83095, epsilon)); + testing.expect(math.approxEq(f32, c, 5.83095, epsilon)); } test "complex.cmath" { diff --git a/std/math/complex/log.zig b/std/math/complex/log.zig index a4a1d1664f..360bb7d21e 100644 --- a/std/math/complex/log.zig +++ b/std/math/complex/log.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -18,6 +18,6 @@ test "complex.clog" { const a = Complex(f32).new(5, 3); const c = log(a); - debug.assert(math.approxEq(f32, c.re, 1.763180, epsilon)); - debug.assert(math.approxEq(f32, c.im, 0.540419, epsilon)); + testing.expect(math.approxEq(f32, c.re, 1.763180, epsilon)); + testing.expect(math.approxEq(f32, c.im, 0.540419, epsilon)); } diff --git a/std/math/complex/pow.zig b/std/math/complex/pow.zig index edf68653b6..bd625626c8 100644 --- a/std/math/complex/pow.zig +++ b/std/math/complex/pow.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -17,6 +17,6 @@ test "complex.cpow" { const b = Complex(f32).new(2.3, -1.3); const c = pow(Complex(f32), a, b); - debug.assert(math.approxEq(f32, c.re, 58.049110, epsilon)); - debug.assert(math.approxEq(f32, c.im, -101.003433, epsilon)); + testing.expect(math.approxEq(f32, c.re, 58.049110, epsilon)); + testing.expect(math.approxEq(f32, c.im, -101.003433, epsilon)); } diff --git a/std/math/complex/proj.zig b/std/math/complex/proj.zig index b6c4cc046e..d31006129e 100644 --- a/std/math/complex/proj.zig +++ b/std/math/complex/proj.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -20,5 +20,5 @@ test "complex.cproj" { const a = Complex(f32).new(5, 3); const c = proj(a); - debug.assert(c.re == 5 and c.im == 3); + testing.expect(c.re == 5 and c.im == 3); } diff --git a/std/math/complex/sin.zig b/std/math/complex/sin.zig index d32b771d3b..9d54ab0969 100644 --- a/std/math/complex/sin.zig +++ b/std/math/complex/sin.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -17,6 +17,6 @@ test "complex.csin" { const a = Complex(f32).new(5, 3); const c = sin(a); - debug.assert(math.approxEq(f32, c.re, -9.654126, epsilon)); - debug.assert(math.approxEq(f32, c.im, 2.841692, epsilon)); + testing.expect(math.approxEq(f32, c.re, -9.654126, epsilon)); + testing.expect(math.approxEq(f32, c.im, 2.841692, epsilon)); } diff --git a/std/math/complex/sinh.zig b/std/math/complex/sinh.zig index dc19a0ba1b..469ea6067a 100644 --- a/std/math/complex/sinh.zig +++ b/std/math/complex/sinh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -151,14 +151,14 @@ test "complex.csinh32" { const a = Complex(f32).new(5, 3); const c = sinh(a); - debug.assert(math.approxEq(f32, c.re, -73.460617, epsilon)); - debug.assert(math.approxEq(f32, c.im, 10.472508, epsilon)); + testing.expect(math.approxEq(f32, c.re, -73.460617, epsilon)); + testing.expect(math.approxEq(f32, c.im, 10.472508, epsilon)); } test "complex.csinh64" { const a = Complex(f64).new(5, 3); const c = sinh(a); - debug.assert(math.approxEq(f64, c.re, -73.460617, epsilon)); - debug.assert(math.approxEq(f64, c.im, 10.472508, epsilon)); + testing.expect(math.approxEq(f64, c.re, -73.460617, epsilon)); + testing.expect(math.approxEq(f64, c.im, 10.472508, epsilon)); } diff --git a/std/math/complex/sqrt.zig b/std/math/complex/sqrt.zig index 47367816f7..60f6061baa 100644 --- a/std/math/complex/sqrt.zig +++ b/std/math/complex/sqrt.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -125,14 +125,14 @@ test "complex.csqrt32" { const a = Complex(f32).new(5, 3); const c = sqrt(a); - debug.assert(math.approxEq(f32, c.re, 2.327117, epsilon)); - debug.assert(math.approxEq(f32, c.im, 0.644574, epsilon)); + testing.expect(math.approxEq(f32, c.re, 2.327117, epsilon)); + testing.expect(math.approxEq(f32, c.im, 0.644574, epsilon)); } test "complex.csqrt64" { const a = Complex(f64).new(5, 3); const c = sqrt(a); - debug.assert(math.approxEq(f64, c.re, 2.3271175190399496, epsilon)); - debug.assert(math.approxEq(f64, c.im, 0.6445742373246469, epsilon)); + testing.expect(math.approxEq(f64, c.re, 2.3271175190399496, epsilon)); + testing.expect(math.approxEq(f64, c.im, 0.6445742373246469, epsilon)); } diff --git a/std/math/complex/tan.zig b/std/math/complex/tan.zig index 4ea5182fa7..db34580598 100644 --- a/std/math/complex/tan.zig +++ b/std/math/complex/tan.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -17,6 +17,6 @@ test "complex.ctan" { const a = Complex(f32).new(5, 3); const c = tan(a); - debug.assert(math.approxEq(f32, c.re, -0.002708233, epsilon)); - debug.assert(math.approxEq(f32, c.im, 1.004165, epsilon)); + testing.expect(math.approxEq(f32, c.re, -0.002708233, epsilon)); + testing.expect(math.approxEq(f32, c.im, 1.004165, epsilon)); } diff --git a/std/math/complex/tanh.zig b/std/math/complex/tanh.zig index e48d438783..03ab431312 100644 --- a/std/math/complex/tanh.zig +++ b/std/math/complex/tanh.zig @@ -1,5 +1,5 @@ const std = @import("../../index.zig"); -const debug = std.debug; +const testing = std.testing; const math = std.math; const cmath = math.complex; const Complex = cmath.Complex; @@ -100,14 +100,14 @@ test "complex.ctanh32" { const a = Complex(f32).new(5, 3); const c = tanh(a); - debug.assert(math.approxEq(f32, c.re, 0.999913, epsilon)); - debug.assert(math.approxEq(f32, c.im, -0.000025, epsilon)); + testing.expect(math.approxEq(f32, c.re, 0.999913, epsilon)); + testing.expect(math.approxEq(f32, c.im, -0.000025, epsilon)); } test "complex.ctanh64" { const a = Complex(f64).new(5, 3); const c = tanh(a); - debug.assert(math.approxEq(f64, c.re, 0.999913, epsilon)); - debug.assert(math.approxEq(f64, c.im, -0.000025, epsilon)); + testing.expect(math.approxEq(f64, c.re, 0.999913, epsilon)); + testing.expect(math.approxEq(f64, c.im, -0.000025, epsilon)); } diff --git a/std/math/copysign.zig b/std/math/copysign.zig index 4c6e333d6c..dbf8f6e1ef 100644 --- a/std/math/copysign.zig +++ b/std/math/copysign.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn copysign(comptime T: type, x: T, y: T) T { @@ -40,28 +40,28 @@ fn copysign64(x: f64, y: f64) f64 { } test "math.copysign" { - assert(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0)); - assert(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0)); - assert(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0)); + expect(copysign(f16, 1.0, 1.0) == copysign16(1.0, 1.0)); + expect(copysign(f32, 1.0, 1.0) == copysign32(1.0, 1.0)); + expect(copysign(f64, 1.0, 1.0) == copysign64(1.0, 1.0)); } test "math.copysign16" { - assert(copysign16(5.0, 1.0) == 5.0); - assert(copysign16(5.0, -1.0) == -5.0); - assert(copysign16(-5.0, -1.0) == -5.0); - assert(copysign16(-5.0, 1.0) == 5.0); + expect(copysign16(5.0, 1.0) == 5.0); + expect(copysign16(5.0, -1.0) == -5.0); + expect(copysign16(-5.0, -1.0) == -5.0); + expect(copysign16(-5.0, 1.0) == 5.0); } test "math.copysign32" { - assert(copysign32(5.0, 1.0) == 5.0); - assert(copysign32(5.0, -1.0) == -5.0); - assert(copysign32(-5.0, -1.0) == -5.0); - assert(copysign32(-5.0, 1.0) == 5.0); + expect(copysign32(5.0, 1.0) == 5.0); + expect(copysign32(5.0, -1.0) == -5.0); + expect(copysign32(-5.0, -1.0) == -5.0); + expect(copysign32(-5.0, 1.0) == 5.0); } test "math.copysign64" { - assert(copysign64(5.0, 1.0) == 5.0); - assert(copysign64(5.0, -1.0) == -5.0); - assert(copysign64(-5.0, -1.0) == -5.0); - assert(copysign64(-5.0, 1.0) == 5.0); + expect(copysign64(5.0, 1.0) == 5.0); + expect(copysign64(5.0, -1.0) == -5.0); + expect(copysign64(-5.0, -1.0) == -5.0); + expect(copysign64(-5.0, 1.0) == 5.0); } diff --git a/std/math/cos.zig b/std/math/cos.zig index b6a2fbffe6..7783ddc09b 100644 --- a/std/math/cos.zig +++ b/std/math/cos.zig @@ -6,7 +6,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn cos(x: var) @typeOf(x) { const T = @typeOf(x); @@ -139,40 +139,40 @@ fn cos64(x_: f64) f64 { } test "math.cos" { - assert(cos(f32(0.0)) == cos32(0.0)); - assert(cos(f64(0.0)) == cos64(0.0)); + expect(cos(f32(0.0)) == cos32(0.0)); + expect(cos(f64(0.0)) == cos64(0.0)); } test "math.cos32" { const epsilon = 0.000001; - assert(math.approxEq(f32, cos32(0.0), 1.0, epsilon)); - assert(math.approxEq(f32, cos32(0.2), 0.980067, epsilon)); - assert(math.approxEq(f32, cos32(0.8923), 0.627623, epsilon)); - assert(math.approxEq(f32, cos32(1.5), 0.070737, epsilon)); - assert(math.approxEq(f32, cos32(37.45), 0.969132, epsilon)); - assert(math.approxEq(f32, cos32(89.123), 0.400798, epsilon)); + expect(math.approxEq(f32, cos32(0.0), 1.0, epsilon)); + expect(math.approxEq(f32, cos32(0.2), 0.980067, epsilon)); + expect(math.approxEq(f32, cos32(0.8923), 0.627623, epsilon)); + expect(math.approxEq(f32, cos32(1.5), 0.070737, epsilon)); + expect(math.approxEq(f32, cos32(37.45), 0.969132, epsilon)); + expect(math.approxEq(f32, cos32(89.123), 0.400798, epsilon)); } test "math.cos64" { const epsilon = 0.000001; - assert(math.approxEq(f64, cos64(0.0), 1.0, epsilon)); - assert(math.approxEq(f64, cos64(0.2), 0.980067, epsilon)); - assert(math.approxEq(f64, cos64(0.8923), 0.627623, epsilon)); - assert(math.approxEq(f64, cos64(1.5), 0.070737, epsilon)); - assert(math.approxEq(f64, cos64(37.45), 0.969132, epsilon)); - assert(math.approxEq(f64, cos64(89.123), 0.40080, epsilon)); + expect(math.approxEq(f64, cos64(0.0), 1.0, epsilon)); + expect(math.approxEq(f64, cos64(0.2), 0.980067, epsilon)); + expect(math.approxEq(f64, cos64(0.8923), 0.627623, epsilon)); + expect(math.approxEq(f64, cos64(1.5), 0.070737, epsilon)); + expect(math.approxEq(f64, cos64(37.45), 0.969132, epsilon)); + expect(math.approxEq(f64, cos64(89.123), 0.40080, epsilon)); } test "math.cos32.special" { - assert(math.isNan(cos32(math.inf(f32)))); - assert(math.isNan(cos32(-math.inf(f32)))); - assert(math.isNan(cos32(math.nan(f32)))); + expect(math.isNan(cos32(math.inf(f32)))); + expect(math.isNan(cos32(-math.inf(f32)))); + expect(math.isNan(cos32(math.nan(f32)))); } test "math.cos64.special" { - assert(math.isNan(cos64(math.inf(f64)))); - assert(math.isNan(cos64(-math.inf(f64)))); - assert(math.isNan(cos64(math.nan(f64)))); + expect(math.isNan(cos64(math.inf(f64)))); + expect(math.isNan(cos64(-math.inf(f64)))); + expect(math.isNan(cos64(math.nan(f64)))); } diff --git a/std/math/cosh.zig b/std/math/cosh.zig index 77e02855fd..57c5eef828 100644 --- a/std/math/cosh.zig +++ b/std/math/cosh.zig @@ -8,7 +8,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; const expo2 = @import("expo2.zig").expo2; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn cosh(x: var) @typeOf(x) { @@ -82,40 +82,40 @@ fn cosh64(x: f64) f64 { } test "math.cosh" { - assert(cosh(f32(1.5)) == cosh32(1.5)); - assert(cosh(f64(1.5)) == cosh64(1.5)); + expect(cosh(f32(1.5)) == cosh32(1.5)); + expect(cosh(f64(1.5)) == cosh64(1.5)); } test "math.cosh32" { const epsilon = 0.000001; - assert(math.approxEq(f32, cosh32(0.0), 1.0, epsilon)); - assert(math.approxEq(f32, cosh32(0.2), 1.020067, epsilon)); - assert(math.approxEq(f32, cosh32(0.8923), 1.425225, epsilon)); - assert(math.approxEq(f32, cosh32(1.5), 2.352410, epsilon)); + expect(math.approxEq(f32, cosh32(0.0), 1.0, epsilon)); + expect(math.approxEq(f32, cosh32(0.2), 1.020067, epsilon)); + expect(math.approxEq(f32, cosh32(0.8923), 1.425225, epsilon)); + expect(math.approxEq(f32, cosh32(1.5), 2.352410, epsilon)); } test "math.cosh64" { const epsilon = 0.000001; - assert(math.approxEq(f64, cosh64(0.0), 1.0, epsilon)); - assert(math.approxEq(f64, cosh64(0.2), 1.020067, epsilon)); - assert(math.approxEq(f64, cosh64(0.8923), 1.425225, epsilon)); - assert(math.approxEq(f64, cosh64(1.5), 2.352410, epsilon)); + expect(math.approxEq(f64, cosh64(0.0), 1.0, epsilon)); + expect(math.approxEq(f64, cosh64(0.2), 1.020067, epsilon)); + expect(math.approxEq(f64, cosh64(0.8923), 1.425225, epsilon)); + expect(math.approxEq(f64, cosh64(1.5), 2.352410, epsilon)); } test "math.cosh32.special" { - assert(cosh32(0.0) == 1.0); - assert(cosh32(-0.0) == 1.0); - assert(math.isPositiveInf(cosh32(math.inf(f32)))); - assert(math.isPositiveInf(cosh32(-math.inf(f32)))); - assert(math.isNan(cosh32(math.nan(f32)))); + expect(cosh32(0.0) == 1.0); + expect(cosh32(-0.0) == 1.0); + expect(math.isPositiveInf(cosh32(math.inf(f32)))); + expect(math.isPositiveInf(cosh32(-math.inf(f32)))); + expect(math.isNan(cosh32(math.nan(f32)))); } test "math.cosh64.special" { - assert(cosh64(0.0) == 1.0); - assert(cosh64(-0.0) == 1.0); - assert(math.isPositiveInf(cosh64(math.inf(f64)))); - assert(math.isPositiveInf(cosh64(-math.inf(f64)))); - assert(math.isNan(cosh64(math.nan(f64)))); + expect(cosh64(0.0) == 1.0); + expect(cosh64(-0.0) == 1.0); + expect(math.isPositiveInf(cosh64(math.inf(f64)))); + expect(math.isPositiveInf(cosh64(-math.inf(f64)))); + expect(math.isNan(cosh64(math.nan(f64)))); } diff --git a/std/math/exp2.zig b/std/math/exp2.zig index 3d8e5d692e..ba0001a09a 100644 --- a/std/math/exp2.zig +++ b/std/math/exp2.zig @@ -5,7 +5,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn exp2(x: var) @typeOf(x) { const T = @typeOf(x); @@ -415,35 +415,35 @@ fn exp2_64(x: f64) f64 { } test "math.exp2" { - assert(exp2(f32(0.8923)) == exp2_32(0.8923)); - assert(exp2(f64(0.8923)) == exp2_64(0.8923)); + expect(exp2(f32(0.8923)) == exp2_32(0.8923)); + expect(exp2(f64(0.8923)) == exp2_64(0.8923)); } test "math.exp2_32" { const epsilon = 0.000001; - assert(exp2_32(0.0) == 1.0); - assert(math.approxEq(f32, exp2_32(0.2), 1.148698, epsilon)); - assert(math.approxEq(f32, exp2_32(0.8923), 1.856133, epsilon)); - assert(math.approxEq(f32, exp2_32(1.5), 2.828427, epsilon)); - assert(math.approxEq(f32, exp2_32(37.45), 187747237888, epsilon)); + expect(exp2_32(0.0) == 1.0); + expect(math.approxEq(f32, exp2_32(0.2), 1.148698, epsilon)); + expect(math.approxEq(f32, exp2_32(0.8923), 1.856133, epsilon)); + expect(math.approxEq(f32, exp2_32(1.5), 2.828427, epsilon)); + expect(math.approxEq(f32, exp2_32(37.45), 187747237888, epsilon)); } test "math.exp2_64" { const epsilon = 0.000001; - assert(exp2_64(0.0) == 1.0); - assert(math.approxEq(f64, exp2_64(0.2), 1.148698, epsilon)); - assert(math.approxEq(f64, exp2_64(0.8923), 1.856133, epsilon)); - assert(math.approxEq(f64, exp2_64(1.5), 2.828427, epsilon)); + expect(exp2_64(0.0) == 1.0); + expect(math.approxEq(f64, exp2_64(0.2), 1.148698, epsilon)); + expect(math.approxEq(f64, exp2_64(0.8923), 1.856133, epsilon)); + expect(math.approxEq(f64, exp2_64(1.5), 2.828427, epsilon)); } test "math.exp2_32.special" { - assert(math.isPositiveInf(exp2_32(math.inf(f32)))); - assert(math.isNan(exp2_32(math.nan(f32)))); + expect(math.isPositiveInf(exp2_32(math.inf(f32)))); + expect(math.isNan(exp2_32(math.nan(f32)))); } test "math.exp2_64.special" { - assert(math.isPositiveInf(exp2_64(math.inf(f64)))); - assert(math.isNan(exp2_64(math.nan(f64)))); + expect(math.isPositiveInf(exp2_64(math.inf(f64)))); + expect(math.isNan(exp2_64(math.nan(f64)))); } diff --git a/std/math/expm1.zig b/std/math/expm1.zig index 6729417f60..ba00ec2561 100644 --- a/std/math/expm1.zig +++ b/std/math/expm1.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn expm1(x: var) @typeOf(x) { const T = @typeOf(x); @@ -278,42 +278,42 @@ fn expm1_64(x_: f64) f64 { } test "math.exp1m" { - assert(expm1(f32(0.0)) == expm1_32(0.0)); - assert(expm1(f64(0.0)) == expm1_64(0.0)); + expect(expm1(f32(0.0)) == expm1_32(0.0)); + expect(expm1(f64(0.0)) == expm1_64(0.0)); } test "math.expm1_32" { const epsilon = 0.000001; - assert(expm1_32(0.0) == 0.0); - assert(math.approxEq(f32, expm1_32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, expm1_32(0.2), 0.221403, epsilon)); - assert(math.approxEq(f32, expm1_32(0.8923), 1.440737, epsilon)); - assert(math.approxEq(f32, expm1_32(1.5), 3.481689, epsilon)); + expect(expm1_32(0.0) == 0.0); + expect(math.approxEq(f32, expm1_32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, expm1_32(0.2), 0.221403, epsilon)); + expect(math.approxEq(f32, expm1_32(0.8923), 1.440737, epsilon)); + expect(math.approxEq(f32, expm1_32(1.5), 3.481689, epsilon)); } test "math.expm1_64" { const epsilon = 0.000001; - assert(expm1_64(0.0) == 0.0); - assert(math.approxEq(f64, expm1_64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, expm1_64(0.2), 0.221403, epsilon)); - assert(math.approxEq(f64, expm1_64(0.8923), 1.440737, epsilon)); - assert(math.approxEq(f64, expm1_64(1.5), 3.481689, epsilon)); + expect(expm1_64(0.0) == 0.0); + expect(math.approxEq(f64, expm1_64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, expm1_64(0.2), 0.221403, epsilon)); + expect(math.approxEq(f64, expm1_64(0.8923), 1.440737, epsilon)); + expect(math.approxEq(f64, expm1_64(1.5), 3.481689, epsilon)); } test "math.expm1_32.special" { const epsilon = 0.000001; - assert(math.isPositiveInf(expm1_32(math.inf(f32)))); - assert(expm1_32(-math.inf(f32)) == -1.0); - assert(math.isNan(expm1_32(math.nan(f32)))); + expect(math.isPositiveInf(expm1_32(math.inf(f32)))); + expect(expm1_32(-math.inf(f32)) == -1.0); + expect(math.isNan(expm1_32(math.nan(f32)))); } test "math.expm1_64.special" { const epsilon = 0.000001; - assert(math.isPositiveInf(expm1_64(math.inf(f64)))); - assert(expm1_64(-math.inf(f64)) == -1.0); - assert(math.isNan(expm1_64(math.nan(f64)))); + expect(math.isPositiveInf(expm1_64(math.inf(f64)))); + expect(expm1_64(-math.inf(f64)) == -1.0); + expect(math.isNan(expm1_64(math.nan(f64)))); } diff --git a/std/math/fabs.zig b/std/math/fabs.zig index 443010ac7f..9fad5644c3 100644 --- a/std/math/fabs.zig +++ b/std/math/fabs.zig @@ -5,7 +5,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn fabs(x: var) @typeOf(x) { @@ -37,40 +37,40 @@ fn fabs64(x: f64) f64 { } test "math.fabs" { - assert(fabs(f16(1.0)) == fabs16(1.0)); - assert(fabs(f32(1.0)) == fabs32(1.0)); - assert(fabs(f64(1.0)) == fabs64(1.0)); + expect(fabs(f16(1.0)) == fabs16(1.0)); + expect(fabs(f32(1.0)) == fabs32(1.0)); + expect(fabs(f64(1.0)) == fabs64(1.0)); } test "math.fabs16" { - assert(fabs16(1.0) == 1.0); - assert(fabs16(-1.0) == 1.0); + expect(fabs16(1.0) == 1.0); + expect(fabs16(-1.0) == 1.0); } test "math.fabs32" { - assert(fabs32(1.0) == 1.0); - assert(fabs32(-1.0) == 1.0); + expect(fabs32(1.0) == 1.0); + expect(fabs32(-1.0) == 1.0); } test "math.fabs64" { - assert(fabs64(1.0) == 1.0); - assert(fabs64(-1.0) == 1.0); + expect(fabs64(1.0) == 1.0); + expect(fabs64(-1.0) == 1.0); } test "math.fabs16.special" { - assert(math.isPositiveInf(fabs(math.inf(f16)))); - assert(math.isPositiveInf(fabs(-math.inf(f16)))); - assert(math.isNan(fabs(math.nan(f16)))); + expect(math.isPositiveInf(fabs(math.inf(f16)))); + expect(math.isPositiveInf(fabs(-math.inf(f16)))); + expect(math.isNan(fabs(math.nan(f16)))); } test "math.fabs32.special" { - assert(math.isPositiveInf(fabs(math.inf(f32)))); - assert(math.isPositiveInf(fabs(-math.inf(f32)))); - assert(math.isNan(fabs(math.nan(f32)))); + expect(math.isPositiveInf(fabs(math.inf(f32)))); + expect(math.isPositiveInf(fabs(-math.inf(f32)))); + expect(math.isNan(fabs(math.nan(f32)))); } test "math.fabs64.special" { - assert(math.isPositiveInf(fabs(math.inf(f64)))); - assert(math.isPositiveInf(fabs(-math.inf(f64)))); - assert(math.isNan(fabs(math.nan(f64)))); + expect(math.isPositiveInf(fabs(math.inf(f64)))); + expect(math.isPositiveInf(fabs(-math.inf(f64)))); + expect(math.isNan(fabs(math.nan(f64)))); } diff --git a/std/math/floor.zig b/std/math/floor.zig index 6ce462b10f..c7c12e37ba 100644 --- a/std/math/floor.zig +++ b/std/math/floor.zig @@ -5,7 +5,7 @@ // - floor(nan) = nan const builtin = @import("builtin"); -const assert = std.debug.assert; +const expect = std.testing.expect; const std = @import("../index.zig"); const math = std.math; @@ -117,49 +117,49 @@ fn floor64(x: f64) f64 { } test "math.floor" { - assert(floor(f16(1.3)) == floor16(1.3)); - assert(floor(f32(1.3)) == floor32(1.3)); - assert(floor(f64(1.3)) == floor64(1.3)); + expect(floor(f16(1.3)) == floor16(1.3)); + expect(floor(f32(1.3)) == floor32(1.3)); + expect(floor(f64(1.3)) == floor64(1.3)); } test "math.floor16" { - assert(floor16(1.3) == 1.0); - assert(floor16(-1.3) == -2.0); - assert(floor16(0.2) == 0.0); + expect(floor16(1.3) == 1.0); + expect(floor16(-1.3) == -2.0); + expect(floor16(0.2) == 0.0); } test "math.floor32" { - assert(floor32(1.3) == 1.0); - assert(floor32(-1.3) == -2.0); - assert(floor32(0.2) == 0.0); + expect(floor32(1.3) == 1.0); + expect(floor32(-1.3) == -2.0); + expect(floor32(0.2) == 0.0); } test "math.floor64" { - assert(floor64(1.3) == 1.0); - assert(floor64(-1.3) == -2.0); - assert(floor64(0.2) == 0.0); + expect(floor64(1.3) == 1.0); + expect(floor64(-1.3) == -2.0); + expect(floor64(0.2) == 0.0); } test "math.floor16.special" { - assert(floor16(0.0) == 0.0); - assert(floor16(-0.0) == -0.0); - assert(math.isPositiveInf(floor16(math.inf(f16)))); - assert(math.isNegativeInf(floor16(-math.inf(f16)))); - assert(math.isNan(floor16(math.nan(f16)))); + expect(floor16(0.0) == 0.0); + expect(floor16(-0.0) == -0.0); + expect(math.isPositiveInf(floor16(math.inf(f16)))); + expect(math.isNegativeInf(floor16(-math.inf(f16)))); + expect(math.isNan(floor16(math.nan(f16)))); } test "math.floor32.special" { - assert(floor32(0.0) == 0.0); - assert(floor32(-0.0) == -0.0); - assert(math.isPositiveInf(floor32(math.inf(f32)))); - assert(math.isNegativeInf(floor32(-math.inf(f32)))); - assert(math.isNan(floor32(math.nan(f32)))); + expect(floor32(0.0) == 0.0); + expect(floor32(-0.0) == -0.0); + expect(math.isPositiveInf(floor32(math.inf(f32)))); + expect(math.isNegativeInf(floor32(-math.inf(f32)))); + expect(math.isNan(floor32(math.nan(f32)))); } test "math.floor64.special" { - assert(floor64(0.0) == 0.0); - assert(floor64(-0.0) == -0.0); - assert(math.isPositiveInf(floor64(math.inf(f64)))); - assert(math.isNegativeInf(floor64(-math.inf(f64)))); - assert(math.isNan(floor64(math.nan(f64)))); + expect(floor64(0.0) == 0.0); + expect(floor64(-0.0) == -0.0); + expect(math.isPositiveInf(floor64(math.inf(f64)))); + expect(math.isNegativeInf(floor64(-math.inf(f64)))); + expect(math.isNan(floor64(math.nan(f64)))); } diff --git a/std/math/fma.zig b/std/math/fma.zig index 21faf4118d..b084cf3cbd 100644 --- a/std/math/fma.zig +++ b/std/math/fma.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn fma(comptime T: type, x: T, y: T, z: T) T { return switch (T) { @@ -135,30 +135,30 @@ fn add_and_denorm(a: f64, b: f64, scale: i32) f64 { } test "math.fma" { - assert(fma(f32, 0.0, 1.0, 1.0) == fma32(0.0, 1.0, 1.0)); - assert(fma(f64, 0.0, 1.0, 1.0) == fma64(0.0, 1.0, 1.0)); + expect(fma(f32, 0.0, 1.0, 1.0) == fma32(0.0, 1.0, 1.0)); + expect(fma(f64, 0.0, 1.0, 1.0) == fma64(0.0, 1.0, 1.0)); } test "math.fma32" { const epsilon = 0.000001; - assert(math.approxEq(f32, fma32(0.0, 5.0, 9.124), 9.124, epsilon)); - assert(math.approxEq(f32, fma32(0.2, 5.0, 9.124), 10.124, epsilon)); - assert(math.approxEq(f32, fma32(0.8923, 5.0, 9.124), 13.5855, epsilon)); - assert(math.approxEq(f32, fma32(1.5, 5.0, 9.124), 16.624, epsilon)); - assert(math.approxEq(f32, fma32(37.45, 5.0, 9.124), 196.374004, epsilon)); - assert(math.approxEq(f32, fma32(89.123, 5.0, 9.124), 454.739005, epsilon)); - assert(math.approxEq(f32, fma32(123123.234375, 5.0, 9.124), 615625.295875, epsilon)); + expect(math.approxEq(f32, fma32(0.0, 5.0, 9.124), 9.124, epsilon)); + expect(math.approxEq(f32, fma32(0.2, 5.0, 9.124), 10.124, epsilon)); + expect(math.approxEq(f32, fma32(0.8923, 5.0, 9.124), 13.5855, epsilon)); + expect(math.approxEq(f32, fma32(1.5, 5.0, 9.124), 16.624, epsilon)); + expect(math.approxEq(f32, fma32(37.45, 5.0, 9.124), 196.374004, epsilon)); + expect(math.approxEq(f32, fma32(89.123, 5.0, 9.124), 454.739005, epsilon)); + expect(math.approxEq(f32, fma32(123123.234375, 5.0, 9.124), 615625.295875, epsilon)); } test "math.fma64" { const epsilon = 0.000001; - assert(math.approxEq(f64, fma64(0.0, 5.0, 9.124), 9.124, epsilon)); - assert(math.approxEq(f64, fma64(0.2, 5.0, 9.124), 10.124, epsilon)); - assert(math.approxEq(f64, fma64(0.8923, 5.0, 9.124), 13.5855, epsilon)); - assert(math.approxEq(f64, fma64(1.5, 5.0, 9.124), 16.624, epsilon)); - assert(math.approxEq(f64, fma64(37.45, 5.0, 9.124), 196.374, epsilon)); - assert(math.approxEq(f64, fma64(89.123, 5.0, 9.124), 454.739, epsilon)); - assert(math.approxEq(f64, fma64(123123.234375, 5.0, 9.124), 615625.295875, epsilon)); + expect(math.approxEq(f64, fma64(0.0, 5.0, 9.124), 9.124, epsilon)); + expect(math.approxEq(f64, fma64(0.2, 5.0, 9.124), 10.124, epsilon)); + expect(math.approxEq(f64, fma64(0.8923, 5.0, 9.124), 13.5855, epsilon)); + expect(math.approxEq(f64, fma64(1.5, 5.0, 9.124), 16.624, epsilon)); + expect(math.approxEq(f64, fma64(37.45, 5.0, 9.124), 196.374, epsilon)); + expect(math.approxEq(f64, fma64(89.123, 5.0, 9.124), 454.739, epsilon)); + expect(math.approxEq(f64, fma64(123123.234375, 5.0, 9.124), 615625.295875, epsilon)); } diff --git a/std/math/frexp.zig b/std/math/frexp.zig index dfc790fdd9..35f3e081a9 100644 --- a/std/math/frexp.zig +++ b/std/math/frexp.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; fn frexp_result(comptime T: type) type { return struct { @@ -103,11 +103,11 @@ fn frexp64(x: f64) frexp64_result { test "math.frexp" { const a = frexp(f32(1.3)); const b = frexp32(1.3); - assert(a.significand == b.significand and a.exponent == b.exponent); + expect(a.significand == b.significand and a.exponent == b.exponent); const c = frexp(f64(1.3)); const d = frexp64(1.3); - assert(c.significand == d.significand and c.exponent == d.exponent); + expect(c.significand == d.significand and c.exponent == d.exponent); } test "math.frexp32" { @@ -115,10 +115,10 @@ test "math.frexp32" { var r: frexp32_result = undefined; r = frexp32(1.3); - assert(math.approxEq(f32, r.significand, 0.65, epsilon) and r.exponent == 1); + expect(math.approxEq(f32, r.significand, 0.65, epsilon) and r.exponent == 1); r = frexp32(78.0234); - assert(math.approxEq(f32, r.significand, 0.609558, epsilon) and r.exponent == 7); + expect(math.approxEq(f32, r.significand, 0.609558, epsilon) and r.exponent == 7); } test "math.frexp64" { @@ -126,46 +126,46 @@ test "math.frexp64" { var r: frexp64_result = undefined; r = frexp64(1.3); - assert(math.approxEq(f64, r.significand, 0.65, epsilon) and r.exponent == 1); + expect(math.approxEq(f64, r.significand, 0.65, epsilon) and r.exponent == 1); r = frexp64(78.0234); - assert(math.approxEq(f64, r.significand, 0.609558, epsilon) and r.exponent == 7); + expect(math.approxEq(f64, r.significand, 0.609558, epsilon) and r.exponent == 7); } test "math.frexp32.special" { var r: frexp32_result = undefined; r = frexp32(0.0); - assert(r.significand == 0.0 and r.exponent == 0); + expect(r.significand == 0.0 and r.exponent == 0); r = frexp32(-0.0); - assert(r.significand == -0.0 and r.exponent == 0); + expect(r.significand == -0.0 and r.exponent == 0); r = frexp32(math.inf(f32)); - assert(math.isPositiveInf(r.significand) and r.exponent == 0); + expect(math.isPositiveInf(r.significand) and r.exponent == 0); r = frexp32(-math.inf(f32)); - assert(math.isNegativeInf(r.significand) and r.exponent == 0); + expect(math.isNegativeInf(r.significand) and r.exponent == 0); r = frexp32(math.nan(f32)); - assert(math.isNan(r.significand)); + expect(math.isNan(r.significand)); } test "math.frexp64.special" { var r: frexp64_result = undefined; r = frexp64(0.0); - assert(r.significand == 0.0 and r.exponent == 0); + expect(r.significand == 0.0 and r.exponent == 0); r = frexp64(-0.0); - assert(r.significand == -0.0 and r.exponent == 0); + expect(r.significand == -0.0 and r.exponent == 0); r = frexp64(math.inf(f64)); - assert(math.isPositiveInf(r.significand) and r.exponent == 0); + expect(math.isPositiveInf(r.significand) and r.exponent == 0); r = frexp64(-math.inf(f64)); - assert(math.isNegativeInf(r.significand) and r.exponent == 0); + expect(math.isNegativeInf(r.significand) and r.exponent == 0); r = frexp64(math.nan(f64)); - assert(math.isNan(r.significand)); + expect(math.isNan(r.significand)); } diff --git a/std/math/hypot.zig b/std/math/hypot.zig index a63657a621..dea11e0a61 100644 --- a/std/math/hypot.zig +++ b/std/math/hypot.zig @@ -7,7 +7,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn hypot(comptime T: type, x: T, y: T) T { @@ -115,48 +115,48 @@ fn hypot64(x: f64, y: f64) f64 { } test "math.hypot" { - assert(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2)); - assert(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2)); + expect(hypot(f32, 0.0, -1.2) == hypot32(0.0, -1.2)); + expect(hypot(f64, 0.0, -1.2) == hypot64(0.0, -1.2)); } test "math.hypot32" { const epsilon = 0.000001; - assert(math.approxEq(f32, hypot32(0.0, -1.2), 1.2, epsilon)); - assert(math.approxEq(f32, hypot32(0.2, -0.34), 0.394462, epsilon)); - assert(math.approxEq(f32, hypot32(0.8923, 2.636890), 2.783772, epsilon)); - assert(math.approxEq(f32, hypot32(1.5, 5.25), 5.460083, epsilon)); - assert(math.approxEq(f32, hypot32(37.45, 159.835), 164.163742, epsilon)); - assert(math.approxEq(f32, hypot32(89.123, 382.028905), 392.286865, epsilon)); - assert(math.approxEq(f32, hypot32(123123.234375, 529428.707813), 543556.875, epsilon)); + expect(math.approxEq(f32, hypot32(0.0, -1.2), 1.2, epsilon)); + expect(math.approxEq(f32, hypot32(0.2, -0.34), 0.394462, epsilon)); + expect(math.approxEq(f32, hypot32(0.8923, 2.636890), 2.783772, epsilon)); + expect(math.approxEq(f32, hypot32(1.5, 5.25), 5.460083, epsilon)); + expect(math.approxEq(f32, hypot32(37.45, 159.835), 164.163742, epsilon)); + expect(math.approxEq(f32, hypot32(89.123, 382.028905), 392.286865, epsilon)); + expect(math.approxEq(f32, hypot32(123123.234375, 529428.707813), 543556.875, epsilon)); } test "math.hypot64" { const epsilon = 0.000001; - assert(math.approxEq(f64, hypot64(0.0, -1.2), 1.2, epsilon)); - assert(math.approxEq(f64, hypot64(0.2, -0.34), 0.394462, epsilon)); - assert(math.approxEq(f64, hypot64(0.8923, 2.636890), 2.783772, epsilon)); - assert(math.approxEq(f64, hypot64(1.5, 5.25), 5.460082, epsilon)); - assert(math.approxEq(f64, hypot64(37.45, 159.835), 164.163728, epsilon)); - assert(math.approxEq(f64, hypot64(89.123, 382.028905), 392.286876, epsilon)); - assert(math.approxEq(f64, hypot64(123123.234375, 529428.707813), 543556.885247, epsilon)); + expect(math.approxEq(f64, hypot64(0.0, -1.2), 1.2, epsilon)); + expect(math.approxEq(f64, hypot64(0.2, -0.34), 0.394462, epsilon)); + expect(math.approxEq(f64, hypot64(0.8923, 2.636890), 2.783772, epsilon)); + expect(math.approxEq(f64, hypot64(1.5, 5.25), 5.460082, epsilon)); + expect(math.approxEq(f64, hypot64(37.45, 159.835), 164.163728, epsilon)); + expect(math.approxEq(f64, hypot64(89.123, 382.028905), 392.286876, epsilon)); + expect(math.approxEq(f64, hypot64(123123.234375, 529428.707813), 543556.885247, epsilon)); } test "math.hypot32.special" { - assert(math.isPositiveInf(hypot32(math.inf(f32), 0.0))); - assert(math.isPositiveInf(hypot32(-math.inf(f32), 0.0))); - assert(math.isPositiveInf(hypot32(0.0, math.inf(f32)))); - assert(math.isPositiveInf(hypot32(0.0, -math.inf(f32)))); - assert(math.isNan(hypot32(math.nan(f32), 0.0))); - assert(math.isNan(hypot32(0.0, math.nan(f32)))); + expect(math.isPositiveInf(hypot32(math.inf(f32), 0.0))); + expect(math.isPositiveInf(hypot32(-math.inf(f32), 0.0))); + expect(math.isPositiveInf(hypot32(0.0, math.inf(f32)))); + expect(math.isPositiveInf(hypot32(0.0, -math.inf(f32)))); + expect(math.isNan(hypot32(math.nan(f32), 0.0))); + expect(math.isNan(hypot32(0.0, math.nan(f32)))); } test "math.hypot64.special" { - assert(math.isPositiveInf(hypot64(math.inf(f64), 0.0))); - assert(math.isPositiveInf(hypot64(-math.inf(f64), 0.0))); - assert(math.isPositiveInf(hypot64(0.0, math.inf(f64)))); - assert(math.isPositiveInf(hypot64(0.0, -math.inf(f64)))); - assert(math.isNan(hypot64(math.nan(f64), 0.0))); - assert(math.isNan(hypot64(0.0, math.nan(f64)))); + expect(math.isPositiveInf(hypot64(math.inf(f64), 0.0))); + expect(math.isPositiveInf(hypot64(-math.inf(f64), 0.0))); + expect(math.isPositiveInf(hypot64(0.0, math.inf(f64)))); + expect(math.isPositiveInf(hypot64(0.0, -math.inf(f64)))); + expect(math.isNan(hypot64(math.nan(f64), 0.0))); + expect(math.isNan(hypot64(0.0, math.nan(f64)))); } diff --git a/std/math/ilogb.zig b/std/math/ilogb.zig index e6bdb14012..e7b6485357 100644 --- a/std/math/ilogb.zig +++ b/std/math/ilogb.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; const minInt = std.math.minInt; @@ -95,38 +95,38 @@ fn ilogb64(x: f64) i32 { } test "math.ilogb" { - assert(ilogb(f32(0.2)) == ilogb32(0.2)); - assert(ilogb(f64(0.2)) == ilogb64(0.2)); + expect(ilogb(f32(0.2)) == ilogb32(0.2)); + expect(ilogb(f64(0.2)) == ilogb64(0.2)); } test "math.ilogb32" { - assert(ilogb32(0.0) == fp_ilogb0); - assert(ilogb32(0.5) == -1); - assert(ilogb32(0.8923) == -1); - assert(ilogb32(10.0) == 3); - assert(ilogb32(-123984) == 16); - assert(ilogb32(2398.23) == 11); + expect(ilogb32(0.0) == fp_ilogb0); + expect(ilogb32(0.5) == -1); + expect(ilogb32(0.8923) == -1); + expect(ilogb32(10.0) == 3); + expect(ilogb32(-123984) == 16); + expect(ilogb32(2398.23) == 11); } test "math.ilogb64" { - assert(ilogb64(0.0) == fp_ilogb0); - assert(ilogb64(0.5) == -1); - assert(ilogb64(0.8923) == -1); - assert(ilogb64(10.0) == 3); - assert(ilogb64(-123984) == 16); - assert(ilogb64(2398.23) == 11); + expect(ilogb64(0.0) == fp_ilogb0); + expect(ilogb64(0.5) == -1); + expect(ilogb64(0.8923) == -1); + expect(ilogb64(10.0) == 3); + expect(ilogb64(-123984) == 16); + expect(ilogb64(2398.23) == 11); } test "math.ilogb32.special" { - assert(ilogb32(math.inf(f32)) == maxInt(i32)); - assert(ilogb32(-math.inf(f32)) == maxInt(i32)); - assert(ilogb32(0.0) == minInt(i32)); - assert(ilogb32(math.nan(f32)) == maxInt(i32)); + expect(ilogb32(math.inf(f32)) == maxInt(i32)); + expect(ilogb32(-math.inf(f32)) == maxInt(i32)); + expect(ilogb32(0.0) == minInt(i32)); + expect(ilogb32(math.nan(f32)) == maxInt(i32)); } test "math.ilogb64.special" { - assert(ilogb64(math.inf(f64)) == maxInt(i32)); - assert(ilogb64(-math.inf(f64)) == maxInt(i32)); - assert(ilogb64(0.0) == minInt(i32)); - assert(ilogb64(math.nan(f64)) == maxInt(i32)); + expect(ilogb64(math.inf(f64)) == maxInt(i32)); + expect(ilogb64(-math.inf(f64)) == maxInt(i32)); + expect(ilogb64(0.0) == minInt(i32)); + expect(ilogb64(math.nan(f64)) == maxInt(i32)); } diff --git a/std/math/index.zig b/std/math/index.zig index f37de2505b..ccfac03038 100644 --- a/std/math/index.zig +++ b/std/math/index.zig @@ -2,6 +2,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const TypeId = builtin.TypeId; const assert = std.debug.assert; +const testing = std.testing; pub const e = 2.71828182845904523536028747135266249775724709369995; pub const pi = 3.14159265358979323846264338327950288419716939937510; @@ -240,7 +241,7 @@ pub fn min(x: var, y: var) @typeOf(x + y) { } test "math.min" { - assert(min(i32(-1), i32(2)) == -1); + testing.expect(min(i32(-1), i32(2)) == -1); } pub fn max(x: var, y: var) @typeOf(x + y) { @@ -248,7 +249,7 @@ pub fn max(x: var, y: var) @typeOf(x + y) { } test "math.max" { - assert(max(i32(-1), i32(2)) == 2); + testing.expect(max(i32(-1), i32(2)) == 2); } pub fn mul(comptime T: type, a: T, b: T) (error{Overflow}!T) { @@ -293,10 +294,10 @@ pub fn shl(comptime T: type, a: T, shift_amt: var) T { } test "math.shl" { - assert(shl(u8, 0b11111111, usize(3)) == 0b11111000); - assert(shl(u8, 0b11111111, usize(8)) == 0); - assert(shl(u8, 0b11111111, usize(9)) == 0); - assert(shl(u8, 0b11111111, isize(-2)) == 0b00111111); + testing.expect(shl(u8, 0b11111111, usize(3)) == 0b11111000); + testing.expect(shl(u8, 0b11111111, usize(8)) == 0); + testing.expect(shl(u8, 0b11111111, usize(9)) == 0); + testing.expect(shl(u8, 0b11111111, isize(-2)) == 0b00111111); } /// Shifts right. Overflowed bits are truncated. @@ -317,10 +318,10 @@ pub fn shr(comptime T: type, a: T, shift_amt: var) T { } test "math.shr" { - assert(shr(u8, 0b11111111, usize(3)) == 0b00011111); - assert(shr(u8, 0b11111111, usize(8)) == 0); - assert(shr(u8, 0b11111111, usize(9)) == 0); - assert(shr(u8, 0b11111111, isize(-2)) == 0b11111100); + testing.expect(shr(u8, 0b11111111, usize(3)) == 0b00011111); + testing.expect(shr(u8, 0b11111111, usize(8)) == 0); + testing.expect(shr(u8, 0b11111111, usize(9)) == 0); + testing.expect(shr(u8, 0b11111111, isize(-2)) == 0b11111100); } /// Rotates right. Only unsigned values can be rotated. @@ -335,11 +336,11 @@ pub fn rotr(comptime T: type, x: T, r: var) T { } test "math.rotr" { - assert(rotr(u8, 0b00000001, usize(0)) == 0b00000001); - assert(rotr(u8, 0b00000001, usize(9)) == 0b10000000); - assert(rotr(u8, 0b00000001, usize(8)) == 0b00000001); - assert(rotr(u8, 0b00000001, usize(4)) == 0b00010000); - assert(rotr(u8, 0b00000001, isize(-1)) == 0b00000010); + testing.expect(rotr(u8, 0b00000001, usize(0)) == 0b00000001); + testing.expect(rotr(u8, 0b00000001, usize(9)) == 0b10000000); + testing.expect(rotr(u8, 0b00000001, usize(8)) == 0b00000001); + testing.expect(rotr(u8, 0b00000001, usize(4)) == 0b00010000); + testing.expect(rotr(u8, 0b00000001, isize(-1)) == 0b00000010); } /// Rotates left. Only unsigned values can be rotated. @@ -354,11 +355,11 @@ pub fn rotl(comptime T: type, x: T, r: var) T { } test "math.rotl" { - assert(rotl(u8, 0b00000001, usize(0)) == 0b00000001); - assert(rotl(u8, 0b00000001, usize(9)) == 0b00000010); - assert(rotl(u8, 0b00000001, usize(8)) == 0b00000001); - assert(rotl(u8, 0b00000001, usize(4)) == 0b00010000); - assert(rotl(u8, 0b00000001, isize(-1)) == 0b10000000); + testing.expect(rotl(u8, 0b00000001, usize(0)) == 0b00000001); + testing.expect(rotl(u8, 0b00000001, usize(9)) == 0b00000010); + testing.expect(rotl(u8, 0b00000001, usize(8)) == 0b00000001); + testing.expect(rotl(u8, 0b00000001, usize(4)) == 0b00010000); + testing.expect(rotl(u8, 0b00000001, isize(-1)) == 0b10000000); } pub fn Log2Int(comptime T: type) type { @@ -389,50 +390,50 @@ pub fn IntFittingRange(comptime from: comptime_int, comptime to: comptime_int) t } test "math.IntFittingRange" { - assert(IntFittingRange(0, 0) == u0); - assert(IntFittingRange(0, 1) == u1); - assert(IntFittingRange(0, 2) == u2); - assert(IntFittingRange(0, 3) == u2); - assert(IntFittingRange(0, 4) == u3); - assert(IntFittingRange(0, 7) == u3); - assert(IntFittingRange(0, 8) == u4); - assert(IntFittingRange(0, 9) == u4); - assert(IntFittingRange(0, 15) == u4); - assert(IntFittingRange(0, 16) == u5); - assert(IntFittingRange(0, 17) == u5); - assert(IntFittingRange(0, 4095) == u12); - assert(IntFittingRange(2000, 4095) == u12); - assert(IntFittingRange(0, 4096) == u13); - assert(IntFittingRange(2000, 4096) == u13); - assert(IntFittingRange(0, 4097) == u13); - assert(IntFittingRange(2000, 4097) == u13); - assert(IntFittingRange(0, 123456789123456798123456789) == u87); - assert(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177); - - assert(IntFittingRange(-1, -1) == i1); - assert(IntFittingRange(-1, 0) == i1); - assert(IntFittingRange(-1, 1) == i2); - assert(IntFittingRange(-2, -2) == i2); - assert(IntFittingRange(-2, -1) == i2); - assert(IntFittingRange(-2, 0) == i2); - assert(IntFittingRange(-2, 1) == i2); - assert(IntFittingRange(-2, 2) == i3); - assert(IntFittingRange(-1, 2) == i3); - assert(IntFittingRange(-1, 3) == i3); - assert(IntFittingRange(-1, 4) == i4); - assert(IntFittingRange(-1, 7) == i4); - assert(IntFittingRange(-1, 8) == i5); - assert(IntFittingRange(-1, 9) == i5); - assert(IntFittingRange(-1, 15) == i5); - assert(IntFittingRange(-1, 16) == i6); - assert(IntFittingRange(-1, 17) == i6); - assert(IntFittingRange(-1, 4095) == i13); - assert(IntFittingRange(-4096, 4095) == i13); - assert(IntFittingRange(-1, 4096) == i14); - assert(IntFittingRange(-4097, 4095) == i14); - assert(IntFittingRange(-1, 4097) == i14); - assert(IntFittingRange(-1, 123456789123456798123456789) == i88); - assert(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178); + testing.expect(IntFittingRange(0, 0) == u0); + testing.expect(IntFittingRange(0, 1) == u1); + testing.expect(IntFittingRange(0, 2) == u2); + testing.expect(IntFittingRange(0, 3) == u2); + testing.expect(IntFittingRange(0, 4) == u3); + testing.expect(IntFittingRange(0, 7) == u3); + testing.expect(IntFittingRange(0, 8) == u4); + testing.expect(IntFittingRange(0, 9) == u4); + testing.expect(IntFittingRange(0, 15) == u4); + testing.expect(IntFittingRange(0, 16) == u5); + testing.expect(IntFittingRange(0, 17) == u5); + testing.expect(IntFittingRange(0, 4095) == u12); + testing.expect(IntFittingRange(2000, 4095) == u12); + testing.expect(IntFittingRange(0, 4096) == u13); + testing.expect(IntFittingRange(2000, 4096) == u13); + testing.expect(IntFittingRange(0, 4097) == u13); + testing.expect(IntFittingRange(2000, 4097) == u13); + testing.expect(IntFittingRange(0, 123456789123456798123456789) == u87); + testing.expect(IntFittingRange(0, 123456789123456798123456789123456789123456798123456789) == u177); + + testing.expect(IntFittingRange(-1, -1) == i1); + testing.expect(IntFittingRange(-1, 0) == i1); + testing.expect(IntFittingRange(-1, 1) == i2); + testing.expect(IntFittingRange(-2, -2) == i2); + testing.expect(IntFittingRange(-2, -1) == i2); + testing.expect(IntFittingRange(-2, 0) == i2); + testing.expect(IntFittingRange(-2, 1) == i2); + testing.expect(IntFittingRange(-2, 2) == i3); + testing.expect(IntFittingRange(-1, 2) == i3); + testing.expect(IntFittingRange(-1, 3) == i3); + testing.expect(IntFittingRange(-1, 4) == i4); + testing.expect(IntFittingRange(-1, 7) == i4); + testing.expect(IntFittingRange(-1, 8) == i5); + testing.expect(IntFittingRange(-1, 9) == i5); + testing.expect(IntFittingRange(-1, 15) == i5); + testing.expect(IntFittingRange(-1, 16) == i6); + testing.expect(IntFittingRange(-1, 17) == i6); + testing.expect(IntFittingRange(-1, 4095) == i13); + testing.expect(IntFittingRange(-4096, 4095) == i13); + testing.expect(IntFittingRange(-1, 4096) == i14); + testing.expect(IntFittingRange(-4097, 4095) == i14); + testing.expect(IntFittingRange(-1, 4097) == i14); + testing.expect(IntFittingRange(-1, 123456789123456798123456789) == i88); + testing.expect(IntFittingRange(-1, 123456789123456798123456789123456789123456798123456789) == i178); } test "math overflow functions" { @@ -441,10 +442,10 @@ test "math overflow functions" { } fn testOverflow() void { - assert((mul(i32, 3, 4) catch unreachable) == 12); - assert((add(i32, 3, 4) catch unreachable) == 7); - assert((sub(i32, 3, 4) catch unreachable) == -1); - assert((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000); + testing.expect((mul(i32, 3, 4) catch unreachable) == 12); + testing.expect((add(i32, 3, 4) catch unreachable) == 7); + testing.expect((sub(i32, 3, 4) catch unreachable) == -1); + testing.expect((shlExact(i32, 0b11, 4) catch unreachable) == 0b110000); } pub fn absInt(x: var) !@typeOf(x) { @@ -465,8 +466,8 @@ test "math.absInt" { comptime testAbsInt(); } fn testAbsInt() void { - assert((absInt(i32(-10)) catch unreachable) == 10); - assert((absInt(i32(10)) catch unreachable) == 10); + testing.expect((absInt(i32(-10)) catch unreachable) == 10); + testing.expect((absInt(i32(10)) catch unreachable) == 10); } pub const absFloat = @import("fabs.zig").fabs; @@ -483,13 +484,13 @@ test "math.divTrunc" { comptime testDivTrunc(); } fn testDivTrunc() void { - assert((divTrunc(i32, 5, 3) catch unreachable) == 1); - assert((divTrunc(i32, -5, 3) catch unreachable) == -1); - if (divTrunc(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); - if (divTrunc(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow); + testing.expect((divTrunc(i32, 5, 3) catch unreachable) == 1); + testing.expect((divTrunc(i32, -5, 3) catch unreachable) == -1); + testing.expectError(error.DivisionByZero, divTrunc(i8, -5, 0)); + testing.expectError(error.Overflow, divTrunc(i8, -128, -1)); - assert((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0); - assert((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0); + testing.expect((divTrunc(f32, 5.0, 3.0) catch unreachable) == 1.0); + testing.expect((divTrunc(f32, -5.0, 3.0) catch unreachable) == -1.0); } pub fn divFloor(comptime T: type, numerator: T, denominator: T) !T { @@ -504,13 +505,13 @@ test "math.divFloor" { comptime testDivFloor(); } fn testDivFloor() void { - assert((divFloor(i32, 5, 3) catch unreachable) == 1); - assert((divFloor(i32, -5, 3) catch unreachable) == -2); - if (divFloor(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); - if (divFloor(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow); + testing.expect((divFloor(i32, 5, 3) catch unreachable) == 1); + testing.expect((divFloor(i32, -5, 3) catch unreachable) == -2); + testing.expectError(error.DivisionByZero, divFloor(i8, -5, 0)); + testing.expectError(error.Overflow, divFloor(i8, -128, -1)); - assert((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0); - assert((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0); + testing.expect((divFloor(f32, 5.0, 3.0) catch unreachable) == 1.0); + testing.expect((divFloor(f32, -5.0, 3.0) catch unreachable) == -2.0); } pub fn divExact(comptime T: type, numerator: T, denominator: T) !T { @@ -527,15 +528,15 @@ test "math.divExact" { comptime testDivExact(); } fn testDivExact() void { - assert((divExact(i32, 10, 5) catch unreachable) == 2); - assert((divExact(i32, -10, 5) catch unreachable) == -2); - if (divExact(i8, -5, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); - if (divExact(i8, -128, -1)) |_| unreachable else |err| assert(err == error.Overflow); - if (divExact(i32, 5, 2)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder); + testing.expect((divExact(i32, 10, 5) catch unreachable) == 2); + testing.expect((divExact(i32, -10, 5) catch unreachable) == -2); + testing.expectError(error.DivisionByZero, divExact(i8, -5, 0)); + testing.expectError(error.Overflow, divExact(i8, -128, -1)); + testing.expectError(error.UnexpectedRemainder, divExact(i32, 5, 2)); - assert((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0); - assert((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0); - if (divExact(f32, 5.0, 2.0)) |_| unreachable else |err| assert(err == error.UnexpectedRemainder); + testing.expect((divExact(f32, 10.0, 5.0) catch unreachable) == 2.0); + testing.expect((divExact(f32, -10.0, 5.0) catch unreachable) == -2.0); + testing.expectError(error.UnexpectedRemainder, divExact(f32, 5.0, 2.0)); } pub fn mod(comptime T: type, numerator: T, denominator: T) !T { @@ -550,15 +551,15 @@ test "math.mod" { comptime testMod(); } fn testMod() void { - assert((mod(i32, -5, 3) catch unreachable) == 1); - assert((mod(i32, 5, 3) catch unreachable) == 2); - if (mod(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator); - if (mod(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); + testing.expect((mod(i32, -5, 3) catch unreachable) == 1); + testing.expect((mod(i32, 5, 3) catch unreachable) == 2); + testing.expectError(error.NegativeDenominator, mod(i32, 10, -1)); + testing.expectError(error.DivisionByZero, mod(i32, 10, 0)); - assert((mod(f32, -5, 3) catch unreachable) == 1); - assert((mod(f32, 5, 3) catch unreachable) == 2); - if (mod(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator); - if (mod(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); + testing.expect((mod(f32, -5, 3) catch unreachable) == 1); + testing.expect((mod(f32, 5, 3) catch unreachable) == 2); + testing.expectError(error.NegativeDenominator, mod(f32, 10, -1)); + testing.expectError(error.DivisionByZero, mod(f32, 10, 0)); } pub fn rem(comptime T: type, numerator: T, denominator: T) !T { @@ -573,15 +574,15 @@ test "math.rem" { comptime testRem(); } fn testRem() void { - assert((rem(i32, -5, 3) catch unreachable) == -2); - assert((rem(i32, 5, 3) catch unreachable) == 2); - if (rem(i32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator); - if (rem(i32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); + testing.expect((rem(i32, -5, 3) catch unreachable) == -2); + testing.expect((rem(i32, 5, 3) catch unreachable) == 2); + testing.expectError(error.NegativeDenominator, rem(i32, 10, -1)); + testing.expectError(error.DivisionByZero, rem(i32, 10, 0)); - assert((rem(f32, -5, 3) catch unreachable) == -2); - assert((rem(f32, 5, 3) catch unreachable) == 2); - if (rem(f32, 10, -1)) |_| unreachable else |err| assert(err == error.NegativeDenominator); - if (rem(f32, 10, 0)) |_| unreachable else |err| assert(err == error.DivisionByZero); + testing.expect((rem(f32, -5, 3) catch unreachable) == -2); + testing.expect((rem(f32, 5, 3) catch unreachable) == 2); + testing.expectError(error.NegativeDenominator, rem(f32, 10, -1)); + testing.expectError(error.DivisionByZero, rem(f32, 10, 0)); } /// Returns the absolute value of the integer parameter. @@ -594,14 +595,14 @@ pub fn absCast(x: var) @IntType(false, @typeOf(x).bit_count) { } test "math.absCast" { - assert(absCast(i32(-999)) == 999); - assert(@typeOf(absCast(i32(-999))) == u32); + testing.expect(absCast(i32(-999)) == 999); + testing.expect(@typeOf(absCast(i32(-999))) == u32); - assert(absCast(i32(999)) == 999); - assert(@typeOf(absCast(i32(999))) == u32); + testing.expect(absCast(i32(999)) == 999); + testing.expect(@typeOf(absCast(i32(999))) == u32); - assert(absCast(i32(minInt(i32))) == -minInt(i32)); - assert(@typeOf(absCast(i32(minInt(i32)))) == u32); + testing.expect(absCast(i32(minInt(i32))) == -minInt(i32)); + testing.expect(@typeOf(absCast(i32(minInt(i32)))) == u32); } /// Returns the negation of the integer parameter. @@ -618,13 +619,13 @@ pub fn negateCast(x: var) !@IntType(true, @typeOf(x).bit_count) { } test "math.negateCast" { - assert((negateCast(u32(999)) catch unreachable) == -999); - assert(@typeOf(negateCast(u32(999)) catch unreachable) == i32); + testing.expect((negateCast(u32(999)) catch unreachable) == -999); + testing.expect(@typeOf(negateCast(u32(999)) catch unreachable) == i32); - assert((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32)); - assert(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32); + testing.expect((negateCast(u32(-minInt(i32))) catch unreachable) == minInt(i32)); + testing.expect(@typeOf(negateCast(u32(-minInt(i32))) catch unreachable) == i32); - if (negateCast(u32(maxInt(i32) + 10))) |_| unreachable else |err| assert(err == error.Overflow); + testing.expectError(error.Overflow, negateCast(u32(maxInt(i32) + 10))); } /// Cast an integer to a different integer type. If the value doesn't fit, @@ -642,13 +643,13 @@ pub fn cast(comptime T: type, x: var) (error{Overflow}!T) { } test "math.cast" { - if (cast(u8, u32(300))) |_| @panic("fail") else |err| assert(err == error.Overflow); - if (cast(i8, i32(-200))) |_| @panic("fail") else |err| assert(err == error.Overflow); - if (cast(u8, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow); - if (cast(u64, i8(-1))) |_| @panic("fail") else |err| assert(err == error.Overflow); + testing.expectError(error.Overflow, cast(u8, u32(300))); + testing.expectError(error.Overflow, cast(i8, i32(-200))); + testing.expectError(error.Overflow, cast(u8, i8(-1))); + testing.expectError(error.Overflow, cast(u64, i8(-1))); - assert((try cast(u8, u32(255))) == u8(255)); - assert(@typeOf(try cast(u8, u32(255))) == u8); + testing.expect((try cast(u8, u32(255))) == u8(255)); + testing.expect(@typeOf(try cast(u8, u32(255))) == u8); } pub const AlignCastError = error{UnalignedMemory}; @@ -692,25 +693,25 @@ pub fn log2_int_ceil(comptime T: type, x: T) Log2Int(T) { } test "std.math.log2_int_ceil" { - assert(log2_int_ceil(u32, 1) == 0); - assert(log2_int_ceil(u32, 2) == 1); - assert(log2_int_ceil(u32, 3) == 2); - assert(log2_int_ceil(u32, 4) == 2); - assert(log2_int_ceil(u32, 5) == 3); - assert(log2_int_ceil(u32, 6) == 3); - assert(log2_int_ceil(u32, 7) == 3); - assert(log2_int_ceil(u32, 8) == 3); - assert(log2_int_ceil(u32, 9) == 4); - assert(log2_int_ceil(u32, 10) == 4); + testing.expect(log2_int_ceil(u32, 1) == 0); + testing.expect(log2_int_ceil(u32, 2) == 1); + testing.expect(log2_int_ceil(u32, 3) == 2); + testing.expect(log2_int_ceil(u32, 4) == 2); + testing.expect(log2_int_ceil(u32, 5) == 3); + testing.expect(log2_int_ceil(u32, 6) == 3); + testing.expect(log2_int_ceil(u32, 7) == 3); + testing.expect(log2_int_ceil(u32, 8) == 3); + testing.expect(log2_int_ceil(u32, 9) == 4); + testing.expect(log2_int_ceil(u32, 10) == 4); } fn testFloorPowerOfTwo() void { - assert(floorPowerOfTwo(u32, 63) == 32); - assert(floorPowerOfTwo(u32, 64) == 64); - assert(floorPowerOfTwo(u32, 65) == 64); - assert(floorPowerOfTwo(u4, 7) == 4); - assert(floorPowerOfTwo(u4, 8) == 8); - assert(floorPowerOfTwo(u4, 9) == 8); + testing.expect(floorPowerOfTwo(u32, 63) == 32); + testing.expect(floorPowerOfTwo(u32, 64) == 64); + testing.expect(floorPowerOfTwo(u32, 65) == 64); + testing.expect(floorPowerOfTwo(u4, 7) == 4); + testing.expect(floorPowerOfTwo(u4, 8) == 8); + testing.expect(floorPowerOfTwo(u4, 9) == 8); } pub fn lossyCast(comptime T: type, value: var) T { @@ -726,7 +727,7 @@ pub fn lossyCast(comptime T: type, value: var) T { test "math.f64_min" { const f64_min_u64 = 0x0010000000000000; const fmin: f64 = f64_min; - assert(@bitCast(u64, fmin) == f64_min_u64); + testing.expect(@bitCast(u64, fmin) == f64_min_u64); } pub fn maxInt(comptime T: type) comptime_int { @@ -745,36 +746,36 @@ pub fn minInt(comptime T: type) comptime_int { } test "minInt and maxInt" { - assert(maxInt(u0) == 0); - assert(maxInt(u1) == 1); - assert(maxInt(u8) == 255); - assert(maxInt(u16) == 65535); - assert(maxInt(u32) == 4294967295); - assert(maxInt(u64) == 18446744073709551615); - - assert(maxInt(i0) == 0); - assert(maxInt(i1) == 0); - assert(maxInt(i8) == 127); - assert(maxInt(i16) == 32767); - assert(maxInt(i32) == 2147483647); - assert(maxInt(i63) == 4611686018427387903); - assert(maxInt(i64) == 9223372036854775807); - - assert(minInt(u0) == 0); - assert(minInt(u1) == 0); - assert(minInt(u8) == 0); - assert(minInt(u16) == 0); - assert(minInt(u32) == 0); - assert(minInt(u63) == 0); - assert(minInt(u64) == 0); - - assert(minInt(i0) == 0); - assert(minInt(i1) == -1); - assert(minInt(i8) == -128); - assert(minInt(i16) == -32768); - assert(minInt(i32) == -2147483648); - assert(minInt(i63) == -4611686018427387904); - assert(minInt(i64) == -9223372036854775808); + testing.expect(maxInt(u0) == 0); + testing.expect(maxInt(u1) == 1); + testing.expect(maxInt(u8) == 255); + testing.expect(maxInt(u16) == 65535); + testing.expect(maxInt(u32) == 4294967295); + testing.expect(maxInt(u64) == 18446744073709551615); + + testing.expect(maxInt(i0) == 0); + testing.expect(maxInt(i1) == 0); + testing.expect(maxInt(i8) == 127); + testing.expect(maxInt(i16) == 32767); + testing.expect(maxInt(i32) == 2147483647); + testing.expect(maxInt(i63) == 4611686018427387903); + testing.expect(maxInt(i64) == 9223372036854775807); + + testing.expect(minInt(u0) == 0); + testing.expect(minInt(u1) == 0); + testing.expect(minInt(u8) == 0); + testing.expect(minInt(u16) == 0); + testing.expect(minInt(u32) == 0); + testing.expect(minInt(u63) == 0); + testing.expect(minInt(u64) == 0); + + testing.expect(minInt(i0) == 0); + testing.expect(minInt(i1) == -1); + testing.expect(minInt(i8) == -128); + testing.expect(minInt(i16) == -32768); + testing.expect(minInt(i32) == -2147483648); + testing.expect(minInt(i63) == -4611686018427387904); + testing.expect(minInt(i64) == -9223372036854775808); } test "max value type" { @@ -782,5 +783,5 @@ test "max value type" { // u32 would not work. But since the value is a number literal, // it works fine. const x: u32 = maxInt(i32); - assert(x == 2147483647); + testing.expect(x == 2147483647); } diff --git a/std/math/isfinite.zig b/std/math/isfinite.zig index bdfdff8f9f..bf1c9ac63c 100644 --- a/std/math/isfinite.zig +++ b/std/math/isfinite.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn isFinite(x: var) bool { @@ -25,16 +25,16 @@ pub fn isFinite(x: var) bool { } test "math.isFinite" { - assert(isFinite(f16(0.0))); - assert(isFinite(f16(-0.0))); - assert(isFinite(f32(0.0))); - assert(isFinite(f32(-0.0))); - assert(isFinite(f64(0.0))); - assert(isFinite(f64(-0.0))); - assert(!isFinite(math.inf(f16))); - assert(!isFinite(-math.inf(f16))); - assert(!isFinite(math.inf(f32))); - assert(!isFinite(-math.inf(f32))); - assert(!isFinite(math.inf(f64))); - assert(!isFinite(-math.inf(f64))); + expect(isFinite(f16(0.0))); + expect(isFinite(f16(-0.0))); + expect(isFinite(f32(0.0))); + expect(isFinite(f32(-0.0))); + expect(isFinite(f64(0.0))); + expect(isFinite(f64(-0.0))); + expect(!isFinite(math.inf(f16))); + expect(!isFinite(-math.inf(f16))); + expect(!isFinite(math.inf(f32))); + expect(!isFinite(-math.inf(f32))); + expect(!isFinite(math.inf(f64))); + expect(!isFinite(-math.inf(f64))); } diff --git a/std/math/isinf.zig b/std/math/isinf.zig index 93f66cc870..e34e9c5971 100644 --- a/std/math/isinf.zig +++ b/std/math/isinf.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn isInf(x: var) bool { @@ -61,46 +61,46 @@ pub fn isNegativeInf(x: var) bool { } test "math.isInf" { - assert(!isInf(f16(0.0))); - assert(!isInf(f16(-0.0))); - assert(!isInf(f32(0.0))); - assert(!isInf(f32(-0.0))); - assert(!isInf(f64(0.0))); - assert(!isInf(f64(-0.0))); - assert(isInf(math.inf(f16))); - assert(isInf(-math.inf(f16))); - assert(isInf(math.inf(f32))); - assert(isInf(-math.inf(f32))); - assert(isInf(math.inf(f64))); - assert(isInf(-math.inf(f64))); + expect(!isInf(f16(0.0))); + expect(!isInf(f16(-0.0))); + expect(!isInf(f32(0.0))); + expect(!isInf(f32(-0.0))); + expect(!isInf(f64(0.0))); + expect(!isInf(f64(-0.0))); + expect(isInf(math.inf(f16))); + expect(isInf(-math.inf(f16))); + expect(isInf(math.inf(f32))); + expect(isInf(-math.inf(f32))); + expect(isInf(math.inf(f64))); + expect(isInf(-math.inf(f64))); } test "math.isPositiveInf" { - assert(!isPositiveInf(f16(0.0))); - assert(!isPositiveInf(f16(-0.0))); - assert(!isPositiveInf(f32(0.0))); - assert(!isPositiveInf(f32(-0.0))); - assert(!isPositiveInf(f64(0.0))); - assert(!isPositiveInf(f64(-0.0))); - assert(isPositiveInf(math.inf(f16))); - assert(!isPositiveInf(-math.inf(f16))); - assert(isPositiveInf(math.inf(f32))); - assert(!isPositiveInf(-math.inf(f32))); - assert(isPositiveInf(math.inf(f64))); - assert(!isPositiveInf(-math.inf(f64))); + expect(!isPositiveInf(f16(0.0))); + expect(!isPositiveInf(f16(-0.0))); + expect(!isPositiveInf(f32(0.0))); + expect(!isPositiveInf(f32(-0.0))); + expect(!isPositiveInf(f64(0.0))); + expect(!isPositiveInf(f64(-0.0))); + expect(isPositiveInf(math.inf(f16))); + expect(!isPositiveInf(-math.inf(f16))); + expect(isPositiveInf(math.inf(f32))); + expect(!isPositiveInf(-math.inf(f32))); + expect(isPositiveInf(math.inf(f64))); + expect(!isPositiveInf(-math.inf(f64))); } test "math.isNegativeInf" { - assert(!isNegativeInf(f16(0.0))); - assert(!isNegativeInf(f16(-0.0))); - assert(!isNegativeInf(f32(0.0))); - assert(!isNegativeInf(f32(-0.0))); - assert(!isNegativeInf(f64(0.0))); - assert(!isNegativeInf(f64(-0.0))); - assert(!isNegativeInf(math.inf(f16))); - assert(isNegativeInf(-math.inf(f16))); - assert(!isNegativeInf(math.inf(f32))); - assert(isNegativeInf(-math.inf(f32))); - assert(!isNegativeInf(math.inf(f64))); - assert(isNegativeInf(-math.inf(f64))); + expect(!isNegativeInf(f16(0.0))); + expect(!isNegativeInf(f16(-0.0))); + expect(!isNegativeInf(f32(0.0))); + expect(!isNegativeInf(f32(-0.0))); + expect(!isNegativeInf(f64(0.0))); + expect(!isNegativeInf(f64(-0.0))); + expect(!isNegativeInf(math.inf(f16))); + expect(isNegativeInf(-math.inf(f16))); + expect(!isNegativeInf(math.inf(f32))); + expect(isNegativeInf(-math.inf(f32))); + expect(!isNegativeInf(math.inf(f64))); + expect(isNegativeInf(-math.inf(f64))); } diff --git a/std/math/isnan.zig b/std/math/isnan.zig index a2cb85b4f7..641da9e620 100644 --- a/std/math/isnan.zig +++ b/std/math/isnan.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn isNan(x: var) bool { @@ -31,10 +31,10 @@ pub fn isSignalNan(x: var) bool { } test "math.isNan" { - assert(isNan(math.nan(f16))); - assert(isNan(math.nan(f32))); - assert(isNan(math.nan(f64))); - assert(!isNan(f16(1.0))); - assert(!isNan(f32(1.0))); - assert(!isNan(f64(1.0))); + expect(isNan(math.nan(f16))); + expect(isNan(math.nan(f32))); + expect(isNan(math.nan(f64))); + expect(!isNan(f16(1.0))); + expect(!isNan(f32(1.0))); + expect(!isNan(f64(1.0))); } diff --git a/std/math/isnormal.zig b/std/math/isnormal.zig index cc088e46a0..2c57aea7a9 100644 --- a/std/math/isnormal.zig +++ b/std/math/isnormal.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn isNormal(x: var) bool { @@ -25,13 +25,13 @@ pub fn isNormal(x: var) bool { } test "math.isNormal" { - assert(!isNormal(math.nan(f16))); - assert(!isNormal(math.nan(f32))); - assert(!isNormal(math.nan(f64))); - assert(!isNormal(f16(0))); - assert(!isNormal(f32(0))); - assert(!isNormal(f64(0))); - assert(isNormal(f16(1.0))); - assert(isNormal(f32(1.0))); - assert(isNormal(f64(1.0))); + expect(!isNormal(math.nan(f16))); + expect(!isNormal(math.nan(f32))); + expect(!isNormal(math.nan(f64))); + expect(!isNormal(f16(0))); + expect(!isNormal(f32(0))); + expect(!isNormal(f64(0))); + expect(isNormal(f16(1.0))); + expect(isNormal(f32(1.0))); + expect(isNormal(f64(1.0))); } diff --git a/std/math/ln.zig b/std/math/ln.zig index a560fee8ec..257ce8054f 100644 --- a/std/math/ln.zig +++ b/std/math/ln.zig @@ -7,7 +7,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const builtin = @import("builtin"); const TypeId = builtin.TypeId; @@ -143,42 +143,42 @@ pub fn ln_64(x_: f64) f64 { } test "math.ln" { - assert(ln(f32(0.2)) == ln_32(0.2)); - assert(ln(f64(0.2)) == ln_64(0.2)); + expect(ln(f32(0.2)) == ln_32(0.2)); + expect(ln(f64(0.2)) == ln_64(0.2)); } test "math.ln32" { const epsilon = 0.000001; - assert(math.approxEq(f32, ln_32(0.2), -1.609438, epsilon)); - assert(math.approxEq(f32, ln_32(0.8923), -0.113953, epsilon)); - assert(math.approxEq(f32, ln_32(1.5), 0.405465, epsilon)); - assert(math.approxEq(f32, ln_32(37.45), 3.623007, epsilon)); - assert(math.approxEq(f32, ln_32(89.123), 4.490017, epsilon)); - assert(math.approxEq(f32, ln_32(123123.234375), 11.720941, epsilon)); + expect(math.approxEq(f32, ln_32(0.2), -1.609438, epsilon)); + expect(math.approxEq(f32, ln_32(0.8923), -0.113953, epsilon)); + expect(math.approxEq(f32, ln_32(1.5), 0.405465, epsilon)); + expect(math.approxEq(f32, ln_32(37.45), 3.623007, epsilon)); + expect(math.approxEq(f32, ln_32(89.123), 4.490017, epsilon)); + expect(math.approxEq(f32, ln_32(123123.234375), 11.720941, epsilon)); } test "math.ln64" { const epsilon = 0.000001; - assert(math.approxEq(f64, ln_64(0.2), -1.609438, epsilon)); - assert(math.approxEq(f64, ln_64(0.8923), -0.113953, epsilon)); - assert(math.approxEq(f64, ln_64(1.5), 0.405465, epsilon)); - assert(math.approxEq(f64, ln_64(37.45), 3.623007, epsilon)); - assert(math.approxEq(f64, ln_64(89.123), 4.490017, epsilon)); - assert(math.approxEq(f64, ln_64(123123.234375), 11.720941, epsilon)); + expect(math.approxEq(f64, ln_64(0.2), -1.609438, epsilon)); + expect(math.approxEq(f64, ln_64(0.8923), -0.113953, epsilon)); + expect(math.approxEq(f64, ln_64(1.5), 0.405465, epsilon)); + expect(math.approxEq(f64, ln_64(37.45), 3.623007, epsilon)); + expect(math.approxEq(f64, ln_64(89.123), 4.490017, epsilon)); + expect(math.approxEq(f64, ln_64(123123.234375), 11.720941, epsilon)); } test "math.ln32.special" { - assert(math.isPositiveInf(ln_32(math.inf(f32)))); - assert(math.isNegativeInf(ln_32(0.0))); - assert(math.isNan(ln_32(-1.0))); - assert(math.isNan(ln_32(math.nan(f32)))); + expect(math.isPositiveInf(ln_32(math.inf(f32)))); + expect(math.isNegativeInf(ln_32(0.0))); + expect(math.isNan(ln_32(-1.0))); + expect(math.isNan(ln_32(math.nan(f32)))); } test "math.ln64.special" { - assert(math.isPositiveInf(ln_64(math.inf(f64)))); - assert(math.isNegativeInf(ln_64(0.0))); - assert(math.isNan(ln_64(-1.0))); - assert(math.isNan(ln_64(math.nan(f64)))); + expect(math.isPositiveInf(ln_64(math.inf(f64)))); + expect(math.isNegativeInf(ln_64(0.0))); + expect(math.isNan(ln_64(-1.0))); + expect(math.isNan(ln_64(math.nan(f64)))); } diff --git a/std/math/log.zig b/std/math/log.zig index 20b6d055e8..21cffcc078 100644 --- a/std/math/log.zig +++ b/std/math/log.zig @@ -2,7 +2,7 @@ const std = @import("../index.zig"); const math = std.math; const builtin = @import("builtin"); const TypeId = builtin.TypeId; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn log(comptime T: type, base: T, x: T) T { if (base == 2) { @@ -41,25 +41,25 @@ pub fn log(comptime T: type, base: T, x: T) T { } test "math.log integer" { - assert(log(u8, 2, 0x1) == 0); - assert(log(u8, 2, 0x2) == 1); - assert(log(i16, 2, 0x72) == 6); - assert(log(u32, 2, 0xFFFFFF) == 23); - assert(log(u64, 2, 0x7FF0123456789ABC) == 62); + expect(log(u8, 2, 0x1) == 0); + expect(log(u8, 2, 0x2) == 1); + expect(log(i16, 2, 0x72) == 6); + expect(log(u32, 2, 0xFFFFFF) == 23); + expect(log(u64, 2, 0x7FF0123456789ABC) == 62); } test "math.log float" { const epsilon = 0.000001; - assert(math.approxEq(f32, log(f32, 6, 0.23947), -0.797723, epsilon)); - assert(math.approxEq(f32, log(f32, 89, 0.23947), -0.318432, epsilon)); - assert(math.approxEq(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon)); + expect(math.approxEq(f32, log(f32, 6, 0.23947), -0.797723, epsilon)); + expect(math.approxEq(f32, log(f32, 89, 0.23947), -0.318432, epsilon)); + expect(math.approxEq(f64, log(f64, 123897, 12389216414), 1.981724596, epsilon)); } test "math.log float_special" { - assert(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974))); - assert(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974))); + expect(log(f32, 2, 0.2301974) == math.log2(f32(0.2301974))); + expect(log(f32, 10, 0.2301974) == math.log10(f32(0.2301974))); - assert(log(f64, 2, 213.23019799993) == math.log2(f64(213.23019799993))); - assert(log(f64, 10, 213.23019799993) == math.log10(f64(213.23019799993))); + expect(log(f64, 2, 213.23019799993) == math.log2(f64(213.23019799993))); + expect(log(f64, 10, 213.23019799993) == math.log10(f64(213.23019799993))); } diff --git a/std/math/log10.zig b/std/math/log10.zig index 2b53d8a6ae..8055f71280 100644 --- a/std/math/log10.zig +++ b/std/math/log10.zig @@ -7,7 +7,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const builtin = @import("builtin"); const TypeId = builtin.TypeId; const maxInt = std.math.maxInt; @@ -171,42 +171,42 @@ pub fn log10_64(x_: f64) f64 { } test "math.log10" { - assert(log10(f32(0.2)) == log10_32(0.2)); - assert(log10(f64(0.2)) == log10_64(0.2)); + testing.expect(log10(f32(0.2)) == log10_32(0.2)); + testing.expect(log10(f64(0.2)) == log10_64(0.2)); } test "math.log10_32" { const epsilon = 0.000001; - assert(math.approxEq(f32, log10_32(0.2), -0.698970, epsilon)); - assert(math.approxEq(f32, log10_32(0.8923), -0.049489, epsilon)); - assert(math.approxEq(f32, log10_32(1.5), 0.176091, epsilon)); - assert(math.approxEq(f32, log10_32(37.45), 1.573452, epsilon)); - assert(math.approxEq(f32, log10_32(89.123), 1.94999, epsilon)); - assert(math.approxEq(f32, log10_32(123123.234375), 5.09034, epsilon)); + testing.expect(math.approxEq(f32, log10_32(0.2), -0.698970, epsilon)); + testing.expect(math.approxEq(f32, log10_32(0.8923), -0.049489, epsilon)); + testing.expect(math.approxEq(f32, log10_32(1.5), 0.176091, epsilon)); + testing.expect(math.approxEq(f32, log10_32(37.45), 1.573452, epsilon)); + testing.expect(math.approxEq(f32, log10_32(89.123), 1.94999, epsilon)); + testing.expect(math.approxEq(f32, log10_32(123123.234375), 5.09034, epsilon)); } test "math.log10_64" { const epsilon = 0.000001; - assert(math.approxEq(f64, log10_64(0.2), -0.698970, epsilon)); - assert(math.approxEq(f64, log10_64(0.8923), -0.049489, epsilon)); - assert(math.approxEq(f64, log10_64(1.5), 0.176091, epsilon)); - assert(math.approxEq(f64, log10_64(37.45), 1.573452, epsilon)); - assert(math.approxEq(f64, log10_64(89.123), 1.94999, epsilon)); - assert(math.approxEq(f64, log10_64(123123.234375), 5.09034, epsilon)); + testing.expect(math.approxEq(f64, log10_64(0.2), -0.698970, epsilon)); + testing.expect(math.approxEq(f64, log10_64(0.8923), -0.049489, epsilon)); + testing.expect(math.approxEq(f64, log10_64(1.5), 0.176091, epsilon)); + testing.expect(math.approxEq(f64, log10_64(37.45), 1.573452, epsilon)); + testing.expect(math.approxEq(f64, log10_64(89.123), 1.94999, epsilon)); + testing.expect(math.approxEq(f64, log10_64(123123.234375), 5.09034, epsilon)); } test "math.log10_32.special" { - assert(math.isPositiveInf(log10_32(math.inf(f32)))); - assert(math.isNegativeInf(log10_32(0.0))); - assert(math.isNan(log10_32(-1.0))); - assert(math.isNan(log10_32(math.nan(f32)))); + testing.expect(math.isPositiveInf(log10_32(math.inf(f32)))); + testing.expect(math.isNegativeInf(log10_32(0.0))); + testing.expect(math.isNan(log10_32(-1.0))); + testing.expect(math.isNan(log10_32(math.nan(f32)))); } test "math.log10_64.special" { - assert(math.isPositiveInf(log10_64(math.inf(f64)))); - assert(math.isNegativeInf(log10_64(0.0))); - assert(math.isNan(log10_64(-1.0))); - assert(math.isNan(log10_64(math.nan(f64)))); + testing.expect(math.isPositiveInf(log10_64(math.inf(f64)))); + testing.expect(math.isNegativeInf(log10_64(0.0))); + testing.expect(math.isNan(log10_64(-1.0))); + testing.expect(math.isNan(log10_64(math.nan(f64)))); } diff --git a/std/math/log1p.zig b/std/math/log1p.zig index 903fceac05..257e7b90d4 100644 --- a/std/math/log1p.zig +++ b/std/math/log1p.zig @@ -9,7 +9,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn log1p(x: var) @typeOf(x) { const T = @typeOf(x); @@ -177,48 +177,48 @@ fn log1p_64(x: f64) f64 { } test "math.log1p" { - assert(log1p(f32(0.0)) == log1p_32(0.0)); - assert(log1p(f64(0.0)) == log1p_64(0.0)); + expect(log1p(f32(0.0)) == log1p_32(0.0)); + expect(log1p(f64(0.0)) == log1p_64(0.0)); } test "math.log1p_32" { const epsilon = 0.000001; - assert(math.approxEq(f32, log1p_32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, log1p_32(0.2), 0.182322, epsilon)); - assert(math.approxEq(f32, log1p_32(0.8923), 0.637793, epsilon)); - assert(math.approxEq(f32, log1p_32(1.5), 0.916291, epsilon)); - assert(math.approxEq(f32, log1p_32(37.45), 3.649359, epsilon)); - assert(math.approxEq(f32, log1p_32(89.123), 4.501175, epsilon)); - assert(math.approxEq(f32, log1p_32(123123.234375), 11.720949, epsilon)); + expect(math.approxEq(f32, log1p_32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, log1p_32(0.2), 0.182322, epsilon)); + expect(math.approxEq(f32, log1p_32(0.8923), 0.637793, epsilon)); + expect(math.approxEq(f32, log1p_32(1.5), 0.916291, epsilon)); + expect(math.approxEq(f32, log1p_32(37.45), 3.649359, epsilon)); + expect(math.approxEq(f32, log1p_32(89.123), 4.501175, epsilon)); + expect(math.approxEq(f32, log1p_32(123123.234375), 11.720949, epsilon)); } test "math.log1p_64" { const epsilon = 0.000001; - assert(math.approxEq(f64, log1p_64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, log1p_64(0.2), 0.182322, epsilon)); - assert(math.approxEq(f64, log1p_64(0.8923), 0.637793, epsilon)); - assert(math.approxEq(f64, log1p_64(1.5), 0.916291, epsilon)); - assert(math.approxEq(f64, log1p_64(37.45), 3.649359, epsilon)); - assert(math.approxEq(f64, log1p_64(89.123), 4.501175, epsilon)); - assert(math.approxEq(f64, log1p_64(123123.234375), 11.720949, epsilon)); + expect(math.approxEq(f64, log1p_64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, log1p_64(0.2), 0.182322, epsilon)); + expect(math.approxEq(f64, log1p_64(0.8923), 0.637793, epsilon)); + expect(math.approxEq(f64, log1p_64(1.5), 0.916291, epsilon)); + expect(math.approxEq(f64, log1p_64(37.45), 3.649359, epsilon)); + expect(math.approxEq(f64, log1p_64(89.123), 4.501175, epsilon)); + expect(math.approxEq(f64, log1p_64(123123.234375), 11.720949, epsilon)); } test "math.log1p_32.special" { - assert(math.isPositiveInf(log1p_32(math.inf(f32)))); - assert(log1p_32(0.0) == 0.0); - assert(log1p_32(-0.0) == -0.0); - assert(math.isNegativeInf(log1p_32(-1.0))); - assert(math.isNan(log1p_32(-2.0))); - assert(math.isNan(log1p_32(math.nan(f32)))); + expect(math.isPositiveInf(log1p_32(math.inf(f32)))); + expect(log1p_32(0.0) == 0.0); + expect(log1p_32(-0.0) == -0.0); + expect(math.isNegativeInf(log1p_32(-1.0))); + expect(math.isNan(log1p_32(-2.0))); + expect(math.isNan(log1p_32(math.nan(f32)))); } test "math.log1p_64.special" { - assert(math.isPositiveInf(log1p_64(math.inf(f64)))); - assert(log1p_64(0.0) == 0.0); - assert(log1p_64(-0.0) == -0.0); - assert(math.isNegativeInf(log1p_64(-1.0))); - assert(math.isNan(log1p_64(-2.0))); - assert(math.isNan(log1p_64(math.nan(f64)))); + expect(math.isPositiveInf(log1p_64(math.inf(f64)))); + expect(log1p_64(0.0) == 0.0); + expect(log1p_64(-0.0) == -0.0); + expect(math.isNegativeInf(log1p_64(-1.0))); + expect(math.isNan(log1p_64(-2.0))); + expect(math.isNan(log1p_64(math.nan(f64)))); } diff --git a/std/math/log2.zig b/std/math/log2.zig index 555c0bdf18..1bb51bf9f9 100644 --- a/std/math/log2.zig +++ b/std/math/log2.zig @@ -7,7 +7,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const builtin = @import("builtin"); const TypeId = builtin.TypeId; const maxInt = std.math.maxInt; @@ -169,40 +169,40 @@ pub fn log2_64(x_: f64) f64 { } test "math.log2" { - assert(log2(f32(0.2)) == log2_32(0.2)); - assert(log2(f64(0.2)) == log2_64(0.2)); + expect(log2(f32(0.2)) == log2_32(0.2)); + expect(log2(f64(0.2)) == log2_64(0.2)); } test "math.log2_32" { const epsilon = 0.000001; - assert(math.approxEq(f32, log2_32(0.2), -2.321928, epsilon)); - assert(math.approxEq(f32, log2_32(0.8923), -0.164399, epsilon)); - assert(math.approxEq(f32, log2_32(1.5), 0.584962, epsilon)); - assert(math.approxEq(f32, log2_32(37.45), 5.226894, epsilon)); - assert(math.approxEq(f32, log2_32(123123.234375), 16.909744, epsilon)); + expect(math.approxEq(f32, log2_32(0.2), -2.321928, epsilon)); + expect(math.approxEq(f32, log2_32(0.8923), -0.164399, epsilon)); + expect(math.approxEq(f32, log2_32(1.5), 0.584962, epsilon)); + expect(math.approxEq(f32, log2_32(37.45), 5.226894, epsilon)); + expect(math.approxEq(f32, log2_32(123123.234375), 16.909744, epsilon)); } test "math.log2_64" { const epsilon = 0.000001; - assert(math.approxEq(f64, log2_64(0.2), -2.321928, epsilon)); - assert(math.approxEq(f64, log2_64(0.8923), -0.164399, epsilon)); - assert(math.approxEq(f64, log2_64(1.5), 0.584962, epsilon)); - assert(math.approxEq(f64, log2_64(37.45), 5.226894, epsilon)); - assert(math.approxEq(f64, log2_64(123123.234375), 16.909744, epsilon)); + expect(math.approxEq(f64, log2_64(0.2), -2.321928, epsilon)); + expect(math.approxEq(f64, log2_64(0.8923), -0.164399, epsilon)); + expect(math.approxEq(f64, log2_64(1.5), 0.584962, epsilon)); + expect(math.approxEq(f64, log2_64(37.45), 5.226894, epsilon)); + expect(math.approxEq(f64, log2_64(123123.234375), 16.909744, epsilon)); } test "math.log2_32.special" { - assert(math.isPositiveInf(log2_32(math.inf(f32)))); - assert(math.isNegativeInf(log2_32(0.0))); - assert(math.isNan(log2_32(-1.0))); - assert(math.isNan(log2_32(math.nan(f32)))); + expect(math.isPositiveInf(log2_32(math.inf(f32)))); + expect(math.isNegativeInf(log2_32(0.0))); + expect(math.isNan(log2_32(-1.0))); + expect(math.isNan(log2_32(math.nan(f32)))); } test "math.log2_64.special" { - assert(math.isPositiveInf(log2_64(math.inf(f64)))); - assert(math.isNegativeInf(log2_64(0.0))); - assert(math.isNan(log2_64(-1.0))); - assert(math.isNan(log2_64(math.nan(f64)))); + expect(math.isPositiveInf(log2_64(math.inf(f64)))); + expect(math.isNegativeInf(log2_64(0.0))); + expect(math.isNan(log2_64(-1.0))); + expect(math.isNan(log2_64(math.nan(f64)))); } diff --git a/std/math/modf.zig b/std/math/modf.zig index 0f619f25bc..2dadda76a9 100644 --- a/std/math/modf.zig +++ b/std/math/modf.zig @@ -5,7 +5,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; fn modf_result(comptime T: type) type { @@ -119,11 +119,11 @@ test "math.modf" { const a = modf(f32(1.0)); const b = modf32(1.0); // NOTE: No struct comparison on generic return type function? non-named, makes sense, but still. - assert(a.ipart == b.ipart and a.fpart == b.fpart); + expect(a.ipart == b.ipart and a.fpart == b.fpart); const c = modf(f64(1.0)); const d = modf64(1.0); - assert(a.ipart == b.ipart and a.fpart == b.fpart); + expect(a.ipart == b.ipart and a.fpart == b.fpart); } test "math.modf32" { @@ -131,24 +131,24 @@ test "math.modf32" { var r: modf32_result = undefined; r = modf32(1.0); - assert(math.approxEq(f32, r.ipart, 1.0, epsilon)); - assert(math.approxEq(f32, r.fpart, 0.0, epsilon)); + expect(math.approxEq(f32, r.ipart, 1.0, epsilon)); + expect(math.approxEq(f32, r.fpart, 0.0, epsilon)); r = modf32(2.545); - assert(math.approxEq(f32, r.ipart, 2.0, epsilon)); - assert(math.approxEq(f32, r.fpart, 0.545, epsilon)); + expect(math.approxEq(f32, r.ipart, 2.0, epsilon)); + expect(math.approxEq(f32, r.fpart, 0.545, epsilon)); r = modf32(3.978123); - assert(math.approxEq(f32, r.ipart, 3.0, epsilon)); - assert(math.approxEq(f32, r.fpart, 0.978123, epsilon)); + expect(math.approxEq(f32, r.ipart, 3.0, epsilon)); + expect(math.approxEq(f32, r.fpart, 0.978123, epsilon)); r = modf32(43874.3); - assert(math.approxEq(f32, r.ipart, 43874, epsilon)); - assert(math.approxEq(f32, r.fpart, 0.300781, epsilon)); + expect(math.approxEq(f32, r.ipart, 43874, epsilon)); + expect(math.approxEq(f32, r.fpart, 0.300781, epsilon)); r = modf32(1234.340780); - assert(math.approxEq(f32, r.ipart, 1234, epsilon)); - assert(math.approxEq(f32, r.fpart, 0.340820, epsilon)); + expect(math.approxEq(f32, r.ipart, 1234, epsilon)); + expect(math.approxEq(f32, r.fpart, 0.340820, epsilon)); } test "math.modf64" { @@ -156,48 +156,48 @@ test "math.modf64" { var r: modf64_result = undefined; r = modf64(1.0); - assert(math.approxEq(f64, r.ipart, 1.0, epsilon)); - assert(math.approxEq(f64, r.fpart, 0.0, epsilon)); + expect(math.approxEq(f64, r.ipart, 1.0, epsilon)); + expect(math.approxEq(f64, r.fpart, 0.0, epsilon)); r = modf64(2.545); - assert(math.approxEq(f64, r.ipart, 2.0, epsilon)); - assert(math.approxEq(f64, r.fpart, 0.545, epsilon)); + expect(math.approxEq(f64, r.ipart, 2.0, epsilon)); + expect(math.approxEq(f64, r.fpart, 0.545, epsilon)); r = modf64(3.978123); - assert(math.approxEq(f64, r.ipart, 3.0, epsilon)); - assert(math.approxEq(f64, r.fpart, 0.978123, epsilon)); + expect(math.approxEq(f64, r.ipart, 3.0, epsilon)); + expect(math.approxEq(f64, r.fpart, 0.978123, epsilon)); r = modf64(43874.3); - assert(math.approxEq(f64, r.ipart, 43874, epsilon)); - assert(math.approxEq(f64, r.fpart, 0.3, epsilon)); + expect(math.approxEq(f64, r.ipart, 43874, epsilon)); + expect(math.approxEq(f64, r.fpart, 0.3, epsilon)); r = modf64(1234.340780); - assert(math.approxEq(f64, r.ipart, 1234, epsilon)); - assert(math.approxEq(f64, r.fpart, 0.340780, epsilon)); + expect(math.approxEq(f64, r.ipart, 1234, epsilon)); + expect(math.approxEq(f64, r.fpart, 0.340780, epsilon)); } test "math.modf32.special" { var r: modf32_result = undefined; r = modf32(math.inf(f32)); - assert(math.isPositiveInf(r.ipart) and math.isNan(r.fpart)); + expect(math.isPositiveInf(r.ipart) and math.isNan(r.fpart)); r = modf32(-math.inf(f32)); - assert(math.isNegativeInf(r.ipart) and math.isNan(r.fpart)); + expect(math.isNegativeInf(r.ipart) and math.isNan(r.fpart)); r = modf32(math.nan(f32)); - assert(math.isNan(r.ipart) and math.isNan(r.fpart)); + expect(math.isNan(r.ipart) and math.isNan(r.fpart)); } test "math.modf64.special" { var r: modf64_result = undefined; r = modf64(math.inf(f64)); - assert(math.isPositiveInf(r.ipart) and math.isNan(r.fpart)); + expect(math.isPositiveInf(r.ipart) and math.isNan(r.fpart)); r = modf64(-math.inf(f64)); - assert(math.isNegativeInf(r.ipart) and math.isNan(r.fpart)); + expect(math.isNegativeInf(r.ipart) and math.isNan(r.fpart)); r = modf64(math.nan(f64)); - assert(math.isNan(r.ipart) and math.isNan(r.fpart)); + expect(math.isNan(r.ipart) and math.isNan(r.fpart)); } diff --git a/std/math/pow.zig b/std/math/pow.zig index 39a2bfa9f7..f037f66d7e 100644 --- a/std/math/pow.zig +++ b/std/math/pow.zig @@ -24,7 +24,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; // This implementation is taken from the go stlib, musl is a bit more complex. pub fn pow(comptime T: type, x: T, y: T) T { @@ -179,56 +179,56 @@ fn isOddInteger(x: f64) bool { test "math.pow" { const epsilon = 0.000001; - assert(math.approxEq(f32, pow(f32, 0.0, 3.3), 0.0, epsilon)); - assert(math.approxEq(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon)); - assert(math.approxEq(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon)); - assert(math.approxEq(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon)); - assert(math.approxEq(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon)); - assert(math.approxEq(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon)); - - assert(math.approxEq(f64, pow(f64, 0.0, 3.3), 0.0, epsilon)); - assert(math.approxEq(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon)); - assert(math.approxEq(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon)); - assert(math.approxEq(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon)); - assert(math.approxEq(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon)); - assert(math.approxEq(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon)); + expect(math.approxEq(f32, pow(f32, 0.0, 3.3), 0.0, epsilon)); + expect(math.approxEq(f32, pow(f32, 0.8923, 3.3), 0.686572, epsilon)); + expect(math.approxEq(f32, pow(f32, 0.2, 3.3), 0.004936, epsilon)); + expect(math.approxEq(f32, pow(f32, 1.5, 3.3), 3.811546, epsilon)); + expect(math.approxEq(f32, pow(f32, 37.45, 3.3), 155736.703125, epsilon)); + expect(math.approxEq(f32, pow(f32, 89.123, 3.3), 2722489.5, epsilon)); + + expect(math.approxEq(f64, pow(f64, 0.0, 3.3), 0.0, epsilon)); + expect(math.approxEq(f64, pow(f64, 0.8923, 3.3), 0.686572, epsilon)); + expect(math.approxEq(f64, pow(f64, 0.2, 3.3), 0.004936, epsilon)); + expect(math.approxEq(f64, pow(f64, 1.5, 3.3), 3.811546, epsilon)); + expect(math.approxEq(f64, pow(f64, 37.45, 3.3), 155736.7160616, epsilon)); + expect(math.approxEq(f64, pow(f64, 89.123, 3.3), 2722490.231436, epsilon)); } test "math.pow.special" { const epsilon = 0.000001; - assert(pow(f32, 4, 0.0) == 1.0); - assert(pow(f32, 7, -0.0) == 1.0); - assert(pow(f32, 45, 1.0) == 45); - assert(pow(f32, -45, 1.0) == -45); - assert(math.isNan(pow(f32, math.nan(f32), 5.0))); - assert(math.isNan(pow(f32, 5.0, math.nan(f32)))); - assert(math.isPositiveInf(pow(f32, 0.0, -1.0))); - //assert(math.isNegativeInf(pow(f32, -0.0, -3.0))); TODO is this required? - assert(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32)))); - assert(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32)))); - assert(pow(f32, 0.0, math.inf(f32)) == 0.0); - assert(pow(f32, -0.0, math.inf(f32)) == 0.0); - assert(math.isPositiveInf(pow(f32, 0.0, -2.0))); - assert(math.isPositiveInf(pow(f32, -0.0, -2.0))); - assert(pow(f32, 0.0, 1.0) == 0.0); - assert(pow(f32, -0.0, 1.0) == -0.0); - assert(pow(f32, 0.0, 2.0) == 0.0); - assert(pow(f32, -0.0, 2.0) == 0.0); - assert(math.approxEq(f32, pow(f32, -1.0, math.inf(f32)), 1.0, epsilon)); - assert(math.approxEq(f32, pow(f32, -1.0, -math.inf(f32)), 1.0, epsilon)); - assert(math.isPositiveInf(pow(f32, 1.2, math.inf(f32)))); - assert(math.isPositiveInf(pow(f32, -1.2, math.inf(f32)))); - assert(pow(f32, 1.2, -math.inf(f32)) == 0.0); - assert(pow(f32, -1.2, -math.inf(f32)) == 0.0); - assert(pow(f32, 0.2, math.inf(f32)) == 0.0); - assert(pow(f32, -0.2, math.inf(f32)) == 0.0); - assert(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32)))); - assert(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32)))); - assert(math.isPositiveInf(pow(f32, math.inf(f32), 1.0))); - assert(pow(f32, math.inf(f32), -1.0) == 0.0); - //assert(pow(f32, -math.inf(f32), 5.0) == pow(f32, -0.0, -5.0)); TODO support negative 0? - assert(pow(f32, -math.inf(f32), -5.2) == pow(f32, -0.0, 5.2)); - assert(math.isNan(pow(f32, -1.0, 1.2))); - assert(math.isNan(pow(f32, -12.4, 78.5))); + expect(pow(f32, 4, 0.0) == 1.0); + expect(pow(f32, 7, -0.0) == 1.0); + expect(pow(f32, 45, 1.0) == 45); + expect(pow(f32, -45, 1.0) == -45); + expect(math.isNan(pow(f32, math.nan(f32), 5.0))); + expect(math.isNan(pow(f32, 5.0, math.nan(f32)))); + expect(math.isPositiveInf(pow(f32, 0.0, -1.0))); + //expect(math.isNegativeInf(pow(f32, -0.0, -3.0))); TODO is this required? + expect(math.isPositiveInf(pow(f32, 0.0, -math.inf(f32)))); + expect(math.isPositiveInf(pow(f32, -0.0, -math.inf(f32)))); + expect(pow(f32, 0.0, math.inf(f32)) == 0.0); + expect(pow(f32, -0.0, math.inf(f32)) == 0.0); + expect(math.isPositiveInf(pow(f32, 0.0, -2.0))); + expect(math.isPositiveInf(pow(f32, -0.0, -2.0))); + expect(pow(f32, 0.0, 1.0) == 0.0); + expect(pow(f32, -0.0, 1.0) == -0.0); + expect(pow(f32, 0.0, 2.0) == 0.0); + expect(pow(f32, -0.0, 2.0) == 0.0); + expect(math.approxEq(f32, pow(f32, -1.0, math.inf(f32)), 1.0, epsilon)); + expect(math.approxEq(f32, pow(f32, -1.0, -math.inf(f32)), 1.0, epsilon)); + expect(math.isPositiveInf(pow(f32, 1.2, math.inf(f32)))); + expect(math.isPositiveInf(pow(f32, -1.2, math.inf(f32)))); + expect(pow(f32, 1.2, -math.inf(f32)) == 0.0); + expect(pow(f32, -1.2, -math.inf(f32)) == 0.0); + expect(pow(f32, 0.2, math.inf(f32)) == 0.0); + expect(pow(f32, -0.2, math.inf(f32)) == 0.0); + expect(math.isPositiveInf(pow(f32, 0.2, -math.inf(f32)))); + expect(math.isPositiveInf(pow(f32, -0.2, -math.inf(f32)))); + expect(math.isPositiveInf(pow(f32, math.inf(f32), 1.0))); + expect(pow(f32, math.inf(f32), -1.0) == 0.0); + //expect(pow(f32, -math.inf(f32), 5.0) == pow(f32, -0.0, -5.0)); TODO support negative 0? + expect(pow(f32, -math.inf(f32), -5.2) == pow(f32, -0.0, 5.2)); + expect(math.isNan(pow(f32, -1.0, 1.2))); + expect(math.isNan(pow(f32, -12.4, 78.5))); } diff --git a/std/math/powi.zig b/std/math/powi.zig index 13c09b192e..9c2a4a4965 100644 --- a/std/math/powi.zig +++ b/std/math/powi.zig @@ -12,7 +12,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; const assert = std.debug.assert; -const assertError = std.debug.assertError; +const testing = std.testing; // This implementation is based on that from the rust stlib pub fn powi(comptime T: type, x: T, y: T) (error{ @@ -103,75 +103,75 @@ pub fn powi(comptime T: type, x: T, y: T) (error{ } test "math.powi" { - assertError(powi(i8, -66, 6), error.Underflow); - assertError(powi(i16, -13, 13), error.Underflow); - assertError(powi(i32, -32, 21), error.Underflow); - assertError(powi(i64, -24, 61), error.Underflow); - assertError(powi(i17, -15, 15), error.Underflow); - assertError(powi(i42, -6, 40), error.Underflow); - - assert((try powi(i8, -5, 3)) == -125); - assert((try powi(i16, -16, 3)) == -4096); - assert((try powi(i32, -91, 3)) == -753571); - assert((try powi(i64, -36, 6)) == 2176782336); - assert((try powi(i17, -2, 15)) == -32768); - assert((try powi(i42, -5, 7)) == -78125); - - assert((try powi(u8, 6, 2)) == 36); - assert((try powi(u16, 5, 4)) == 625); - assert((try powi(u32, 12, 6)) == 2985984); - assert((try powi(u64, 34, 2)) == 1156); - assert((try powi(u17, 16, 3)) == 4096); - assert((try powi(u42, 34, 6)) == 1544804416); - - assertError(powi(i8, 120, 7), error.Overflow); - assertError(powi(i16, 73, 15), error.Overflow); - assertError(powi(i32, 23, 31), error.Overflow); - assertError(powi(i64, 68, 61), error.Overflow); - assertError(powi(i17, 15, 15), error.Overflow); - assertError(powi(i42, 121312, 41), error.Overflow); - - assertError(powi(u8, 123, 7), error.Overflow); - assertError(powi(u16, 2313, 15), error.Overflow); - assertError(powi(u32, 8968, 31), error.Overflow); - assertError(powi(u64, 2342, 63), error.Overflow); - assertError(powi(u17, 2723, 16), error.Overflow); - assertError(powi(u42, 8234, 41), error.Overflow); + testing.expectError(error.Underflow, powi(i8, -66, 6)); + testing.expectError(error.Underflow, powi(i16, -13, 13)); + testing.expectError(error.Underflow, powi(i32, -32, 21)); + testing.expectError(error.Underflow, powi(i64, -24, 61)); + testing.expectError(error.Underflow, powi(i17, -15, 15)); + testing.expectError(error.Underflow, powi(i42, -6, 40)); + + testing.expect((try powi(i8, -5, 3)) == -125); + testing.expect((try powi(i16, -16, 3)) == -4096); + testing.expect((try powi(i32, -91, 3)) == -753571); + testing.expect((try powi(i64, -36, 6)) == 2176782336); + testing.expect((try powi(i17, -2, 15)) == -32768); + testing.expect((try powi(i42, -5, 7)) == -78125); + + testing.expect((try powi(u8, 6, 2)) == 36); + testing.expect((try powi(u16, 5, 4)) == 625); + testing.expect((try powi(u32, 12, 6)) == 2985984); + testing.expect((try powi(u64, 34, 2)) == 1156); + testing.expect((try powi(u17, 16, 3)) == 4096); + testing.expect((try powi(u42, 34, 6)) == 1544804416); + + testing.expectError(error.Overflow, powi(i8, 120, 7)); + testing.expectError(error.Overflow, powi(i16, 73, 15)); + testing.expectError(error.Overflow, powi(i32, 23, 31)); + testing.expectError(error.Overflow, powi(i64, 68, 61)); + testing.expectError(error.Overflow, powi(i17, 15, 15)); + testing.expectError(error.Overflow, powi(i42, 121312, 41)); + + testing.expectError(error.Overflow, powi(u8, 123, 7)); + testing.expectError(error.Overflow, powi(u16, 2313, 15)); + testing.expectError(error.Overflow, powi(u32, 8968, 31)); + testing.expectError(error.Overflow, powi(u64, 2342, 63)); + testing.expectError(error.Overflow, powi(u17, 2723, 16)); + testing.expectError(error.Overflow, powi(u42, 8234, 41)); } test "math.powi.special" { - assertError(powi(i8, -2, 8), error.Underflow); - assertError(powi(i16, -2, 16), error.Underflow); - assertError(powi(i32, -2, 32), error.Underflow); - assertError(powi(i64, -2, 64), error.Underflow); - assertError(powi(i17, -2, 17), error.Underflow); - assertError(powi(i42, -2, 42), error.Underflow); - - assert((try powi(i8, -1, 3)) == -1); - assert((try powi(i16, -1, 2)) == 1); - assert((try powi(i32, -1, 16)) == 1); - assert((try powi(i64, -1, 6)) == 1); - assert((try powi(i17, -1, 15)) == -1); - assert((try powi(i42, -1, 7)) == -1); - - assert((try powi(u8, 1, 2)) == 1); - assert((try powi(u16, 1, 4)) == 1); - assert((try powi(u32, 1, 6)) == 1); - assert((try powi(u64, 1, 2)) == 1); - assert((try powi(u17, 1, 3)) == 1); - assert((try powi(u42, 1, 6)) == 1); - - assertError(powi(i8, 2, 7), error.Overflow); - assertError(powi(i16, 2, 15), error.Overflow); - assertError(powi(i32, 2, 31), error.Overflow); - assertError(powi(i64, 2, 63), error.Overflow); - assertError(powi(i17, 2, 16), error.Overflow); - assertError(powi(i42, 2, 41), error.Overflow); - - assertError(powi(u8, 2, 8), error.Overflow); - assertError(powi(u16, 2, 16), error.Overflow); - assertError(powi(u32, 2, 32), error.Overflow); - assertError(powi(u64, 2, 64), error.Overflow); - assertError(powi(u17, 2, 17), error.Overflow); - assertError(powi(u42, 2, 42), error.Overflow); + testing.expectError(error.Underflow, powi(i8, -2, 8)); + testing.expectError(error.Underflow, powi(i16, -2, 16)); + testing.expectError(error.Underflow, powi(i32, -2, 32)); + testing.expectError(error.Underflow, powi(i64, -2, 64)); + testing.expectError(error.Underflow, powi(i17, -2, 17)); + testing.expectError(error.Underflow, powi(i42, -2, 42)); + + testing.expect((try powi(i8, -1, 3)) == -1); + testing.expect((try powi(i16, -1, 2)) == 1); + testing.expect((try powi(i32, -1, 16)) == 1); + testing.expect((try powi(i64, -1, 6)) == 1); + testing.expect((try powi(i17, -1, 15)) == -1); + testing.expect((try powi(i42, -1, 7)) == -1); + + testing.expect((try powi(u8, 1, 2)) == 1); + testing.expect((try powi(u16, 1, 4)) == 1); + testing.expect((try powi(u32, 1, 6)) == 1); + testing.expect((try powi(u64, 1, 2)) == 1); + testing.expect((try powi(u17, 1, 3)) == 1); + testing.expect((try powi(u42, 1, 6)) == 1); + + testing.expectError(error.Overflow, powi(i8, 2, 7)); + testing.expectError(error.Overflow, powi(i16, 2, 15)); + testing.expectError(error.Overflow, powi(i32, 2, 31)); + testing.expectError(error.Overflow, powi(i64, 2, 63)); + testing.expectError(error.Overflow, powi(i17, 2, 16)); + testing.expectError(error.Overflow, powi(i42, 2, 41)); + + testing.expectError(error.Overflow, powi(u8, 2, 8)); + testing.expectError(error.Overflow, powi(u16, 2, 16)); + testing.expectError(error.Overflow, powi(u32, 2, 32)); + testing.expectError(error.Overflow, powi(u64, 2, 64)); + testing.expectError(error.Overflow, powi(u17, 2, 17)); + testing.expectError(error.Overflow, powi(u42, 2, 42)); } diff --git a/std/math/round.zig b/std/math/round.zig index 4fe35365c8..7346b703c9 100644 --- a/std/math/round.zig +++ b/std/math/round.zig @@ -5,7 +5,7 @@ // - round(nan) = nan const builtin = @import("builtin"); -const assert = std.debug.assert; +const expect = std.testing.expect; const std = @import("../index.zig"); const math = std.math; @@ -85,36 +85,36 @@ fn round64(x_: f64) f64 { } test "math.round" { - assert(round(f32(1.3)) == round32(1.3)); - assert(round(f64(1.3)) == round64(1.3)); + expect(round(f32(1.3)) == round32(1.3)); + expect(round(f64(1.3)) == round64(1.3)); } test "math.round32" { - assert(round32(1.3) == 1.0); - assert(round32(-1.3) == -1.0); - assert(round32(0.2) == 0.0); - assert(round32(1.8) == 2.0); + expect(round32(1.3) == 1.0); + expect(round32(-1.3) == -1.0); + expect(round32(0.2) == 0.0); + expect(round32(1.8) == 2.0); } test "math.round64" { - assert(round64(1.3) == 1.0); - assert(round64(-1.3) == -1.0); - assert(round64(0.2) == 0.0); - assert(round64(1.8) == 2.0); + expect(round64(1.3) == 1.0); + expect(round64(-1.3) == -1.0); + expect(round64(0.2) == 0.0); + expect(round64(1.8) == 2.0); } test "math.round32.special" { - assert(round32(0.0) == 0.0); - assert(round32(-0.0) == -0.0); - assert(math.isPositiveInf(round32(math.inf(f32)))); - assert(math.isNegativeInf(round32(-math.inf(f32)))); - assert(math.isNan(round32(math.nan(f32)))); + expect(round32(0.0) == 0.0); + expect(round32(-0.0) == -0.0); + expect(math.isPositiveInf(round32(math.inf(f32)))); + expect(math.isNegativeInf(round32(-math.inf(f32)))); + expect(math.isNan(round32(math.nan(f32)))); } test "math.round64.special" { - assert(round64(0.0) == 0.0); - assert(round64(-0.0) == -0.0); - assert(math.isPositiveInf(round64(math.inf(f64)))); - assert(math.isNegativeInf(round64(-math.inf(f64)))); - assert(math.isNan(round64(math.nan(f64)))); + expect(round64(0.0) == 0.0); + expect(round64(-0.0) == -0.0); + expect(math.isPositiveInf(round64(math.inf(f64)))); + expect(math.isNegativeInf(round64(-math.inf(f64)))); + expect(math.isNan(round64(math.nan(f64)))); } diff --git a/std/math/scalbn.zig b/std/math/scalbn.zig index f72c7e866f..d37a8659a9 100644 --- a/std/math/scalbn.zig +++ b/std/math/scalbn.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn scalbn(x: var, n: i32) @typeOf(x) { const T = @typeOf(x); @@ -72,14 +72,14 @@ fn scalbn64(x: f64, n_: i32) f64 { } test "math.scalbn" { - assert(scalbn(f32(1.5), 4) == scalbn32(1.5, 4)); - assert(scalbn(f64(1.5), 4) == scalbn64(1.5, 4)); + expect(scalbn(f32(1.5), 4) == scalbn32(1.5, 4)); + expect(scalbn(f64(1.5), 4) == scalbn64(1.5, 4)); } test "math.scalbn32" { - assert(scalbn32(1.5, 4) == 24.0); + expect(scalbn32(1.5, 4) == 24.0); } test "math.scalbn64" { - assert(scalbn64(1.5, 4) == 24.0); + expect(scalbn64(1.5, 4) == 24.0); } diff --git a/std/math/signbit.zig b/std/math/signbit.zig index 8c6829dfcd..728f651aec 100644 --- a/std/math/signbit.zig +++ b/std/math/signbit.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn signbit(x: var) bool { const T = @typeOf(x); @@ -28,22 +28,22 @@ fn signbit64(x: f64) bool { } test "math.signbit" { - assert(signbit(f16(4.0)) == signbit16(4.0)); - assert(signbit(f32(4.0)) == signbit32(4.0)); - assert(signbit(f64(4.0)) == signbit64(4.0)); + expect(signbit(f16(4.0)) == signbit16(4.0)); + expect(signbit(f32(4.0)) == signbit32(4.0)); + expect(signbit(f64(4.0)) == signbit64(4.0)); } test "math.signbit16" { - assert(!signbit16(4.0)); - assert(signbit16(-3.0)); + expect(!signbit16(4.0)); + expect(signbit16(-3.0)); } test "math.signbit32" { - assert(!signbit32(4.0)); - assert(signbit32(-3.0)); + expect(!signbit32(4.0)); + expect(signbit32(-3.0)); } test "math.signbit64" { - assert(!signbit64(4.0)); - assert(signbit64(-3.0)); + expect(!signbit64(4.0)); + expect(signbit64(-3.0)); } diff --git a/std/math/sin.zig b/std/math/sin.zig index 15b2f9f17a..5ade6636c7 100644 --- a/std/math/sin.zig +++ b/std/math/sin.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn sin(x: var) @typeOf(x) { const T = @typeOf(x); @@ -142,45 +142,45 @@ fn sin64(x_: f64) f64 { } test "math.sin" { - assert(sin(f32(0.0)) == sin32(0.0)); - assert(sin(f64(0.0)) == sin64(0.0)); - assert(comptime (math.sin(f64(2))) == math.sin(f64(2))); + expect(sin(f32(0.0)) == sin32(0.0)); + expect(sin(f64(0.0)) == sin64(0.0)); + expect(comptime (math.sin(f64(2))) == math.sin(f64(2))); } test "math.sin32" { const epsilon = 0.000001; - assert(math.approxEq(f32, sin32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, sin32(0.2), 0.198669, epsilon)); - assert(math.approxEq(f32, sin32(0.8923), 0.778517, epsilon)); - assert(math.approxEq(f32, sin32(1.5), 0.997495, epsilon)); - assert(math.approxEq(f32, sin32(37.45), -0.246544, epsilon)); - assert(math.approxEq(f32, sin32(89.123), 0.916166, epsilon)); + expect(math.approxEq(f32, sin32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, sin32(0.2), 0.198669, epsilon)); + expect(math.approxEq(f32, sin32(0.8923), 0.778517, epsilon)); + expect(math.approxEq(f32, sin32(1.5), 0.997495, epsilon)); + expect(math.approxEq(f32, sin32(37.45), -0.246544, epsilon)); + expect(math.approxEq(f32, sin32(89.123), 0.916166, epsilon)); } test "math.sin64" { const epsilon = 0.000001; - assert(math.approxEq(f64, sin64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, sin64(0.2), 0.198669, epsilon)); - assert(math.approxEq(f64, sin64(0.8923), 0.778517, epsilon)); - assert(math.approxEq(f64, sin64(1.5), 0.997495, epsilon)); - assert(math.approxEq(f64, sin64(37.45), -0.246543, epsilon)); - assert(math.approxEq(f64, sin64(89.123), 0.916166, epsilon)); + expect(math.approxEq(f64, sin64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, sin64(0.2), 0.198669, epsilon)); + expect(math.approxEq(f64, sin64(0.8923), 0.778517, epsilon)); + expect(math.approxEq(f64, sin64(1.5), 0.997495, epsilon)); + expect(math.approxEq(f64, sin64(37.45), -0.246543, epsilon)); + expect(math.approxEq(f64, sin64(89.123), 0.916166, epsilon)); } test "math.sin32.special" { - assert(sin32(0.0) == 0.0); - assert(sin32(-0.0) == -0.0); - assert(math.isNan(sin32(math.inf(f32)))); - assert(math.isNan(sin32(-math.inf(f32)))); - assert(math.isNan(sin32(math.nan(f32)))); + expect(sin32(0.0) == 0.0); + expect(sin32(-0.0) == -0.0); + expect(math.isNan(sin32(math.inf(f32)))); + expect(math.isNan(sin32(-math.inf(f32)))); + expect(math.isNan(sin32(math.nan(f32)))); } test "math.sin64.special" { - assert(sin64(0.0) == 0.0); - assert(sin64(-0.0) == -0.0); - assert(math.isNan(sin64(math.inf(f64)))); - assert(math.isNan(sin64(-math.inf(f64)))); - assert(math.isNan(sin64(math.nan(f64)))); + expect(sin64(0.0) == 0.0); + expect(sin64(-0.0) == -0.0); + expect(math.isNan(sin64(math.inf(f64)))); + expect(math.isNan(sin64(-math.inf(f64)))); + expect(math.isNan(sin64(math.nan(f64)))); } diff --git a/std/math/sinh.zig b/std/math/sinh.zig index 733b89754a..95924ba55a 100644 --- a/std/math/sinh.zig +++ b/std/math/sinh.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const expo2 = @import("expo2.zig").expo2; const maxInt = std.math.maxInt; @@ -87,40 +87,40 @@ fn sinh64(x: f64) f64 { } test "math.sinh" { - assert(sinh(f32(1.5)) == sinh32(1.5)); - assert(sinh(f64(1.5)) == sinh64(1.5)); + expect(sinh(f32(1.5)) == sinh32(1.5)); + expect(sinh(f64(1.5)) == sinh64(1.5)); } test "math.sinh32" { const epsilon = 0.000001; - assert(math.approxEq(f32, sinh32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, sinh32(0.2), 0.201336, epsilon)); - assert(math.approxEq(f32, sinh32(0.8923), 1.015512, epsilon)); - assert(math.approxEq(f32, sinh32(1.5), 2.129279, epsilon)); + expect(math.approxEq(f32, sinh32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, sinh32(0.2), 0.201336, epsilon)); + expect(math.approxEq(f32, sinh32(0.8923), 1.015512, epsilon)); + expect(math.approxEq(f32, sinh32(1.5), 2.129279, epsilon)); } test "math.sinh64" { const epsilon = 0.000001; - assert(math.approxEq(f64, sinh64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, sinh64(0.2), 0.201336, epsilon)); - assert(math.approxEq(f64, sinh64(0.8923), 1.015512, epsilon)); - assert(math.approxEq(f64, sinh64(1.5), 2.129279, epsilon)); + expect(math.approxEq(f64, sinh64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, sinh64(0.2), 0.201336, epsilon)); + expect(math.approxEq(f64, sinh64(0.8923), 1.015512, epsilon)); + expect(math.approxEq(f64, sinh64(1.5), 2.129279, epsilon)); } test "math.sinh32.special" { - assert(sinh32(0.0) == 0.0); - assert(sinh32(-0.0) == -0.0); - assert(math.isPositiveInf(sinh32(math.inf(f32)))); - assert(math.isNegativeInf(sinh32(-math.inf(f32)))); - assert(math.isNan(sinh32(math.nan(f32)))); + expect(sinh32(0.0) == 0.0); + expect(sinh32(-0.0) == -0.0); + expect(math.isPositiveInf(sinh32(math.inf(f32)))); + expect(math.isNegativeInf(sinh32(-math.inf(f32)))); + expect(math.isNan(sinh32(math.nan(f32)))); } test "math.sinh64.special" { - assert(sinh64(0.0) == 0.0); - assert(sinh64(-0.0) == -0.0); - assert(math.isPositiveInf(sinh64(math.inf(f64)))); - assert(math.isNegativeInf(sinh64(-math.inf(f64)))); - assert(math.isNan(sinh64(math.nan(f64)))); + expect(sinh64(0.0) == 0.0); + expect(sinh64(-0.0) == -0.0); + expect(math.isPositiveInf(sinh64(math.inf(f64)))); + expect(math.isNegativeInf(sinh64(-math.inf(f64)))); + expect(math.isNan(sinh64(math.nan(f64)))); } diff --git a/std/math/sqrt.zig b/std/math/sqrt.zig index 4300f20f5a..9996b44b20 100644 --- a/std/math/sqrt.zig +++ b/std/math/sqrt.zig @@ -7,7 +7,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const builtin = @import("builtin"); const TypeId = builtin.TypeId; const maxInt = std.math.maxInt; @@ -32,75 +32,75 @@ pub fn sqrt(x: var) (if (@typeId(@typeOf(x)) == TypeId.Int) @IntType(false, @typ } test "math.sqrt" { - assert(sqrt(f16(0.0)) == @sqrt(f16, 0.0)); - assert(sqrt(f32(0.0)) == @sqrt(f32, 0.0)); - assert(sqrt(f64(0.0)) == @sqrt(f64, 0.0)); + expect(sqrt(f16(0.0)) == @sqrt(f16, 0.0)); + expect(sqrt(f32(0.0)) == @sqrt(f32, 0.0)); + expect(sqrt(f64(0.0)) == @sqrt(f64, 0.0)); } test "math.sqrt16" { const epsilon = 0.000001; - assert(@sqrt(f16, 0.0) == 0.0); - assert(math.approxEq(f16, @sqrt(f16, 2.0), 1.414214, epsilon)); - assert(math.approxEq(f16, @sqrt(f16, 3.6), 1.897367, epsilon)); - assert(@sqrt(f16, 4.0) == 2.0); - assert(math.approxEq(f16, @sqrt(f16, 7.539840), 2.745877, epsilon)); - assert(math.approxEq(f16, @sqrt(f16, 19.230934), 4.385309, epsilon)); - assert(@sqrt(f16, 64.0) == 8.0); - assert(math.approxEq(f16, @sqrt(f16, 64.1), 8.006248, epsilon)); - assert(math.approxEq(f16, @sqrt(f16, 8942.230469), 94.563370, epsilon)); + expect(@sqrt(f16, 0.0) == 0.0); + expect(math.approxEq(f16, @sqrt(f16, 2.0), 1.414214, epsilon)); + expect(math.approxEq(f16, @sqrt(f16, 3.6), 1.897367, epsilon)); + expect(@sqrt(f16, 4.0) == 2.0); + expect(math.approxEq(f16, @sqrt(f16, 7.539840), 2.745877, epsilon)); + expect(math.approxEq(f16, @sqrt(f16, 19.230934), 4.385309, epsilon)); + expect(@sqrt(f16, 64.0) == 8.0); + expect(math.approxEq(f16, @sqrt(f16, 64.1), 8.006248, epsilon)); + expect(math.approxEq(f16, @sqrt(f16, 8942.230469), 94.563370, epsilon)); } test "math.sqrt32" { const epsilon = 0.000001; - assert(@sqrt(f32, 0.0) == 0.0); - assert(math.approxEq(f32, @sqrt(f32, 2.0), 1.414214, epsilon)); - assert(math.approxEq(f32, @sqrt(f32, 3.6), 1.897367, epsilon)); - assert(@sqrt(f32, 4.0) == 2.0); - assert(math.approxEq(f32, @sqrt(f32, 7.539840), 2.745877, epsilon)); - assert(math.approxEq(f32, @sqrt(f32, 19.230934), 4.385309, epsilon)); - assert(@sqrt(f32, 64.0) == 8.0); - assert(math.approxEq(f32, @sqrt(f32, 64.1), 8.006248, epsilon)); - assert(math.approxEq(f32, @sqrt(f32, 8942.230469), 94.563370, epsilon)); + expect(@sqrt(f32, 0.0) == 0.0); + expect(math.approxEq(f32, @sqrt(f32, 2.0), 1.414214, epsilon)); + expect(math.approxEq(f32, @sqrt(f32, 3.6), 1.897367, epsilon)); + expect(@sqrt(f32, 4.0) == 2.0); + expect(math.approxEq(f32, @sqrt(f32, 7.539840), 2.745877, epsilon)); + expect(math.approxEq(f32, @sqrt(f32, 19.230934), 4.385309, epsilon)); + expect(@sqrt(f32, 64.0) == 8.0); + expect(math.approxEq(f32, @sqrt(f32, 64.1), 8.006248, epsilon)); + expect(math.approxEq(f32, @sqrt(f32, 8942.230469), 94.563370, epsilon)); } test "math.sqrt64" { const epsilon = 0.000001; - assert(@sqrt(f64, 0.0) == 0.0); - assert(math.approxEq(f64, @sqrt(f64, 2.0), 1.414214, epsilon)); - assert(math.approxEq(f64, @sqrt(f64, 3.6), 1.897367, epsilon)); - assert(@sqrt(f64, 4.0) == 2.0); - assert(math.approxEq(f64, @sqrt(f64, 7.539840), 2.745877, epsilon)); - assert(math.approxEq(f64, @sqrt(f64, 19.230934), 4.385309, epsilon)); - assert(@sqrt(f64, 64.0) == 8.0); - assert(math.approxEq(f64, @sqrt(f64, 64.1), 8.006248, epsilon)); - assert(math.approxEq(f64, @sqrt(f64, 8942.230469), 94.563367, epsilon)); + expect(@sqrt(f64, 0.0) == 0.0); + expect(math.approxEq(f64, @sqrt(f64, 2.0), 1.414214, epsilon)); + expect(math.approxEq(f64, @sqrt(f64, 3.6), 1.897367, epsilon)); + expect(@sqrt(f64, 4.0) == 2.0); + expect(math.approxEq(f64, @sqrt(f64, 7.539840), 2.745877, epsilon)); + expect(math.approxEq(f64, @sqrt(f64, 19.230934), 4.385309, epsilon)); + expect(@sqrt(f64, 64.0) == 8.0); + expect(math.approxEq(f64, @sqrt(f64, 64.1), 8.006248, epsilon)); + expect(math.approxEq(f64, @sqrt(f64, 8942.230469), 94.563367, epsilon)); } test "math.sqrt16.special" { - assert(math.isPositiveInf(@sqrt(f16, math.inf(f16)))); - assert(@sqrt(f16, 0.0) == 0.0); - assert(@sqrt(f16, -0.0) == -0.0); - assert(math.isNan(@sqrt(f16, -1.0))); - assert(math.isNan(@sqrt(f16, math.nan(f16)))); + expect(math.isPositiveInf(@sqrt(f16, math.inf(f16)))); + expect(@sqrt(f16, 0.0) == 0.0); + expect(@sqrt(f16, -0.0) == -0.0); + expect(math.isNan(@sqrt(f16, -1.0))); + expect(math.isNan(@sqrt(f16, math.nan(f16)))); } test "math.sqrt32.special" { - assert(math.isPositiveInf(@sqrt(f32, math.inf(f32)))); - assert(@sqrt(f32, 0.0) == 0.0); - assert(@sqrt(f32, -0.0) == -0.0); - assert(math.isNan(@sqrt(f32, -1.0))); - assert(math.isNan(@sqrt(f32, math.nan(f32)))); + expect(math.isPositiveInf(@sqrt(f32, math.inf(f32)))); + expect(@sqrt(f32, 0.0) == 0.0); + expect(@sqrt(f32, -0.0) == -0.0); + expect(math.isNan(@sqrt(f32, -1.0))); + expect(math.isNan(@sqrt(f32, math.nan(f32)))); } test "math.sqrt64.special" { - assert(math.isPositiveInf(@sqrt(f64, math.inf(f64)))); - assert(@sqrt(f64, 0.0) == 0.0); - assert(@sqrt(f64, -0.0) == -0.0); - assert(math.isNan(@sqrt(f64, -1.0))); - assert(math.isNan(@sqrt(f64, math.nan(f64)))); + expect(math.isPositiveInf(@sqrt(f64, math.inf(f64)))); + expect(@sqrt(f64, 0.0) == 0.0); + expect(@sqrt(f64, -0.0) == -0.0); + expect(math.isNan(@sqrt(f64, -1.0))); + expect(math.isNan(@sqrt(f64, math.nan(f64)))); } fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) { @@ -127,10 +127,10 @@ fn sqrt_int(comptime T: type, value: T) @IntType(false, T.bit_count / 2) { } test "math.sqrt_int" { - assert(sqrt_int(u32, 3) == 1); - assert(sqrt_int(u32, 4) == 2); - assert(sqrt_int(u32, 5) == 2); - assert(sqrt_int(u32, 8) == 2); - assert(sqrt_int(u32, 9) == 3); - assert(sqrt_int(u32, 10) == 3); + expect(sqrt_int(u32, 3) == 1); + expect(sqrt_int(u32, 4) == 2); + expect(sqrt_int(u32, 5) == 2); + expect(sqrt_int(u32, 8) == 2); + expect(sqrt_int(u32, 9) == 3); + expect(sqrt_int(u32, 10) == 3); } diff --git a/std/math/tan.zig b/std/math/tan.zig index a71a17e625..ec43092320 100644 --- a/std/math/tan.zig +++ b/std/math/tan.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; pub fn tan(x: var) @typeOf(x) { const T = @typeOf(x); @@ -129,44 +129,44 @@ fn tan64(x_: f64) f64 { } test "math.tan" { - assert(tan(f32(0.0)) == tan32(0.0)); - assert(tan(f64(0.0)) == tan64(0.0)); + expect(tan(f32(0.0)) == tan32(0.0)); + expect(tan(f64(0.0)) == tan64(0.0)); } test "math.tan32" { const epsilon = 0.000001; - assert(math.approxEq(f32, tan32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, tan32(0.2), 0.202710, epsilon)); - assert(math.approxEq(f32, tan32(0.8923), 1.240422, epsilon)); - assert(math.approxEq(f32, tan32(1.5), 14.101420, epsilon)); - assert(math.approxEq(f32, tan32(37.45), -0.254397, epsilon)); - assert(math.approxEq(f32, tan32(89.123), 2.285852, epsilon)); + expect(math.approxEq(f32, tan32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, tan32(0.2), 0.202710, epsilon)); + expect(math.approxEq(f32, tan32(0.8923), 1.240422, epsilon)); + expect(math.approxEq(f32, tan32(1.5), 14.101420, epsilon)); + expect(math.approxEq(f32, tan32(37.45), -0.254397, epsilon)); + expect(math.approxEq(f32, tan32(89.123), 2.285852, epsilon)); } test "math.tan64" { const epsilon = 0.000001; - assert(math.approxEq(f64, tan64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, tan64(0.2), 0.202710, epsilon)); - assert(math.approxEq(f64, tan64(0.8923), 1.240422, epsilon)); - assert(math.approxEq(f64, tan64(1.5), 14.101420, epsilon)); - assert(math.approxEq(f64, tan64(37.45), -0.254397, epsilon)); - assert(math.approxEq(f64, tan64(89.123), 2.2858376, epsilon)); + expect(math.approxEq(f64, tan64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, tan64(0.2), 0.202710, epsilon)); + expect(math.approxEq(f64, tan64(0.8923), 1.240422, epsilon)); + expect(math.approxEq(f64, tan64(1.5), 14.101420, epsilon)); + expect(math.approxEq(f64, tan64(37.45), -0.254397, epsilon)); + expect(math.approxEq(f64, tan64(89.123), 2.2858376, epsilon)); } test "math.tan32.special" { - assert(tan32(0.0) == 0.0); - assert(tan32(-0.0) == -0.0); - assert(math.isNan(tan32(math.inf(f32)))); - assert(math.isNan(tan32(-math.inf(f32)))); - assert(math.isNan(tan32(math.nan(f32)))); + expect(tan32(0.0) == 0.0); + expect(tan32(-0.0) == -0.0); + expect(math.isNan(tan32(math.inf(f32)))); + expect(math.isNan(tan32(-math.inf(f32)))); + expect(math.isNan(tan32(math.nan(f32)))); } test "math.tan64.special" { - assert(tan64(0.0) == 0.0); - assert(tan64(-0.0) == -0.0); - assert(math.isNan(tan64(math.inf(f64)))); - assert(math.isNan(tan64(-math.inf(f64)))); - assert(math.isNan(tan64(math.nan(f64)))); + expect(tan64(0.0) == 0.0); + expect(tan64(-0.0) == -0.0); + expect(math.isNan(tan64(math.inf(f64)))); + expect(math.isNan(tan64(-math.inf(f64)))); + expect(math.isNan(tan64(math.nan(f64)))); } diff --git a/std/math/tanh.zig b/std/math/tanh.zig index faeb2641cc..a35449a053 100644 --- a/std/math/tanh.zig +++ b/std/math/tanh.zig @@ -7,7 +7,7 @@ const builtin = @import("builtin"); const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const expo2 = @import("expo2.zig").expo2; const maxInt = std.math.maxInt; @@ -113,42 +113,42 @@ fn tanh64(x: f64) f64 { } test "math.tanh" { - assert(tanh(f32(1.5)) == tanh32(1.5)); - assert(tanh(f64(1.5)) == tanh64(1.5)); + expect(tanh(f32(1.5)) == tanh32(1.5)); + expect(tanh(f64(1.5)) == tanh64(1.5)); } test "math.tanh32" { const epsilon = 0.000001; - assert(math.approxEq(f32, tanh32(0.0), 0.0, epsilon)); - assert(math.approxEq(f32, tanh32(0.2), 0.197375, epsilon)); - assert(math.approxEq(f32, tanh32(0.8923), 0.712528, epsilon)); - assert(math.approxEq(f32, tanh32(1.5), 0.905148, epsilon)); - assert(math.approxEq(f32, tanh32(37.45), 1.0, epsilon)); + expect(math.approxEq(f32, tanh32(0.0), 0.0, epsilon)); + expect(math.approxEq(f32, tanh32(0.2), 0.197375, epsilon)); + expect(math.approxEq(f32, tanh32(0.8923), 0.712528, epsilon)); + expect(math.approxEq(f32, tanh32(1.5), 0.905148, epsilon)); + expect(math.approxEq(f32, tanh32(37.45), 1.0, epsilon)); } test "math.tanh64" { const epsilon = 0.000001; - assert(math.approxEq(f64, tanh64(0.0), 0.0, epsilon)); - assert(math.approxEq(f64, tanh64(0.2), 0.197375, epsilon)); - assert(math.approxEq(f64, tanh64(0.8923), 0.712528, epsilon)); - assert(math.approxEq(f64, tanh64(1.5), 0.905148, epsilon)); - assert(math.approxEq(f64, tanh64(37.45), 1.0, epsilon)); + expect(math.approxEq(f64, tanh64(0.0), 0.0, epsilon)); + expect(math.approxEq(f64, tanh64(0.2), 0.197375, epsilon)); + expect(math.approxEq(f64, tanh64(0.8923), 0.712528, epsilon)); + expect(math.approxEq(f64, tanh64(1.5), 0.905148, epsilon)); + expect(math.approxEq(f64, tanh64(37.45), 1.0, epsilon)); } test "math.tanh32.special" { - assert(tanh32(0.0) == 0.0); - assert(tanh32(-0.0) == -0.0); - assert(tanh32(math.inf(f32)) == 1.0); - assert(tanh32(-math.inf(f32)) == -1.0); - assert(math.isNan(tanh32(math.nan(f32)))); + expect(tanh32(0.0) == 0.0); + expect(tanh32(-0.0) == -0.0); + expect(tanh32(math.inf(f32)) == 1.0); + expect(tanh32(-math.inf(f32)) == -1.0); + expect(math.isNan(tanh32(math.nan(f32)))); } test "math.tanh64.special" { - assert(tanh64(0.0) == 0.0); - assert(tanh64(-0.0) == -0.0); - assert(tanh64(math.inf(f64)) == 1.0); - assert(tanh64(-math.inf(f64)) == -1.0); - assert(math.isNan(tanh64(math.nan(f64)))); + expect(tanh64(0.0) == 0.0); + expect(tanh64(-0.0) == -0.0); + expect(tanh64(math.inf(f64)) == 1.0); + expect(tanh64(-math.inf(f64)) == -1.0); + expect(math.isNan(tanh64(math.nan(f64)))); } diff --git a/std/math/trunc.zig b/std/math/trunc.zig index bb309a1e24..8c91ccc568 100644 --- a/std/math/trunc.zig +++ b/std/math/trunc.zig @@ -6,7 +6,7 @@ const std = @import("../index.zig"); const math = std.math; -const assert = std.debug.assert; +const expect = std.testing.expect; const maxInt = std.math.maxInt; pub fn trunc(x: var) @typeOf(x) { @@ -61,34 +61,34 @@ fn trunc64(x: f64) f64 { } test "math.trunc" { - assert(trunc(f32(1.3)) == trunc32(1.3)); - assert(trunc(f64(1.3)) == trunc64(1.3)); + expect(trunc(f32(1.3)) == trunc32(1.3)); + expect(trunc(f64(1.3)) == trunc64(1.3)); } test "math.trunc32" { - assert(trunc32(1.3) == 1.0); - assert(trunc32(-1.3) == -1.0); - assert(trunc32(0.2) == 0.0); + expect(trunc32(1.3) == 1.0); + expect(trunc32(-1.3) == -1.0); + expect(trunc32(0.2) == 0.0); } test "math.trunc64" { - assert(trunc64(1.3) == 1.0); - assert(trunc64(-1.3) == -1.0); - assert(trunc64(0.2) == 0.0); + expect(trunc64(1.3) == 1.0); + expect(trunc64(-1.3) == -1.0); + expect(trunc64(0.2) == 0.0); } test "math.trunc32.special" { - assert(trunc32(0.0) == 0.0); // 0x3F800000 - assert(trunc32(-0.0) == -0.0); - assert(math.isPositiveInf(trunc32(math.inf(f32)))); - assert(math.isNegativeInf(trunc32(-math.inf(f32)))); - assert(math.isNan(trunc32(math.nan(f32)))); + expect(trunc32(0.0) == 0.0); // 0x3F800000 + expect(trunc32(-0.0) == -0.0); + expect(math.isPositiveInf(trunc32(math.inf(f32)))); + expect(math.isNegativeInf(trunc32(-math.inf(f32)))); + expect(math.isNan(trunc32(math.nan(f32)))); } test "math.trunc64.special" { - assert(trunc64(0.0) == 0.0); - assert(trunc64(-0.0) == -0.0); - assert(math.isPositiveInf(trunc64(math.inf(f64)))); - assert(math.isNegativeInf(trunc64(-math.inf(f64)))); - assert(math.isNan(trunc64(math.nan(f64)))); + expect(trunc64(0.0) == 0.0); + expect(trunc64(-0.0) == -0.0); + expect(math.isPositiveInf(trunc64(math.inf(f64)))); + expect(math.isNegativeInf(trunc64(-math.inf(f64)))); + expect(math.isNan(trunc64(math.nan(f64)))); } diff --git a/std/mem.zig b/std/mem.zig index a6cbae744f..1c7523bf13 100644 --- a/std/mem.zig +++ b/std/mem.zig @@ -6,6 +6,7 @@ const builtin = @import("builtin"); const mem = @This(); const meta = std.meta; const trait = meta.trait; +const testing = std.testing; pub const Allocator = struct { pub const Error = error{OutOfMemory}; @@ -181,7 +182,7 @@ test "mem.secureZero" { set(u8, a[0..], 0); secureZero(u8, b[0..]); - assert(eql(u8, a[0..], b[0..])); + testing.expectEqualSlices(u8, a[0..], b[0..]); } pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare { @@ -210,11 +211,11 @@ pub fn compare(comptime T: type, lhs: []const T, rhs: []const T) Compare { } test "mem.compare" { - assert(compare(u8, "abcd", "bee") == Compare.LessThan); - assert(compare(u8, "abc", "abc") == Compare.Equal); - assert(compare(u8, "abc", "abc0") == Compare.LessThan); - assert(compare(u8, "", "") == Compare.Equal); - assert(compare(u8, "", "a") == Compare.LessThan); + testing.expect(compare(u8, "abcd", "bee") == Compare.LessThan); + testing.expect(compare(u8, "abc", "abc") == Compare.Equal); + testing.expect(compare(u8, "abc", "abc0") == Compare.LessThan); + testing.expect(compare(u8, "", "") == Compare.Equal); + testing.expect(compare(u8, "", "a") == Compare.LessThan); } /// Returns true if lhs < rhs, false otherwise @@ -227,11 +228,11 @@ pub fn lessThan(comptime T: type, lhs: []const T, rhs: []const T) bool { } test "mem.lessThan" { - assert(lessThan(u8, "abcd", "bee")); - assert(!lessThan(u8, "abc", "abc")); - assert(lessThan(u8, "abc", "abc0")); - assert(!lessThan(u8, "", "")); - assert(lessThan(u8, "", "a")); + testing.expect(lessThan(u8, "abcd", "bee")); + testing.expect(!lessThan(u8, "abc", "abc")); + testing.expect(lessThan(u8, "abc", "abc0")); + testing.expect(!lessThan(u8, "", "")); + testing.expect(lessThan(u8, "", "a")); } /// Compares two slices and returns whether they are equal. @@ -296,10 +297,10 @@ pub fn trim(comptime T: type, slice: []const T, values_to_strip: []const T) []co } test "mem.trim" { - assert(eql(u8, trimLeft(u8, " foo\n ", " \n"), "foo\n ")); - assert(eql(u8, trimRight(u8, " foo\n ", " \n"), " foo")); - assert(eql(u8, trim(u8, " foo\n ", " \n"), "foo")); - assert(eql(u8, trim(u8, "foo", " \n"), "foo")); + testing.expectEqualSlices(u8, "foo\n ", trimLeft(u8, " foo\n ", " \n")); + testing.expectEqualSlices(u8, " foo", trimRight(u8, " foo\n ", " \n")); + testing.expectEqualSlices(u8, "foo", trim(u8, " foo\n ", " \n")); + testing.expectEqualSlices(u8, "foo", trim(u8, "foo", " \n")); } /// Linear search for the index of a scalar value inside a slice. @@ -380,20 +381,20 @@ pub fn indexOfPos(comptime T: type, haystack: []const T, start_index: usize, nee } test "mem.indexOf" { - assert(indexOf(u8, "one two three four", "four").? == 14); - assert(lastIndexOf(u8, "one two three two four", "two").? == 14); - assert(indexOf(u8, "one two three four", "gour") == null); - assert(lastIndexOf(u8, "one two three four", "gour") == null); - assert(indexOf(u8, "foo", "foo").? == 0); - assert(lastIndexOf(u8, "foo", "foo").? == 0); - assert(indexOf(u8, "foo", "fool") == null); - assert(lastIndexOf(u8, "foo", "lfoo") == null); - assert(lastIndexOf(u8, "foo", "fool") == null); - - assert(indexOf(u8, "foo foo", "foo").? == 0); - assert(lastIndexOf(u8, "foo foo", "foo").? == 4); - assert(lastIndexOfAny(u8, "boo, cat", "abo").? == 6); - assert(lastIndexOfScalar(u8, "boo", 'o').? == 2); + testing.expect(indexOf(u8, "one two three four", "four").? == 14); + testing.expect(lastIndexOf(u8, "one two three two four", "two").? == 14); + testing.expect(indexOf(u8, "one two three four", "gour") == null); + testing.expect(lastIndexOf(u8, "one two three four", "gour") == null); + testing.expect(indexOf(u8, "foo", "foo").? == 0); + testing.expect(lastIndexOf(u8, "foo", "foo").? == 0); + testing.expect(indexOf(u8, "foo", "fool") == null); + testing.expect(lastIndexOf(u8, "foo", "lfoo") == null); + testing.expect(lastIndexOf(u8, "foo", "fool") == null); + + testing.expect(indexOf(u8, "foo foo", "foo").? == 0); + testing.expect(lastIndexOf(u8, "foo foo", "foo").? == 4); + testing.expect(lastIndexOfAny(u8, "boo, cat", "abo").? == 6); + testing.expect(lastIndexOfScalar(u8, "boo", 'o').? == 2); } /// Reads an integer from memory with size equal to bytes.len. @@ -504,34 +505,34 @@ test "comptime read/write int" { var bytes: [2]u8 = undefined; std.mem.writeIntLittle(u16, &bytes, 0x1234); const result = std.mem.readIntBig(u16, &bytes); - std.debug.assert(result == 0x3412); + testing.expect(result == 0x3412); } comptime { var bytes: [2]u8 = undefined; std.mem.writeIntBig(u16, &bytes, 0x1234); const result = std.mem.readIntLittle(u16, &bytes); - std.debug.assert(result == 0x3412); + testing.expect(result == 0x3412); } } test "readIntBig and readIntLittle" { - assert(readIntSliceBig(u0, []u8{}) == 0x0); - assert(readIntSliceLittle(u0, []u8{}) == 0x0); + testing.expect(readIntSliceBig(u0, []u8{}) == 0x0); + testing.expect(readIntSliceLittle(u0, []u8{}) == 0x0); - assert(readIntSliceBig(u8, []u8{0x32}) == 0x32); - assert(readIntSliceLittle(u8, []u8{0x12}) == 0x12); + testing.expect(readIntSliceBig(u8, []u8{0x32}) == 0x32); + testing.expect(readIntSliceLittle(u8, []u8{0x12}) == 0x12); - assert(readIntSliceBig(u16, []u8{ 0x12, 0x34 }) == 0x1234); - assert(readIntSliceLittle(u16, []u8{ 0x12, 0x34 }) == 0x3412); + testing.expect(readIntSliceBig(u16, []u8{ 0x12, 0x34 }) == 0x1234); + testing.expect(readIntSliceLittle(u16, []u8{ 0x12, 0x34 }) == 0x3412); - assert(readIntSliceBig(u72, []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024); - assert(readIntSliceLittle(u72, []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec); + testing.expect(readIntSliceBig(u72, []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 }) == 0x123456789abcdef024); + testing.expect(readIntSliceLittle(u72, []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe }) == 0xfedcba9876543210ec); - assert(readIntSliceBig(i8, []u8{0xff}) == -1); - assert(readIntSliceLittle(i8, []u8{0xfe}) == -2); + testing.expect(readIntSliceBig(i8, []u8{0xff}) == -1); + testing.expect(readIntSliceLittle(i8, []u8{0xfe}) == -2); - assert(readIntSliceBig(i16, []u8{ 0xff, 0xfd }) == -3); - assert(readIntSliceLittle(i16, []u8{ 0xfc, 0xff }) == -4); + testing.expect(readIntSliceBig(i16, []u8{ 0xff, 0xfd }) == -3); + testing.expect(readIntSliceLittle(i16, []u8{ 0xfc, 0xff }) == -4); } /// Writes an integer to memory, storing it in twos-complement. @@ -645,34 +646,34 @@ test "writeIntBig and writeIntLittle" { var buf9: [9]u8 = undefined; writeIntBig(u0, &buf0, 0x0); - assert(eql_slice_u8(buf0[0..], []u8{})); + testing.expect(eql_slice_u8(buf0[0..], []u8{})); writeIntLittle(u0, &buf0, 0x0); - assert(eql_slice_u8(buf0[0..], []u8{})); + testing.expect(eql_slice_u8(buf0[0..], []u8{})); writeIntBig(u8, &buf1, 0x12); - assert(eql_slice_u8(buf1[0..], []u8{0x12})); + testing.expect(eql_slice_u8(buf1[0..], []u8{0x12})); writeIntLittle(u8, &buf1, 0x34); - assert(eql_slice_u8(buf1[0..], []u8{0x34})); + testing.expect(eql_slice_u8(buf1[0..], []u8{0x34})); writeIntBig(u16, &buf2, 0x1234); - assert(eql_slice_u8(buf2[0..], []u8{ 0x12, 0x34 })); + testing.expect(eql_slice_u8(buf2[0..], []u8{ 0x12, 0x34 })); writeIntLittle(u16, &buf2, 0x5678); - assert(eql_slice_u8(buf2[0..], []u8{ 0x78, 0x56 })); + testing.expect(eql_slice_u8(buf2[0..], []u8{ 0x78, 0x56 })); writeIntBig(u72, &buf9, 0x123456789abcdef024); - assert(eql_slice_u8(buf9[0..], []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); + testing.expect(eql_slice_u8(buf9[0..], []u8{ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xbc, 0xde, 0xf0, 0x24 })); writeIntLittle(u72, &buf9, 0xfedcba9876543210ec); - assert(eql_slice_u8(buf9[0..], []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); + testing.expect(eql_slice_u8(buf9[0..], []u8{ 0xec, 0x10, 0x32, 0x54, 0x76, 0x98, 0xba, 0xdc, 0xfe })); writeIntBig(i8, &buf1, -1); - assert(eql_slice_u8(buf1[0..], []u8{0xff})); + testing.expect(eql_slice_u8(buf1[0..], []u8{0xff})); writeIntLittle(i8, &buf1, -2); - assert(eql_slice_u8(buf1[0..], []u8{0xfe})); + testing.expect(eql_slice_u8(buf1[0..], []u8{0xfe})); writeIntBig(i16, &buf2, -3); - assert(eql_slice_u8(buf2[0..], []u8{ 0xff, 0xfd })); + testing.expect(eql_slice_u8(buf2[0..], []u8{ 0xff, 0xfd })); writeIntLittle(i16, &buf2, -4); - assert(eql_slice_u8(buf2[0..], []u8{ 0xfc, 0xff })); + testing.expect(eql_slice_u8(buf2[0..], []u8{ 0xfc, 0xff })); } pub fn hash_slice_u8(k: []const u8) u32 { @@ -706,46 +707,46 @@ pub fn tokenize(buffer: []const u8, delimiter_bytes: []const u8) TokenIterator { test "mem.tokenize" { var it = tokenize(" abc def ghi ", " "); - assert(eql(u8, it.next().?, "abc")); - assert(eql(u8, it.next().?, "def")); - assert(eql(u8, it.next().?, "ghi")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "abc")); + testing.expect(eql(u8, it.next().?, "def")); + testing.expect(eql(u8, it.next().?, "ghi")); + testing.expect(it.next() == null); it = tokenize("..\\bob", "\\"); - assert(eql(u8, it.next().?, "..")); - assert(eql(u8, "..", "..\\bob"[0..it.index])); - assert(eql(u8, it.next().?, "bob")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "..")); + testing.expect(eql(u8, "..", "..\\bob"[0..it.index])); + testing.expect(eql(u8, it.next().?, "bob")); + testing.expect(it.next() == null); it = tokenize("//a/b", "/"); - assert(eql(u8, it.next().?, "a")); - assert(eql(u8, it.next().?, "b")); - assert(eql(u8, "//a/b", "//a/b"[0..it.index])); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "a")); + testing.expect(eql(u8, it.next().?, "b")); + testing.expect(eql(u8, "//a/b", "//a/b"[0..it.index])); + testing.expect(it.next() == null); it = tokenize("|", "|"); - assert(it.next() == null); + testing.expect(it.next() == null); it = tokenize("", "|"); - assert(it.next() == null); + testing.expect(it.next() == null); it = tokenize("hello", ""); - assert(eql(u8, it.next().?, "hello")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "hello")); + testing.expect(it.next() == null); it = tokenize("hello", " "); - assert(eql(u8, it.next().?, "hello")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "hello")); + testing.expect(it.next() == null); } test "mem.tokenize (multibyte)" { var it = tokenize("a|b,c/d e", " /,|"); - assert(eql(u8, it.next().?, "a")); - assert(eql(u8, it.next().?, "b")); - assert(eql(u8, it.next().?, "c")); - assert(eql(u8, it.next().?, "d")); - assert(eql(u8, it.next().?, "e")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "a")); + testing.expect(eql(u8, it.next().?, "b")); + testing.expect(eql(u8, it.next().?, "c")); + testing.expect(eql(u8, it.next().?, "d")); + testing.expect(eql(u8, it.next().?, "e")); + testing.expect(it.next() == null); } /// Returns an iterator that iterates over the slices of `buffer` that @@ -769,34 +770,34 @@ pub fn separate(buffer: []const u8, delimiter: []const u8) SplitIterator { test "mem.separate" { var it = separate("abc|def||ghi", "|"); - assert(eql(u8, it.next().?, "abc")); - assert(eql(u8, it.next().?, "def")); - assert(eql(u8, it.next().?, "")); - assert(eql(u8, it.next().?, "ghi")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "abc")); + testing.expect(eql(u8, it.next().?, "def")); + testing.expect(eql(u8, it.next().?, "")); + testing.expect(eql(u8, it.next().?, "ghi")); + testing.expect(it.next() == null); it = separate("", "|"); - assert(eql(u8, it.next().?, "")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "")); + testing.expect(it.next() == null); it = separate("|", "|"); - assert(eql(u8, it.next().?, "")); - assert(eql(u8, it.next().?, "")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "")); + testing.expect(eql(u8, it.next().?, "")); + testing.expect(it.next() == null); it = separate("hello", " "); - assert(eql(u8, it.next().?, "hello")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "hello")); + testing.expect(it.next() == null); } test "mem.separate (multibyte)" { var it = separate("a, b ,, c, d, e", ", "); - assert(eql(u8, it.next().?, "a")); - assert(eql(u8, it.next().?, "b ,")); - assert(eql(u8, it.next().?, "c")); - assert(eql(u8, it.next().?, "d")); - assert(eql(u8, it.next().?, "e")); - assert(it.next() == null); + testing.expect(eql(u8, it.next().?, "a")); + testing.expect(eql(u8, it.next().?, "b ,")); + testing.expect(eql(u8, it.next().?, "c")); + testing.expect(eql(u8, it.next().?, "d")); + testing.expect(eql(u8, it.next().?, "e")); + testing.expect(it.next() == null); } pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool { @@ -804,8 +805,8 @@ pub fn startsWith(comptime T: type, haystack: []const T, needle: []const T) bool } test "mem.startsWith" { - assert(startsWith(u8, "Bob", "Bo")); - assert(!startsWith(u8, "Needle in haystack", "haystack")); + testing.expect(startsWith(u8, "Bob", "Bo")); + testing.expect(!startsWith(u8, "Needle in haystack", "haystack")); } pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool { @@ -813,8 +814,8 @@ pub fn endsWith(comptime T: type, haystack: []const T, needle: []const T) bool { } test "mem.endsWith" { - assert(endsWith(u8, "Needle in haystack", "haystack")); - assert(!endsWith(u8, "Bob", "Bo")); + testing.expect(endsWith(u8, "Needle in haystack", "haystack")); + testing.expect(!endsWith(u8, "Bob", "Bo")); } pub const TokenIterator = struct { @@ -913,15 +914,15 @@ pub fn join(allocator: *Allocator, separator: []const u8, slices: []const []cons test "mem.join" { var buf: [1024]u8 = undefined; const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; - assert(eql(u8, try join(a, ",", [][]const u8{ "a", "b", "c" }), "a,b,c")); - assert(eql(u8, try join(a, ",", [][]const u8{"a"}), "a")); - assert(eql(u8, try join(a, ",", [][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c")); + testing.expect(eql(u8, try join(a, ",", [][]const u8{ "a", "b", "c" }), "a,b,c")); + testing.expect(eql(u8, try join(a, ",", [][]const u8{"a"}), "a")); + testing.expect(eql(u8, try join(a, ",", [][]const u8{ "a", "", "b", "", "c" }), "a,,b,,c")); } test "testStringEquality" { - assert(eql(u8, "abcd", "abcd")); - assert(!eql(u8, "abcdef", "abZdef")); - assert(!eql(u8, "abcdefg", "abcdef")); + testing.expect(eql(u8, "abcd", "abcd")); + testing.expect(!eql(u8, "abcdef", "abZdef")); + testing.expect(!eql(u8, "abcdefg", "abcdef")); } test "testReadInt" { @@ -936,12 +937,12 @@ fn testReadIntImpl() void { 0x56, 0x78, }; - assert(readInt(u32, &bytes, builtin.Endian.Big) == 0x12345678); - assert(readIntBig(u32, &bytes) == 0x12345678); - assert(readIntBig(i32, &bytes) == 0x12345678); - assert(readInt(u32, &bytes, builtin.Endian.Little) == 0x78563412); - assert(readIntLittle(u32, &bytes) == 0x78563412); - assert(readIntLittle(i32, &bytes) == 0x78563412); + testing.expect(readInt(u32, &bytes, builtin.Endian.Big) == 0x12345678); + testing.expect(readIntBig(u32, &bytes) == 0x12345678); + testing.expect(readIntBig(i32, &bytes) == 0x12345678); + testing.expect(readInt(u32, &bytes, builtin.Endian.Little) == 0x78563412); + testing.expect(readIntLittle(u32, &bytes) == 0x78563412); + testing.expect(readIntLittle(i32, &bytes) == 0x78563412); } { const buf = []u8{ @@ -951,7 +952,7 @@ fn testReadIntImpl() void { 0x34, }; const answer = readInt(u32, &buf, builtin.Endian.Big); - assert(answer == 0x00001234); + testing.expect(answer == 0x00001234); } { const buf = []u8{ @@ -961,17 +962,17 @@ fn testReadIntImpl() void { 0x00, }; const answer = readInt(u32, &buf, builtin.Endian.Little); - assert(answer == 0x00003412); + testing.expect(answer == 0x00003412); } { const bytes = []u8{ 0xff, 0xfe, }; - assert(readIntBig(u16, &bytes) == 0xfffe); - assert(readIntBig(i16, &bytes) == -0x0002); - assert(readIntLittle(u16, &bytes) == 0xfeff); - assert(readIntLittle(i16, &bytes) == -0x0101); + testing.expect(readIntBig(u16, &bytes) == 0xfffe); + testing.expect(readIntBig(i16, &bytes) == -0x0002); + testing.expect(readIntLittle(u16, &bytes) == 0xfeff); + testing.expect(readIntLittle(i16, &bytes) == -0x0101); } } @@ -983,19 +984,19 @@ fn testWriteIntImpl() void { var bytes: [8]u8 = undefined; writeIntSlice(u0, bytes[0..], 0, builtin.Endian.Big); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(u0, bytes[0..], 0, builtin.Endian.Little); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, })); writeIntSlice(u64, bytes[0..], 0x12345678CAFEBABE, builtin.Endian.Big); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, @@ -1007,7 +1008,7 @@ fn testWriteIntImpl() void { })); writeIntSlice(u64, bytes[0..], 0xBEBAFECA78563412, builtin.Endian.Little); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, @@ -1019,7 +1020,7 @@ fn testWriteIntImpl() void { })); writeIntSlice(u32, bytes[0..], 0x12345678, builtin.Endian.Big); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x00, 0x00, 0x00, @@ -1031,7 +1032,7 @@ fn testWriteIntImpl() void { })); writeIntSlice(u32, bytes[0..], 0x78563412, builtin.Endian.Little); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x12, 0x34, 0x56, @@ -1043,7 +1044,7 @@ fn testWriteIntImpl() void { })); writeIntSlice(u16, bytes[0..], 0x1234, builtin.Endian.Big); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x00, 0x00, 0x00, @@ -1055,7 +1056,7 @@ fn testWriteIntImpl() void { })); writeIntSlice(u16, bytes[0..], 0x1234, builtin.Endian.Little); - assert(eql(u8, bytes, []u8{ + testing.expect(eql(u8, bytes, []u8{ 0x34, 0x12, 0x00, @@ -1076,7 +1077,7 @@ pub fn min(comptime T: type, slice: []const T) T { } test "mem.min" { - assert(min(u8, "abcdefg") == 'a'); + testing.expect(min(u8, "abcdefg") == 'a'); } pub fn max(comptime T: type, slice: []const T) T { @@ -1088,7 +1089,7 @@ pub fn max(comptime T: type, slice: []const T) T { } test "mem.max" { - assert(max(u8, "abcdefg") == 'g'); + testing.expect(max(u8, "abcdefg") == 'g'); } pub fn swap(comptime T: type, a: *T, b: *T) void { @@ -1116,7 +1117,7 @@ test "std.mem.reverse" { }; reverse(i32, arr[0..]); - assert(eql(i32, arr, []i32{ + testing.expect(eql(i32, arr, []i32{ 4, 2, 1, @@ -1143,7 +1144,7 @@ test "std.mem.rotate" { }; rotate(i32, arr[0..], 2); - assert(eql(i32, arr, []i32{ + testing.expect(eql(i32, arr, []i32{ 1, 2, 4, @@ -1225,12 +1226,12 @@ test "std.mem.asBytes" { builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - debug.assert(std.mem.eql(u8, asBytes(&deadbeef), deadbeef_bytes)); + testing.expect(std.mem.eql(u8, asBytes(&deadbeef), deadbeef_bytes)); var codeface = u32(0xC0DEFACE); for (asBytes(&codeface).*) |*b| b.* = 0; - debug.assert(codeface == 0); + testing.expect(codeface == 0); const S = packed struct { a: u8, @@ -1245,7 +1246,7 @@ test "std.mem.asBytes" { .c = 0xDE, .d = 0xA1, }; - debug.assert(std.mem.eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); + testing.expect(std.mem.eql(u8, asBytes(&inst), "\xBE\xEF\xDE\xA1")); } ///Given any value, returns a copy of its bytes in an array. @@ -1256,14 +1257,14 @@ pub fn toBytes(value: var) [@sizeOf(@typeOf(value))]u8 { test "std.mem.toBytes" { var my_bytes = toBytes(u32(0x12345678)); switch (builtin.endian) { - builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x12\x34\x56\x78")), - builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x78\x56\x34\x12")), + builtin.Endian.Big => testing.expect(std.mem.eql(u8, my_bytes, "\x12\x34\x56\x78")), + builtin.Endian.Little => testing.expect(std.mem.eql(u8, my_bytes, "\x78\x56\x34\x12")), } my_bytes[0] = '\x99'; switch (builtin.endian) { - builtin.Endian.Big => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x34\x56\x78")), - builtin.Endian.Little => debug.assert(std.mem.eql(u8, my_bytes, "\x99\x56\x34\x12")), + builtin.Endian.Big => testing.expect(std.mem.eql(u8, my_bytes, "\x99\x34\x56\x78")), + builtin.Endian.Little => testing.expect(std.mem.eql(u8, my_bytes, "\x99\x56\x34\x12")), } } @@ -1292,17 +1293,17 @@ test "std.mem.bytesAsValue" { builtin.Endian.Little => "\xEF\xBE\xAD\xDE", }; - debug.assert(deadbeef == bytesAsValue(u32, &deadbeef_bytes).*); + testing.expect(deadbeef == bytesAsValue(u32, &deadbeef_bytes).*); var codeface_bytes = switch (builtin.endian) { builtin.Endian.Big => "\xC0\xDE\xFA\xCE", builtin.Endian.Little => "\xCE\xFA\xDE\xC0", }; var codeface = bytesAsValue(u32, &codeface_bytes); - debug.assert(codeface.* == 0xC0DEFACE); + testing.expect(codeface.* == 0xC0DEFACE); codeface.* = 0; for (codeface_bytes) |b| - debug.assert(b == 0); + testing.expect(b == 0); const S = packed struct { a: u8, @@ -1319,7 +1320,7 @@ test "std.mem.bytesAsValue" { }; const inst_bytes = "\xBE\xEF\xDE\xA1"; const inst2 = bytesAsValue(S, &inst_bytes); - debug.assert(meta.eql(inst, inst2.*)); + testing.expect(meta.eql(inst, inst2.*)); } ///Given a pointer to an array of bytes, returns a value of the specified type backed by a @@ -1334,7 +1335,7 @@ test "std.mem.bytesToValue" { }; const deadbeef = bytesToValue(u32, deadbeef_bytes); - debug.assert(deadbeef == u32(0xDEADBEEF)); + testing.expect(deadbeef == u32(0xDEADBEEF)); } fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { @@ -1345,7 +1346,7 @@ fn SubArrayPtrReturnType(comptime T: type, comptime length: usize) type { ///Given a pointer to an array, returns a pointer to a portion of that array, preserving constness. pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubArrayPtrReturnType(@typeOf(ptr), length) { - debug.assert(start + length <= ptr.*.len); + assert(start + length <= ptr.*.len); const ReturnType = SubArrayPtrReturnType(@typeOf(ptr), length); const T = meta.Child(meta.Child(@typeOf(ptr))); @@ -1355,14 +1356,14 @@ pub fn subArrayPtr(ptr: var, comptime start: usize, comptime length: usize) SubA test "std.mem.subArrayPtr" { const a1 = "abcdef"; const sub1 = subArrayPtr(&a1, 2, 3); - debug.assert(std.mem.eql(u8, sub1.*, "cde")); + testing.expect(std.mem.eql(u8, sub1.*, "cde")); var a2 = "abcdef"; var sub2 = subArrayPtr(&a2, 2, 3); - debug.assert(std.mem.eql(u8, sub2, "cde")); + testing.expect(std.mem.eql(u8, sub2, "cde")); sub2[1] = 'X'; - debug.assert(std.mem.eql(u8, a2, "abcXef")); + testing.expect(std.mem.eql(u8, a2, "abcXef")); } /// Round an address up to the nearest aligned address @@ -1371,16 +1372,16 @@ pub fn alignForward(addr: usize, alignment: usize) usize { } test "std.mem.alignForward" { - debug.assertOrPanic(alignForward(1, 1) == 1); - debug.assertOrPanic(alignForward(2, 1) == 2); - debug.assertOrPanic(alignForward(1, 2) == 2); - debug.assertOrPanic(alignForward(2, 2) == 2); - debug.assertOrPanic(alignForward(3, 2) == 4); - debug.assertOrPanic(alignForward(4, 2) == 4); - debug.assertOrPanic(alignForward(7, 8) == 8); - debug.assertOrPanic(alignForward(8, 8) == 8); - debug.assertOrPanic(alignForward(9, 8) == 16); - debug.assertOrPanic(alignForward(15, 8) == 16); - debug.assertOrPanic(alignForward(16, 8) == 16); - debug.assertOrPanic(alignForward(17, 8) == 24); + testing.expect(alignForward(1, 1) == 1); + testing.expect(alignForward(2, 1) == 2); + testing.expect(alignForward(1, 2) == 2); + testing.expect(alignForward(2, 2) == 2); + testing.expect(alignForward(3, 2) == 4); + testing.expect(alignForward(4, 2) == 4); + testing.expect(alignForward(7, 8) == 8); + testing.expect(alignForward(8, 8) == 8); + testing.expect(alignForward(9, 8) == 16); + testing.expect(alignForward(15, 8) == 16); + testing.expect(alignForward(16, 8) == 16); + testing.expect(alignForward(17, 8) == 24); } diff --git a/std/meta/index.zig b/std/meta/index.zig index 4d195a6a12..3f8ea762a6 100644 --- a/std/meta/index.zig +++ b/std/meta/index.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const debug = std.debug; const mem = std.mem; const math = std.math; +const testing = std.testing; pub const trait = @import("trait.zig"); @@ -64,16 +65,16 @@ test "std.meta.tagName" { var u2a = U2{ .C = 0 }; var u2b = U2{ .D = 0 }; - debug.assert(mem.eql(u8, tagName(E1.A), "A")); - debug.assert(mem.eql(u8, tagName(E1.B), "B")); - debug.assert(mem.eql(u8, tagName(E2.C), "C")); - debug.assert(mem.eql(u8, tagName(E2.D), "D")); - debug.assert(mem.eql(u8, tagName(error.E), "E")); - debug.assert(mem.eql(u8, tagName(error.F), "F")); - debug.assert(mem.eql(u8, tagName(u1g), "G")); - debug.assert(mem.eql(u8, tagName(u1h), "H")); - debug.assert(mem.eql(u8, tagName(u2a), "C")); - debug.assert(mem.eql(u8, tagName(u2b), "D")); + testing.expect(mem.eql(u8, tagName(E1.A), "A")); + testing.expect(mem.eql(u8, tagName(E1.B), "B")); + testing.expect(mem.eql(u8, tagName(E2.C), "C")); + testing.expect(mem.eql(u8, tagName(E2.D), "D")); + testing.expect(mem.eql(u8, tagName(error.E), "E")); + testing.expect(mem.eql(u8, tagName(error.F), "F")); + testing.expect(mem.eql(u8, tagName(u1g), "G")); + testing.expect(mem.eql(u8, tagName(u1h), "H")); + testing.expect(mem.eql(u8, tagName(u2a), "C")); + testing.expect(mem.eql(u8, tagName(u2b), "D")); } pub fn stringToEnum(comptime T: type, str: []const u8) ?T { @@ -90,9 +91,9 @@ test "std.meta.stringToEnum" { A, B, }; - debug.assert(E1.A == stringToEnum(E1, "A").?); - debug.assert(E1.B == stringToEnum(E1, "B").?); - debug.assert(null == stringToEnum(E1, "C")); + testing.expect(E1.A == stringToEnum(E1, "A").?); + testing.expect(E1.B == stringToEnum(E1, "B").?); + testing.expect(null == stringToEnum(E1, "C")); } pub fn bitCount(comptime T: type) comptime_int { @@ -104,8 +105,8 @@ pub fn bitCount(comptime T: type) comptime_int { } test "std.meta.bitCount" { - debug.assert(bitCount(u8) == 8); - debug.assert(bitCount(f32) == 32); + testing.expect(bitCount(u8) == 8); + testing.expect(bitCount(f32) == 32); } pub fn alignment(comptime T: type) comptime_int { @@ -115,11 +116,11 @@ pub fn alignment(comptime T: type) comptime_int { } test "std.meta.alignment" { - debug.assert(alignment(u8) == 1); - debug.assert(alignment(*align(1) u8) == 1); - debug.assert(alignment(*align(2) u8) == 2); - debug.assert(alignment([]align(1) u8) == 1); - debug.assert(alignment([]align(2) u8) == 2); + testing.expect(alignment(u8) == 1); + testing.expect(alignment(*align(1) u8) == 1); + testing.expect(alignment(*align(2) u8) == 2); + testing.expect(alignment([]align(1) u8) == 1); + testing.expect(alignment([]align(2) u8) == 2); } pub fn Child(comptime T: type) type { @@ -133,11 +134,11 @@ pub fn Child(comptime T: type) type { } test "std.meta.Child" { - debug.assert(Child([1]u8) == u8); - debug.assert(Child(*u8) == u8); - debug.assert(Child([]u8) == u8); - debug.assert(Child(?u8) == u8); - debug.assert(Child(promise->u8) == u8); + testing.expect(Child([1]u8) == u8); + testing.expect(Child(*u8) == u8); + testing.expect(Child([]u8) == u8); + testing.expect(Child(?u8) == u8); + testing.expect(Child(promise->u8) == u8); } pub fn containerLayout(comptime T: type) TypeInfo.ContainerLayout { @@ -172,15 +173,15 @@ test "std.meta.containerLayout" { a: u8, }; - debug.assert(containerLayout(E1) == TypeInfo.ContainerLayout.Auto); - debug.assert(containerLayout(E2) == TypeInfo.ContainerLayout.Packed); - debug.assert(containerLayout(E3) == TypeInfo.ContainerLayout.Extern); - debug.assert(containerLayout(S1) == TypeInfo.ContainerLayout.Auto); - debug.assert(containerLayout(S2) == TypeInfo.ContainerLayout.Packed); - debug.assert(containerLayout(S3) == TypeInfo.ContainerLayout.Extern); - debug.assert(containerLayout(U1) == TypeInfo.ContainerLayout.Auto); - debug.assert(containerLayout(U2) == TypeInfo.ContainerLayout.Packed); - debug.assert(containerLayout(U3) == TypeInfo.ContainerLayout.Extern); + testing.expect(containerLayout(E1) == TypeInfo.ContainerLayout.Auto); + testing.expect(containerLayout(E2) == TypeInfo.ContainerLayout.Packed); + testing.expect(containerLayout(E3) == TypeInfo.ContainerLayout.Extern); + testing.expect(containerLayout(S1) == TypeInfo.ContainerLayout.Auto); + testing.expect(containerLayout(S2) == TypeInfo.ContainerLayout.Packed); + testing.expect(containerLayout(S3) == TypeInfo.ContainerLayout.Extern); + testing.expect(containerLayout(U1) == TypeInfo.ContainerLayout.Auto); + testing.expect(containerLayout(U2) == TypeInfo.ContainerLayout.Packed); + testing.expect(containerLayout(U3) == TypeInfo.ContainerLayout.Extern); } pub fn definitions(comptime T: type) []TypeInfo.Definition { @@ -214,8 +215,8 @@ test "std.meta.definitions" { }; inline for (defs) |def| { - debug.assert(def.len == 1); - debug.assert(comptime mem.eql(u8, def[0].name, "a")); + testing.expect(def.len == 1); + testing.expect(comptime mem.eql(u8, def[0].name, "a")); } } @@ -250,8 +251,8 @@ test "std.meta.definitionInfo" { }; inline for (infos) |info| { - debug.assert(comptime mem.eql(u8, info.name, "a")); - debug.assert(!info.is_pub); + testing.expect(comptime mem.eql(u8, info.name, "a")); + testing.expect(!info.is_pub); } } @@ -288,16 +289,16 @@ test "std.meta.fields" { const sf = comptime fields(S1); const uf = comptime fields(U1); - debug.assert(e1f.len == 1); - debug.assert(e2f.len == 1); - debug.assert(sf.len == 1); - debug.assert(uf.len == 1); - debug.assert(mem.eql(u8, e1f[0].name, "A")); - debug.assert(mem.eql(u8, e2f[0].name, "A")); - debug.assert(mem.eql(u8, sf[0].name, "a")); - debug.assert(mem.eql(u8, uf[0].name, "a")); - debug.assert(comptime sf[0].field_type == u8); - debug.assert(comptime uf[0].field_type == u8); + testing.expect(e1f.len == 1); + testing.expect(e2f.len == 1); + testing.expect(sf.len == 1); + testing.expect(uf.len == 1); + testing.expect(mem.eql(u8, e1f[0].name, "A")); + testing.expect(mem.eql(u8, e2f[0].name, "A")); + testing.expect(mem.eql(u8, sf[0].name, "a")); + testing.expect(mem.eql(u8, uf[0].name, "a")); + testing.expect(comptime sf[0].field_type == u8); + testing.expect(comptime uf[0].field_type == u8); } pub fn fieldInfo(comptime T: type, comptime field_name: []const u8) switch (@typeInfo(T)) { @@ -332,12 +333,12 @@ test "std.meta.fieldInfo" { const sf = comptime fieldInfo(S1, "a"); const uf = comptime fieldInfo(U1, "a"); - debug.assert(mem.eql(u8, e1f.name, "A")); - debug.assert(mem.eql(u8, e2f.name, "A")); - debug.assert(mem.eql(u8, sf.name, "a")); - debug.assert(mem.eql(u8, uf.name, "a")); - debug.assert(comptime sf.field_type == u8); - debug.assert(comptime uf.field_type == u8); + testing.expect(mem.eql(u8, e1f.name, "A")); + testing.expect(mem.eql(u8, e2f.name, "A")); + testing.expect(mem.eql(u8, sf.name, "a")); + testing.expect(mem.eql(u8, uf.name, "a")); + testing.expect(comptime sf.field_type == u8); + testing.expect(comptime uf.field_type == u8); } pub fn TagType(comptime T: type) type { @@ -358,8 +359,8 @@ test "std.meta.TagType" { D: u16, }; - debug.assert(TagType(E) == u8); - debug.assert(TagType(U) == E); + testing.expect(TagType(E) == u8); + testing.expect(TagType(U) == E); } ///Returns the active tag of a tagged union @@ -380,18 +381,18 @@ test "std.meta.activeTag" { }; var u = U{ .Int = 32 }; - debug.assert(activeTag(u) == UE.Int); + testing.expect(activeTag(u) == UE.Int); u = U{ .Float = 112.9876 }; - debug.assert(activeTag(u) == UE.Float); + testing.expect(activeTag(u) == UE.Float); } ///Given a tagged union type, and an enum, return the type of the union /// field corresponding to the enum tag. pub fn TagPayloadType(comptime U: type, tag: var) type { const Tag = @typeOf(tag); - debug.assert(trait.is(builtin.TypeId.Union)(U)); - debug.assert(trait.is(builtin.TypeId.Enum)(Tag)); + testing.expect(trait.is(builtin.TypeId.Union)(U)); + testing.expect(trait.is(builtin.TypeId.Enum)(Tag)); const info = @typeInfo(U).Union; @@ -410,7 +411,7 @@ test "std.meta.TagPayloadType" { }; const MovedEvent = TagPayloadType(Event, Event.Moved); var e: Event = undefined; - debug.assert(MovedEvent == @typeOf(e.Moved)); + testing.expect(MovedEvent == @typeOf(e.Moved)); } ///Compares two of any type for equality. Containers are compared on a field-by-field basis, @@ -509,19 +510,19 @@ test "std.meta.eql" { const u_2 = U{ .s = s_1 }; const u_3 = U{ .f = 24 }; - debug.assert(eql(s_1, s_3)); - debug.assert(eql(&s_1, &s_1)); - debug.assert(!eql(&s_1, &s_3)); - debug.assert(eql(u_1, u_3)); - debug.assert(!eql(u_1, u_2)); + testing.expect(eql(s_1, s_3)); + testing.expect(eql(&s_1, &s_1)); + testing.expect(!eql(&s_1, &s_3)); + testing.expect(eql(u_1, u_3)); + testing.expect(!eql(u_1, u_2)); var a1 = "abcdef"; var a2 = "abcdef"; var a3 = "ghijkl"; - debug.assert(eql(a1, a2)); - debug.assert(!eql(a1, a3)); - debug.assert(!eql(a1[0..], a2[0..])); + testing.expect(eql(a1, a2)); + testing.expect(!eql(a1, a3)); + testing.expect(!eql(a1[0..], a2[0..])); const EU = struct { fn tst(err: bool) !u8 { @@ -530,9 +531,9 @@ test "std.meta.eql" { } }; - debug.assert(eql(EU.tst(true), EU.tst(true))); - debug.assert(eql(EU.tst(false), EU.tst(false))); - debug.assert(!eql(EU.tst(false), EU.tst(true))); + testing.expect(eql(EU.tst(true), EU.tst(true))); + testing.expect(eql(EU.tst(false), EU.tst(false))); + testing.expect(!eql(EU.tst(false), EU.tst(true))); } test "intToEnum with error return" { @@ -546,9 +547,9 @@ test "intToEnum with error return" { var zero: u8 = 0; var one: u16 = 1; - debug.assert(intToEnum(E1, zero) catch unreachable == E1.A); - debug.assert(intToEnum(E2, one) catch unreachable == E2.B); - debug.assertError(intToEnum(E1, one), error.InvalidEnumTag); + testing.expect(intToEnum(E1, zero) catch unreachable == E1.A); + testing.expect(intToEnum(E2, one) catch unreachable == E2.B); + testing.expectError(error.InvalidEnumTag, intToEnum(E1, one)); } pub const IntToEnumError = error{InvalidEnumTag}; diff --git a/std/meta/trait.zig b/std/meta/trait.zig index c9d9e971c9..7fca5f4dcd 100644 --- a/std/meta/trait.zig +++ b/std/meta/trait.zig @@ -2,6 +2,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const mem = std.mem; const debug = std.debug; +const testing = std.testing; const warn = debug.warn; const meta = @import("index.zig"); @@ -50,8 +51,8 @@ test "std.meta.trait.multiTrait" { hasField("x"), hasField("y"), }); - debug.assert(isVector(Vector2)); - debug.assert(!isVector(u8)); + testing.expect(isVector(Vector2)); + testing.expect(!isVector(u8)); } /// @@ -85,12 +86,12 @@ test "std.meta.trait.hasDef" { const value = u8(16); }; - debug.assert(hasDef("value")(TestStruct)); - debug.assert(!hasDef("value")(TestStructFail)); - debug.assert(!hasDef("value")(*TestStruct)); - debug.assert(!hasDef("value")(**TestStructFail)); - debug.assert(!hasDef("x")(TestStruct)); - debug.assert(!hasDef("value")(u8)); + testing.expect(hasDef("value")(TestStruct)); + testing.expect(!hasDef("value")(TestStructFail)); + testing.expect(!hasDef("value")(*TestStruct)); + testing.expect(!hasDef("value")(**TestStructFail)); + testing.expect(!hasDef("x")(TestStruct)); + testing.expect(!hasDef("value")(u8)); } /// @@ -111,9 +112,9 @@ test "std.meta.trait.hasFn" { pub fn useless() void {} }; - debug.assert(hasFn("useless")(TestStruct)); - debug.assert(!hasFn("append")(TestStruct)); - debug.assert(!hasFn("useless")(u8)); + testing.expect(hasFn("useless")(TestStruct)); + testing.expect(!hasFn("append")(TestStruct)); + testing.expect(!hasFn("useless")(u8)); } /// @@ -143,11 +144,11 @@ test "std.meta.trait.hasField" { value: u32, }; - debug.assert(hasField("value")(TestStruct)); - debug.assert(!hasField("value")(*TestStruct)); - debug.assert(!hasField("x")(TestStruct)); - debug.assert(!hasField("x")(**TestStruct)); - debug.assert(!hasField("value")(u8)); + testing.expect(hasField("value")(TestStruct)); + testing.expect(!hasField("value")(*TestStruct)); + testing.expect(!hasField("x")(TestStruct)); + testing.expect(!hasField("x")(**TestStruct)); + testing.expect(!hasField("value")(u8)); } /// @@ -161,11 +162,11 @@ pub fn is(comptime id: builtin.TypeId) TraitFn { } test "std.meta.trait.is" { - debug.assert(is(builtin.TypeId.Int)(u8)); - debug.assert(!is(builtin.TypeId.Int)(f32)); - debug.assert(is(builtin.TypeId.Pointer)(*u8)); - debug.assert(is(builtin.TypeId.Void)(void)); - debug.assert(!is(builtin.TypeId.Optional)(anyerror)); + testing.expect(is(builtin.TypeId.Int)(u8)); + testing.expect(!is(builtin.TypeId.Int)(f32)); + testing.expect(is(builtin.TypeId.Pointer)(*u8)); + testing.expect(is(builtin.TypeId.Void)(void)); + testing.expect(!is(builtin.TypeId.Optional)(anyerror)); } /// @@ -180,9 +181,9 @@ pub fn isPtrTo(comptime id: builtin.TypeId) TraitFn { } test "std.meta.trait.isPtrTo" { - debug.assert(!isPtrTo(builtin.TypeId.Struct)(struct {})); - debug.assert(isPtrTo(builtin.TypeId.Struct)(*struct {})); - debug.assert(!isPtrTo(builtin.TypeId.Struct)(**struct {})); + testing.expect(!isPtrTo(builtin.TypeId.Struct)(struct {})); + testing.expect(isPtrTo(builtin.TypeId.Struct)(*struct {})); + testing.expect(!isPtrTo(builtin.TypeId.Struct)(**struct {})); } ///////////Strait trait Fns @@ -205,9 +206,9 @@ test "std.meta.trait.isExtern" { const TestExStruct = extern struct {}; const TestStruct = struct {}; - debug.assert(isExtern(TestExStruct)); - debug.assert(!isExtern(TestStruct)); - debug.assert(!isExtern(u8)); + testing.expect(isExtern(TestExStruct)); + testing.expect(!isExtern(TestStruct)); + testing.expect(!isExtern(u8)); } /// @@ -226,9 +227,9 @@ test "std.meta.trait.isPacked" { const TestPStruct = packed struct {}; const TestStruct = struct {}; - debug.assert(isPacked(TestPStruct)); - debug.assert(!isPacked(TestStruct)); - debug.assert(!isPacked(u8)); + testing.expect(isPacked(TestPStruct)); + testing.expect(!isPacked(TestStruct)); + testing.expect(!isPacked(u8)); } /// @@ -240,10 +241,10 @@ pub fn isUnsignedInt(comptime T: type) bool { } test "isUnsignedInt" { - debug.assert(isUnsignedInt(u32) == true); - debug.assert(isUnsignedInt(comptime_int) == false); - debug.assert(isUnsignedInt(i64) == false); - debug.assert(isUnsignedInt(f64) == false); + testing.expect(isUnsignedInt(u32) == true); + testing.expect(isUnsignedInt(comptime_int) == false); + testing.expect(isUnsignedInt(i64) == false); + testing.expect(isUnsignedInt(f64) == false); } /// @@ -256,10 +257,10 @@ pub fn isSignedInt(comptime T: type) bool { } test "isSignedInt" { - debug.assert(isSignedInt(u32) == false); - debug.assert(isSignedInt(comptime_int) == true); - debug.assert(isSignedInt(i64) == true); - debug.assert(isSignedInt(f64) == false); + testing.expect(isSignedInt(u32) == false); + testing.expect(isSignedInt(comptime_int) == true); + testing.expect(isSignedInt(i64) == true); + testing.expect(isSignedInt(f64) == false); } /// @@ -273,9 +274,9 @@ pub fn isSingleItemPtr(comptime T: type) bool { test "std.meta.trait.isSingleItemPtr" { const array = []u8{0} ** 10; - debug.assert(isSingleItemPtr(@typeOf(&array[0]))); - debug.assert(!isSingleItemPtr(@typeOf(array))); - debug.assert(!isSingleItemPtr(@typeOf(array[0..1]))); + testing.expect(isSingleItemPtr(@typeOf(&array[0]))); + testing.expect(!isSingleItemPtr(@typeOf(array))); + testing.expect(!isSingleItemPtr(@typeOf(array[0..1]))); } /// @@ -290,9 +291,9 @@ pub fn isManyItemPtr(comptime T: type) bool { test "std.meta.trait.isManyItemPtr" { const array = []u8{0} ** 10; const mip = @ptrCast([*]const u8, &array[0]); - debug.assert(isManyItemPtr(@typeOf(mip))); - debug.assert(!isManyItemPtr(@typeOf(array))); - debug.assert(!isManyItemPtr(@typeOf(array[0..1]))); + testing.expect(isManyItemPtr(@typeOf(mip))); + testing.expect(!isManyItemPtr(@typeOf(array))); + testing.expect(!isManyItemPtr(@typeOf(array[0..1]))); } /// @@ -306,9 +307,9 @@ pub fn isSlice(comptime T: type) bool { test "std.meta.trait.isSlice" { const array = []u8{0} ** 10; - debug.assert(isSlice(@typeOf(array[0..]))); - debug.assert(!isSlice(@typeOf(array))); - debug.assert(!isSlice(@typeOf(&array[0]))); + testing.expect(isSlice(@typeOf(array[0..]))); + testing.expect(!isSlice(@typeOf(array))); + testing.expect(!isSlice(@typeOf(&array[0]))); } /// @@ -328,10 +329,10 @@ test "std.meta.trait.isIndexable" { const array = []u8{0} ** 10; const slice = array[0..]; - debug.assert(isIndexable(@typeOf(array))); - debug.assert(isIndexable(@typeOf(&array))); - debug.assert(isIndexable(@typeOf(slice))); - debug.assert(!isIndexable(meta.Child(@typeOf(slice)))); + testing.expect(isIndexable(@typeOf(array))); + testing.expect(isIndexable(@typeOf(&array))); + testing.expect(isIndexable(@typeOf(slice))); + testing.expect(!isIndexable(meta.Child(@typeOf(slice)))); } /// @@ -347,13 +348,13 @@ test "std.meta.trait.isNumber" { number: u8, }; - debug.assert(isNumber(u32)); - debug.assert(isNumber(f32)); - debug.assert(isNumber(u64)); - debug.assert(isNumber(@typeOf(102))); - debug.assert(isNumber(@typeOf(102.123))); - debug.assert(!isNumber([]u8)); - debug.assert(!isNumber(NotANumber)); + testing.expect(isNumber(u32)); + testing.expect(isNumber(f32)); + testing.expect(isNumber(u64)); + testing.expect(isNumber(@typeOf(102))); + testing.expect(isNumber(@typeOf(102.123))); + testing.expect(!isNumber([]u8)); + testing.expect(!isNumber(NotANumber)); } /// @@ -366,10 +367,10 @@ pub fn isConstPtr(comptime T: type) bool { test "std.meta.trait.isConstPtr" { var t = u8(0); const c = u8(0); - debug.assert(isConstPtr(*const @typeOf(t))); - debug.assert(isConstPtr(@typeOf(&c))); - debug.assert(!isConstPtr(*@typeOf(t))); - debug.assert(!isConstPtr(@typeOf(6))); + testing.expect(isConstPtr(*const @typeOf(t))); + testing.expect(isConstPtr(@typeOf(&c))); + testing.expect(!isConstPtr(*@typeOf(t))); + testing.expect(!isConstPtr(@typeOf(6))); } /// @@ -393,8 +394,8 @@ test "std.meta.trait.isContainer" { B, }; - debug.assert(isContainer(TestStruct)); - debug.assert(isContainer(TestUnion)); - debug.assert(isContainer(TestEnum)); - debug.assert(!isContainer(u8)); + testing.expect(isContainer(TestStruct)); + testing.expect(isContainer(TestUnion)); + testing.expect(isContainer(TestEnum)); + testing.expect(!isContainer(u8)); } diff --git a/std/mutex.zig b/std/mutex.zig index 54173fa38a..a13b1c06c7 100644 --- a/std/mutex.zig +++ b/std/mutex.zig @@ -2,7 +2,7 @@ const std = @import("index.zig"); const builtin = @import("builtin"); const AtomicOrder = builtin.AtomicOrder; const AtomicRmwOp = builtin.AtomicRmwOp; -const assert = std.debug.assert; +const testing = std.testing; const SpinLock = std.SpinLock; const linux = std.os.linux; const windows = std.os.windows; @@ -149,7 +149,7 @@ test "std.Mutex" { if (builtin.single_threaded) { worker(&context); - std.debug.assertOrPanic(context.data == TestContext.incr_count); + testing.expect(context.data == TestContext.incr_count); } else { const thread_count = 10; var threads: [thread_count]*std.os.Thread = undefined; @@ -159,7 +159,7 @@ test "std.Mutex" { for (threads) |t| t.wait(); - std.debug.assertOrPanic(context.data == thread_count * TestContext.incr_count); + testing.expect(context.data == thread_count * TestContext.incr_count); } } diff --git a/std/os/child_process.zig b/std/os/child_process.zig index 6635b76976..1da0b3492b 100644 --- a/std/os/child_process.zig +++ b/std/os/child_process.zig @@ -7,7 +7,6 @@ const posix = os.posix; const windows = os.windows; const mem = std.mem; const debug = std.debug; -const assert = debug.assert; const BufMap = std.BufMap; const Buffer = std.Buffer; const builtin = @import("builtin"); diff --git a/std/os/index.zig b/std/os/index.zig index 8e9876c36b..f52c12c2b6 100644 --- a/std/os/index.zig +++ b/std/os/index.zig @@ -91,6 +91,7 @@ pub const GetAppDataDirError = @import("get_app_data_dir.zig").GetAppDataDirErro const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const c = std.c; @@ -172,7 +173,7 @@ test "os.getRandomBytes" { try getRandomBytes(buf_b[0..]); // Check if random (not 100% conclusive) - assert(!mem.eql(u8, buf_a, buf_b)); + testing.expect(!mem.eql(u8, buf_a, buf_b)); } /// Raises a signal in the current kernel thread, ending its execution. @@ -828,7 +829,7 @@ pub fn getEnvVarOwned(allocator: *mem.Allocator, key: []const u8) GetEnvVarOwned test "os.getEnvVarOwned" { var ga = debug.global_allocator; - debug.assertError(getEnvVarOwned(ga, "BADENV"), error.EnvironmentVariableNotFound); + testing.expectError(error.EnvironmentVariableNotFound, getEnvVarOwned(ga, "BADENV")); } /// Caller must free the returned memory. @@ -2219,9 +2220,9 @@ fn testWindowsCmdLine(input_cmd_line: [*]const u8, expected_args: []const []cons var it = ArgIteratorWindows.initWithCmdLine(input_cmd_line); for (expected_args) |expected_arg| { const arg = it.next(debug.global_allocator).? catch unreachable; - assert(mem.eql(u8, arg, expected_arg)); + testing.expectEqualSlices(u8, expected_arg, arg); } - assert(it.next(debug.global_allocator) == null); + testing.expect(it.next(debug.global_allocator) == null); } // TODO make this a build variable that you can set diff --git a/std/os/linux/test.zig b/std/os/linux/test.zig index 4de26012c7..40fb7823d2 100644 --- a/std/os/linux/test.zig +++ b/std/os/linux/test.zig @@ -1,19 +1,19 @@ const std = @import("../../index.zig"); const builtin = @import("builtin"); const linux = std.os.linux; -const assert = std.debug.assert; +const expect = std.testing.expect; test "getpid" { - assert(linux.getpid() != 0); + expect(linux.getpid() != 0); } test "timer" { const epoll_fd = linux.epoll_create(); var err = linux.getErrno(epoll_fd); - assert(err == 0); + expect(err == 0); const timer_fd = linux.timerfd_create(linux.CLOCK_MONOTONIC, 0); - assert(linux.getErrno(timer_fd) == 0); + expect(linux.getErrno(timer_fd) == 0); const time_interval = linux.timespec{ .tv_sec = 0, @@ -26,7 +26,7 @@ test "timer" { }; err = linux.timerfd_settime(@intCast(i32, timer_fd), 0, &new_time, null); - assert(err == 0); + expect(err == 0); var event = linux.epoll_event{ .events = linux.EPOLLIN | linux.EPOLLOUT | linux.EPOLLET, @@ -34,7 +34,7 @@ test "timer" { }; err = linux.epoll_ctl(@intCast(i32, epoll_fd), linux.EPOLL_CTL_ADD, @intCast(i32, timer_fd), &event); - assert(err == 0); + expect(err == 0); const events_one: linux.epoll_event = undefined; var events = []linux.epoll_event{events_one} ** 8; diff --git a/std/os/path.zig b/std/os/path.zig index 266a77b97c..5ba345fceb 100644 --- a/std/os/path.zig +++ b/std/os/path.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const Os = builtin.Os; const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const mem = std.mem; const fmt = std.fmt; const Allocator = mem.Allocator; @@ -94,14 +95,14 @@ fn testJoinWindows(paths: []const []const u8, expected: []const u8) void { var buf: [1024]u8 = undefined; const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; const actual = joinWindows(a, paths) catch @panic("fail"); - debug.assertOrPanic(mem.eql(u8, actual, expected)); + testing.expectEqualSlices(u8, expected, actual); } fn testJoinPosix(paths: []const []const u8, expected: []const u8) void { var buf: [1024]u8 = undefined; const a = &std.heap.FixedBufferAllocator.init(&buf).allocator; const actual = joinPosix(a, paths) catch @panic("fail"); - debug.assertOrPanic(mem.eql(u8, actual, expected)); + testing.expectEqualSlices(u8, expected, actual); } test "os.path.join" { @@ -193,11 +194,11 @@ test "os.path.isAbsolutePosix" { } fn testIsAbsoluteWindows(path: []const u8, expected_result: bool) void { - assert(isAbsoluteWindows(path) == expected_result); + testing.expectEqual(expected_result, isAbsoluteWindows(path)); } fn testIsAbsolutePosix(path: []const u8, expected_result: bool) void { - assert(isAbsolutePosix(path) == expected_result); + testing.expectEqual(expected_result, isAbsolutePosix(path)); } pub const WindowsPath = struct { @@ -281,33 +282,33 @@ pub fn windowsParsePath(path: []const u8) WindowsPath { test "os.path.windowsParsePath" { { const parsed = windowsParsePath("//a/b"); - assert(parsed.is_abs); - assert(parsed.kind == WindowsPath.Kind.NetworkShare); - assert(mem.eql(u8, parsed.disk_designator, "//a/b")); + testing.expect(parsed.is_abs); + testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); + testing.expect(mem.eql(u8, parsed.disk_designator, "//a/b")); } { const parsed = windowsParsePath("\\\\a\\b"); - assert(parsed.is_abs); - assert(parsed.kind == WindowsPath.Kind.NetworkShare); - assert(mem.eql(u8, parsed.disk_designator, "\\\\a\\b")); + testing.expect(parsed.is_abs); + testing.expect(parsed.kind == WindowsPath.Kind.NetworkShare); + testing.expect(mem.eql(u8, parsed.disk_designator, "\\\\a\\b")); } { const parsed = windowsParsePath("\\\\a\\"); - assert(!parsed.is_abs); - assert(parsed.kind == WindowsPath.Kind.None); - assert(mem.eql(u8, parsed.disk_designator, "")); + testing.expect(!parsed.is_abs); + testing.expect(parsed.kind == WindowsPath.Kind.None); + testing.expect(mem.eql(u8, parsed.disk_designator, "")); } { const parsed = windowsParsePath("/usr/local"); - assert(parsed.is_abs); - assert(parsed.kind == WindowsPath.Kind.None); - assert(mem.eql(u8, parsed.disk_designator, "")); + testing.expect(parsed.is_abs); + testing.expect(parsed.kind == WindowsPath.Kind.None); + testing.expect(mem.eql(u8, parsed.disk_designator, "")); } { const parsed = windowsParsePath("c:../"); - assert(!parsed.is_abs); - assert(parsed.kind == WindowsPath.Kind.Drive); - assert(mem.eql(u8, parsed.disk_designator, "c:")); + testing.expect(!parsed.is_abs); + testing.expect(parsed.kind == WindowsPath.Kind.Drive); + testing.expect(mem.eql(u8, parsed.disk_designator, "c:")); } } @@ -642,10 +643,10 @@ test "os.path.resolve" { if (windowsParsePath(cwd).kind == WindowsPath.Kind.Drive) { cwd[0] = asciiUpper(cwd[0]); } - assert(mem.eql(u8, testResolveWindows([][]const u8{"."}), cwd)); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{"."}), cwd)); } else { - assert(mem.eql(u8, testResolvePosix([][]const u8{ "a/b/c/", "../../.." }), cwd)); - assert(mem.eql(u8, testResolvePosix([][]const u8{"."}), cwd)); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "a/b/c/", "../../.." }), cwd)); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{"."}), cwd)); } } @@ -662,7 +663,7 @@ test "os.path.resolveWindows" { if (parsed_cwd.kind == WindowsPath.Kind.Drive) { expected[0] = asciiUpper(parsed_cwd.disk_designator[0]); } - assert(mem.eql(u8, result, expected)); + testing.expect(mem.eql(u8, result, expected)); } { const result = testResolveWindows([][]const u8{ "usr/local", "lib\\zig" }); @@ -673,36 +674,36 @@ test "os.path.resolveWindows" { if (parsed_cwd.kind == WindowsPath.Kind.Drive) { expected[0] = asciiUpper(parsed_cwd.disk_designator[0]); } - assert(mem.eql(u8, result, expected)); + testing.expect(mem.eql(u8, result, expected)); } } - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }), "C:\\hi\\ok")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }), "C:\\blah\\a")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }), "C:\\blah\\a")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }), "D:\\e.exe")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "c:/some/file" }), "C:\\some\\file")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "d:/ignore", "d:some/dir//" }), "D:\\ignore\\some\\dir")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "//server/share", "..", "relative\\" }), "\\\\server\\share\\relative")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//" }), "C:\\")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//dir" }), "C:\\dir")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server/share" }), "\\\\server\\share\\")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server//share" }), "\\\\server\\share\\")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "///some//dir" }), "C:\\some\\dir")); - assert(mem.eql(u8, testResolveWindows([][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }), "C:\\foo\\tmp.3\\cycles\\root.js")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:\\a\\b\\c", "/hi", "ok" }), "C:\\hi\\ok")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "c:../a" }), "C:\\blah\\a")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/blah\\blah", "d:/games", "C:../a" }), "C:\\blah\\a")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "d:\\a/b\\c/d", "\\e.exe" }), "D:\\e.exe")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/ignore", "c:/some/file" }), "C:\\some\\file")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "d:/ignore", "d:some/dir//" }), "D:\\ignore\\some\\dir")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "//server/share", "..", "relative\\" }), "\\\\server\\share\\relative")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//" }), "C:\\")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//dir" }), "C:\\dir")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server/share" }), "\\\\server\\share\\")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "//server//share" }), "\\\\server\\share\\")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "c:/", "///some//dir" }), "C:\\some\\dir")); + testing.expect(mem.eql(u8, testResolveWindows([][]const u8{ "C:\\foo\\tmp.3\\", "..\\tmp.3\\cycles\\root.js" }), "C:\\foo\\tmp.3\\cycles\\root.js")); } test "os.path.resolvePosix" { - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c" }), "/a/b/c")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c", "//d", "e///" }), "/d/e")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b/c", "..", "../" }), "/a")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/", "..", ".." }), "/")); - assert(mem.eql(u8, testResolvePosix([][]const u8{"/a/b/c/"}), "/a/b/c")); - - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "../", "file/" }), "/var/file")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "/../", "file/" }), "/file")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/some/dir", ".", "/absolute/" }), "/absolute")); - assert(mem.eql(u8, testResolvePosix([][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }), "/foo/tmp.3/cycles/root.js")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c" }), "/a/b/c")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b", "c", "//d", "e///" }), "/d/e")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/a/b/c", "..", "../" }), "/a")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/", "..", ".." }), "/")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{"/a/b/c/"}), "/a/b/c")); + + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "../", "file/" }), "/var/file")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/var/lib", "/../", "file/" }), "/file")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/some/dir", ".", "/absolute/" }), "/absolute")); + testing.expect(mem.eql(u8, testResolvePosix([][]const u8{ "/foo/tmp.3/", "../tmp.3/cycles/root.js" }), "/foo/tmp.3/cycles/root.js")); } fn testResolveWindows(paths: []const []const u8) []u8 { @@ -833,17 +834,17 @@ test "os.path.dirnameWindows" { fn testDirnamePosix(input: []const u8, expected_output: ?[]const u8) void { if (dirnamePosix(input)) |output| { - assert(mem.eql(u8, output, expected_output.?)); + testing.expect(mem.eql(u8, output, expected_output.?)); } else { - assert(expected_output == null); + testing.expect(expected_output == null); } } fn testDirnameWindows(input: []const u8, expected_output: ?[]const u8) void { if (dirnameWindows(input)) |output| { - assert(mem.eql(u8, output, expected_output.?)); + testing.expect(mem.eql(u8, output, expected_output.?)); } else { - assert(expected_output == null); + testing.expect(expected_output == null); } } @@ -948,15 +949,15 @@ test "os.path.basename" { } fn testBasename(input: []const u8, expected_output: []const u8) void { - assert(mem.eql(u8, basename(input), expected_output)); + testing.expectEqualSlices(u8, expected_output, basename(input)); } fn testBasenamePosix(input: []const u8, expected_output: []const u8) void { - assert(mem.eql(u8, basenamePosix(input), expected_output)); + testing.expectEqualSlices(u8, expected_output, basenamePosix(input)); } fn testBasenameWindows(input: []const u8, expected_output: []const u8) void { - assert(mem.eql(u8, basenameWindows(input), expected_output)); + testing.expectEqualSlices(u8, expected_output, basenameWindows(input)); } /// Returns the relative path from `from` to `to`. If `from` and `to` each @@ -1131,12 +1132,12 @@ test "os.path.relative" { fn testRelativePosix(from: []const u8, to: []const u8, expected_output: []const u8) void { const result = relativePosix(debug.global_allocator, from, to) catch unreachable; - assert(mem.eql(u8, result, expected_output)); + testing.expectEqualSlices(u8, expected_output, result); } fn testRelativeWindows(from: []const u8, to: []const u8, expected_output: []const u8) void { const result = relativeWindows(debug.global_allocator, from, to) catch unreachable; - assert(mem.eql(u8, result, expected_output)); + testing.expectEqualSlices(u8, expected_output, result); } pub const RealError = error{ @@ -1283,5 +1284,5 @@ pub fn realAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 { test "os.path.real" { // at least call it so it gets compiled var buf: [os.MAX_PATH_BYTES]u8 = undefined; - std.debug.assertError(real(&buf, "definitely_bogus_does_not_exist1234"), error.FileNotFound); + testing.expectError(error.FileNotFound, real(&buf, "definitely_bogus_does_not_exist1234")); } diff --git a/std/os/test.zig b/std/os/test.zig index bd9148d1b1..b2a4d96651 100644 --- a/std/os/test.zig +++ b/std/os/test.zig @@ -1,6 +1,6 @@ const std = @import("../index.zig"); const os = std.os; -const assert = std.debug.assert; +const expect = std.testing.expect; const io = std.io; const mem = std.mem; @@ -18,7 +18,7 @@ test "makePath, put some files in it, deleteTree" { if (os.Dir.open(a, "os_test_tmp")) |dir| { @panic("expected error"); } else |err| { - assert(err == error.FileNotFound); + expect(err == error.FileNotFound); } } @@ -27,7 +27,7 @@ test "access file" { if (os.File.access("os_test_tmp" ++ os.path.sep_str ++ "file.txt")) |ok| { @panic("expected error"); } else |err| { - assert(err == error.FileNotFound); + expect(err == error.FileNotFound); } try io.writeFile("os_test_tmp" ++ os.path.sep_str ++ "file.txt", ""); @@ -47,9 +47,9 @@ test "std.os.Thread.getCurrentId" { const thread_id = thread.handle(); thread.wait(); switch (builtin.os) { - builtin.Os.windows => assert(os.Thread.getCurrentId() != thread_current_id), + builtin.Os.windows => expect(os.Thread.getCurrentId() != thread_current_id), else => { - assert(thread_current_id == thread_id); + expect(thread_current_id == thread_id); }, } } @@ -69,7 +69,7 @@ test "spawn threads" { thread3.wait(); thread4.wait(); - assert(shared_ctx == 4); + expect(shared_ctx == 4); } fn start1(ctx: void) u8 { @@ -83,7 +83,7 @@ fn start2(ctx: *i32) u8 { test "cpu count" { const cpu_count = try std.os.cpuCount(a); - assert(cpu_count >= 1); + expect(cpu_count >= 1); } test "AtomicFile" { @@ -101,7 +101,7 @@ test "AtomicFile" { try af.finish(); } const content = try io.readFileAlloc(allocator, test_out_file); - assert(mem.eql(u8, content, test_content)); + expect(mem.eql(u8, content, test_content)); try os.deleteFile(test_out_file); } diff --git a/std/os/time.zig b/std/os/time.zig index 4b11a69376..638fb41ff1 100644 --- a/std/os/time.zig +++ b/std/os/time.zig @@ -2,6 +2,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const Os = builtin.Os; const debug = std.debug; +const testing = std.testing; const windows = std.os.windows; const linux = std.os.linux; @@ -270,7 +271,7 @@ test "os.time.timestamp" { sleep(ns_per_ms); const time_1 = milliTimestamp(); const interval = time_1 - time_0; - debug.assert(interval > 0 and interval < margin); + testing.expect(interval > 0 and interval < margin); } test "os.time.Timer" { @@ -280,11 +281,11 @@ test "os.time.Timer" { var timer = try Timer.start(); sleep(10 * ns_per_ms); const time_0 = timer.read(); - debug.assert(time_0 > 0 and time_0 < margin); + testing.expect(time_0 > 0 and time_0 < margin); const time_1 = timer.lap(); - debug.assert(time_1 >= time_0); + testing.expect(time_1 >= time_0); timer.reset(); - debug.assert(timer.read() < time_1); + testing.expect(timer.read() < time_1); } diff --git a/std/rand/index.zig b/std/rand/index.zig index c335063e64..12dd763aee 100644 --- a/std/rand/index.zig +++ b/std/rand/index.zig @@ -17,6 +17,7 @@ const std = @import("../index.zig"); const builtin = @import("builtin"); const assert = std.debug.assert; +const expect = std.testing.expect; const mem = std.mem; const math = std.math; const ziggurat = @import("ziggurat.zig"); @@ -316,43 +317,43 @@ test "Random int" { fn testRandomInt() void { var r = SequentialPrng.init(); - assert(r.random.int(u0) == 0); + expect(r.random.int(u0) == 0); r.next_value = 0; - assert(r.random.int(u1) == 0); - assert(r.random.int(u1) == 1); - assert(r.random.int(u2) == 2); - assert(r.random.int(u2) == 3); - assert(r.random.int(u2) == 0); + expect(r.random.int(u1) == 0); + expect(r.random.int(u1) == 1); + expect(r.random.int(u2) == 2); + expect(r.random.int(u2) == 3); + expect(r.random.int(u2) == 0); r.next_value = 0xff; - assert(r.random.int(u8) == 0xff); + expect(r.random.int(u8) == 0xff); r.next_value = 0x11; - assert(r.random.int(u8) == 0x11); + expect(r.random.int(u8) == 0x11); r.next_value = 0xff; - assert(r.random.int(u32) == 0xffffffff); + expect(r.random.int(u32) == 0xffffffff); r.next_value = 0x11; - assert(r.random.int(u32) == 0x11111111); + expect(r.random.int(u32) == 0x11111111); r.next_value = 0xff; - assert(r.random.int(i32) == -1); + expect(r.random.int(i32) == -1); r.next_value = 0x11; - assert(r.random.int(i32) == 0x11111111); + expect(r.random.int(i32) == 0x11111111); r.next_value = 0xff; - assert(r.random.int(i8) == -1); + expect(r.random.int(i8) == -1); r.next_value = 0x11; - assert(r.random.int(i8) == 0x11); + expect(r.random.int(i8) == 0x11); r.next_value = 0xff; - assert(r.random.int(u33) == 0x1ffffffff); + expect(r.random.int(u33) == 0x1ffffffff); r.next_value = 0xff; - assert(r.random.int(i1) == -1); + expect(r.random.int(i1) == -1); r.next_value = 0xff; - assert(r.random.int(i2) == -1); + expect(r.random.int(i2) == -1); r.next_value = 0xff; - assert(r.random.int(i33) == -1); + expect(r.random.int(i33) == -1); } test "Random boolean" { @@ -361,10 +362,10 @@ test "Random boolean" { } fn testRandomBoolean() void { var r = SequentialPrng.init(); - assert(r.random.boolean() == false); - assert(r.random.boolean() == true); - assert(r.random.boolean() == false); - assert(r.random.boolean() == true); + expect(r.random.boolean() == false); + expect(r.random.boolean() == true); + expect(r.random.boolean() == false); + expect(r.random.boolean() == true); } test "Random intLessThan" { @@ -375,36 +376,36 @@ test "Random intLessThan" { fn testRandomIntLessThan() void { var r = SequentialPrng.init(); r.next_value = 0xff; - assert(r.random.uintLessThan(u8, 4) == 3); - assert(r.next_value == 0); - assert(r.random.uintLessThan(u8, 4) == 0); - assert(r.next_value == 1); + expect(r.random.uintLessThan(u8, 4) == 3); + expect(r.next_value == 0); + expect(r.random.uintLessThan(u8, 4) == 0); + expect(r.next_value == 1); r.next_value = 0; - assert(r.random.uintLessThan(u64, 32) == 0); + expect(r.random.uintLessThan(u64, 32) == 0); // trigger the bias rejection code path r.next_value = 0; - assert(r.random.uintLessThan(u8, 3) == 0); + expect(r.random.uintLessThan(u8, 3) == 0); // verify we incremented twice - assert(r.next_value == 2); + expect(r.next_value == 2); r.next_value = 0xff; - assert(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f); + expect(r.random.intRangeLessThan(u8, 0, 0x80) == 0x7f); r.next_value = 0xff; - assert(r.random.intRangeLessThan(u8, 0x7f, 0xff) == 0xfe); + expect(r.random.intRangeLessThan(u8, 0x7f, 0xff) == 0xfe); r.next_value = 0xff; - assert(r.random.intRangeLessThan(i8, 0, 0x40) == 0x3f); + expect(r.random.intRangeLessThan(i8, 0, 0x40) == 0x3f); r.next_value = 0xff; - assert(r.random.intRangeLessThan(i8, -0x40, 0x40) == 0x3f); + expect(r.random.intRangeLessThan(i8, -0x40, 0x40) == 0x3f); r.next_value = 0xff; - assert(r.random.intRangeLessThan(i8, -0x80, 0) == -1); + expect(r.random.intRangeLessThan(i8, -0x80, 0) == -1); r.next_value = 0xff; - assert(r.random.intRangeLessThan(i3, -4, 0) == -1); + expect(r.random.intRangeLessThan(i3, -4, 0) == -1); r.next_value = 0xff; - assert(r.random.intRangeLessThan(i3, -2, 2) == 1); + expect(r.random.intRangeLessThan(i3, -2, 2) == 1); } test "Random intAtMost" { @@ -415,34 +416,34 @@ test "Random intAtMost" { fn testRandomIntAtMost() void { var r = SequentialPrng.init(); r.next_value = 0xff; - assert(r.random.uintAtMost(u8, 3) == 3); - assert(r.next_value == 0); - assert(r.random.uintAtMost(u8, 3) == 0); + expect(r.random.uintAtMost(u8, 3) == 3); + expect(r.next_value == 0); + expect(r.random.uintAtMost(u8, 3) == 0); // trigger the bias rejection code path r.next_value = 0; - assert(r.random.uintAtMost(u8, 2) == 0); + expect(r.random.uintAtMost(u8, 2) == 0); // verify we incremented twice - assert(r.next_value == 2); + expect(r.next_value == 2); r.next_value = 0xff; - assert(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); + expect(r.random.intRangeAtMost(u8, 0, 0x7f) == 0x7f); r.next_value = 0xff; - assert(r.random.intRangeAtMost(u8, 0x7f, 0xfe) == 0xfe); + expect(r.random.intRangeAtMost(u8, 0x7f, 0xfe) == 0xfe); r.next_value = 0xff; - assert(r.random.intRangeAtMost(i8, 0, 0x3f) == 0x3f); + expect(r.random.intRangeAtMost(i8, 0, 0x3f) == 0x3f); r.next_value = 0xff; - assert(r.random.intRangeAtMost(i8, -0x40, 0x3f) == 0x3f); + expect(r.random.intRangeAtMost(i8, -0x40, 0x3f) == 0x3f); r.next_value = 0xff; - assert(r.random.intRangeAtMost(i8, -0x80, -1) == -1); + expect(r.random.intRangeAtMost(i8, -0x80, -1) == -1); r.next_value = 0xff; - assert(r.random.intRangeAtMost(i3, -4, -1) == -1); + expect(r.random.intRangeAtMost(i3, -4, -1) == -1); r.next_value = 0xff; - assert(r.random.intRangeAtMost(i3, -2, 1) == 1); + expect(r.random.intRangeAtMost(i3, -2, 1) == 1); - assert(r.random.uintAtMost(u0, 0) == 0); + expect(r.random.uintAtMost(u0, 0) == 0); } test "Random Biased" { @@ -450,30 +451,30 @@ test "Random Biased" { // 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); + expect(r.random.uintLessThanBiased(u1, 1) == 0); + expect(r.random.uintLessThanBiased(u32, 10) < 10); + expect(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); + expect(r.random.uintAtMostBiased(u0, 0) == 0); + expect(r.random.uintAtMostBiased(u1, 0) <= 0); + expect(r.random.uintAtMostBiased(u32, 10) <= 10); + expect(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); + expect(r.random.intRangeLessThanBiased(u1, 0, 1) == 0); + expect(r.random.intRangeLessThanBiased(i1, -1, 0) == -1); + expect(r.random.intRangeLessThanBiased(u32, 10, 20) >= 10); + expect(r.random.intRangeLessThanBiased(i32, 10, 20) >= 10); + expect(r.random.intRangeLessThanBiased(u64, 20, 40) >= 20); + expect(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); + //expect(r.random.intRangeAtMostBiased(u0, 0, 0) == 0); + expect(r.random.intRangeAtMostBiased(u1, 0, 1) >= 0); + expect(r.random.intRangeAtMostBiased(i1, -1, 0) >= -1); + expect(r.random.intRangeAtMostBiased(u32, 10, 20) >= 10); + expect(r.random.intRangeAtMostBiased(i32, 10, 20) >= 10); + expect(r.random.intRangeAtMostBiased(u64, 20, 40) >= 20); + expect(r.random.intRangeAtMostBiased(i64, 20, 40) >= 20); } // Generator to extend 64-bit seed values into longer sequences. @@ -510,7 +511,7 @@ test "splitmix64 sequence" { }; for (seq) |s| { - std.debug.assert(s == r.next()); + expect(s == r.next()); } } @@ -603,7 +604,7 @@ test "pcg sequence" { }; for (seq) |s| { - std.debug.assert(s == r.next()); + expect(s == r.next()); } } @@ -712,7 +713,7 @@ test "xoroshiro sequence" { }; for (seq1) |s| { - std.debug.assert(s == r.next()); + expect(s == r.next()); } r.jump(); @@ -727,7 +728,7 @@ test "xoroshiro sequence" { }; for (seq2) |s| { - std.debug.assert(s == r.next()); + expect(s == r.next()); } } @@ -930,7 +931,7 @@ test "isaac64 sequence" { }; for (seq) |s| { - std.debug.assert(s == r.next()); + expect(s == r.next()); } } @@ -941,12 +942,12 @@ test "Random float" { var i: usize = 0; while (i < 1000) : (i += 1) { const val1 = prng.random.float(f32); - std.debug.assert(val1 >= 0.0); - std.debug.assert(val1 < 1.0); + expect(val1 >= 0.0); + expect(val1 < 1.0); const val2 = prng.random.float(f64); - std.debug.assert(val2 >= 0.0); - std.debug.assert(val2 < 1.0); + expect(val2 >= 0.0); + expect(val2 < 1.0); } } @@ -960,12 +961,12 @@ test "Random shuffle" { while (i < 1000) : (i += 1) { prng.random.shuffle(u8, seq[0..]); seen[seq[0]] = true; - std.debug.assert(sumArray(seq[0..]) == 10); + expect(sumArray(seq[0..]) == 10); } // we should see every entry at the head at least once for (seen) |e| { - std.debug.assert(e == true); + expect(e == true); } } diff --git a/std/rb.zig b/std/rb.zig index beb7f3aae9..27e60dca6a 100644 --- a/std/rb.zig +++ b/std/rb.zig @@ -1,5 +1,6 @@ const std = @import("index.zig"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; // For mem.Compare const Color = enum(u1) { @@ -533,13 +534,13 @@ test "rb" { _ = tree.insert(&ns[8].node); _ = tree.insert(&ns[9].node); tree.remove(&ns[3].node); - assert(tree.insert(&dup.node) == &ns[7].node); + testing.expect(tree.insert(&dup.node) == &ns[7].node); try tree.replace(&ns[7].node, &dup.node); var num: *testNumber = undefined; num = testGetNumber(tree.first().?); while (num.node.next() != null) { - assert(testGetNumber(num.node.next().?).value > num.value); + testing.expect(testGetNumber(num.node.next().?).value > num.value); num = testGetNumber(num.node.next().?); } } diff --git a/std/segmented_list.zig b/std/segmented_list.zig index d786e0becd..26b7fa48f5 100644 --- a/std/segmented_list.zig +++ b/std/segmented_list.zig @@ -1,5 +1,6 @@ const std = @import("index.zig"); const assert = std.debug.assert; +const testing = std.testing; const Allocator = std.mem.Allocator; // Imagine that `fn at(self: *Self, index: usize) &T` is a customer asking for a box @@ -352,14 +353,14 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void { var i: usize = 0; while (i < 100) : (i += 1) { try list.push(@intCast(i32, i + 1)); - assert(list.len == i + 1); + testing.expect(list.len == i + 1); } } { var i: usize = 0; while (i < 100) : (i += 1) { - assert(list.at(i).* == @intCast(i32, i + 1)); + testing.expect(list.at(i).* == @intCast(i32, i + 1)); } } @@ -368,35 +369,35 @@ fn testSegmentedList(comptime prealloc: usize, allocator: *Allocator) !void { var x: i32 = 0; while (it.next()) |item| { x += 1; - assert(item.* == x); + testing.expect(item.* == x); } - assert(x == 100); + testing.expect(x == 100); while (it.prev()) |item| : (x -= 1) { - assert(item.* == x); + testing.expect(item.* == x); } - assert(x == 0); + testing.expect(x == 0); } - assert(list.pop().? == 100); - assert(list.len == 99); + testing.expect(list.pop().? == 100); + testing.expect(list.len == 99); try list.pushMany([]i32{ 1, 2, 3, }); - assert(list.len == 102); - assert(list.pop().? == 3); - assert(list.pop().? == 2); - assert(list.pop().? == 1); - assert(list.len == 99); + testing.expect(list.len == 102); + testing.expect(list.pop().? == 3); + testing.expect(list.pop().? == 2); + testing.expect(list.pop().? == 1); + testing.expect(list.len == 99); try list.pushMany([]const i32{}); - assert(list.len == 99); + testing.expect(list.len == 99); var i: i32 = 99; while (list.pop()) |item| : (i -= 1) { - assert(item == i); + testing.expect(item == i); list.shrinkCapacity(list.len); } } diff --git a/std/sort.zig b/std/sort.zig index f29f51b7cf..86a6724fee 100644 --- a/std/sort.zig +++ b/std/sort.zig @@ -1,5 +1,6 @@ const std = @import("index.zig"); const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; const math = std.math; const builtin = @import("builtin"); @@ -1031,8 +1032,8 @@ fn testStableSort() void { for (cases) |*case| { insertionSort(IdAndValue, (case.*)[0..], cmpByValue); for (case.*) |item, i| { - assert(item.id == expected[i].id); - assert(item.value == expected[i].value); + testing.expect(item.id == expected[i].id); + testing.expect(item.value == expected[i].value); } } } @@ -1077,7 +1078,7 @@ test "std.sort" { const slice = buf[0..case[0].len]; mem.copy(u8, slice, case[0]); sort(u8, slice, asc(u8)); - assert(mem.eql(u8, slice, case[1])); + testing.expect(mem.eql(u8, slice, case[1])); } const i32cases = [][]const []const i32{ @@ -1112,7 +1113,7 @@ test "std.sort" { const slice = buf[0..case[0].len]; mem.copy(i32, slice, case[0]); sort(i32, slice, asc(i32)); - assert(mem.eql(i32, slice, case[1])); + testing.expect(mem.eql(i32, slice, case[1])); } } @@ -1149,7 +1150,7 @@ test "std.sort descending" { const slice = buf[0..case[0].len]; mem.copy(i32, slice, case[0]); sort(i32, slice, desc(i32)); - assert(mem.eql(i32, slice, case[1])); + testing.expect(mem.eql(i32, slice, case[1])); } } @@ -1157,7 +1158,7 @@ test "another sort case" { var arr = []i32{ 5, 3, 1, 2, 4 }; sort(i32, arr[0..], asc(i32)); - assert(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 })); + testing.expect(mem.eql(i32, arr, []i32{ 1, 2, 3, 4, 5 })); } test "sort fuzz testing" { @@ -1185,9 +1186,9 @@ fn fuzzTest(rng: *std.rand.Random) void { var index: usize = 1; while (index < array.len) : (index += 1) { if (array[index].value == array[index - 1].value) { - assert(array[index].id > array[index - 1].id); + testing.expect(array[index].id > array[index - 1].id); } else { - assert(array[index].value > array[index - 1].value); + testing.expect(array[index].value > array[index - 1].value); } } } diff --git a/std/special/compiler_rt/divti3_test.zig b/std/special/compiler_rt/divti3_test.zig index eef5a9b812..e1c1babae7 100644 --- a/std/special/compiler_rt/divti3_test.zig +++ b/std/special/compiler_rt/divti3_test.zig @@ -1,9 +1,9 @@ const __divti3 = @import("divti3.zig").__divti3; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__divti3(a: i128, b: i128, expected: i128) void { const x = __divti3(a, b); - assert(x == expected); + testing.expect(x == expected); } test "divti3" { diff --git a/std/special/compiler_rt/extendXfYf2_test.zig b/std/special/compiler_rt/extendXfYf2_test.zig index 9969607011..050a799823 100644 --- a/std/special/compiler_rt/extendXfYf2_test.zig +++ b/std/special/compiler_rt/extendXfYf2_test.zig @@ -1,7 +1,6 @@ const __extenddftf2 = @import("extendXfYf2.zig").__extenddftf2; const __extendhfsf2 = @import("extendXfYf2.zig").__extendhfsf2; const __extendsftf2 = @import("extendXfYf2.zig").__extendsftf2; -const assert = @import("std").debug.assert; fn test__extenddftf2(a: f64, expectedHi: u64, expectedLo: u64) void { const x = __extenddftf2(a); diff --git a/std/special/compiler_rt/fixdfdi_test.zig b/std/special/compiler_rt/fixdfdi_test.zig index 72bcf452d2..a55245210b 100644 --- a/std/special/compiler_rt/fixdfdi_test.zig +++ b/std/special/compiler_rt/fixdfdi_test.zig @@ -1,13 +1,13 @@ const __fixdfdi = @import("fixdfdi.zig").__fixdfdi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixdfdi(a: f64, expected: i64) void { const x = __fixdfdi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u64({x})\n", a, @bitCast(u64, a), x, x, expected, expected, @bitCast(u64, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixdfdi" { diff --git a/std/special/compiler_rt/fixdfsi_test.zig b/std/special/compiler_rt/fixdfsi_test.zig index 147f46534a..5cb9bef1d9 100644 --- a/std/special/compiler_rt/fixdfsi_test.zig +++ b/std/special/compiler_rt/fixdfsi_test.zig @@ -1,13 +1,13 @@ const __fixdfsi = @import("fixdfsi.zig").__fixdfsi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixdfsi(a: f64, expected: i32) void { const x = __fixdfsi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u64({x})\n", a, @bitCast(u64, a), x, x, expected, expected, @bitCast(u32, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixdfsi" { diff --git a/std/special/compiler_rt/fixdfti_test.zig b/std/special/compiler_rt/fixdfti_test.zig index 5bb3a31a3f..5fbcd206ed 100644 --- a/std/special/compiler_rt/fixdfti_test.zig +++ b/std/special/compiler_rt/fixdfti_test.zig @@ -1,13 +1,13 @@ const __fixdfti = @import("fixdfti.zig").__fixdfti; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixdfti(a: f64, expected: i128) void { const x = __fixdfti(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u64({x})\n", a, @bitCast(u64, a), x, x, expected, expected, @bitCast(u128, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixdfti" { diff --git a/std/special/compiler_rt/fixint_test.zig b/std/special/compiler_rt/fixint_test.zig index 6676bddbee..0b0936d1e2 100644 --- a/std/special/compiler_rt/fixint_test.zig +++ b/std/special/compiler_rt/fixint_test.zig @@ -1,7 +1,7 @@ const is_test = @import("builtin").is_test; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; const fixint = @import("fixint.zig").fixint; @@ -9,7 +9,7 @@ const fixint = @import("fixint.zig").fixint; fn test__fixint(comptime fp_t: type, comptime fixint_t: type, a: fp_t, expected: fixint_t) void { const x = fixint(fp_t, fixint_t, a); //warn("a={} x={}:{x} expected={}:{x})\n", a, x, x, expected, expected); - assert(x == expected); + testing.expect(x == expected); } test "fixint.i1" { diff --git a/std/special/compiler_rt/fixsfdi_test.zig b/std/special/compiler_rt/fixsfdi_test.zig index ef8e50e38e..276670421b 100644 --- a/std/special/compiler_rt/fixsfdi_test.zig +++ b/std/special/compiler_rt/fixsfdi_test.zig @@ -1,13 +1,13 @@ const __fixsfdi = @import("fixsfdi.zig").__fixsfdi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixsfdi(a: f32, expected: i64) void { const x = __fixsfdi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u32({x})\n", a, @bitCast(u32, a), x, x, expected, expected, @bitCast(u64, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixsfdi" { diff --git a/std/special/compiler_rt/fixsfsi_test.zig b/std/special/compiler_rt/fixsfsi_test.zig index d5c0ba5c2a..a05c15e536 100644 --- a/std/special/compiler_rt/fixsfsi_test.zig +++ b/std/special/compiler_rt/fixsfsi_test.zig @@ -1,13 +1,13 @@ const __fixsfsi = @import("fixsfsi.zig").__fixsfsi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixsfsi(a: f32, expected: i32) void { const x = __fixsfsi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u32({x})\n", a, @bitCast(u32, a), x, x, expected, expected, @bitCast(u32, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixsfsi" { diff --git a/std/special/compiler_rt/fixsfti_test.zig b/std/special/compiler_rt/fixsfti_test.zig index d693143b18..32410e2dee 100644 --- a/std/special/compiler_rt/fixsfti_test.zig +++ b/std/special/compiler_rt/fixsfti_test.zig @@ -1,13 +1,13 @@ const __fixsfti = @import("fixsfti.zig").__fixsfti; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixsfti(a: f32, expected: i128) void { const x = __fixsfti(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u128({x})\n", a, @bitCast(u32, a), x, x, expected, expected, @bitCast(u128, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixsfti" { diff --git a/std/special/compiler_rt/fixtfdi_test.zig b/std/special/compiler_rt/fixtfdi_test.zig index 58ccbc5832..a31bcb7d6f 100644 --- a/std/special/compiler_rt/fixtfdi_test.zig +++ b/std/special/compiler_rt/fixtfdi_test.zig @@ -1,13 +1,13 @@ const __fixtfdi = @import("fixtfdi.zig").__fixtfdi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixtfdi(a: f128, expected: i64) void { const x = __fixtfdi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u64({x})\n", a, @bitCast(u128, a), x, x, expected, expected, @bitCast(u64, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixtfdi" { diff --git a/std/special/compiler_rt/fixtfsi_test.zig b/std/special/compiler_rt/fixtfsi_test.zig index 7a3cc7f46c..7b37e4e09f 100644 --- a/std/special/compiler_rt/fixtfsi_test.zig +++ b/std/special/compiler_rt/fixtfsi_test.zig @@ -1,13 +1,13 @@ const __fixtfsi = @import("fixtfsi.zig").__fixtfsi; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixtfsi(a: f128, expected: i32) void { const x = __fixtfsi(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u32({x})\n", a, @bitCast(u128, a), x, x, expected, expected, @bitCast(u32, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixtfsi" { diff --git a/std/special/compiler_rt/fixtfti_test.zig b/std/special/compiler_rt/fixtfti_test.zig index 520009486a..be461a1c91 100644 --- a/std/special/compiler_rt/fixtfti_test.zig +++ b/std/special/compiler_rt/fixtfti_test.zig @@ -1,13 +1,13 @@ const __fixtfti = @import("fixtfti.zig").__fixtfti; const std = @import("std"); const math = std.math; -const assert = std.debug.assert; +const testing = std.testing; const warn = std.debug.warn; fn test__fixtfti(a: f128, expected: i128) void { const x = __fixtfti(a); //warn("a={}:{x} x={}:{x} expected={}:{x}:u128({x})\n", a, @bitCast(u128, a), x, x, expected, expected, @bitCast(u128, expected)); - assert(x == expected); + testing.expect(x == expected); } test "fixtfti" { diff --git a/std/special/compiler_rt/fixunsdfdi_test.zig b/std/special/compiler_rt/fixunsdfdi_test.zig index e59d09f8de..67eeb70520 100644 --- a/std/special/compiler_rt/fixunsdfdi_test.zig +++ b/std/special/compiler_rt/fixunsdfdi_test.zig @@ -1,9 +1,9 @@ const __fixunsdfdi = @import("fixunsdfdi.zig").__fixunsdfdi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunsdfdi(a: f64, expected: u64) void { const x = __fixunsdfdi(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunsdfdi" { diff --git a/std/special/compiler_rt/fixunsdfsi_test.zig b/std/special/compiler_rt/fixunsdfsi_test.zig index db6e32e23d..c006473fb9 100644 --- a/std/special/compiler_rt/fixunsdfsi_test.zig +++ b/std/special/compiler_rt/fixunsdfsi_test.zig @@ -1,9 +1,9 @@ const __fixunsdfsi = @import("fixunsdfsi.zig").__fixunsdfsi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunsdfsi(a: f64, expected: u32) void { const x = __fixunsdfsi(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunsdfsi" { diff --git a/std/special/compiler_rt/fixunsdfti_test.zig b/std/special/compiler_rt/fixunsdfti_test.zig index 7f7b083d19..8241900692 100644 --- a/std/special/compiler_rt/fixunsdfti_test.zig +++ b/std/special/compiler_rt/fixunsdfti_test.zig @@ -1,9 +1,9 @@ const __fixunsdfti = @import("fixunsdfti.zig").__fixunsdfti; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunsdfti(a: f64, expected: u128) void { const x = __fixunsdfti(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunsdfti" { diff --git a/std/special/compiler_rt/fixunssfdi_test.zig b/std/special/compiler_rt/fixunssfdi_test.zig index e4e6c1736d..e2089822d2 100644 --- a/std/special/compiler_rt/fixunssfdi_test.zig +++ b/std/special/compiler_rt/fixunssfdi_test.zig @@ -1,9 +1,9 @@ const __fixunssfdi = @import("fixunssfdi.zig").__fixunssfdi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunssfdi(a: f32, expected: u64) void { const x = __fixunssfdi(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunssfdi" { diff --git a/std/special/compiler_rt/fixunssfsi_test.zig b/std/special/compiler_rt/fixunssfsi_test.zig index 614c648dfe..4aee84d2d2 100644 --- a/std/special/compiler_rt/fixunssfsi_test.zig +++ b/std/special/compiler_rt/fixunssfsi_test.zig @@ -1,9 +1,9 @@ const __fixunssfsi = @import("fixunssfsi.zig").__fixunssfsi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunssfsi(a: f32, expected: u32) void { const x = __fixunssfsi(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunssfsi" { diff --git a/std/special/compiler_rt/fixunssfti_test.zig b/std/special/compiler_rt/fixunssfti_test.zig index 43ad527f53..4cb27cbb8a 100644 --- a/std/special/compiler_rt/fixunssfti_test.zig +++ b/std/special/compiler_rt/fixunssfti_test.zig @@ -1,9 +1,9 @@ const __fixunssfti = @import("fixunssfti.zig").__fixunssfti; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunssfti(a: f32, expected: u128) void { const x = __fixunssfti(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunssfti" { diff --git a/std/special/compiler_rt/fixunstfdi_test.zig b/std/special/compiler_rt/fixunstfdi_test.zig index 6b1b9b7bd2..0d47641c09 100644 --- a/std/special/compiler_rt/fixunstfdi_test.zig +++ b/std/special/compiler_rt/fixunstfdi_test.zig @@ -1,9 +1,9 @@ const __fixunstfdi = @import("fixunstfdi.zig").__fixunstfdi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunstfdi(a: f128, expected: u64) void { const x = __fixunstfdi(a); - assert(x == expected); + testing.expect(x == expected); } test "fixunstfdi" { diff --git a/std/special/compiler_rt/fixunstfsi_test.zig b/std/special/compiler_rt/fixunstfsi_test.zig index f47fcb3c86..e709636912 100644 --- a/std/special/compiler_rt/fixunstfsi_test.zig +++ b/std/special/compiler_rt/fixunstfsi_test.zig @@ -1,9 +1,9 @@ const __fixunstfsi = @import("fixunstfsi.zig").__fixunstfsi; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunstfsi(a: f128, expected: u32) void { const x = __fixunstfsi(a); - assert(x == expected); + testing.expect(x == expected); } const inf128 = @bitCast(f128, u128(0x7fff0000000000000000000000000000)); diff --git a/std/special/compiler_rt/fixunstfti_test.zig b/std/special/compiler_rt/fixunstfti_test.zig index 9128ac6c08..833e4779dd 100644 --- a/std/special/compiler_rt/fixunstfti_test.zig +++ b/std/special/compiler_rt/fixunstfti_test.zig @@ -1,9 +1,9 @@ const __fixunstfti = @import("fixunstfti.zig").__fixunstfti; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__fixunstfti(a: f128, expected: u128) void { const x = __fixunstfti(a); - assert(x == expected); + testing.expect(x == expected); } const inf128 = @bitCast(f128, u128(0x7fff0000000000000000000000000000)); diff --git a/std/special/compiler_rt/floattidf_test.zig b/std/special/compiler_rt/floattidf_test.zig index 25dc595052..4914342c31 100644 --- a/std/special/compiler_rt/floattidf_test.zig +++ b/std/special/compiler_rt/floattidf_test.zig @@ -1,9 +1,9 @@ const __floattidf = @import("floattidf.zig").__floattidf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floattidf(a: i128, expected: f64) void { const x = __floattidf(a); - assert(x == expected); + testing.expect(x == expected); } test "floattidf" { diff --git a/std/special/compiler_rt/floattisf_test.zig b/std/special/compiler_rt/floattisf_test.zig index ecb8eac60a..a6aa115307 100644 --- a/std/special/compiler_rt/floattisf_test.zig +++ b/std/special/compiler_rt/floattisf_test.zig @@ -1,9 +1,9 @@ const __floattisf = @import("floattisf.zig").__floattisf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floattisf(a: i128, expected: f32) void { const x = __floattisf(a); - assert(x == expected); + testing.expect(x == expected); } test "floattisf" { diff --git a/std/special/compiler_rt/floattitf_test.zig b/std/special/compiler_rt/floattitf_test.zig index da2ccc8b35..53e3e48bdb 100644 --- a/std/special/compiler_rt/floattitf_test.zig +++ b/std/special/compiler_rt/floattitf_test.zig @@ -1,9 +1,9 @@ const __floattitf = @import("floattitf.zig").__floattitf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floattitf(a: i128, expected: f128) void { const x = __floattitf(a); - assert(x == expected); + testing.expect(x == expected); } test "floattitf" { diff --git a/std/special/compiler_rt/floatunditf_test.zig b/std/special/compiler_rt/floatunditf_test.zig index 8533c75070..5b4e195870 100644 --- a/std/special/compiler_rt/floatunditf_test.zig +++ b/std/special/compiler_rt/floatunditf_test.zig @@ -1,5 +1,4 @@ const __floatunditf = @import("floatunditf.zig").__floatunditf; -const assert = @import("std").debug.assert; fn test__floatunditf(a: u128, expected_hi: u64, expected_lo: u64) void { const x = __floatunditf(a); diff --git a/std/special/compiler_rt/floatunsitf_test.zig b/std/special/compiler_rt/floatunsitf_test.zig index 06f54cde03..52e4786903 100644 --- a/std/special/compiler_rt/floatunsitf_test.zig +++ b/std/special/compiler_rt/floatunsitf_test.zig @@ -1,5 +1,4 @@ const __floatunsitf = @import("floatunsitf.zig").__floatunsitf; -const assert = @import("std").debug.assert; fn test__floatunsitf(a: u64, expected_hi: u64, expected_lo: u64) void { const x = __floatunsitf(a); diff --git a/std/special/compiler_rt/floatuntidf_test.zig b/std/special/compiler_rt/floatuntidf_test.zig index e2c79378e2..974f3e4be3 100644 --- a/std/special/compiler_rt/floatuntidf_test.zig +++ b/std/special/compiler_rt/floatuntidf_test.zig @@ -1,9 +1,9 @@ const __floatuntidf = @import("floatuntidf.zig").__floatuntidf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floatuntidf(a: u128, expected: f64) void { const x = __floatuntidf(a); - assert(x == expected); + testing.expect(x == expected); } test "floatuntidf" { diff --git a/std/special/compiler_rt/floatuntisf_test.zig b/std/special/compiler_rt/floatuntisf_test.zig index 7f84c1f963..3a97807066 100644 --- a/std/special/compiler_rt/floatuntisf_test.zig +++ b/std/special/compiler_rt/floatuntisf_test.zig @@ -1,9 +1,9 @@ const __floatuntisf = @import("floatuntisf.zig").__floatuntisf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floatuntisf(a: u128, expected: f32) void { const x = __floatuntisf(a); - assert(x == expected); + testing.expect(x == expected); } test "floatuntisf" { diff --git a/std/special/compiler_rt/floatuntitf_test.zig b/std/special/compiler_rt/floatuntitf_test.zig index 8e67fee108..09f3eabb3e 100644 --- a/std/special/compiler_rt/floatuntitf_test.zig +++ b/std/special/compiler_rt/floatuntitf_test.zig @@ -1,9 +1,9 @@ const __floatuntitf = @import("floatuntitf.zig").__floatuntitf; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__floatuntitf(a: u128, expected: f128) void { const x = __floatuntitf(a); - assert(x == expected); + testing.expect(x == expected); } test "floatuntitf" { diff --git a/std/special/compiler_rt/index.zig b/std/special/compiler_rt/index.zig index 4bbfc2b290..3df94db589 100644 --- a/std/special/compiler_rt/index.zig +++ b/std/special/compiler_rt/index.zig @@ -110,6 +110,7 @@ comptime { const std = @import("std"); const assert = std.debug.assert; +const testing = std.testing; const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4; @@ -417,7 +418,7 @@ test "test_umoddi3" { fn test_one_umoddi3(a: u64, b: u64, expected_r: u64) void { const r = __umoddi3(a, b); - assert(r == expected_r); + testing.expect(r == expected_r); } test "test_udivsi3" { @@ -1091,5 +1092,5 @@ test "test_udivsi3" { fn test_one_udivsi3(a: u32, b: u32, expected_q: u32) void { const q: u32 = __udivsi3(a, b); - assert(q == expected_q); + testing.expect(q == expected_q); } diff --git a/std/special/compiler_rt/muloti4_test.zig b/std/special/compiler_rt/muloti4_test.zig index 6b3671323f..00144a8839 100644 --- a/std/special/compiler_rt/muloti4_test.zig +++ b/std/special/compiler_rt/muloti4_test.zig @@ -1,10 +1,10 @@ const __muloti4 = @import("muloti4.zig").__muloti4; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__muloti4(a: i128, b: i128, expected: i128, expected_overflow: c_int) void { var overflow: c_int = undefined; const x = __muloti4(a, b, &overflow); - assert(overflow == expected_overflow and (expected_overflow != 0 or x == expected)); + testing.expect(overflow == expected_overflow and (expected_overflow != 0 or x == expected)); } test "muloti4" { diff --git a/std/special/compiler_rt/multi3_test.zig b/std/special/compiler_rt/multi3_test.zig index 413ff20a79..92c580e20f 100644 --- a/std/special/compiler_rt/multi3_test.zig +++ b/std/special/compiler_rt/multi3_test.zig @@ -1,9 +1,9 @@ const __multi3 = @import("multi3.zig").__multi3; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__multi3(a: i128, b: i128, expected: i128) void { const x = __multi3(a, b); - assert(x == expected); + testing.expect(x == expected); } test "multi3" { diff --git a/std/special/compiler_rt/udivmoddi4_test.zig b/std/special/compiler_rt/udivmoddi4_test.zig index 34b9dda1ea..5e6924f290 100644 --- a/std/special/compiler_rt/udivmoddi4_test.zig +++ b/std/special/compiler_rt/udivmoddi4_test.zig @@ -1,13 +1,13 @@ // Disable formatting to avoid unnecessary source repository bloat. // zig fmt: off const __udivmoddi4 = @import("udivmoddi4.zig").__udivmoddi4; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__udivmoddi4(a: u64, b: u64, expected_q: u64, expected_r: u64) void { var r: u64 = undefined; const q = __udivmoddi4(a, b, &r); - assert(q == expected_q); - assert(r == expected_r); + testing.expect(q == expected_q); + testing.expect(r == expected_r); } test "udivmoddi4" { diff --git a/std/special/compiler_rt/udivmodti4_test.zig b/std/special/compiler_rt/udivmodti4_test.zig index f6b370c26e..0c7880f346 100644 --- a/std/special/compiler_rt/udivmodti4_test.zig +++ b/std/special/compiler_rt/udivmodti4_test.zig @@ -1,13 +1,13 @@ // Disable formatting to avoid unnecessary source repository bloat. // zig fmt: off const __udivmodti4 = @import("udivmodti4.zig").__udivmodti4; -const assert = @import("std").debug.assert; +const testing = @import("std").testing; fn test__udivmodti4(a: u128, b: u128, expected_q: u128, expected_r: u128) void { var r: u128 = undefined; const q = __udivmodti4(a, b, &r); - assert(q == expected_q); - assert(r == expected_r); + testing.expect(q == expected_q); + testing.expect(r == expected_r); } test "udivmodti4" { diff --git a/std/special/init-lib/src/main.zig b/std/special/init-lib/src/main.zig index 27fdeb2030..747bb08573 100644 --- a/std/special/init-lib/src/main.zig +++ b/std/special/init-lib/src/main.zig @@ -1,10 +1,10 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const testing = std.testing; export fn add(a: i32, b: i32) i32 { return a + b; } test "basic add functionality" { - assertOrPanic(add(3, 7) == 10); + testing.expect(add(3, 7) == 10); } diff --git a/std/statically_initialized_mutex.zig b/std/statically_initialized_mutex.zig index 37582d49c1..16bcd7adaf 100644 --- a/std/statically_initialized_mutex.zig +++ b/std/statically_initialized_mutex.zig @@ -3,6 +3,7 @@ const builtin = @import("builtin"); const AtomicOrder = builtin.AtomicOrder; const AtomicRmwOp = builtin.AtomicRmwOp; const assert = std.debug.assert; +const expect = std.testing.expect; const windows = std.os.windows; /// Lock may be held only once. If the same thread @@ -95,7 +96,7 @@ test "std.StaticallyInitializedMutex" { if (builtin.single_threaded) { TestContext.worker(&context); - std.debug.assertOrPanic(context.data == TestContext.incr_count); + expect(context.data == TestContext.incr_count); } else { const thread_count = 10; var threads: [thread_count]*std.os.Thread = undefined; @@ -105,6 +106,6 @@ test "std.StaticallyInitializedMutex" { for (threads) |t| t.wait(); - std.debug.assertOrPanic(context.data == thread_count * TestContext.incr_count); + expect(context.data == thread_count * TestContext.incr_count); } } diff --git a/std/testing.zig b/std/testing.zig new file mode 100644 index 0000000000..ade6e8b0dd --- /dev/null +++ b/std/testing.zig @@ -0,0 +1,152 @@ +const builtin = @import("builtin"); +const TypeId = builtin.TypeId; +const std = @import("index.zig"); + +/// 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 { + // TODO remove the workaround here for https://github.com/ziglang/zig/issues/1936 + if (actual_error_union) |actual_payload| { + // TODO remove workaround here for https://github.com/ziglang/zig/issues/557 + if (@sizeOf(@typeOf(actual_payload)) == 0) { + std.debug.panic("expected error.{}, found {} value", @errorName(expected_error), @typeName(@typeOf(actual_payload))); + } else { + std.debug.panic("expected error.{}, found {}", @errorName(expected_error), actual_payload); + } + } else |actual_error| { + if (expected_error != actual_error) { + std.debug.panic("expected error.{}, found error.{}", @errorName(expected_error), @errorName(actual_error)); + } + } +} + +/// 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. +pub fn expectEqual(expected: var, actual: var) void { + if (@typeOf(actual) != @typeOf(expected)) { + @compileError("type mismatch. expected " ++ @typeName(@typeOf(expected)) ++ ", found " ++ @typeName(@typeOf(actual))); + } + + switch (@typeInfo(@typeOf(actual))) { + TypeId.NoReturn, + TypeId.BoundFn, + TypeId.ArgTuple, + TypeId.Opaque, + => @compileError("value of type " ++ @typeName(@typeOf(actual)) ++ " encountered"), + + TypeId.Undefined, + TypeId.Null, + TypeId.Void, + => return, + + TypeId.Type, + TypeId.Bool, + TypeId.Int, + TypeId.Float, + TypeId.ComptimeFloat, + TypeId.ComptimeInt, + TypeId.Enum, + TypeId.Namespace, + TypeId.Fn, + TypeId.Promise, + TypeId.Vector, + TypeId.ErrorSet, + => { + if (actual != expected) { + std.debug.panic("expected {}, found {}", expected, actual); + } + }, + + TypeId.Pointer => |pointer| { + switch (pointer.size) { + builtin.TypeInfo.Pointer.Size.One, + builtin.TypeInfo.Pointer.Size.Many, + => { + if (actual != expected) { + std.debug.panic("expected {}, found {}", expected, actual); + } + }, + + builtin.TypeInfo.Pointer.Size.Slice => { + if (actual.ptr != expected.ptr) { + std.debug.panic("expected slice ptr {}, found {}", expected.ptr, actual.ptr); + } + if (actual.len != expected.len) { + std.debug.panic("expected slice len {}, found {}", expected.len, actual.len); + } + }, + } + }, + + TypeId.Array => |array| expectEqualSlices(array.child, &expected, &actual), + + TypeId.Struct => { + @compileError("TODO implement testing.expectEqual for structs"); + }, + + TypeId.Union => |union_info| { + if (union_info.tag_type == null) { + @compileError("Unable to compare untagged union values"); + } + @compileError("TODO implement testing.expectEqual for tagged unions"); + }, + + TypeId.Optional => { + if (expected) |expected_payload| { + if (actual) |actual_payload| { + expectEqual(expected_payload, actual_payload); + } else { + std.debug.panic("expected {}, found null", expected_payload); + } + } else { + if (actual) |actual_payload| { + std.debug.panic("expected null, found {}", actual_payload); + } + } + }, + + TypeId.ErrorUnion => { + if (expected) |expected_payload| { + if (actual) |actual_payload| { + expectEqual(expected_payload, actual_payload); + } else |actual_err| { + std.debug.panic("expected {}, found {}", expected_payload, actual_err); + } + } else |expected_err| { + if (actual) |actual_payload| { + std.debug.panic("expected {}, found {}", expected_err, actual_payload); + } else |actual_err| { + expectEqual(expected_err, actual_err); + } + } + }, + + } +} + +/// 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) { + std.debug.panic("slice lengths differ. expected {}, found {}", expected.len, actual.len); + } + var i: usize = 0; + while (i < expected.len) : (i += 1) { + if (expected[i] != actual[i]) { + std.debug.panic("index {} incorrect. expected {}, found {}", i, expected[i], actual[i]); + } + } +} + +/// 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"); +} diff --git a/std/unicode.zig b/std/unicode.zig index 2e542bcb19..fccdf513b9 100644 --- a/std/unicode.zig +++ b/std/unicode.zig @@ -1,7 +1,7 @@ const std = @import("./index.zig"); const builtin = @import("builtin"); -const debug = std.debug; const assert = std.debug.assert; +const testing = std.testing; const mem = std.mem; /// Returns how many bytes the UTF-8 representation would require @@ -32,7 +32,7 @@ pub fn utf8ByteSequenceLength(first_byte: u8) !u3 { /// Returns: the number of bytes written to out. pub fn utf8Encode(c: u32, out: []u8) !u3 { const length = try utf8CodepointSequenceLength(c); - debug.assert(out.len >= length); + assert(out.len >= length); switch (length) { // The pattern for each is the same // - Increasing the initial shift by 6 each time @@ -81,8 +81,8 @@ const Utf8Decode2Error = error{ Utf8OverlongEncoding, }; pub fn utf8Decode2(bytes: []const u8) Utf8Decode2Error!u32 { - debug.assert(bytes.len == 2); - debug.assert(bytes[0] & 0b11100000 == 0b11000000); + assert(bytes.len == 2); + assert(bytes[0] & 0b11100000 == 0b11000000); var value: u32 = bytes[0] & 0b00011111; if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation; @@ -100,8 +100,8 @@ const Utf8Decode3Error = error{ Utf8EncodesSurrogateHalf, }; pub fn utf8Decode3(bytes: []const u8) Utf8Decode3Error!u32 { - debug.assert(bytes.len == 3); - debug.assert(bytes[0] & 0b11110000 == 0b11100000); + assert(bytes.len == 3); + assert(bytes[0] & 0b11110000 == 0b11100000); var value: u32 = bytes[0] & 0b00001111; if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation; @@ -124,8 +124,8 @@ const Utf8Decode4Error = error{ Utf8CodepointTooLarge, }; pub fn utf8Decode4(bytes: []const u8) Utf8Decode4Error!u32 { - debug.assert(bytes.len == 4); - debug.assert(bytes[0] & 0b11111000 == 0b11110000); + assert(bytes.len == 4); + assert(bytes[0] & 0b11111000 == 0b11110000); var value: u32 = bytes[0] & 0b00000111; if (bytes[1] & 0b11000000 != 0b10000000) return error.Utf8ExpectedContinuation; @@ -274,23 +274,23 @@ test "utf8 encode" { fn testUtf8Encode() !void { // A few taken from wikipedia a few taken elsewhere var array: [4]u8 = undefined; - debug.assert((try utf8Encode(try utf8Decode("€"), array[0..])) == 3); - debug.assert(array[0] == 0b11100010); - debug.assert(array[1] == 0b10000010); - debug.assert(array[2] == 0b10101100); + testing.expect((try utf8Encode(try utf8Decode("€"), array[0..])) == 3); + testing.expect(array[0] == 0b11100010); + testing.expect(array[1] == 0b10000010); + testing.expect(array[2] == 0b10101100); - debug.assert((try utf8Encode(try utf8Decode("$"), array[0..])) == 1); - debug.assert(array[0] == 0b00100100); + testing.expect((try utf8Encode(try utf8Decode("$"), array[0..])) == 1); + testing.expect(array[0] == 0b00100100); - debug.assert((try utf8Encode(try utf8Decode("¢"), array[0..])) == 2); - debug.assert(array[0] == 0b11000010); - debug.assert(array[1] == 0b10100010); + testing.expect((try utf8Encode(try utf8Decode("¢"), array[0..])) == 2); + testing.expect(array[0] == 0b11000010); + testing.expect(array[1] == 0b10100010); - debug.assert((try utf8Encode(try utf8Decode("𐍈"), array[0..])) == 4); - debug.assert(array[0] == 0b11110000); - debug.assert(array[1] == 0b10010000); - debug.assert(array[2] == 0b10001101); - debug.assert(array[3] == 0b10001000); + testing.expect((try utf8Encode(try utf8Decode("𐍈"), array[0..])) == 4); + testing.expect(array[0] == 0b11110000); + testing.expect(array[1] == 0b10010000); + testing.expect(array[2] == 0b10001101); + testing.expect(array[3] == 0b10001000); } test "utf8 encode error" { @@ -306,11 +306,7 @@ fn testUtf8EncodeError() void { } fn testErrorEncode(codePoint: u32, array: []u8, expectedErr: anyerror) void { - if (utf8Encode(codePoint, array)) |_| { - unreachable; - } else |err| { - debug.assert(err == expectedErr); - } + testing.expectError(expectedErr, utf8Encode(codePoint, array)); } test "utf8 iterator on ascii" { @@ -321,16 +317,16 @@ fn testUtf8IteratorOnAscii() void { const s = Utf8View.initComptime("abc"); var it1 = s.iterator(); - debug.assert(std.mem.eql(u8, "a", it1.nextCodepointSlice().?)); - debug.assert(std.mem.eql(u8, "b", it1.nextCodepointSlice().?)); - debug.assert(std.mem.eql(u8, "c", it1.nextCodepointSlice().?)); - debug.assert(it1.nextCodepointSlice() == null); + testing.expect(std.mem.eql(u8, "a", it1.nextCodepointSlice().?)); + testing.expect(std.mem.eql(u8, "b", it1.nextCodepointSlice().?)); + testing.expect(std.mem.eql(u8, "c", it1.nextCodepointSlice().?)); + testing.expect(it1.nextCodepointSlice() == null); var it2 = s.iterator(); - debug.assert(it2.nextCodepoint().? == 'a'); - debug.assert(it2.nextCodepoint().? == 'b'); - debug.assert(it2.nextCodepoint().? == 'c'); - debug.assert(it2.nextCodepoint() == null); + testing.expect(it2.nextCodepoint().? == 'a'); + testing.expect(it2.nextCodepoint().? == 'b'); + testing.expect(it2.nextCodepoint().? == 'c'); + testing.expect(it2.nextCodepoint() == null); } test "utf8 view bad" { @@ -340,12 +336,7 @@ test "utf8 view bad" { fn testUtf8ViewBad() void { // Compile-time error. // const s3 = Utf8View.initComptime("\xfe\xf2"); - const s = Utf8View.init("hel\xadlo"); - if (s) |_| { - unreachable; - } else |err| { - debug.assert(err == error.InvalidUtf8); - } + testing.expectError(error.InvalidUtf8, Utf8View.init("hel\xadlo")); } test "utf8 view ok" { @@ -356,16 +347,16 @@ fn testUtf8ViewOk() void { const s = Utf8View.initComptime("東京市"); var it1 = s.iterator(); - debug.assert(std.mem.eql(u8, "東", it1.nextCodepointSlice().?)); - debug.assert(std.mem.eql(u8, "京", it1.nextCodepointSlice().?)); - debug.assert(std.mem.eql(u8, "市", it1.nextCodepointSlice().?)); - debug.assert(it1.nextCodepointSlice() == null); + testing.expect(std.mem.eql(u8, "東", it1.nextCodepointSlice().?)); + testing.expect(std.mem.eql(u8, "京", it1.nextCodepointSlice().?)); + testing.expect(std.mem.eql(u8, "市", it1.nextCodepointSlice().?)); + testing.expect(it1.nextCodepointSlice() == null); var it2 = s.iterator(); - debug.assert(it2.nextCodepoint().? == 0x6771); - debug.assert(it2.nextCodepoint().? == 0x4eac); - debug.assert(it2.nextCodepoint().? == 0x5e02); - debug.assert(it2.nextCodepoint() == null); + testing.expect(it2.nextCodepoint().? == 0x6771); + testing.expect(it2.nextCodepoint().? == 0x4eac); + testing.expect(it2.nextCodepoint().? == 0x5e02); + testing.expect(it2.nextCodepoint() == null); } test "bad utf8 slice" { @@ -373,10 +364,10 @@ test "bad utf8 slice" { testBadUtf8Slice(); } fn testBadUtf8Slice() void { - debug.assert(utf8ValidateSlice("abc")); - debug.assert(!utf8ValidateSlice("abc\xc0")); - debug.assert(!utf8ValidateSlice("abc\xc0abc")); - debug.assert(utf8ValidateSlice("abc\xdf\xbf")); + testing.expect(utf8ValidateSlice("abc")); + testing.expect(!utf8ValidateSlice("abc\xc0")); + testing.expect(!utf8ValidateSlice("abc\xc0abc")); + testing.expect(utf8ValidateSlice("abc\xdf\xbf")); } test "valid utf8" { @@ -459,21 +450,17 @@ fn testMiscInvalidUtf8() void { } fn testError(bytes: []const u8, expected_err: anyerror) void { - if (testDecode(bytes)) |_| { - unreachable; - } else |err| { - debug.assert(err == expected_err); - } + testing.expectError(expected_err, testDecode(bytes)); } fn testValid(bytes: []const u8, expected_codepoint: u32) void { - debug.assert((testDecode(bytes) catch unreachable) == expected_codepoint); + testing.expect((testDecode(bytes) catch unreachable) == expected_codepoint); } fn testDecode(bytes: []const u8) !u32 { const length = try utf8ByteSequenceLength(bytes[0]); if (bytes.len < length) return error.UnexpectedEof; - debug.assert(bytes.len == length); + testing.expect(bytes.len == length); return utf8Decode(bytes); } @@ -513,14 +500,14 @@ test "utf16leToUtf8" { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 'A'); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 'a'); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "Aa")); + testing.expect(mem.eql(u8, utf8, "Aa")); } { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0x80); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xffff); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf")); + testing.expect(mem.eql(u8, utf8, "\xc2\x80" ++ "\xef\xbf\xbf")); } { @@ -528,7 +515,7 @@ test "utf16leToUtf8" { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd7ff); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xe000); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80")); + testing.expect(mem.eql(u8, utf8, "\xed\x9f\xbf" ++ "\xee\x80\x80")); } { @@ -536,7 +523,7 @@ test "utf16leToUtf8" { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xd800); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "\xf0\x90\x80\x80")); + testing.expect(mem.eql(u8, utf8, "\xf0\x90\x80\x80")); } { @@ -544,14 +531,14 @@ test "utf16leToUtf8" { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdfff); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf")); + testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xbf\xbf")); } { mem.writeIntSliceLittle(u16, utf16le_as_bytes[0..], 0xdbff); mem.writeIntSliceLittle(u16, utf16le_as_bytes[2..], 0xdc00); const utf8 = try utf16leToUtf8Alloc(std.debug.global_allocator, utf16le); - assert(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); + testing.expect(mem.eql(u8, utf8, "\xf4\x8f\xb0\x80")); } } diff --git a/std/zig/ast.zig b/std/zig/ast.zig index bb2099fb75..f6ac4ed98c 100644 --- a/std/zig/ast.zig +++ b/std/zig/ast.zig @@ -1,5 +1,6 @@ const std = @import("../index.zig"); const assert = std.debug.assert; +const testing = std.testing; const SegmentedList = std.SegmentedList; const mem = std.mem; const Token = std.zig.Token; @@ -2224,5 +2225,5 @@ test "iterate" { .shebang = null, }; var base = &root.base; - assert(base.iterate(0) == null); + testing.expect(base.iterate(0) == null); } diff --git a/std/zig/parser_test.zig b/std/zig/parser_test.zig index 2b60fb9b00..7ad842e4b6 100644 --- a/std/zig/parser_test.zig +++ b/std/zig/parser_test.zig @@ -1940,7 +1940,7 @@ fn testTransform(source: []const u8, expected_source: []const u8) !void { warn("std.zig.render returned {} instead of {}\n", anything_changed, changes_expected); return error.TestFailed; } - std.debug.assert(anything_changed == changes_expected); + std.testing.expect(anything_changed == changes_expected); failing_allocator.allocator.free(result_source); break :x failing_allocator.index; }; diff --git a/std/zig/tokenizer.zig b/std/zig/tokenizer.zig index 4941fe2cc2..e71babe4e8 100644 --- a/std/zig/tokenizer.zig +++ b/std/zig/tokenizer.zig @@ -1345,5 +1345,5 @@ fn testTokenize(source: []const u8, expected_tokens: []const Token.Id) void { } } const last_token = tokenizer.next(); - std.debug.assert(last_token.id == Token.Id.Eof); + std.testing.expect(last_token.id == Token.Id.Eof); } diff --git a/test/cli.zig b/test/cli.zig index 745da4dd80..1520b3bde0 100644 --- a/test/cli.zig +++ b/test/cli.zig @@ -1,7 +1,7 @@ const std = @import("std"); const builtin = @import("builtin"); const os = std.os; -const assertOrPanic = std.debug.assertOrPanic; +const testing = std.testing; var a: *std.mem.Allocator = undefined; @@ -87,13 +87,13 @@ fn exec(cwd: []const u8, argv: []const []const u8) !os.ChildProcess.ExecResult { fn testZigInitLib(zig_exe: []const u8, dir_path: []const u8) !void { _ = try exec(dir_path, [][]const u8{ zig_exe, "init-lib" }); const test_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "test" }); - assertOrPanic(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n")); + testing.expect(std.mem.endsWith(u8, test_result.stderr, "All tests passed.\n")); } fn testZigInitExe(zig_exe: []const u8, dir_path: []const u8) !void { _ = try exec(dir_path, [][]const u8{ zig_exe, "init-exe" }); const run_result = try exec(dir_path, [][]const u8{ zig_exe, "build", "run" }); - assertOrPanic(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n")); + testing.expect(std.mem.eql(u8, run_result.stderr, "All your base are belong to us.\n")); } fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { @@ -126,6 +126,6 @@ fn testGodboltApi(zig_exe: []const u8, dir_path: []const u8) anyerror!void { _ = try exec(dir_path, args); const out_asm = try std.io.readFileAlloc(a, example_s_path); - assertOrPanic(std.mem.indexOf(u8, out_asm, "square:") != null); - assertOrPanic(std.mem.indexOf(u8, out_asm, "imul\tedi, edi") != null); + testing.expect(std.mem.indexOf(u8, out_asm, "square:") != null); + testing.expect(std.mem.indexOf(u8, out_asm, "imul\tedi, edi") != null); } diff --git a/test/stage1/behavior/align.zig b/test/stage1/behavior/align.zig index aa7a93ad84..dbd705ddc4 100644 --- a/test/stage1/behavior/align.zig +++ b/test/stage1/behavior/align.zig @@ -1,13 +1,13 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const builtin = @import("builtin"); var foo: u8 align(4) = 100; test "global variable alignment" { - assertOrPanic(@typeOf(&foo).alignment == 4); - assertOrPanic(@typeOf(&foo) == *align(4) u8); + expect(@typeOf(&foo).alignment == 4); + expect(@typeOf(&foo) == *align(4) u8); const slice = (*[1]u8)(&foo)[0..]; - assertOrPanic(@typeOf(slice) == []align(4) u8); + expect(@typeOf(slice) == []align(4) u8); } fn derp() align(@sizeOf(usize) * 2) i32 { @@ -17,9 +17,9 @@ fn noop1() align(1) void {} fn noop4() align(4) void {} test "function alignment" { - assertOrPanic(derp() == 1234); - assertOrPanic(@typeOf(noop1) == fn () align(1) void); - assertOrPanic(@typeOf(noop4) == fn () align(4) void); + expect(derp() == 1234); + expect(@typeOf(noop1) == fn () align(1) void); + expect(@typeOf(noop4) == fn () align(4) void); noop1(); noop4(); } @@ -30,7 +30,7 @@ var baz: packed struct { } = undefined; test "packed struct alignment" { - assertOrPanic(@typeOf(&baz.b) == *align(1) u32); + expect(@typeOf(&baz.b) == *align(1) u32); } const blah: packed struct { @@ -40,17 +40,17 @@ const blah: packed struct { } = undefined; test "bit field alignment" { - assertOrPanic(@typeOf(&blah.b) == *align(1:3:1) const u3); + expect(@typeOf(&blah.b) == *align(1:3:1) const u3); } test "default alignment allows unspecified in type syntax" { - assertOrPanic(*u32 == *align(@alignOf(u32)) u32); + expect(*u32 == *align(@alignOf(u32)) u32); } test "implicitly decreasing pointer alignment" { const a: u32 align(4) = 3; const b: u32 align(8) = 4; - assertOrPanic(addUnaligned(&a, &b) == 7); + expect(addUnaligned(&a, &b) == 7); } fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 { @@ -60,7 +60,7 @@ fn addUnaligned(a: *align(1) const u32, b: *align(1) const u32) u32 { test "implicitly decreasing slice alignment" { const a: u32 align(4) = 3; const b: u32 align(8) = 4; - assertOrPanic(addUnalignedSlice((*[1]u32)(&a)[0..], (*[1]u32)(&b)[0..]) == 7); + expect(addUnalignedSlice((*[1]u32)(&a)[0..], (*[1]u32)(&b)[0..]) == 7); } fn addUnalignedSlice(a: []align(1) const u32, b: []align(1) const u32) u32 { return a[0] + b[0]; @@ -77,7 +77,7 @@ fn testBytesAlign(b: u8) void { b, }; const ptr = @ptrCast(*u32, &bytes[0]); - assertOrPanic(ptr.* == 0x33333333); + expect(ptr.* == 0x33333333); } test "specifying alignment allows slice cast" { @@ -91,13 +91,13 @@ fn testBytesAlignSlice(b: u8) void { b, }; const slice: []u32 = @bytesToSlice(u32, bytes[0..]); - assertOrPanic(slice[0] == 0x33333333); + expect(slice[0] == 0x33333333); } test "@alignCast pointers" { var x: u32 align(4) = 1; expectsOnly1(&x); - assertOrPanic(x == 2); + expect(x == 2); } fn expectsOnly1(x: *align(1) u32) void { expects4(@alignCast(4, x)); @@ -113,7 +113,7 @@ test "@alignCast slices" { }; const slice = array[0..]; sliceExpectsOnly1(slice); - assertOrPanic(slice[0] == 2); + expect(slice[0] == 2); } fn sliceExpectsOnly1(slice: []align(1) u32) void { sliceExpects4(@alignCast(4, slice)); @@ -128,7 +128,7 @@ test "implicitly decreasing fn alignment" { } fn testImplicitlyDecreaseFnAlign(ptr: fn () align(1) i32, answer: i32) void { - assertOrPanic(ptr() == answer); + expect(ptr() == answer); } fn alignedSmall() align(8) i32 { @@ -139,7 +139,7 @@ fn alignedBig() align(16) i32 { } test "@alignCast functions" { - assertOrPanic(fnExpectsOnly1(simple4) == 0x19); + expect(fnExpectsOnly1(simple4) == 0x19); } fn fnExpectsOnly1(ptr: fn () align(1) i32) i32 { return fnExpects4(@alignCast(4, ptr)); @@ -152,9 +152,9 @@ fn simple4() align(4) i32 { } test "generic function with align param" { - assertOrPanic(whyWouldYouEverDoThis(1) == 0x1); - assertOrPanic(whyWouldYouEverDoThis(4) == 0x1); - assertOrPanic(whyWouldYouEverDoThis(8) == 0x1); + expect(whyWouldYouEverDoThis(1) == 0x1); + expect(whyWouldYouEverDoThis(4) == 0x1); + expect(whyWouldYouEverDoThis(8) == 0x1); } fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 { @@ -164,28 +164,28 @@ fn whyWouldYouEverDoThis(comptime align_bytes: u8) align(align_bytes) u8 { test "@ptrCast preserves alignment of bigger source" { var x: u32 align(16) = 1234; const ptr = @ptrCast(*u8, &x); - assertOrPanic(@typeOf(ptr) == *align(16) u8); + expect(@typeOf(ptr) == *align(16) u8); } test "runtime known array index has best alignment possible" { // take full advantage of over-alignment var array align(4) = []u8{ 1, 2, 3, 4 }; - assertOrPanic(@typeOf(&array[0]) == *align(4) u8); - assertOrPanic(@typeOf(&array[1]) == *u8); - assertOrPanic(@typeOf(&array[2]) == *align(2) u8); - assertOrPanic(@typeOf(&array[3]) == *u8); + expect(@typeOf(&array[0]) == *align(4) u8); + expect(@typeOf(&array[1]) == *u8); + expect(@typeOf(&array[2]) == *align(2) u8); + expect(@typeOf(&array[3]) == *u8); // because align is too small but we still figure out to use 2 var bigger align(2) = []u64{ 1, 2, 3, 4 }; - assertOrPanic(@typeOf(&bigger[0]) == *align(2) u64); - assertOrPanic(@typeOf(&bigger[1]) == *align(2) u64); - assertOrPanic(@typeOf(&bigger[2]) == *align(2) u64); - assertOrPanic(@typeOf(&bigger[3]) == *align(2) u64); + expect(@typeOf(&bigger[0]) == *align(2) u64); + expect(@typeOf(&bigger[1]) == *align(2) u64); + expect(@typeOf(&bigger[2]) == *align(2) u64); + expect(@typeOf(&bigger[3]) == *align(2) u64); // because pointer is align 2 and u32 align % 2 == 0 we can assume align 2 var smaller align(2) = []u32{ 1, 2, 3, 4 }; - comptime assertOrPanic(@typeOf(smaller[0..]) == []align(2) u32); - comptime assertOrPanic(@typeOf(smaller[0..].ptr) == [*]align(2) u32); + comptime expect(@typeOf(smaller[0..]) == []align(2) u32); + comptime expect(@typeOf(smaller[0..].ptr) == [*]align(2) u32); testIndex(smaller[0..].ptr, 0, *align(2) u32); testIndex(smaller[0..].ptr, 1, *align(2) u32); testIndex(smaller[0..].ptr, 2, *align(2) u32); @@ -198,14 +198,14 @@ test "runtime known array index has best alignment possible" { testIndex2(array[0..].ptr, 3, *u8); } fn testIndex(smaller: [*]align(2) u32, index: usize, comptime T: type) void { - comptime assertOrPanic(@typeOf(&smaller[index]) == T); + comptime expect(@typeOf(&smaller[index]) == T); } fn testIndex2(ptr: [*]align(4) u8, index: usize, comptime T: type) void { - comptime assertOrPanic(@typeOf(&ptr[index]) == T); + comptime expect(@typeOf(&ptr[index]) == T); } test "alignstack" { - assertOrPanic(fnWithAlignedStack() == 1234); + expect(fnWithAlignedStack() == 1234); } fn fnWithAlignedStack() i32 { @@ -214,7 +214,7 @@ fn fnWithAlignedStack() i32 { } test "alignment of structs" { - assertOrPanic(@alignOf(struct { + expect(@alignOf(struct { a: i32, b: *i32, }) == @alignOf(usize)); diff --git a/test/stage1/behavior/alignof.zig b/test/stage1/behavior/alignof.zig index 98c805908b..d4d9661ead 100644 --- a/test/stage1/behavior/alignof.zig +++ b/test/stage1/behavior/alignof.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const builtin = @import("builtin"); const maxInt = std.math.maxInt; @@ -10,9 +10,9 @@ const Foo = struct { }; test "@alignOf(T) before referencing T" { - comptime assertOrPanic(@alignOf(Foo) != maxInt(usize)); + comptime expect(@alignOf(Foo) != maxInt(usize)); if (builtin.arch == builtin.Arch.x86_64) { - comptime assertOrPanic(@alignOf(Foo) == 4); + comptime expect(@alignOf(Foo) == 4); } } diff --git a/test/stage1/behavior/array.zig b/test/stage1/behavior/array.zig index 1183305209..99b828e53c 100644 --- a/test/stage1/behavior/array.zig +++ b/test/stage1/behavior/array.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; test "arrays" { @@ -18,8 +18,8 @@ test "arrays" { i += 1; } - assertOrPanic(accumulator == 15); - assertOrPanic(getArrayLen(array) == 5); + expect(accumulator == 15); + expect(getArrayLen(array) == 5); } fn getArrayLen(a: []const u32) usize { return a.len; @@ -29,8 +29,8 @@ test "void arrays" { var array: [4]void = undefined; array[0] = void{}; array[1] = array[2]; - assertOrPanic(@sizeOf(@typeOf(array)) == 0); - assertOrPanic(array.len == 4); + expect(@sizeOf(@typeOf(array)) == 0); + expect(array.len == 4); } test "array literal" { @@ -41,12 +41,12 @@ test "array literal" { 1, }; - assertOrPanic(hex_mult.len == 4); - assertOrPanic(hex_mult[1] == 256); + expect(hex_mult.len == 4); + expect(hex_mult[1] == 256); } test "array dot len const expr" { - assertOrPanic(comptime x: { + expect(comptime x: { break :x some_array.len == 4; }); } @@ -70,11 +70,11 @@ test "nested arrays" { "thing", }; for (array_of_strings) |s, i| { - if (i == 0) assertOrPanic(mem.eql(u8, s, "hello")); - if (i == 1) assertOrPanic(mem.eql(u8, s, "this")); - if (i == 2) assertOrPanic(mem.eql(u8, s, "is")); - if (i == 3) assertOrPanic(mem.eql(u8, s, "my")); - if (i == 4) assertOrPanic(mem.eql(u8, s, "thing")); + if (i == 0) expect(mem.eql(u8, s, "hello")); + if (i == 1) expect(mem.eql(u8, s, "this")); + if (i == 2) expect(mem.eql(u8, s, "is")); + if (i == 3) expect(mem.eql(u8, s, "my")); + if (i == 4) expect(mem.eql(u8, s, "thing")); } } @@ -92,9 +92,9 @@ test "set global var array via slice embedded in struct" { s.a[1].b = 2; s.a[2].b = 3; - assertOrPanic(s_array[0].b == 1); - assertOrPanic(s_array[1].b == 2); - assertOrPanic(s_array[2].b == 3); + expect(s_array[0].b == 1); + expect(s_array[1].b == 2); + expect(s_array[2].b == 3); } test "array literal with specified size" { @@ -102,27 +102,27 @@ test "array literal with specified size" { 1, 2, }; - assertOrPanic(array[0] == 1); - assertOrPanic(array[1] == 2); + expect(array[0] == 1); + expect(array[1] == 2); } test "array child property" { var x: [5]i32 = undefined; - assertOrPanic(@typeOf(x).Child == i32); + expect(@typeOf(x).Child == i32); } test "array len property" { var x: [5]i32 = undefined; - assertOrPanic(@typeOf(x).len == 5); + expect(@typeOf(x).len == 5); } test "array len field" { var arr = [4]u8{ 0, 0, 0, 0 }; var ptr = &arr; - assertOrPanic(arr.len == 4); - comptime assertOrPanic(arr.len == 4); - assertOrPanic(ptr.len == 4); - comptime assertOrPanic(ptr.len == 4); + expect(arr.len == 4); + comptime expect(arr.len == 4); + expect(ptr.len == 4); + comptime expect(ptr.len == 4); } test "single-item pointer to array indexing and slicing" { @@ -133,7 +133,7 @@ test "single-item pointer to array indexing and slicing" { fn testSingleItemPtrArrayIndexSlice() void { var array = "aaaa"; doSomeMangling(&array); - assertOrPanic(mem.eql(u8, "azya", array)); + expect(mem.eql(u8, "azya", array)); } fn doSomeMangling(array: *[4]u8) void { @@ -150,7 +150,7 @@ fn testImplicitCastSingleItemPtr() void { var byte: u8 = 100; const slice = (*[1]u8)(&byte)[0..]; slice[0] += 1; - assertOrPanic(byte == 101); + expect(byte == 101); } fn testArrayByValAtComptime(b: [2]u8) u8 { @@ -165,7 +165,7 @@ test "comptime evalutating function that takes array by value" { test "implicit comptime in array type size" { var arr: [plusOne(10)]bool = undefined; - assertOrPanic(arr.len == 11); + expect(arr.len == 11); } fn plusOne(x: u32) u32 { @@ -197,15 +197,15 @@ test "array literal as argument to function" { }); } fn foo(x: []const i32) void { - assertOrPanic(x[0] == 1); - assertOrPanic(x[1] == 2); - assertOrPanic(x[2] == 3); + expect(x[0] == 1); + expect(x[1] == 2); + expect(x[2] == 3); } fn foo2(trash: bool, x: []const i32) void { - assertOrPanic(trash); - assertOrPanic(x[0] == 1); - assertOrPanic(x[1] == 2); - assertOrPanic(x[2] == 3); + expect(trash); + expect(x[0] == 1); + expect(x[1] == 2); + expect(x[2] == 3); } }; S.entry(2); @@ -229,12 +229,12 @@ test "double nested array to const slice cast in array literal" { []i32{1}, []i32{ two, 3 }, }; - assertOrPanic(cases2.len == 2); - assertOrPanic(cases2[0].len == 1); - assertOrPanic(cases2[0][0] == 1); - assertOrPanic(cases2[1].len == 2); - assertOrPanic(cases2[1][0] == 2); - assertOrPanic(cases2[1][1] == 3); + expect(cases2.len == 2); + expect(cases2[0].len == 1); + expect(cases2[0][0] == 1); + expect(cases2[1].len == 2); + expect(cases2[1][0] == 2); + expect(cases2[1][1] == 3); const cases3 = [][]const []const i32{ [][]const i32{[]i32{1}}, @@ -248,21 +248,21 @@ test "double nested array to const slice cast in array literal" { } fn check(cases: []const []const []const i32) void { - assertOrPanic(cases.len == 3); - assertOrPanic(cases[0].len == 1); - assertOrPanic(cases[0][0].len == 1); - assertOrPanic(cases[0][0][0] == 1); - assertOrPanic(cases[1].len == 1); - assertOrPanic(cases[1][0].len == 2); - assertOrPanic(cases[1][0][0] == 2); - assertOrPanic(cases[1][0][1] == 3); - assertOrPanic(cases[2].len == 2); - assertOrPanic(cases[2][0].len == 1); - assertOrPanic(cases[2][0][0] == 4); - assertOrPanic(cases[2][1].len == 3); - assertOrPanic(cases[2][1][0] == 5); - assertOrPanic(cases[2][1][1] == 6); - assertOrPanic(cases[2][1][2] == 7); + expect(cases.len == 3); + expect(cases[0].len == 1); + expect(cases[0][0].len == 1); + expect(cases[0][0][0] == 1); + expect(cases[1].len == 1); + expect(cases[1][0].len == 2); + expect(cases[1][0][0] == 2); + expect(cases[1][0][1] == 3); + expect(cases[2].len == 2); + expect(cases[2][0].len == 1); + expect(cases[2][0][0] == 4); + expect(cases[2][1].len == 3); + expect(cases[2][1][0] == 5); + expect(cases[2][1][1] == 6); + expect(cases[2][1][2] == 7); } }; S.entry(2); diff --git a/test/stage1/behavior/asm.zig b/test/stage1/behavior/asm.zig index 48701c5836..845d80777a 100644 --- a/test/stage1/behavior/asm.zig +++ b/test/stage1/behavior/asm.zig @@ -1,5 +1,5 @@ const config = @import("builtin"); -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; comptime { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { @@ -13,7 +13,7 @@ comptime { test "module level assembly" { if (config.arch == config.Arch.x86_64 and config.os == config.Os.linux) { - assertOrPanic(aoeu() == 1234); + expect(aoeu() == 1234); } } diff --git a/test/stage1/behavior/atomics.zig b/test/stage1/behavior/atomics.zig index fa3c5f29a6..daa463fd45 100644 --- a/test/stage1/behavior/atomics.zig +++ b/test/stage1/behavior/atomics.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const builtin = @import("builtin"); const AtomicRmwOp = builtin.AtomicRmwOp; const AtomicOrder = builtin.AtomicOrder; @@ -7,18 +7,18 @@ const AtomicOrder = builtin.AtomicOrder; test "cmpxchg" { var x: i32 = 1234; if (@cmpxchgWeak(i32, &x, 99, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { - assertOrPanic(x1 == 1234); + expect(x1 == 1234); } else { @panic("cmpxchg should have failed"); } while (@cmpxchgWeak(i32, &x, 1234, 5678, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { - assertOrPanic(x1 == 1234); + expect(x1 == 1234); } - assertOrPanic(x == 5678); + expect(x == 5678); - assertOrPanic(@cmpxchgStrong(i32, &x, 5678, 42, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); - assertOrPanic(x == 42); + expect(@cmpxchgStrong(i32, &x, 5678, 42, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); + expect(x == 42); } test "fence" { @@ -30,24 +30,24 @@ test "fence" { test "atomicrmw and atomicload" { var data: u8 = 200; testAtomicRmw(&data); - assertOrPanic(data == 42); + expect(data == 42); testAtomicLoad(&data); } fn testAtomicRmw(ptr: *u8) void { const prev_value = @atomicRmw(u8, ptr, AtomicRmwOp.Xchg, 42, AtomicOrder.SeqCst); - assertOrPanic(prev_value == 200); + expect(prev_value == 200); comptime { var x: i32 = 1234; const y: i32 = 12345; - assertOrPanic(@atomicLoad(i32, &x, AtomicOrder.SeqCst) == 1234); - assertOrPanic(@atomicLoad(i32, &y, AtomicOrder.SeqCst) == 12345); + expect(@atomicLoad(i32, &x, AtomicOrder.SeqCst) == 1234); + expect(@atomicLoad(i32, &y, AtomicOrder.SeqCst) == 12345); } } fn testAtomicLoad(ptr: *u8) void { const x = @atomicLoad(u8, ptr, AtomicOrder.SeqCst); - assertOrPanic(x == 42); + expect(x == 42); } test "cmpxchg with ptr" { @@ -56,16 +56,16 @@ test "cmpxchg with ptr" { var data3: i32 = 9101; var x: *i32 = &data1; if (@cmpxchgWeak(*i32, &x, &data2, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { - assertOrPanic(x1 == &data1); + expect(x1 == &data1); } else { @panic("cmpxchg should have failed"); } while (@cmpxchgWeak(*i32, &x, &data1, &data3, AtomicOrder.SeqCst, AtomicOrder.SeqCst)) |x1| { - assertOrPanic(x1 == &data1); + expect(x1 == &data1); } - assertOrPanic(x == &data3); + expect(x == &data3); - assertOrPanic(@cmpxchgStrong(*i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); - assertOrPanic(x == &data2); + expect(@cmpxchgStrong(*i32, &x, &data3, &data2, AtomicOrder.SeqCst, AtomicOrder.SeqCst) == null); + expect(x == &data2); } diff --git a/test/stage1/behavior/bit_shifting.zig b/test/stage1/behavior/bit_shifting.zig index 3290688358..610acc06c2 100644 --- a/test/stage1/behavior/bit_shifting.zig +++ b/test/stage1/behavior/bit_shifting.zig @@ -1,9 +1,9 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; fn ShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, comptime V: type) type { - assertOrPanic(Key == @IntType(false, Key.bit_count)); - assertOrPanic(Key.bit_count >= mask_bit_count); + expect(Key == @IntType(false, Key.bit_count)); + expect(Key.bit_count >= mask_bit_count); const ShardKey = @IntType(false, mask_bit_count); const shift_amount = Key.bit_count - ShardKey.bit_count; return struct { @@ -77,12 +77,12 @@ fn testShardedTable(comptime Key: type, comptime mask_bit_count: comptime_int, c var node_buffer: [node_count]Table.Node = undefined; for (node_buffer) |*node, i| { const key = @intCast(Key, i); - assertOrPanic(table.get(key) == null); + expect(table.get(key) == null); node.init(key, {}); table.put(node); } for (node_buffer) |*node, i| { - assertOrPanic(table.get(@intCast(Key, i)) == node); + expect(table.get(@intCast(Key, i)) == node); } } diff --git a/test/stage1/behavior/bitcast.zig b/test/stage1/behavior/bitcast.zig index 19030255e4..9607d2e3ef 100644 --- a/test/stage1/behavior/bitcast.zig +++ b/test/stage1/behavior/bitcast.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const maxInt = std.math.maxInt; test "@bitCast i32 -> u32" { @@ -8,8 +8,8 @@ test "@bitCast i32 -> u32" { } fn testBitCast_i32_u32() void { - assertOrPanic(conv(-1) == maxInt(u32)); - assertOrPanic(conv2(maxInt(u32)) == -1); + expect(conv(-1) == maxInt(u32)); + expect(conv2(maxInt(u32)) == -1); } fn conv(x: i32) u32 { @@ -27,7 +27,7 @@ test "@bitCast extern enum to its integer type" { fn testBitCastExternEnum() void { var SOCK_DGRAM = @This().B; var sock_dgram = @bitCast(c_int, SOCK_DGRAM); - assertOrPanic(sock_dgram == 1); + expect(sock_dgram == 1); } }; diff --git a/test/stage1/behavior/bitreverse.zig b/test/stage1/behavior/bitreverse.zig index 97787ace84..3897a3eab7 100644 --- a/test/stage1/behavior/bitreverse.zig +++ b/test/stage1/behavior/bitreverse.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const minInt = std.math.minInt; test "@bitreverse" { @@ -9,73 +9,73 @@ test "@bitreverse" { fn testBitReverse() void { // using comptime_ints, unsigned - assertOrPanic(@bitreverse(u0, 0) == 0); - assertOrPanic(@bitreverse(u5, 0x12) == 0x9); - assertOrPanic(@bitreverse(u8, 0x12) == 0x48); - assertOrPanic(@bitreverse(u16, 0x1234) == 0x2c48); - assertOrPanic(@bitreverse(u24, 0x123456) == 0x6a2c48); - assertOrPanic(@bitreverse(u32, 0x12345678) == 0x1e6a2c48); - assertOrPanic(@bitreverse(u40, 0x123456789a) == 0x591e6a2c48); - assertOrPanic(@bitreverse(u48, 0x123456789abc) == 0x3d591e6a2c48); - assertOrPanic(@bitreverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48); - assertOrPanic(@bitreverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48); - assertOrPanic(@bitreverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48); + expect(@bitreverse(u0, 0) == 0); + expect(@bitreverse(u5, 0x12) == 0x9); + expect(@bitreverse(u8, 0x12) == 0x48); + expect(@bitreverse(u16, 0x1234) == 0x2c48); + expect(@bitreverse(u24, 0x123456) == 0x6a2c48); + expect(@bitreverse(u32, 0x12345678) == 0x1e6a2c48); + expect(@bitreverse(u40, 0x123456789a) == 0x591e6a2c48); + expect(@bitreverse(u48, 0x123456789abc) == 0x3d591e6a2c48); + expect(@bitreverse(u56, 0x123456789abcde) == 0x7b3d591e6a2c48); + expect(@bitreverse(u64, 0x123456789abcdef1) == 0x8f7b3d591e6a2c48); + expect(@bitreverse(u128, 0x123456789abcdef11121314151617181) == 0x818e868a828c84888f7b3d591e6a2c48); // using runtime uints, unsigned var num0: u0 = 0; - assertOrPanic(@bitreverse(u0, num0) == 0); + expect(@bitreverse(u0, num0) == 0); var num5: u5 = 0x12; - assertOrPanic(@bitreverse(u5, num5) == 0x9); + expect(@bitreverse(u5, num5) == 0x9); var num8: u8 = 0x12; - assertOrPanic(@bitreverse(u8, num8) == 0x48); + expect(@bitreverse(u8, num8) == 0x48); var num16: u16 = 0x1234; - assertOrPanic(@bitreverse(u16, num16) == 0x2c48); + expect(@bitreverse(u16, num16) == 0x2c48); var num24: u24 = 0x123456; - assertOrPanic(@bitreverse(u24, num24) == 0x6a2c48); + expect(@bitreverse(u24, num24) == 0x6a2c48); var num32: u32 = 0x12345678; - assertOrPanic(@bitreverse(u32, num32) == 0x1e6a2c48); + expect(@bitreverse(u32, num32) == 0x1e6a2c48); var num40: u40 = 0x123456789a; - assertOrPanic(@bitreverse(u40, num40) == 0x591e6a2c48); + expect(@bitreverse(u40, num40) == 0x591e6a2c48); var num48: u48 = 0x123456789abc; - assertOrPanic(@bitreverse(u48, num48) == 0x3d591e6a2c48); + expect(@bitreverse(u48, num48) == 0x3d591e6a2c48); var num56: u56 = 0x123456789abcde; - assertOrPanic(@bitreverse(u56, num56) == 0x7b3d591e6a2c48); + expect(@bitreverse(u56, num56) == 0x7b3d591e6a2c48); var num64: u64 = 0x123456789abcdef1; - assertOrPanic(@bitreverse(u64, num64) == 0x8f7b3d591e6a2c48); + expect(@bitreverse(u64, num64) == 0x8f7b3d591e6a2c48); var num128: u128 = 0x123456789abcdef11121314151617181; - assertOrPanic(@bitreverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48); + expect(@bitreverse(u128, num128) == 0x818e868a828c84888f7b3d591e6a2c48); // using comptime_ints, signed, positive - assertOrPanic(@bitreverse(i0, 0) == 0); - assertOrPanic(@bitreverse(i8, @bitCast(i8, u8(0x92))) == @bitCast(i8, u8(0x49))); - assertOrPanic(@bitreverse(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x2c48))); - assertOrPanic(@bitreverse(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x6a2c48))); - assertOrPanic(@bitreverse(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x1e6a2c48))); - assertOrPanic(@bitreverse(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40(0x591e6a2c48))); - assertOrPanic(@bitreverse(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0x3d591e6a2c48))); - assertOrPanic(@bitreverse(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0x7b3d591e6a2c48))); - assertOrPanic(@bitreverse(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0x8f7b3d591e6a2c48))); - assertOrPanic(@bitreverse(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) == @bitCast(i128, u128(0x818e868a828c84888f7b3d591e6a2c48))); + expect(@bitreverse(i0, 0) == 0); + expect(@bitreverse(i8, @bitCast(i8, u8(0x92))) == @bitCast(i8, u8(0x49))); + expect(@bitreverse(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x2c48))); + expect(@bitreverse(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x6a2c48))); + expect(@bitreverse(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x1e6a2c48))); + expect(@bitreverse(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40(0x591e6a2c48))); + expect(@bitreverse(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0x3d591e6a2c48))); + expect(@bitreverse(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0x7b3d591e6a2c48))); + expect(@bitreverse(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0x8f7b3d591e6a2c48))); + expect(@bitreverse(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) == @bitCast(i128, u128(0x818e868a828c84888f7b3d591e6a2c48))); // using comptime_ints, signed, negative. Compare to runtime ints returned from llvm. var neg5: i5 = minInt(i5) + 1; - assertOrPanic(@bitreverse(i5, minInt(i5) + 1) == @bitreverse(i5, neg5)); + expect(@bitreverse(i5, minInt(i5) + 1) == @bitreverse(i5, neg5)); var neg8: i8 = -18; - assertOrPanic(@bitreverse(i8, -18) == @bitreverse(i8, neg8)); + expect(@bitreverse(i8, -18) == @bitreverse(i8, neg8)); var neg16: i16 = -32694; - assertOrPanic(@bitreverse(i16, -32694) == @bitreverse(i16, neg16)); + expect(@bitreverse(i16, -32694) == @bitreverse(i16, neg16)); var neg24: i24 = -6773785; - assertOrPanic(@bitreverse(i24, -6773785) == @bitreverse(i24, neg24)); + expect(@bitreverse(i24, -6773785) == @bitreverse(i24, neg24)); var neg32: i32 = -16773785; - assertOrPanic(@bitreverse(i32, -16773785) == @bitreverse(i32, neg32)); + expect(@bitreverse(i32, -16773785) == @bitreverse(i32, neg32)); var neg40: i40 = minInt(i40) + 12345; - assertOrPanic(@bitreverse(i40, minInt(i40) + 12345) == @bitreverse(i40, neg40)); + expect(@bitreverse(i40, minInt(i40) + 12345) == @bitreverse(i40, neg40)); var neg48: i48 = minInt(i48) + 12345; - assertOrPanic(@bitreverse(i48, minInt(i48) + 12345) == @bitreverse(i48, neg48)); + expect(@bitreverse(i48, minInt(i48) + 12345) == @bitreverse(i48, neg48)); var neg56: i56 = minInt(i56) + 12345; - assertOrPanic(@bitreverse(i56, minInt(i56) + 12345) == @bitreverse(i56, neg56)); + expect(@bitreverse(i56, minInt(i56) + 12345) == @bitreverse(i56, neg56)); var neg64: i64 = minInt(i64) + 12345; - assertOrPanic(@bitreverse(i64, minInt(i64) + 12345) == @bitreverse(i64, neg64)); + expect(@bitreverse(i64, minInt(i64) + 12345) == @bitreverse(i64, neg64)); var neg128: i128 = minInt(i128) + 12345; - assertOrPanic(@bitreverse(i128, minInt(i128) + 12345) == @bitreverse(i128, neg128)); + expect(@bitreverse(i128, minInt(i128) + 12345) == @bitreverse(i128, neg128)); } diff --git a/test/stage1/behavior/bool.zig b/test/stage1/behavior/bool.zig index 2d7241526f..dfc2279005 100644 --- a/test/stage1/behavior/bool.zig +++ b/test/stage1/behavior/bool.zig @@ -1,25 +1,25 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "bool literals" { - assertOrPanic(true); - assertOrPanic(!false); + expect(true); + expect(!false); } test "cast bool to int" { const t = true; const f = false; - assertOrPanic(@boolToInt(t) == u32(1)); - assertOrPanic(@boolToInt(f) == u32(0)); + expect(@boolToInt(t) == u32(1)); + expect(@boolToInt(f) == u32(0)); nonConstCastBoolToInt(t, f); } fn nonConstCastBoolToInt(t: bool, f: bool) void { - assertOrPanic(@boolToInt(t) == u32(1)); - assertOrPanic(@boolToInt(f) == u32(0)); + expect(@boolToInt(t) == u32(1)); + expect(@boolToInt(f) == u32(0)); } test "bool cmp" { - assertOrPanic(testBoolCmp(true, false) == false); + expect(testBoolCmp(true, false) == false); } fn testBoolCmp(a: bool, b: bool) bool { return a == b; @@ -30,6 +30,6 @@ const global_t = true; const not_global_f = !global_f; const not_global_t = !global_t; test "compile time bool not" { - assertOrPanic(not_global_f); - assertOrPanic(!not_global_t); + expect(not_global_f); + expect(!not_global_t); } diff --git a/test/stage1/behavior/bswap.zig b/test/stage1/behavior/bswap.zig index 8084538e03..beffa0f73a 100644 --- a/test/stage1/behavior/bswap.zig +++ b/test/stage1/behavior/bswap.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "@bswap" { comptime testByteSwap(); @@ -7,26 +7,26 @@ test "@bswap" { } fn testByteSwap() void { - assertOrPanic(@bswap(u0, 0) == 0); - assertOrPanic(@bswap(u8, 0x12) == 0x12); - assertOrPanic(@bswap(u16, 0x1234) == 0x3412); - assertOrPanic(@bswap(u24, 0x123456) == 0x563412); - assertOrPanic(@bswap(u32, 0x12345678) == 0x78563412); - assertOrPanic(@bswap(u40, 0x123456789a) == 0x9a78563412); - assertOrPanic(@bswap(u48, 0x123456789abc) == 0xbc9a78563412); - assertOrPanic(@bswap(u56, 0x123456789abcde) == 0xdebc9a78563412); - assertOrPanic(@bswap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412); - assertOrPanic(@bswap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412); + expect(@bswap(u0, 0) == 0); + expect(@bswap(u8, 0x12) == 0x12); + expect(@bswap(u16, 0x1234) == 0x3412); + expect(@bswap(u24, 0x123456) == 0x563412); + expect(@bswap(u32, 0x12345678) == 0x78563412); + expect(@bswap(u40, 0x123456789a) == 0x9a78563412); + expect(@bswap(u48, 0x123456789abc) == 0xbc9a78563412); + expect(@bswap(u56, 0x123456789abcde) == 0xdebc9a78563412); + expect(@bswap(u64, 0x123456789abcdef1) == 0xf1debc9a78563412); + expect(@bswap(u128, 0x123456789abcdef11121314151617181) == 0x8171615141312111f1debc9a78563412); - assertOrPanic(@bswap(i0, 0) == 0); - assertOrPanic(@bswap(i8, -50) == -50); - assertOrPanic(@bswap(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x3412))); - assertOrPanic(@bswap(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x563412))); - assertOrPanic(@bswap(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x78563412))); - assertOrPanic(@bswap(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40(0x9a78563412))); - assertOrPanic(@bswap(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0xbc9a78563412))); - assertOrPanic(@bswap(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0xdebc9a78563412))); - assertOrPanic(@bswap(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0xf1debc9a78563412))); - assertOrPanic(@bswap(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) == + expect(@bswap(i0, 0) == 0); + expect(@bswap(i8, -50) == -50); + expect(@bswap(i16, @bitCast(i16, u16(0x1234))) == @bitCast(i16, u16(0x3412))); + expect(@bswap(i24, @bitCast(i24, u24(0x123456))) == @bitCast(i24, u24(0x563412))); + expect(@bswap(i32, @bitCast(i32, u32(0x12345678))) == @bitCast(i32, u32(0x78563412))); + expect(@bswap(i40, @bitCast(i40, u40(0x123456789a))) == @bitCast(i40, u40(0x9a78563412))); + expect(@bswap(i48, @bitCast(i48, u48(0x123456789abc))) == @bitCast(i48, u48(0xbc9a78563412))); + expect(@bswap(i56, @bitCast(i56, u56(0x123456789abcde))) == @bitCast(i56, u56(0xdebc9a78563412))); + expect(@bswap(i64, @bitCast(i64, u64(0x123456789abcdef1))) == @bitCast(i64, u64(0xf1debc9a78563412))); + expect(@bswap(i128, @bitCast(i128, u128(0x123456789abcdef11121314151617181))) == @bitCast(i128, u128(0x8171615141312111f1debc9a78563412))); } diff --git a/test/stage1/behavior/bugs/1076.zig b/test/stage1/behavior/bugs/1076.zig index 69a7e70f7d..9dc1d111ea 100644 --- a/test/stage1/behavior/bugs/1076.zig +++ b/test/stage1/behavior/bugs/1076.zig @@ -1,6 +1,6 @@ const std = @import("std"); const mem = std.mem; -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "comptime code should not modify constant data" { testCastPtrOfArrayToSliceAndPtr(); @@ -11,6 +11,6 @@ fn testCastPtrOfArrayToSliceAndPtr() void { var array = "aoeu"; const x: [*]u8 = &array; x[0] += 1; - assertOrPanic(mem.eql(u8, array[0..], "boeu")); + expect(mem.eql(u8, array[0..], "boeu")); } diff --git a/test/stage1/behavior/bugs/1277.zig b/test/stage1/behavior/bugs/1277.zig index a83e7653e2..3aa1db2ea0 100644 --- a/test/stage1/behavior/bugs/1277.zig +++ b/test/stage1/behavior/bugs/1277.zig @@ -11,5 +11,5 @@ fn f() i32 { } test "don't emit an LLVM global for a const function when it's in an optional in a struct" { - std.debug.assertOrPanic(s.f.?() == 1234); + std.testing.expect(s.f.?() == 1234); } diff --git a/test/stage1/behavior/bugs/1322.zig b/test/stage1/behavior/bugs/1322.zig index 2e67f4473f..f1d61baa3a 100644 --- a/test/stage1/behavior/bugs/1322.zig +++ b/test/stage1/behavior/bugs/1322.zig @@ -13,7 +13,7 @@ const C = struct {}; test "tagged union with all void fields but a meaningful tag" { var a: A = A{ .b = B{ .c = C{} } }; - std.debug.assertOrPanic(@TagType(B)(a.b) == @TagType(B).c); + std.testing.expect(@TagType(B)(a.b) == @TagType(B).c); a = A{ .b = B.None }; - std.debug.assertOrPanic(@TagType(B)(a.b) == @TagType(B).None); + std.testing.expect(@TagType(B)(a.b) == @TagType(B).None); } diff --git a/test/stage1/behavior/bugs/1381.zig b/test/stage1/behavior/bugs/1381.zig index 2d452da156..91c994d7c0 100644 --- a/test/stage1/behavior/bugs/1381.zig +++ b/test/stage1/behavior/bugs/1381.zig @@ -17,5 +17,5 @@ test "union that needs padding bytes inside an array" { }; const a = as[0].B; - std.debug.assertOrPanic(a.D == 1); + std.testing.expect(a.D == 1); } diff --git a/test/stage1/behavior/bugs/1421.zig b/test/stage1/behavior/bugs/1421.zig index fbc932781a..48cf1ae2a6 100644 --- a/test/stage1/behavior/bugs/1421.zig +++ b/test/stage1/behavior/bugs/1421.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const S = struct { fn method() builtin.TypeInfo { @@ -10,5 +10,5 @@ const S = struct { test "functions with return type required to be comptime are generic" { const ti = S.method(); - assertOrPanic(builtin.TypeId(ti) == builtin.TypeId.Struct); + expect(builtin.TypeId(ti) == builtin.TypeId.Struct); } diff --git a/test/stage1/behavior/bugs/1442.zig b/test/stage1/behavior/bugs/1442.zig index e9dfd5d2ce..d5ea3f66fe 100644 --- a/test/stage1/behavior/bugs/1442.zig +++ b/test/stage1/behavior/bugs/1442.zig @@ -7,5 +7,5 @@ const Union = union(enum) { test "const error union field alignment" { var union_or_err: anyerror!Union = Union{ .Color = 1234 }; - std.debug.assertOrPanic((union_or_err catch unreachable).Color == 1234); + std.testing.expect((union_or_err catch unreachable).Color == 1234); } diff --git a/test/stage1/behavior/bugs/1486.zig b/test/stage1/behavior/bugs/1486.zig index 0483e3828c..d1bb8d7053 100644 --- a/test/stage1/behavior/bugs/1486.zig +++ b/test/stage1/behavior/bugs/1486.zig @@ -1,11 +1,11 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const ptr = &global; var global: u64 = 123; test "constant pointer to global variable causes runtime load" { global = 1234; - assertOrPanic(&global == ptr); - assertOrPanic(ptr.* == 1234); + expect(&global == ptr); + expect(ptr.* == 1234); } diff --git a/test/stage1/behavior/bugs/394.zig b/test/stage1/behavior/bugs/394.zig index 766ad9e157..b1f0b6b605 100644 --- a/test/stage1/behavior/bugs/394.zig +++ b/test/stage1/behavior/bugs/394.zig @@ -7,12 +7,12 @@ const S = struct { y: E, }; -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "bug 394 fixed" { const x = S{ .x = 3, .y = E{ .B = 1 }, }; - assertOrPanic(x.x == 3); + expect(x.x == 3); } diff --git a/test/stage1/behavior/bugs/655.zig b/test/stage1/behavior/bugs/655.zig index 67ba6a231f..d4491bfc27 100644 --- a/test/stage1/behavior/bugs/655.zig +++ b/test/stage1/behavior/bugs/655.zig @@ -3,10 +3,10 @@ const other_file = @import("655_other_file.zig"); test "function with *const parameter with type dereferenced by namespace" { const x: other_file.Integer = 1234; - comptime std.debug.assertOrPanic(@typeOf(&x) == *const other_file.Integer); + comptime std.testing.expect(@typeOf(&x) == *const other_file.Integer); foo(&x); } fn foo(x: *const other_file.Integer) void { - std.debug.assertOrPanic(x.* == 1234); + std.testing.expect(x.* == 1234); } diff --git a/test/stage1/behavior/bugs/656.zig b/test/stage1/behavior/bugs/656.zig index cb37fe67fe..159ec52d43 100644 --- a/test/stage1/behavior/bugs/656.zig +++ b/test/stage1/behavior/bugs/656.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const PrefixOp = union(enum) { Return, @@ -22,7 +22,7 @@ fn foo(a: bool, b: bool) void { PrefixOp.AddrOf => |addr_of_info| { if (b) {} if (addr_of_info.align_expr) |align_expr| { - assertOrPanic(align_expr == 1234); + expect(align_expr == 1234); } }, PrefixOp.Return => {}, diff --git a/test/stage1/behavior/bugs/726.zig b/test/stage1/behavior/bugs/726.zig index ce20480c63..dd2a135b56 100644 --- a/test/stage1/behavior/bugs/726.zig +++ b/test/stage1/behavior/bugs/726.zig @@ -1,9 +1,9 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "@ptrCast from const to nullable" { const c: u8 = 4; var x: ?*const u8 = @ptrCast(?*const u8, &c); - assertOrPanic(x.?.* == 4); + expect(x.?.* == 4); } test "@ptrCast from var in empty struct to nullable" { @@ -11,6 +11,6 @@ test "@ptrCast from var in empty struct to nullable" { var c: u8 = 4; }; var x: ?*const u8 = @ptrCast(?*const u8, &container.c); - assertOrPanic(x.?.* == 4); + expect(x.?.* == 4); } diff --git a/test/stage1/behavior/bugs/920.zig b/test/stage1/behavior/bugs/920.zig index e29c5c4acf..10c002f6ba 100644 --- a/test/stage1/behavior/bugs/920.zig +++ b/test/stage1/behavior/bugs/920.zig @@ -60,6 +60,6 @@ test "bug 920 fixed" { }; for (NormalDist1.f) |_, i| { - std.debug.assertOrPanic(NormalDist1.f[i] == NormalDist.f[i]); + std.testing.expect(NormalDist1.f[i] == NormalDist.f[i]); } } diff --git a/test/stage1/behavior/byval_arg_var.zig b/test/stage1/behavior/byval_arg_var.zig index 14ee212ce0..3794a965c6 100644 --- a/test/stage1/behavior/byval_arg_var.zig +++ b/test/stage1/behavior/byval_arg_var.zig @@ -6,7 +6,7 @@ test "pass string literal byvalue to a generic var param" { start(); blowUpStack(10); - std.debug.assertOrPanic(std.mem.eql(u8, result, "string literal")); + std.testing.expect(std.mem.eql(u8, result, "string literal")); } fn start() void { diff --git a/test/stage1/behavior/cancel.zig b/test/stage1/behavior/cancel.zig index 863da4bdb8..7fadf7f230 100644 --- a/test/stage1/behavior/cancel.zig +++ b/test/stage1/behavior/cancel.zig @@ -10,9 +10,9 @@ test "cancel forwards" { const p = async<&da.allocator> f1() catch unreachable; cancel p; - std.debug.assertOrPanic(defer_f1); - std.debug.assertOrPanic(defer_f2); - std.debug.assertOrPanic(defer_f3); + std.testing.expect(defer_f1); + std.testing.expect(defer_f2); + std.testing.expect(defer_f3); } async fn f1() void { @@ -47,10 +47,10 @@ test "cancel backwards" { const p = async<&da.allocator> b1() catch unreachable; cancel p; - std.debug.assertOrPanic(defer_b1); - std.debug.assertOrPanic(defer_b2); - std.debug.assertOrPanic(defer_b3); - std.debug.assertOrPanic(defer_b4); + std.testing.expect(defer_b1); + std.testing.expect(defer_b2); + std.testing.expect(defer_b3); + std.testing.expect(defer_b4); } async fn b1() void { diff --git a/test/stage1/behavior/cast.zig b/test/stage1/behavior/cast.zig index 27f685a96e..8ed03f4936 100644 --- a/test/stage1/behavior/cast.zig +++ b/test/stage1/behavior/cast.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const mem = std.mem; const maxInt = std.math.maxInt; @@ -7,12 +7,12 @@ test "int to ptr cast" { const x = usize(13); const y = @intToPtr(*u8, x); const z = @ptrToInt(y); - assertOrPanic(z == 13); + expect(z == 13); } test "integer literal to pointer cast" { const vga_mem = @intToPtr(*u16, 0xB8000); - assertOrPanic(@ptrToInt(vga_mem) == 0xB8000); + expect(@ptrToInt(vga_mem) == 0xB8000); } test "pointer reinterpret const float to int" { @@ -20,7 +20,7 @@ test "pointer reinterpret const float to int" { const float_ptr = &float; const int_ptr = @ptrCast(*const i32, float_ptr); const int_val = int_ptr.*; - assertOrPanic(int_val == 858993411); + expect(int_val == 858993411); } test "implicitly cast indirect pointer to maybe-indirect pointer" { @@ -44,10 +44,10 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" { const p = &s; const q = &p; const r = &q; - assertOrPanic(42 == S.constConst(q)); - assertOrPanic(42 == S.maybeConstConst(q)); - assertOrPanic(42 == S.constConstConst(r)); - assertOrPanic(42 == S.maybeConstConstConst(r)); + expect(42 == S.constConst(q)); + expect(42 == S.maybeConstConst(q)); + expect(42 == S.constConstConst(r)); + expect(42 == S.maybeConstConstConst(r)); } test "explicit cast from integer to error type" { @@ -57,14 +57,14 @@ test "explicit cast from integer to error type" { fn testCastIntToErr(err: anyerror) void { const x = @errorToInt(err); const y = @intToError(x); - assertOrPanic(error.ItBroke == y); + expect(error.ItBroke == y); } test "peer resolve arrays of different size to const slice" { - assertOrPanic(mem.eql(u8, boolToStr(true), "true")); - assertOrPanic(mem.eql(u8, boolToStr(false), "false")); - comptime assertOrPanic(mem.eql(u8, boolToStr(true), "true")); - comptime assertOrPanic(mem.eql(u8, boolToStr(false), "false")); + expect(mem.eql(u8, boolToStr(true), "true")); + expect(mem.eql(u8, boolToStr(false), "false")); + comptime expect(mem.eql(u8, boolToStr(true), "true")); + comptime expect(mem.eql(u8, boolToStr(false), "false")); } fn boolToStr(b: bool) []const u8 { return if (b) "true" else "false"; @@ -77,8 +77,8 @@ test "peer resolve array and const slice" { fn testPeerResolveArrayConstSlice(b: bool) void { const value1 = if (b) "aoeu" else ([]const u8)("zz"); const value2 = if (b) ([]const u8)("zz") else "aoeu"; - assertOrPanic(mem.eql(u8, value1, "aoeu")); - assertOrPanic(mem.eql(u8, value2, "zz")); + expect(mem.eql(u8, value1, "aoeu")); + expect(mem.eql(u8, value2, "zz")); } test "implicitly cast from T to anyerror!?T" { @@ -92,14 +92,14 @@ const A = struct { fn castToOptionalTypeError(z: i32) void { const x = i32(1); const y: anyerror!?i32 = x; - assertOrPanic((try y).? == 1); + expect((try y).? == 1); const f = z; const g: anyerror!?i32 = f; const a = A{ .a = z }; const b: anyerror!?A = a; - assertOrPanic((b catch unreachable).?.a == 1); + expect((b catch unreachable).?.a == 1); } test "implicitly cast from int to anyerror!?T" { @@ -114,7 +114,7 @@ fn implicitIntLitToOptional() void { test "return null from fn() anyerror!?&T" { const a = returnNullFromOptionalTypeErrorRef(); const b = returnNullLitFromOptionalTypeErrorRef(); - assertOrPanic((try a) == null and (try b) == null); + expect((try a) == null and (try b) == null); } fn returnNullFromOptionalTypeErrorRef() anyerror!?*A { const a: ?*A = null; @@ -125,11 +125,11 @@ fn returnNullLitFromOptionalTypeErrorRef() anyerror!?*A { } test "peer type resolution: ?T and T" { - assertOrPanic(peerTypeTAndOptionalT(true, false).? == 0); - assertOrPanic(peerTypeTAndOptionalT(false, false).? == 3); + expect(peerTypeTAndOptionalT(true, false).? == 0); + expect(peerTypeTAndOptionalT(false, false).? == 3); comptime { - assertOrPanic(peerTypeTAndOptionalT(true, false).? == 0); - assertOrPanic(peerTypeTAndOptionalT(false, false).? == 3); + expect(peerTypeTAndOptionalT(true, false).? == 0); + expect(peerTypeTAndOptionalT(false, false).? == 3); } } fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { @@ -141,11 +141,11 @@ fn peerTypeTAndOptionalT(c: bool, b: bool) ?usize { } test "peer type resolution: [0]u8 and []const u8" { - assertOrPanic(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); - assertOrPanic(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); + expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); + expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); comptime { - assertOrPanic(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); - assertOrPanic(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); + expect(peerTypeEmptyArrayAndSlice(true, "hi").len == 0); + expect(peerTypeEmptyArrayAndSlice(false, "hi").len == 1); } } fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { @@ -157,8 +157,8 @@ fn peerTypeEmptyArrayAndSlice(a: bool, slice: []const u8) []const u8 { } test "implicitly cast from [N]T to ?[]const T" { - assertOrPanic(mem.eql(u8, castToOptionalSlice().?, "hi")); - comptime assertOrPanic(mem.eql(u8, castToOptionalSlice().?, "hi")); + expect(mem.eql(u8, castToOptionalSlice().?, "hi")); + comptime expect(mem.eql(u8, castToOptionalSlice().?, "hi")); } fn castToOptionalSlice() ?[]const u8 { @@ -171,7 +171,7 @@ test "implicitly cast from [0]T to anyerror![]T" { } fn testCastZeroArrayToErrSliceMut() void { - assertOrPanic((gimmeErrOrSlice() catch unreachable).len == 0); + expect((gimmeErrOrSlice() catch unreachable).len == 0); } fn gimmeErrOrSlice() anyerror![]u8 { @@ -182,14 +182,14 @@ test "peer type resolution: [0]u8, []const u8, and anyerror![]u8" { { var data = "hi"; const slice = data[0..]; - assertOrPanic((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); - assertOrPanic((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); + expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); + expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); } comptime { var data = "hi"; const slice = data[0..]; - assertOrPanic((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); - assertOrPanic((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); + expect((try peerTypeEmptyArrayAndSliceAndError(true, slice)).len == 0); + expect((try peerTypeEmptyArrayAndSliceAndError(false, slice)).len == 1); } } fn peerTypeEmptyArrayAndSliceAndError(a: bool, slice: []u8) anyerror![]u8 { @@ -207,7 +207,7 @@ test "resolve undefined with integer" { fn testResolveUndefWithInt(b: bool, x: i32) void { const value = if (b) x else undefined; if (b) { - assertOrPanic(value == x); + expect(value == x); } } @@ -219,17 +219,17 @@ test "implicit cast from &const [N]T to []const T" { fn testCastConstArrayRefToConstSlice() void { const blah = "aoeu"; const const_array_ref = &blah; - assertOrPanic(@typeOf(const_array_ref) == *const [4]u8); + expect(@typeOf(const_array_ref) == *const [4]u8); const slice: []const u8 = const_array_ref; - assertOrPanic(mem.eql(u8, slice, "aoeu")); + expect(mem.eql(u8, slice, "aoeu")); } test "peer type resolution: error and [N]T" { // TODO: implicit error!T to error!U where T can implicitly cast to U - //assertOrPanic(mem.eql(u8, try testPeerErrorAndArray(0), "OK")); - //comptime assertOrPanic(mem.eql(u8, try testPeerErrorAndArray(0), "OK")); - assertOrPanic(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK")); - comptime assertOrPanic(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK")); + //expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK")); + //comptime expect(mem.eql(u8, try testPeerErrorAndArray(0), "OK")); + expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK")); + comptime expect(mem.eql(u8, try testPeerErrorAndArray2(1), "OKK")); } //fn testPeerErrorAndArray(x: u8) error![]const u8 { @@ -253,9 +253,9 @@ test "@floatToInt" { fn testFloatToInts() void { const x = i32(1e4); - assertOrPanic(x == 10000); + expect(x == 10000); const y = @floatToInt(i32, f32(1e4)); - assertOrPanic(y == 10000); + expect(y == 10000); expectFloatToInt(f16, 255.1, u8, 255); expectFloatToInt(f16, 127.2, i8, 127); expectFloatToInt(f16, -128.2, i8, -128); @@ -266,7 +266,7 @@ fn testFloatToInts() void { } fn expectFloatToInt(comptime F: type, f: F, comptime I: type, i: I) void { - assertOrPanic(@floatToInt(I, f) == i); + expect(@floatToInt(I, f) == i); } test "cast u128 to f128 and back" { @@ -275,7 +275,7 @@ test "cast u128 to f128 and back" { } fn testCast128() void { - assertOrPanic(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000); + expect(cast128Int(cast128Float(0x7fff0000000000000000000000000000)) == 0x7fff0000000000000000000000000000); } fn cast128Int(x: f128) u128 { @@ -295,9 +295,9 @@ test "const slice widen cast" { }; const u32_value = @bytesToSlice(u32, bytes[0..])[0]; - assertOrPanic(u32_value == 0x12121212); + expect(u32_value == 0x12121212); - assertOrPanic(@bitCast(u32, bytes) == 0x12121212); + expect(@bitCast(u32, bytes) == 0x12121212); } test "single-item pointer of array to slice and to unknown length pointer" { @@ -309,76 +309,76 @@ fn testCastPtrOfArrayToSliceAndPtr() void { var array = "aoeu"; const x: [*]u8 = &array; x[0] += 1; - assertOrPanic(mem.eql(u8, array[0..], "boeu")); + expect(mem.eql(u8, array[0..], "boeu")); const y: []u8 = &array; y[0] += 1; - assertOrPanic(mem.eql(u8, array[0..], "coeu")); + expect(mem.eql(u8, array[0..], "coeu")); } test "cast *[1][*]const u8 to [*]const ?[*]const u8" { const window_name = [1][*]const u8{c"window name"}; const x: [*]const ?[*]const u8 = &window_name; - assertOrPanic(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); + expect(mem.eql(u8, std.cstr.toSliceConst(x[0].?), "window name")); } test "@intCast comptime_int" { const result = @intCast(i32, 1234); - assertOrPanic(@typeOf(result) == i32); - assertOrPanic(result == 1234); + expect(@typeOf(result) == i32); + expect(result == 1234); } test "@floatCast comptime_int and comptime_float" { { const result = @floatCast(f16, 1234); - assertOrPanic(@typeOf(result) == f16); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f16); + expect(result == 1234.0); } { const result = @floatCast(f16, 1234.0); - assertOrPanic(@typeOf(result) == f16); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f16); + expect(result == 1234.0); } { const result = @floatCast(f32, 1234); - assertOrPanic(@typeOf(result) == f32); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f32); + expect(result == 1234.0); } { const result = @floatCast(f32, 1234.0); - assertOrPanic(@typeOf(result) == f32); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f32); + expect(result == 1234.0); } } test "comptime_int @intToFloat" { { const result = @intToFloat(f16, 1234); - assertOrPanic(@typeOf(result) == f16); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f16); + expect(result == 1234.0); } { const result = @intToFloat(f32, 1234); - assertOrPanic(@typeOf(result) == f32); - assertOrPanic(result == 1234.0); + expect(@typeOf(result) == f32); + expect(result == 1234.0); } } test "@bytesToSlice keeps pointer alignment" { var bytes = []u8{ 0x01, 0x02, 0x03, 0x04 }; const numbers = @bytesToSlice(u32, bytes[0..]); - comptime assertOrPanic(@typeOf(numbers) == []align(@alignOf(@typeOf(bytes))) u32); + comptime expect(@typeOf(numbers) == []align(@alignOf(@typeOf(bytes))) u32); } test "@intCast i32 to u7" { var x: u128 = maxInt(u128); var y: i32 = 120; var z = x >> @intCast(u7, y); - assertOrPanic(z == 0xff); + expect(z == 0xff); } test "implicit cast undefined to optional" { - assertOrPanic(MakeType(void).getNull() == null); - assertOrPanic(MakeType(void).getNonNull() != null); + expect(MakeType(void).getNull() == null); + expect(MakeType(void).getNonNull() != null); } fn MakeType(comptime T: type) type { @@ -398,16 +398,16 @@ test "implicit cast from *[N]T to ?[*]T" { var y: [4]u16 = [4]u16{ 0, 1, 2, 3 }; x = &y; - assertOrPanic(std.mem.eql(u16, x.?[0..4], y[0..4])); + expect(std.mem.eql(u16, x.?[0..4], y[0..4])); x.?[0] = 8; y[3] = 6; - assertOrPanic(std.mem.eql(u16, x.?[0..4], y[0..4])); + expect(std.mem.eql(u16, x.?[0..4], y[0..4])); } test "implicit cast from *T to ?*c_void" { var a: u8 = 1; incrementVoidPtrValue(&a); - std.debug.assertOrPanic(a == 2); + std.testing.expect(a == 2); } fn incrementVoidPtrValue(value: ?*c_void) void { @@ -417,7 +417,7 @@ fn incrementVoidPtrValue(value: ?*c_void) void { test "implicit cast from [*]T to ?*c_void" { var a = []u8{ 3, 2, 1 }; incrementVoidPtrArray(a[0..].ptr, 3); - assertOrPanic(std.mem.eql(u8, a, []u8{ 4, 3, 2 })); + expect(std.mem.eql(u8, a, []u8{ 4, 3, 2 })); } fn incrementVoidPtrArray(array: ?*c_void, len: usize) void { @@ -441,27 +441,27 @@ pub const FUNCTION_CONSTANT = @intToPtr(PFN_void, maxInt(usize)); pub const PFN_void = extern fn (*c_void) void; fn foobar(func: PFN_void) void { - std.debug.assertOrPanic(@ptrToInt(func) == maxInt(usize)); + std.testing.expect(@ptrToInt(func) == maxInt(usize)); } test "implicit ptr to *c_void" { var a: u32 = 1; var ptr: *c_void = &a; var b: *u32 = @ptrCast(*u32, ptr); - assertOrPanic(b.* == 1); + expect(b.* == 1); var ptr2: ?*c_void = &a; var c: *u32 = @ptrCast(*u32, ptr2.?); - assertOrPanic(c.* == 1); + expect(c.* == 1); } test "@intCast to comptime_int" { - assertOrPanic(@intCast(comptime_int, 0) == 0); + expect(@intCast(comptime_int, 0) == 0); } test "implicit cast comptime numbers to any type when the value fits" { const a: u64 = 255; var b: u8 = a; - assertOrPanic(b == 255); + expect(b == 255); } test "@intToEnum passed a comptime_int to an enum with one item" { @@ -469,14 +469,14 @@ test "@intToEnum passed a comptime_int to an enum with one item" { A, }; const x = @intToEnum(E, 0); - assertOrPanic(x == E.A); + expect(x == E.A); } test "@intCast to u0 and use the result" { const S = struct { fn doTheTest(zero: u1, one: u1, bigzero: i32) void { - assertOrPanic((one << @intCast(u0, bigzero)) == 1); - assertOrPanic((zero << @intCast(u0, bigzero)) == 0); + expect((one << @intCast(u0, bigzero)) == 1); + expect((zero << @intCast(u0, bigzero)) == 0); } }; S.doTheTest(0, 1, 0); diff --git a/test/stage1/behavior/const_slice_child.zig b/test/stage1/behavior/const_slice_child.zig index 5b9b70a558..57044d8aae 100644 --- a/test/stage1/behavior/const_slice_child.zig +++ b/test/stage1/behavior/const_slice_child.zig @@ -1,5 +1,6 @@ -const debug = @import("std").debug; -const assertOrPanic = debug.assertOrPanic; +const std = @import("std"); +const debug = std.debug; +const expect = std.testing.expect; var argv: [*]const [*]const u8 = undefined; @@ -15,10 +16,10 @@ test "const slice child" { } fn foo(args: [][]const u8) void { - assertOrPanic(args.len == 3); - assertOrPanic(streql(args[0], "one")); - assertOrPanic(streql(args[1], "two")); - assertOrPanic(streql(args[2], "three")); + expect(args.len == 3); + expect(streql(args[0], "one")); + expect(streql(args[1], "two")); + expect(streql(args[2], "three")); } fn bar(argc: usize) void { diff --git a/test/stage1/behavior/coroutine_await_struct.zig b/test/stage1/behavior/coroutine_await_struct.zig index 6ca2a301ec..29f77bf67c 100644 --- a/test/stage1/behavior/coroutine_await_struct.zig +++ b/test/stage1/behavior/coroutine_await_struct.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const Foo = struct { x: i32, @@ -18,8 +18,8 @@ test "coroutine await struct" { await_seq('f'); resume await_a_promise; await_seq('i'); - assertOrPanic(await_final_result.x == 1234); - assertOrPanic(std.mem.eql(u8, await_points, "abcdefghi")); + expect(await_final_result.x == 1234); + expect(std.mem.eql(u8, await_points, "abcdefghi")); } async fn await_amain() void { await_seq('b'); diff --git a/test/stage1/behavior/coroutines.zig b/test/stage1/behavior/coroutines.zig index a2327c5060..be977bbfce 100644 --- a/test/stage1/behavior/coroutines.zig +++ b/test/stage1/behavior/coroutines.zig @@ -1,6 +1,6 @@ const std = @import("std"); const builtin = @import("builtin"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; var x: i32 = 1; @@ -9,9 +9,9 @@ test "create a coroutine and cancel it" { defer da.deinit(); const p = try async<&da.allocator> simpleAsyncFn(); - comptime assertOrPanic(@typeOf(p) == promise->void); + comptime expect(@typeOf(p) == promise->void); cancel p; - assertOrPanic(x == 2); + expect(x == 2); } async fn simpleAsyncFn() void { x += 1; @@ -31,7 +31,7 @@ test "coroutine suspend, resume, cancel" { cancel p; seq('g'); - assertOrPanic(std.mem.eql(u8, points, "abcdefg")); + expect(std.mem.eql(u8, points, "abcdefg")); } async fn testAsyncSeq() void { defer seq('e'); @@ -53,9 +53,9 @@ test "coroutine suspend with block" { defer da.deinit(); const p = try async<&da.allocator> testSuspendBlock(); - std.debug.assertOrPanic(!result); + std.testing.expect(!result); resume a_promise; - std.debug.assertOrPanic(result); + std.testing.expect(result); cancel p; } @@ -63,13 +63,13 @@ var a_promise: promise = undefined; var result = false; async fn testSuspendBlock() void { suspend { - comptime assertOrPanic(@typeOf(@handle()) == promise->void); + comptime expect(@typeOf(@handle()) == promise->void); a_promise = @handle(); } //Test to make sure that @handle() works as advertised (issue #1296) //var our_handle: promise = @handle(); - assertOrPanic(a_promise == @handle()); + expect(a_promise == @handle()); result = true; } @@ -86,8 +86,8 @@ test "coroutine await" { await_seq('f'); resume await_a_promise; await_seq('i'); - assertOrPanic(await_final_result == 1234); - assertOrPanic(std.mem.eql(u8, await_points, "abcdefghi")); + expect(await_final_result == 1234); + expect(std.mem.eql(u8, await_points, "abcdefghi")); } async fn await_amain() void { await_seq('b'); @@ -123,8 +123,8 @@ test "coroutine await early return" { early_seq('a'); const p = async<&da.allocator> early_amain() catch @panic("out of memory"); early_seq('f'); - assertOrPanic(early_final_result == 1234); - assertOrPanic(std.mem.eql(u8, early_points, "abcdef")); + expect(early_final_result == 1234); + expect(std.mem.eql(u8, early_points, "abcdef")); } async fn early_amain() void { early_seq('b'); @@ -170,7 +170,7 @@ test "async function with dot syntax" { defer da.deinit(); const p = try async<&da.allocator> S.foo(); cancel p; - assertOrPanic(S.y == 2); + expect(S.y == 2); } test "async fn pointer in a struct field" { @@ -182,9 +182,9 @@ test "async fn pointer in a struct field" { var da = std.heap.DirectAllocator.init(); defer da.deinit(); const p = (async<&da.allocator> foo.bar(&data)) catch unreachable; - assertOrPanic(data == 2); + expect(data == 2); cancel p; - assertOrPanic(data == 4); + expect(data == 4); } async<*std.mem.Allocator> fn simpleAsyncFn2(y: *i32) void { defer y.* += 2; @@ -230,9 +230,9 @@ async fn suspendThenFail() anyerror!void { } async fn printTrace(p: promise->(anyerror!void)) void { (await p) catch |e| { - std.debug.assertOrPanic(e == error.Fail); + std.testing.expect(e == error.Fail); if (@errorReturnTrace()) |trace| { - assertOrPanic(trace.index == 1); + expect(trace.index == 1); } else switch (builtin.mode) { builtin.Mode.Debug, builtin.Mode.ReleaseSafe => @panic("expected return trace"), builtin.Mode.ReleaseFast, builtin.Mode.ReleaseSmall => {}, @@ -246,7 +246,7 @@ test "break from suspend" { var my_result: i32 = 1; const p = try async testBreakFromSuspend(&my_result); cancel p; - std.debug.assertOrPanic(my_result == 2); + std.testing.expect(my_result == 2); } async fn testBreakFromSuspend(my_result: *i32) void { suspend { diff --git a/test/stage1/behavior/defer.zig b/test/stage1/behavior/defer.zig index 6c6c60311e..0bb9125e7c 100644 --- a/test/stage1/behavior/defer.zig +++ b/test/stage1/behavior/defer.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; var result: [3]u8 = undefined; var index: usize = undefined; @@ -21,18 +21,18 @@ fn runSomeErrorDefers(x: bool) !bool { } test "mixing normal and error defers" { - assertOrPanic(runSomeErrorDefers(true) catch unreachable); - assertOrPanic(result[0] == 'c'); - assertOrPanic(result[1] == 'a'); + expect(runSomeErrorDefers(true) catch unreachable); + expect(result[0] == 'c'); + expect(result[1] == 'a'); const ok = runSomeErrorDefers(false) catch |err| x: { - assertOrPanic(err == error.FalseNotAllowed); + expect(err == error.FalseNotAllowed); break :x true; }; - assertOrPanic(ok); - assertOrPanic(result[0] == 'c'); - assertOrPanic(result[1] == 'b'); - assertOrPanic(result[2] == 'a'); + expect(ok); + expect(result[0] == 'c'); + expect(result[1] == 'b'); + expect(result[2] == 'a'); } test "break and continue inside loop inside defer expression" { @@ -47,7 +47,7 @@ fn testBreakContInDefer(x: usize) void { if (i < 5) continue; if (i == 5) break; } - assertOrPanic(i == 5); + expect(i == 5); } } @@ -59,11 +59,11 @@ test "defer and labeled break" { break :blk; } - assertOrPanic(i == 1); + expect(i == 1); } test "errdefer does not apply to fn inside fn" { - if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| assertOrPanic(e == error.Bad); + if (testNestedFnErrDefer()) |_| @panic("expected error") else |e| expect(e == error.Bad); } fn testNestedFnErrDefer() anyerror!void { diff --git a/test/stage1/behavior/enum.zig b/test/stage1/behavior/enum.zig index 9de138ef78..899aeea67d 100644 --- a/test/stage1/behavior/enum.zig +++ b/test/stage1/behavior/enum.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; test "enum type" { @@ -11,16 +11,16 @@ test "enum type" { }; const bar = Bar.B; - assertOrPanic(bar == Bar.B); - assertOrPanic(@memberCount(Foo) == 3); - assertOrPanic(@memberCount(Bar) == 4); - assertOrPanic(@sizeOf(Foo) == @sizeOf(FooNoVoid)); - assertOrPanic(@sizeOf(Bar) == 1); + expect(bar == Bar.B); + expect(@memberCount(Foo) == 3); + expect(@memberCount(Bar) == 4); + expect(@sizeOf(Foo) == @sizeOf(FooNoVoid)); + expect(@sizeOf(Bar) == 1); } test "enum as return value" { switch (returnAnInt(13)) { - Foo.One => |value| assertOrPanic(value == 13), + Foo.One => |value| expect(value == 13), else => unreachable, } } @@ -92,14 +92,14 @@ test "enum to int" { } fn shouldEqual(n: Number, expected: u3) void { - assertOrPanic(@enumToInt(n) == expected); + expect(@enumToInt(n) == expected); } test "int to enum" { testIntToEnumEval(3); } fn testIntToEnumEval(x: i32) void { - assertOrPanic(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three); + expect(@intToEnum(IntToEnumNumber, @intCast(u3, x)) == IntToEnumNumber.Three); } const IntToEnumNumber = enum { Zero, @@ -110,8 +110,8 @@ const IntToEnumNumber = enum { }; test "@tagName" { - assertOrPanic(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three")); - comptime assertOrPanic(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three")); + expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three")); + comptime expect(mem.eql(u8, testEnumTagNameBare(BareNumber.Three), "Three")); } fn testEnumTagNameBare(n: BareNumber) []const u8 { @@ -126,8 +126,8 @@ const BareNumber = enum { test "enum alignment" { comptime { - assertOrPanic(@alignOf(AlignTestEnum) >= @alignOf([9]u8)); - assertOrPanic(@alignOf(AlignTestEnum) >= @alignOf(u64)); + expect(@alignOf(AlignTestEnum) >= @alignOf([9]u8)); + expect(@alignOf(AlignTestEnum) >= @alignOf(u64)); } } @@ -663,10 +663,10 @@ const ValueCount257 = enum { test "enum sizes" { comptime { - assertOrPanic(@sizeOf(ValueCount1) == 0); - assertOrPanic(@sizeOf(ValueCount2) == 1); - assertOrPanic(@sizeOf(ValueCount256) == 1); - assertOrPanic(@sizeOf(ValueCount257) == 2); + expect(@sizeOf(ValueCount1) == 0); + expect(@sizeOf(ValueCount2) == 1); + expect(@sizeOf(ValueCount256) == 1); + expect(@sizeOf(ValueCount257) == 2); } } @@ -685,12 +685,12 @@ test "set enum tag type" { { var x = Small.One; x = Small.Two; - comptime assertOrPanic(@TagType(Small) == u2); + comptime expect(@TagType(Small) == u2); } { var x = Small2.One; x = Small2.Two; - comptime assertOrPanic(@TagType(Small2) == u2); + comptime expect(@TagType(Small2) == u2); } } @@ -737,17 +737,17 @@ const bit_field_1 = BitFieldOfEnums{ test "bit field access with enum fields" { var data = bit_field_1; - assertOrPanic(getA(&data) == A.Two); - assertOrPanic(getB(&data) == B.Three3); - assertOrPanic(getC(&data) == C.Four4); - comptime assertOrPanic(@sizeOf(BitFieldOfEnums) == 1); + expect(getA(&data) == A.Two); + expect(getB(&data) == B.Three3); + expect(getC(&data) == C.Four4); + comptime expect(@sizeOf(BitFieldOfEnums) == 1); data.b = B.Four3; - assertOrPanic(data.b == B.Four3); + expect(data.b == B.Four3); data.a = A.Three; - assertOrPanic(data.a == A.Three); - assertOrPanic(data.b == B.Four3); + expect(data.a == A.Three); + expect(data.b == B.Four3); } fn getA(data: *const BitFieldOfEnums) A { @@ -768,7 +768,7 @@ test "casting enum to its tag type" { } fn testCastEnumToTagType(value: Small2) void { - assertOrPanic(@enumToInt(value) == 1); + expect(@enumToInt(value) == 1); } const MultipleChoice = enum(u32) { @@ -784,8 +784,8 @@ test "enum with specified tag values" { } fn testEnumWithSpecifiedTagValues(x: MultipleChoice) void { - assertOrPanic(@enumToInt(x) == 60); - assertOrPanic(1234 == switch (x) { + expect(@enumToInt(x) == 60); + expect(1234 == switch (x) { MultipleChoice.A => 1, MultipleChoice.B => 2, MultipleChoice.C => u32(1234), @@ -811,8 +811,8 @@ test "enum with specified and unspecified tag values" { } fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void { - assertOrPanic(@enumToInt(x) == 1000); - assertOrPanic(1234 == switch (x) { + expect(@enumToInt(x) == 1000); + expect(1234 == switch (x) { MultipleChoice2.A => 1, MultipleChoice2.B => 2, MultipleChoice2.C => 3, @@ -826,8 +826,8 @@ fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void { } test "cast integer literal to enum" { - assertOrPanic(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1); - assertOrPanic(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B); + expect(@intToEnum(MultipleChoice2, 0) == MultipleChoice2.Unspecified1); + expect(@intToEnum(MultipleChoice2, 40) == MultipleChoice2.B); } const EnumWithOneMember = enum { @@ -865,14 +865,14 @@ const EnumWithTagValues = enum(u4) { D = 1 << 3, }; test "enum with tag values don't require parens" { - assertOrPanic(@enumToInt(EnumWithTagValues.C) == 0b0100); + expect(@enumToInt(EnumWithTagValues.C) == 0b0100); } test "enum with 1 field but explicit tag type should still have the tag type" { const Enum = enum(u8) { B = 2, }; - comptime @import("std").debug.assertOrPanic(@sizeOf(Enum) == @sizeOf(u8)); + comptime @import("std").testing.expect(@sizeOf(Enum) == @sizeOf(u8)); } test "empty extern enum with members" { @@ -881,7 +881,7 @@ test "empty extern enum with members" { B, C, }; - assertOrPanic(@sizeOf(E) == @sizeOf(c_int)); + expect(@sizeOf(E) == @sizeOf(c_int)); } test "tag name with assigned enum values" { @@ -890,5 +890,5 @@ test "tag name with assigned enum values" { B = 0, }; var b = LocalFoo.B; - assertOrPanic(mem.eql(u8, @tagName(b), "B")); + expect(mem.eql(u8, @tagName(b), "B")); } diff --git a/test/stage1/behavior/enum_with_members.zig b/test/stage1/behavior/enum_with_members.zig index 49af1ceae7..2e022a3427 100644 --- a/test/stage1/behavior/enum_with_members.zig +++ b/test/stage1/behavior/enum_with_members.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; const fmt = @import("std").fmt; @@ -19,9 +19,9 @@ test "enum with members" { const b = ET{ .UINT = 42 }; var buf: [20]u8 = undefined; - assertOrPanic((a.print(buf[0..]) catch unreachable) == 3); - assertOrPanic(mem.eql(u8, buf[0..3], "-42")); + expect((a.print(buf[0..]) catch unreachable) == 3); + expect(mem.eql(u8, buf[0..3], "-42")); - assertOrPanic((b.print(buf[0..]) catch unreachable) == 2); - assertOrPanic(mem.eql(u8, buf[0..2], "42")); + expect((b.print(buf[0..]) catch unreachable) == 2); + expect(mem.eql(u8, buf[0..2], "42")); } diff --git a/test/stage1/behavior/error.zig b/test/stage1/behavior/error.zig index c7e38712bc..265ddd9d6c 100644 --- a/test/stage1/behavior/error.zig +++ b/test/stage1/behavior/error.zig @@ -1,6 +1,6 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; -const assertError = std.debug.assertError; +const expect = std.testing.expect; +const expectError = std.testing.expectError; const mem = std.mem; const builtin = @import("builtin"); @@ -19,7 +19,7 @@ pub fn baz() anyerror!i32 { } test "error wrapping" { - assertOrPanic((baz() catch unreachable) == 15); + expect((baz() catch unreachable) == 15); } fn gimmeItBroke() []const u8 { @@ -27,14 +27,14 @@ fn gimmeItBroke() []const u8 { } test "@errorName" { - assertOrPanic(mem.eql(u8, @errorName(error.AnError), "AnError")); - assertOrPanic(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName")); + expect(mem.eql(u8, @errorName(error.AnError), "AnError")); + expect(mem.eql(u8, @errorName(error.ALongerErrorName), "ALongerErrorName")); } test "error values" { const a = @errorToInt(error.err1); const b = @errorToInt(error.err2); - assertOrPanic(a != b); + expect(a != b); } test "redefinition of error values allowed" { @@ -47,8 +47,8 @@ fn shouldBeNotEqual(a: anyerror, b: anyerror) void { test "error binary operator" { const a = errBinaryOperatorG(true) catch 3; const b = errBinaryOperatorG(false) catch 3; - assertOrPanic(a == 3); - assertOrPanic(b == 10); + expect(a == 3); + expect(b == 10); } fn errBinaryOperatorG(x: bool) anyerror!isize { return if (x) error.ItBroke else isize(10); @@ -56,7 +56,7 @@ fn errBinaryOperatorG(x: bool) anyerror!isize { test "unwrap simple value from error" { const i = unwrapSimpleValueFromErrorDo() catch unreachable; - assertOrPanic(i == 13); + expect(i == 13); } fn unwrapSimpleValueFromErrorDo() anyerror!isize { return 13; @@ -82,10 +82,10 @@ test "error union type " { fn testErrorUnionType() void { const x: anyerror!i32 = 1234; - if (x) |value| assertOrPanic(value == 1234) else |_| unreachable; - assertOrPanic(@typeId(@typeOf(x)) == builtin.TypeId.ErrorUnion); - assertOrPanic(@typeId(@typeOf(x).ErrorSet) == builtin.TypeId.ErrorSet); - assertOrPanic(@typeOf(x).ErrorSet == anyerror); + if (x) |value| expect(value == 1234) else |_| unreachable; + expect(@typeId(@typeOf(x)) == builtin.TypeId.ErrorUnion); + expect(@typeId(@typeOf(x).ErrorSet) == builtin.TypeId.ErrorSet); + expect(@typeOf(x).ErrorSet == anyerror); } test "error set type" { @@ -99,12 +99,12 @@ const MyErrSet = error{ }; fn testErrorSetType() void { - assertOrPanic(@memberCount(MyErrSet) == 2); + expect(@memberCount(MyErrSet) == 2); const a: MyErrSet!i32 = 5678; const b: MyErrSet!i32 = MyErrSet.OutOfMemory; - if (a) |value| assertOrPanic(value == 5678) else |err| switch (err) { + if (a) |value| expect(value == 5678) else |err| switch (err) { error.OutOfMemory => unreachable, error.FileNotFound => unreachable, } @@ -127,7 +127,7 @@ const Set2 = error{ fn testExplicitErrorSetCast(set1: Set1) void { var x = @errSetCast(Set2, set1); var y = @errSetCast(Set1, x); - assertOrPanic(y == error.A); + expect(y == error.A); } test "comptime test error for empty error set" { @@ -138,12 +138,12 @@ test "comptime test error for empty error set" { const EmptyErrorSet = error{}; fn testComptimeTestErrorEmptySet(x: EmptyErrorSet!i32) void { - if (x) |v| assertOrPanic(v == 1234) else |err| @compileError("bad"); + if (x) |v| expect(v == 1234) else |err| @compileError("bad"); } test "syntax: optional operator in front of error union operator" { comptime { - assertOrPanic(?(anyerror!i32) == ?(anyerror!i32)); + expect(?(anyerror!i32) == ?(anyerror!i32)); } } @@ -173,7 +173,7 @@ fn testErrorUnionPeerTypeResolution(x: i32) void { if (y) |_| { @panic("expected error"); } else |e| { - assertOrPanic(e == error.A); + expect(e == error.A); } } @@ -282,13 +282,13 @@ test "nested error union function call in optional unwrap" { return null; } }; - assertOrPanic((try S.errorable()) == 1234); - assertError(S.errorable2(), error.Failure); - assertError(S.errorable3(), error.Other); + expect((try S.errorable()) == 1234); + expectError(error.Failure, S.errorable2()); + expectError(error.Other, S.errorable3()); comptime { - assertOrPanic((try S.errorable()) == 1234); - assertError(S.errorable2(), error.Failure); - assertError(S.errorable3(), error.Other); + expect((try S.errorable()) == 1234); + expectError(error.Failure, S.errorable2()); + expectError(error.Other, S.errorable3()); } } @@ -303,7 +303,7 @@ test "widen cast integer payload of error union function call" { return 1234; } }; - assertOrPanic((try S.errorable()) == 1234); + expect((try S.errorable()) == 1234); } test "return function call to error set from error union function" { @@ -316,17 +316,17 @@ test "return function call to error set from error union function" { return error.Failure; } }; - assertError(S.errorable(), error.Failure); - comptime assertError(S.errorable(), error.Failure); + expectError(error.Failure, S.errorable()); + comptime expectError(error.Failure, S.errorable()); } test "optional error set is the same size as error set" { - comptime assertOrPanic(@sizeOf(?anyerror) == @sizeOf(anyerror)); + comptime expect(@sizeOf(?anyerror) == @sizeOf(anyerror)); const S = struct { fn returnsOptErrSet() ?anyerror { return null; } }; - assertOrPanic(S.returnsOptErrSet() == null); - comptime assertOrPanic(S.returnsOptErrSet() == null); + expect(S.returnsOptErrSet() == null); + comptime expect(S.returnsOptErrSet() == null); } diff --git a/test/stage1/behavior/eval.zig b/test/stage1/behavior/eval.zig index 0d1ecfab5b..5976761f77 100644 --- a/test/stage1/behavior/eval.zig +++ b/test/stage1/behavior/eval.zig @@ -1,9 +1,9 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const builtin = @import("builtin"); test "compile time recursion" { - assertOrPanic(some_data.len == 21); + expect(some_data.len == 21); } var some_data: [@intCast(usize, fibonacci(7))]u8 = undefined; fn fibonacci(x: i32) i32 { @@ -16,7 +16,7 @@ fn unwrapAndAddOne(blah: ?i32) i32 { } const should_be_1235 = unwrapAndAddOne(1234); test "static add one" { - assertOrPanic(should_be_1235 == 1235); + expect(should_be_1235 == 1235); } test "inlined loop" { @@ -24,7 +24,7 @@ test "inlined loop" { comptime var sum = 0; inline while (i <= 5) : (i += 1) sum += i; - assertOrPanic(sum == 15); + expect(sum == 15); } fn gimme1or2(comptime a: bool) i32 { @@ -34,12 +34,12 @@ fn gimme1or2(comptime a: bool) i32 { return z; } test "inline variable gets result of const if" { - assertOrPanic(gimme1or2(true) == 1); - assertOrPanic(gimme1or2(false) == 2); + expect(gimme1or2(true) == 1); + expect(gimme1or2(false) == 2); } test "static function evaluation" { - assertOrPanic(statically_added_number == 3); + expect(statically_added_number == 3); } const statically_added_number = staticAdd(1, 2); fn staticAdd(a: i32, b: i32) i32 { @@ -47,8 +47,8 @@ fn staticAdd(a: i32, b: i32) i32 { } test "const expr eval on single expr blocks" { - assertOrPanic(constExprEvalOnSingleExprBlocksFn(1, true) == 3); - comptime assertOrPanic(constExprEvalOnSingleExprBlocksFn(1, true) == 3); + expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3); + comptime expect(constExprEvalOnSingleExprBlocksFn(1, true) == 3); } fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 { @@ -64,10 +64,10 @@ fn constExprEvalOnSingleExprBlocksFn(x: i32, b: bool) i32 { } test "statically initialized list" { - assertOrPanic(static_point_list[0].x == 1); - assertOrPanic(static_point_list[0].y == 2); - assertOrPanic(static_point_list[1].x == 3); - assertOrPanic(static_point_list[1].y == 4); + expect(static_point_list[0].x == 1); + expect(static_point_list[0].y == 2); + expect(static_point_list[1].x == 3); + expect(static_point_list[1].y == 4); } const Point = struct { x: i32, @@ -85,8 +85,8 @@ fn makePoint(x: i32, y: i32) Point { } test "static eval list init" { - assertOrPanic(static_vec3.data[2] == 1.0); - assertOrPanic(vec3(0.0, 0.0, 3.0).data[2] == 3.0); + expect(static_vec3.data[2] == 1.0); + expect(vec3(0.0, 0.0, 3.0).data[2] == 3.0); } const static_vec3 = vec3(0.0, 0.0, 1.0); pub const Vec3 = struct { @@ -102,12 +102,12 @@ pub fn vec3(x: f32, y: f32, z: f32) Vec3 { test "constant expressions" { var array: [array_size]u8 = undefined; - assertOrPanic(@sizeOf(@typeOf(array)) == 20); + expect(@sizeOf(@typeOf(array)) == 20); } const array_size: u8 = 20; test "constant struct with negation" { - assertOrPanic(vertices[0].x == -0.6); + expect(vertices[0].x == -0.6); } const Vertex = struct { x: f32, @@ -142,7 +142,7 @@ const vertices = []Vertex{ test "statically initialized struct" { st_init_str_foo.x += 1; - assertOrPanic(st_init_str_foo.x == 14); + expect(st_init_str_foo.x == 14); } const StInitStrFoo = struct { x: i32, @@ -155,7 +155,7 @@ var st_init_str_foo = StInitStrFoo{ test "statically initalized array literal" { const y: [4]u8 = st_init_arr_lit_x; - assertOrPanic(y[3] == 4); + expect(y[3] == 4); } const st_init_arr_lit_x = []u8{ 1, @@ -167,15 +167,15 @@ const st_init_arr_lit_x = []u8{ test "const slice" { comptime { const a = "1234567890"; - assertOrPanic(a.len == 10); + expect(a.len == 10); const b = a[1..2]; - assertOrPanic(b.len == 1); - assertOrPanic(b[0] == '2'); + expect(b.len == 1); + expect(b[0] == '2'); } } test "try to trick eval with runtime if" { - assertOrPanic(testTryToTrickEvalWithRuntimeIf(true) == 10); + expect(testTryToTrickEvalWithRuntimeIf(true) == 10); } fn testTryToTrickEvalWithRuntimeIf(b: bool) usize { @@ -201,16 +201,16 @@ fn letsTryToCompareBools(a: bool, b: bool) bool { return max(bool, a, b); } test "inlined block and runtime block phi" { - assertOrPanic(letsTryToCompareBools(true, true)); - assertOrPanic(letsTryToCompareBools(true, false)); - assertOrPanic(letsTryToCompareBools(false, true)); - assertOrPanic(!letsTryToCompareBools(false, false)); + expect(letsTryToCompareBools(true, true)); + expect(letsTryToCompareBools(true, false)); + expect(letsTryToCompareBools(false, true)); + expect(!letsTryToCompareBools(false, false)); comptime { - assertOrPanic(letsTryToCompareBools(true, true)); - assertOrPanic(letsTryToCompareBools(true, false)); - assertOrPanic(letsTryToCompareBools(false, true)); - assertOrPanic(!letsTryToCompareBools(false, false)); + expect(letsTryToCompareBools(true, true)); + expect(letsTryToCompareBools(true, false)); + expect(letsTryToCompareBools(false, true)); + expect(!letsTryToCompareBools(false, false)); } } @@ -255,14 +255,14 @@ fn performFn(comptime prefix_char: u8, start_value: i32) i32 { } test "comptime iterate over fn ptr list" { - assertOrPanic(performFn('t', 1) == 6); - assertOrPanic(performFn('o', 0) == 1); - assertOrPanic(performFn('w', 99) == 99); + expect(performFn('t', 1) == 6); + expect(performFn('o', 0) == 1); + expect(performFn('w', 99) == 99); } test "eval @setRuntimeSafety at compile-time" { const result = comptime fnWithSetRuntimeSafety(); - assertOrPanic(result == 1234); + expect(result == 1234); } fn fnWithSetRuntimeSafety() i32 { @@ -272,7 +272,7 @@ fn fnWithSetRuntimeSafety() i32 { test "eval @setFloatMode at compile-time" { const result = comptime fnWithFloatMode(); - assertOrPanic(result == 1234.0); + expect(result == 1234.0); } fn fnWithFloatMode() f32 { @@ -293,15 +293,15 @@ var simple_struct = SimpleStruct{ .field = 1234 }; const bound_fn = simple_struct.method; test "call method on bound fn referring to var instance" { - assertOrPanic(bound_fn() == 1237); + expect(bound_fn() == 1237); } test "ptr to local array argument at comptime" { comptime { var bytes: [10]u8 = undefined; modifySomeBytes(bytes[0..]); - assertOrPanic(bytes[0] == 'a'); - assertOrPanic(bytes[9] == 'b'); + expect(bytes[0] == 'a'); + expect(bytes[9] == 'b'); } } @@ -329,9 +329,9 @@ fn testCompTimeUIntComparisons(x: u32) void { } test "const ptr to variable data changes at runtime" { - assertOrPanic(foo_ref.name[0] == 'a'); + expect(foo_ref.name[0] == 'a'); foo_ref.name = "b"; - assertOrPanic(foo_ref.name[0] == 'b'); + expect(foo_ref.name[0] == 'b'); } const Foo = struct { @@ -342,8 +342,8 @@ var foo_contents = Foo{ .name = "a" }; const foo_ref = &foo_contents; test "create global array with for loop" { - assertOrPanic(global_array[5] == 5 * 5); - assertOrPanic(global_array[9] == 9 * 9); + expect(global_array[5] == 5 * 5); + expect(global_array[9] == 9 * 9); } const global_array = x: { @@ -358,7 +358,7 @@ test "compile-time downcast when the bits fit" { comptime { const spartan_count: u16 = 255; const byte = @intCast(u8, spartan_count); - assertOrPanic(byte == 255); + expect(byte == 255); } } @@ -366,44 +366,44 @@ const hi1 = "hi"; const hi2 = hi1; test "const global shares pointer with other same one" { assertEqualPtrs(&hi1[0], &hi2[0]); - comptime assertOrPanic(&hi1[0] == &hi2[0]); + comptime expect(&hi1[0] == &hi2[0]); } fn assertEqualPtrs(ptr1: *const u8, ptr2: *const u8) void { - assertOrPanic(ptr1 == ptr2); + expect(ptr1 == ptr2); } test "@setEvalBranchQuota" { comptime { - // 1001 for the loop and then 1 more for the assertOrPanic fn call + // 1001 for the loop and then 1 more for the expect fn call @setEvalBranchQuota(1002); var i = 0; var sum = 0; while (i < 1001) : (i += 1) { sum += i; } - assertOrPanic(sum == 500500); + expect(sum == 500500); } } // TODO test "float literal at compile time not lossy" { -// TODO assertOrPanic(16777216.0 + 1.0 == 16777217.0); -// TODO assertOrPanic(9007199254740992.0 + 1.0 == 9007199254740993.0); +// TODO expect(16777216.0 + 1.0 == 16777217.0); +// TODO expect(9007199254740992.0 + 1.0 == 9007199254740993.0); // TODO } test "f32 at compile time is lossy" { - assertOrPanic(f32(1 << 24) + 1 == 1 << 24); + expect(f32(1 << 24) + 1 == 1 << 24); } test "f64 at compile time is lossy" { - assertOrPanic(f64(1 << 53) + 1 == 1 << 53); + expect(f64(1 << 53) + 1 == 1 << 53); } test "f128 at compile time is lossy" { - assertOrPanic(f128(10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0); + expect(f128(10384593717069655257060992658440192.0) + 1 == 10384593717069655257060992658440192.0); } comptime { - assertOrPanic(f128(1 << 113) == 10384593717069655257060992658440192); + expect(f128(1 << 113) == 10384593717069655257060992658440192); } pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type { @@ -415,15 +415,15 @@ pub fn TypeWithCompTimeSlice(comptime field_name: []const u8) type { test "string literal used as comptime slice is memoized" { const a = "link"; const b = "link"; - comptime assertOrPanic(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node); - comptime assertOrPanic(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node); + comptime expect(TypeWithCompTimeSlice(a).Node == TypeWithCompTimeSlice(b).Node); + comptime expect(TypeWithCompTimeSlice("link").Node == TypeWithCompTimeSlice("link").Node); } test "comptime slice of undefined pointer of length 0" { const slice1 = ([*]i32)(undefined)[0..0]; - assertOrPanic(slice1.len == 0); + expect(slice1.len == 0); const slice2 = ([*]i32)(undefined)[100..100]; - assertOrPanic(slice2.len == 0); + expect(slice2.len == 0); } fn copyWithPartialInline(s: []u32, b: []u8) void { @@ -445,16 +445,16 @@ test "binary math operator in partially inlined function" { r.* = @intCast(u8, i + 1); copyWithPartialInline(s[0..], b[0..]); - assertOrPanic(s[0] == 0x1020304); - assertOrPanic(s[1] == 0x5060708); - assertOrPanic(s[2] == 0x90a0b0c); - assertOrPanic(s[3] == 0xd0e0f10); + expect(s[0] == 0x1020304); + expect(s[1] == 0x5060708); + expect(s[2] == 0x90a0b0c); + expect(s[3] == 0xd0e0f10); } test "comptime function with the same args is memoized" { comptime { - assertOrPanic(MakeType(i32) == MakeType(i32)); - assertOrPanic(MakeType(i32) != MakeType(f64)); + expect(MakeType(i32) == MakeType(i32)); + expect(MakeType(i32) != MakeType(f64)); } } @@ -470,7 +470,7 @@ test "comptime function with mutable pointer is not memoized" { const ptr = &x; increment(ptr); increment(ptr); - assertOrPanic(x == 3); + expect(x == 3); } } @@ -496,14 +496,14 @@ fn doesAlotT(comptime T: type, value: usize) T { } test "@setEvalBranchQuota at same scope as generic function call" { - assertOrPanic(doesAlotT(u32, 2) == 2); + expect(doesAlotT(u32, 2) == 2); } test "comptime slice of slice preserves comptime var" { comptime { var buff: [10]u8 = undefined; buff[0..][0..][0] = 1; - assertOrPanic(buff[0..][0..][0] == 1); + expect(buff[0..][0..][0] == 1); } } @@ -512,7 +512,7 @@ test "comptime slice of pointer preserves comptime var" { var buff: [10]u8 = undefined; var a = buff[0..].ptr; a[0..1][0] = 1; - assertOrPanic(buff[0..][0..][0] == 1); + expect(buff[0..][0..][0] == 1); } } @@ -526,9 +526,9 @@ const SingleFieldStruct = struct { test "const ptr to comptime mutable data is not memoized" { comptime { var foo = SingleFieldStruct{ .x = 1 }; - assertOrPanic(foo.read_x() == 1); + expect(foo.read_x() == 1); foo.x = 2; - assertOrPanic(foo.read_x() == 2); + expect(foo.read_x() == 2); } } @@ -537,7 +537,7 @@ test "array concat of slices gives slice" { var a: []const u8 = "aoeu"; var b: []const u8 = "asdf"; const c = a ++ b; - assertOrPanic(std.mem.eql(u8, c, "aoeuasdf")); + expect(std.mem.eql(u8, c, "aoeuasdf")); } } @@ -554,14 +554,14 @@ test "comptime shlWithOverflow" { break :amt amt; }; - assertOrPanic(ct_shifted == rt_shifted); + expect(ct_shifted == rt_shifted); } test "runtime 128 bit integer division" { var a: u128 = 152313999999999991610955792383; var b: u128 = 10000000000000000000; var c = a / b; - assertOrPanic(c == 15231399999); + expect(c == 15231399999); } pub const Info = struct { @@ -574,20 +574,20 @@ test "comptime modification of const struct field" { comptime { var res = diamond_info; res.version = 1; - assertOrPanic(diamond_info.version == 0); - assertOrPanic(res.version == 1); + expect(diamond_info.version == 0); + expect(res.version == 1); } } test "pointer to type" { comptime { var T: type = i32; - assertOrPanic(T == i32); + expect(T == i32); var ptr = &T; - assertOrPanic(@typeOf(ptr) == *type); + expect(@typeOf(ptr) == *type); ptr.* = f32; - assertOrPanic(T == f32); - assertOrPanic(*T == *f32); + expect(T == f32); + expect(*T == *f32); } } @@ -596,17 +596,17 @@ test "slice of type" { var types_array = []type{ i32, f64, type }; for (types_array) |T, i| { switch (i) { - 0 => assertOrPanic(T == i32), - 1 => assertOrPanic(T == f64), - 2 => assertOrPanic(T == type), + 0 => expect(T == i32), + 1 => expect(T == f64), + 2 => expect(T == type), else => unreachable, } } for (types_array[0..]) |T, i| { switch (i) { - 0 => assertOrPanic(T == i32), - 1 => assertOrPanic(T == f64), - 2 => assertOrPanic(T == type), + 0 => expect(T == i32), + 1 => expect(T == f64), + 2 => expect(T == type), else => unreachable, } } @@ -623,7 +623,7 @@ fn wrap(comptime T: type) Wrapper { test "function which returns struct with type field causes implicit comptime" { const ty = wrap(i32).T; - assertOrPanic(ty == i32); + expect(ty == i32); } test "call method with comptime pass-by-non-copying-value self parameter" { @@ -637,12 +637,12 @@ test "call method with comptime pass-by-non-copying-value self parameter" { const s = S{ .a = 2 }; var b = s.b(); - assertOrPanic(b == 2); + expect(b == 2); } test "@tagName of @typeId" { const str = @tagName(@typeId(u8)); - assertOrPanic(std.mem.eql(u8, str, "Int")); + expect(std.mem.eql(u8, str, "Int")); } test "setting backward branch quota just before a generic fn call" { @@ -663,8 +663,8 @@ fn testVarInsideInlineLoop(args: ...) void { comptime var i = 0; inline while (i < args.len) : (i += 1) { const x = args[i]; - if (i == 0) assertOrPanic(x); - if (i == 1) assertOrPanic(x == 42); + if (i == 0) expect(x); + if (i == 1) expect(x == 42); } } @@ -674,7 +674,7 @@ test "inline for with same type but different values" { var a: T = undefined; res += a.len; } - assertOrPanic(res == 5); + expect(res == 5); } test "refer to the type of a generic function" { @@ -688,13 +688,13 @@ fn doNothingWithType(comptime T: type) void {} test "zero extend from u0 to u1" { var zero_u0: u0 = 0; var zero_u1: u1 = zero_u0; - assertOrPanic(zero_u1 == 0); + expect(zero_u1 == 0); } test "bit shift a u1" { var x: u1 = 1; var y = x << 0; - assertOrPanic(y == 1); + expect(y == 1); } test "@bytesToslice on a packed struct" { @@ -704,7 +704,7 @@ test "@bytesToslice on a packed struct" { var b = [1]u8{9}; var f = @bytesToSlice(F, b); - assertOrPanic(f[0].a == 9); + expect(f[0].a == 9); } test "comptime pointer cast array and then slice" { @@ -716,8 +716,8 @@ test "comptime pointer cast array and then slice" { const ptrB: [*]const u8 = &array; const sliceB: []const u8 = ptrB[0..2]; - assertOrPanic(sliceA[1] == 2); - assertOrPanic(sliceB[1] == 2); + expect(sliceA[1] == 2); + expect(sliceB[1] == 2); } test "slice bounds in comptime concatenation" { @@ -726,47 +726,47 @@ test "slice bounds in comptime concatenation" { break :blk b[8..9]; }; const str = "" ++ bs; - assertOrPanic(str.len == 1); - assertOrPanic(std.mem.eql(u8, str, "1")); + expect(str.len == 1); + expect(std.mem.eql(u8, str, "1")); const str2 = bs ++ ""; - assertOrPanic(str2.len == 1); - assertOrPanic(std.mem.eql(u8, str2, "1")); + expect(str2.len == 1); + expect(std.mem.eql(u8, str2, "1")); } test "comptime bitwise operators" { comptime { - assertOrPanic(3 & 1 == 1); - assertOrPanic(3 & -1 == 3); - assertOrPanic(-3 & -1 == -3); - assertOrPanic(3 | -1 == -1); - assertOrPanic(-3 | -1 == -1); - assertOrPanic(3 ^ -1 == -4); - assertOrPanic(-3 ^ -1 == 2); - assertOrPanic(~i8(-1) == 0); - assertOrPanic(~i128(-1) == 0); - assertOrPanic(18446744073709551615 & 18446744073709551611 == 18446744073709551611); - assertOrPanic(-18446744073709551615 & -18446744073709551611 == -18446744073709551615); - assertOrPanic(~u128(0) == 0xffffffffffffffffffffffffffffffff); + expect(3 & 1 == 1); + expect(3 & -1 == 3); + expect(-3 & -1 == -3); + expect(3 | -1 == -1); + expect(-3 | -1 == -1); + expect(3 ^ -1 == -4); + expect(-3 ^ -1 == 2); + expect(~i8(-1) == 0); + expect(~i128(-1) == 0); + expect(18446744073709551615 & 18446744073709551611 == 18446744073709551611); + expect(-18446744073709551615 & -18446744073709551611 == -18446744073709551615); + expect(~u128(0) == 0xffffffffffffffffffffffffffffffff); } } test "*align(1) u16 is the same as *align(1:0:2) u16" { comptime { - assertOrPanic(*align(1:0:2) u16 == *align(1) u16); + expect(*align(1:0:2) u16 == *align(1) u16); // TODO add parsing support for this syntax - //assertOrPanic(*align(:0:2) u16 == *u16); + //expect(*align(:0:2) u16 == *u16); } } test "array concatenation forces comptime" { var a = oneItem(3) ++ oneItem(4); - assertOrPanic(std.mem.eql(i32, a, []i32{ 3, 4 })); + expect(std.mem.eql(i32, a, []i32{ 3, 4 })); } test "array multiplication forces comptime" { var a = oneItem(3) ** scalar(2); - assertOrPanic(std.mem.eql(i32, a, []i32{ 3, 3 })); + expect(std.mem.eql(i32, a, []i32{ 3, 3 })); } fn oneItem(x: i32) [1]i32 { diff --git a/test/stage1/behavior/field_parent_ptr.zig b/test/stage1/behavior/field_parent_ptr.zig index ed2487c020..6026a49d12 100644 --- a/test/stage1/behavior/field_parent_ptr.zig +++ b/test/stage1/behavior/field_parent_ptr.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "@fieldParentPtr non-first field" { testParentFieldPtr(&foo.c); @@ -25,17 +25,17 @@ const foo = Foo{ }; fn testParentFieldPtr(c: *const i32) void { - assertOrPanic(c == &foo.c); + expect(c == &foo.c); const base = @fieldParentPtr(Foo, "c", c); - assertOrPanic(base == &foo); - assertOrPanic(&base.c == c); + expect(base == &foo); + expect(&base.c == c); } fn testParentFieldPtrFirst(a: *const bool) void { - assertOrPanic(a == &foo.a); + expect(a == &foo.a); const base = @fieldParentPtr(Foo, "a", a); - assertOrPanic(base == &foo); - assertOrPanic(&base.a == a); + expect(base == &foo); + expect(&base.a == a); } diff --git a/test/stage1/behavior/fn.zig b/test/stage1/behavior/fn.zig index 3011bc41d0..3f78c80290 100644 --- a/test/stage1/behavior/fn.zig +++ b/test/stage1/behavior/fn.zig @@ -1,7 +1,7 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "params" { - assertOrPanic(testParamsAdd(22, 11) == 33); + expect(testParamsAdd(22, 11) == 33); } fn testParamsAdd(a: i32, b: i32) i32 { return a + b; @@ -21,32 +21,32 @@ test "void parameters" { fn voidFun(a: i32, b: void, c: i32, d: void) void { const v = b; const vv: void = if (a == 1) v else {}; - assertOrPanic(a + c == 3); + expect(a + c == 3); return vv; } test "mutable local variables" { var zero: i32 = 0; - assertOrPanic(zero == 0); + expect(zero == 0); var i = i32(0); while (i != 3) { i += 1; } - assertOrPanic(i == 3); + expect(i == 3); } test "separate block scopes" { { const no_conflict: i32 = 5; - assertOrPanic(no_conflict == 5); + expect(no_conflict == 5); } const c = x: { const no_conflict = i32(10); break :x no_conflict; }; - assertOrPanic(c == 10); + expect(c == 10); } test "call function with empty string" { @@ -59,7 +59,7 @@ fn @"weird function name"() i32 { return 1234; } test "weird function name" { - assertOrPanic(@"weird function name"() == 1234); + expect(@"weird function name"() == 1234); } test "implicit cast function unreachable return" { @@ -80,7 +80,7 @@ test "function pointers" { fn4, }; for (fns) |f, i| { - assertOrPanic(f() == @intCast(u32, i) + 5); + expect(f() == @intCast(u32, i) + 5); } } fn fn1() u32 { @@ -97,7 +97,7 @@ fn fn4() u32 { } test "inline function call" { - assertOrPanic(@inlineCall(add, 3, 9) == 12); + expect(@inlineCall(add, 3, 9) == 12); } fn add(a: i32, b: i32) i32 { @@ -110,7 +110,7 @@ test "number literal as an argument" { } fn numberLiteralArg(a: var) void { - assertOrPanic(a == 3); + expect(a == 3); } test "assign inline fn to const variable" { @@ -121,7 +121,7 @@ test "assign inline fn to const variable" { inline fn inlineFn() void {} test "pass by non-copying value" { - assertOrPanic(addPointCoords(Point{ .x = 1, .y = 2 }) == 3); + expect(addPointCoords(Point{ .x = 1, .y = 2 }) == 3); } const Point = struct { @@ -134,17 +134,17 @@ fn addPointCoords(pt: Point) i32 { } test "pass by non-copying value through var arg" { - assertOrPanic(addPointCoordsVar(Point{ .x = 1, .y = 2 }) == 3); + expect(addPointCoordsVar(Point{ .x = 1, .y = 2 }) == 3); } fn addPointCoordsVar(pt: var) i32 { - comptime assertOrPanic(@typeOf(pt) == Point); + comptime expect(@typeOf(pt) == Point); return pt.x + pt.y; } test "pass by non-copying value as method" { var pt = Point2{ .x = 1, .y = 2 }; - assertOrPanic(pt.addPointCoords() == 3); + expect(pt.addPointCoords() == 3); } const Point2 = struct { @@ -158,7 +158,7 @@ const Point2 = struct { test "pass by non-copying value as method, which is generic" { var pt = Point3{ .x = 1, .y = 2 }; - assertOrPanic(pt.addPointCoords(i32) == 3); + expect(pt.addPointCoords(i32) == 3); } const Point3 = struct { @@ -173,7 +173,7 @@ const Point3 = struct { test "pass by non-copying value as method, at comptime" { comptime { var pt = Point2{ .x = 1, .y = 2 }; - assertOrPanic(pt.addPointCoords() == 3); + expect(pt.addPointCoords() == 3); } } @@ -189,7 +189,7 @@ fn outer(y: u32) fn (u32) u32 { test "return inner function which references comptime variable of outer function" { var func = outer(10); - assertOrPanic(func(3) == 7); + expect(func(3) == 7); } test "extern struct with stdcallcc fn pointer" { @@ -203,6 +203,6 @@ test "extern struct with stdcallcc fn pointer" { var s: S = undefined; s.ptr = S.foo; - assertOrPanic(s.ptr() == 1234); + expect(s.ptr() == 1234); } diff --git a/test/stage1/behavior/fn_in_struct_in_comptime.zig b/test/stage1/behavior/fn_in_struct_in_comptime.zig index 0af076d40a..030693ac59 100644 --- a/test/stage1/behavior/fn_in_struct_in_comptime.zig +++ b/test/stage1/behavior/fn_in_struct_in_comptime.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; fn get_foo() fn (*u8) usize { comptime { @@ -13,5 +13,5 @@ fn get_foo() fn (*u8) usize { test "define a function in an anonymous struct in comptime" { const foo = get_foo(); - assertOrPanic(foo(@intToPtr(*u8, 12345)) == 12345); + expect(foo(@intToPtr(*u8, 12345)) == 12345); } diff --git a/test/stage1/behavior/for.zig b/test/stage1/behavior/for.zig index b6d1ef24c4..b10dd14fa4 100644 --- a/test/stage1/behavior/for.zig +++ b/test/stage1/behavior/for.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const mem = std.mem; test "continue in for loop" { @@ -26,7 +26,7 @@ test "for loop with pointer elem var" { var target: [source.len]u8 = undefined; mem.copy(u8, target[0..], source); mangleString(target[0..]); - assertOrPanic(mem.eql(u8, target, "bcdefgh")); + expect(mem.eql(u8, target, "bcdefgh")); } fn mangleString(s: []u8) void { for (s) |*c| { @@ -68,7 +68,7 @@ test "basic for loop" { buf_index += 1; } - assertOrPanic(mem.eql(u8, buffer[0..buf_index], expected_result)); + expect(mem.eql(u8, buffer[0..buf_index], expected_result)); } test "break from outer for loop" { @@ -85,7 +85,7 @@ fn testBreakOuter() void { break :outer; } } - assertOrPanic(count == 1); + expect(count == 1); } test "continue outer for loop" { @@ -102,5 +102,5 @@ fn testContinueOuter() void { continue :outer; } } - assertOrPanic(counter == array.len); + expect(counter == array.len); } diff --git a/test/stage1/behavior/generics.zig b/test/stage1/behavior/generics.zig index a0928634a7..637f9b88ae 100644 --- a/test/stage1/behavior/generics.zig +++ b/test/stage1/behavior/generics.zig @@ -1,9 +1,9 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "simple generic fn" { - assertOrPanic(max(i32, 3, -1) == 3); - assertOrPanic(max(f32, 0.123, 0.456) == 0.456); - assertOrPanic(add(2, 3) == 5); + expect(max(i32, 3, -1) == 3); + expect(max(f32, 0.123, 0.456) == 0.456); + expect(add(2, 3) == 5); } fn max(comptime T: type, a: T, b: T) T { @@ -16,7 +16,7 @@ fn add(comptime a: i32, b: i32) i32 { const the_max = max(u32, 1234, 5678); test "compile time generic eval" { - assertOrPanic(the_max == 5678); + expect(the_max == 5678); } fn gimmeTheBigOne(a: u32, b: u32) u32 { @@ -32,19 +32,19 @@ fn sameButWithFloats(a: f64, b: f64) f64 { } test "fn with comptime args" { - assertOrPanic(gimmeTheBigOne(1234, 5678) == 5678); - assertOrPanic(shouldCallSameInstance(34, 12) == 34); - assertOrPanic(sameButWithFloats(0.43, 0.49) == 0.49); + expect(gimmeTheBigOne(1234, 5678) == 5678); + expect(shouldCallSameInstance(34, 12) == 34); + expect(sameButWithFloats(0.43, 0.49) == 0.49); } test "var params" { - assertOrPanic(max_i32(12, 34) == 34); - assertOrPanic(max_f64(1.2, 3.4) == 3.4); + expect(max_i32(12, 34) == 34); + expect(max_f64(1.2, 3.4) == 3.4); } comptime { - assertOrPanic(max_i32(12, 34) == 34); - assertOrPanic(max_f64(1.2, 3.4) == 3.4); + expect(max_i32(12, 34) == 34); + expect(max_f64(1.2, 3.4) == 3.4); } fn max_var(a: var, b: var) @typeOf(a + b) { @@ -76,8 +76,8 @@ test "function with return type type" { var list2: List(i32) = undefined; list.length = 10; list2.length = 10; - assertOrPanic(list.prealloc_items.len == 8); - assertOrPanic(list2.prealloc_items.len == 8); + expect(list.prealloc_items.len == 8); + expect(list2.prealloc_items.len == 8); } test "generic struct" { @@ -89,9 +89,9 @@ test "generic struct" { .value = true, .next = null, }; - assertOrPanic(a1.value == 13); - assertOrPanic(a1.value == a1.getVal()); - assertOrPanic(b1.getVal()); + expect(a1.value == 13); + expect(a1.value == a1.getVal()); + expect(b1.getVal()); } fn GenNode(comptime T: type) type { return struct { @@ -104,7 +104,7 @@ fn GenNode(comptime T: type) type { } test "const decls in struct" { - assertOrPanic(GenericDataThing(3).count_plus_one == 4); + expect(GenericDataThing(3).count_plus_one == 4); } fn GenericDataThing(comptime count: isize) type { return struct { @@ -113,15 +113,15 @@ fn GenericDataThing(comptime count: isize) type { } test "use generic param in generic param" { - assertOrPanic(aGenericFn(i32, 3, 4) == 7); + expect(aGenericFn(i32, 3, 4) == 7); } fn aGenericFn(comptime T: type, comptime a: T, b: T) T { return a + b; } test "generic fn with implicit cast" { - assertOrPanic(getFirstByte(u8, []u8{13}) == 13); - assertOrPanic(getFirstByte(u16, []u16{ + expect(getFirstByte(u8, []u8{13}) == 13); + expect(getFirstByte(u16, []u16{ 0, 13, }) == 0); @@ -146,6 +146,6 @@ fn foo2(arg: var) bool { } test "array of generic fns" { - assertOrPanic(foos[0](true)); - assertOrPanic(!foos[1](true)); + expect(foos[0](true)); + expect(!foos[1](true)); } diff --git a/test/stage1/behavior/if.zig b/test/stage1/behavior/if.zig index 58d1b8fd73..a61b9dcfb4 100644 --- a/test/stage1/behavior/if.zig +++ b/test/stage1/behavior/if.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "if statements" { shouldBeEqual(1, 1); @@ -24,7 +24,7 @@ fn firstEqlThird(a: i32, b: i32, c: i32) void { } test "else if expression" { - assertOrPanic(elseIfExpressionF(1) == 1); + expect(elseIfExpressionF(1) == 1); } fn elseIfExpressionF(c: u8) u8 { if (c == 0) { diff --git a/test/stage1/behavior/import.zig b/test/stage1/behavior/import.zig index 736e4c219d..9a8c6848e2 100644 --- a/test/stage1/behavior/import.zig +++ b/test/stage1/behavior/import.zig @@ -1,10 +1,10 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const a_namespace = @import("import/a_namespace.zig"); test "call fn via namespace lookup" { - assertOrPanic(a_namespace.foo() == 1234); + expect(a_namespace.foo() == 1234); } test "importing the same thing gives the same import" { - assertOrPanic(@import("std") == @import("std")); + expect(@import("std") == @import("std")); } diff --git a/test/stage1/behavior/incomplete_struct_param_tld.zig b/test/stage1/behavior/incomplete_struct_param_tld.zig index d062311b2e..77a3dfd221 100644 --- a/test/stage1/behavior/incomplete_struct_param_tld.zig +++ b/test/stage1/behavior/incomplete_struct_param_tld.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const A = struct { b: B, @@ -26,5 +26,5 @@ test "incomplete struct param top level declaration" { .c = C{ .x = 13 }, }, }; - assertOrPanic(foo(a) == 13); + expect(foo(a) == 13); } diff --git a/test/stage1/behavior/inttoptr.zig b/test/stage1/behavior/inttoptr.zig index bf657fc86a..b1780f93d6 100644 --- a/test/stage1/behavior/inttoptr.zig +++ b/test/stage1/behavior/inttoptr.zig @@ -1,6 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "casting random address to function pointer" { randomAddressToFunction(); diff --git a/test/stage1/behavior/ir_block_deps.zig b/test/stage1/behavior/ir_block_deps.zig index bc61e11df7..821079df79 100644 --- a/test/stage1/behavior/ir_block_deps.zig +++ b/test/stage1/behavior/ir_block_deps.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; fn foo(id: u64) !i32 { return switch (id) { @@ -16,6 +16,6 @@ fn getErrInt() anyerror!i32 { } test "ir block deps" { - assertOrPanic((foo(1) catch unreachable) == 0); - assertOrPanic((foo(2) catch unreachable) == 0); + expect((foo(1) catch unreachable) == 0); + expect((foo(2) catch unreachable) == 0); } diff --git a/test/stage1/behavior/math.zig b/test/stage1/behavior/math.zig index 9d6a5a4997..0b88cc4497 100644 --- a/test/stage1/behavior/math.zig +++ b/test/stage1/behavior/math.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const maxInt = std.math.maxInt; const minInt = std.math.minInt; @@ -8,57 +8,57 @@ test "division" { comptime testDivision(); } fn testDivision() void { - assertOrPanic(div(u32, 13, 3) == 4); - assertOrPanic(div(f16, 1.0, 2.0) == 0.5); - assertOrPanic(div(f32, 1.0, 2.0) == 0.5); - - assertOrPanic(divExact(u32, 55, 11) == 5); - assertOrPanic(divExact(i32, -55, 11) == -5); - assertOrPanic(divExact(f16, 55.0, 11.0) == 5.0); - assertOrPanic(divExact(f16, -55.0, 11.0) == -5.0); - assertOrPanic(divExact(f32, 55.0, 11.0) == 5.0); - assertOrPanic(divExact(f32, -55.0, 11.0) == -5.0); - - assertOrPanic(divFloor(i32, 5, 3) == 1); - assertOrPanic(divFloor(i32, -5, 3) == -2); - assertOrPanic(divFloor(f16, 5.0, 3.0) == 1.0); - assertOrPanic(divFloor(f16, -5.0, 3.0) == -2.0); - assertOrPanic(divFloor(f32, 5.0, 3.0) == 1.0); - assertOrPanic(divFloor(f32, -5.0, 3.0) == -2.0); - assertOrPanic(divFloor(i32, -0x80000000, -2) == 0x40000000); - assertOrPanic(divFloor(i32, 0, -0x80000000) == 0); - assertOrPanic(divFloor(i32, -0x40000001, 0x40000000) == -2); - assertOrPanic(divFloor(i32, -0x80000000, 1) == -0x80000000); - - assertOrPanic(divTrunc(i32, 5, 3) == 1); - assertOrPanic(divTrunc(i32, -5, 3) == -1); - assertOrPanic(divTrunc(f16, 5.0, 3.0) == 1.0); - assertOrPanic(divTrunc(f16, -5.0, 3.0) == -1.0); - assertOrPanic(divTrunc(f32, 5.0, 3.0) == 1.0); - assertOrPanic(divTrunc(f32, -5.0, 3.0) == -1.0); - assertOrPanic(divTrunc(f64, 5.0, 3.0) == 1.0); - assertOrPanic(divTrunc(f64, -5.0, 3.0) == -1.0); + expect(div(u32, 13, 3) == 4); + expect(div(f16, 1.0, 2.0) == 0.5); + expect(div(f32, 1.0, 2.0) == 0.5); + + expect(divExact(u32, 55, 11) == 5); + expect(divExact(i32, -55, 11) == -5); + expect(divExact(f16, 55.0, 11.0) == 5.0); + expect(divExact(f16, -55.0, 11.0) == -5.0); + expect(divExact(f32, 55.0, 11.0) == 5.0); + expect(divExact(f32, -55.0, 11.0) == -5.0); + + expect(divFloor(i32, 5, 3) == 1); + expect(divFloor(i32, -5, 3) == -2); + expect(divFloor(f16, 5.0, 3.0) == 1.0); + expect(divFloor(f16, -5.0, 3.0) == -2.0); + expect(divFloor(f32, 5.0, 3.0) == 1.0); + expect(divFloor(f32, -5.0, 3.0) == -2.0); + expect(divFloor(i32, -0x80000000, -2) == 0x40000000); + expect(divFloor(i32, 0, -0x80000000) == 0); + expect(divFloor(i32, -0x40000001, 0x40000000) == -2); + expect(divFloor(i32, -0x80000000, 1) == -0x80000000); + + expect(divTrunc(i32, 5, 3) == 1); + expect(divTrunc(i32, -5, 3) == -1); + expect(divTrunc(f16, 5.0, 3.0) == 1.0); + expect(divTrunc(f16, -5.0, 3.0) == -1.0); + expect(divTrunc(f32, 5.0, 3.0) == 1.0); + expect(divTrunc(f32, -5.0, 3.0) == -1.0); + expect(divTrunc(f64, 5.0, 3.0) == 1.0); + expect(divTrunc(f64, -5.0, 3.0) == -1.0); comptime { - assertOrPanic( + expect( 1194735857077236777412821811143690633098347576 % 508740759824825164163191790951174292733114988 == 177254337427586449086438229241342047632117600, ); - assertOrPanic( + expect( @rem(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -177254337427586449086438229241342047632117600, ); - assertOrPanic( + expect( 1194735857077236777412821811143690633098347576 / 508740759824825164163191790951174292733114988 == 2, ); - assertOrPanic( + expect( @divTrunc(-1194735857077236777412821811143690633098347576, 508740759824825164163191790951174292733114988) == -2, ); - assertOrPanic( + expect( @divTrunc(1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == -2, ); - assertOrPanic( + expect( @divTrunc(-1194735857077236777412821811143690633098347576, -508740759824825164163191790951174292733114988) == 2, ); - assertOrPanic( + expect( 4126227191251978491697987544882340798050766755606969681711 % 10 == 1, ); } @@ -78,9 +78,9 @@ fn divTrunc(comptime T: type, a: T, b: T) T { test "@addWithOverflow" { var result: u8 = undefined; - assertOrPanic(@addWithOverflow(u8, 250, 100, &result)); - assertOrPanic(!@addWithOverflow(u8, 100, 150, &result)); - assertOrPanic(result == 250); + expect(@addWithOverflow(u8, 250, 100, &result)); + expect(!@addWithOverflow(u8, 100, 150, &result)); + expect(result == 250); } // TODO test mulWithOverflow @@ -88,9 +88,9 @@ test "@addWithOverflow" { test "@shlWithOverflow" { var result: u16 = undefined; - assertOrPanic(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); - assertOrPanic(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); - assertOrPanic(result == 0b1011111111111100); + expect(@shlWithOverflow(u16, 0b0010111111111111, 3, &result)); + expect(!@shlWithOverflow(u16, 0b0010111111111111, 2, &result)); + expect(result == 0b1011111111111100); } test "@clz" { @@ -99,11 +99,11 @@ test "@clz" { } fn testClz() void { - assertOrPanic(clz(u8(0b00001010)) == 4); - assertOrPanic(clz(u8(0b10001010)) == 0); - assertOrPanic(clz(u8(0b00000000)) == 8); - assertOrPanic(clz(u128(0xffffffffffffffff)) == 64); - assertOrPanic(clz(u128(0x10000000000000000)) == 63); + expect(clz(u8(0b00001010)) == 4); + expect(clz(u8(0b10001010)) == 0); + expect(clz(u8(0b00000000)) == 8); + expect(clz(u128(0xffffffffffffffff)) == 64); + expect(clz(u128(0x10000000000000000)) == 63); } fn clz(x: var) usize { @@ -116,9 +116,9 @@ test "@ctz" { } fn testCtz() void { - assertOrPanic(ctz(u8(0b10100000)) == 5); - assertOrPanic(ctz(u8(0b10001010)) == 1); - assertOrPanic(ctz(u8(0b00000000)) == 8); + expect(ctz(u8(0b10100000)) == 5); + expect(ctz(u8(0b10001010)) == 1); + expect(ctz(u8(0b00000000)) == 8); } fn ctz(x: var) usize { @@ -128,27 +128,27 @@ fn ctz(x: var) usize { test "assignment operators" { var i: u32 = 0; i += 5; - assertOrPanic(i == 5); + expect(i == 5); i -= 2; - assertOrPanic(i == 3); + expect(i == 3); i *= 20; - assertOrPanic(i == 60); + expect(i == 60); i /= 3; - assertOrPanic(i == 20); + expect(i == 20); i %= 11; - assertOrPanic(i == 9); + expect(i == 9); i <<= 1; - assertOrPanic(i == 18); + expect(i == 18); i >>= 2; - assertOrPanic(i == 4); + expect(i == 4); i = 6; i &= 5; - assertOrPanic(i == 4); + expect(i == 4); i ^= 6; - assertOrPanic(i == 2); + expect(i == 2); i = 6; i |= 3; - assertOrPanic(i == 7); + expect(i == 7); } test "three expr in a row" { @@ -170,14 +170,14 @@ fn testThreeExprInARow(f: bool, t: bool) void { assertFalse(i32(7) != --(i32(7))); } fn assertFalse(b: bool) void { - assertOrPanic(!b); + expect(!b); } test "const number literal" { const one = 1; const eleven = ten + one; - assertOrPanic(eleven == 11); + expect(eleven == 11); } const ten = 10; @@ -187,9 +187,9 @@ test "unsigned wrapping" { } fn testUnsignedWrappingEval(x: u32) void { const zero = x +% 1; - assertOrPanic(zero == 0); + expect(zero == 0); const orig = zero -% 1; - assertOrPanic(orig == maxInt(u32)); + expect(orig == maxInt(u32)); } test "signed wrapping" { @@ -198,9 +198,9 @@ test "signed wrapping" { } fn testSignedWrappingEval(x: i32) void { const min_val = x +% 1; - assertOrPanic(min_val == minInt(i32)); + expect(min_val == minInt(i32)); const max_val = min_val -% 1; - assertOrPanic(max_val == maxInt(i32)); + expect(max_val == maxInt(i32)); } test "negation wrapping" { @@ -208,9 +208,9 @@ test "negation wrapping" { comptime testNegationWrappingEval(minInt(i16)); } fn testNegationWrappingEval(x: i16) void { - assertOrPanic(x == -32768); + expect(x == -32768); const neg = -%x; - assertOrPanic(neg == -32768); + expect(neg == -32768); } test "unsigned 64-bit division" { @@ -219,8 +219,8 @@ test "unsigned 64-bit division" { } fn test_u64_div() void { const result = divWithResult(1152921504606846976, 34359738365); - assertOrPanic(result.quotient == 33554432); - assertOrPanic(result.remainder == 100663296); + expect(result.quotient == 33554432); + expect(result.remainder == 100663296); } fn divWithResult(a: u64, b: u64) DivResult { return DivResult{ @@ -234,36 +234,36 @@ const DivResult = struct { }; test "binary not" { - assertOrPanic(comptime x: { + expect(comptime x: { break :x ~u16(0b1010101010101010) == 0b0101010101010101; }); - assertOrPanic(comptime x: { + expect(comptime x: { break :x ~u64(2147483647) == 18446744071562067968; }); testBinaryNot(0b1010101010101010); } fn testBinaryNot(x: u16) void { - assertOrPanic(~x == 0b0101010101010101); + expect(~x == 0b0101010101010101); } test "small int addition" { var x: @IntType(false, 2) = 0; - assertOrPanic(x == 0); + expect(x == 0); x += 1; - assertOrPanic(x == 1); + expect(x == 1); x += 1; - assertOrPanic(x == 2); + expect(x == 2); x += 1; - assertOrPanic(x == 3); + expect(x == 3); var result: @typeOf(x) = 3; - assertOrPanic(@addWithOverflow(@typeOf(x), x, 1, &result)); + expect(@addWithOverflow(@typeOf(x), x, 1, &result)); - assertOrPanic(result == 0); + expect(result == 0); } test "float equality" { @@ -276,20 +276,20 @@ test "float equality" { fn testFloatEqualityImpl(x: f64, y: f64) void { const y2 = x + 1.0; - assertOrPanic(y == y2); + expect(y == y2); } test "allow signed integer division/remainder when values are comptime known and positive or exact" { - assertOrPanic(5 / 3 == 1); - assertOrPanic(-5 / -3 == 1); - assertOrPanic(-6 / 3 == -2); + expect(5 / 3 == 1); + expect(-5 / -3 == 1); + expect(-6 / 3 == -2); - assertOrPanic(5 % 3 == 2); - assertOrPanic(-6 % 3 == 0); + expect(5 % 3 == 2); + expect(-6 % 3 == 0); } test "hex float literal parsing" { - comptime assertOrPanic(0x1.0 == 1.0); + comptime expect(0x1.0 == 1.0); } test "quad hex float literal parsing in range" { @@ -304,7 +304,7 @@ test "quad hex float literal parsing accurate" { // implied 1 is dropped, with an exponent of 0 (0x3fff) after biasing. const expected: u128 = 0x3fff1111222233334444555566667777; - assertOrPanic(@bitCast(u128, a) == expected); + expect(@bitCast(u128, a) == expected); } test "hex float literal within range" { @@ -319,7 +319,7 @@ test "truncating shift left" { } fn testShlTrunc(x: u16) void { const shifted = x << 1; - assertOrPanic(shifted == 65534); + expect(shifted == 65534); } test "truncating shift right" { @@ -328,7 +328,7 @@ test "truncating shift right" { } fn testShrTrunc(x: u16) void { const shifted = x >> 1; - assertOrPanic(shifted == 32767); + expect(shifted == 32767); } test "exact shift left" { @@ -337,7 +337,7 @@ test "exact shift left" { } fn testShlExact(x: u8) void { const shifted = @shlExact(x, 2); - assertOrPanic(shifted == 0b11010100); + expect(shifted == 0b11010100); } test "exact shift right" { @@ -346,22 +346,22 @@ test "exact shift right" { } fn testShrExact(x: u8) void { const shifted = @shrExact(x, 2); - assertOrPanic(shifted == 0b00101101); + expect(shifted == 0b00101101); } test "comptime_int addition" { comptime { - assertOrPanic(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950); - assertOrPanic(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380); + expect(35361831660712422535336160538497375248 + 101752735581729509668353361206450473702 == 137114567242441932203689521744947848950); + expect(594491908217841670578297176641415611445982232488944558774612 + 390603545391089362063884922208143568023166603618446395589768 == 985095453608931032642182098849559179469148836107390954364380); } } test "comptime_int multiplication" { comptime { - assertOrPanic( + expect( 45960427431263824329884196484953148229 * 128339149605334697009938835852565949723 == 5898522172026096622534201617172456926982464453350084962781392314016180490567, ); - assertOrPanic( + expect( 594491908217841670578297176641415611445982232488944558774612 * 390603545391089362063884922208143568023166603618446395589768 == 232210647056203049913662402532976186578842425262306016094292237500303028346593132411865381225871291702600263463125370016, ); } @@ -369,7 +369,7 @@ test "comptime_int multiplication" { test "comptime_int shifting" { comptime { - assertOrPanic((u128(1) << 127) == 0x80000000000000000000000000000000); + expect((u128(1) << 127) == 0x80000000000000000000000000000000); } } @@ -377,16 +377,16 @@ test "comptime_int multi-limb shift and mask" { comptime { var a = 0xefffffffa0000001eeeeeeefaaaaaaab; - assertOrPanic(u32(a & 0xffffffff) == 0xaaaaaaab); + expect(u32(a & 0xffffffff) == 0xaaaaaaab); a >>= 32; - assertOrPanic(u32(a & 0xffffffff) == 0xeeeeeeef); + expect(u32(a & 0xffffffff) == 0xeeeeeeef); a >>= 32; - assertOrPanic(u32(a & 0xffffffff) == 0xa0000001); + expect(u32(a & 0xffffffff) == 0xa0000001); a >>= 32; - assertOrPanic(u32(a & 0xffffffff) == 0xefffffff); + expect(u32(a & 0xffffffff) == 0xefffffff); a >>= 32; - assertOrPanic(a == 0); + expect(a == 0); } } @@ -394,7 +394,7 @@ test "comptime_int multi-limb partial shift right" { comptime { var a = 0x1ffffffffeeeeeeee; a >>= 16; - assertOrPanic(a == 0x1ffffffffeeee); + expect(a == 0x1ffffffffeeee); } } @@ -404,23 +404,23 @@ test "xor" { } fn test_xor() void { - assertOrPanic(0xFF ^ 0x00 == 0xFF); - assertOrPanic(0xF0 ^ 0x0F == 0xFF); - assertOrPanic(0xFF ^ 0xF0 == 0x0F); - assertOrPanic(0xFF ^ 0x0F == 0xF0); - assertOrPanic(0xFF ^ 0xFF == 0x00); + expect(0xFF ^ 0x00 == 0xFF); + expect(0xF0 ^ 0x0F == 0xFF); + expect(0xFF ^ 0xF0 == 0x0F); + expect(0xFF ^ 0x0F == 0xF0); + expect(0xFF ^ 0xFF == 0x00); } test "comptime_int xor" { comptime { - assertOrPanic(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assertOrPanic(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assertOrPanic(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF); - assertOrPanic(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000); - assertOrPanic(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000); - assertOrPanic(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); - assertOrPanic(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF); - assertOrPanic(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000); + expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0x00000000000000000000000000000000 == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0x0000000000000000FFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + expect(0xFFFFFFFFFFFFFFFF0000000000000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x0000000000000000FFFFFFFFFFFFFFFF); + expect(0x0000000000000000FFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFFFFFFFFFF0000000000000000); + expect(0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000000000000000000000000000); + expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0x00000000FFFFFFFF00000000FFFFFFFF == 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF); + expect(0xFFFFFFFF00000000FFFFFFFF00000000 ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0x00000000FFFFFFFF00000000FFFFFFFF); + expect(0x00000000FFFFFFFF00000000FFFFFFFF ^ 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF == 0xFFFFFFFF00000000FFFFFFFF00000000); } } @@ -434,23 +434,23 @@ fn make_f128(x: f128) f128 { } fn test_f128() void { - assertOrPanic(@sizeOf(f128) == 16); - assertOrPanic(make_f128(1.0) == 1.0); - assertOrPanic(make_f128(1.0) != 1.1); - assertOrPanic(make_f128(1.0) > 0.9); - assertOrPanic(make_f128(1.0) >= 0.9); - assertOrPanic(make_f128(1.0) >= 1.0); + expect(@sizeOf(f128) == 16); + expect(make_f128(1.0) == 1.0); + expect(make_f128(1.0) != 1.1); + expect(make_f128(1.0) > 0.9); + expect(make_f128(1.0) >= 0.9); + expect(make_f128(1.0) >= 1.0); should_not_be_zero(1.0); } fn should_not_be_zero(x: f128) void { - assertOrPanic(x != 0.0); + expect(x != 0.0); } test "comptime float rem int" { comptime { var x = f32(1) % 2; - assertOrPanic(x == 1.0); + expect(x == 1.0); } } @@ -465,8 +465,8 @@ test "remainder division" { } fn remdiv(comptime T: type) void { - assertOrPanic(T(1) == T(1) % T(2)); - assertOrPanic(T(1) == T(7) % T(3)); + expect(T(1) == T(1) % T(2)); + expect(T(1) == T(7) % T(3)); } test "@sqrt" { @@ -480,19 +480,19 @@ test "@sqrt" { const x = 14.0; const y = x * x; const z = @sqrt(@typeOf(y), y); - comptime assertOrPanic(z == x); + comptime expect(z == x); } fn testSqrt(comptime T: type, x: T) void { - assertOrPanic(@sqrt(T, x * x) == x); + expect(@sqrt(T, x * x) == x); } test "comptime_int param and return" { const a = comptimeAdd(35361831660712422535336160538497375248, 101752735581729509668353361206450473702); - assertOrPanic(a == 137114567242441932203689521744947848950); + expect(a == 137114567242441932203689521744947848950); const b = comptimeAdd(594491908217841670578297176641415611445982232488944558774612, 390603545391089362063884922208143568023166603618446395589768); - assertOrPanic(b == 985095453608931032642182098849559179469148836107390954364380); + expect(b == 985095453608931032642182098849559179469148836107390954364380); } fn comptimeAdd(comptime a: comptime_int, comptime b: comptime_int) comptime_int { diff --git a/test/stage1/behavior/misc.zig b/test/stage1/behavior/misc.zig index 3cc8e5f31e..91cab78bc7 100644 --- a/test/stage1/behavior/misc.zig +++ b/test/stage1/behavior/misc.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const mem = std.mem; const cstr = std.cstr; const builtin = @import("builtin"); @@ -26,38 +26,38 @@ test "call disabled extern fn" { } test "@IntType builtin" { - assertOrPanic(@IntType(true, 8) == i8); - assertOrPanic(@IntType(true, 16) == i16); - assertOrPanic(@IntType(true, 32) == i32); - assertOrPanic(@IntType(true, 64) == i64); - - assertOrPanic(@IntType(false, 8) == u8); - assertOrPanic(@IntType(false, 16) == u16); - assertOrPanic(@IntType(false, 32) == u32); - assertOrPanic(@IntType(false, 64) == u64); - - assertOrPanic(i8.bit_count == 8); - assertOrPanic(i16.bit_count == 16); - assertOrPanic(i32.bit_count == 32); - assertOrPanic(i64.bit_count == 64); - - assertOrPanic(i8.is_signed); - assertOrPanic(i16.is_signed); - assertOrPanic(i32.is_signed); - assertOrPanic(i64.is_signed); - assertOrPanic(isize.is_signed); - - assertOrPanic(!u8.is_signed); - assertOrPanic(!u16.is_signed); - assertOrPanic(!u32.is_signed); - assertOrPanic(!u64.is_signed); - assertOrPanic(!usize.is_signed); + expect(@IntType(true, 8) == i8); + expect(@IntType(true, 16) == i16); + expect(@IntType(true, 32) == i32); + expect(@IntType(true, 64) == i64); + + expect(@IntType(false, 8) == u8); + expect(@IntType(false, 16) == u16); + expect(@IntType(false, 32) == u32); + expect(@IntType(false, 64) == u64); + + expect(i8.bit_count == 8); + expect(i16.bit_count == 16); + expect(i32.bit_count == 32); + expect(i64.bit_count == 64); + + expect(i8.is_signed); + expect(i16.is_signed); + expect(i32.is_signed); + expect(i64.is_signed); + expect(isize.is_signed); + + expect(!u8.is_signed); + expect(!u16.is_signed); + expect(!u32.is_signed); + expect(!u64.is_signed); + expect(!usize.is_signed); } test "floating point primitive bit counts" { - assertOrPanic(f16.bit_count == 16); - assertOrPanic(f32.bit_count == 32); - assertOrPanic(f64.bit_count == 64); + expect(f16.bit_count == 16); + expect(f32.bit_count == 32); + expect(f64.bit_count == 64); } test "short circuit" { @@ -72,7 +72,7 @@ fn testShortCircuit(f: bool, t: bool) void { var hit_4 = f; if (t or x: { - assertOrPanic(f); + expect(f); break :x f; }) { hit_1 = t; @@ -81,31 +81,31 @@ fn testShortCircuit(f: bool, t: bool) void { hit_2 = t; break :x f; }) { - assertOrPanic(f); + expect(f); } if (t and x: { hit_3 = t; break :x f; }) { - assertOrPanic(f); + expect(f); } if (f and x: { - assertOrPanic(f); + expect(f); break :x f; }) { - assertOrPanic(f); + expect(f); } else { hit_4 = t; } - assertOrPanic(hit_1); - assertOrPanic(hit_2); - assertOrPanic(hit_3); - assertOrPanic(hit_4); + expect(hit_1); + expect(hit_2); + expect(hit_3); + expect(hit_4); } test "truncate" { - assertOrPanic(testTruncate(0x10fd) == 0xfd); + expect(testTruncate(0x10fd) == 0xfd); } fn testTruncate(x: u32) u8 { return @truncate(u8, x); @@ -116,16 +116,16 @@ fn first4KeysOfHomeRow() []const u8 { } test "return string from function" { - assertOrPanic(mem.eql(u8, first4KeysOfHomeRow(), "aoeu")); + expect(mem.eql(u8, first4KeysOfHomeRow(), "aoeu")); } const g1: i32 = 1233 + 1; var g2: i32 = 0; test "global variables" { - assertOrPanic(g2 == 0); + expect(g2 == 0); g2 = g1; - assertOrPanic(g2 == 1234); + expect(g2 == 1234); } test "memcpy and memset intrinsics" { @@ -142,7 +142,7 @@ test "builtin static eval" { const x: i32 = comptime x: { break :x 1 + 2 + 3; }; - assertOrPanic(x == comptime 6); + expect(x == comptime 6); } test "slicing" { @@ -163,7 +163,7 @@ test "slicing" { test "constant equal function pointers" { const alias = emptyFn; - assertOrPanic(comptime x: { + expect(comptime x: { break :x emptyFn == alias; }); } @@ -171,25 +171,25 @@ test "constant equal function pointers" { fn emptyFn() void {} test "hex escape" { - assertOrPanic(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello")); + expect(mem.eql(u8, "\x68\x65\x6c\x6c\x6f", "hello")); } test "string concatenation" { - assertOrPanic(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); + expect(mem.eql(u8, "OK" ++ " IT " ++ "WORKED", "OK IT WORKED")); } test "array mult operator" { - assertOrPanic(mem.eql(u8, "ab" ** 5, "ababababab")); + expect(mem.eql(u8, "ab" ** 5, "ababababab")); } test "string escapes" { - assertOrPanic(mem.eql(u8, "\"", "\x22")); - assertOrPanic(mem.eql(u8, "\'", "\x27")); - assertOrPanic(mem.eql(u8, "\n", "\x0a")); - assertOrPanic(mem.eql(u8, "\r", "\x0d")); - assertOrPanic(mem.eql(u8, "\t", "\x09")); - assertOrPanic(mem.eql(u8, "\\", "\x5c")); - assertOrPanic(mem.eql(u8, "\u1234\u0069", "\xe1\x88\xb4\x69")); + expect(mem.eql(u8, "\"", "\x22")); + expect(mem.eql(u8, "\'", "\x27")); + expect(mem.eql(u8, "\n", "\x0a")); + expect(mem.eql(u8, "\r", "\x0d")); + expect(mem.eql(u8, "\t", "\x09")); + expect(mem.eql(u8, "\\", "\x5c")); + expect(mem.eql(u8, "\u1234\u0069", "\xe1\x88\xb4\x69")); } test "multiline string" { @@ -199,7 +199,7 @@ test "multiline string" { \\three ; const s2 = "one\ntwo)\nthree"; - assertOrPanic(mem.eql(u8, s1, s2)); + expect(mem.eql(u8, s1, s2)); } test "multiline C string" { @@ -209,11 +209,11 @@ test "multiline C string" { c\\three ; const s2 = c"one\ntwo)\nthree"; - assertOrPanic(cstr.cmp(s1, s2) == 0); + expect(cstr.cmp(s1, s2) == 0); } test "type equality" { - assertOrPanic(*const u8 != *u8); + expect(*const u8 != *u8); } const global_a: i32 = 1234; @@ -221,7 +221,7 @@ const global_b: *const i32 = &global_a; const global_c: *const f32 = @ptrCast(*const f32, global_b); test "compile time global reinterpret" { const d = @ptrCast(*const i32, global_c); - assertOrPanic(d.* == 1234); + expect(d.* == 1234); } test "explicit cast maybe pointers" { @@ -247,8 +247,8 @@ test "cast undefined" { fn testCastUndefined(x: []const u8) void {} test "cast small unsigned to larger signed" { - assertOrPanic(castSmallUnsignedToLargerSigned1(200) == i16(200)); - assertOrPanic(castSmallUnsignedToLargerSigned2(9999) == i64(9999)); + expect(castSmallUnsignedToLargerSigned1(200) == i16(200)); + expect(castSmallUnsignedToLargerSigned2(9999) == i64(9999)); } fn castSmallUnsignedToLargerSigned1(x: u8) i16 { return x; @@ -258,7 +258,7 @@ fn castSmallUnsignedToLargerSigned2(x: u16) i64 { } test "implicit cast after unreachable" { - assertOrPanic(outer() == 1234); + expect(outer() == 1234); } fn inner() i32 { return 1234; @@ -273,13 +273,13 @@ test "pointer dereferencing" { y.* += 1; - assertOrPanic(x == 4); - assertOrPanic(y.* == 4); + expect(x == 4); + expect(y.* == 4); } test "call result of if else expression" { - assertOrPanic(mem.eql(u8, f2(true), "a")); - assertOrPanic(mem.eql(u8, f2(false), "b")); + expect(mem.eql(u8, f2(true), "a")); + expect(mem.eql(u8, f2(false), "b")); } fn f2(x: bool) []const u8 { return (if (x) fA else fB)(); @@ -321,8 +321,8 @@ const test3_bar = Test3Foo{ .Two = 13 }; fn test3_1(f: Test3Foo) void { switch (f) { Test3Foo.Three => |pt| { - assertOrPanic(pt.x == 3); - assertOrPanic(pt.y == 4); + expect(pt.x == 3); + expect(pt.y == 4); }, else => unreachable, } @@ -330,14 +330,14 @@ fn test3_1(f: Test3Foo) void { fn test3_2(f: Test3Foo) void { switch (f) { Test3Foo.Two => |x| { - assertOrPanic(x == 13); + expect(x == 13); }, else => unreachable, } } test "character literals" { - assertOrPanic('\'' == single_quote); + expect('\'' == single_quote); } const single_quote = '\''; @@ -346,13 +346,13 @@ test "take address of parameter" { } fn testTakeAddressOfParameter(f: f32) void { const f_ptr = &f; - assertOrPanic(f_ptr.* == 12.34); + expect(f_ptr.* == 12.34); } test "pointer comparison" { const a = ([]const u8)("a"); const b = &a; - assertOrPanic(ptrEql(b, b)); + expect(ptrEql(b, b)); } fn ptrEql(a: *const []const u8, b: *const []const u8) bool { return a == b; @@ -367,31 +367,31 @@ test "C string concatenation" { { var i: u32 = 0; while (i < len_with_null) : (i += 1) { - assertOrPanic(a[i] == b[i]); + expect(a[i] == b[i]); } } - assertOrPanic(a[len] == 0); - assertOrPanic(b[len] == 0); + expect(a[len] == 0); + expect(b[len] == 0); } test "cast slice to u8 slice" { - assertOrPanic(@sizeOf(i32) == 4); + expect(@sizeOf(i32) == 4); var big_thing_array = []i32{ 1, 2, 3, 4 }; const big_thing_slice: []i32 = big_thing_array[0..]; const bytes = @sliceToBytes(big_thing_slice); - assertOrPanic(bytes.len == 4 * 4); + expect(bytes.len == 4 * 4); bytes[4] = 0; bytes[5] = 0; bytes[6] = 0; bytes[7] = 0; - assertOrPanic(big_thing_slice[1] == 0); + expect(big_thing_slice[1] == 0); const big_thing_again = @bytesToSlice(i32, bytes); - assertOrPanic(big_thing_again[2] == 3); + expect(big_thing_again[2] == 3); big_thing_again[2] = -1; - assertOrPanic(bytes[8] == maxInt(u8)); - assertOrPanic(bytes[9] == maxInt(u8)); - assertOrPanic(bytes[10] == maxInt(u8)); - assertOrPanic(bytes[11] == maxInt(u8)); + expect(bytes[8] == maxInt(u8)); + expect(bytes[9] == maxInt(u8)); + expect(bytes[10] == maxInt(u8)); + expect(bytes[11] == maxInt(u8)); } test "pointer to void return type" { @@ -408,7 +408,7 @@ fn testPointerToVoidReturnType2() *const void { test "non const ptr to aliased type" { const int = i32; - assertOrPanic(?*int == ?*i32); + expect(?*int == ?*i32); } test "array 2D const double ptr" { @@ -421,8 +421,8 @@ test "array 2D const double ptr" { fn testArray2DConstDoublePtr(ptr: *const f32) void { const ptr2 = @ptrCast([*]const f32, ptr); - assertOrPanic(ptr2[0] == 1.0); - assertOrPanic(ptr2[1] == 2.0); + expect(ptr2[0] == 1.0); + expect(ptr2[1] == 2.0); } const Tid = builtin.TypeId; @@ -444,32 +444,32 @@ const AUnion = union { test "@typeId" { comptime { - assertOrPanic(@typeId(type) == Tid.Type); - assertOrPanic(@typeId(void) == Tid.Void); - assertOrPanic(@typeId(bool) == Tid.Bool); - assertOrPanic(@typeId(noreturn) == Tid.NoReturn); - assertOrPanic(@typeId(i8) == Tid.Int); - assertOrPanic(@typeId(u8) == Tid.Int); - assertOrPanic(@typeId(i64) == Tid.Int); - assertOrPanic(@typeId(u64) == Tid.Int); - assertOrPanic(@typeId(f32) == Tid.Float); - assertOrPanic(@typeId(f64) == Tid.Float); - assertOrPanic(@typeId(*f32) == Tid.Pointer); - assertOrPanic(@typeId([2]u8) == Tid.Array); - assertOrPanic(@typeId(AStruct) == Tid.Struct); - assertOrPanic(@typeId(@typeOf(1)) == Tid.ComptimeInt); - assertOrPanic(@typeId(@typeOf(1.0)) == Tid.ComptimeFloat); - assertOrPanic(@typeId(@typeOf(undefined)) == Tid.Undefined); - assertOrPanic(@typeId(@typeOf(null)) == Tid.Null); - assertOrPanic(@typeId(?i32) == Tid.Optional); - assertOrPanic(@typeId(anyerror!i32) == Tid.ErrorUnion); - assertOrPanic(@typeId(anyerror) == Tid.ErrorSet); - assertOrPanic(@typeId(AnEnum) == Tid.Enum); - assertOrPanic(@typeId(@typeOf(AUnionEnum.One)) == Tid.Enum); - assertOrPanic(@typeId(AUnionEnum) == Tid.Union); - assertOrPanic(@typeId(AUnion) == Tid.Union); - assertOrPanic(@typeId(fn () void) == Tid.Fn); - assertOrPanic(@typeId(@typeOf(builtin)) == Tid.Namespace); + expect(@typeId(type) == Tid.Type); + expect(@typeId(void) == Tid.Void); + expect(@typeId(bool) == Tid.Bool); + expect(@typeId(noreturn) == Tid.NoReturn); + expect(@typeId(i8) == Tid.Int); + expect(@typeId(u8) == Tid.Int); + expect(@typeId(i64) == Tid.Int); + expect(@typeId(u64) == Tid.Int); + expect(@typeId(f32) == Tid.Float); + expect(@typeId(f64) == Tid.Float); + expect(@typeId(*f32) == Tid.Pointer); + expect(@typeId([2]u8) == Tid.Array); + expect(@typeId(AStruct) == Tid.Struct); + expect(@typeId(@typeOf(1)) == Tid.ComptimeInt); + expect(@typeId(@typeOf(1.0)) == Tid.ComptimeFloat); + expect(@typeId(@typeOf(undefined)) == Tid.Undefined); + expect(@typeId(@typeOf(null)) == Tid.Null); + expect(@typeId(?i32) == Tid.Optional); + expect(@typeId(anyerror!i32) == Tid.ErrorUnion); + expect(@typeId(anyerror) == Tid.ErrorSet); + expect(@typeId(AnEnum) == Tid.Enum); + expect(@typeId(@typeOf(AUnionEnum.One)) == Tid.Enum); + expect(@typeId(AUnionEnum) == Tid.Union); + expect(@typeId(AUnion) == Tid.Union); + expect(@typeId(fn () void) == Tid.Fn); + expect(@typeId(@typeOf(builtin)) == Tid.Namespace); // TODO bound fn // TODO arg tuple // TODO opaque @@ -485,13 +485,13 @@ test "@typeName" { Unused, }; comptime { - assertOrPanic(mem.eql(u8, @typeName(i64), "i64")); - assertOrPanic(mem.eql(u8, @typeName(*usize), "*usize")); + expect(mem.eql(u8, @typeName(i64), "i64")); + expect(mem.eql(u8, @typeName(*usize), "*usize")); // https://github.com/ziglang/zig/issues/675 - assertOrPanic(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)")); - assertOrPanic(mem.eql(u8, @typeName(Struct), "Struct")); - assertOrPanic(mem.eql(u8, @typeName(Union), "Union")); - assertOrPanic(mem.eql(u8, @typeName(Enum), "Enum")); + expect(mem.eql(u8, @typeName(TypeFromFn(u8)), "TypeFromFn(u8)")); + expect(mem.eql(u8, @typeName(Struct), "Struct")); + expect(mem.eql(u8, @typeName(Union), "Union")); + expect(mem.eql(u8, @typeName(Enum), "Enum")); } } @@ -501,14 +501,14 @@ fn TypeFromFn(comptime T: type) type { test "double implicit cast in same expression" { var x = i32(u16(nine())); - assertOrPanic(x == 9); + expect(x == 9); } fn nine() u8 { return 9; } test "global variable initialized to global variable array element" { - assertOrPanic(global_ptr == &gdt[0]); + expect(global_ptr == &gdt[0]); } const GDTEntry = struct { field: i32, @@ -529,9 +529,9 @@ export fn writeToVRam() void { const OpaqueA = @OpaqueType(); const OpaqueB = @OpaqueType(); test "@OpaqueType" { - assertOrPanic(*OpaqueA != *OpaqueB); - assertOrPanic(mem.eql(u8, @typeName(OpaqueA), "OpaqueA")); - assertOrPanic(mem.eql(u8, @typeName(OpaqueB), "OpaqueB")); + expect(*OpaqueA != *OpaqueB); + expect(mem.eql(u8, @typeName(OpaqueA), "OpaqueA")); + expect(mem.eql(u8, @typeName(OpaqueB), "OpaqueB")); } test "variable is allowed to be a pointer to an opaque type" { @@ -571,7 +571,7 @@ fn fnThatClosesOverLocalConst() type { test "function closes over local const" { const x = fnThatClosesOverLocalConst().g(); - assertOrPanic(x == 1); + expect(x == 1); } test "cold function" { @@ -608,21 +608,21 @@ export fn testPackedStuff(a: *const PackedStruct, b: *const PackedUnion, c: Pack test "slicing zero length array" { const s1 = ""[0..]; const s2 = ([]u32{})[0..]; - assertOrPanic(s1.len == 0); - assertOrPanic(s2.len == 0); - assertOrPanic(mem.eql(u8, s1, "")); - assertOrPanic(mem.eql(u32, s2, []u32{})); + expect(s1.len == 0); + expect(s2.len == 0); + expect(mem.eql(u8, s1, "")); + expect(mem.eql(u32, s2, []u32{})); } const addr1 = @ptrCast(*const u8, emptyFn); test "comptime cast fn to ptr" { const addr2 = @ptrCast(*const u8, emptyFn); - comptime assertOrPanic(addr1 == addr2); + comptime expect(addr1 == addr2); } test "equality compare fn ptrs" { var a = emptyFn; - assertOrPanic(a == a); + expect(a == a); } test "self reference through fn ptr field" { @@ -637,26 +637,26 @@ test "self reference through fn ptr field" { }; var a: S.A = undefined; a.f = S.foo; - assertOrPanic(a.f(a) == 12); + expect(a.f(a) == 12); } test "volatile load and store" { var number: i32 = 1234; const ptr = (*volatile i32)(&number); ptr.* += 1; - assertOrPanic(ptr.* == 1235); + expect(ptr.* == 1235); } test "slice string literal has type []const u8" { comptime { - assertOrPanic(@typeOf("aoeu"[0..]) == []const u8); + expect(@typeOf("aoeu"[0..]) == []const u8); const array = []i32{ 1, 2, 3, 4 }; - assertOrPanic(@typeOf(array[0..]) == []const i32); + expect(@typeOf(array[0..]) == []const i32); } } test "pointer child field" { - assertOrPanic((*u32).Child == u32); + expect((*u32).Child == u32); } test "struct inside function" { @@ -675,11 +675,11 @@ fn testStructInFn() void { block.kind += 1; - assertOrPanic(block.kind == 1235); + expect(block.kind == 1235); } test "fn call returning scalar optional in equality expression" { - assertOrPanic(getNull() == null); + expect(getNull() == null); } fn getNull() ?*i32 { @@ -691,5 +691,5 @@ test "thread local variable" { threadlocal var t: i32 = 1234; }; S.t += 1; - assertOrPanic(S.t == 1235); + expect(S.t == 1235); } diff --git a/test/stage1/behavior/namespace_depends_on_compile_var/index.zig b/test/stage1/behavior/namespace_depends_on_compile_var/index.zig index fe3e0cc020..32feaced10 100644 --- a/test/stage1/behavior/namespace_depends_on_compile_var/index.zig +++ b/test/stage1/behavior/namespace_depends_on_compile_var/index.zig @@ -1,11 +1,11 @@ const builtin = @import("builtin"); -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "namespace depends on compile var" { if (some_namespace.a_bool) { - assertOrPanic(some_namespace.a_bool); + expect(some_namespace.a_bool); } else { - assertOrPanic(!some_namespace.a_bool); + expect(!some_namespace.a_bool); } } const some_namespace = switch (builtin.os) { diff --git a/test/stage1/behavior/new_stack_call.zig b/test/stage1/behavior/new_stack_call.zig index b9ae2d27cd..1e01a5a8a2 100644 --- a/test/stage1/behavior/new_stack_call.zig +++ b/test/stage1/behavior/new_stack_call.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; var new_stack_bytes: [1024]u8 = undefined; @@ -10,17 +10,17 @@ test "calling a function with a new stack" { const b = @newStackCall(new_stack_bytes[512..], targetFunction, arg); _ = targetFunction(arg); - assertOrPanic(arg == 1234); - assertOrPanic(a < b); + expect(arg == 1234); + expect(a < b); } fn targetFunction(x: i32) usize { - assertOrPanic(x == 1234); + expect(x == 1234); var local_variable: i32 = 42; const ptr = &local_variable; ptr.* += 1; - assertOrPanic(local_variable == 43); + expect(local_variable == 43); return @ptrToInt(ptr); } diff --git a/test/stage1/behavior/null.zig b/test/stage1/behavior/null.zig index e2f86a05ba..8c9b86b260 100644 --- a/test/stage1/behavior/null.zig +++ b/test/stage1/behavior/null.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "optional type" { const x: ?bool = true; @@ -17,13 +17,13 @@ test "optional type" { const z = next_x orelse 1234; - assertOrPanic(z == 1234); + expect(z == 1234); const final_x: ?i32 = 13; const num = final_x orelse unreachable; - assertOrPanic(num == 13); + expect(num == 13); } test "test maybe object and get a pointer to the inner value" { @@ -33,7 +33,7 @@ test "test maybe object and get a pointer to the inner value" { b.* = false; } - assertOrPanic(maybe_bool.? == false); + expect(maybe_bool.? == false); } test "rhs maybe unwrap return" { @@ -47,9 +47,9 @@ test "maybe return" { } fn maybeReturnImpl() void { - assertOrPanic(foo(1235).?); + expect(foo(1235).?); if (foo(null) != null) unreachable; - assertOrPanic(!foo(1234).?); + expect(!foo(1234).?); } fn foo(x: ?i32) ?bool { @@ -58,7 +58,7 @@ fn foo(x: ?i32) ?bool { } test "if var maybe pointer" { - assertOrPanic(shouldBeAPlus1(Particle{ + expect(shouldBeAPlus1(Particle{ .a = 14, .b = 1, .c = 1, @@ -84,10 +84,10 @@ const Particle = struct { test "null literal outside function" { const is_null = here_is_a_null_literal.context == null; - assertOrPanic(is_null); + expect(is_null); const is_non_null = here_is_a_null_literal.context != null; - assertOrPanic(!is_non_null); + expect(!is_non_null); } const SillyStruct = struct { context: ?i32, @@ -98,8 +98,8 @@ test "test null runtime" { testTestNullRuntime(null); } fn testTestNullRuntime(x: ?i32) void { - assertOrPanic(x == null); - assertOrPanic(!(x != null)); + expect(x == null); + expect(!(x != null)); } test "optional void" { @@ -108,8 +108,8 @@ test "optional void" { } fn optionalVoidImpl() void { - assertOrPanic(bar(null) == null); - assertOrPanic(bar({}) != null); + expect(bar(null) == null); + expect(bar({}) != null); } fn bar(x: ?void) ?void { @@ -133,7 +133,7 @@ test "unwrap optional which is field of global var" { } struct_with_optional.field = 1234; if (struct_with_optional.field) |payload| { - assertOrPanic(payload == 1234); + expect(payload == 1234); } else { unreachable; } @@ -141,13 +141,13 @@ test "unwrap optional which is field of global var" { test "null with default unwrap" { const x: i32 = null orelse 1; - assertOrPanic(x == 1); + expect(x == 1); } test "optional types" { comptime { const opt_type_struct = StructWithOptionalType{ .t = u8 }; - assertOrPanic(opt_type_struct.t != null and opt_type_struct.t.? == u8); + expect(opt_type_struct.t != null and opt_type_struct.t.? == u8); } } @@ -158,5 +158,5 @@ const StructWithOptionalType = struct { test "optional pointer to 0 bit type null value at runtime" { const EmptyStruct = struct {}; var x: ?*EmptyStruct = null; - assertOrPanic(x == null); + expect(x == null); } diff --git a/test/stage1/behavior/optional.zig b/test/stage1/behavior/optional.zig index 14692cb1ea..a65bed020c 100644 --- a/test/stage1/behavior/optional.zig +++ b/test/stage1/behavior/optional.zig @@ -1,11 +1,11 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; pub const EmptyStruct = struct {}; test "optional pointer to size zero struct" { var e = EmptyStruct{}; var o: ?*EmptyStruct = &e; - assertOrPanic(o != null); + expect(o != null); } test "equality compare nullable pointers" { @@ -18,15 +18,15 @@ fn testNullPtrsEql() void { var x: ?*i32 = null; var y: ?*i32 = null; - assertOrPanic(x == y); + expect(x == y); y = &number; - assertOrPanic(x != y); - assertOrPanic(x != &number); - assertOrPanic(&number != x); + expect(x != y); + expect(x != &number); + expect(&number != x); x = &number; - assertOrPanic(x == y); - assertOrPanic(x == &number); - assertOrPanic(&number == x); + expect(x == y); + expect(x == &number); + expect(&number == x); } test "address of unwrap optional" { @@ -43,7 +43,7 @@ test "address of unwrap optional" { }; S.global = S.Foo{ .a = 1234 }; const foo = S.getFoo() catch unreachable; - assertOrPanic(foo.a == 1234); + expect(foo.a == 1234); } test "passing an optional integer as a parameter" { @@ -57,15 +57,15 @@ test "passing an optional integer as a parameter" { return x.? == 1234; } }; - assertOrPanic(S.entry()); - comptime assertOrPanic(S.entry()); + expect(S.entry()); + comptime expect(S.entry()); } test "unwrap function call with optional pointer return value" { const S = struct { fn entry() void { - assertOrPanic(foo().?.* == 1234); - assertOrPanic(bar() == null); + expect(foo().?.* == 1234); + expect(bar() == null); } const global: i32 = 1234; fn foo() ?*const i32 { diff --git a/test/stage1/behavior/pointers.zig b/test/stage1/behavior/pointers.zig index 1142d89ab5..47b19700ee 100644 --- a/test/stage1/behavior/pointers.zig +++ b/test/stage1/behavior/pointers.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "dereference pointer" { comptime testDerefPtr(); @@ -10,33 +10,33 @@ fn testDerefPtr() void { var x: i32 = 1234; var y = &x; y.* += 1; - assertOrPanic(x == 1235); + expect(x == 1235); } test "pointer arithmetic" { var ptr = c"abcd"; - assertOrPanic(ptr[0] == 'a'); + expect(ptr[0] == 'a'); ptr += 1; - assertOrPanic(ptr[0] == 'b'); + expect(ptr[0] == 'b'); ptr += 1; - assertOrPanic(ptr[0] == 'c'); + expect(ptr[0] == 'c'); ptr += 1; - assertOrPanic(ptr[0] == 'd'); + expect(ptr[0] == 'd'); ptr += 1; - assertOrPanic(ptr[0] == 0); + expect(ptr[0] == 0); ptr -= 1; - assertOrPanic(ptr[0] == 'd'); + expect(ptr[0] == 'd'); ptr -= 1; - assertOrPanic(ptr[0] == 'c'); + expect(ptr[0] == 'c'); ptr -= 1; - assertOrPanic(ptr[0] == 'b'); + expect(ptr[0] == 'b'); ptr -= 1; - assertOrPanic(ptr[0] == 'a'); + expect(ptr[0] == 'a'); } test "double pointer parsing" { - comptime assertOrPanic(PtrOf(PtrOf(i32)) == **i32); + comptime expect(PtrOf(PtrOf(i32)) == **i32); } fn PtrOf(comptime T: type) type { diff --git a/test/stage1/behavior/popcount.zig b/test/stage1/behavior/popcount.zig index f7f8bb523b..2b63284720 100644 --- a/test/stage1/behavior/popcount.zig +++ b/test/stage1/behavior/popcount.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "@popCount" { comptime testPopCount(); @@ -8,18 +8,18 @@ test "@popCount" { fn testPopCount() void { { var x: u32 = 0xaa; - assertOrPanic(@popCount(x) == 4); + expect(@popCount(x) == 4); } { var x: u32 = 0xaaaaaaaa; - assertOrPanic(@popCount(x) == 16); + expect(@popCount(x) == 16); } { var x: i16 = -1; - assertOrPanic(@popCount(x) == 16); + expect(@popCount(x) == 16); } comptime { - assertOrPanic(@popCount(0b11111111000110001100010000100001000011000011100101010001) == 24); + expect(@popCount(0b11111111000110001100010000100001000011000011100101010001) == 24); } } diff --git a/test/stage1/behavior/ptrcast.zig b/test/stage1/behavior/ptrcast.zig index 54c3dda849..3787382aea 100644 --- a/test/stage1/behavior/ptrcast.zig +++ b/test/stage1/behavior/ptrcast.zig @@ -1,6 +1,6 @@ const builtin = @import("builtin"); const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "reinterpret bytes as integer with nonzero offset" { testReinterpretBytesAsInteger(); @@ -13,7 +13,7 @@ fn testReinterpretBytesAsInteger() void { builtin.Endian.Little => 0xab785634, builtin.Endian.Big => 0x345678ab, }; - assertOrPanic(@ptrCast(*align(1) const u32, bytes[1..5].ptr).* == expected); + expect(@ptrCast(*align(1) const u32, bytes[1..5].ptr).* == expected); } test "reinterpret bytes of an array into an extern struct" { @@ -32,12 +32,12 @@ fn testReinterpretBytesAsExternStruct() void { var ptr = @ptrCast(*const S, &bytes); var val = ptr.c; - assertOrPanic(val == 5); + expect(val == 5); } test "reinterpret struct field at comptime" { const numLittle = comptime Bytes.init(0x12345678); - assertOrPanic(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes)); + expect(std.mem.eql(u8, []u8{ 0x78, 0x56, 0x34, 0x12 }, numLittle.bytes)); } const Bytes = struct { diff --git a/test/stage1/behavior/pub_enum/index.zig b/test/stage1/behavior/pub_enum/index.zig index 181113f6bf..15e4114c2d 100644 --- a/test/stage1/behavior/pub_enum/index.zig +++ b/test/stage1/behavior/pub_enum/index.zig @@ -1,13 +1,13 @@ const other = @import("other.zig"); -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "pub enum" { pubEnumTest(other.APubEnum.Two); } fn pubEnumTest(foo: other.APubEnum) void { - assertOrPanic(foo == other.APubEnum.Two); + expect(foo == other.APubEnum.Two); } test "cast with imported symbol" { - assertOrPanic(other.size_t(42) == 42); + expect(other.size_t(42) == 42); } diff --git a/test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig b/test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig index acbe6b2459..2c1cf06268 100644 --- a/test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig +++ b/test/stage1/behavior/ref_var_in_if_after_if_2nd_switch_prong.zig @@ -1,14 +1,14 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; var ok: bool = false; test "reference a variable in an if after an if in the 2nd switch prong" { foo(true, Num.Two, false, "aoeu"); - assertOrPanic(!ok); + expect(!ok); foo(false, Num.One, false, "aoeu"); - assertOrPanic(!ok); + expect(!ok); foo(true, Num.One, false, "aoeu"); - assertOrPanic(ok); + expect(ok); } const Num = enum { @@ -32,6 +32,6 @@ fn foo(c: bool, k: Num, c2: bool, b: []const u8) void { } fn a(x: []const u8) void { - assertOrPanic(mem.eql(u8, x, "aoeu")); + expect(mem.eql(u8, x, "aoeu")); ok = true; } diff --git a/test/stage1/behavior/reflection.zig b/test/stage1/behavior/reflection.zig index f4c142e0f7..55efc85b97 100644 --- a/test/stage1/behavior/reflection.zig +++ b/test/stage1/behavior/reflection.zig @@ -1,25 +1,25 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; const reflection = @This(); test "reflection: array, pointer, optional, error union type child" { comptime { - assertOrPanic(([10]u8).Child == u8); - assertOrPanic((*u8).Child == u8); - assertOrPanic((anyerror!u8).Payload == u8); - assertOrPanic((?u8).Child == u8); + expect(([10]u8).Child == u8); + expect((*u8).Child == u8); + expect((anyerror!u8).Payload == u8); + expect((?u8).Child == u8); } } test "reflection: function return type, var args, and param types" { comptime { - assertOrPanic(@typeOf(dummy).ReturnType == i32); - assertOrPanic(!@typeOf(dummy).is_var_args); - assertOrPanic(@typeOf(dummy_varargs).is_var_args); - assertOrPanic(@typeOf(dummy).arg_count == 3); - assertOrPanic(@ArgType(@typeOf(dummy), 0) == bool); - assertOrPanic(@ArgType(@typeOf(dummy), 1) == i32); - assertOrPanic(@ArgType(@typeOf(dummy), 2) == f32); + expect(@typeOf(dummy).ReturnType == i32); + expect(!@typeOf(dummy).is_var_args); + expect(@typeOf(dummy_varargs).is_var_args); + expect(@typeOf(dummy).arg_count == 3); + expect(@ArgType(@typeOf(dummy), 0) == bool); + expect(@ArgType(@typeOf(dummy), 1) == i32); + expect(@ArgType(@typeOf(dummy), 2) == f32); } } @@ -30,31 +30,31 @@ fn dummy_varargs(args: ...) void {} test "reflection: struct member types and names" { comptime { - assertOrPanic(@memberCount(Foo) == 3); + expect(@memberCount(Foo) == 3); - assertOrPanic(@memberType(Foo, 0) == i32); - assertOrPanic(@memberType(Foo, 1) == bool); - assertOrPanic(@memberType(Foo, 2) == void); + expect(@memberType(Foo, 0) == i32); + expect(@memberType(Foo, 1) == bool); + expect(@memberType(Foo, 2) == void); - assertOrPanic(mem.eql(u8, @memberName(Foo, 0), "one")); - assertOrPanic(mem.eql(u8, @memberName(Foo, 1), "two")); - assertOrPanic(mem.eql(u8, @memberName(Foo, 2), "three")); + expect(mem.eql(u8, @memberName(Foo, 0), "one")); + expect(mem.eql(u8, @memberName(Foo, 1), "two")); + expect(mem.eql(u8, @memberName(Foo, 2), "three")); } } test "reflection: enum member types and names" { comptime { - assertOrPanic(@memberCount(Bar) == 4); + expect(@memberCount(Bar) == 4); - assertOrPanic(@memberType(Bar, 0) == void); - assertOrPanic(@memberType(Bar, 1) == i32); - assertOrPanic(@memberType(Bar, 2) == bool); - assertOrPanic(@memberType(Bar, 3) == f64); + expect(@memberType(Bar, 0) == void); + expect(@memberType(Bar, 1) == i32); + expect(@memberType(Bar, 2) == bool); + expect(@memberType(Bar, 3) == f64); - assertOrPanic(mem.eql(u8, @memberName(Bar, 0), "One")); - assertOrPanic(mem.eql(u8, @memberName(Bar, 1), "Two")); - assertOrPanic(mem.eql(u8, @memberName(Bar, 2), "Three")); - assertOrPanic(mem.eql(u8, @memberName(Bar, 3), "Four")); + expect(mem.eql(u8, @memberName(Bar, 0), "One")); + expect(mem.eql(u8, @memberName(Bar, 1), "Two")); + expect(mem.eql(u8, @memberName(Bar, 2), "Three")); + expect(mem.eql(u8, @memberName(Bar, 3), "Four")); } } @@ -65,18 +65,18 @@ test "reflection: @field" { .three = void{}, }; - assertOrPanic(f.one == f.one); - assertOrPanic(@field(f, "o" ++ "ne") == f.one); - assertOrPanic(@field(f, "t" ++ "wo") == f.two); - assertOrPanic(@field(f, "th" ++ "ree") == f.three); - assertOrPanic(@field(Foo, "const" ++ "ant") == Foo.constant); - assertOrPanic(@field(Bar, "O" ++ "ne") == Bar.One); - assertOrPanic(@field(Bar, "T" ++ "wo") == Bar.Two); - assertOrPanic(@field(Bar, "Th" ++ "ree") == Bar.Three); - assertOrPanic(@field(Bar, "F" ++ "our") == Bar.Four); - assertOrPanic(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2)); + expect(f.one == f.one); + expect(@field(f, "o" ++ "ne") == f.one); + expect(@field(f, "t" ++ "wo") == f.two); + expect(@field(f, "th" ++ "ree") == f.three); + expect(@field(Foo, "const" ++ "ant") == Foo.constant); + expect(@field(Bar, "O" ++ "ne") == Bar.One); + expect(@field(Bar, "T" ++ "wo") == Bar.Two); + expect(@field(Bar, "Th" ++ "ree") == Bar.Three); + expect(@field(Bar, "F" ++ "our") == Bar.Four); + expect(@field(reflection, "dum" ++ "my")(true, 1, 2) == dummy(true, 1, 2)); @field(f, "o" ++ "ne") = 4; - assertOrPanic(f.one == 4); + expect(f.one == 4); } const Foo = struct { diff --git a/test/stage1/behavior/sizeof_and_typeof.zig b/test/stage1/behavior/sizeof_and_typeof.zig index ddaea4c242..58a6c81759 100644 --- a/test/stage1/behavior/sizeof_and_typeof.zig +++ b/test/stage1/behavior/sizeof_and_typeof.zig @@ -1,9 +1,9 @@ const builtin = @import("builtin"); -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "@sizeOf and @typeOf" { const y: @typeOf(x) = 120; - assertOrPanic(@sizeOf(@typeOf(y)) == 2); + expect(@sizeOf(@typeOf(y)) == 2); } const x: u16 = 13; const z: @typeOf(x) = 19; @@ -30,40 +30,40 @@ const P = packed struct { test "@byteOffsetOf" { // Packed structs have fixed memory layout - assertOrPanic(@byteOffsetOf(P, "a") == 0); - assertOrPanic(@byteOffsetOf(P, "b") == 1); - assertOrPanic(@byteOffsetOf(P, "c") == 5); - assertOrPanic(@byteOffsetOf(P, "d") == 6); - assertOrPanic(@byteOffsetOf(P, "e") == 6); - assertOrPanic(@byteOffsetOf(P, "f") == 7); - assertOrPanic(@byteOffsetOf(P, "g") == 9); + expect(@byteOffsetOf(P, "a") == 0); + expect(@byteOffsetOf(P, "b") == 1); + expect(@byteOffsetOf(P, "c") == 5); + expect(@byteOffsetOf(P, "d") == 6); + expect(@byteOffsetOf(P, "e") == 6); + expect(@byteOffsetOf(P, "f") == 7); + expect(@byteOffsetOf(P, "g") == 9); // Normal struct fields can be moved/padded var a: A = undefined; - assertOrPanic(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a")); - assertOrPanic(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b")); - assertOrPanic(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c")); - assertOrPanic(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d")); - assertOrPanic(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e")); - assertOrPanic(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f")); - assertOrPanic(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g")); + expect(@ptrToInt(&a.a) - @ptrToInt(&a) == @byteOffsetOf(A, "a")); + expect(@ptrToInt(&a.b) - @ptrToInt(&a) == @byteOffsetOf(A, "b")); + expect(@ptrToInt(&a.c) - @ptrToInt(&a) == @byteOffsetOf(A, "c")); + expect(@ptrToInt(&a.d) - @ptrToInt(&a) == @byteOffsetOf(A, "d")); + expect(@ptrToInt(&a.e) - @ptrToInt(&a) == @byteOffsetOf(A, "e")); + expect(@ptrToInt(&a.f) - @ptrToInt(&a) == @byteOffsetOf(A, "f")); + expect(@ptrToInt(&a.g) - @ptrToInt(&a) == @byteOffsetOf(A, "g")); } test "@bitOffsetOf" { // Packed structs have fixed memory layout - assertOrPanic(@bitOffsetOf(P, "a") == 0); - assertOrPanic(@bitOffsetOf(P, "b") == 8); - assertOrPanic(@bitOffsetOf(P, "c") == 40); - assertOrPanic(@bitOffsetOf(P, "d") == 48); - assertOrPanic(@bitOffsetOf(P, "e") == 51); - assertOrPanic(@bitOffsetOf(P, "f") == 56); - assertOrPanic(@bitOffsetOf(P, "g") == 72); + expect(@bitOffsetOf(P, "a") == 0); + expect(@bitOffsetOf(P, "b") == 8); + expect(@bitOffsetOf(P, "c") == 40); + expect(@bitOffsetOf(P, "d") == 48); + expect(@bitOffsetOf(P, "e") == 51); + expect(@bitOffsetOf(P, "f") == 56); + expect(@bitOffsetOf(P, "g") == 72); - assertOrPanic(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a")); - assertOrPanic(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b")); - assertOrPanic(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c")); - assertOrPanic(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d")); - assertOrPanic(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e")); - assertOrPanic(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f")); - assertOrPanic(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g")); + expect(@byteOffsetOf(A, "a") * 8 == @bitOffsetOf(A, "a")); + expect(@byteOffsetOf(A, "b") * 8 == @bitOffsetOf(A, "b")); + expect(@byteOffsetOf(A, "c") * 8 == @bitOffsetOf(A, "c")); + expect(@byteOffsetOf(A, "d") * 8 == @bitOffsetOf(A, "d")); + expect(@byteOffsetOf(A, "e") * 8 == @bitOffsetOf(A, "e")); + expect(@byteOffsetOf(A, "f") * 8 == @bitOffsetOf(A, "f")); + expect(@byteOffsetOf(A, "g") * 8 == @bitOffsetOf(A, "g")); } diff --git a/test/stage1/behavior/slice.zig b/test/stage1/behavior/slice.zig index cc29e43485..13fa84d0fa 100644 --- a/test/stage1/behavior/slice.zig +++ b/test/stage1/behavior/slice.zig @@ -1,20 +1,20 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; const x = @intToPtr([*]i32, 0x1000)[0..0x500]; const y = x[0x100..]; test "compile time slice of pointer to hard coded address" { - assertOrPanic(@ptrToInt(x.ptr) == 0x1000); - assertOrPanic(x.len == 0x500); + expect(@ptrToInt(x.ptr) == 0x1000); + expect(x.len == 0x500); - assertOrPanic(@ptrToInt(y.ptr) == 0x1100); - assertOrPanic(y.len == 0x400); + expect(@ptrToInt(y.ptr) == 0x1100); + expect(y.len == 0x400); } test "slice child property" { var array: [5]i32 = undefined; var slice = array[0..]; - assertOrPanic(@typeOf(slice).Child == i32); + expect(@typeOf(slice).Child == i32); } test "runtime safety lets us slice from len..len" { @@ -23,7 +23,7 @@ test "runtime safety lets us slice from len..len" { 2, 3, }; - assertOrPanic(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), "")); + expect(mem.eql(u8, sliceFromLenToLen(an_array[0..], 3, 3), "")); } fn sliceFromLenToLen(a_slice: []u8, start: usize, end: usize) []u8 { @@ -36,5 +36,5 @@ test "implicitly cast array of size 0 to slice" { } fn assertLenIsZero(msg: []const u8) void { - assertOrPanic(msg.len == 0); + expect(msg.len == 0); } diff --git a/test/stage1/behavior/struct.zig b/test/stage1/behavior/struct.zig index 92ae2baa15..a045f482a2 100644 --- a/test/stage1/behavior/struct.zig +++ b/test/stage1/behavior/struct.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const builtin = @import("builtin"); const maxInt = std.math.maxInt; @@ -12,7 +12,7 @@ const empty_global_instance = StructWithNoFields{}; test "call struct static method" { const result = StructWithNoFields.add(3, 4); - assertOrPanic(result == 7); + expect(result == 7); } test "return empty struct instance" { @@ -25,7 +25,7 @@ fn returnEmptyStructInstance() StructWithNoFields { const should_be_11 = StructWithNoFields.add(5, 6); test "invoke static method in global scope" { - assertOrPanic(should_be_11 == 11); + expect(should_be_11 == 11); } test "void struct fields" { @@ -34,8 +34,8 @@ test "void struct fields" { .b = 1, .c = void{}, }; - assertOrPanic(foo.b == 1); - assertOrPanic(@sizeOf(VoidStructFieldsFoo) == 4); + expect(foo.b == 1); + expect(@sizeOf(VoidStructFieldsFoo) == 4); } const VoidStructFieldsFoo = struct { a: void, @@ -50,7 +50,7 @@ test "structs" { foo.b = foo.a == 1; testFoo(foo); testMutation(&foo); - assertOrPanic(foo.c == 100); + expect(foo.c == 100); } const StructFoo = struct { a: i32, @@ -58,7 +58,7 @@ const StructFoo = struct { c: f32, }; fn testFoo(foo: StructFoo) void { - assertOrPanic(foo.b); + expect(foo.b); } fn testMutation(foo: *StructFoo) void { foo.c = 100; @@ -83,7 +83,7 @@ test "struct point to self" { root.next = &node; - assertOrPanic(node.next.next.next.val.x == 1); + expect(node.next.next.next.val.x == 1); } test "struct byval assign" { @@ -92,18 +92,18 @@ test "struct byval assign" { foo1.a = 1234; foo2.a = 0; - assertOrPanic(foo2.a == 0); + expect(foo2.a == 0); foo2 = foo1; - assertOrPanic(foo2.a == 1234); + expect(foo2.a == 1234); } fn structInitializer() void { const val = Val{ .x = 42 }; - assertOrPanic(val.x == 42); + expect(val.x == 42); } test "fn call of struct field" { - assertOrPanic(callStructField(Foo{ .ptr = aFunc }) == 13); + expect(callStructField(Foo{ .ptr = aFunc }) == 13); } const Foo = struct { @@ -122,7 +122,7 @@ test "store member function in variable" { const instance = MemberFnTestFoo{ .x = 1234 }; const memberFn = MemberFnTestFoo.member; const result = memberFn(instance); - assertOrPanic(result == 1234); + expect(result == 1234); } const MemberFnTestFoo = struct { x: i32, @@ -134,12 +134,12 @@ const MemberFnTestFoo = struct { test "call member function directly" { const instance = MemberFnTestFoo{ .x = 1234 }; const result = MemberFnTestFoo.member(instance); - assertOrPanic(result == 1234); + expect(result == 1234); } test "member functions" { const r = MemberFnRand{ .seed = 1234 }; - assertOrPanic(r.getSeed() == 1234); + expect(r.getSeed() == 1234); } const MemberFnRand = struct { seed: u32, @@ -150,7 +150,7 @@ const MemberFnRand = struct { test "return struct byval from function" { const bar = makeBar(1234, 5678); - assertOrPanic(bar.y == 5678); + expect(bar.y == 5678); } const Bar = struct { x: i32, @@ -165,7 +165,7 @@ fn makeBar(x: i32, y: i32) Bar { test "empty struct method call" { const es = EmptyStruct{}; - assertOrPanic(es.method() == 1234); + expect(es.method() == 1234); } const EmptyStruct = struct { fn method(es: *const EmptyStruct) i32 { @@ -182,7 +182,7 @@ fn testReturnEmptyStructFromFn() EmptyStruct2 { } test "pass slice of empty struct to fn" { - assertOrPanic(testPassSliceOfEmptyStructToFn([]EmptyStruct2{EmptyStruct2{}}) == 1); + expect(testPassSliceOfEmptyStructToFn([]EmptyStruct2{EmptyStruct2{}}) == 1); } fn testPassSliceOfEmptyStructToFn(slice: []const EmptyStruct2) usize { return slice.len; @@ -200,7 +200,7 @@ test "packed struct" { }; foo.y += 1; const four = foo.x + foo.y; - assertOrPanic(four == 4); + expect(four == 4); } const BitField1 = packed struct { @@ -217,17 +217,17 @@ const bit_field_1 = BitField1{ test "bit field access" { var data = bit_field_1; - assertOrPanic(getA(&data) == 1); - assertOrPanic(getB(&data) == 2); - assertOrPanic(getC(&data) == 3); - comptime assertOrPanic(@sizeOf(BitField1) == 1); + expect(getA(&data) == 1); + expect(getB(&data) == 2); + expect(getC(&data) == 3); + comptime expect(@sizeOf(BitField1) == 1); data.b += 1; - assertOrPanic(data.b == 3); + expect(data.b == 3); data.a += 1; - assertOrPanic(data.a == 2); - assertOrPanic(data.b == 3); + expect(data.a == 2); + expect(data.b == 3); } fn getA(data: *const BitField1) u3 { @@ -254,8 +254,8 @@ const Foo96Bits = packed struct { test "packed struct 24bits" { comptime { - assertOrPanic(@sizeOf(Foo24Bits) == 3); - assertOrPanic(@sizeOf(Foo96Bits) == 12); + expect(@sizeOf(Foo24Bits) == 3); + expect(@sizeOf(Foo96Bits) == 12); } var value = Foo96Bits{ @@ -265,28 +265,28 @@ test "packed struct 24bits" { .d = 0, }; value.a += 1; - assertOrPanic(value.a == 1); - assertOrPanic(value.b == 0); - assertOrPanic(value.c == 0); - assertOrPanic(value.d == 0); + expect(value.a == 1); + expect(value.b == 0); + expect(value.c == 0); + expect(value.d == 0); value.b += 1; - assertOrPanic(value.a == 1); - assertOrPanic(value.b == 1); - assertOrPanic(value.c == 0); - assertOrPanic(value.d == 0); + expect(value.a == 1); + expect(value.b == 1); + expect(value.c == 0); + expect(value.d == 0); value.c += 1; - assertOrPanic(value.a == 1); - assertOrPanic(value.b == 1); - assertOrPanic(value.c == 1); - assertOrPanic(value.d == 0); + expect(value.a == 1); + expect(value.b == 1); + expect(value.c == 1); + expect(value.d == 0); value.d += 1; - assertOrPanic(value.a == 1); - assertOrPanic(value.b == 1); - assertOrPanic(value.c == 1); - assertOrPanic(value.d == 1); + expect(value.a == 1); + expect(value.b == 1); + expect(value.c == 1); + expect(value.d == 1); } const FooArray24Bits = packed struct { @@ -297,43 +297,43 @@ const FooArray24Bits = packed struct { test "packed array 24bits" { comptime { - assertOrPanic(@sizeOf([9]Foo24Bits) == 9 * 3); - assertOrPanic(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2); + expect(@sizeOf([9]Foo24Bits) == 9 * 3); + expect(@sizeOf(FooArray24Bits) == 2 + 2 * 3 + 2); } var bytes = []u8{0} ** (@sizeOf(FooArray24Bits) + 1); bytes[bytes.len - 1] = 0xaa; const ptr = &@bytesToSlice(FooArray24Bits, bytes[0 .. bytes.len - 1])[0]; - assertOrPanic(ptr.a == 0); - assertOrPanic(ptr.b[0].field == 0); - assertOrPanic(ptr.b[1].field == 0); - assertOrPanic(ptr.c == 0); + expect(ptr.a == 0); + expect(ptr.b[0].field == 0); + expect(ptr.b[1].field == 0); + expect(ptr.c == 0); ptr.a = maxInt(u16); - assertOrPanic(ptr.a == maxInt(u16)); - assertOrPanic(ptr.b[0].field == 0); - assertOrPanic(ptr.b[1].field == 0); - assertOrPanic(ptr.c == 0); + expect(ptr.a == maxInt(u16)); + expect(ptr.b[0].field == 0); + expect(ptr.b[1].field == 0); + expect(ptr.c == 0); ptr.b[0].field = maxInt(u24); - assertOrPanic(ptr.a == maxInt(u16)); - assertOrPanic(ptr.b[0].field == maxInt(u24)); - assertOrPanic(ptr.b[1].field == 0); - assertOrPanic(ptr.c == 0); + expect(ptr.a == maxInt(u16)); + expect(ptr.b[0].field == maxInt(u24)); + expect(ptr.b[1].field == 0); + expect(ptr.c == 0); ptr.b[1].field = maxInt(u24); - assertOrPanic(ptr.a == maxInt(u16)); - assertOrPanic(ptr.b[0].field == maxInt(u24)); - assertOrPanic(ptr.b[1].field == maxInt(u24)); - assertOrPanic(ptr.c == 0); + expect(ptr.a == maxInt(u16)); + expect(ptr.b[0].field == maxInt(u24)); + expect(ptr.b[1].field == maxInt(u24)); + expect(ptr.c == 0); ptr.c = maxInt(u16); - assertOrPanic(ptr.a == maxInt(u16)); - assertOrPanic(ptr.b[0].field == maxInt(u24)); - assertOrPanic(ptr.b[1].field == maxInt(u24)); - assertOrPanic(ptr.c == maxInt(u16)); + expect(ptr.a == maxInt(u16)); + expect(ptr.b[0].field == maxInt(u24)); + expect(ptr.b[1].field == maxInt(u24)); + expect(ptr.c == maxInt(u16)); - assertOrPanic(bytes[bytes.len - 1] == 0xaa); + expect(bytes[bytes.len - 1] == 0xaa); } const FooStructAligned = packed struct { @@ -347,17 +347,17 @@ const FooArrayOfAligned = packed struct { test "aligned array of packed struct" { comptime { - assertOrPanic(@sizeOf(FooStructAligned) == 2); - assertOrPanic(@sizeOf(FooArrayOfAligned) == 2 * 2); + expect(@sizeOf(FooStructAligned) == 2); + expect(@sizeOf(FooArrayOfAligned) == 2 * 2); } var bytes = []u8{0xbb} ** @sizeOf(FooArrayOfAligned); const ptr = &@bytesToSlice(FooArrayOfAligned, bytes[0..bytes.len])[0]; - assertOrPanic(ptr.a[0].a == 0xbb); - assertOrPanic(ptr.a[0].b == 0xbb); - assertOrPanic(ptr.a[1].a == 0xbb); - assertOrPanic(ptr.a[1].b == 0xbb); + expect(ptr.a[0].a == 0xbb); + expect(ptr.a[0].b == 0xbb); + expect(ptr.a[1].a == 0xbb); + expect(ptr.a[1].b == 0xbb); } test "runtime struct initialization of bitfield" { @@ -370,10 +370,10 @@ test "runtime struct initialization of bitfield" { .y = @intCast(u4, x2), }; - assertOrPanic(s1.x == x1); - assertOrPanic(s1.y == x1); - assertOrPanic(s2.x == @intCast(u4, x2)); - assertOrPanic(s2.y == @intCast(u4, x2)); + expect(s1.x == x1); + expect(s1.y == x1); + expect(s2.x == @intCast(u4, x2)); + expect(s2.y == @intCast(u4, x2)); } var x1 = u4(1); @@ -400,18 +400,18 @@ test "native bit field understands endianness" { @memcpy(bytes[0..].ptr, @ptrCast([*]u8, &all), 8); var bitfields = @ptrCast(*Bitfields, bytes[0..].ptr).*; - assertOrPanic(bitfields.f1 == 0x1111); - assertOrPanic(bitfields.f2 == 0x2222); - assertOrPanic(bitfields.f3 == 0x33); - assertOrPanic(bitfields.f4 == 0x44); - assertOrPanic(bitfields.f5 == 0x5); - assertOrPanic(bitfields.f6 == 0x6); - assertOrPanic(bitfields.f7 == 0x77); + expect(bitfields.f1 == 0x1111); + expect(bitfields.f2 == 0x2222); + expect(bitfields.f3 == 0x33); + expect(bitfields.f4 == 0x44); + expect(bitfields.f5 == 0x5); + expect(bitfields.f6 == 0x6); + expect(bitfields.f7 == 0x77); } test "align 1 field before self referential align 8 field as slice return type" { const result = alloc(Expr); - assertOrPanic(result.len == 0); + expect(result.len == 0); } const Expr = union(enum) { @@ -434,10 +434,10 @@ test "call method with mutable reference to struct with no fields" { }; var s = S{}; - assertOrPanic(S.doC(&s)); - assertOrPanic(s.doC()); - assertOrPanic(S.do(&s)); - assertOrPanic(s.do()); + expect(S.doC(&s)); + expect(s.doC()); + expect(S.do(&s)); + expect(s.do()); } test "implicit cast packed struct field to const ptr" { @@ -453,7 +453,7 @@ test "implicit cast packed struct field to const ptr" { var lup: LevelUpMove = undefined; lup.level = 12; const res = LevelUpMove.toInt(lup.level); - assertOrPanic(res == 12); + expect(res == 12); } test "pointer to packed struct member in a stack variable" { @@ -464,7 +464,7 @@ test "pointer to packed struct member in a stack variable" { var s = S{ .a = 2, .b = 0 }; var b_ptr = &s.b; - assertOrPanic(s.b == 0); + expect(s.b == 0); b_ptr.* = 2; - assertOrPanic(s.b == 2); + expect(s.b == 2); } diff --git a/test/stage1/behavior/struct_contains_null_ptr_itself.zig b/test/stage1/behavior/struct_contains_null_ptr_itself.zig index 4cc479f31c..991d742cec 100644 --- a/test/stage1/behavior/struct_contains_null_ptr_itself.zig +++ b/test/stage1/behavior/struct_contains_null_ptr_itself.zig @@ -1,9 +1,9 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "struct contains null pointer which contains original struct" { var x: ?*NodeLineComment = null; - assertOrPanic(x == null); + expect(x == null); } pub const Node = struct { diff --git a/test/stage1/behavior/struct_contains_slice_of_itself.zig b/test/stage1/behavior/struct_contains_slice_of_itself.zig index 15780a7c44..52c6579654 100644 --- a/test/stage1/behavior/struct_contains_slice_of_itself.zig +++ b/test/stage1/behavior/struct_contains_slice_of_itself.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const Node = struct { payload: i32, @@ -39,12 +39,12 @@ test "struct contains slice of itself" { .payload = 1234, .children = nodes[0..], }; - assertOrPanic(root.payload == 1234); - assertOrPanic(root.children[0].payload == 1); - assertOrPanic(root.children[1].payload == 2); - assertOrPanic(root.children[2].payload == 3); - assertOrPanic(root.children[2].children[0].payload == 31); - assertOrPanic(root.children[2].children[1].payload == 32); + expect(root.payload == 1234); + expect(root.children[0].payload == 1); + expect(root.children[1].payload == 2); + expect(root.children[2].payload == 3); + expect(root.children[2].children[0].payload == 31); + expect(root.children[2].children[1].payload == 32); } test "struct contains aligned slice of itself" { @@ -76,10 +76,10 @@ test "struct contains aligned slice of itself" { .payload = 1234, .children = nodes[0..], }; - assertOrPanic(root.payload == 1234); - assertOrPanic(root.children[0].payload == 1); - assertOrPanic(root.children[1].payload == 2); - assertOrPanic(root.children[2].payload == 3); - assertOrPanic(root.children[2].children[0].payload == 31); - assertOrPanic(root.children[2].children[1].payload == 32); + expect(root.payload == 1234); + expect(root.children[0].payload == 1); + expect(root.children[1].payload == 2); + expect(root.children[2].payload == 3); + expect(root.children[2].children[0].payload == 31); + expect(root.children[2].children[1].payload == 32); } diff --git a/test/stage1/behavior/switch.zig b/test/stage1/behavior/switch.zig index 4ac971397e..1059bf28f8 100644 --- a/test/stage1/behavior/switch.zig +++ b/test/stage1/behavior/switch.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "switch with numbers" { testSwitchWithNumbers(13); @@ -10,14 +10,14 @@ fn testSwitchWithNumbers(x: u32) void { 13 => true, else => false, }; - assertOrPanic(result); + expect(result); } test "switch with all ranges" { - assertOrPanic(testSwitchWithAllRanges(50, 3) == 1); - assertOrPanic(testSwitchWithAllRanges(101, 0) == 2); - assertOrPanic(testSwitchWithAllRanges(300, 5) == 3); - assertOrPanic(testSwitchWithAllRanges(301, 6) == 6); + expect(testSwitchWithAllRanges(50, 3) == 1); + expect(testSwitchWithAllRanges(101, 0) == 2); + expect(testSwitchWithAllRanges(300, 5) == 3); + expect(testSwitchWithAllRanges(301, 6) == 6); } fn testSwitchWithAllRanges(x: u32, y: u32) u32 { @@ -40,7 +40,7 @@ test "implicit comptime switch" { }; comptime { - assertOrPanic(result + 1 == 14); + expect(result + 1 == 14); } } @@ -71,7 +71,7 @@ fn nonConstSwitch(foo: SwitchStatmentFoo) void { SwitchStatmentFoo.C => 3, SwitchStatmentFoo.D => 4, }; - assertOrPanic(val == 3); + expect(val == 3); } const SwitchStatmentFoo = enum { A, @@ -93,10 +93,10 @@ const SwitchProngWithVarEnum = union(enum) { fn switchProngWithVarFn(a: SwitchProngWithVarEnum) void { switch (a) { SwitchProngWithVarEnum.One => |x| { - assertOrPanic(x == 13); + expect(x == 13); }, SwitchProngWithVarEnum.Two => |x| { - assertOrPanic(x == 13.0); + expect(x == 13.0); }, SwitchProngWithVarEnum.Meh => |x| { const v: void = x; @@ -116,7 +116,7 @@ fn testSwitchEnumPtrCapture() void { else => unreachable, } switch (value) { - SwitchProngWithVarEnum.One => |x| assertOrPanic(x == 1235), + SwitchProngWithVarEnum.One => |x| expect(x == 1235), else => unreachable, } } @@ -127,7 +127,7 @@ test "switch with multiple expressions" { 4, 5, 6 => 2, else => i32(3), }; - assertOrPanic(x == 2); + expect(x == 2); } fn returnsFive() i32 { return 5; @@ -149,12 +149,12 @@ fn returnsFalse() bool { } } test "switch on const enum with var" { - assertOrPanic(!returnsFalse()); + expect(!returnsFalse()); } test "switch on type" { - assertOrPanic(trueIfBoolFalseOtherwise(bool)); - assertOrPanic(!trueIfBoolFalseOtherwise(i32)); + expect(trueIfBoolFalseOtherwise(bool)); + expect(!trueIfBoolFalseOtherwise(i32)); } fn trueIfBoolFalseOtherwise(comptime T: type) bool { @@ -170,16 +170,16 @@ test "switch handles all cases of number" { } fn testSwitchHandleAllCases() void { - assertOrPanic(testSwitchHandleAllCasesExhaustive(0) == 3); - assertOrPanic(testSwitchHandleAllCasesExhaustive(1) == 2); - assertOrPanic(testSwitchHandleAllCasesExhaustive(2) == 1); - assertOrPanic(testSwitchHandleAllCasesExhaustive(3) == 0); + expect(testSwitchHandleAllCasesExhaustive(0) == 3); + expect(testSwitchHandleAllCasesExhaustive(1) == 2); + expect(testSwitchHandleAllCasesExhaustive(2) == 1); + expect(testSwitchHandleAllCasesExhaustive(3) == 0); - assertOrPanic(testSwitchHandleAllCasesRange(100) == 0); - assertOrPanic(testSwitchHandleAllCasesRange(200) == 1); - assertOrPanic(testSwitchHandleAllCasesRange(201) == 2); - assertOrPanic(testSwitchHandleAllCasesRange(202) == 4); - assertOrPanic(testSwitchHandleAllCasesRange(230) == 3); + expect(testSwitchHandleAllCasesRange(100) == 0); + expect(testSwitchHandleAllCasesRange(200) == 1); + expect(testSwitchHandleAllCasesRange(201) == 2); + expect(testSwitchHandleAllCasesRange(202) == 4); + expect(testSwitchHandleAllCasesRange(230) == 3); } fn testSwitchHandleAllCasesExhaustive(x: u2) u2 { @@ -207,8 +207,8 @@ test "switch all prongs unreachable" { } fn testAllProngsUnreachable() void { - assertOrPanic(switchWithUnreachable(1) == 2); - assertOrPanic(switchWithUnreachable(2) == 10); + expect(switchWithUnreachable(1) == 2); + expect(switchWithUnreachable(2) == 10); } fn switchWithUnreachable(x: i32) i32 { @@ -230,7 +230,7 @@ test "capture value of switch with all unreachable prongs" { const x = return_a_number() catch |err| switch (err) { else => unreachable, }; - assertOrPanic(x == 1); + expect(x == 1); } test "switching on booleans" { @@ -239,14 +239,14 @@ test "switching on booleans" { } fn testSwitchOnBools() void { - assertOrPanic(testSwitchOnBoolsTrueAndFalse(true) == false); - assertOrPanic(testSwitchOnBoolsTrueAndFalse(false) == true); + expect(testSwitchOnBoolsTrueAndFalse(true) == false); + expect(testSwitchOnBoolsTrueAndFalse(false) == true); - assertOrPanic(testSwitchOnBoolsTrueWithElse(true) == false); - assertOrPanic(testSwitchOnBoolsTrueWithElse(false) == true); + expect(testSwitchOnBoolsTrueWithElse(true) == false); + expect(testSwitchOnBoolsTrueWithElse(false) == true); - assertOrPanic(testSwitchOnBoolsFalseWithElse(true) == false); - assertOrPanic(testSwitchOnBoolsFalseWithElse(false) == true); + expect(testSwitchOnBoolsFalseWithElse(true) == false); + expect(testSwitchOnBoolsFalseWithElse(false) == true); } fn testSwitchOnBoolsTrueAndFalse(x: bool) bool { diff --git a/test/stage1/behavior/switch_prong_err_enum.zig b/test/stage1/behavior/switch_prong_err_enum.zig index 6ac1919f0d..3593eabb5a 100644 --- a/test/stage1/behavior/switch_prong_err_enum.zig +++ b/test/stage1/behavior/switch_prong_err_enum.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; var read_count: u64 = 0; @@ -22,9 +22,9 @@ fn doThing(form_id: u64) anyerror!FormValue { test "switch prong returns error enum" { switch (doThing(17) catch unreachable) { FormValue.Address => |payload| { - assertOrPanic(payload == 1); + expect(payload == 1); }, else => unreachable, } - assertOrPanic(read_count == 1); + expect(read_count == 1); } diff --git a/test/stage1/behavior/switch_prong_implicit_cast.zig b/test/stage1/behavior/switch_prong_implicit_cast.zig index 4ca031e2e1..da965915ca 100644 --- a/test/stage1/behavior/switch_prong_implicit_cast.zig +++ b/test/stage1/behavior/switch_prong_implicit_cast.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const FormValue = union(enum) { One: void, @@ -18,5 +18,5 @@ test "switch prong implicit cast" { FormValue.One => false, FormValue.Two => |x| x, }; - assertOrPanic(result); + expect(result); } diff --git a/test/stage1/behavior/this.zig b/test/stage1/behavior/this.zig index 0e3a7a03ae..a0bee3a3ee 100644 --- a/test/stage1/behavior/this.zig +++ b/test/stage1/behavior/this.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const module = @This(); @@ -20,7 +20,7 @@ fn add(x: i32, y: i32) i32 { } test "this refer to module call private fn" { - assertOrPanic(module.add(1, 2) == 3); + expect(module.add(1, 2) == 3); } test "this refer to container" { @@ -29,7 +29,7 @@ test "this refer to container" { .y = 34, }; pt.addOne(); - assertOrPanic(pt.x == 13); - assertOrPanic(pt.y == 35); + expect(pt.x == 13); + expect(pt.y == 35); } diff --git a/test/stage1/behavior/truncate.zig b/test/stage1/behavior/truncate.zig index b7904bc7fb..c195b64cbf 100644 --- a/test/stage1/behavior/truncate.zig +++ b/test/stage1/behavior/truncate.zig @@ -1,8 +1,8 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "truncate u0 to larger integer allowed and has comptime known result" { var x: u0 = 0; const y = @truncate(u8, x); - comptime assertOrPanic(y == 0); + comptime expect(y == 0); } diff --git a/test/stage1/behavior/try.zig b/test/stage1/behavior/try.zig index ed48875eb4..9c700f6260 100644 --- a/test/stage1/behavior/try.zig +++ b/test/stage1/behavior/try.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "try on error union" { tryOnErrorUnionImpl(); @@ -11,7 +11,7 @@ fn tryOnErrorUnionImpl() void { error.CrappedOut => i32(2), else => unreachable, }; - assertOrPanic(x == 11); + expect(x == 11); } fn returnsTen() anyerror!i32 { @@ -20,10 +20,10 @@ fn returnsTen() anyerror!i32 { test "try without vars" { const result1 = if (failIfTrue(true)) 1 else |_| i32(2); - assertOrPanic(result1 == 2); + expect(result1 == 2); const result2 = if (failIfTrue(false)) 1 else |_| i32(2); - assertOrPanic(result2 == 1); + expect(result2 == 1); } fn failIfTrue(ok: bool) anyerror!void { @@ -38,6 +38,6 @@ test "try then not executed with assignment" { if (failIfTrue(true)) { unreachable; } else |err| { - assertOrPanic(err == error.ItBroke); + expect(err == error.ItBroke); } } diff --git a/test/stage1/behavior/type_info.zig b/test/stage1/behavior/type_info.zig index f3bb17b282..ce0ad795b4 100644 --- a/test/stage1/behavior/type_info.zig +++ b/test/stage1/behavior/type_info.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; const TypeInfo = @import("builtin").TypeInfo; const TypeId = @import("builtin").TypeId; @@ -9,10 +9,10 @@ test "type info: tag type, void info" { } fn testBasic() void { - assertOrPanic(@TagType(TypeInfo) == TypeId); + expect(@TagType(TypeInfo) == TypeId); const void_info = @typeInfo(void); - assertOrPanic(TypeId(void_info) == TypeId.Void); - assertOrPanic(void_info.Void == {}); + expect(TypeId(void_info) == TypeId.Void); + expect(void_info.Void == {}); } test "type info: integer, floating point type info" { @@ -22,13 +22,13 @@ test "type info: integer, floating point type info" { fn testIntFloat() void { const u8_info = @typeInfo(u8); - assertOrPanic(TypeId(u8_info) == TypeId.Int); - assertOrPanic(!u8_info.Int.is_signed); - assertOrPanic(u8_info.Int.bits == 8); + expect(TypeId(u8_info) == TypeId.Int); + expect(!u8_info.Int.is_signed); + expect(u8_info.Int.bits == 8); const f64_info = @typeInfo(f64); - assertOrPanic(TypeId(f64_info) == TypeId.Float); - assertOrPanic(f64_info.Float.bits == 64); + expect(TypeId(f64_info) == TypeId.Float); + expect(f64_info.Float.bits == 64); } test "type info: pointer type info" { @@ -38,12 +38,12 @@ test "type info: pointer type info" { fn testPointer() void { const u32_ptr_info = @typeInfo(*u32); - assertOrPanic(TypeId(u32_ptr_info) == TypeId.Pointer); - assertOrPanic(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One); - assertOrPanic(u32_ptr_info.Pointer.is_const == false); - assertOrPanic(u32_ptr_info.Pointer.is_volatile == false); - assertOrPanic(u32_ptr_info.Pointer.alignment == @alignOf(u32)); - assertOrPanic(u32_ptr_info.Pointer.child == u32); + expect(TypeId(u32_ptr_info) == TypeId.Pointer); + expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.One); + expect(u32_ptr_info.Pointer.is_const == false); + expect(u32_ptr_info.Pointer.is_volatile == false); + expect(u32_ptr_info.Pointer.alignment == @alignOf(u32)); + expect(u32_ptr_info.Pointer.child == u32); } test "type info: unknown length pointer type info" { @@ -53,12 +53,12 @@ test "type info: unknown length pointer type info" { fn testUnknownLenPtr() void { const u32_ptr_info = @typeInfo([*]const volatile f64); - assertOrPanic(TypeId(u32_ptr_info) == TypeId.Pointer); - assertOrPanic(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many); - assertOrPanic(u32_ptr_info.Pointer.is_const == true); - assertOrPanic(u32_ptr_info.Pointer.is_volatile == true); - assertOrPanic(u32_ptr_info.Pointer.alignment == @alignOf(f64)); - assertOrPanic(u32_ptr_info.Pointer.child == f64); + expect(TypeId(u32_ptr_info) == TypeId.Pointer); + expect(u32_ptr_info.Pointer.size == TypeInfo.Pointer.Size.Many); + expect(u32_ptr_info.Pointer.is_const == true); + expect(u32_ptr_info.Pointer.is_volatile == true); + expect(u32_ptr_info.Pointer.alignment == @alignOf(f64)); + expect(u32_ptr_info.Pointer.child == f64); } test "type info: slice type info" { @@ -68,12 +68,12 @@ test "type info: slice type info" { fn testSlice() void { const u32_slice_info = @typeInfo([]u32); - assertOrPanic(TypeId(u32_slice_info) == TypeId.Pointer); - assertOrPanic(u32_slice_info.Pointer.size == TypeInfo.Pointer.Size.Slice); - assertOrPanic(u32_slice_info.Pointer.is_const == false); - assertOrPanic(u32_slice_info.Pointer.is_volatile == false); - assertOrPanic(u32_slice_info.Pointer.alignment == 4); - assertOrPanic(u32_slice_info.Pointer.child == u32); + expect(TypeId(u32_slice_info) == TypeId.Pointer); + expect(u32_slice_info.Pointer.size == TypeInfo.Pointer.Size.Slice); + expect(u32_slice_info.Pointer.is_const == false); + expect(u32_slice_info.Pointer.is_volatile == false); + expect(u32_slice_info.Pointer.alignment == 4); + expect(u32_slice_info.Pointer.child == u32); } test "type info: array type info" { @@ -83,9 +83,9 @@ test "type info: array type info" { fn testArray() void { const arr_info = @typeInfo([42]bool); - assertOrPanic(TypeId(arr_info) == TypeId.Array); - assertOrPanic(arr_info.Array.len == 42); - assertOrPanic(arr_info.Array.child == bool); + expect(TypeId(arr_info) == TypeId.Array); + expect(arr_info.Array.len == 42); + expect(arr_info.Array.child == bool); } test "type info: optional type info" { @@ -95,8 +95,8 @@ test "type info: optional type info" { fn testOptional() void { const null_info = @typeInfo(?void); - assertOrPanic(TypeId(null_info) == TypeId.Optional); - assertOrPanic(null_info.Optional.child == void); + expect(TypeId(null_info) == TypeId.Optional); + expect(null_info.Optional.child == void); } test "type info: promise info" { @@ -106,12 +106,12 @@ test "type info: promise info" { fn testPromise() void { const null_promise_info = @typeInfo(promise); - assertOrPanic(TypeId(null_promise_info) == TypeId.Promise); - assertOrPanic(null_promise_info.Promise.child == null); + expect(TypeId(null_promise_info) == TypeId.Promise); + expect(null_promise_info.Promise.child == null); const promise_info = @typeInfo(promise->usize); - assertOrPanic(TypeId(promise_info) == TypeId.Promise); - assertOrPanic(promise_info.Promise.child.? == usize); + expect(TypeId(promise_info) == TypeId.Promise); + expect(promise_info.Promise.child.? == usize); } test "type info: error set, error union info" { @@ -127,15 +127,15 @@ fn testErrorSet() void { }; const error_set_info = @typeInfo(TestErrorSet); - assertOrPanic(TypeId(error_set_info) == TypeId.ErrorSet); - assertOrPanic(error_set_info.ErrorSet.errors.len == 3); - assertOrPanic(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First")); - assertOrPanic(error_set_info.ErrorSet.errors[2].value == @errorToInt(TestErrorSet.Third)); + expect(TypeId(error_set_info) == TypeId.ErrorSet); + expect(error_set_info.ErrorSet.errors.len == 3); + expect(mem.eql(u8, error_set_info.ErrorSet.errors[0].name, "First")); + expect(error_set_info.ErrorSet.errors[2].value == @errorToInt(TestErrorSet.Third)); const error_union_info = @typeInfo(TestErrorSet!usize); - assertOrPanic(TypeId(error_union_info) == TypeId.ErrorUnion); - assertOrPanic(error_union_info.ErrorUnion.error_set == TestErrorSet); - assertOrPanic(error_union_info.ErrorUnion.payload == usize); + expect(TypeId(error_union_info) == TypeId.ErrorUnion); + expect(error_union_info.ErrorUnion.error_set == TestErrorSet); + expect(error_union_info.ErrorUnion.payload == usize); } test "type info: enum info" { @@ -152,13 +152,13 @@ fn testEnum() void { }; const os_info = @typeInfo(Os); - assertOrPanic(TypeId(os_info) == TypeId.Enum); - assertOrPanic(os_info.Enum.layout == TypeInfo.ContainerLayout.Auto); - assertOrPanic(os_info.Enum.fields.len == 4); - assertOrPanic(mem.eql(u8, os_info.Enum.fields[1].name, "Macos")); - assertOrPanic(os_info.Enum.fields[3].value == 3); - assertOrPanic(os_info.Enum.tag_type == u2); - assertOrPanic(os_info.Enum.defs.len == 0); + expect(TypeId(os_info) == TypeId.Enum); + expect(os_info.Enum.layout == TypeInfo.ContainerLayout.Auto); + expect(os_info.Enum.fields.len == 4); + expect(mem.eql(u8, os_info.Enum.fields[1].name, "Macos")); + expect(os_info.Enum.fields[3].value == 3); + expect(os_info.Enum.tag_type == u2); + expect(os_info.Enum.defs.len == 0); } test "type info: union info" { @@ -168,14 +168,14 @@ test "type info: union info" { fn testUnion() void { const typeinfo_info = @typeInfo(TypeInfo); - assertOrPanic(TypeId(typeinfo_info) == TypeId.Union); - assertOrPanic(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto); - assertOrPanic(typeinfo_info.Union.tag_type.? == TypeId); - assertOrPanic(typeinfo_info.Union.fields.len == 25); - assertOrPanic(typeinfo_info.Union.fields[4].enum_field != null); - assertOrPanic(typeinfo_info.Union.fields[4].enum_field.?.value == 4); - assertOrPanic(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int)); - assertOrPanic(typeinfo_info.Union.defs.len == 21); + expect(TypeId(typeinfo_info) == TypeId.Union); + expect(typeinfo_info.Union.layout == TypeInfo.ContainerLayout.Auto); + expect(typeinfo_info.Union.tag_type.? == TypeId); + expect(typeinfo_info.Union.fields.len == 25); + expect(typeinfo_info.Union.fields[4].enum_field != null); + expect(typeinfo_info.Union.fields[4].enum_field.?.value == 4); + expect(typeinfo_info.Union.fields[4].field_type == @typeOf(@typeInfo(u8).Int)); + expect(typeinfo_info.Union.defs.len == 21); const TestNoTagUnion = union { Foo: void, @@ -183,22 +183,22 @@ fn testUnion() void { }; const notag_union_info = @typeInfo(TestNoTagUnion); - assertOrPanic(TypeId(notag_union_info) == TypeId.Union); - assertOrPanic(notag_union_info.Union.tag_type == null); - assertOrPanic(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto); - assertOrPanic(notag_union_info.Union.fields.len == 2); - assertOrPanic(notag_union_info.Union.fields[0].enum_field == null); - assertOrPanic(notag_union_info.Union.fields[1].field_type == u32); + expect(TypeId(notag_union_info) == TypeId.Union); + expect(notag_union_info.Union.tag_type == null); + expect(notag_union_info.Union.layout == TypeInfo.ContainerLayout.Auto); + expect(notag_union_info.Union.fields.len == 2); + expect(notag_union_info.Union.fields[0].enum_field == null); + expect(notag_union_info.Union.fields[1].field_type == u32); const TestExternUnion = extern union { foo: *c_void, }; const extern_union_info = @typeInfo(TestExternUnion); - assertOrPanic(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern); - assertOrPanic(extern_union_info.Union.tag_type == null); - assertOrPanic(extern_union_info.Union.fields[0].enum_field == null); - assertOrPanic(extern_union_info.Union.fields[0].field_type == *c_void); + expect(extern_union_info.Union.layout == TypeInfo.ContainerLayout.Extern); + expect(extern_union_info.Union.tag_type == null); + expect(extern_union_info.Union.fields[0].enum_field == null); + expect(extern_union_info.Union.fields[0].field_type == *c_void); } test "type info: struct info" { @@ -208,17 +208,17 @@ test "type info: struct info" { fn testStruct() void { const struct_info = @typeInfo(TestStruct); - assertOrPanic(TypeId(struct_info) == TypeId.Struct); - assertOrPanic(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed); - assertOrPanic(struct_info.Struct.fields.len == 3); - assertOrPanic(struct_info.Struct.fields[1].offset == null); - assertOrPanic(struct_info.Struct.fields[2].field_type == *TestStruct); - assertOrPanic(struct_info.Struct.defs.len == 2); - assertOrPanic(struct_info.Struct.defs[0].is_pub); - assertOrPanic(!struct_info.Struct.defs[0].data.Fn.is_extern); - assertOrPanic(struct_info.Struct.defs[0].data.Fn.lib_name == null); - assertOrPanic(struct_info.Struct.defs[0].data.Fn.return_type == void); - assertOrPanic(struct_info.Struct.defs[0].data.Fn.fn_type == fn (*const TestStruct) void); + expect(TypeId(struct_info) == TypeId.Struct); + expect(struct_info.Struct.layout == TypeInfo.ContainerLayout.Packed); + expect(struct_info.Struct.fields.len == 3); + expect(struct_info.Struct.fields[1].offset == null); + expect(struct_info.Struct.fields[2].field_type == *TestStruct); + expect(struct_info.Struct.defs.len == 2); + expect(struct_info.Struct.defs[0].is_pub); + expect(!struct_info.Struct.defs[0].data.Fn.is_extern); + expect(struct_info.Struct.defs[0].data.Fn.lib_name == null); + expect(struct_info.Struct.defs[0].data.Fn.return_type == void); + expect(struct_info.Struct.defs[0].data.Fn.fn_type == fn (*const TestStruct) void); } const TestStruct = packed struct { @@ -238,18 +238,18 @@ test "type info: function type info" { fn testFunction() void { const fn_info = @typeInfo(@typeOf(foo)); - assertOrPanic(TypeId(fn_info) == TypeId.Fn); - assertOrPanic(fn_info.Fn.calling_convention == TypeInfo.CallingConvention.Unspecified); - assertOrPanic(fn_info.Fn.is_generic); - assertOrPanic(fn_info.Fn.args.len == 2); - assertOrPanic(fn_info.Fn.is_var_args); - assertOrPanic(fn_info.Fn.return_type == null); - assertOrPanic(fn_info.Fn.async_allocator_type == null); + expect(TypeId(fn_info) == TypeId.Fn); + expect(fn_info.Fn.calling_convention == TypeInfo.CallingConvention.Unspecified); + expect(fn_info.Fn.is_generic); + expect(fn_info.Fn.args.len == 2); + expect(fn_info.Fn.is_var_args); + expect(fn_info.Fn.return_type == null); + expect(fn_info.Fn.async_allocator_type == null); const test_instance: TestStruct = undefined; const bound_fn_info = @typeInfo(@typeOf(test_instance.foo)); - assertOrPanic(TypeId(bound_fn_info) == TypeId.BoundFn); - assertOrPanic(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct); + expect(TypeId(bound_fn_info) == TypeId.BoundFn); + expect(bound_fn_info.BoundFn.args[0].arg_type.? == *const TestStruct); } fn foo(comptime a: usize, b: bool, args: ...) usize { @@ -270,7 +270,7 @@ test "type info: vectors" { fn testVector() void { const vec_info = @typeInfo(@Vector(4, i32)); - assertOrPanic(TypeId(vec_info) == TypeId.Vector); - assertOrPanic(vec_info.Vector.len == 4); - assertOrPanic(vec_info.Vector.child == i32); + expect(TypeId(vec_info) == TypeId.Vector); + expect(vec_info.Vector.len == 4); + expect(vec_info.Vector.child == i32); } diff --git a/test/stage1/behavior/undefined.zig b/test/stage1/behavior/undefined.zig index 333e217d49..4c233576cd 100644 --- a/test/stage1/behavior/undefined.zig +++ b/test/stage1/behavior/undefined.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const mem = @import("std").mem; fn initStaticArray() [10]i32 { @@ -11,16 +11,16 @@ fn initStaticArray() [10]i32 { } const static_array = initStaticArray(); test "init static array to undefined" { - assertOrPanic(static_array[0] == 1); - assertOrPanic(static_array[4] == 2); - assertOrPanic(static_array[7] == 3); - assertOrPanic(static_array[9] == 4); + expect(static_array[0] == 1); + expect(static_array[4] == 2); + expect(static_array[7] == 3); + expect(static_array[9] == 4); comptime { - assertOrPanic(static_array[0] == 1); - assertOrPanic(static_array[4] == 2); - assertOrPanic(static_array[7] == 3); - assertOrPanic(static_array[9] == 4); + expect(static_array[0] == 1); + expect(static_array[4] == 2); + expect(static_array[7] == 3); + expect(static_array[9] == 4); } } @@ -40,12 +40,12 @@ test "assign undefined to struct" { comptime { var foo: Foo = undefined; setFooX(&foo); - assertOrPanic(foo.x == 2); + expect(foo.x == 2); } { var foo: Foo = undefined; setFooX(&foo); - assertOrPanic(foo.x == 2); + expect(foo.x == 2); } } @@ -53,17 +53,17 @@ test "assign undefined to struct with method" { comptime { var foo: Foo = undefined; foo.setFooXMethod(); - assertOrPanic(foo.x == 3); + expect(foo.x == 3); } { var foo: Foo = undefined; foo.setFooXMethod(); - assertOrPanic(foo.x == 3); + expect(foo.x == 3); } } test "type name of undefined" { const x = undefined; - assertOrPanic(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)")); + expect(mem.eql(u8, @typeName(@typeOf(x)), "(undefined)")); } diff --git a/test/stage1/behavior/underscore.zig b/test/stage1/behavior/underscore.zig index 7443319336..fd5aebc87e 100644 --- a/test/stage1/behavior/underscore.zig +++ b/test/stage1/behavior/underscore.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "ignore lval with underscore" { _ = false; diff --git a/test/stage1/behavior/union.zig b/test/stage1/behavior/union.zig index c8e8feb11e..0a4e2cfb92 100644 --- a/test/stage1/behavior/union.zig +++ b/test/stage1/behavior/union.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const Value = union(enum) { Int: u64, @@ -27,11 +27,11 @@ const array = []Value{ test "unions embedded in aggregate types" { switch (array[1]) { - Value.Array => |arr| assertOrPanic(arr[4] == 3), + Value.Array => |arr| expect(arr[4] == 3), else => unreachable, } switch ((err catch unreachable).val1) { - Value.Int => |x| assertOrPanic(x == 1234), + Value.Int => |x| expect(x == 1234), else => unreachable, } } @@ -43,18 +43,18 @@ const Foo = union { test "basic unions" { var foo = Foo{ .int = 1 }; - assertOrPanic(foo.int == 1); + expect(foo.int == 1); foo = Foo{ .float = 12.34 }; - assertOrPanic(foo.float == 12.34); + expect(foo.float == 12.34); } test "comptime union field access" { comptime { var foo = Foo{ .int = 0 }; - assertOrPanic(foo.int == 0); + expect(foo.int == 0); foo = Foo{ .float = 42.42 }; - assertOrPanic(foo.float == 42.42); + expect(foo.float == 42.42); } } @@ -62,10 +62,10 @@ test "init union with runtime value" { var foo: Foo = undefined; setFloat(&foo, 12.34); - assertOrPanic(foo.float == 12.34); + expect(foo.float == 12.34); setInt(&foo, 42); - assertOrPanic(foo.int == 42); + expect(foo.int == 42); } fn setFloat(foo: *Foo, x: f64) void { @@ -83,9 +83,9 @@ const FooExtern = extern union { test "basic extern unions" { var foo = FooExtern{ .int = 1 }; - assertOrPanic(foo.int == 1); + expect(foo.int == 1); foo.float = 12.34; - assertOrPanic(foo.float == 12.34); + expect(foo.float == 12.34); } const Letter = enum { @@ -105,11 +105,11 @@ test "union with specified enum tag" { } fn doTest() void { - assertOrPanic(bar(Payload{ .A = 1234 }) == -10); + expect(bar(Payload{ .A = 1234 }) == -10); } fn bar(value: Payload) i32 { - assertOrPanic(Letter(value) == Letter.A); + expect(Letter(value) == Letter.A); return switch (value) { Payload.A => |x| return x - 1244, Payload.B => |x| if (x == 12.34) i32(20) else 21, @@ -125,8 +125,8 @@ const MultipleChoice = union(enum(u32)) { }; test "simple union(enum(u32))" { var x = MultipleChoice.C; - assertOrPanic(x == MultipleChoice.C); - assertOrPanic(@enumToInt(@TagType(MultipleChoice)(x)) == 60); + expect(x == MultipleChoice.C); + expect(@enumToInt(@TagType(MultipleChoice)(x)) == 60); } const MultipleChoice2 = union(enum(u32)) { @@ -142,14 +142,14 @@ const MultipleChoice2 = union(enum(u32)) { }; test "union(enum(u32)) with specified and unspecified tag values" { - comptime assertOrPanic(@TagType(@TagType(MultipleChoice2)) == u32); + comptime expect(@TagType(@TagType(MultipleChoice2)) == u32); testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 }); comptime testEnumWithSpecifiedAndUnspecifiedTagValues(MultipleChoice2{ .C = 123 }); } fn testEnumWithSpecifiedAndUnspecifiedTagValues(x: MultipleChoice2) void { - assertOrPanic(@enumToInt(@TagType(MultipleChoice2)(x)) == 60); - assertOrPanic(1123 == switch (x) { + expect(@enumToInt(@TagType(MultipleChoice2)(x)) == 60); + expect(1123 == switch (x) { MultipleChoice2.A => 1, MultipleChoice2.B => 2, MultipleChoice2.C => |v| i32(1000) + v, @@ -167,7 +167,7 @@ const ExternPtrOrInt = extern union { int: u64, }; test "extern union size" { - comptime assertOrPanic(@sizeOf(ExternPtrOrInt) == 8); + comptime expect(@sizeOf(ExternPtrOrInt) == 8); } const PackedPtrOrInt = packed union { @@ -175,14 +175,14 @@ const PackedPtrOrInt = packed union { int: u64, }; test "extern union size" { - comptime assertOrPanic(@sizeOf(PackedPtrOrInt) == 8); + comptime expect(@sizeOf(PackedPtrOrInt) == 8); } const ZeroBits = union { OnlyField: void, }; test "union with only 1 field which is void should be zero bits" { - comptime assertOrPanic(@sizeOf(ZeroBits) == 0); + comptime expect(@sizeOf(ZeroBits) == 0); } const TheTag = enum { @@ -196,9 +196,9 @@ const TheUnion = union(TheTag) { C: i32, }; test "union field access gives the enum values" { - assertOrPanic(TheUnion.A == TheTag.A); - assertOrPanic(TheUnion.B == TheTag.B); - assertOrPanic(TheUnion.C == TheTag.C); + expect(TheUnion.A == TheTag.A); + expect(TheUnion.B == TheTag.B); + expect(TheUnion.C == TheTag.C); } test "cast union to tag type of union" { @@ -207,12 +207,12 @@ test "cast union to tag type of union" { } fn testCastUnionToTagType(x: TheUnion) void { - assertOrPanic(TheTag(x) == TheTag.B); + expect(TheTag(x) == TheTag.B); } test "cast tag type of union to union" { var x: Value2 = Letter2.B; - assertOrPanic(Letter2(x) == Letter2.B); + expect(Letter2(x) == Letter2.B); } const Letter2 = enum { A, @@ -227,11 +227,11 @@ const Value2 = union(Letter2) { test "implicit cast union to its tag type" { var x: Value2 = Letter2.B; - assertOrPanic(x == Letter2.B); + expect(x == Letter2.B); giveMeLetterB(x); } fn giveMeLetterB(x: Letter2) void { - assertOrPanic(x == Value2.B); + expect(x == Value2.B); } pub const PackThis = union(enum) { @@ -244,7 +244,7 @@ test "constant packed union" { } fn testConstPackedUnion(expected_tokens: []const PackThis) void { - assertOrPanic(expected_tokens[0].StringLiteral == 1); + expect(expected_tokens[0].StringLiteral == 1); } test "switch on union with only 1 field" { @@ -256,7 +256,7 @@ test "switch on union with only 1 field" { z = PartialInstWithPayload{ .Compiled = 1234 }; switch (z) { PartialInstWithPayload.Compiled => |x| { - assertOrPanic(x == 1234); + expect(x == 1234); return; }, } @@ -282,11 +282,11 @@ test "access a member of tagged union with conflicting enum tag name" { const B = void; }; - comptime assertOrPanic(Bar.A == u8); + comptime expect(Bar.A == u8); } test "tagged union initialization with runtime void" { - assertOrPanic(testTaggedUnionInit({})); + expect(testTaggedUnionInit({})); } const TaggedUnionWithAVoid = union(enum) { @@ -324,9 +324,9 @@ test "union with only 1 field casted to its enum type" { var e = Expr{ .Literal = Literal{ .Bool = true } }; const Tag = @TagType(Expr); - comptime assertOrPanic(@TagType(Tag) == comptime_int); + comptime expect(@TagType(Tag) == comptime_int); var t = Tag(e); - assertOrPanic(t == Expr.Literal); + expect(t == Expr.Literal); } test "union with only 1 field casted to its enum type which has enum value specified" { @@ -344,9 +344,9 @@ test "union with only 1 field casted to its enum type which has enum value speci }; var e = Expr{ .Literal = Literal{ .Bool = true } }; - comptime assertOrPanic(@TagType(Tag) == comptime_int); + comptime expect(@TagType(Tag) == comptime_int); var t = Tag(e); - assertOrPanic(t == Expr.Literal); - assertOrPanic(@enumToInt(t) == 33); - comptime assertOrPanic(@enumToInt(t) == 33); + expect(t == Expr.Literal); + expect(@enumToInt(t) == 33); + comptime expect(@enumToInt(t) == 33); } diff --git a/test/stage1/behavior/var_args.zig b/test/stage1/behavior/var_args.zig index 1f782a3bb3..cc93b57f06 100644 --- a/test/stage1/behavior/var_args.zig +++ b/test/stage1/behavior/var_args.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; fn add(args: ...) i32 { var sum = i32(0); @@ -12,9 +12,9 @@ fn add(args: ...) i32 { } test "add arbitrary args" { - assertOrPanic(add(i32(1), i32(2), i32(3), i32(4)) == 10); - assertOrPanic(add(i32(1234)) == 1234); - assertOrPanic(add() == 0); + expect(add(i32(1), i32(2), i32(3), i32(4)) == 10); + expect(add(i32(1234)) == 1234); + expect(add() == 0); } fn readFirstVarArg(args: ...) void { @@ -26,9 +26,9 @@ test "send void arg to var args" { } test "pass args directly" { - assertOrPanic(addSomeStuff(i32(1), i32(2), i32(3), i32(4)) == 10); - assertOrPanic(addSomeStuff(i32(1234)) == 1234); - assertOrPanic(addSomeStuff() == 0); + expect(addSomeStuff(i32(1), i32(2), i32(3), i32(4)) == 10); + expect(addSomeStuff(i32(1234)) == 1234); + expect(addSomeStuff() == 0); } fn addSomeStuff(args: ...) i32 { @@ -36,24 +36,24 @@ fn addSomeStuff(args: ...) i32 { } test "runtime parameter before var args" { - assertOrPanic(extraFn(10) == 0); - assertOrPanic(extraFn(10, false) == 1); - assertOrPanic(extraFn(10, false, true) == 2); + expect(extraFn(10) == 0); + expect(extraFn(10, false) == 1); + expect(extraFn(10, false, true) == 2); // TODO issue #313 //comptime { - // assertOrPanic(extraFn(10) == 0); - // assertOrPanic(extraFn(10, false) == 1); - // assertOrPanic(extraFn(10, false, true) == 2); + // expect(extraFn(10) == 0); + // expect(extraFn(10, false) == 1); + // expect(extraFn(10, false, true) == 2); //} } fn extraFn(extra: u32, args: ...) usize { if (args.len >= 1) { - assertOrPanic(args[0] == false); + expect(args[0] == false); } if (args.len >= 2) { - assertOrPanic(args[1] == true); + expect(args[1] == true); } return args.len; } @@ -71,8 +71,8 @@ fn foo2(args: ...) bool { } test "array of var args functions" { - assertOrPanic(foos[0]()); - assertOrPanic(!foos[1]()); + expect(foos[0]()); + expect(!foos[1]()); } test "pass zero length array to var args param" { diff --git a/test/stage1/behavior/vector.zig b/test/stage1/behavior/vector.zig index c97ee0e3d6..7cead12b65 100644 --- a/test/stage1/behavior/vector.zig +++ b/test/stage1/behavior/vector.zig @@ -1,15 +1,15 @@ const std = @import("std"); const mem = std.mem; -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; test "vector wrap operators" { const S = struct { fn doTheTest() void { const v: @Vector(4, i32) = [4]i32{ 10, 20, 30, 40 }; const x: @Vector(4, i32) = [4]i32{ 1, 2, 3, 4 }; - assertOrPanic(mem.eql(i32, ([4]i32)(v +% x), [4]i32{ 11, 22, 33, 44 })); - assertOrPanic(mem.eql(i32, ([4]i32)(v -% x), [4]i32{ 9, 18, 27, 36 })); - assertOrPanic(mem.eql(i32, ([4]i32)(v *% x), [4]i32{ 10, 40, 90, 160 })); + expect(mem.eql(i32, ([4]i32)(v +% x), [4]i32{ 11, 22, 33, 44 })); + expect(mem.eql(i32, ([4]i32)(v -% x), [4]i32{ 9, 18, 27, 36 })); + expect(mem.eql(i32, ([4]i32)(v *% x), [4]i32{ 10, 40, 90, 160 })); } }; S.doTheTest(); diff --git a/test/stage1/behavior/void.zig b/test/stage1/behavior/void.zig index 431d3f4eb1..9722791946 100644 --- a/test/stage1/behavior/void.zig +++ b/test/stage1/behavior/void.zig @@ -1,4 +1,4 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; const Foo = struct { a: void, @@ -13,14 +13,14 @@ test "compare void with void compile time known" { .b = 1, .c = {}, }; - assertOrPanic(foo.a == {}); + expect(foo.a == {}); } } test "iterate over a void slice" { var j: usize = 0; for (times(10)) |_, i| { - assertOrPanic(i == j); + expect(i == j); j += 1; } } @@ -31,5 +31,5 @@ fn times(n: usize) []const void { test "void optional" { var x: ?void = {}; - assertOrPanic(x != null); + expect(x != null); } diff --git a/test/stage1/behavior/while.zig b/test/stage1/behavior/while.zig index 579b4e4db8..29ad90ed17 100644 --- a/test/stage1/behavior/while.zig +++ b/test/stage1/behavior/while.zig @@ -1,12 +1,12 @@ -const assertOrPanic = @import("std").debug.assertOrPanic; +const expect = @import("std").testing.expect; test "while loop" { var i: i32 = 0; while (i < 4) { i += 1; } - assertOrPanic(i == 4); - assertOrPanic(whileLoop1() == 1); + expect(i == 4); + expect(whileLoop1() == 1); } fn whileLoop1() i32 { return whileLoop2(); @@ -18,7 +18,7 @@ fn whileLoop2() i32 { } test "static eval while" { - assertOrPanic(static_eval_while_number == 1); + expect(static_eval_while_number == 1); } const static_eval_while_number = staticWhileLoop1(); fn staticWhileLoop1() i32 { @@ -32,7 +32,7 @@ fn staticWhileLoop2() i32 { test "continue and break" { runContinueAndBreakTest(); - assertOrPanic(continue_and_break_counter == 8); + expect(continue_and_break_counter == 8); } var continue_and_break_counter: i32 = 0; fn runContinueAndBreakTest() void { @@ -45,7 +45,7 @@ fn runContinueAndBreakTest() void { } break; } - assertOrPanic(i == 4); + expect(i == 4); } test "return with implicit cast from while loop" { @@ -66,7 +66,7 @@ test "while with continue expression" { sum += i; } } - assertOrPanic(sum == 40); + expect(sum == 40); } test "while with else" { @@ -78,8 +78,8 @@ test "while with else" { } else { got_else += 1; } - assertOrPanic(sum == 10); - assertOrPanic(got_else == 1); + expect(sum == 10); + expect(got_else == 1); } test "while with optional as condition" { @@ -88,7 +88,7 @@ test "while with optional as condition" { while (getNumberOrNull()) |value| { sum += value; } - assertOrPanic(sum == 45); + expect(sum == 45); } test "while with optional as condition with else" { @@ -97,12 +97,12 @@ test "while with optional as condition with else" { var got_else: i32 = 0; while (getNumberOrNull()) |value| { sum += value; - assertOrPanic(got_else == 0); + expect(got_else == 0); } else { got_else += 1; } - assertOrPanic(sum == 45); - assertOrPanic(got_else == 1); + expect(sum == 45); + expect(got_else == 1); } test "while with error union condition" { @@ -112,11 +112,11 @@ test "while with error union condition" { while (getNumberOrErr()) |value| { sum += value; } else |err| { - assertOrPanic(err == error.OutOfNumbers); + expect(err == error.OutOfNumbers); got_else += 1; } - assertOrPanic(sum == 45); - assertOrPanic(got_else == 1); + expect(sum == 45); + expect(got_else == 1); } var numbers_left: i32 = undefined; @@ -138,7 +138,7 @@ test "while on optional with else result follow else prong" { break value; } else i32(2); - assertOrPanic(result == 2); + expect(result == 2); } test "while on optional with else result follow break prong" { @@ -146,7 +146,7 @@ test "while on optional with else result follow break prong" { break value; } else i32(2); - assertOrPanic(result == 10); + expect(result == 10); } test "while on error union with else result follow else prong" { @@ -154,7 +154,7 @@ test "while on error union with else result follow else prong" { break value; } else |err| i32(2); - assertOrPanic(result == 2); + expect(result == 2); } test "while on error union with else result follow break prong" { @@ -162,7 +162,7 @@ test "while on error union with else result follow break prong" { break value; } else |err| i32(2); - assertOrPanic(result == 10); + expect(result == 10); } test "while on bool with else result follow else prong" { @@ -170,7 +170,7 @@ test "while on bool with else result follow else prong" { break i32(10); } else i32(2); - assertOrPanic(result == 2); + expect(result == 2); } test "while on bool with else result follow break prong" { @@ -178,7 +178,7 @@ test "while on bool with else result follow break prong" { break i32(10); } else i32(2); - assertOrPanic(result == 10); + expect(result == 10); } test "break from outer while loop" { diff --git a/test/stage1/behavior/widening.zig b/test/stage1/behavior/widening.zig index 7577868aff..f7c238ee8d 100644 --- a/test/stage1/behavior/widening.zig +++ b/test/stage1/behavior/widening.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; const mem = std.mem; test "integer widening" { @@ -9,13 +9,13 @@ test "integer widening" { var d: u64 = c; var e: u64 = d; var f: u128 = e; - assertOrPanic(f == a); + expect(f == a); } test "implicit unsigned integer to signed integer" { var a: u8 = 250; var b: i16 = a; - assertOrPanic(b == 250); + expect(b == 250); } test "float widening" { @@ -23,6 +23,6 @@ test "float widening" { var b: f32 = a; var c: f64 = b; var d: f128 = c; - assertOrPanic(d == a); + expect(d == a); } diff --git a/test/stage1/c_abi/main.zig b/test/stage1/c_abi/main.zig index 196311a283..4805fc9896 100644 --- a/test/stage1/c_abi/main.zig +++ b/test/stage1/c_abi/main.zig @@ -1,5 +1,5 @@ const std = @import("std"); -const assertOrPanic = std.debug.assertOrPanic; +const expect = std.testing.expect; extern fn run_c_tests() void; @@ -33,28 +33,28 @@ test "C ABI integers" { } export fn zig_u8(x: u8) void { - assertOrPanic(x == 0xff); + expect(x == 0xff); } export fn zig_u16(x: u16) void { - assertOrPanic(x == 0xfffe); + expect(x == 0xfffe); } export fn zig_u32(x: u32) void { - assertOrPanic(x == 0xfffffffd); + expect(x == 0xfffffffd); } export fn zig_u64(x: u64) void { - assertOrPanic(x == 0xfffffffffffffffc); + expect(x == 0xfffffffffffffffc); } export fn zig_i8(x: i8) void { - assertOrPanic(x == -1); + expect(x == -1); } export fn zig_i16(x: i16) void { - assertOrPanic(x == -2); + expect(x == -2); } export fn zig_i32(x: i32) void { - assertOrPanic(x == -3); + expect(x == -3); } export fn zig_i64(x: i64) void { - assertOrPanic(x == -4); + expect(x == -4); } extern fn c_f32(f32) void; @@ -66,10 +66,10 @@ test "C ABI floats" { } export fn zig_f32(x: f32) void { - assertOrPanic(x == 12.34); + expect(x == 12.34); } export fn zig_f64(x: f64) void { - assertOrPanic(x == 56.78); + expect(x == 56.78); } extern fn c_ptr(*c_void) void; @@ -79,7 +79,7 @@ test "C ABI pointer" { } export fn zig_ptr(x: *c_void) void { - assertOrPanic(@ptrToInt(x) == 0xdeadbeef); + expect(@ptrToInt(x) == 0xdeadbeef); } extern fn c_bool(bool) void; @@ -89,7 +89,7 @@ test "C ABI bool" { } export fn zig_bool(x: bool) void { - assertOrPanic(x); + expect(x); } extern fn c_array([10]u8) void; @@ -100,7 +100,7 @@ test "C ABI array" { } export fn zig_array(x: [10]u8) void { - assertOrPanic(std.mem.eql(u8, x, "1234567890")); + expect(std.mem.eql(u8, x, "1234567890")); } const BigStruct = extern struct { @@ -124,11 +124,11 @@ test "C ABI big struct" { } export fn zig_big_struct(x: BigStruct) void { - assertOrPanic(x.a == 1); - assertOrPanic(x.b == 2); - assertOrPanic(x.c == 3); - assertOrPanic(x.d == 4); - assertOrPanic(x.e == 5); + expect(x.a == 1); + expect(x.b == 2); + expect(x.c == 3); + expect(x.d == 4); + expect(x.e == 5); } const BigUnion = extern union { @@ -150,11 +150,11 @@ test "C ABI big union" { } export fn zig_big_union(x: BigUnion) void { - assertOrPanic(x.a.a == 1); - assertOrPanic(x.a.b == 2); - assertOrPanic(x.a.c == 3); - assertOrPanic(x.a.d == 4); - assertOrPanic(x.a.e == 5); + expect(x.a.a == 1); + expect(x.a.b == 2); + expect(x.a.c == 3); + expect(x.a.d == 4); + expect(x.a.e == 5); } const SmallStructInts = extern struct { @@ -176,10 +176,10 @@ test "C ABI small struct of ints" { } export fn zig_small_struct_ints(x: SmallStructInts) void { - assertOrPanic(x.a == 1); - assertOrPanic(x.b == 2); - assertOrPanic(x.c == 3); - assertOrPanic(x.d == 4); + expect(x.a == 1); + expect(x.b == 2); + expect(x.c == 3); + expect(x.d == 4); } const SplitStructInt = extern struct { @@ -199,9 +199,9 @@ test "C ABI split struct of ints" { } export fn zig_split_struct_ints(x: SplitStructInt) void { - assertOrPanic(x.a == 1234); - assertOrPanic(x.b == 100); - assertOrPanic(x.c == 1337); + expect(x.a == 1234); + expect(x.b == 100); + expect(x.c == 1337); } extern fn c_big_struct_both(BigStruct) BigStruct; @@ -215,19 +215,19 @@ test "C ABI sret and byval together" { .e = 5, }; var y = c_big_struct_both(s); - assertOrPanic(y.a == 10); - assertOrPanic(y.b == 11); - assertOrPanic(y.c == 12); - assertOrPanic(y.d == 13); - assertOrPanic(y.e == 14); + expect(y.a == 10); + expect(y.b == 11); + expect(y.c == 12); + expect(y.d == 13); + expect(y.e == 14); } export fn zig_big_struct_both(x: BigStruct) BigStruct { - assertOrPanic(x.a == 30); - assertOrPanic(x.b == 31); - assertOrPanic(x.c == 32); - assertOrPanic(x.d == 33); - assertOrPanic(x.e == 34); + expect(x.a == 30); + expect(x.b == 31); + expect(x.c == 32); + expect(x.d == 33); + expect(x.e == 34); var s = BigStruct{ .a = 20, .b = 21, diff --git a/test/standalone/brace_expansion/main.zig b/test/standalone/brace_expansion/main.zig index 52863d5fa4..f5bcd59ecf 100644 --- a/test/standalone/brace_expansion/main.zig +++ b/test/standalone/brace_expansion/main.zig @@ -3,6 +3,7 @@ const io = std.io; const mem = std.mem; const debug = std.debug; const assert = debug.assert; +const testing = std.testing; const Buffer = std.Buffer; const ArrayList = std.ArrayList; const maxInt = std.math.maxInt; @@ -220,11 +221,7 @@ fn expectError(test_input: []const u8, expected_err: anyerror) void { var output_buf = Buffer.initSize(global_allocator, 0) catch unreachable; defer output_buf.deinit(); - if (expandString(test_input, &output_buf)) { - unreachable; - } else |err| { - assert(expected_err == err); - } + testing.expectError(expected_err, expandString(test_input, &output_buf)); } test "valid inputs" { @@ -256,5 +253,5 @@ fn expectExpansion(test_input: []const u8, expected_result: []const u8) void { expandString(test_input, &result) catch unreachable; - assert(mem.eql(u8, result.toSlice(), expected_result)); + testing.expectEqualSlices(u8, expected_result, result.toSlice()); } diff --git a/test/standalone/issue_794/main.zig b/test/standalone/issue_794/main.zig index 356a106418..191bdc9b4f 100644 --- a/test/standalone/issue_794/main.zig +++ b/test/standalone/issue_794/main.zig @@ -1,7 +1,7 @@ const c = @cImport(@cInclude("foo.h")); const std = @import("std"); -const assert = std.debug.assert; +const testing = std.testing; test "c import" { - comptime assert(c.NUMBER == 1234); + comptime testing.expect(c.NUMBER == 1234); } -- cgit v1.2.3