master
1const std = @import("std");
2const builtin = @import("builtin");
3const expect = std.testing.expect;
4const expectEqual = std.testing.expectEqual;
5const mem = std.mem;
6
7fn initStaticArray() [10]i32 {
8 var array: [10]i32 = undefined;
9 array[0] = 1;
10 array[4] = 2;
11 array[7] = 3;
12 array[9] = 4;
13 return array;
14}
15const static_array = initStaticArray();
16test "init static array to undefined" {
17 // This test causes `initStaticArray()` to be codegen'd, and the
18 // C backend does not yet support returning arrays, so it fails
19 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
20 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
21
22 try expect(static_array[0] == 1);
23 try expect(static_array[4] == 2);
24 try expect(static_array[7] == 3);
25 try expect(static_array[9] == 4);
26
27 comptime {
28 try expect(static_array[0] == 1);
29 try expect(static_array[4] == 2);
30 try expect(static_array[7] == 3);
31 try expect(static_array[9] == 4);
32 }
33}
34
35const Foo = struct {
36 x: i32,
37
38 fn setFooXMethod(foo: *Foo) void {
39 foo.x = 3;
40 }
41};
42
43fn setFooX(foo: *Foo) void {
44 foo.x = 2;
45}
46
47test "assign undefined to struct" {
48 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
49 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
50
51 comptime {
52 var foo: Foo = undefined;
53 setFooX(&foo);
54 try expect(foo.x == 2);
55 }
56 {
57 var foo: Foo = undefined;
58 setFooX(&foo);
59 try expect(foo.x == 2);
60 }
61}
62
63test "assign undefined to struct with method" {
64 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
65 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
66
67 comptime {
68 var foo: Foo = undefined;
69 foo.setFooXMethod();
70 try expect(foo.x == 3);
71 }
72 {
73 var foo: Foo = undefined;
74 foo.setFooXMethod();
75 try expect(foo.x == 3);
76 }
77}
78
79test "type name of undefined" {
80 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
81 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
82
83 const x = undefined;
84 try expect(mem.eql(u8, @typeName(@TypeOf(x)), "@TypeOf(undefined)"));
85}
86
87var buf: []u8 = undefined;
88
89test "reslice of undefined global var slice" {
90 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
91 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
92
93 var stack_buf: [100]u8 = [_]u8{0} ** 100;
94 buf = &stack_buf;
95 const x = buf[0..1];
96 try @import("std").testing.expect(x.len == 1 and x[0] == 0);
97}
98
99test "returned undef is 0xaa bytes when runtime safety is enabled" {
100 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
101 if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest;
102 if (builtin.zig_backend == .stage2_riscv64) return error.SkipZigTest;
103
104 const Rect = struct {
105 x: f32,
106 fn getUndefStruct() @This() {
107 @setRuntimeSafety(true);
108 return undefined;
109 }
110 fn getUndefInt() u32 {
111 @setRuntimeSafety(true);
112 return undefined;
113 }
114 };
115 try std.testing.expect(@as(u32, @bitCast(Rect.getUndefStruct().x)) == 0xAAAAAAAA);
116 try std.testing.expect(Rect.getUndefInt() == 0xAAAAAAAA);
117}