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 --- std/hash/adler.zig | 12 ++++++------ std/hash/crc.zig | 25 +++++++++++++------------ std/hash/fnv.zig | 18 +++++++++--------- std/hash/siphash.zig | 15 ++++++++------- 4 files changed, 36 insertions(+), 34 deletions(-) (limited to 'std/hash') 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); } } -- cgit v1.2.3