diff options
| author | David Czihak <git@dcz.at> | 2026-05-07 14:33:19 +0200 |
|---|---|---|
| committer | David Czihak <git@dcz.at> | 2026-05-07 14:33:19 +0200 |
| commit | ddf2de739068b5ff0866ccb1d067f3cb53a4fc55 (patch) | |
| tree | 1a77efe9d73a6172be3c37d29b321eadd4efe379 /examples/hello-zig/src/root.zig | |
Initial commitv0.1.7
Diffstat (limited to 'examples/hello-zig/src/root.zig')
| -rw-r--r-- | examples/hello-zig/src/root.zig | 83 |
1 files changed, 83 insertions, 0 deletions
diff --git a/examples/hello-zig/src/root.zig b/examples/hello-zig/src/root.zig new file mode 100644 index 0000000..f6600ac --- /dev/null +++ b/examples/hello-zig/src/root.zig @@ -0,0 +1,83 @@ +const std = @import("std"); + +pub const OutputStyle = enum { + compact, + verbose, +}; + +pub const NumberKind = union(enum) { + negative: i64, + zero, + positive: u64, +}; + +pub const Greeter = struct { + name: []const u8, + punctuation: u8 = '!', + + pub fn write(self: Greeter, writer: anytype, style: OutputStyle) !void { + switch (style) { + .compact => try writer.print("hello, {s}{c}\n", .{ self.name, self.punctuation }), + .verbose => { + try writer.print( + \\hello, {s}{c} + \\this sample is here to exercise Zig syntax support in Nova. + \\ + , .{ self.name, self.punctuation }); + }, + } + } +}; + +pub fn classifyNumber(value: i64) NumberKind { + if (value == 0) return .zero; + if (value < 0) return .{ .negative = value }; + return .{ .positive = @intCast(value) }; +} + +pub fn describeNumber(allocator: std.mem.Allocator, value: i64) ![]u8 { + return switch (classifyNumber(value)) { + .zero => std.fmt.allocPrint(allocator, "zero", .{}), + .negative => |negative| std.fmt.allocPrint(allocator, "negative({d})", .{negative}), + .positive => |positive| std.fmt.allocPrint(allocator, "positive({d})", .{positive}), + }; +} + +pub fn collectEvenSquares( + allocator: std.mem.Allocator, + values: []const i32, +) !std.ArrayList(i32) { + var result: std.ArrayList(i32) = .empty; + errdefer result.deinit(allocator); + + for (values) |value| { + if (@mod(value, 2) != 0) continue; + try result.append(allocator, value * value); + } + + return result; +} + +test "classifyNumber covers the main branches" { + try std.testing.expectEqual(NumberKind{ .negative = -5 }, classifyNumber(-5)); + try std.testing.expectEqual(NumberKind.zero, classifyNumber(0)); + try std.testing.expectEqual(NumberKind{ .positive = 9 }, classifyNumber(9)); +} + +test "collectEvenSquares keeps only even numbers" { + const values = [_]i32{ -4, -3, 0, 1, 2, 7 }; + var result = try collectEvenSquares(std.testing.allocator, &values); + defer result.deinit(std.testing.allocator); + + try std.testing.expectEqualSlices(i32, &.{ 16, 0, 4 }, result.items); +} + +test "Greeter.write renders verbose output" { + var list: std.ArrayList(u8) = .empty; + defer list.deinit(std.testing.allocator); + + const writer = list.writer(std.testing.allocator); + try (Greeter{ .name = "Zig" }).write(writer, .verbose); + + try std.testing.expect(std.mem.indexOf(u8, list.items, "exercise Zig syntax support") != null); +} |
