diff options
| author | David Czihak <git@dcz.at> | 2026-05-09 13:01:50 +0200 |
|---|---|---|
| committer | David Czihak <git@dcz.at> | 2026-05-09 13:01:50 +0200 |
| commit | 4b6f66fd512c254b5a82220dc77411fe391dd258 (patch) | |
| tree | 7d77d7966e9ad2e296986ea8cfeb607088028195 /examples/hello-zig/src/root.zig | |
| parent | 64e9c56fc665972fdde5234c4fb2f2a882e237dc (diff) | |
Chore: Rework examples for thorough extension testing
Diffstat (limited to 'examples/hello-zig/src/root.zig')
| -rw-r--r-- | examples/hello-zig/src/root.zig | 83 |
1 files changed, 0 insertions, 83 deletions
diff --git a/examples/hello-zig/src/root.zig b/examples/hello-zig/src/root.zig deleted file mode 100644 index f6600ac..0000000 --- a/examples/hello-zig/src/root.zig +++ /dev/null @@ -1,83 +0,0 @@ -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); -} |
