diff --git a/src/FieldUnion.zig b/src/FieldUnion.zig index 197dbf3813f6c217686eeee647962f6388465326..79312957937381f30483c961bfbc47fd9a292d80 100644 --- a/src/FieldUnion.zig +++ b/src/FieldUnion.zig @@ -1,6 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); +const expectSimilarType = extras.expectSimilarType; pub fn FieldUnion(comptime T: type) type { const infos = std.meta.fields(T); @@ -20,3 +21,18 @@ pub fn FieldUnion(comptime T: type) type { .decls = &.{}, } }); } + +test { + try expectSimilarType( + FieldUnion(struct { + a: u32, + b: u8, + c: u16, + }), + union(enum) { + a: u32, + b: u8, + c: u16, + }, + ); +} diff --git a/src/Partial.zig b/src/Partial.zig index 28b6ed1a39761a0adf49edf74308563227defbbc..147636e38a3e9b621c7ca12f7fa2d1508773ece9 100644 --- a/src/Partial.zig +++ b/src/Partial.zig @@ -40,3 +40,14 @@ test { }, ); } + +test { + try expectSimilarType( + Partial(struct { + a: ?u8, + }), + struct { + a: ??u8, + }, + ); +} diff --git a/src/ReverseFields.zig b/src/ReverseFields.zig index a864883c18d92e5751e1c9e2752978b4d4e3f8d6..514a0a46f8ee5b8380ada3f7efc823d5d76ebd96 100644 --- a/src/ReverseFields.zig +++ b/src/ReverseFields.zig @@ -1,6 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); +const expectSimilarType = extras.expectSimilarType; pub fn ReverseFields(comptime T: type) type { var info = @typeInfo(T).Struct; @@ -14,13 +15,14 @@ pub fn ReverseFields(comptime T: type) type { } test { - const A = struct { x: u8, y: u16 }; - const B = ReverseFields(A); - const fields = std.meta.fields(B); - - try std.testing.expect(fields.len == 2); - try std.testing.expect(fields[0].type == u16); - try std.testing.expect(std.mem.eql(u8, fields[0].name, "y")); - try std.testing.expect(fields[1].type == u8); - try std.testing.expect(std.mem.eql(u8, fields[1].name, "x")); + try expectSimilarType( + ReverseFields(struct { + x: u8, + y: u16, + }), + struct { + y: u16, + x: u8, + }, + ); } diff --git a/src/expectSimilarType.zig b/src/expectSimilarType.zig index 2eaa6f8434c92a2859cfbde996bb6da6b0be44a4..c73841d40947bdd45c9ac9106e052f14d2d325b0 100644 --- a/src/expectSimilarType.zig +++ b/src/expectSimilarType.zig @@ -28,6 +28,43 @@ pub fn expectSimilarType(comptime A: type, comptime B: type) !void { return; } + if (info_a == .Union) { + const info_a_u = info_a.Union; + const info_b_u = info_b.Union; + + try std.testing.expect(info_a_u.layout == info_b_u.layout); + try expectSimilarType(info_a_u.tag_type.?, info_b_u.tag_type.?); + + for (info_a_u.decls, info_b_u.decls) |da, db| { + try std.testing.expect(std.mem.eql(u8, da.name, db.name)); + } + + inline for (info_a_u.fields, info_b_u.fields) |fa, fb| { + try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); + try std.testing.expect(fa.type == fb.type); + try std.testing.expect(fa.alignment == fb.alignment); + } + + return; + } + if (info_a == .Enum) { + const info_a_e = info_a.Enum; + const info_b_e = info_b.Enum; + + try std.testing.expect(info_a_e.tag_type == info_b_e.tag_type); + try std.testing.expect(info_a_e.is_exhaustive == info_b_e.is_exhaustive); + + for (info_a_e.decls, info_b_e.decls) |da, db| { + try std.testing.expect(std.mem.eql(u8, da.name, db.name)); + } + + inline for (info_a_e.fields, info_b_e.fields) |fa, fb| { + try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); + try std.testing.expect(fa.value == fb.value); + } + + return; + } @compileError("not implemented"); } diff --git a/src/isArrayOf.zig b/src/isArrayOf.zig index 0cc73b85efa779c6267f788b98f8e09c9eaff5ac..ceb24b5561d2e48e50cf473e3a48af787842b314 100644 --- a/src/isArrayOf.zig +++ b/src/isArrayOf.zig @@ -13,3 +13,17 @@ pub fn isArrayOf(comptime T: type) std.meta.trait.TraitFn { }; return Closure.trait; } + +const L = isArrayOf(u8); +test { + try std.testing.expect(L([5]u8)); +} +test { + try std.testing.expect(!L([3]u32)); +} +test { + try std.testing.expect(!L(struct { a: u8 })); +} +test { + try std.testing.expect(!L(enum { a, b, c })); +} diff --git a/src/parse_bool.zig b/src/parse_bool.zig index fbba992c6dc3511116d28644e82cfc79bd3ff329..7750204976b93022133ae63149758d0b584dbfba 100644 --- a/src/parse_bool.zig +++ b/src/parse_bool.zig @@ -1,7 +1,24 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); +const parse_int = extras.parse_int; pub fn parse_bool(s: ?string) bool { - return extras.parse_int(u1, s, 10, 0) > 0; + return parse_int(u1, s, 10, 0) > 0; +} + +test { + try std.testing.expect(parse_bool("1")); +} +test { + try std.testing.expect(!parse_bool("0")); +} +test { + try std.testing.expect(!parse_bool("Q")); +} +test { + try std.testing.expect(!parse_bool("")); +} +test { + try std.testing.expect(!parse_bool(null)); } diff --git a/src/parse_int.zig b/src/parse_int.zig index 70f91c4c5a40915e65dd09f5123bb86748553c71..a601095adf97c1f012f55d799c1de58ecc3f7dbe 100644 --- a/src/parse_int.zig +++ b/src/parse_int.zig @@ -6,3 +6,16 @@ pub fn parse_int(comptime T: type, s: ?string, b: u8, d: T) T { if (s == null) return d; return std.fmt.parseInt(T, s.?, b) catch d; } + +test { + try std.testing.expect(parse_int(u32, "25", 10, 13) == 25); +} +test { + try std.testing.expect(parse_int(u32, "Q", 10, 13) == 13); +} +test { + try std.testing.expect(parse_int(u32, "", 10, 13) == 13); +} +test { + try std.testing.expect(parse_int(u32, null, 10, 13) == 13); +} diff --git a/src/to_hex.zig b/src/to_hex.zig index 3c8cc996a381aa57812db98924d3930dab284560..354381ef88e55c493f51512db8d1930be07f1f68 100644 --- a/src/to_hex.zig +++ b/src/to_hex.zig @@ -1,6 +1,7 @@ const std = @import("std"); const string = []const u8; const extras = @import("./lib.zig"); +const rawInt = extras.rawInt; pub fn to_hex(array: anytype) [array.len * 2]u8 { var res: [array.len * 2]u8 = undefined; @@ -8,3 +9,7 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 { std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable; return res; } + +test { + try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132")); +}