zig/test/cases/fn.zig

179 lines
3.1 KiB
Zig
Raw Normal View History

2017-01-05 00:57:48 -08:00
const assert = @import("std").debug.assert;
test "params" {
2016-12-21 21:20:14 -08:00
assert(testParamsAdd(22, 11) == 33);
}
fn testParamsAdd(a: i32, b: i32) i32 {
return a + b;
2016-12-21 21:20:14 -08:00
}
2017-09-09 19:53:32 -07:00
test "local variables" {
2016-12-21 21:20:14 -08:00
testLocVars(2);
}
fn testLocVars(b: i32) void {
2016-12-21 21:20:14 -08:00
const a: i32 = 1;
if (a + b != 3) unreachable;
2016-12-21 21:20:14 -08:00
}
2017-09-09 19:53:32 -07:00
test "void parameters" {
2016-12-21 21:46:17 -08:00
voidFun(1, void{}, 2, {});
}
fn voidFun(a: i32, b: void, c: i32, d: void) void {
2016-12-21 21:46:17 -08:00
const v = b;
const vv: void = if (a == 1) v else {};
2016-12-21 21:46:17 -08:00
assert(a + c == 3);
return vv;
}
2016-12-21 21:20:14 -08:00
2017-09-09 19:53:32 -07:00
test "mutable local variables" {
var zero: i32 = 0;
2016-12-21 21:55:21 -08:00
assert(zero == 0);
var i = i32(0);
while (i != 3) {
i += 1;
}
assert(i == 3);
}
2017-09-09 19:53:32 -07:00
test "separate block scopes" {
2016-12-21 21:55:21 -08:00
{
const no_conflict: i32 = 5;
2016-12-21 21:55:21 -08:00
assert(no_conflict == 5);
}
const c = x: {
2016-12-21 21:55:21 -08:00
const no_conflict = i32(10);
break :x no_conflict;
2016-12-21 21:55:21 -08:00
};
assert(c == 10);
}
2017-09-09 19:53:32 -07:00
test "call function with empty string" {
2016-12-21 22:42:30 -08:00
acceptsString("");
}
fn acceptsString(foo: []u8) void {}
2016-12-21 22:42:30 -08:00
fn @"weird function name"() i32 {
return 1234;
}
test "weird function name" {
assert(@"weird function name"() == 1234);
2016-12-21 22:42:30 -08:00
}
2016-12-21 21:55:21 -08:00
2017-09-09 19:53:32 -07:00
test "implicit cast function unreachable return" {
2016-12-22 07:09:53 -08:00
wantsFnWithVoid(fnWithUnreachable);
}
2018-05-30 13:09:11 -07:00
fn wantsFnWithVoid(f: fn () void) void {}
2016-12-22 07:09:53 -08:00
fn fnWithUnreachable() noreturn {
unreachable;
2016-12-22 07:09:53 -08:00
}
2017-09-09 19:53:32 -07:00
test "function pointers" {
2018-05-28 17:23:55 -07:00
const fns = []@typeOf(fn1){
fn1,
fn2,
fn3,
fn4,
};
2016-12-26 00:44:59 -08:00
for (fns) |f, i| {
assert(f() == @intCast(u32, i) + 5);
2016-12-26 00:44:59 -08:00
}
}
fn fn1() u32 {
return 5;
}
fn fn2() u32 {
return 6;
}
fn fn3() u32 {
return 7;
}
fn fn4() u32 {
return 8;
}
test "inline function call" {
assert(@inlineCall(add, 3, 9) == 12);
}
fn add(a: i32, b: i32) i32 {
return a + b;
}
test "number literal as an argument" {
numberLiteralArg(3);
comptime numberLiteralArg(3);
}
fn numberLiteralArg(a: var) void {
assert(a == 3);
}
test "assign inline fn to const variable" {
const a = inlineFn;
a();
}
inline fn inlineFn() void {}
test "pass by non-copying value" {
assert(addPointCoords(Point{ .x = 1, .y = 2 }) == 3);
}
const Point = struct {
x: i32,
y: i32,
};
fn addPointCoords(pt: Point) i32 {
return pt.x + pt.y;
}
test "pass by non-copying value through var arg" {
assert(addPointCoordsVar(Point{ .x = 1, .y = 2 }) == 3);
}
fn addPointCoordsVar(pt: var) i32 {
comptime assert(@typeOf(pt) == Point);
return pt.x + pt.y;
}
test "pass by non-copying value as method" {
var pt = Point2{ .x = 1, .y = 2 };
assert(pt.addPointCoords() == 3);
}
const Point2 = struct {
x: i32,
y: i32,
fn addPointCoords(self: Point2) i32 {
return self.x + self.y;
}
};
test "pass by non-copying value as method, which is generic" {
var pt = Point3{ .x = 1, .y = 2 };
assert(pt.addPointCoords(i32) == 3);
}
const Point3 = struct {
x: i32,
y: i32,
fn addPointCoords(self: Point3, comptime T: type) i32 {
return self.x + self.y;
}
};
test "pass by non-copying value as method, at comptime" {
comptime {
var pt = Point2{ .x = 1, .y = 2 };
assert(pt.addPointCoords() == 3);
}
}