1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
const std = @import("std");
const TestContext = @import("../../src/test.zig").TestContext;
const build_options = @import("build_options");
// These tests should work with all platforms, but we're using linux_x64 for
// now for consistency. Will be expanded eventually.
const linux_x64 = std.zig.CrossTarget{
.cpu_arch = .x86_64,
.os_tag = .linux,
};
pub fn addCases(ctx: *TestContext) !void {
{
var case = ctx.exeUsingLlvmBackend("simple addition and subtraction", linux_x64);
case.addCompareOutput(
\\fn add(a: i32, b: i32) i32 {
\\ return a + b;
\\}
\\
\\export fn main() c_int {
\\ var a: i32 = -5;
\\ const x = add(a, 7);
\\ var y = add(2, 0);
\\ y -= x;
\\ return y;
\\}
, "");
}
{
var case = ctx.exeUsingLlvmBackend("llvm hello world", linux_x64);
case.addCompareOutput(
\\extern fn puts(s: [*:0]const u8) c_int;
\\
\\export fn main() c_int {
\\ _ = puts("hello world!");
\\ return 0;
\\}
, "hello world!" ++ std.cstr.line_sep);
}
{
var case = ctx.exeUsingLlvmBackend("simple if statement", linux_x64);
case.addCompareOutput(
\\fn add(a: i32, b: i32) i32 {
\\ return a + b;
\\}
\\
\\fn assert(ok: bool) void {
\\ if (!ok) unreachable;
\\}
\\
\\export fn main() c_int {
\\ assert(add(1,2) == 3);
\\ return 0;
\\}
, "");
}
{
var case = ctx.exeUsingLlvmBackend("blocks", linux_x64);
case.addCompareOutput(
\\fn assert(ok: bool) void {
\\ if (!ok) unreachable;
\\}
\\
\\fn foo(ok: bool) i32 {
\\ const val: i32 = blk: {
\\ var x: i32 = 1;
\\ if (!ok) break :blk x + 9;
\\ break :blk x + 19;
\\ };
\\ return val + 10;
\\}
\\
\\export fn main() c_int {
\\ assert(foo(false) == 20);
\\ assert(foo(true) == 30);
\\ return 0;
\\}
, "");
}
{
var case = ctx.exeUsingLlvmBackend("nested blocks", linux_x64);
case.addCompareOutput(
\\fn assert(ok: bool) void {
\\ if (!ok) unreachable;
\\}
\\
\\fn foo(ok: bool) i32 {
\\ var val: i32 = blk: {
\\ const val2: i32 = another: {
\\ if (!ok) break :blk 10;
\\ break :another 10;
\\ };
\\ break :blk val2 + 10;
\\ };
\\ return val;
\\}
\\
\\export fn main() c_int {
\\ assert(foo(false) == 10);
\\ assert(foo(true) == 20);
\\ return 0;
\\}
, "");
}
{
var case = ctx.exeUsingLlvmBackend("while loops", linux_x64);
case.addCompareOutput(
\\fn assert(ok: bool) void {
\\ if (!ok) unreachable;
\\}
\\
\\export fn main() c_int {
\\ var sum: u32 = 0;
\\ var i: u32 = 0;
\\ while (i < 5) : (i += 1) {
\\ sum += i;
\\ }
\\ assert(sum == 10);
\\ assert(i == 5);
\\ return 0;
\\}
, "");
}
}
|