authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-06 20:14:06+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-04-06 20:14:06+02:00
log0a936c1d766e3f0b81d86bffee95d15b50fc6d58
tree843bf0c3f7b62bc2a96336b582375f5979217257
parentdc54e50db27f76721c33cd437b62ceb598a00447

Add some tests for the runtime safety checks


1 files changed, 69 insertions(+), 0 deletions(-)

test/runtime_safety.zig+69
......@@ -1,6 +1,75 @@
11const tests = @import("tests.zig");
22
33pub fn addCases(cases: *tests.CompareOutputContext) void {
4 {
5 const check_panic_msg =
6 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {
7 \\ if (std.mem.eql(u8, message, "index out of bounds")) {
8 \\ std.process.exit(126); // good
9 \\ }
10 \\ std.process.exit(0); // test failed
11 \\}
12 ;
13
14 cases.addRuntimeSafety("slicing operator with sentinel",
15 \\const std = @import("std");
16 ++ check_panic_msg ++
17 \\pub fn main() void {
18 \\ var buf = [4]u8{'a','b','c',0};
19 \\ const slice = buf[0..4 :0];
20 \\}
21 );
22 cases.addRuntimeSafety("slicing operator with sentinel",
23 \\const std = @import("std");
24 ++ check_panic_msg ++
25 \\pub fn main() void {
26 \\ var buf = [4]u8{'a','b','c',0};
27 \\ const slice = buf[0..:0];
28 \\}
29 );
30 cases.addRuntimeSafety("slicing operator with sentinel",
31 \\const std = @import("std");
32 ++ check_panic_msg ++
33 \\pub fn main() void {
34 \\ var buf_zero = [0]u8{};
35 \\ const slice = buf_zero[0..0 :0];
36 \\}
37 );
38 cases.addRuntimeSafety("slicing operator with sentinel",
39 \\const std = @import("std");
40 ++ check_panic_msg ++
41 \\pub fn main() void {
42 \\ var buf_zero = [0]u8{};
43 \\ const slice = buf_zero[0..:0];
44 \\}
45 );
46 cases.addRuntimeSafety("slicing operator with sentinel",
47 \\const std = @import("std");
48 ++ check_panic_msg ++
49 \\pub fn main() void {
50 \\ var buf_sentinel = [2:0]u8{'a','b'};
51 \\ @ptrCast(*[3]u8, &buf_sentinel)[2] = 0;
52 \\ const slice = buf_sentinel[0..3 :0];
53 \\}
54 );
55 cases.addRuntimeSafety("slicing operator with sentinel",
56 \\const std = @import("std");
57 ++ check_panic_msg ++
58 \\pub fn main() void {
59 \\ var buf_slice: []u8 = &[3]u8{ 'a', 'b', 0 };
60 \\ const slice = buf_slice[0..3 :0];
61 \\}
62 );
63 cases.addRuntimeSafety("slicing operator with sentinel",
64 \\const std = @import("std");
65 ++ check_panic_msg ++
66 \\pub fn main() void {
67 \\ var buf_slice: []u8 = &[3]u8{ 'a', 'b', 0 };
68 \\ const slice = buf_slice[0.. :0];
69 \\}
70 );
71 }
72
473 cases.addRuntimeSafety("shift left by huge amount",
574 \\const std = @import("std");
675 \\pub fn panic(message: []const u8, stack_trace: ?*@import("builtin").StackTrace) noreturn {