| author | |
| committer | |
| log | 4461bc011659155d72810a9a8cd3b33129b0823d |
| tree | bfb65e0e185975e5ca27e911b311cb6e52f52af1 |
| parent | a1d0efbb3e97764e36caf81ac8f9ac93bb1e8a84 |
8 files changed, 125 insertions(+), 10 deletions(-)
src/FieldUnion.zig+16| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const expectSimilarType = extras.expectSimilarType; | ||
| 4 | 5 | ||
| 5 | pub fn FieldUnion(comptime T: type) type { | 6 | pub fn FieldUnion(comptime T: type) type { |
| 6 | const infos = std.meta.fields(T); | 7 | const infos = std.meta.fields(T); |
| ... | @@ -20,3 +21,18 @@ pub fn FieldUnion(comptime T: type) type { | ... | @@ -20,3 +21,18 @@ pub fn FieldUnion(comptime T: type) type { |
| 20 | .decls = &.{}, | 21 | .decls = &.{}, |
| 21 | } }); | 22 | } }); |
| 22 | } | 23 | } |
| 24 | |||
| 25 | test { | ||
| 26 | try expectSimilarType( | ||
| 27 | FieldUnion(struct { | ||
| 28 | a: u32, | ||
| 29 | b: u8, | ||
| 30 | c: u16, | ||
| 31 | }), | ||
| 32 | union(enum) { | ||
| 33 | a: u32, | ||
| 34 | b: u8, | ||
| 35 | c: u16, | ||
| 36 | }, | ||
| 37 | ); | ||
| 38 | } |
src/Partial.zig+11| ... | @@ -40,3 +40,14 @@ test { | ... | @@ -40,3 +40,14 @@ test { |
| 40 | }, | 40 | }, |
| 41 | ); | 41 | ); |
| 42 | } | 42 | } |
| 43 | |||
| 44 | test { | ||
| 45 | try expectSimilarType( | ||
| 46 | Partial(struct { | ||
| 47 | a: ?u8, | ||
| 48 | }), | ||
| 49 | struct { | ||
| 50 | a: ??u8, | ||
| 51 | }, | ||
| 52 | ); | ||
| 53 | } |
src/ReverseFields.zig+11-9| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const expectSimilarType = extras.expectSimilarType; | ||
| 4 | 5 | ||
| 5 | pub fn ReverseFields(comptime T: type) type { | 6 | pub fn ReverseFields(comptime T: type) type { |
| 6 | var info = @typeInfo(T).Struct; | 7 | var info = @typeInfo(T).Struct; |
| ... | @@ -14,13 +15,14 @@ pub fn ReverseFields(comptime T: type) type { | ... | @@ -14,13 +15,14 @@ pub fn ReverseFields(comptime T: type) type { |
| 14 | } | 15 | } |
| 15 | 16 | ||
| 16 | test { | 17 | test { |
| 17 | const A = struct { x: u8, y: u16 }; | 18 | try expectSimilarType( |
| 18 | const B = ReverseFields(A); | 19 | ReverseFields(struct { |
| 19 | const fields = std.meta.fields(B); | 20 | x: u8, |
| 20 | 21 | y: u16, | |
| 21 | try std.testing.expect(fields.len == 2); | 22 | }), |
| 22 | try std.testing.expect(fields[0].type == u16); | 23 | struct { |
| 23 | try std.testing.expect(std.mem.eql(u8, fields[0].name, "y")); | 24 | y: u16, |
| 24 | try std.testing.expect(fields[1].type == u8); | 25 | x: u8, |
| 25 | try std.testing.expect(std.mem.eql(u8, fields[1].name, "x")); | 26 | }, |
| 27 | ); | ||
| 26 | } | 28 | } |
src/expectSimilarType.zig+37| ... | @@ -28,6 +28,43 @@ pub fn expectSimilarType(comptime A: type, comptime B: type) !void { | ... | @@ -28,6 +28,43 @@ pub fn expectSimilarType(comptime A: type, comptime B: type) !void { |
| 28 | 28 | ||
| 29 | return; | 29 | return; |
| 30 | } | 30 | } |
| 31 | if (info_a == .Union) { | ||
| 32 | const info_a_u = info_a.Union; | ||
| 33 | const info_b_u = info_b.Union; | ||
| 34 | |||
| 35 | try std.testing.expect(info_a_u.layout == info_b_u.layout); | ||
| 36 | try expectSimilarType(info_a_u.tag_type.?, info_b_u.tag_type.?); | ||
| 37 | |||
| 38 | for (info_a_u.decls, info_b_u.decls) |da, db| { | ||
| 39 | try std.testing.expect(std.mem.eql(u8, da.name, db.name)); | ||
| 40 | } | ||
| 41 | |||
| 42 | inline for (info_a_u.fields, info_b_u.fields) |fa, fb| { | ||
| 43 | try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); | ||
| 44 | try std.testing.expect(fa.type == fb.type); | ||
| 45 | try std.testing.expect(fa.alignment == fb.alignment); | ||
| 46 | } | ||
| 47 | |||
| 48 | return; | ||
| 49 | } | ||
| 50 | if (info_a == .Enum) { | ||
| 51 | const info_a_e = info_a.Enum; | ||
| 52 | const info_b_e = info_b.Enum; | ||
| 53 | |||
| 54 | try std.testing.expect(info_a_e.tag_type == info_b_e.tag_type); | ||
| 55 | try std.testing.expect(info_a_e.is_exhaustive == info_b_e.is_exhaustive); | ||
| 56 | |||
| 57 | for (info_a_e.decls, info_b_e.decls) |da, db| { | ||
| 58 | try std.testing.expect(std.mem.eql(u8, da.name, db.name)); | ||
| 59 | } | ||
| 60 | |||
| 61 | inline for (info_a_e.fields, info_b_e.fields) |fa, fb| { | ||
| 62 | try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); | ||
| 63 | try std.testing.expect(fa.value == fb.value); | ||
| 64 | } | ||
| 65 | |||
| 66 | return; | ||
| 67 | } | ||
| 31 | @compileError("not implemented"); | 68 | @compileError("not implemented"); |
| 32 | } | 69 | } |
| 33 | 70 |
src/isArrayOf.zig+14| ... | @@ -13,3 +13,17 @@ pub fn isArrayOf(comptime T: type) std.meta.trait.TraitFn { | ... | @@ -13,3 +13,17 @@ pub fn isArrayOf(comptime T: type) std.meta.trait.TraitFn { |
| 13 | }; | 13 | }; |
| 14 | return Closure.trait; | 14 | return Closure.trait; |
| 15 | } | 15 | } |
| 16 | |||
| 17 | const L = isArrayOf(u8); | ||
| 18 | test { | ||
| 19 | try std.testing.expect(L([5]u8)); | ||
| 20 | } | ||
| 21 | test { | ||
| 22 | try std.testing.expect(!L([3]u32)); | ||
| 23 | } | ||
| 24 | test { | ||
| 25 | try std.testing.expect(!L(struct { a: u8 })); | ||
| 26 | } | ||
| 27 | test { | ||
| 28 | try std.testing.expect(!L(enum { a, b, c })); | ||
| 29 | } |
src/parse_bool.zig+18-1| ... | @@ -1,7 +1,24 @@ | ... | @@ -1,7 +1,24 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const parse_int = extras.parse_int; | ||
| 4 | 5 | ||
| 5 | pub fn parse_bool(s: ?string) bool { | 6 | pub fn parse_bool(s: ?string) bool { |
| 6 | return extras.parse_int(u1, s, 10, 0) > 0; | 7 | return parse_int(u1, s, 10, 0) > 0; |
| 8 | } | ||
| 9 | |||
| 10 | test { | ||
| 11 | try std.testing.expect(parse_bool("1")); | ||
| 12 | } | ||
| 13 | test { | ||
| 14 | try std.testing.expect(!parse_bool("0")); | ||
| 15 | } | ||
| 16 | test { | ||
| 17 | try std.testing.expect(!parse_bool("Q")); | ||
| 18 | } | ||
| 19 | test { | ||
| 20 | try std.testing.expect(!parse_bool("")); | ||
| 21 | } | ||
| 22 | test { | ||
| 23 | try std.testing.expect(!parse_bool(null)); | ||
| 7 | } | 24 | } |
src/parse_int.zig+13| ... | @@ -6,3 +6,16 @@ pub fn parse_int(comptime T: type, s: ?string, b: u8, d: T) T { | ... | @@ -6,3 +6,16 @@ pub fn parse_int(comptime T: type, s: ?string, b: u8, d: T) T { |
| 6 | if (s == null) return d; | 6 | if (s == null) return d; |
| 7 | return std.fmt.parseInt(T, s.?, b) catch d; | 7 | return std.fmt.parseInt(T, s.?, b) catch d; |
| 8 | } | 8 | } |
| 9 | |||
| 10 | test { | ||
| 11 | try std.testing.expect(parse_int(u32, "25", 10, 13) == 25); | ||
| 12 | } | ||
| 13 | test { | ||
| 14 | try std.testing.expect(parse_int(u32, "Q", 10, 13) == 13); | ||
| 15 | } | ||
| 16 | test { | ||
| 17 | try std.testing.expect(parse_int(u32, "", 10, 13) == 13); | ||
| 18 | } | ||
| 19 | test { | ||
| 20 | try std.testing.expect(parse_int(u32, null, 10, 13) == 13); | ||
| 21 | } |
src/to_hex.zig+5| ... | @@ -1,6 +1,7 @@ | ... | @@ -1,6 +1,7 @@ |
| 1 | const std = @import("std"); | 1 | const std = @import("std"); |
| 2 | const string = []const u8; | 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); | 3 | const extras = @import("./lib.zig"); |
| 4 | const rawInt = extras.rawInt; | ||
| 4 | 5 | ||
| 5 | pub fn to_hex(array: anytype) [array.len * 2]u8 { | 6 | pub fn to_hex(array: anytype) [array.len * 2]u8 { |
| 6 | var res: [array.len * 2]u8 = undefined; | 7 | var res: [array.len * 2]u8 = undefined; |
| ... | @@ -8,3 +9,7 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { | ... | @@ -8,3 +9,7 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { |
| 8 | std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable; | 9 | std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable; |
| 9 | return res; | 10 | return res; |
| 10 | } | 11 | } |
| 12 | |||
| 13 | test { | ||
| 14 | try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132")); | ||
| 15 | } |