OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 // Test GrowableArray.dart. | 4 // Test GrowableArray.dart. |
5 | 5 |
6 #library("GrowableObjectArray2Test.dart"); | 6 #library("GrowableObjectArray2Test.dart"); |
7 #import("dart:coreimpl"); | 7 #import("dart:coreimpl"); |
8 | 8 |
9 class GrowableObjectArray2Test { | 9 class GrowableObjectArray2Test { |
10 static testMain() { | 10 static testMain() { |
11 var g = new GrowableObjectArray(); | 11 var g = new GrowableObjectArray(); |
12 Expect.equals(true, g is Array); | 12 Expect.equals(true, g is List); |
13 Expect.equals(true, g is GrowableObjectArray); | 13 Expect.equals(true, g is GrowableObjectArray); |
14 Expect.equals(true, g.isEmpty()); | 14 Expect.equals(true, g.isEmpty()); |
15 for (int i = 0; i < 100; i++) { | 15 for (int i = 0; i < 100; i++) { |
16 g.add(1); | 16 g.add(1); |
17 } | 17 } |
18 g.add(1001); | 18 g.add(1001); |
19 Expect.equals(101, g.length); | 19 Expect.equals(101, g.length); |
20 Expect.equals(1001, g[100]); | 20 Expect.equals(1001, g[100]); |
21 Expect.equals(false, g.isEmpty()); | 21 Expect.equals(false, g.isEmpty()); |
22 Expect.equals(1001, g.last()); | 22 Expect.equals(1001, g.last()); |
23 Expect.equals(1001, g.removeLast()); | 23 Expect.equals(1001, g.removeLast()); |
24 Expect.equals(100, g.length); | 24 Expect.equals(100, g.length); |
25 | 25 |
26 var f = new GrowableObjectArray(); | 26 var f = new GrowableObjectArray(); |
27 var object_array = new Array(20); | 27 var list = new List(20); |
28 for (int i = 0; i < object_array.length; i++) { | 28 for (int i = 0; i < list.length; i++) { |
29 f.add(2); | 29 f.add(2); |
30 object_array[i] = 5; | 30 list[i] = 5; |
31 } | 31 } |
32 object_array.copyFrom(f, 0, 0, f.length); | 32 list.copyFrom(f, 0, 0, f.length); |
33 for (int i = 0; i < f.length; i++) { | 33 for (int i = 0; i < f.length; i++) { |
34 Expect.equals(2, object_array[i]); | 34 Expect.equals(2, list[i]); |
35 } | 35 } |
36 f.copyFrom(g, 10, 0, 2); | 36 f.copyFrom(g, 10, 0, 2); |
37 Expect.equals(20, f.length); | 37 Expect.equals(20, f.length); |
38 | 38 |
39 bool exception_caught = false; | 39 bool exceptionCaught = false; |
40 try { | 40 try { |
41 var elem = g[g.length]; | 41 var elem = g[g.length]; |
42 } catch (IndexOutOfRangeException e) { | 42 } catch (IndexOutOfRangeException e) { |
43 exception_caught = true; | 43 exceptionCaught = true; |
44 } | 44 } |
45 Expect.equals(true, exception_caught); | 45 Expect.equals(true, exceptionCaught); |
46 | 46 |
47 Array<int> plain_array = [4, 3, 9, 12, -4, 9]; | 47 List<int> plainArray = [4, 3, 9, 12, -4, 9]; |
48 GrowableObjectArray h = new GrowableObjectArray.withCapacity(4); | 48 GrowableObjectArray h = new GrowableObjectArray.withCapacity(4); |
49 plain_array.forEach((elem) { h.add(elem); }); | 49 plainArray.forEach((elem) { h.add(elem); }); |
50 int compare(a, b) { | 50 int compare(a, b) { |
51 if (a < b) return -1; | 51 if (a < b) return -1; |
52 if (a > b) return 1; | 52 if (a > b) return 1; |
53 return 0; | 53 return 0; |
54 } | 54 } |
55 h.sort(compare); | 55 h.sort(compare); |
56 Expect.equals(6, h.length); | 56 Expect.equals(6, h.length); |
57 Expect.equals(-4, h[0]); | 57 Expect.equals(-4, h[0]); |
58 Expect.equals(12, h[h.length - 1]); | 58 Expect.equals(12, h[h.length - 1]); |
59 Set<int> t = new Set<int>.from(h); | 59 Set<int> t = new Set<int>.from(h); |
60 Expect.equals(true, t.contains(9)); | 60 Expect.equals(true, t.contains(9)); |
61 Expect.equals(true, t.contains(-4)); | 61 Expect.equals(true, t.contains(-4)); |
62 Expect.equals(false, t.contains(-3)); | 62 Expect.equals(false, t.contains(-3)); |
63 Expect.equals(5, t.length); | 63 Expect.equals(5, t.length); |
64 | 64 |
65 h.clear(); | 65 h.clear(); |
66 Array array = const [0, 1, 2, 3, 4]; | 66 List array = const [0, 1, 2, 3, 4]; |
67 h.addAll(array); | 67 h.addAll(array); |
68 Expect.equals(5, h.length); | 68 Expect.equals(5, h.length); |
69 } | 69 } |
70 } | 70 } |
71 | 71 |
72 main() { | 72 main() { |
73 GrowableObjectArray2Test.testMain(); | 73 GrowableObjectArray2Test.testMain(); |
74 } | 74 } |
OLD | NEW |