|
| 1 | +const std = @import("std"); |
| 2 | +const testing = std.testing; |
| 3 | + |
| 4 | +const rows = @import("diamond.zig").rows; |
| 5 | + |
| 6 | +fn free(slices: [][]u8) void { |
| 7 | + for (slices) |slice| { |
| 8 | + testing.allocator.free(slice); |
| 9 | + } |
| 10 | + testing.allocator.free(slices); |
| 11 | +} |
| 12 | + |
| 13 | +fn testRows(allocator: std.mem.Allocator, expected: []const []const u8, letter: u8) !void { |
| 14 | + const actual = try rows(allocator, letter); |
| 15 | + defer free(actual); |
| 16 | + try testing.expectEqual(expected.len, actual.len); |
| 17 | + for (0..expected.len) |i| { |
| 18 | + try testing.expectEqualStrings(expected[i], actual[i]); |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +test "Degenerate case with a single 'A' row" { |
| 23 | + const expected = [_][]const u8{ |
| 24 | + "A", // |
| 25 | + }; |
| 26 | + try std.testing.checkAllAllocationFailures( |
| 27 | + std.testing.allocator, |
| 28 | + testRows, |
| 29 | + .{ &expected, 'A' }, |
| 30 | + ); |
| 31 | +} |
| 32 | + |
| 33 | +test "Degenerate case with no row containing 3 distinct groups of spaces" { |
| 34 | + const expected = [_][]const u8{ |
| 35 | + " A ", // |
| 36 | + "B B", // |
| 37 | + " A ", // |
| 38 | + }; |
| 39 | + try std.testing.checkAllAllocationFailures( |
| 40 | + std.testing.allocator, |
| 41 | + testRows, |
| 42 | + .{ &expected, 'B' }, |
| 43 | + ); |
| 44 | +} |
| 45 | + |
| 46 | +test "Smallest non-degenerate case with odd diamond side length" { |
| 47 | + const expected = [_][]const u8{ |
| 48 | + " A ", // |
| 49 | + " B B ", // |
| 50 | + "C C", // |
| 51 | + " B B ", // |
| 52 | + " A ", // |
| 53 | + }; |
| 54 | + try std.testing.checkAllAllocationFailures( |
| 55 | + std.testing.allocator, |
| 56 | + testRows, |
| 57 | + .{ &expected, 'C' }, |
| 58 | + ); |
| 59 | +} |
| 60 | + |
| 61 | +test "Smallest non-degenerate case with even diamond side length" { |
| 62 | + const expected = [_][]const u8{ |
| 63 | + " A ", // |
| 64 | + " B B ", // |
| 65 | + " C C ", // |
| 66 | + "D D", // |
| 67 | + " C C ", // |
| 68 | + " B B ", // |
| 69 | + " A ", // |
| 70 | + }; |
| 71 | + try std.testing.checkAllAllocationFailures( |
| 72 | + std.testing.allocator, |
| 73 | + testRows, |
| 74 | + .{ &expected, 'D' }, |
| 75 | + ); |
| 76 | +} |
| 77 | + |
| 78 | +test "Largest possible diamond" { |
| 79 | + const expected = [_][]const u8{ |
| 80 | + " A ", // |
| 81 | + " B B ", // |
| 82 | + " C C ", // |
| 83 | + " D D ", // |
| 84 | + " E E ", // |
| 85 | + " F F ", // |
| 86 | + " G G ", // |
| 87 | + " H H ", // |
| 88 | + " I I ", // |
| 89 | + " J J ", // |
| 90 | + " K K ", // |
| 91 | + " L L ", // |
| 92 | + " M M ", // |
| 93 | + " N N ", // |
| 94 | + " O O ", // |
| 95 | + " P P ", // |
| 96 | + " Q Q ", // |
| 97 | + " R R ", // |
| 98 | + " S S ", // |
| 99 | + " T T ", // |
| 100 | + " U U ", // |
| 101 | + " V V ", // |
| 102 | + " W W ", // |
| 103 | + " X X ", // |
| 104 | + " Y Y ", // |
| 105 | + "Z Z", // |
| 106 | + " Y Y ", // |
| 107 | + " X X ", // |
| 108 | + " W W ", // |
| 109 | + " V V ", // |
| 110 | + " U U ", // |
| 111 | + " T T ", // |
| 112 | + " S S ", // |
| 113 | + " R R ", // |
| 114 | + " Q Q ", // |
| 115 | + " P P ", // |
| 116 | + " O O ", // |
| 117 | + " N N ", // |
| 118 | + " M M ", // |
| 119 | + " L L ", // |
| 120 | + " K K ", // |
| 121 | + " J J ", // |
| 122 | + " I I ", // |
| 123 | + " H H ", // |
| 124 | + " G G ", // |
| 125 | + " F F ", // |
| 126 | + " E E ", // |
| 127 | + " D D ", // |
| 128 | + " C C ", // |
| 129 | + " B B ", // |
| 130 | + " A ", // |
| 131 | + }; |
| 132 | + try std.testing.checkAllAllocationFailures( |
| 133 | + std.testing.allocator, |
| 134 | + testRows, |
| 135 | + .{ &expected, 'Z' }, |
| 136 | + ); |
| 137 | +} |
0 commit comments