2017-01-05 16:57:48 +08:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
2017-03-17 04:02:35 +08:00
|
|
|
test "params" {
|
2016-12-22 13:20:14 +08:00
|
|
|
assert(testParamsAdd(22, 11) == 33);
|
|
|
|
}
|
2018-01-25 17:10:11 +08:00
|
|
|
fn testParamsAdd(a: i32, b: i32) i32 {
|
2017-12-22 13:50:30 +08:00
|
|
|
return a + b;
|
2016-12-22 13:20:14 +08:00
|
|
|
}
|
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "local variables" {
|
2016-12-22 13:20:14 +08:00
|
|
|
testLocVars(2);
|
|
|
|
}
|
2018-01-25 17:10:11 +08:00
|
|
|
fn testLocVars(b: i32) void {
|
2016-12-22 13:20:14 +08:00
|
|
|
const a: i32 = 1;
|
2017-03-26 16:58:48 +08:00
|
|
|
if (a + b != 3) unreachable;
|
2016-12-22 13:20:14 +08:00
|
|
|
}
|
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "void parameters" {
|
2016-12-22 13:46:17 +08:00
|
|
|
voidFun(1, void{}, 2, {});
|
|
|
|
}
|
2018-01-25 17:10:11 +08:00
|
|
|
fn voidFun(a: i32, b: void, c: i32, d: void) void {
|
2016-12-22 13:46:17 +08:00
|
|
|
const v = b;
|
2017-12-22 13:50:30 +08:00
|
|
|
const vv: void = if (a == 1) v else {};
|
2016-12-22 13:46:17 +08:00
|
|
|
assert(a + c == 3);
|
|
|
|
return vv;
|
|
|
|
}
|
2016-12-22 13:20:14 +08:00
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "mutable local variables" {
|
2018-05-01 08:35:54 +08:00
|
|
|
var zero: i32 = 0;
|
2016-12-22 13:55:21 +08:00
|
|
|
assert(zero == 0);
|
|
|
|
|
|
|
|
var i = i32(0);
|
|
|
|
while (i != 3) {
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
assert(i == 3);
|
|
|
|
}
|
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "separate block scopes" {
|
2016-12-22 13:55:21 +08:00
|
|
|
{
|
2018-05-01 08:35:54 +08:00
|
|
|
const no_conflict: i32 = 5;
|
2016-12-22 13:55:21 +08:00
|
|
|
assert(no_conflict == 5);
|
|
|
|
}
|
|
|
|
|
2017-12-22 13:50:30 +08:00
|
|
|
const c = x: {
|
2016-12-22 13:55:21 +08:00
|
|
|
const no_conflict = i32(10);
|
2017-12-22 13:50:30 +08:00
|
|
|
break :x no_conflict;
|
2016-12-22 13:55:21 +08:00
|
|
|
};
|
|
|
|
assert(c == 10);
|
|
|
|
}
|
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "call function with empty string" {
|
2016-12-22 14:42:30 +08:00
|
|
|
acceptsString("");
|
|
|
|
}
|
|
|
|
|
2018-05-01 08:35:54 +08:00
|
|
|
fn acceptsString(foo: []u8) void {}
|
2016-12-22 14:42:30 +08:00
|
|
|
|
2018-01-25 17:10:11 +08:00
|
|
|
fn @"weird function name"() i32 {
|
2017-03-17 04:02:35 +08:00
|
|
|
return 1234;
|
|
|
|
}
|
|
|
|
test "weird function name" {
|
|
|
|
assert(@"weird function name"() == 1234);
|
2016-12-22 14:42:30 +08:00
|
|
|
}
|
2016-12-22 13:55:21 +08:00
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "implicit cast function unreachable return" {
|
2016-12-22 23:09:53 +08:00
|
|
|
wantsFnWithVoid(fnWithUnreachable);
|
|
|
|
}
|
|
|
|
|
2018-05-31 04:09:11 +08:00
|
|
|
fn wantsFnWithVoid(f: fn () void) void {}
|
2016-12-22 23:09:53 +08:00
|
|
|
|
2018-01-25 17:10:11 +08:00
|
|
|
fn fnWithUnreachable() noreturn {
|
2017-12-22 13:50:30 +08:00
|
|
|
unreachable;
|
2016-12-22 23:09:53 +08:00
|
|
|
}
|
|
|
|
|
2017-09-10 10:53:32 +08:00
|
|
|
test "function pointers" {
|
2018-05-29 08:23:55 +08:00
|
|
|
const fns = []@typeOf(fn1){
|
2018-05-01 08:35:54 +08:00
|
|
|
fn1,
|
|
|
|
fn2,
|
|
|
|
fn3,
|
|
|
|
fn4,
|
|
|
|
};
|
2016-12-26 16:44:59 +08:00
|
|
|
for (fns) |f, i| {
|
|
|
|
assert(f() == u32(i) + 5);
|
|
|
|
}
|
|
|
|
}
|
2018-05-01 08:35:54 +08:00
|
|
|
fn fn1() u32 {
|
|
|
|
return 5;
|
|
|
|
}
|
|
|
|
fn fn2() u32 {
|
|
|
|
return 6;
|
|
|
|
}
|
|
|
|
fn fn3() u32 {
|
|
|
|
return 7;
|
|
|
|
}
|
|
|
|
fn fn4() u32 {
|
|
|
|
return 8;
|
|
|
|
}
|
2017-04-13 12:13:54 +08:00
|
|
|
|
|
|
|
test "inline function call" {
|
2017-05-10 10:54:23 +08:00
|
|
|
assert(@inlineCall(add, 3, 9) == 12);
|
2017-04-13 12:13:54 +08:00
|
|
|
}
|
|
|
|
|
2018-05-01 08:35:54 +08:00
|
|
|
fn add(a: i32, b: i32) i32 {
|
|
|
|
return a + b;
|
|
|
|
}
|
2018-04-12 02:44:32 +08:00
|
|
|
|
|
|
|
test "number literal as an argument" {
|
|
|
|
numberLiteralArg(3);
|
|
|
|
comptime numberLiteralArg(3);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn numberLiteralArg(a: var) void {
|
|
|
|
assert(a == 3);
|
|
|
|
}
|
2018-04-13 04:26:23 +08:00
|
|
|
|
|
|
|
test "assign inline fn to const variable" {
|
|
|
|
const a = inlineFn;
|
|
|
|
a();
|
|
|
|
}
|
|
|
|
|
2018-05-01 08:35:54 +08:00
|
|
|
inline fn inlineFn() void {}
|