zig

fork of https://codeberg.org/ziglang/zig
Log | Files | Refs | README | LICENSE

test_multidimensional_arrays.zig (969B) - Raw


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