aboutsummaryrefslogtreecommitdiff
path: root/examples/multi-step/src
diff options
context:
space:
mode:
authorDavid Czihak <git@dcz.at>2026-05-09 13:01:50 +0200
committerDavid Czihak <git@dcz.at>2026-05-09 13:01:50 +0200
commit4b6f66fd512c254b5a82220dc77411fe391dd258 (patch)
tree7d77d7966e9ad2e296986ea8cfeb607088028195 /examples/multi-step/src
parent64e9c56fc665972fdde5234c4fb2f2a882e237dc (diff)
Chore: Rework examples for thorough extension testing
Diffstat (limited to 'examples/multi-step/src')
-rw-r--r--examples/multi-step/src/lib.zig67
-rw-r--r--examples/multi-step/src/main.zig35
2 files changed, 102 insertions, 0 deletions
diff --git a/examples/multi-step/src/lib.zig b/examples/multi-step/src/lib.zig
new file mode 100644
index 0000000..ea244c1
--- /dev/null
+++ b/examples/multi-step/src/lib.zig
@@ -0,0 +1,67 @@
+const std = @import("std");
+
+/// Returns the sum of a slice of i32 values.
+pub fn sum(values: []const i32) i64 {
+ var total: i64 = 0;
+ for (values) |v| total += v;
+ return total;
+}
+
+/// Returns the product of a slice of i32 values.
+/// Returns 1 for an empty slice (identity element).
+pub fn product(values: []const i32) i64 {
+ var result: i64 = 1;
+ for (values) |v| result *= v;
+ return result;
+}
+
+/// Returns the minimum value, or null for an empty slice.
+pub fn min(values: []const i32) ?i32 {
+ if (values.len == 0) return null;
+ var m = values[0];
+ for (values[1..]) |v| {
+ if (v < m) m = v;
+ }
+ return m;
+}
+
+/// Returns the maximum value, or null for an empty slice.
+pub fn max(values: []const i32) ?i32 {
+ if (values.len == 0) return null;
+ var m = values[0];
+ for (values[1..]) |v| {
+ if (v > m) m = v;
+ }
+ return m;
+}
+
+test "sum empty" {
+ try std.testing.expectEqual(@as(i64, 0), sum(&.{}));
+}
+
+test "sum positive values" {
+ try std.testing.expectEqual(@as(i64, 15), sum(&.{ 1, 2, 3, 4, 5 }));
+}
+
+test "sum with negatives" {
+ try std.testing.expectEqual(@as(i64, 0), sum(&.{ -3, -2, -1, 0, 1, 2, 3 }));
+}
+
+test "product empty" {
+ try std.testing.expectEqual(@as(i64, 1), product(&.{}));
+}
+
+test "product small" {
+ try std.testing.expectEqual(@as(i64, 120), product(&.{ 1, 2, 3, 4, 5 }));
+}
+
+test "min and max" {
+ const data = [_]i32{ 3, 1, 4, 1, 5, 9, 2, 6 };
+ try std.testing.expectEqual(@as(?i32, 1), min(&data));
+ try std.testing.expectEqual(@as(?i32, 9), max(&data));
+}
+
+test "min max empty" {
+ try std.testing.expectEqual(@as(?i32, null), min(&.{}));
+ try std.testing.expectEqual(@as(?i32, null), max(&.{}));
+}
diff --git a/examples/multi-step/src/main.zig b/examples/multi-step/src/main.zig
new file mode 100644
index 0000000..a984b18
--- /dev/null
+++ b/examples/multi-step/src/main.zig
@@ -0,0 +1,35 @@
+const std = @import("std");
+const lib = @import("multi_step");
+
+pub fn main(init: std.process.Init) !void {
+ const io = init.io;
+ var buf: [4096]u8 = undefined;
+ var fw: std.Io.File.Writer = .init(.stdout(), io, &buf);
+ defer fw.interface.flush() catch {};
+ const w = &fw.interface;
+
+ const args = try init.minimal.args.toSlice(init.arena.allocator());
+ const mode = if (args.len > 1) args[1] else "";
+
+ if (std.mem.eql(u8, mode, "--server")) {
+ try w.print("Server mode (simulated)\n", .{});
+ return;
+ }
+
+ if (std.mem.eql(u8, mode, "--bench")) {
+ const data = [_]i32{ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
+ var i: u32 = 0;
+ while (i < 1_000_000) : (i += 1) {
+ _ = lib.sum(&data);
+ }
+ try w.print("bench: 1M sum() calls completed\n", .{});
+ return;
+ }
+
+ const data = [_]i32{ 10, 20, 30, 40, 50 };
+ try w.print("data: {any}\n", .{data});
+ try w.print("sum = {d}\n", .{lib.sum(&data)});
+ try w.print("product = {d}\n", .{lib.product(&data)});
+ try w.print("min = {?d}\n", .{lib.min(&data)});
+ try w.print("max = {?d}\n", .{lib.max(&data)});
+}