mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 08:33:06 +00:00
Add some test cases for the previous commits
This commit is contained in:
parent
97f36d93f4
commit
b45d2968e5
@ -1,6 +1,85 @@
|
||||
const tests = @import("tests.zig");
|
||||
|
||||
pub fn addCases(cases: *tests.CompareOutputContext) void {
|
||||
{
|
||||
const check_panic_msg =
|
||||
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
|
||||
\\ if (std.mem.eql(u8, message, "reached unreachable code")) {
|
||||
\\ std.process.exit(126); // good
|
||||
\\ }
|
||||
\\ std.process.exit(0); // test failed
|
||||
\\}
|
||||
;
|
||||
|
||||
cases.addRuntimeSafety("switch on corrupted enum value",
|
||||
\\const std = @import("std");
|
||||
++ check_panic_msg ++
|
||||
\\const E = enum(u32) {
|
||||
\\ X = 1,
|
||||
\\};
|
||||
\\pub fn main() void {
|
||||
\\ var e: E = undefined;
|
||||
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
|
||||
\\ switch (e) {
|
||||
\\ .X => @breakpoint(),
|
||||
\\ }
|
||||
\\}
|
||||
);
|
||||
|
||||
cases.addRuntimeSafety("switch on corrupted union value",
|
||||
\\const std = @import("std");
|
||||
++ check_panic_msg ++
|
||||
\\const U = union(enum(u32)) {
|
||||
\\ X: u8,
|
||||
\\};
|
||||
\\pub fn main() void {
|
||||
\\ var u: U = undefined;
|
||||
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
|
||||
\\ switch (u) {
|
||||
\\ .X => @breakpoint(),
|
||||
\\ }
|
||||
\\}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const check_panic_msg =
|
||||
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
|
||||
\\ if (std.mem.eql(u8, message, "invalid enum value")) {
|
||||
\\ std.process.exit(126); // good
|
||||
\\ }
|
||||
\\ std.process.exit(0); // test failed
|
||||
\\}
|
||||
;
|
||||
|
||||
cases.addRuntimeSafety("@tagName on corrupted enum value",
|
||||
\\const std = @import("std");
|
||||
++ check_panic_msg ++
|
||||
\\const E = enum(u32) {
|
||||
\\ X = 1,
|
||||
\\};
|
||||
\\pub fn main() void {
|
||||
\\ var e: E = undefined;
|
||||
\\ @memset(@ptrCast([*]u8, &e), 0x55, @sizeOf(E));
|
||||
\\ var n = @tagName(e);
|
||||
\\}
|
||||
);
|
||||
|
||||
cases.addRuntimeSafety("@tagName on corrupted union value",
|
||||
\\const std = @import("std");
|
||||
++ check_panic_msg ++
|
||||
\\const U = union(enum(u32)) {
|
||||
\\ X: u8,
|
||||
\\};
|
||||
\\pub fn main() void {
|
||||
\\ var u: U = undefined;
|
||||
\\ @memset(@ptrCast([*]u8, &u), 0x55, @sizeOf(U));
|
||||
\\ var t: @TagType(U) = u;
|
||||
\\ var n = @tagName(t);
|
||||
\\}
|
||||
);
|
||||
}
|
||||
|
||||
{
|
||||
const check_panic_msg =
|
||||
\\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
|
||||
|
Loading…
Reference in New Issue
Block a user