typun.zig (2416B)
1 const std = @import("std"); 2 const utils = @import("src/lib::utils.zig"); 3 const assert = std.debug.assert; 4 const NaNBox = @import("src/lib::nanbox.zig").NaNBox; 5 const Packer = @import("src/lib::nanbox.zig").Packer; 6 7 fn typetypeof(Box: type, comptime tag: Box.Type) type { 8 const T = Box.typeof(tag); 9 10 return if (utils.is_type(T, "pointer")) 11 @typeInfo(T).pointer.child 12 else 13 T; 14 } 15 16 pub fn dbg(box: anytype, comptime tag: @TypeOf(box).Type) @TypeOf(box) { 17 const Box = @TypeOf(box); 18 19 std.debug.print("U64: {b:064}\n", .{box.value}); 20 std.debug.print("Flg: {b:064}\n", .{Box.flag(tag).flag}); 21 std.debug.print("Msk: {b:064}\n", .{Box.flag(tag).mask}); 22 std.debug.print("Dec: {b:064}\n", .{Box.flag(tag).decode(box.value)}); 23 std.debug.print("Tru: {any}\n", .{Box.flag(tag).true}); 24 std.debug.print("Chk: {any}\n", .{Box.flag(tag).check(box.value)}); 25 26 return box; 27 } 28 29 pub fn tst(Box: type, comptime tag: Box.Type, val: typetypeof(Box, tag)) !void { 30 const T = Box.typeof(tag); 31 32 std.debug.print("Test tag {s}, type {s}, value {any}\n", .{ @tagName(tag), @typeName(T), val }); 33 34 const ret = if (comptime utils.is_type(T, "pointer")) 35 (try dbg(Box.of(tag, &val), tag).to(tag)).* 36 else 37 try dbg(Box.of(tag, val), tag).to(tag); 38 39 assert(ret == val); 40 } 41 42 pub fn test1() !void { 43 const Box = NaNBox(.{ 44 .u32 = u32, 45 .i24 = i24, 46 .void = void, 47 .pf64 = *const f64, 48 .f64 = f64, 49 .pf32 = *const f32, 50 .true = void, 51 .false = void, 52 }); 53 54 try tst(Box, .u32, 123); 55 try tst(Box, .pf32, 123); 56 try tst(Box, .i24, -13); 57 try tst(Box, .f64, 14.123); 58 try tst(Box, .void, {}); 59 try tst(Box, .true, {}); 60 try tst(Box, .false, {}); 61 } 62 63 pub fn printpack(comptime max: u6, packer: Packer(max)) void { 64 for (packer.matrix) |val| 65 if (val) |v| 66 std.debug.print("{b:0" ++ std.fmt.comptimePrint("{d}", .{max}) ++ "} ", .{v}) 67 else 68 std.debug.print("{s:" ++ std.fmt.comptimePrint("{d}", .{max}) ++ "} ", .{"null"}); 69 std.debug.print("\n", .{}); 70 } 71 72 pub fn main(_: std.process.Init) anyerror!u8 { 73 var packer = Packer(5).init(); 74 75 printpack(5, packer); 76 _ = packer.next(4); 77 printpack(5, packer); 78 _ = packer.next(2); 79 printpack(5, packer); 80 _ = packer.next(3); 81 printpack(5, packer); 82 83 try test1(); 84 85 return 0; 86 }