2017-01-23 21:40:17 +00:00
|
|
|
const assert = @import("std").debug.assert;
|
|
|
|
|
|
|
|
fn add(args: ...) -> i32 {
|
|
|
|
var sum = i32(0);
|
2017-05-03 23:12:07 +01:00
|
|
|
{comptime var i: usize = 0; inline while (i < args.len) : (i += 1) {
|
2017-01-23 21:40:17 +00:00
|
|
|
sum += args[i];
|
|
|
|
}}
|
|
|
|
return sum;
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "testAddArbitraryArgs" {
|
2017-01-23 21:40:17 +00:00
|
|
|
assert(add(i32(1), i32(2), i32(3), i32(4)) == 10);
|
|
|
|
assert(add(i32(1234)) == 1234);
|
2017-01-24 04:30:20 +00:00
|
|
|
assert(add() == 0);
|
2017-01-23 21:40:17 +00:00
|
|
|
}
|
2017-01-31 20:50:38 +00:00
|
|
|
|
|
|
|
fn readFirstVarArg(args: ...) {
|
|
|
|
const value = args[0];
|
|
|
|
}
|
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "sendVoidArgToVarArgs" {
|
2017-01-31 20:50:38 +00:00
|
|
|
readFirstVarArg({});
|
|
|
|
}
|
2017-03-09 21:13:38 +00:00
|
|
|
|
2017-03-16 20:02:35 +00:00
|
|
|
test "testPassArgsDirectly" {
|
2017-03-09 21:13:38 +00:00
|
|
|
assert(addSomeStuff(i32(1), i32(2), i32(3), i32(4)) == 10);
|
|
|
|
assert(addSomeStuff(i32(1234)) == 1234);
|
|
|
|
assert(addSomeStuff() == 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn addSomeStuff(args: ...) -> i32 {
|
|
|
|
return add(args);
|
|
|
|
}
|
2017-04-07 02:00:49 +01:00
|
|
|
|
|
|
|
test "runtime parameter before var args" {
|
|
|
|
assert(extraFn(10) == 0);
|
|
|
|
assert(extraFn(10, false) == 1);
|
|
|
|
assert(extraFn(10, false, true) == 2);
|
|
|
|
|
2017-04-07 05:33:19 +01:00
|
|
|
// TODO issue #313
|
2017-04-07 02:00:49 +01:00
|
|
|
//comptime {
|
|
|
|
// assert(extraFn(10) == 0);
|
|
|
|
// assert(extraFn(10, false) == 1);
|
|
|
|
// assert(extraFn(10, false, true) == 2);
|
|
|
|
//}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn extraFn(extra: u32, args: ...) -> usize {
|
|
|
|
if (args.len >= 1) {
|
|
|
|
assert(args[0] == false);
|
|
|
|
}
|
|
|
|
if (args.len >= 2) {
|
|
|
|
assert(args[1] == true);
|
|
|
|
}
|
|
|
|
return args.len;
|
|
|
|
}
|