| ... | @@ -0,0 +1,41 @@ |
| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | const builtin = @import("builtin"); |
| 5 | const native_endian = builtin.target.cpu.arch.endian(); |
| 6 | |
| 7 | pub fn rawInt(comptime T: type, comptime literal: comptime_int) T { |
| 8 | comptime std.debug.assert(@typeInfo(T).Int.bits % 8 == 0); |
| 9 | return switch (native_endian) { |
| 10 | .Little => @byteSwap(@as(T, literal)), |
| 11 | .Big => @compileError("unreachable"), |
| 12 | }; |
| 13 | } |
| 14 | |
| 15 | test { |
| 16 | const bytes = std.mem.toBytes(rawInt(u16, 0x4e5a)); |
| 17 | try std.testing.expect(bytes[0] == 0x4e); |
| 18 | } |
| 19 | test { |
| 20 | const bytes = std.mem.toBytes(rawInt(u16, 0x4e5a)); |
| 21 | try std.testing.expect(bytes[0] == 0x4e); |
| 22 | try std.testing.expect(bytes[1] == 0x5a); |
| 23 | } |
| 24 | test { |
| 25 | const bytes = std.mem.toBytes(rawInt(u32, 0x4e5a7da9)); |
| 26 | try std.testing.expect(bytes[0] == 0x4e); |
| 27 | try std.testing.expect(bytes[1] == 0x5a); |
| 28 | try std.testing.expect(bytes[2] == 0x7d); |
| 29 | try std.testing.expect(bytes[3] == 0xa9); |
| 30 | } |
| 31 | test { |
| 32 | const bytes = std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132)); |
| 33 | try std.testing.expect(bytes[0] == 0x4e); |
| 34 | try std.testing.expect(bytes[1] == 0x5a); |
| 35 | try std.testing.expect(bytes[2] == 0x7d); |
| 36 | try std.testing.expect(bytes[3] == 0xa9); |
| 37 | try std.testing.expect(bytes[4] == 0xf3); |
| 38 | try std.testing.expect(bytes[5] == 0xf1); |
| 39 | try std.testing.expect(bytes[6] == 0xd1); |
| 40 | try std.testing.expect(bytes[7] == 0x32); |
| 41 | } |