authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 01:37:33-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2016-12-26 01:37:33-05:00
log1b5d1877aea6c0db7fbe6f56ffe4412ce2cffff7
tree759b24b8c172f29676b3917fca1536c20f08812b
parent2b5c7b1b8ed4d836d34b87c5fc847acd30566300

IR: port more tests


4 files changed, 84 insertions(+), 32 deletions(-)

test/cases/enum_with_members.zig deleted-31
...@@ -1,31 +0,0 @@
1const std = @import("std");
2const assert = std.debug.assert;
3const io = std.io;
4const str = std.str;
5
6enum ET {
7 SINT: i32,
8 UINT: u32,
9
10 pub fn print(a: &ET, buf: []u8) -> %usize {
11 return switch (*a) {
12 SINT => |x| { io.bufPrintInt(i32, buf, x) },
13 UINT => |x| { io.bufPrintInt(u32, buf, x) },
14 }
15 }
16}
17
18fn enumWithMembers() {
19 @setFnTest(this, true);
20
21 const a = ET.SINT { -42 };
22 const b = ET.UINT { 42 };
23 var buf: [20]u8 = undefined;
24
25 assert(%%a.print(buf) == 3);
26 assert(str.eql(buf[0...3], "-42"));
27
28 assert(%%b.print(buf) == 2);
29 assert(str.eql(buf[0...2], "42"));
30}
31
test/cases3/enum_with_members.zig created+83
...@@ -0,0 +1,83 @@
1const ET = enum {
2 SINT: i32,
3 UINT: u32,
4
5 pub fn print(a: &ET, buf: []u8) -> %usize {
6 return switch (*a) {
7 ET.SINT => |x| { bufPrintInt(i32, buf, x) },
8 ET.UINT => |x| { bufPrintInt(u32, buf, x) },
9 }
10 }
11};
12
13fn enumWithMembers() {
14 @setFnTest(this);
15
16 const a = ET.SINT { -42 };
17 const b = ET.UINT { 42 };
18 var buf: [20]u8 = undefined;
19
20 assert(%%a.print(buf) == 3);
21 assert(memeql(buf[0...3], "-42"));
22
23 assert(%%b.print(buf) == 2);
24 assert(memeql(buf[0...2], "42"));
25}
26
27// TODO all the below should be imported from std
28
29const max_u64_base10_digits = 20;
30pub fn bufPrintInt(inline T: type, out_buf: []u8, x: T) -> usize {
31 if (T.is_signed) bufPrintSigned(T, out_buf, x) else bufPrintUnsigned(T, out_buf, x)
32}
33
34fn bufPrintSigned(inline T: type, out_buf: []u8, x: T) -> usize {
35 const uint = @intType(false, T.bit_count);
36 if (x < 0) {
37 out_buf[0] = '-';
38 return 1 + bufPrintUnsigned(uint, out_buf[1...], uint(-(x + 1)) + 1);
39 } else {
40 return bufPrintUnsigned(uint, out_buf, uint(x));
41 }
42}
43
44fn bufPrintUnsigned(inline T: type, out_buf: []u8, x: T) -> usize {
45 var buf: [max_u64_base10_digits]u8 = undefined;
46 var a = x;
47 var index: usize = buf.len;
48
49 while (true) {
50 const digit = a % 10;
51 index -= 1;
52 buf[index] = '0' + u8(digit);
53 a /= 10;
54 if (a == 0)
55 break;
56 }
57
58 const len = buf.len - index;
59
60 @memcpy(&out_buf[0], &buf[index], len);
61
62 return len;
63}
64
65// TODO const assert = @import("std").debug.assert;
66fn assert(ok: bool) {
67 if (!ok)
68 @unreachable();
69}
70
71// TODO import from std.str
72pub fn memeql(a: []const u8, b: []const u8) -> bool {
73 sliceEql(u8, a, b)
74}
75
76// TODO import from std.str
77pub fn sliceEql(inline T: type, a: []const T, b: []const T) -> bool {
78 if (a.len != b.len) return false;
79 for (a) |item, index| {
80 if (b[index] != item) return false;
81 }
82 return true;
83}
test/self_hosted.zig-1
...@@ -2,7 +2,6 @@ const std = @import("std");...@@ -2,7 +2,6 @@ const std = @import("std");
2const assert = std.debug.assert;2const assert = std.debug.assert;
3const str = std.str;3const str = std.str;
4const cstr = std.cstr;4const cstr = std.cstr;
5const test_enum_with_members = @import("cases/enum_with_members.zig");
65
76
8fn explicitCastMaybePointers() {7fn explicitCastMaybePointers() {
test/self_hosted3.zig+1
...@@ -6,6 +6,7 @@ const test_cast= @import("cases3/cast.zig");...@@ -6,6 +6,7 @@ const test_cast= @import("cases3/cast.zig");
6const test_const_slice_child = @import("cases3/const_slice_child.zig");6const test_const_slice_child = @import("cases3/const_slice_child.zig");
7const test_defer = @import("cases3/defer.zig");7const test_defer = @import("cases3/defer.zig");
8const test_enum = @import("cases3/enum.zig");8const test_enum = @import("cases3/enum.zig");
9const test_enum_with_members = @import("cases3/enum_with_members.zig");
9const test_error = @import("cases3/error.zig");10const test_error = @import("cases3/error.zig");
10const test_eval = @import("cases3/eval.zig");11const test_eval = @import("cases3/eval.zig");
11const test_fn = @import("cases3/fn.zig");12const test_fn = @import("cases3/fn.zig");