mirror of
https://codeberg.org/ziglang/zig.git
synced 2026-03-08 02:44:43 +01:00
40 lines
901 B
Zig
40 lines
901 B
Zig
const expectEqual = @import("std").testing.expectEqual;
|
|
|
|
const CmdFn = struct {
|
|
name: []const u8,
|
|
func: fn (i32) i32,
|
|
};
|
|
|
|
const cmd_fns = [_]CmdFn{
|
|
CmdFn{ .name = "one", .func = one },
|
|
CmdFn{ .name = "two", .func = two },
|
|
CmdFn{ .name = "three", .func = three },
|
|
};
|
|
fn one(value: i32) i32 {
|
|
return value + 1;
|
|
}
|
|
fn two(value: i32) i32 {
|
|
return value + 2;
|
|
}
|
|
fn three(value: i32) i32 {
|
|
return value + 3;
|
|
}
|
|
|
|
fn performFn(comptime prefix_char: u8, start_value: i32) i32 {
|
|
var result: i32 = start_value;
|
|
comptime var i = 0;
|
|
inline while (i < cmd_fns.len) : (i += 1) {
|
|
if (cmd_fns[i].name[0] == prefix_char) {
|
|
result = cmd_fns[i].func(result);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
test "perform fn" {
|
|
try expectEqual(6, performFn('t', 1));
|
|
try expectEqual(1, performFn('o', 0));
|
|
try expectEqual(99, performFn('w', 99));
|
|
}
|
|
|
|
// test
|