| author | |
| committer | |
| log | 1b5d1877aea6c0db7fbe6f56ffe4412ce2cffff7 |
| tree | 759b24b8c172f29676b3917fca1536c20f08812b |
| parent | 2b5c7b1b8ed4d836d34b87c5fc847acd30566300 |
4 files changed, 84 insertions(+), 32 deletions(-)
test/cases/enum_with_members.zig deleted-31| ... | ... | @@ -1,31 +0,0 @@ |
| 1 | const std = @import("std"); | |
| 2 | const assert = std.debug.assert; | |
| 3 | const io = std.io; | |
| 4 | const str = std.str; | |
| 5 | ||
| 6 | enum 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 | ||
| 18 | fn 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 @@ |
| 1 | const 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 | ||
| 13 | fn 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 | ||
| 29 | const max_u64_base10_digits = 20; | |
| 30 | pub 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 | ||
| 34 | fn 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 | ||
| 44 | fn 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; | |
| 66 | fn assert(ok: bool) { | |
| 67 | if (!ok) | |
| 68 | @unreachable(); | |
| 69 | } | |
| 70 | ||
| 71 | // TODO import from std.str | |
| 72 | pub fn memeql(a: []const u8, b: []const u8) -> bool { | |
| 73 | sliceEql(u8, a, b) | |
| 74 | } | |
| 75 | ||
| 76 | // TODO import from std.str | |
| 77 | pub 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 | 2 | const assert = std.debug.assert; |
| 3 | 3 | const str = std.str; |
| 4 | 4 | const cstr = std.cstr; |
| 5 | const test_enum_with_members = @import("cases/enum_with_members.zig"); | |
| 6 | 5 | |
| 7 | 6 | |
| 8 | 7 | fn explicitCastMaybePointers() { |
test/self_hosted3.zig+1| ... | ... | @@ -6,6 +6,7 @@ const test_cast= @import("cases3/cast.zig"); |
| 6 | 6 | const test_const_slice_child = @import("cases3/const_slice_child.zig"); |
| 7 | 7 | const test_defer = @import("cases3/defer.zig"); |
| 8 | 8 | const test_enum = @import("cases3/enum.zig"); |
| 9 | const test_enum_with_members = @import("cases3/enum_with_members.zig"); | |
| 9 | 10 | const test_error = @import("cases3/error.zig"); |
| 10 | 11 | const test_eval = @import("cases3/eval.zig"); |
| 11 | 12 | const test_fn = @import("cases3/fn.zig"); |