dart-sdk/tests/corelib/list_insert_all_test.dart
Liam Appelbe 57de801b70 [nnbd/test] Fix some strong mode tests.
error_stack_trace2_test: As part of the late field spec, we no longer
have a special error for cyclic statics, so they just end up with stack
overflow errors, which don't have a stack trace.

list_..._test: Modify ListMixin<T> in NNBD libs so that addAll,
replaceRange, and insertAll work for non-nullable T.

Change-Id: I0f3f0c30f1aa45ea148eb5cdc954f024b7ba4777
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/141252
Commit-Queue: Liam Appelbe <liama@google.com>
Reviewed-by: Alexander Markov <alexmarkov@google.com>
Reviewed-by: Régis Crelier <regis@google.com>
2020-03-31 18:14:45 +00:00

94 lines
3.2 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.
import "package:expect/expect.dart";
import "dart:collection";
test(List<int> list, int index, Iterable<int> iterable) {
List copy = list.toList();
list.insertAll(index, iterable);
List iterableList = iterable.toList();
Expect.equals(copy.length + iterableList.length, list.length);
for (int i = 0; i < index; i++) {
Expect.equals(copy[i], list[i]);
}
for (int i = 0; i < iterableList.length; i++) {
Expect.equals(iterableList[i], list[i + index]);
}
for (int i = index + iterableList.length; i < copy.length; i++) {
Expect.equals(copy[i], list[i + iterableList.length]);
}
}
class MyList<T> extends ListBase<T> {
List<T> list;
MyList(this.list);
get length => list.length;
set length(value) {
list.length = value;
}
operator [](index) => list[index];
operator []=(index, val) {
list[index] = val;
}
void add(T element) {
list.add(element);
}
toString() => list.toString();
}
main() {
test([1, 2, 3], 0, [4, 5]);
test([1, 2, 3], 1, [4, 5]);
test([1, 2, 3], 2, [4, 5]);
test([1, 2, 3], 3, [4, 5]);
test([1, 2, 3], 2, [4]);
test([1, 2, 3], 3, []);
test([1, 2, 3], 0, [4, 5].map((x) => x));
test([1, 2, 3], 1, [4, 5].map((x) => x));
test([1, 2, 3], 2, [4, 5].map((x) => x));
test([1, 2, 3], 3, [4, 5].map((x) => x));
test([1, 2, 3], 2, [4].map((x) => x));
test([1, 2, 3], 3, [].map((x) => x));
test([1, 2, 3], 0, const [4, 5]);
test([1, 2, 3], 1, const [4, 5]);
test([1, 2, 3], 2, const [4, 5]);
test([1, 2, 3], 3, const [4, 5]);
test([1, 2, 3], 2, const [4]);
test([1, 2, 3], 3, const []);
test([1, 2, 3], 0, new Iterable.generate(2, (x) => x + 4));
test([1, 2, 3], 1, new Iterable.generate(2, (x) => x + 4));
test([1, 2, 3], 2, new Iterable.generate(2, (x) => x + 4));
test([1, 2, 3], 3, new Iterable.generate(2, (x) => x + 4));
test([1, 2, 3], 2, new Iterable.generate(1, (x) => x + 4));
test([1, 2, 3], 3, new Iterable.generate(0, (x) => x + 4));
test(new MyList([1, 2, 3]), 0, [4, 5]);
test(new MyList([1, 2, 3]), 1, [4, 5]);
test(new MyList([1, 2, 3]), 2, [4]);
test(new MyList([1, 2, 3]), 3, []);
test(new MyList([1, 2, 3]), 2, [4, 5]);
test(new MyList([1, 2, 3]), 3, [4, 5]);
test(new MyList([1, 2, 3]), 0, [4, 5].map((x) => x));
test(new MyList([1, 2, 3]), 1, [4, 5].map((x) => x));
test(new MyList([1, 2, 3]), 2, [4, 5].map((x) => x));
test(new MyList([1, 2, 3]), 3, [4, 5].map((x) => x));
test(new MyList([1, 2, 3]), 2, [4].map((x) => x));
test(new MyList([1, 2, 3]), 3, [].map((x) => x));
test(new MyList([]), 0, []);
test(new MyList([]), 0, [4]);
test(new MyList([]), 0, [4, 5]);
test(new MyList([1]), 0, [4, 5]);
test(new MyList([1]), 1, [4, 5]);
Expect.throwsRangeError(() => test([1, 2, 3], -1, [4, 5]));
Expect.throwsUnsupportedError(
() => test([1, 2, 3].toList(growable: false), -1, [4, 5]));
Expect.throwsRangeError(() => test(new MyList([1, 2, 3]), -1, [4, 5]));
Expect.throwsUnsupportedError(
() => test([1, 2, 3].toList(growable: false), 0, [4, 5]));
}