2019-02-08 23:18:47 +00:00
|
|
|
const expect = @import("std").testing.expect;
|
2017-02-07 22:19:51 +00:00
|
|
|
const mem = @import("std").mem;
|
2017-03-10 00:12:15 +00:00
|
|
|
const fmt = @import("std").fmt;
|
2017-01-05 08:57:48 +00:00
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
const ET = union(enum) {
|
2016-12-26 06:37:33 +00:00
|
|
|
SINT: i32,
|
|
|
|
UINT: u32,
|
|
|
|
|
2018-11-13 13:08:37 +00:00
|
|
|
pub fn print(a: *const ET, buf: []u8) anyerror!usize {
|
2018-05-01 01:35:54 +01:00
|
|
|
return switch (a.*) {
|
2019-08-18 00:17:16 +01:00
|
|
|
ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
|
|
|
|
ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, false, fmt.FormatOptions{}),
|
2017-12-22 05:50:30 +00:00
|
|
|
};
|
2016-12-26 06:37:33 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-05-24 02:38:31 +01:00
|
|
|
test "enum with members" {
|
2018-11-13 13:08:37 +00:00
|
|
|
const a = ET{ .SINT = -42 };
|
|
|
|
const b = ET{ .UINT = 42 };
|
2016-12-26 06:37:33 +00:00
|
|
|
var buf: [20]u8 = undefined;
|
|
|
|
|
2021-05-04 19:23:22 +01:00
|
|
|
try expect((a.print(buf[0..]) catch unreachable) == 3);
|
|
|
|
try expect(mem.eql(u8, buf[0..3], "-42"));
|
2016-12-26 06:37:33 +00:00
|
|
|
|
2021-05-04 19:23:22 +01:00
|
|
|
try expect((b.print(buf[0..]) catch unreachable) == 2);
|
|
|
|
try expect(mem.eql(u8, buf[0..2], "42"));
|
2016-12-26 06:37:33 +00:00
|
|
|
}
|