mirror of
https://github.com/dart-lang/sdk
synced 2024-09-15 23:59:47 +00:00
02a8b015ad
Change-Id: I1783f624c7ce7d3b962c301c27ac0f4422d7c28f Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/126824 Commit-Queue: Bob Nystrom <rnystrom@google.com> Auto-Submit: Bob Nystrom <rnystrom@google.com> Reviewed-by: Lasse R.H. Nielsen <lrn@google.com>
63 lines
1.7 KiB
Dart
63 lines
1.7 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';
|
|
|
|
class MyList extends ListBase {
|
|
List list;
|
|
MyList(this.list);
|
|
|
|
get length => list.length;
|
|
set length(val) {
|
|
list.length = val;
|
|
}
|
|
|
|
operator [](index) => list[index];
|
|
operator []=(index, val) => list[index] = val;
|
|
|
|
String toString() => "[" + join(", ") + "]";
|
|
}
|
|
|
|
// l1 must be a modifiable list with 5 elements from 0 to 4.
|
|
void testModifiableList(l1) {
|
|
// Index must be integer and in range.
|
|
Expect.throwsRangeError(() => l1.insert(-1, 5), "negative");
|
|
Expect.throwsRangeError(() => l1.insert(6, 5), "too large");
|
|
Expect.throws(() => l1.insert(null, 5));
|
|
Expect.throws(() => l1.insert("1", 5));
|
|
Expect.throws(() => l1.insert(1.5, 5));
|
|
|
|
l1.insert(5, 5);
|
|
Expect.equals(6, l1.length);
|
|
Expect.equals(5, l1[5]);
|
|
Expect.equals("[0, 1, 2, 3, 4, 5]", l1.toString());
|
|
|
|
l1.insert(0, -1);
|
|
Expect.equals(7, l1.length);
|
|
Expect.equals(-1, l1[0]);
|
|
Expect.equals("[-1, 0, 1, 2, 3, 4, 5]", l1.toString());
|
|
}
|
|
|
|
void main() {
|
|
// Normal modifiable list.
|
|
testModifiableList([0, 1, 2, 3, 4]);
|
|
testModifiableList(new MyList([0, 1, 2, 3, 4]));
|
|
|
|
// Fixed size list.
|
|
var l2 = new List(5);
|
|
for (var i = 0; i < 5; i++) l2[i] = i;
|
|
Expect.throwsUnsupportedError(() => l2.insert(2, 5), "fixed-length");
|
|
|
|
// Unmodifiable list.
|
|
var l3 = const [0, 1, 2, 3, 4];
|
|
Expect.throwsUnsupportedError(() => l3.insert(2, 5), "unmodifiable");
|
|
|
|
// Empty list is not special.
|
|
var l4 = [];
|
|
l4.insert(0, 499);
|
|
Expect.equals(1, l4.length);
|
|
Expect.equals(499, l4[0]);
|
|
}
|