2022-03-13 02:55:48 +00:00
|
|
|
const builtin = @import("builtin");
|
2021-12-29 03:17:34 +00:00
|
|
|
const std = @import("std");
|
|
|
|
const expect = std.testing.expect;
|
|
|
|
const mem = std.mem;
|
|
|
|
const fmt = 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.*) {
|
2021-04-10 12:09:38 +01:00
|
|
|
ET.SINT => |x| fmt.formatIntBuf(buf, x, 10, .lower, fmt.FormatOptions{}),
|
|
|
|
ET.UINT => |x| fmt.formatIntBuf(buf, x, 10, .lower, 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" {
|
2022-03-18 22:02:52 +00:00
|
|
|
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
|
|
|
|
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
|
2022-03-13 02:55:48 +00:00
|
|
|
|
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
|
|
|
}
|