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 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
45
56pub fn FieldUnion(comptime T: type) type {
67 const infos = std.meta.fields(T);
......@@ -20,3 +21,18 @@ pub fn FieldUnion(comptime T: type) type {
2021 .decls = &.{},
2122 } });
2223}
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 {
4040 },
4141 );
4242}
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 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const expectSimilarType = extras.expectSimilarType;
45
56pub fn ReverseFields(comptime T: type) type {
67 var info = @typeInfo(T).Struct;
......@@ -14,13 +15,14 @@ pub fn ReverseFields(comptime T: type) type {
1415}
1516
1617test {
17 const A = struct { x: u8, y: u16 };
18 const B = ReverseFields(A);
19 const fields = std.meta.fields(B);
20
21 try std.testing.expect(fields.len == 2);
22 try std.testing.expect(fields[0].type == u16);
23 try std.testing.expect(std.mem.eql(u8, fields[0].name, "y"));
24 try std.testing.expect(fields[1].type == u8);
25 try std.testing.expect(std.mem.eql(u8, fields[1].name, "x"));
18 try expectSimilarType(
19 ReverseFields(struct {
20 x: u8,
21 y: u16,
22 }),
23 struct {
24 y: u16,
25 x: u8,
26 },
27 );
2628}
src/expectSimilarType.zig+37
......@@ -28,6 +28,43 @@ pub fn expectSimilarType(comptime A: type, comptime B: type) !void {
2828
2929 return;
3030 }
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 }
3168 @compileError("not implemented");
3269}
3370
src/isArrayOf.zig+14
......@@ -13,3 +13,17 @@ pub fn isArrayOf(comptime T: type) std.meta.trait.TraitFn {
1313 };
1414 return Closure.trait;
1515}
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 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const parse_int = extras.parse_int;
45
56pub 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));
724}
src/parse_int.zig+13
......@@ -6,3 +6,16 @@ pub fn parse_int(comptime T: type, s: ?string, b: u8, d: T) T {
66 if (s == null) return d;
77 return std.fmt.parseInt(T, s.?, b) catch d;
88}
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 @@
11const std = @import("std");
22const string = []const u8;
33const extras = @import("./lib.zig");
4const rawInt = extras.rawInt;
45
56pub fn to_hex(array: anytype) [array.len * 2]u8 {
67 var res: [array.len * 2]u8 = undefined;
......@@ -8,3 +9,7 @@ pub fn to_hex(array: anytype) [array.len * 2]u8 {
89 std.fmt.format(fbs.writer(), "{x}", .{std.fmt.fmtSliceHexLower(&array)}) catch unreachable;
910 return res;
1011}
12
13test {
14 try std.testing.expect(std.mem.eql(u8, &to_hex(std.mem.toBytes(rawInt(u64, 0x4e5a7da9f3f1d132))), "4e5a7da9f3f1d132"));
15}