Commit 3b5e26b7f7

Andrew Kelley <superjoe30@gmail.com>
2017-01-05 09:57:48
self hosted tests import std library
1 parent ee09eb7
test/cases/namespace_depends_on_compile_var/index.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn namespaceDependsOnCompileVar() {
     @setFnTest(this);
 
@@ -11,9 +13,3 @@ const some_namespace = switch(@compileVar("os")) {
     Os.linux => @import("cases/namespace_depends_on_compile_var/a.zig"),
     else => @import("cases/namespace_depends_on_compile_var/b.zig"),
 };
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/pub_enum/index.zig
@@ -1,4 +1,5 @@
 const other = @import("cases/pub_enum/other.zig");
+const assert = @import("std").debug.assert;
 
 fn pubEnum() {
     @setFnTest(this);
@@ -14,10 +15,3 @@ fn castWithImportedSymbol() {
 
     assert(other.size_t(42) == 42);
 }
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/array.zig
@@ -1,3 +1,6 @@
+const assert = @import("std").debug.assert;
+const str = @import("std").str;
+
 fn arrays() {
     @setFnTest(this);
 
@@ -60,32 +63,10 @@ fn nestedArrays() {
 
     const array_of_strings = [][]u8 {"hello", "this", "is", "my", "thing"};
     for (array_of_strings) |s, i| {
-        if (i == 0) assert(memeql(s, "hello"));
-        if (i == 1) assert(memeql(s, "this"));
-        if (i == 2) assert(memeql(s, "is"));
-        if (i == 3) assert(memeql(s, "my"));
-        if (i == 4) assert(memeql(s, "thing"));
-    }
-}
-
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
-// TODO import from std.str
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
-    sliceEql(u8, a, b)
-}
-
-// TODO import from std.str
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
-    if (a.len != b.len) return false;
-    for (a) |item, index| {
-        if (b[index] != item) return false;
+        if (i == 0) assert(str.eql(s, "hello"));
+        if (i == 1) assert(str.eql(s, "this"));
+        if (i == 2) assert(str.eql(s, "is"));
+        if (i == 3) assert(str.eql(s, "my"));
+        if (i == 4) assert(str.eql(s, "thing"));
     }
-    return true;
 }
test/cases/atomics.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn cmpxchg() {
     @setFnTest(this);
 
@@ -13,9 +15,3 @@ fn fence() {
     @fence(AtomicOrder.SeqCst);
     x = 5678;
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/bool.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn boolLiterals() {
     @setFnTest(this);
 
@@ -28,9 +30,3 @@ fn boolCmp() {
 fn testBoolCmp(a: bool, b: bool) -> bool {
     a == b
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/cast.zig
@@ -1,14 +1,10 @@
-//fn intToPtrCast() {
-//    @setFnTest(this);
-//
-//    const x = isize(13);
-//    const y = (&u8)(x);
-//    const z = usize(y);
-//    assert(z == 13);
-//}
+const assert = @import("std").debug.assert;
 
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
+fn intToPtrCast() {
+    @setFnTest(this);
+
+    const x = isize(13);
+    const y = (&u8)(x);
+    const z = usize(y);
+    assert(z == 13);
 }
test/cases/const_slice_child.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 var argv: &&const u8 = undefined;
 
 fn constSliceChild() {
@@ -41,9 +43,3 @@ fn streql(a: []const u8, b: []const u8) -> bool {
     }
     return true;
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/defer.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 var result: [3]u8 = undefined;
 var index: usize = undefined;
 
@@ -49,9 +51,3 @@ fn mixingNormalAndMaybeDefers() {
     assert(result[1] == 'b');
     assert(result[2] == 'a');
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/enum.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn enumType() {
     @setFnTest(this);
 
@@ -111,13 +113,3 @@ const IntToEnumNumber = enum {
     Three,
     Four,
 };
-
-
-// TODO import from std
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
-
-
test/cases/enum_with_members.zig
@@ -1,11 +1,15 @@
+const assert = @import("std").debug.assert;
+const str = @import("std").str;
+const io = @import("std").io;
+
 const ET = enum {
     SINT: i32,
     UINT: u32,
 
     pub fn print(a: &ET, buf: []u8) -> %usize {
         return switch (*a) {
-            ET.SINT => |x| { bufPrintInt(i32, buf, x) },
-            ET.UINT => |x| { bufPrintInt(u32, buf, x) },
+            ET.SINT => |x| { io.bufPrintInt(i32, buf, x) },
+            ET.UINT => |x| { io.bufPrintInt(u32, buf, x) },
         }
     }
 };
@@ -18,66 +22,8 @@ fn enumWithMembers() {
     var buf: [20]u8 = undefined;
 
     assert(%%a.print(buf) == 3);
-    assert(memeql(buf[0...3], "-42"));
+    assert(str.eql(buf[0...3], "-42"));
 
     assert(%%b.print(buf) == 2);
-    assert(memeql(buf[0...2], "42"));
-}
-
-// TODO all the below should be imported from std
-
-const max_u64_base10_digits = 20;
-pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize {
-    if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
-}
-
-fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
-    const uint = @intType(false, T.bit_count);
-    if (x < 0) {
-        out_buf[0] = '-';
-        return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
-    } else {
-        return bufPrintUnsigned(uint, out_buf, uint(x));
-    }
-}
-
-fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
-    var buf: [max_u64_base10_digits]u8 = undefined;
-    var a = x;
-    var index: usize = buf.len;
-
-    while (true) {
-        const digit = a % 10;
-        index -= 1;
-        buf[index] = '0' + u8(digit);
-        a /= 10;
-        if (a == 0)
-            break;
-    }
-
-    const len = buf.len - index;
-
-    @memcpy(&out_buf[0], &buf[index], len);
-
-    return len;
-}
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
-// TODO import from std.str
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
-    sliceEql(u8, a, b)
-}
-
-// TODO import from std.str
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
-    if (a.len != b.len) return false;
-    for (a) |item, index| {
-        if (b[index] != item) return false;
-    }
-    return true;
+    assert(str.eql(buf[0...2], "42"));
 }
test/cases/error.zig
@@ -1,3 +1,6 @@
+const assert = @import("std").debug.assert;
+const str = @import("std").str;
+
 pub fn foo() -> %i32 {
     const x = %return bar();
     return x + 1
@@ -25,8 +28,8 @@ fn gimmeItBroke() -> []const u8 {
 
 fn errorName() {
     @setFnTest(this);
-    assert(memeql(@errorName(error.AnError), "AnError"));
-    assert(memeql(@errorName(error.ALongerErrorName), "ALongerErrorName"));
+    assert(str.eql(@errorName(error.AnError), "AnError"));
+    assert(str.eql(@errorName(error.ALongerErrorName), "ALongerErrorName"));
 }
 error AnError;
 error ALongerErrorName;
@@ -97,26 +100,3 @@ fn doErrReturnInAssignment() -> %void {
 fn makeANonErr() -> %i32 {
     return 1;
 }
-
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
-// TODO import from std.str
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
-    sliceEql(u8, a, b)
-}
-
-// TODO import from std.str
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
-    if (a.len != b.len) return false;
-    for (a) |item, index| {
-        if (b[index] != item) return false;
-    }
-    return true;
-}
-
test/cases/eval.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn compileTimeRecursion() {
     @setFnTest(this);
 
@@ -159,11 +161,3 @@ fn staticallyInitializedArrayLiteral() {
     assert(y[3] == 4);
 }
 const st_init_arr_lit_x = []u8{1,2,3,4};
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
test/cases/fn.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn params() {
     @setFnTest(this);
 
@@ -98,11 +100,3 @@ fn fn1() -> u32 {5}
 fn fn2() -> u32 {6}
 fn fn3() -> u32 {7}
 fn fn4() -> u32 {8}
-
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/for.zig
@@ -1,3 +1,7 @@
+const std = @import("std");
+const assert = std.debug.assert;
+const str = std.str;
+
 fn continueInForLoop() {
     @setFnTest(this);
 
@@ -20,31 +24,10 @@ fn forLoopWithPointerElemVar() {
     var target: [source.len]u8 = undefined;
     @memcpy(&target[0], &source[0], source.len);
     mangleString(target);
-    assert(memeql(target, "bcdefgh"));
+    assert(str.eql(target, "bcdefgh"));
 }
 fn mangleString(s: []u8) {
     for (s) |*c| {
         *c += 1;
     }
 }
-
-
-// TODO import from std.str
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
-    sliceEql(u8, a, b)
-}
-
-// TODO import from std.str
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
-    if (a.len != b.len) return false;
-    for (a) |item, index| {
-        if (b[index] != item) return false;
-    }
-    return true;
-}
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/generics.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn simpleGenericFn() {
     @setFnTest(this);
 
@@ -138,10 +140,3 @@ fn getByte(ptr: ?&u8) -> u8 {*??ptr}
 fn getFirstByte(inline T: type, mem: []T) -> u8 {
     getByte((&u8)(&mem[0]))
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
test/cases/goto.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn gotoAndLabels() {
     @setFnTest(this);
 
@@ -37,9 +39,3 @@ entry:
     defer it_worked = true;
     if (b) goto exit;
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/if.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn ifStatements() {
     @setFnTest(this);
 
@@ -38,9 +40,3 @@ fn elseIfExpressionF(c: u8) -> u8 {
         u8(2)
     }
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/import.zig
@@ -1,3 +1,4 @@
+const assert = @import("std").debug.assert;
 const a_namespace = @import("cases/import/a_namespace.zig");
 
 fn callFnViaNamespaceLookup() {
@@ -5,9 +6,3 @@ fn callFnViaNamespaceLookup() {
 
     assert(a_namespace.foo() == 1234);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/ir_block_deps.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn foo(id: u64) -> %i32 {
     return switch (id) {
         1 => getErrInt(),
@@ -19,9 +21,3 @@ fn irBlockDeps() {
     assert(%%foo(1) == 0);
     assert(%%foo(2) == 0);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/math.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn exactDivision() {
     @setFnTest(this);
 
@@ -175,10 +177,3 @@ fn binaryNot() {
 fn testBinaryNot(x: u16) {
     assert(~x == 0b0101010101010101);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
test/cases/misc.zig
@@ -1,3 +1,7 @@
+const assert = @import("std").debug.assert;
+const str = @import("std").str;
+const cstr = @import("std").cstr;
+
 // normal comment
 /// this is a documentation comment
 /// doc comment line 2
@@ -137,7 +141,7 @@ fn first4KeysOfHomeRow() -> []const u8 {
 fn ReturnStringFromFunction() {
     @setFnTest(this);
 
-    assert(memeql(first4KeysOfHomeRow(), "aoeu"));
+    assert(str.eql(first4KeysOfHomeRow(), "aoeu"));
 }
 
 const g1 : i32 = 1233 + 1;
@@ -203,31 +207,31 @@ fn emptyFn() {}
 fn hexEscape() {
     @setFnTest(this);
 
-    assert(memeql("\x68\x65\x6c\x6c\x6f", "hello"));
+    assert(str.eql("\x68\x65\x6c\x6c\x6f", "hello"));
 }
 
 fn stringConcatenation() {
     @setFnTest(this);
 
-    assert(memeql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
+    assert(str.eql("OK" ++ " IT " ++ "WORKED", "OK IT WORKED"));
 }
 
 fn arrayMultOperator() {
     @setFnTest(this);
 
-    assert(memeql("ab" ** 5, "ababababab"));
+    assert(str.eql("ab" ** 5, "ababababab"));
 }
 
 fn stringEscapes() {
     @setFnTest(this);
 
-    assert(memeql("\"", "\x22"));
-    assert(memeql("\'", "\x27"));
-    assert(memeql("\n", "\x0a"));
-    assert(memeql("\r", "\x0d"));
-    assert(memeql("\t", "\x09"));
-    assert(memeql("\\", "\x5c"));
-    assert(memeql("\u1234\u0069", "\xe1\x88\xb4\x69"));
+    assert(str.eql("\"", "\x22"));
+    assert(str.eql("\'", "\x27"));
+    assert(str.eql("\n", "\x0a"));
+    assert(str.eql("\r", "\x0d"));
+    assert(str.eql("\t", "\x09"));
+    assert(str.eql("\\", "\x5c"));
+    assert(str.eql("\u1234\u0069", "\xe1\x88\xb4\x69"));
 }
 
 fn multilineString() {
@@ -239,7 +243,7 @@ fn multilineString() {
         \\three
     ;
     const s2 = "one\ntwo)\nthree";
-    assert(memeql(s1, s2));
+    assert(str.eql(s1, s2));
 }
 
 fn multilineCString() {
@@ -251,7 +255,7 @@ fn multilineCString() {
         c\\three
     ;
     const s2 = c"one\ntwo)\nthree";
-    assert(cstrcmp(s1, s2) == 0);
+    assert(cstr.cmp(s1, s2) == 0);
 }
 
 
@@ -337,8 +341,8 @@ fn pointerDereferencing() {
 fn callResultOfIfElseExpression() {
     @setFnTest(this);
 
-    assert(memeql(f2(true), "a"));
-    assert(memeql(f2(false), "b"));
+    assert(str.eql(f2(true), "a"));
+    assert(str.eql(f2(false), "b"));
 }
 fn f2(x: bool) -> []u8 {
     return (if (x) fA else fB)();
@@ -442,7 +446,7 @@ fn cStringConcatenation() {
     const a = c"OK" ++ c" IT " ++ c"WORKED";
     const b = c"OK IT WORKED";
 
-    const len = cstrlen(b);
+    const len = cstr.len(b);
     const len_with_null = len + 1;
     {var i: u32 = 0; while (i < len_with_null; i += 1) {
         assert(a[i] == b[i]);
@@ -486,43 +490,3 @@ const test_pointer_to_void_return_type_x = void{};
 fn testPointerToVoidReturnType2() -> &const void {
     return &test_pointer_to_void_return_type_x;
 }
-
-// TODO import from std.cstr
-pub fn cstrlen(ptr: &const u8) -> usize {
-    var count: usize = 0;
-    while (ptr[count] != 0; count += 1) {}
-    return count;
-}
-
-// TODO import from std.str
-pub fn memeql(a: []const u8, b: []const u8) -> bool {
-    sliceEql(u8, a, b)
-}
-
-// TODO import from std.str
-pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
-    if (a.len != b.len) return false;
-    for (a) |item, index| {
-        if (b[index] != item) return false;
-    }
-    return true;
-}
-
-// TODO import from std.cstr
-pub fn cstrcmp(a: &const u8, b: &const u8) -> i8 {
-    var index: usize = 0;
-    while (a[index] == b[index] && a[index] != 0; index += 1) {}
-    return if (a[index] > b[index]) {
-        1
-    } else if (a[index] < b[index]) {
-        -1
-    } else {
-        i8(0)
-    };
-}
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/null.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn nullableType() {
     @setFnTest(this);
 
@@ -97,10 +99,3 @@ fn foo(x: ?i32) -> ?bool {
     const value = ?return x;
     return value > 1234;
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
-
test/cases/sizeof_and_typeof.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn sizeofAndTypeOf() {
     @setFnTest(this);
 
@@ -6,9 +8,3 @@ fn sizeofAndTypeOf() {
 }
 const x: u16 = 13;
 const z: @typeOf(x) = 19;
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/struct.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 const StructWithNoFields = struct {
     fn add(a: i32, b: i32) -> i32 { a + b }
 };
@@ -206,10 +208,3 @@ fn passSliceOfEmptyStructToFn() {
 fn testPassSliceOfEmptyStructToFn(slice: []EmptyStruct2) -> usize {
     slice.len
 }
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/struct_contains_slice_of_itself.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 const Node = struct {
     payload: i32,
     children: []Node,
@@ -40,9 +42,3 @@ fn structContainsSliceOfItself() {
     assert(root.children[2].children[0].payload == 31);
     assert(root.children[2].children[1].payload == 32);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/switch.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn switchWithNumbers() {
     @setFnTest(this);
 
@@ -127,10 +129,3 @@ fn switchWithMultipleExpressions() {
 fn returnsFive() -> i32 {
     5
 }
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/switch_prong_err_enum.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 var read_count: u64 = 0;
 
 fn readOnce() -> %u64 {
@@ -25,9 +27,3 @@ fn switchProngReturnsErrorEnum() {
     %%doThing(17);
     assert(read_count == 1);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/switch_prong_implicit_cast.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 const FormValue = enum {
     One,
     Two: bool,
@@ -22,9 +24,3 @@ fn switchProngImplicitCast() {
     };
     assert(result);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/this.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 const module = this;
 
 fn Point(inline T: type) -> type {
@@ -49,9 +51,3 @@ fn thisReferToFn() {
 
     assert(factorial(5) == 120);
 }
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}
test/cases/while.zig
@@ -1,3 +1,5 @@
+const assert = @import("std").debug.assert;
+
 fn whileLoop() {
     @setFnTest(this);
 
@@ -72,11 +74,3 @@ fn whileWithContinueExpr() {
     }}
     assert(sum == 40);
 }
-
-
-
-// TODO const assert = @import("std").debug.assert;
-fn assert(ok: bool) {
-    if (!ok)
-        @unreachable();
-}