1const std = @import("std");
2const native_endian = @import("builtin").target.cpu.arch.endian();
3const expectEqual = std.testing.expectEqual;
4
5test "pointer casting" {
6 const bytes: [4]u8 align(@alignOf(u32)) = .{ 0x10, 0x20, 0x30, 0x40 };
7 const u32_ptr: *const u32 = @ptrCast(&bytes);
8
9 // Because we directly reinterpreted bytes of memory, the `u32` value we
10 // load from `u32_ptr` depends on the target endian:
11 switch (native_endian) {
12 .little => try expectEqual(0x40302010, u32_ptr.*),
13 .big => try expectEqual(0x10203040, u32_ptr.*),
14 }
15
16 // To instead reinterpret the logical bit representation of `bytes` with no
17 // dependency on the target endian, use `@bitCast`, which always places
18 // earlier array elements into less-significant bits:
19 try expectEqual(0x40302010, @as(u32, @bitCast(bytes)));
20}
21
22test "pointer child type" {
23 // pointer types have a `child` field which tells you the type they point to.
24 try expectEqual(u32, @typeInfo(*u32).pointer.child);
25}
26
27// test