| 1 | const std = @import("std"); |
| 2 | const string = []const u8; |
| 3 | const extras = @import("./lib.zig"); |
| 4 | |
| 5 | pub fn expectSimilarType(comptime A: type, comptime B: type) !void { |
| 6 | const info_a = @typeInfo(A); |
| 7 | const info_b = @typeInfo(B); |
| 8 | try std.testing.expect(std.meta.activeTag(info_a) == std.meta.activeTag(info_b)); |
| 9 | |
| 10 | if (info_a == .@"struct") { |
| 11 | const info_a_s = info_a.@"struct"; |
| 12 | const info_b_s = info_b.@"struct"; |
| 13 | |
| 14 | try std.testing.expect(info_a_s.layout == info_b_s.layout); |
| 15 | try std.testing.expect(info_a_s.is_tuple == info_b_s.is_tuple); |
| 16 | try std.testing.expect(info_a_s.backing_integer == info_b_s.backing_integer); |
| 17 | |
| 18 | inline for (info_a_s.fields, info_b_s.fields) |fa, fb| { |
| 19 | try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); |
| 20 | try expectSimilarType(fa.type, fb.type); |
| 21 | try std.testing.expect(fa.alignment == fb.alignment); |
| 22 | try std.testing.expect(fa.is_comptime == fb.is_comptime); |
| 23 | } |
| 24 | |
| 25 | return; |
| 26 | } |
| 27 | if (info_a == .@"union") { |
| 28 | const info_a_u = info_a.@"union"; |
| 29 | const info_b_u = info_b.@"union"; |
| 30 | |
| 31 | try std.testing.expect(info_a_u.layout == info_b_u.layout); |
| 32 | try expectSimilarType(info_a_u.tag_type.?, info_b_u.tag_type.?); |
| 33 | |
| 34 | inline for (info_a_u.fields, info_b_u.fields) |fa, fb| { |
| 35 | try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); |
| 36 | try expectSimilarType(fa.type, fb.type); |
| 37 | try std.testing.expect(fa.alignment == fb.alignment); |
| 38 | } |
| 39 | |
| 40 | return; |
| 41 | } |
| 42 | if (info_a == .@"enum") { |
| 43 | const info_a_e = info_a.@"enum"; |
| 44 | const info_b_e = info_b.@"enum"; |
| 45 | |
| 46 | try std.testing.expect(info_a_e.tag_type == info_b_e.tag_type); |
| 47 | try std.testing.expect(info_a_e.is_exhaustive == info_b_e.is_exhaustive); |
| 48 | |
| 49 | inline for (info_a_e.fields, info_b_e.fields) |fa, fb| { |
| 50 | try std.testing.expect(std.mem.eql(u8, fa.name, fb.name)); |
| 51 | try std.testing.expect(fa.value == fb.value); |
| 52 | } |
| 53 | |
| 54 | return; |
| 55 | } |
| 56 | try std.testing.expect(A == B); |
| 57 | } |
| 58 | |
| 59 | test { |
| 60 | try expectSimilarType( |
| 61 | struct { a: u32, b: u8, c: u16 }, |
| 62 | struct { a: u32, b: u8, c: u16 }, |
| 63 | ); |
| 64 | } |
| 65 | |
| 66 | test { |
| 67 | try expectSimilarType( |
| 68 | union(enum) { a: u32, b: u8, c: u16 }, |
| 69 | union(enum) { a: u32, b: u8, c: u16 }, |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | test { |
| 74 | try expectSimilarType( |
| 75 | enum { a, b, c, d }, |
| 76 | enum { a, b, c, d }, |
| 77 | ); |
| 78 | } |