|
| 1 | +const std = @import("std"); |
| 2 | +const testing = std.testing; |
| 3 | + |
| 4 | +const matrix = @import("matrix.zig"); |
| 5 | +const row = matrix.row; |
| 6 | +const column = matrix.column; |
| 7 | + |
| 8 | +test "extract row from one number matrix" { |
| 9 | + const expected = &[_]i16{1}; |
| 10 | + const s = "1"; |
| 11 | + const actual = try row(testing.allocator, s, 1); |
| 12 | + defer testing.allocator.free(actual); |
| 13 | + try testing.expectEqualSlices(i16, expected, actual); |
| 14 | +} |
| 15 | + |
| 16 | +test "can extract row" { |
| 17 | + const expected = &[_]i16{ 3, 4 }; |
| 18 | + const s = "1 2\n3 4"; |
| 19 | + const actual = try row(testing.allocator, s, 2); |
| 20 | + defer testing.allocator.free(actual); |
| 21 | + try testing.expectEqualSlices(i16, expected, actual); |
| 22 | +} |
| 23 | + |
| 24 | +test "extract row where numbers have different widths" { |
| 25 | + const expected = &[_]i16{ 10, 20 }; |
| 26 | + const s = "1 2\n10 20"; |
| 27 | + const actual = try row(testing.allocator, s, 2); |
| 28 | + defer testing.allocator.free(actual); |
| 29 | + try testing.expectEqualSlices(i16, expected, actual); |
| 30 | +} |
| 31 | + |
| 32 | +test "can extract row from non-square matrix with no corresponding column" { |
| 33 | + const expected = &[_]i16{ 8, 7, 6 }; |
| 34 | + const s = "1 2 3\n4 5 6\n7 8 9\n8 7 6"; |
| 35 | + const actual = try row(testing.allocator, s, 4); |
| 36 | + defer testing.allocator.free(actual); |
| 37 | + try testing.expectEqualSlices(i16, expected, actual); |
| 38 | +} |
| 39 | + |
| 40 | +test "extract column from one number matrix" { |
| 41 | + const expected = &[_]i16{1}; |
| 42 | + const s = "1"; |
| 43 | + const actual = try column(testing.allocator, s, 1); |
| 44 | + defer testing.allocator.free(actual); |
| 45 | + try testing.expectEqualSlices(i16, expected, actual); |
| 46 | +} |
| 47 | + |
| 48 | +test "can extract column" { |
| 49 | + const expected = &[_]i16{ 3, 6, 9 }; |
| 50 | + const s = "1 2 3\n4 5 6\n7 8 9"; |
| 51 | + const actual = try column(testing.allocator, s, 3); |
| 52 | + defer testing.allocator.free(actual); |
| 53 | + try testing.expectEqualSlices(i16, expected, actual); |
| 54 | +} |
| 55 | + |
| 56 | +test "can extract column from non-square matrix with no corresponding row" { |
| 57 | + const expected = &[_]i16{ 4, 8, 6 }; |
| 58 | + const s = "1 2 3 4\n5 6 7 8\n9 8 7 6"; |
| 59 | + const actual = try column(testing.allocator, s, 4); |
| 60 | + defer testing.allocator.free(actual); |
| 61 | + try testing.expectEqualSlices(i16, expected, actual); |
| 62 | +} |
| 63 | + |
| 64 | +test "extract column where numbers have different widths" { |
| 65 | + const expected = &[_]i16{ 1903, 3, 4 }; |
| 66 | + const s = "89 1903 3\n18 3 1\n9 4 800"; |
| 67 | + const actual = try column(testing.allocator, s, 2); |
| 68 | + defer testing.allocator.free(actual); |
| 69 | + try testing.expectEqualSlices(i16, expected, actual); |
| 70 | +} |
| 71 | + |
| 72 | +test "row with negative numbers" { |
| 73 | + const expected = &[_]i16{ -57, 9, -42 }; |
| 74 | + const s = "1 2 4\n-57 9 -42\n10 0 65"; |
| 75 | + const actual = try row(testing.allocator, s, 2); |
| 76 | + defer testing.allocator.free(actual); |
| 77 | + try testing.expectEqualSlices(i16, expected, actual); |
| 78 | +} |
| 79 | + |
| 80 | +test "column with negative numbers" { |
| 81 | + const expected = &[_]i16{ -4, -42, -465 }; |
| 82 | + const s = "1 2 -4\n-57 9 -42\n10 0 -465"; |
| 83 | + const actual = try column(testing.allocator, s, 3); |
| 84 | + defer testing.allocator.free(actual); |
| 85 | + try testing.expectEqualSlices(i16, expected, actual); |
| 86 | +} |
0 commit comments