Skip to content

Commit ea6946c

Browse files
committed
std.EnumArray: edit return of iterator.next()
1 parent f43f89a commit ea6946c

File tree

2 files changed

+125
-110
lines changed

2 files changed

+125
-110
lines changed

lib/std/enums.zig

Lines changed: 120 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,115 @@ pub fn EnumSet(comptime E: type) type {
468468
};
469469
}
470470

471+
test "pure EnumSet fns" {
472+
const Suit = enum { spades, hearts, clubs, diamonds };
473+
474+
const empty = EnumSet(Suit).initEmpty();
475+
const full = EnumSet(Suit).initFull();
476+
const black = EnumSet(Suit).initMany(&[_]Suit{ .spades, .clubs });
477+
const red = EnumSet(Suit).initMany(&[_]Suit{ .hearts, .diamonds });
478+
479+
try testing.expect(empty.eql(empty));
480+
try testing.expect(full.eql(full));
481+
try testing.expect(!empty.eql(full));
482+
try testing.expect(!full.eql(empty));
483+
try testing.expect(!empty.eql(black));
484+
try testing.expect(!full.eql(red));
485+
try testing.expect(!red.eql(empty));
486+
try testing.expect(!black.eql(full));
487+
488+
try testing.expect(empty.subsetOf(empty));
489+
try testing.expect(empty.subsetOf(full));
490+
try testing.expect(full.subsetOf(full));
491+
try testing.expect(!black.subsetOf(red));
492+
try testing.expect(!red.subsetOf(black));
493+
494+
try testing.expect(full.supersetOf(full));
495+
try testing.expect(full.supersetOf(empty));
496+
try testing.expect(empty.supersetOf(empty));
497+
try testing.expect(!black.supersetOf(red));
498+
try testing.expect(!red.supersetOf(black));
499+
500+
try testing.expect(empty.complement().eql(full));
501+
try testing.expect(full.complement().eql(empty));
502+
try testing.expect(black.complement().eql(red));
503+
try testing.expect(red.complement().eql(black));
504+
505+
try testing.expect(empty.unionWith(empty).eql(empty));
506+
try testing.expect(empty.unionWith(full).eql(full));
507+
try testing.expect(full.unionWith(full).eql(full));
508+
try testing.expect(full.unionWith(empty).eql(full));
509+
try testing.expect(black.unionWith(red).eql(full));
510+
try testing.expect(red.unionWith(black).eql(full));
511+
512+
try testing.expect(empty.intersectWith(empty).eql(empty));
513+
try testing.expect(empty.intersectWith(full).eql(empty));
514+
try testing.expect(full.intersectWith(full).eql(full));
515+
try testing.expect(full.intersectWith(empty).eql(empty));
516+
try testing.expect(black.intersectWith(red).eql(empty));
517+
try testing.expect(red.intersectWith(black).eql(empty));
518+
519+
try testing.expect(empty.xorWith(empty).eql(empty));
520+
try testing.expect(empty.xorWith(full).eql(full));
521+
try testing.expect(full.xorWith(full).eql(empty));
522+
try testing.expect(full.xorWith(empty).eql(full));
523+
try testing.expect(black.xorWith(red).eql(full));
524+
try testing.expect(red.xorWith(black).eql(full));
525+
526+
try testing.expect(empty.differenceWith(empty).eql(empty));
527+
try testing.expect(empty.differenceWith(full).eql(empty));
528+
try testing.expect(full.differenceWith(full).eql(empty));
529+
try testing.expect(full.differenceWith(empty).eql(full));
530+
try testing.expect(full.differenceWith(red).eql(black));
531+
try testing.expect(full.differenceWith(black).eql(red));
532+
}
533+
534+
test "EnumSet empty" {
535+
const E = enum {};
536+
const empty = EnumSet(E).initEmpty();
537+
const full = EnumSet(E).initFull();
538+
539+
try testing.expect(empty.eql(full));
540+
try testing.expect(empty.complement().eql(full));
541+
try testing.expect(empty.complement().eql(full.complement()));
542+
try testing.expect(empty.eql(full.complement()));
543+
}
544+
545+
test "EnumSet const iterator" {
546+
const Direction = enum { up, down, left, right };
547+
const diag_move = init: {
548+
var move = EnumSet(Direction).initEmpty();
549+
move.insert(.right);
550+
move.insert(.up);
551+
break :init move;
552+
};
553+
554+
var result = EnumSet(Direction).initEmpty();
555+
var it = diag_move.iterator();
556+
while (it.next()) |dir| {
557+
result.insert(dir);
558+
}
559+
560+
try testing.expect(result.eql(diag_move));
561+
}
562+
563+
test "EnumSet non-exhaustive" {
564+
const BitIndices = enum(u4) {
565+
a = 0,
566+
b = 1,
567+
c = 4,
568+
_,
569+
};
570+
const BitField = EnumSet(BitIndices);
571+
572+
var flags = BitField.init(.{ .a = true, .b = true });
573+
flags.insert(.c);
574+
flags.remove(.a);
575+
try testing.expect(!flags.contains(.a));
576+
try testing.expect(flags.contains(.b));
577+
try testing.expect(flags.contains(.c));
578+
}
579+
471580
/// A map keyed by an enum, backed by a bitfield and a dense array.
472581
/// If the enum is exhaustive but not dense, a mapping will be constructed from
473582
/// enum values to dense indices. This type does no dynamic
@@ -1183,7 +1292,7 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
11831292
key: Key,
11841293

