dart-sdk/tests/corelib/iterable_return_type_test.dart
Robert Nystrom 284f662022 Migrate the corelib_2/ tests starting with "e" through "i".
Change-Id: I764d0457da85b2935e1d83309eab4c4b5b3ea000
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126204
Commit-Queue: Bob Nystrom <rnystrom@google.com>
Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
Auto-Submit: Bob Nystrom <rnystrom@google.com>
2019-12-02 23:19:10 +00:00

91 lines
3 KiB
Dart

// Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
// Regression test for dart2js where [List.addAll] was not typed
// correctly.
import "package:expect/expect.dart";
import 'dart:collection';
import 'dart:typed_data';
testIntIterable(iterable) {
Expect.isTrue(iterable is Iterable<int>, "${iterable.runtimeType}");
Expect.isFalse(iterable is Iterable<String>, "${iterable.runtimeType}");
}
void testIterable(Iterable<int> iterable, [int depth = 3]) {
testIntIterable(iterable);
if (depth > 0) {
testIterable(iterable.where((x) => true), depth - 1);
testIterable(iterable.skip(1), depth - 1);
testIterable(iterable.take(1), depth - 1);
testIterable(iterable.skipWhile((x) => false), depth - 1);
testIterable(iterable.takeWhile((x) => true), depth - 1);
testList(iterable.toList(growable: true), depth - 1);
testList(iterable.toList(growable: false), depth - 1);
testIterable(iterable.toSet(), depth - 1);
}
}
void testList(List<int> list, [int depth = 3]) {
testIterable(list, depth);
if (depth > 0) {
testIterable(list.getRange(0, list.length), depth - 1);
testIterable(list.reversed, depth - 1);
testMap(list.asMap(), depth - 1);
}
}
void testMap(Map<int, int> map, [int depth = 3]) {
Expect.isTrue(map is Map<int, int>);
Expect.isFalse(map is Map<int, String>);
Expect.isFalse(map is Map<String, int>);
if (depth > 0) {
testIterable(map.keys, depth - 1);
testIterable(map.values, depth - 1);
}
}
main() {
// Empty lists.
testList(<int>[]);
testList(new List<int>.empty());
testList(new List<int>());
testList(const <int>[]);
testList(new List<int>.generate(0, (x) => x + 1));
// Singleton lists.
testList(<int>[1]);
testList(new List<int>.filled(1, 1));
testList(new List<int>()..add(1));
testList(const <int>[1]);
testList(new List<int>.generate(1, (x) => x + 1));
// Typed lists.
testList(new Uint8List(1)..[0] = 1); // //# 01: ok
testList(new Int8List(1)..[0] = 1); // //# 01: continued
testList(new Uint16List(1)..[0] = 1); // //# 01: continued
testList(new Int16List(1)..[0] = 1); // //# 01: continued
testList(new Uint32List(1)..[0] = 1); // //# 01: continued
testList(new Int32List(1)..[0] = 1); // //# 01: continued
testList(new Uint64List(1)..[0] = 1); // //# 02: ok
testList(new Int64List(1)..[0] = 1); // //# 02: continued
testIterable(new Set<int>()..add(1));
testIterable(new HashSet<int>()..add(1));
testIterable(new LinkedHashSet<int>()..add(1));
testIterable(new SplayTreeSet<int>()..add(1));
testIterable(new Queue<int>()..add(1));
testIterable(new DoubleLinkedQueue<int>()..add(1));
testIterable(new ListQueue<int>()..add(1));
testMap(new Map<int, int>()..[1] = 1);
testMap(new HashMap<int, int>()..[1] = 1);
testMap(new LinkedHashMap<int, int>()..[1] = 1);
testMap(new SplayTreeMap<int, int>()..[1] = 1);
testMap(<int, int>{1: 1});
testMap(const <int, int>{1: 1});
}