Skip to content

Commit 8d335e5

Browse files
rakudramaCommit Bot
authored and
Commit Bot
committed
[lib/collection] Test for #48282
Change-Id: I62306feab09f8dcd0335daa296b6c744bc139e62 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/231336 Reviewed-by: Lasse Nielsen <[email protected]> Commit-Queue: Stephen Adams <[email protected]>
1 parent e31691b commit 8d335e5

File tree

2 files changed

+150
-0
lines changed

2 files changed

+150
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
import 'dart:collection';
6+
7+
import 'package:expect/expect.dart';
8+
9+
// Test that a Map's keys, values and entries iterables are consistent with
10+
// their map.
11+
//
12+
// While it is generally not permitted to modify a map while iterating the keys,
13+
// values, or entries, it is possible to iterate these collections in between
14+
// modifications of the map.
15+
//
16+
// See #48282
17+
18+
void check(String kind, Map m) {
19+
Expect.equals(0, m.length);
20+
21+
// These existing iterables
22+
final keys = m.keys;
23+
final values = m.values;
24+
final entries = m.entries;
25+
26+
for (int i = 0; i < 20; i++) {
27+
Expect.equals(i, m.length);
28+
29+
// Fresh iterables.
30+
Expect.equals(i, m.keys.length);
31+
Expect.equals(i, m.values.length);
32+
Expect.equals(i, m.entries.length);
33+
34+
Expect.equals(i, List.of(m.keys).length);
35+
Expect.equals(i, List.of(m.values).length);
36+
Expect.equals(i, List.of(m.entries).length);
37+
38+
Expect.equals(i, iteratedLength(m.keys));
39+
Expect.equals(i, iteratedLength(m.values));
40+
Expect.equals(i, iteratedLength(m.entries));
41+
42+
// Existing iterables.
43+
Expect.equals(i, keys.length);
44+
Expect.equals(i, values.length);
45+
Expect.equals(i, entries.length);
46+
47+
Expect.equals(i, List.of(keys).length);
48+
Expect.equals(i, List.of(values).length);
49+
Expect.equals(i, List.of(entries).length);
50+
51+
Expect.equals(i, iteratedLength(keys));
52+
Expect.equals(i, iteratedLength(values));
53+
Expect.equals(i, iteratedLength(entries));
54+
55+
m[i] = i;
56+
}
57+
}
58+
59+
int iteratedLength(Iterable iterable) {
60+
int length = 0;
61+
final iterator = iterable.iterator;
62+
while (iterator.moveNext()) {
63+
length++;
64+
}
65+
return length;
66+
}
67+
68+
void main() {
69+
check('Map', {});
70+
check('Map.identity', Map.identity());
71+
check('HashMap', HashMap());
72+
check('HashMap.identity', HashMap.identity());
73+
check('SplayTreeMap', SplayTreeMap());
74+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
// Copyright (c) 2022, the Dart project authors. Please see the AUTHORS file
2+
// for details. All rights reserved. Use of this source code is governed by a
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
// @dart = 2.9
6+
7+
import 'dart:collection';
8+
9+
import 'package:expect/expect.dart';
10+
11+
// Test that a Map's keys, values and entries iterables are consistent with
12+
// their map.
13+
//
14+
// While it is generally not permitted to modify a map while iterating the keys,
15+
// values, or entries, it is possible to iterate these collections in between
16+
// modifications of the map.
17+
//
18+
// See #48282
19+
20+
void check(String kind, Map m) {
21+
Expect.equals(0, m.length);
22+
23+
// These existing iterables
24+
final keys = m.keys;
25+
final values = m.values;
26+
final entries = m.entries;
27+
28+
for (int i = 0; i < 20; i++) {
29+
Expect.equals(i, m.length);
30+
31+
// Fresh iterables.
32+
Expect.equals(i, m.keys.length);
33+
Expect.equals(i, m.values.length);
34+
Expect.equals(i, m.entries.length);
35+
36+
Expect.equals(i, List.of(m.keys).length);
37+
Expect.equals(i, List.of(m.values).length);
38+
Expect.equals(i, List.of(m.entries).length);
39+
40+
Expect.equals(i, iteratedLength(m.keys));
41+
Expect.equals(i, iteratedLength(m.values));
42+
Expect.equals(i, iteratedLength(m.entries));
43+
44+
// Existing iterables.
45+
Expect.equals(i, keys.length);
46+
Expect.equals(i, values.length);
47+
Expect.equals(i, entries.length);
48+
49+
Expect.equals(i, List.of(keys).length);
50+
Expect.equals(i, List.of(values).length);
51+
Expect.equals(i, List.of(entries).length);
52+
53+
Expect.equals(i, iteratedLength(keys));
54+
Expect.equals(i, iteratedLength(values));
55+
Expect.equals(i, iteratedLength(entries));
56+
57+
m[i] = i;
58+
}
59+
}
60+
61+
int iteratedLength(Iterable iterable) {
62+
int length = 0;
63+
final iterator = iterable.iterator;
64+
while (iterator.moveNext()) {
65+
length++;
66+
}
67+
return length;
68+
}
69+
70+
void main() {
71+
check('Map', {});
72+
check('Map.identity', Map.identity());
73+
check('HashMap', HashMap());
74+
check('HashMap.identity', HashMap.identity());
75+
check('SplayTreeMap', SplayTreeMap());
76+
}

0 commit comments

Comments
 (0)