| 1 | const std = @import("std"); |
| 2 | const expectEqual = std.testing.expectEqual; |
| 3 | |
| 4 | const mat4x5 = [4][5]f32{ |
| 5 | [_]f32{ 1.0, 0.0, 0.0, 0.0, 0.0 }, |
| 6 | [_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 }, |
| 7 | [_]f32{ 0.0, 0.0, 1.0, 0.0, 0.0 }, |
| 8 | [_]f32{ 0.0, 0.0, 0.0, 1.0, 9.9 }, |
| 9 | }; |
| 10 | test "multidimensional arrays" { |
| 11 | // mat4x5 itself is a one-dimensional array of arrays. |
| 12 | try expectEqual(mat4x5[1], [_]f32{ 0.0, 1.0, 0.0, 1.0, 0.0 }); |
| 13 | |
| 14 | // Access the 2D array by indexing the outer array, and then the inner array. |
| 15 | try expectEqual(9.9, mat4x5[3][4]); |
| 16 | |
| 17 | // Here we iterate with for loops. |
| 18 | for (mat4x5, 0..) |row, row_index| { |
| 19 | for (row, 0..) |cell, column_index| { |
| 20 | if (row_index == column_index) { |
| 21 | try expectEqual(1.0, cell); |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | // Initialize a multidimensional array to zeros. |
| 27 | const all_zero: [4][5]f32 = @splat(@splat(0)); |
| 28 | try expectEqual(0, all_zero[0][0]); |
| 29 | } |
| 30 | |
| 31 | // test |