| ... | @@ -18,7 +18,7 @@ const EnumField = std.builtin.TypeInfo.EnumField; | ... | @@ -18,7 +18,7 @@ const EnumField = std.builtin.TypeInfo.EnumField; |
| 18 | pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_default: ?Data) type { | 18 | pub fn EnumFieldStruct(comptime E: type, comptime Data: type, comptime field_default: ?Data) type { |
| 19 | const StructField = std.builtin.TypeInfo.StructField; | 19 | const StructField = std.builtin.TypeInfo.StructField; |
| 20 | var fields: []const StructField = &[_]StructField{}; | 20 | var fields: []const StructField = &[_]StructField{}; |
| 21 | for (uniqueFields(E)) |field, i| { | 21 | for (std.meta.fields(E)) |field, i| { |
| 22 | fields = fields ++ &[_]StructField{.{ | 22 | fields = fields ++ &[_]StructField{.{ |
| 23 | .name = field.name, | 23 | .name = field.name, |
| 24 | .field_type = Data, | 24 | .field_type = Data, |
| ... | @@ -48,72 +48,12 @@ pub fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) [] | ... | @@ -48,72 +48,12 @@ pub fn valuesFromFields(comptime E: type, comptime fields: []const EnumField) [] |
| 48 | } | 48 | } |
| 49 | } | 49 | } |
| 50 | | 50 | |
| 51 | test "std.enums.valuesFromFields" { | | |
| 52 | const E = extern enum { a, b, c, d = 0 }; | | |
| 53 | const fields = valuesFromFields(E, &[_]EnumField{ | | |
| 54 | .{ .name = "b", .value = undefined }, | | |
| 55 | .{ .name = "a", .value = undefined }, | | |
| 56 | .{ .name = "a", .value = undefined }, | | |
| 57 | .{ .name = "d", .value = undefined }, | | |
| 58 | }); | | |
| 59 | testing.expectEqual(E.b, fields[0]); | | |
| 60 | testing.expectEqual(E.a, fields[1]); | | |
| 61 | testing.expectEqual(E.d, fields[2]); // a == d | | |
| 62 | testing.expectEqual(E.d, fields[3]); | | |
| 63 | } | | |
| 64 | | | |
| 65 | /// Returns the set of all named values in the given enum, in | 51 | /// Returns the set of all named values in the given enum, in |
| 66 | /// declaration order. | 52 | /// declaration order. |
| 67 | pub fn values(comptime E: type) []const E { | 53 | pub fn values(comptime E: type) []const E { |
| 68 | return comptime valuesFromFields(E, @typeInfo(E).Enum.fields); | 54 | return comptime valuesFromFields(E, @typeInfo(E).Enum.fields); |
| 69 | } | 55 | } |
| 70 | | 56 | |
| 71 | test "std.enum.values" { | | |
| 72 | const E = extern enum { a, b, c, d = 0 }; | | |
| 73 | testing.expectEqualSlices(E, &.{ .a, .b, .c, .d }, values(E)); | | |
| 74 | } | | |
| 75 | | | |
| 76 | /// Returns the set of all unique named values in the given enum, in | | |
| 77 | /// declaration order. For repeated values in extern enums, only the | | |
| 78 | /// first name for each value is included. | | |
| 79 | pub fn uniqueValues(comptime E: type) []const E { | | |
| 80 | return comptime valuesFromFields(E, uniqueFields(E)); | | |
| 81 | } | | |
| 82 | | | |
| 83 | test "std.enum.uniqueValues" { | | |
| 84 | const E = extern enum { a, b, c, d = 0, e, f = 3 }; | | |
| 85 | testing.expectEqualSlices(E, &.{ .a, .b, .c, .f }, uniqueValues(E)); | | |
| 86 | | | |
| 87 | const F = enum { a, b, c }; | | |
| 88 | testing.expectEqualSlices(F, &.{ .a, .b, .c }, uniqueValues(F)); | | |
| 89 | } | | |
| 90 | | | |
| 91 | /// Returns the set of all unique field values in the given enum, in | | |
| 92 | /// declaration order. For repeated values in extern enums, only the | | |
| 93 | /// first name for each value is included. | | |
| 94 | pub fn uniqueFields(comptime E: type) []const EnumField { | | |
| 95 | comptime { | | |
| 96 | const info = @typeInfo(E).Enum; | | |
| 97 | const raw_fields = info.fields; | | |
| 98 | // Only extern enums can contain duplicates, | | |
| 99 | // so fast path other types. | | |
| 100 | if (info.layout != .Extern) { | | |
| 101 | return raw_fields; | | |
| 102 | } | | |
| 103 | | | |
| 104 | var unique_fields: []const EnumField = &[_]EnumField{}; | | |
| 105 | outer: for (raw_fields) |candidate| { | | |
| 106 | for (unique_fields) |u| { | | |
| 107 | if (u.value == candidate.value) | | |
| 108 | continue :outer; | | |
| 109 | } | | |
| 110 | unique_fields = unique_fields ++ &[_]EnumField{candidate}; | | |
| 111 | } | | |
| 112 | | | |
| 113 | return unique_fields; | | |
| 114 | } | | |
| 115 | } | | |
| 116 | | | |
| 117 | /// Determines the length of a direct-mapped enum array, indexed by | 57 | /// Determines the length of a direct-mapped enum array, indexed by |
| 118 | /// @intCast(usize, @enumToInt(enum_value)). | 58 | /// @intCast(usize, @enumToInt(enum_value)). |
| 119 | /// If the enum is non-exhaustive, the resulting length will only be enough | 59 | /// If the enum is non-exhaustive, the resulting length will only be enough |
| ... | @@ -126,7 +66,7 @@ pub fn uniqueFields(comptime E: type) []const EnumField { | ... | @@ -126,7 +66,7 @@ pub fn uniqueFields(comptime E: type) []const EnumField { |
| 126 | fn directEnumArrayLen(comptime E: type, comptime max_unused_slots: comptime_int) comptime_int { | 66 | fn directEnumArrayLen(comptime E: type, comptime max_unused_slots: comptime_int) comptime_int { |
| 127 | var max_value: comptime_int = -1; | 67 | var max_value: comptime_int = -1; |
| 128 | const max_usize: comptime_int = ~@as(usize, 0); | 68 | const max_usize: comptime_int = ~@as(usize, 0); |
| 129 | const fields = uniqueFields(E); | 69 | const fields = std.meta.fields(E); |
| 130 | for (fields) |f| { | 70 | for (fields) |f| { |
| 131 | if (f.value < 0) { | 71 | if (f.value < 0) { |
| 132 | @compileError("Cannot create a direct enum array for " ++ @typeName(E) ++ ", field ." ++ f.name ++ " has a negative value."); | 72 | @compileError("Cannot create a direct enum array for " ++ @typeName(E) ++ ", field ." ++ f.name ++ " has a negative value."); |
| ... | @@ -248,8 +188,8 @@ pub fn nameCast(comptime E: type, comptime value: anytype) E { | ... | @@ -248,8 +188,8 @@ pub fn nameCast(comptime E: type, comptime value: anytype) E { |
| 248 | } | 188 | } |
| 249 | | 189 | |
| 250 | test "std.enums.nameCast" { | 190 | test "std.enums.nameCast" { |
| 251 | const A = enum { a = 0, b = 1 }; | 191 | const A = enum(u1) { a = 0, b = 1 }; |
| 252 | const B = enum { a = 1, b = 0 }; | 192 | const B = enum(u1) { a = 1, b = 0 }; |
| 253 | testing.expectEqual(A.a, nameCast(A, .a)); | 193 | testing.expectEqual(A.a, nameCast(A, .a)); |
| 254 | testing.expectEqual(A.a, nameCast(A, A.a)); | 194 | testing.expectEqual(A.a, nameCast(A, A.a)); |
| 255 | testing.expectEqual(A.a, nameCast(A, B.a)); | 195 | testing.expectEqual(A.a, nameCast(A, B.a)); |
| ... | @@ -796,7 +736,7 @@ pub fn EnumIndexer(comptime E: type) type { | ... | @@ -796,7 +736,7 @@ pub fn EnumIndexer(comptime E: type) type { |
| 796 | @compileError("Cannot create an enum indexer for a non-exhaustive enum."); | 736 | @compileError("Cannot create an enum indexer for a non-exhaustive enum."); |
| 797 | } | 737 | } |
| 798 | | 738 | |
| 799 | const const_fields = uniqueFields(E); | 739 | const const_fields = std.meta.fields(E); |
| 800 | var fields = const_fields[0..const_fields.len].*; | 740 | var fields = const_fields[0..const_fields.len].*; |
| 801 | if (fields.len == 0) { | 741 | if (fields.len == 0) { |
| 802 | return struct { | 742 | return struct { |
| ... | @@ -848,7 +788,7 @@ pub fn EnumIndexer(comptime E: type) type { | ... | @@ -848,7 +788,7 @@ pub fn EnumIndexer(comptime E: type) type { |
| 848 | } | 788 | } |
| 849 | | 789 | |
| 850 | test "std.enums.EnumIndexer dense zeroed" { | 790 | test "std.enums.EnumIndexer dense zeroed" { |
| 851 | const E = enum { b = 1, a = 0, c = 2 }; | 791 | const E = enum(u2) { b = 1, a = 0, c = 2 }; |
| 852 | const Indexer = EnumIndexer(E); | 792 | const Indexer = EnumIndexer(E); |
| 853 | ensureIndexer(Indexer); | 793 | ensureIndexer(Indexer); |
| 854 | testing.expectEqual(E, Indexer.Key); | 794 | testing.expectEqual(E, Indexer.Key); |
| ... | @@ -910,379 +850,3 @@ test "std.enums.EnumIndexer sparse" { | ... | @@ -910,379 +850,3 @@ test "std.enums.EnumIndexer sparse" { |
| 910 | testing.expectEqual(E.b, Indexer.keyForIndex(1)); | 850 | testing.expectEqual(E.b, Indexer.keyForIndex(1)); |
| 911 | testing.expectEqual(E.c, Indexer.keyForIndex(2)); | 851 | testing.expectEqual(E.c, Indexer.keyForIndex(2)); |
| 912 | } | 852 | } |
| 913 | | | |
| 914 | test "std.enums.EnumIndexer repeats" { | | |
| 915 | const E = extern enum { a = -2, c = 6, b = 4, b2 = 4 }; | | |
| 916 | const Indexer = EnumIndexer(E); | | |
| 917 | ensureIndexer(Indexer); | | |
| 918 | testing.expectEqual(E, Indexer.Key); | | |
| 919 | testing.expectEqual(@as(usize, 3), Indexer.count); | | |
| 920 | | | |
| 921 | testing.expectEqual(@as(usize, 0), Indexer.indexOf(.a)); | | |
| 922 | testing.expectEqual(@as(usize, 1), Indexer.indexOf(.b)); | | |
| 923 | testing.expectEqual(@as(usize, 2), Indexer.indexOf(.c)); | | |
| 924 | | | |
| 925 | testing.expectEqual(E.a, Indexer.keyForIndex(0)); | | |
| 926 | testing.expectEqual(E.b, Indexer.keyForIndex(1)); | | |
| 927 | testing.expectEqual(E.c, Indexer.keyForIndex(2)); | | |
| 928 | } | | |
| 929 | | | |
| 930 | test "std.enums.EnumSet" { | | |
| 931 | const E = extern enum { a, b, c, d, e = 0 }; | | |
| 932 | const Set = EnumSet(E); | | |
| 933 | testing.expectEqual(E, Set.Key); | | |
| 934 | testing.expectEqual(EnumIndexer(E), Set.Indexer); | | |
| 935 | testing.expectEqual(@as(usize, 4), Set.len); | | |
| 936 | | | |
| 937 | // Empty sets | | |
| 938 | const empty = Set{}; | | |
| 939 | comptime testing.expect(empty.count() == 0); | | |
| 940 | | | |
| 941 | var empty_b = Set.init(.{}); | | |
| 942 | testing.expect(empty_b.count() == 0); | | |
| 943 | | | |
| 944 | const empty_c = comptime Set.init(.{}); | | |
| 945 | comptime testing.expect(empty_c.count() == 0); | | |
| 946 | | | |
| 947 | const full = Set.initFull(); | | |
| 948 | testing.expect(full.count() == Set.len); | | |
| 949 | | | |
| 950 | const full_b = comptime Set.initFull(); | | |
| 951 | comptime testing.expect(full_b.count() == Set.len); | | |
| 952 | | | |
| 953 | testing.expectEqual(false, empty.contains(.a)); | | |
| 954 | testing.expectEqual(false, empty.contains(.b)); | | |
| 955 | testing.expectEqual(false, empty.contains(.c)); | | |
| 956 | testing.expectEqual(false, empty.contains(.d)); | | |
| 957 | testing.expectEqual(false, empty.contains(.e)); | | |
| 958 | { | | |
| 959 | var iter = empty_b.iterator(); | | |
| 960 | testing.expectEqual(@as(?E, null), iter.next()); | | |
| 961 | } | | |
| 962 | | | |
| 963 | var mut = Set.init(.{ | | |
| 964 | .a = true, | | |
| 965 | .c = true, | | |
| 966 | }); | | |
| 967 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 968 | testing.expectEqual(true, mut.contains(.a)); | | |
| 969 | testing.expectEqual(false, mut.contains(.b)); | | |
| 970 | testing.expectEqual(true, mut.contains(.c)); | | |
| 971 | testing.expectEqual(false, mut.contains(.d)); | | |
| 972 | testing.expectEqual(true, mut.contains(.e)); // aliases a | | |
| 973 | { | | |
| 974 | var it = mut.iterator(); | | |
| 975 | testing.expectEqual(@as(?E, .a), it.next()); | | |
| 976 | testing.expectEqual(@as(?E, .c), it.next()); | | |
| 977 | testing.expectEqual(@as(?E, null), it.next()); | | |
| 978 | } | | |
| 979 | | | |
| 980 | mut.toggleAll(); | | |
| 981 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 982 | testing.expectEqual(false, mut.contains(.a)); | | |
| 983 | testing.expectEqual(true, mut.contains(.b)); | | |
| 984 | testing.expectEqual(false, mut.contains(.c)); | | |
| 985 | testing.expectEqual(true, mut.contains(.d)); | | |
| 986 | testing.expectEqual(false, mut.contains(.e)); // aliases a | | |
| 987 | { | | |
| 988 | var it = mut.iterator(); | | |
| 989 | testing.expectEqual(@as(?E, .b), it.next()); | | |
| 990 | testing.expectEqual(@as(?E, .d), it.next()); | | |
| 991 | testing.expectEqual(@as(?E, null), it.next()); | | |
| 992 | } | | |
| 993 | | | |
| 994 | mut.toggleSet(Set.init(.{ .a = true, .b = true })); | | |
| 995 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 996 | testing.expectEqual(true, mut.contains(.a)); | | |
| 997 | testing.expectEqual(false, mut.contains(.b)); | | |
| 998 | testing.expectEqual(false, mut.contains(.c)); | | |
| 999 | testing.expectEqual(true, mut.contains(.d)); | | |
| 1000 | testing.expectEqual(true, mut.contains(.e)); // aliases a | | |
| 1001 | | | |
| 1002 | mut.setUnion(Set.init(.{ .a = true, .b = true })); | | |
| 1003 | testing.expectEqual(@as(usize, 3), mut.count()); | | |
| 1004 | testing.expectEqual(true, mut.contains(.a)); | | |
| 1005 | testing.expectEqual(true, mut.contains(.b)); | | |
| 1006 | testing.expectEqual(false, mut.contains(.c)); | | |
| 1007 | testing.expectEqual(true, mut.contains(.d)); | | |
| 1008 | | | |
| 1009 | mut.remove(.c); | | |
| 1010 | mut.remove(.b); | | |
| 1011 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 1012 | testing.expectEqual(true, mut.contains(.a)); | | |
| 1013 | testing.expectEqual(false, mut.contains(.b)); | | |
| 1014 | testing.expectEqual(false, mut.contains(.c)); | | |
| 1015 | testing.expectEqual(true, mut.contains(.d)); | | |
| 1016 | | | |
| 1017 | mut.setIntersection(Set.init(.{ .a = true, .b = true })); | | |
| 1018 | testing.expectEqual(@as(usize, 1), mut.count()); | | |
| 1019 | testing.expectEqual(true, mut.contains(.a)); | | |
| 1020 | testing.expectEqual(false, mut.contains(.b)); | | |
| 1021 | testing.expectEqual(false, mut.contains(.c)); | | |
| 1022 | testing.expectEqual(false, mut.contains(.d)); | | |
| 1023 | | | |
| 1024 | mut.insert(.a); | | |
| 1025 | mut.insert(.b); | | |
| 1026 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 1027 | testing.expectEqual(true, mut.contains(.a)); | | |
| 1028 | testing.expectEqual(true, mut.contains(.b)); | | |
| 1029 | testing.expectEqual(false, mut.contains(.c)); | | |
| 1030 | testing.expectEqual(false, mut.contains(.d)); | | |
| 1031 | | | |
| 1032 | mut.setPresent(.a, false); | | |
| 1033 | mut.toggle(.b); | | |
| 1034 | mut.toggle(.c); | | |
| 1035 | mut.setPresent(.d, true); | | |
| 1036 | testing.expectEqual(@as(usize, 2), mut.count()); | | |
| 1037 | testing.expectEqual(false, mut.contains(.a)); | | |
| 1038 | testing.expectEqual(false, mut.contains(.b)); | | |
| 1039 | testing.expectEqual(true, mut.contains(.c)); | | |
| 1040 | testing.expectEqual(true, mut.contains(.d)); | | |
| 1041 | } | | |
| 1042 | | | |
| 1043 | test "std.enums.EnumArray void" { | | |
| 1044 | const E = extern enum { a, b, c, d, e = 0 }; | | |
| 1045 | const ArrayVoid = EnumArray(E, void); | | |
| 1046 | testing.expectEqual(E, ArrayVoid.Key); | | |
| 1047 | testing.expectEqual(EnumIndexer(E), ArrayVoid.Indexer); | | |
| 1048 | testing.expectEqual(void, ArrayVoid.Value); | | |
| 1049 | testing.expectEqual(@as(usize, 4), ArrayVoid.len); | | |
| 1050 | | | |
| 1051 | const undef = ArrayVoid.initUndefined(); | | |
| 1052 | var inst = ArrayVoid.initFill({}); | | |
| 1053 | const inst2 = ArrayVoid.init(.{ .a = {}, .b = {}, .c = {}, .d = {} }); | | |
| 1054 | const inst3 = ArrayVoid.initDefault({}, .{}); | | |
| 1055 | | | |
| 1056 | _ = inst.get(.a); | | |
| 1057 | _ = inst.getPtr(.b); | | |
| 1058 | _ = inst.getPtrConst(.c); | | |
| 1059 | inst.set(.a, {}); | | |
| 1060 | | | |
| 1061 | var it = inst.iterator(); | | |
| 1062 | testing.expectEqual(E.a, it.next().?.key); | | |
| 1063 | testing.expectEqual(E.b, it.next().?.key); | | |
| 1064 | testing.expectEqual(E.c, it.next().?.key); | | |
| 1065 | testing.expectEqual(E.d, it.next().?.key); | | |
| 1066 | testing.expect(it.next() == null); | | |
| 1067 | } | | |
| 1068 | | | |
| 1069 | test "std.enums.EnumArray sized" { | | |
| 1070 | const E = extern enum { a, b, c, d, e = 0 }; | | |
| 1071 | const Array = EnumArray(E, usize); | | |
| 1072 | testing.expectEqual(E, Array.Key); | | |
| 1073 | testing.expectEqual(EnumIndexer(E), Array.Indexer); | | |
| 1074 | testing.expectEqual(usize, Array.Value); | | |
| 1075 | testing.expectEqual(@as(usize, 4), Array.len); | | |
| 1076 | | | |
| 1077 | const undef = Array.initUndefined(); | | |
| 1078 | var inst = Array.initFill(5); | | |
| 1079 | const inst2 = Array.init(.{ .a = 1, .b = 2, .c = 3, .d = 4 }); | | |
| 1080 | const inst3 = Array.initDefault(6, .{ .b = 4, .c = 2 }); | | |
| 1081 | | | |
| 1082 | testing.expectEqual(@as(usize, 5), inst.get(.a)); | | |
| 1083 | testing.expectEqual(@as(usize, 5), inst.get(.b)); | | |
| 1084 | testing.expectEqual(@as(usize, 5), inst.get(.c)); | | |
| 1085 | testing.expectEqual(@as(usize, 5), inst.get(.d)); | | |
| 1086 | | | |
| 1087 | testing.expectEqual(@as(usize, 1), inst2.get(.a)); | | |
| 1088 | testing.expectEqual(@as(usize, 2), inst2.get(.b)); | | |
| 1089 | testing.expectEqual(@as(usize, 3), inst2.get(.c)); | | |
| 1090 | testing.expectEqual(@as(usize, 4), inst2.get(.d)); | | |
| 1091 | | | |
| 1092 | testing.expectEqual(@as(usize, 6), inst3.get(.a)); | | |
| 1093 | testing.expectEqual(@as(usize, 4), inst3.get(.b)); | | |
| 1094 | testing.expectEqual(@as(usize, 2), inst3.get(.c)); | | |
| 1095 | testing.expectEqual(@as(usize, 6), inst3.get(.d)); | | |
| 1096 | | | |
| 1097 | testing.expectEqual(&inst.values[0], inst.getPtr(.a)); | | |
| 1098 | testing.expectEqual(&inst.values[1], inst.getPtr(.b)); | | |
| 1099 | testing.expectEqual(&inst.values[2], inst.getPtr(.c)); | | |
| 1100 | testing.expectEqual(&inst.values[3], inst.getPtr(.d)); | | |
| 1101 | | | |
| 1102 | testing.expectEqual(@as(*const usize, &inst.values[0]), inst.getPtrConst(.a)); | | |
| 1103 | testing.expectEqual(@as(*const usize, &inst.values[1]), inst.getPtrConst(.b)); | | |
| 1104 | testing.expectEqual(@as(*const usize, &inst.values[2]), inst.getPtrConst(.c)); | | |
| 1105 | testing.expectEqual(@as(*const usize, &inst.values[3]), inst.getPtrConst(.d)); | | |
| 1106 | | | |
| 1107 | inst.set(.c, 8); | | |
| 1108 | testing.expectEqual(@as(usize, 5), inst.get(.a)); | | |
| 1109 | testing.expectEqual(@as(usize, 5), inst.get(.b)); | | |
| 1110 | testing.expectEqual(@as(usize, 8), inst.get(.c)); | | |
| 1111 | testing.expectEqual(@as(usize, 5), inst.get(.d)); | | |
| 1112 | | | |
| 1113 | var it = inst.iterator(); | | |
| 1114 | const Entry = Array.Entry; | | |
| 1115 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1116 | .key = .a, | | |
| 1117 | .value = &inst.values[0], | | |
| 1118 | }), it.next()); | | |
| 1119 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1120 | .key = .b, | | |
| 1121 | .value = &inst.values[1], | | |
| 1122 | }), it.next()); | | |
| 1123 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1124 | .key = .c, | | |
| 1125 | .value = &inst.values[2], | | |
| 1126 | }), it.next()); | | |
| 1127 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1128 | .key = .d, | | |
| 1129 | .value = &inst.values[3], | | |
| 1130 | }), it.next()); | | |
| 1131 | testing.expectEqual(@as(?Entry, null), it.next()); | | |
| 1132 | } | | |
| 1133 | | | |
| 1134 | test "std.enums.EnumMap void" { | | |
| 1135 | const E = extern enum { a, b, c, d, e = 0 }; | | |
| 1136 | const Map = EnumMap(E, void); | | |
| 1137 | testing.expectEqual(E, Map.Key); | | |
| 1138 | testing.expectEqual(EnumIndexer(E), Map.Indexer); | | |
| 1139 | testing.expectEqual(void, Map.Value); | | |
| 1140 | testing.expectEqual(@as(usize, 4), Map.len); | | |
| 1141 | | | |
| 1142 | const b = Map.initFull({}); | | |
| 1143 | testing.expectEqual(@as(usize, 4), b.count()); | | |
| 1144 | | | |
| 1145 | const c = Map.initFullWith(.{ .a = {}, .b = {}, .c = {}, .d = {} }); | | |
| 1146 | testing.expectEqual(@as(usize, 4), c.count()); | | |
| 1147 | | | |
| 1148 | const d = Map.initFullWithDefault({}, .{ .b = {} }); | | |
| 1149 | testing.expectEqual(@as(usize, 4), d.count()); | | |
| 1150 | | | |
| 1151 | var a = Map.init(.{ .b = {}, .d = {} }); | | |
| 1152 | testing.expectEqual(@as(usize, 2), a.count()); | | |
| 1153 | testing.expectEqual(false, a.contains(.a)); | | |
| 1154 | testing.expectEqual(true, a.contains(.b)); | | |
| 1155 | testing.expectEqual(false, a.contains(.c)); | | |
| 1156 | testing.expectEqual(true, a.contains(.d)); | | |
| 1157 | testing.expect(a.get(.a) == null); | | |
| 1158 | testing.expect(a.get(.b) != null); | | |
| 1159 | testing.expect(a.get(.c) == null); | | |
| 1160 | testing.expect(a.get(.d) != null); | | |
| 1161 | testing.expect(a.getPtr(.a) == null); | | |
| 1162 | testing.expect(a.getPtr(.b) != null); | | |
| 1163 | testing.expect(a.getPtr(.c) == null); | | |
| 1164 | testing.expect(a.getPtr(.d) != null); | | |
| 1165 | testing.expect(a.getPtrConst(.a) == null); | | |
| 1166 | testing.expect(a.getPtrConst(.b) != null); | | |
| 1167 | testing.expect(a.getPtrConst(.c) == null); | | |
| 1168 | testing.expect(a.getPtrConst(.d) != null); | | |
| 1169 | _ = a.getPtrAssertContains(.b); | | |
| 1170 | _ = a.getAssertContains(.d); | | |
| 1171 | | | |
| 1172 | a.put(.a, {}); | | |
| 1173 | a.put(.a, {}); | | |
| 1174 | a.putUninitialized(.c).* = {}; | | |
| 1175 | a.putUninitialized(.c).* = {}; | | |
| 1176 | | | |
| 1177 | testing.expectEqual(@as(usize, 4), a.count()); | | |
| 1178 | testing.expect(a.get(.a) != null); | | |
| 1179 | testing.expect(a.get(.b) != null); | | |
| 1180 | testing.expect(a.get(.c) != null); | | |
| 1181 | testing.expect(a.get(.d) != null); | | |
| 1182 | | | |
| 1183 | a.remove(.a); | | |
| 1184 | _ = a.fetchRemove(.c); | | |
| 1185 | | | |
| 1186 | var iter = a.iterator(); | | |
| 1187 | const Entry = Map.Entry; | | |
| 1188 | testing.expectEqual(E.b, iter.next().?.key); | | |
| 1189 | testing.expectEqual(E.d, iter.next().?.key); | | |
| 1190 | testing.expect(iter.next() == null); | | |
| 1191 | } | | |
| 1192 | | | |
| 1193 | test "std.enums.EnumMap sized" { | | |
| 1194 | const E = extern enum { a, b, c, d, e = 0 }; | | |
| 1195 | const Map = EnumMap(E, usize); | | |
| 1196 | testing.expectEqual(E, Map.Key); | | |
| 1197 | testing.expectEqual(EnumIndexer(E), Map.Indexer); | | |
| 1198 | testing.expectEqual(usize, Map.Value); | | |
| 1199 | testing.expectEqual(@as(usize, 4), Map.len); | | |
| 1200 | | | |
| 1201 | const b = Map.initFull(5); | | |
| 1202 | testing.expectEqual(@as(usize, 4), b.count()); | | |
| 1203 | testing.expect(b.contains(.a)); | | |
| 1204 | testing.expect(b.contains(.b)); | | |
| 1205 | testing.expect(b.contains(.c)); | | |
| 1206 | testing.expect(b.contains(.d)); | | |
| 1207 | testing.expectEqual(@as(?usize, 5), b.get(.a)); | | |
| 1208 | testing.expectEqual(@as(?usize, 5), b.get(.b)); | | |
| 1209 | testing.expectEqual(@as(?usize, 5), b.get(.c)); | | |
| 1210 | testing.expectEqual(@as(?usize, 5), b.get(.d)); | | |
| 1211 | | | |
| 1212 | const c = Map.initFullWith(.{ .a = 1, .b = 2, .c = 3, .d = 4 }); | | |
| 1213 | testing.expectEqual(@as(usize, 4), c.count()); | | |
| 1214 | testing.expect(c.contains(.a)); | | |
| 1215 | testing.expect(c.contains(.b)); | | |
| 1216 | testing.expect(c.contains(.c)); | | |
| 1217 | testing.expect(c.contains(.d)); | | |
| 1218 | testing.expectEqual(@as(?usize, 1), c.get(.a)); | | |
| 1219 | testing.expectEqual(@as(?usize, 2), c.get(.b)); | | |
| 1220 | testing.expectEqual(@as(?usize, 3), c.get(.c)); | | |
| 1221 | testing.expectEqual(@as(?usize, 4), c.get(.d)); | | |
| 1222 | | | |
| 1223 | const d = Map.initFullWithDefault(6, .{ .b = 2, .c = 4 }); | | |
| 1224 | testing.expectEqual(@as(usize, 4), d.count()); | | |
| 1225 | testing.expect(d.contains(.a)); | | |
| 1226 | testing.expect(d.contains(.b)); | | |
| 1227 | testing.expect(d.contains(.c)); | | |
| 1228 | testing.expect(d.contains(.d)); | | |
| 1229 | testing.expectEqual(@as(?usize, 6), d.get(.a)); | | |
| 1230 | testing.expectEqual(@as(?usize, 2), d.get(.b)); | | |
| 1231 | testing.expectEqual(@as(?usize, 4), d.get(.c)); | | |
| 1232 | testing.expectEqual(@as(?usize, 6), d.get(.d)); | | |
| 1233 | | | |
| 1234 | var a = Map.init(.{ .b = 2, .d = 4 }); | | |
| 1235 | testing.expectEqual(@as(usize, 2), a.count()); | | |
| 1236 | testing.expectEqual(false, a.contains(.a)); | | |
| 1237 | testing.expectEqual(true, a.contains(.b)); | | |
| 1238 | testing.expectEqual(false, a.contains(.c)); | | |
| 1239 | testing.expectEqual(true, a.contains(.d)); | | |
| 1240 | | | |
| 1241 | testing.expectEqual(@as(?usize, null), a.get(.a)); | | |
| 1242 | testing.expectEqual(@as(?usize, 2), a.get(.b)); | | |
| 1243 | testing.expectEqual(@as(?usize, null), a.get(.c)); | | |
| 1244 | testing.expectEqual(@as(?usize, 4), a.get(.d)); | | |
| 1245 | | | |
| 1246 | testing.expectEqual(@as(?*usize, null), a.getPtr(.a)); | | |
| 1247 | testing.expectEqual(@as(?*usize, &a.values[1]), a.getPtr(.b)); | | |
| 1248 | testing.expectEqual(@as(?*usize, null), a.getPtr(.c)); | | |
| 1249 | testing.expectEqual(@as(?*usize, &a.values[3]), a.getPtr(.d)); | | |
| 1250 | | | |
| 1251 | testing.expectEqual(@as(?*const usize, null), a.getPtrConst(.a)); | | |
| 1252 | testing.expectEqual(@as(?*const usize, &a.values[1]), a.getPtrConst(.b)); | | |
| 1253 | testing.expectEqual(@as(?*const usize, null), a.getPtrConst(.c)); | | |
| 1254 | testing.expectEqual(@as(?*const usize, &a.values[3]), a.getPtrConst(.d)); | | |
| 1255 | | | |
| 1256 | testing.expectEqual(@as(*const usize, &a.values[1]), a.getPtrAssertContains(.b)); | | |
| 1257 | testing.expectEqual(@as(*const usize, &a.values[3]), a.getPtrAssertContains(.d)); | | |
| 1258 | testing.expectEqual(@as(usize, 2), a.getAssertContains(.b)); | | |
| 1259 | testing.expectEqual(@as(usize, 4), a.getAssertContains(.d)); | | |
| 1260 | | | |
| 1261 | a.put(.a, 3); | | |
| 1262 | a.put(.a, 5); | | |
| 1263 | a.putUninitialized(.c).* = 7; | | |
| 1264 | a.putUninitialized(.c).* = 9; | | |
| 1265 | | | |
| 1266 | testing.expectEqual(@as(usize, 4), a.count()); | | |
| 1267 | testing.expectEqual(@as(?usize, 5), a.get(.a)); | | |
| 1268 | testing.expectEqual(@as(?usize, 2), a.get(.b)); | | |
| 1269 | testing.expectEqual(@as(?usize, 9), a.get(.c)); | | |
| 1270 | testing.expectEqual(@as(?usize, 4), a.get(.d)); | | |
| 1271 | | | |
| 1272 | a.remove(.a); | | |
| 1273 | testing.expectEqual(@as(?usize, null), a.fetchRemove(.a)); | | |
| 1274 | testing.expectEqual(@as(?usize, 9), a.fetchRemove(.c)); | | |
| 1275 | a.remove(.c); | | |
| 1276 | | | |
| 1277 | var iter = a.iterator(); | | |
| 1278 | const Entry = Map.Entry; | | |
| 1279 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1280 | .key = .b, | | |
| 1281 | .value = &a.values[1], | | |
| 1282 | }), iter.next()); | | |
| 1283 | testing.expectEqual(@as(?Entry, Entry{ | | |
| 1284 | .key = .d, | | |
| 1285 | .value = &a.values[3], | | |
| 1286 | }), iter.next()); | | |
| 1287 | testing.expectEqual(@as(?Entry, null), iter.next()); | | |
| 1288 | } | | |