authorgravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 00:40:06 -08:00
committergravatar for hello@nektro.netMeghan Denny <hello@nektro.net> 2024-01-29 00:40:06 -08:00
log4461bc011659155d72810a9a8cd3b33129b0823d
treebfb65e0e185975e5ca27e911b311cb6e52f52af1
parenta1d0efbb3e97764e36caf81ac8f9ac93bb1e8a84

add more tests


8 files changed, 125 insertions(+), 10 deletions(-)

src/FieldUnion.zig+16
...@@ -1,6 +1,7 @@...@@ -1,6 +1,7 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
45
5pub fn FieldUnion(comptime T: type) type {6pub 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
25test {
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
44test {
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 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
45
5pub fn ReverseFields(comptime T: type) type {6pub 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}
1516
16test {17test {
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,
2021 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 {
2828
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}
3370
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
17const L = isArrayOf(u8);
18test {
19 try std.testing.expect(L([5]u8));
20}
21test {
22 try std.testing.expect(!L([3]u32));
23}
24test {
25 try std.testing.expect(!L(struct { a: u8 }));
26}
27test {
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 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const parse_int = extras.parse_int;
45
5pub fn parse_bool(s: ?string) bool {6pub 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
10test {
11 try std.testing.expect(parse_bool("1"));
12}
13test {
14 try std.testing.expect(!parse_bool("0"));
15}
16test {
17 try std.testing.expect(!parse_bool("Q"));
18}
19test {
20 try std.testing.expect(!parse_bool(""));
21}
22test {
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
10test {
11 try std.testing.expect(parse_int(u32, "25", 10, 13) == 25);
12}
13test {
14 try std.testing.expect(parse_int(u32, "Q", 10, 13) == 13);
15}
16test {
17 try std.testing.expect(parse_int(u32, "", 10, 13) == 13);
18}
19test {
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 @@
1const std = @import("std");1const std = @import("std");
2const string = []const u8;2const string = []const u8;
3const extras = @import("./lib.zig");3const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;
45
5pub fn to_hex(array: anytype) [array.len * 2]u8 {6pub 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
13test {
14 try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132"));
15}