|
| 1 | +const std = @import("std"); |
| 2 | +const testing = std.testing; |
| 3 | + |
| 4 | +const saddle_points = @import("saddle_points.zig"); |
| 5 | +const saddlePoints = saddle_points.saddlePoints; |
| 6 | +const Point = saddle_points.Point; |
| 7 | + |
| 8 | +test "Can identify single saddle point" { |
| 9 | + const matrix = [3][3]i32{ |
| 10 | + [3]i32{ 9, 8, 7 }, // |
| 11 | + [3]i32{ 5, 3, 2 }, // |
| 12 | + [3]i32{ 6, 6, 7 }, // |
| 13 | + }; |
| 14 | + const expected = [_]Point{ |
| 15 | + .{ .row = 2, .column = 1 }, // |
| 16 | + }; |
| 17 | + const actual = try saddlePoints(3, 3, testing.allocator, matrix); |
| 18 | + defer testing.allocator.free(actual); |
| 19 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 20 | +} |
| 21 | + |
| 22 | +test "Can identify that empty matrix has no saddle points" { |
| 23 | + const matrix = [1][0]i32{ |
| 24 | + [0]i32{}, // |
| 25 | + }; |
| 26 | + const expected = [_]Point{}; |
| 27 | + const actual = try saddlePoints(1, 0, testing.allocator, matrix); |
| 28 | + defer testing.allocator.free(actual); |
| 29 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 30 | +} |
| 31 | + |
| 32 | +test "Can identify lack of saddle points when there are none" { |
| 33 | + const matrix = [3][3]i32{ |
| 34 | + [3]i32{ 1, 2, 3 }, // |
| 35 | + [3]i32{ 3, 1, 2 }, // |
| 36 | + [3]i32{ 2, 3, 1 }, // |
| 37 | + }; |
| 38 | + const expected = [_]Point{}; |
| 39 | + const actual = try saddlePoints(3, 3, testing.allocator, matrix); |
| 40 | + defer testing.allocator.free(actual); |
| 41 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 42 | +} |
| 43 | + |
| 44 | +test "Can identify multiple saddle points in a column" { |
| 45 | + const matrix = [3][3]i32{ |
| 46 | + [3]i32{ 4, 5, 4 }, // |
| 47 | + [3]i32{ 3, 5, 5 }, // |
| 48 | + [3]i32{ 1, 5, 4 }, // |
| 49 | + }; |
| 50 | + const expected = [_]Point{ |
| 51 | + .{ .row = 1, .column = 2 }, // |
| 52 | + .{ .row = 2, .column = 2 }, // |
| 53 | + .{ .row = 3, .column = 2 }, // |
| 54 | + }; |
| 55 | + const actual = try saddlePoints(3, 3, testing.allocator, matrix); |
| 56 | + defer testing.allocator.free(actual); |
| 57 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 58 | +} |
| 59 | + |
| 60 | +test "Can identify multiple saddle points in a row" { |
| 61 | + const matrix = [3][3]i32{ |
| 62 | + [3]i32{ 6, 7, 8 }, // |
| 63 | + [3]i32{ 5, 5, 5 }, // |
| 64 | + [3]i32{ 7, 5, 6 }, // |
| 65 | + }; |
| 66 | + const expected = [_]Point{ |
| 67 | + .{ .row = 2, .column = 1 }, // |
| 68 | + .{ .row = 2, .column = 2 }, // |
| 69 | + .{ .row = 2, .column = 3 }, // |
| 70 | + }; |
| 71 | + const actual = try saddlePoints(3, 3, testing.allocator, matrix); |
| 72 | + defer testing.allocator.free(actual); |
| 73 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 74 | +} |
| 75 | + |
| 76 | +test "Can identify saddle point in bottom right corner" { |
| 77 | + const matrix = [3][3]i32{ |
| 78 | + [3]i32{ 8, 7, 9 }, // |
| 79 | + [3]i32{ 6, 7, 6 }, // |
| 80 | + [3]i32{ 3, 2, 5 }, // |
| 81 | + }; |
| 82 | + const expected = [_]Point{ |
| 83 | + .{ .row = 3, .column = 3 }, // |
| 84 | + }; |
| 85 | + const actual = try saddlePoints(3, 3, testing.allocator, matrix); |
| 86 | + defer testing.allocator.free(actual); |
| 87 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 88 | +} |
| 89 | + |
| 90 | +test "Can identify saddle points in a non square matrix" { |
| 91 | + const matrix = [2][3]i32{ |
| 92 | + [3]i32{ 3, 1, 3 }, // |
| 93 | + [3]i32{ 3, 2, 4 }, // |
| 94 | + }; |
| 95 | + const expected = [_]Point{ |
| 96 | + .{ .row = 1, .column = 1 }, // |
| 97 | + .{ .row = 1, .column = 3 }, // |
| 98 | + }; |
| 99 | + const actual = try saddlePoints(2, 3, testing.allocator, matrix); |
| 100 | + defer testing.allocator.free(actual); |
| 101 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 102 | +} |
| 103 | + |
| 104 | +test "Can identify that saddle points in a single column matrix are those with the minimum value" { |
| 105 | + const matrix = [4][1]i32{ |
| 106 | + [1]i32{2}, // |
| 107 | + [1]i32{1}, // |
| 108 | + [1]i32{4}, // |
| 109 | + [1]i32{1}, // |
| 110 | + }; |
| 111 | + const expected = [_]Point{ |
| 112 | + .{ .row = 2, .column = 1 }, // |
| 113 | + .{ .row = 4, .column = 1 }, // |
| 114 | + }; |
| 115 | + const actual = try saddlePoints(4, 1, testing.allocator, matrix); |
| 116 | + defer testing.allocator.free(actual); |
| 117 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 118 | +} |
| 119 | + |
| 120 | +test "Can identify that saddle points in a single row matrix are those with the maximum value" { |
| 121 | + const matrix = [1][4]i32{ |
| 122 | + [4]i32{ 2, 5, 3, 5 }, // |
| 123 | + }; |
| 124 | + const expected = [_]Point{ |
| 125 | + .{ .row = 1, .column = 2 }, // |
| 126 | + .{ .row = 1, .column = 4 }, // |
| 127 | + }; |
| 128 | + const actual = try saddlePoints(1, 4, testing.allocator, matrix); |
| 129 | + defer testing.allocator.free(actual); |
| 130 | + try testing.expectEqualSlices(Point, &expected, actual); |
| 131 | +} |
0 commit comments