11851294
/// A pointer to the value in the array associated
1186-
/// with this key. Modifications through this
1295+
/// with this key. Modifications through this
11871296
/// pointer will modify the underlying data.
11881297
value: *Value,
11891298
};
@@ -1196,7 +1305,7 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
11961305
const index = self.index;
11971306
if (index < Indexer.count) {
11981307
self.index += 1;
1199-
return Entry{
1308+
return .{
12001309
.key = Indexer.keyForIndex(index),
12011310
.value = &self.values[index],
12021311
};
@@ -1207,113 +1316,17 @@ pub fn EnumArray(comptime E: type, comptime V: type) type {
12071316
};
12081317
}
12091318

1210-
test "pure EnumSet fns" {
1211-
const Suit = enum { spades, hearts, clubs, diamonds };
1212-
1213-
const empty = EnumSet(Suit).initEmpty();
1214-
const full = EnumSet(Suit).initFull();
1215-
const black = EnumSet(Suit).initMany(&[_]Suit{ .spades, .clubs });
1216-
const red = EnumSet(Suit).initMany(&[_]Suit{ .hearts, .diamonds });
1217-
1218-
try testing.expect(empty.eql(empty));
1219-
try testing.expect(full.eql(full));
1220-
try testing.expect(!empty.eql(full));
1221-
try testing.expect(!full.eql(empty));
1222-
try testing.expect(!empty.eql(black));
1223-
try testing.expect(!full.eql(red));
1224-
try testing.expect(!red.eql(empty));
1225-
try testing.expect(!black.eql(full));
1226-
1227-
try testing.expect(empty.subsetOf(empty));
1228-
try testing.expect(empty.subsetOf(full));
1229-
try testing.expect(full.subsetOf(full));
1230-
try testing.expect(!black.subsetOf(red));
1231-
try testing.expect(!red.subsetOf(black));
1232-
1233-
try testing.expect(full.supersetOf(full));
1234-
try testing.expect(full.supersetOf(empty));
1235-
try testing.expect(empty.supersetOf(empty));
1236-
try testing.expect(!black.supersetOf(red));
1237-
try testing.expect(!red.supersetOf(black));
1238-
1239-
try testing.expect(empty.complement().eql(full));
1240-
try testing.expect(full.complement().eql(empty));
1241-
try testing.expect(black.complement().eql(red));
1242-
try testing.expect(red.complement().eql(black));
1243-
1244-
try testing.expect(empty.unionWith(empty).eql(empty));
1245-
try testing.expect(empty.unionWith(full).eql(full));
1246-
try testing.expect(full.unionWith(full).eql(full));
1247-
try testing.expect(full.unionWith(empty).eql(full));
1248-
try testing.expect(black.unionWith(red).eql(full));
1249-
try testing.expect(red.unionWith(black).eql(full));
1250-
1251-
try testing.expect(empty.intersectWith(empty).eql(empty));
1252-
try testing.expect(empty.intersectWith(full).eql(empty));
1253-
try testing.expect(full.intersectWith(full).eql(full));
1254-
try testing.expect(full.intersectWith(empty).eql(empty));
1255-
try testing.expect(black.intersectWith(red).eql(empty));
1256-
try testing.expect(red.intersectWith(black).eql(empty));
1257-
1258-
try testing.expect(empty.xorWith(empty).eql(empty));
1259-
try testing.expect(empty.xorWith(full).eql(full));
1260-
try testing.expect(full.xorWith(full).eql(empty));
1261-
try testing.expect(full.xorWith(empty).eql(full));
1262-
try testing.expect(black.xorWith(red).eql(full));
1263-
try testing.expect(red.xorWith(black).eql(full));
1264-
1265-
try testing.expect(empty.differenceWith(empty).eql(empty));
1266-
try testing.expect(empty.differenceWith(full).eql(empty));
1267-
try testing.expect(full.differenceWith(full).eql(empty));
1268-
try testing.expect(full.differenceWith(empty).eql(full));
1269-
try testing.expect(full.differenceWith(red).eql(black));
1270-
try testing.expect(full.differenceWith(black).eql(red));
1271-
}
1272-
1273-
test "EnumSet empty" {
1274-
const E = enum {};
1275-
const empty = EnumSet(E).initEmpty();
1276-
const full = EnumSet(E).initFull();
1277-
1278-
try std.testing.expect(empty.eql(full));
1279-
try std.testing.expect(empty.complement().eql(full));
1280-
try std.testing.expect(empty.complement().eql(full.complement()));
1281-
try std.testing.expect(empty.eql(full.complement()));
1282-
}
1283-
1284-
test "EnumSet const iterator" {
1285-
const Direction = enum { up, down, left, right };
1286-
const diag_move = init: {
1287-
var move = EnumSet(Direction).initEmpty();
1288-
move.insert(.right);
1289-
move.insert(.up);
1290-
break :init move;
1291-
};
1292-
1293-
var result = EnumSet(Direction).initEmpty();
1294-
var it = diag_move.iterator();
1295-
while (it.next()) |dir| {
1296-
result.insert(dir);
1297-
}
1298-
1299-
try testing.expect(result.eql(diag_move));
1300-
}
1319+
test "EnumArray iterator for one field" {
1320+
const Enum0 = std.meta.FieldEnum(struct { x: u32 });
1321+
const Enum1 = enum { x };
13011322

1302-
test "EnumSet non-exhaustive" {
1303-
const BitIndices = enum(u4) {
1304-
a = 0,
1305-
b = 1,
1306-
c = 4,
1307-
_,
1308-
};
1309-
const BitField = EnumSet(BitIndices);
1323+
var a = std.EnumArray(Enum0, u32).initFill(123);
1324+
var it0 = a.iterator();
1325+
try testing.expectEqual(it0.next().?.key, Enum0.x);
13101326

1311-
var flags = BitField.init(.{ .a = true, .b = true });
1312-
flags.insert(.c);
1313-
flags.remove(.a);
1314-
try testing.expect(!flags.contains(.a));
1315-
try testing.expect(flags.contains(.b));
1316-
try testing.expect(flags.contains(.c));
1327+
var b = std.EnumArray(Enum1, u32).initFill(123);
1328+
var it1 = b.iterator();
1329+
try testing.expectEqual(it1.next().?.key, Enum1.x);
13171330
}
13181331

13191332
pub fn EnumIndexer(comptime E: type) type {

test/behavior/type.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,16 +381,18 @@ test "Type.Enum" {
381381
try testing.expectEqual(@as(u32, 5), @intFromEnum(Bar.b));
382382
try testing.expectEqual(@as(u32, 6), @intFromEnum(@as(Bar, @enumFromInt(6))));
383383

384-
{ // from https://github.com/ziglang/zig/issues/19985
384+
{ // https://github.com/ziglang/zig/issues/19985
385385
{ // enum with single field can be initialized.
386386
const E = @Type(.{ .@"enum" = .{
387387
.tag_type = u0,
388388
.is_exhaustive = true,
389389
.fields = &.{.{ .name = "foo", .value = 0 }},
390390
.decls = &.{},
391391
} });
392-
const s: struct { E } = .{.foo};
393-
try testing.expectEqual(.foo, s[0]);
392+
const s0: struct { E } = .{.foo};
393+
const s1: struct { k: E } = .{ .k = .foo };
394+
try testing.expectEqual(.foo, s0[0]);
395+
try testing.expectEqual(.foo, s1.k);
394396
}
395397

396398
{ // meta.FieldEnum() with single field

0 commit comments

Comments
 (0)