zig

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

test_coerce_slices_arrays_and_pointers.zig (2329B) - Raw


      1 const std = @import("std");
      2 const expect = std.testing.expect;
      3 
      4 // You can assign constant pointers to arrays to a slice with
      5 // const modifier on the element type. Useful in particular for
      6 // String literals.
      7 test "*const [N]T to []const T" {
      8     const x1: []const u8 = "hello";
      9     const x2: []const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
     10     try expect(std.mem.eql(u8, x1, x2));
     11 
     12     const y: []const f32 = &[2]f32{ 1.2, 3.4 };
     13     try expect(y[0] == 1.2);
     14 }
     15 
     16 // Likewise, it works when the destination type is an error union.
     17 test "*const [N]T to E![]const T" {
     18     const x1: anyerror![]const u8 = "hello";
     19     const x2: anyerror![]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
     20     try expect(std.mem.eql(u8, try x1, try x2));
     21 
     22     const y: anyerror![]const f32 = &[2]f32{ 1.2, 3.4 };
     23     try expect((try y)[0] == 1.2);
     24 }
     25 
     26 // Likewise, it works when the destination type is an optional.
     27 test "*const [N]T to ?[]const T" {
     28     const x1: ?[]const u8 = "hello";
     29     const x2: ?[]const u8 = &[5]u8{ 'h', 'e', 'l', 'l', 111 };
     30     try expect(std.mem.eql(u8, x1.?, x2.?));
     31 
     32     const y: ?[]const f32 = &[2]f32{ 1.2, 3.4 };
     33     try expect(y.?[0] == 1.2);
     34 }
     35 
     36 // In this cast, the array length becomes the slice length.
     37 test "*[N]T to []T" {
     38     var buf: [5]u8 = "hello".*;
     39     const x: []u8 = &buf;
     40     try expect(std.mem.eql(u8, x, "hello"));
     41 
     42     const buf2 = [2]f32{ 1.2, 3.4 };
     43     const x2: []const f32 = &buf2;
     44     try expect(std.mem.eql(f32, x2, &[2]f32{ 1.2, 3.4 }));
     45 }
     46 
     47 // Single-item pointers to arrays can be coerced to many-item pointers.
     48 test "*[N]T to [*]T" {
     49     var buf: [5]u8 = "hello".*;
     50     const x: [*]u8 = &buf;
     51     try expect(x[4] == 'o');
     52     // x[5] would be an uncaught out of bounds pointer dereference!
     53 }
     54 
     55 // Likewise, it works when the destination type is an optional.
     56 test "*[N]T to ?[*]T" {
     57     var buf: [5]u8 = "hello".*;
     58     const x: ?[*]u8 = &buf;
     59     try expect(x.?[4] == 'o');
     60 }
     61 
     62 // Single-item pointers can be cast to len-1 single-item arrays.
     63 test "*T to *[1]T" {
     64     var x: i32 = 1234;
     65     const y: *[1]i32 = &x;
     66     const z: [*]i32 = y;
     67     try expect(z[0] == 1234);
     68 }
     69 
     70 // Sentinel-terminated slices can be coerced into sentinel-terminated pointers
     71 test "[:x]T to [*:x]T" {
     72     const buf: [:0]const u8 = "hello";
     73     const buf2: [*:0]const u8 = buf;
     74     try expect(buf2[4] == 'o');
     75 }
     76 
     77 // test