From 1b5d1877aea6c0db7fbe6f56ffe4412ce2cffff7 Mon Sep 17 00:00:00 2001 From: Andrew Kelley Date: Mon, 26 Dec 2016 01:37:33 -0500 Subject: [PATCH] IR: port more tests --- test/cases/enum_with_members.zig | 31 ------------ test/cases3/enum_with_members.zig | 83 +++++++++++++++++++++++++++++++ test/self_hosted.zig | 1 - test/self_hosted3.zig | 1 + 4 files changed, 84 insertions(+), 32 deletions(-) delete mode 100644 test/cases/enum_with_members.zig create mode 100644 test/cases3/enum_with_members.zig diff --git a/test/cases/enum_with_members.zig b/test/cases/enum_with_members.zig deleted file mode 100644 index 4f26c9f258..0000000000 --- a/test/cases/enum_with_members.zig +++ /dev/null @@ -1,31 +0,0 @@ -const std = @import("std"); -const assert = std.debug.assert; -const io = std.io; -const str = std.str; - -enum ET { - SINT: i32, - UINT: u32, - - pub fn print(a: &ET, buf: []u8) -> %usize { - return switch (*a) { - SINT => |x| { io.bufPrintInt(i32, buf, x) }, - UINT => |x| { io.bufPrintInt(u32, buf, x) }, - } - } -} - -fn enumWithMembers() { - @setFnTest(this, true); - - const a = ET.SINT { -42 }; - const b = ET.UINT { 42 }; - var buf: [20]u8 = undefined; - - assert(%%a.print(buf) == 3); - assert(str.eql(buf[0...3], "-42")); - - assert(%%b.print(buf) == 2); - assert(str.eql(buf[0...2], "42")); -} - diff --git a/test/cases3/enum_with_members.zig b/test/cases3/enum_with_members.zig new file mode 100644 index 0000000000..2c7256ca32 --- /dev/null +++ b/test/cases3/enum_with_members.zig @@ -0,0 +1,83 @@ +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) }, + } + } +}; + +fn enumWithMembers() { + @setFnTest(this); + + const a = ET.SINT { -42 }; + const b = ET.UINT { 42 }; + var buf: [20]u8 = undefined; + + assert(%%a.print(buf) == 3); + assert(memeql(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; +} diff --git a/test/self_hosted.zig b/test/self_hosted.zig index 8ec3dab833..daf44d2a37 100644 --- a/test/self_hosted.zig +++ b/test/self_hosted.zig @@ -2,7 +2,6 @@ const std = @import("std"); const assert = std.debug.assert; const str = std.str; const cstr = std.cstr; -const test_enum_with_members = @import("cases/enum_with_members.zig"); fn explicitCastMaybePointers() { diff --git a/test/self_hosted3.zig b/test/self_hosted3.zig index 9a7331f2f8..46eac63c69 100644 --- a/test/self_hosted3.zig +++ b/test/self_hosted3.zig @@ -6,6 +6,7 @@ const test_cast= @import("cases3/cast.zig"); const test_const_slice_child = @import("cases3/const_slice_child.zig"); const test_defer = @import("cases3/defer.zig"); const test_enum = @import("cases3/enum.zig"); +const test_enum_with_members = @import("cases3/enum_with_members.zig"); const test_error = @import("cases3/error.zig"); const test_eval = @import("cases3/eval.zig"); const test_fn = @import("cases3/fn.zig");