| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // Dart test program for testing arrays. | |
| 5 | |
| 6 class ListTest { | |
| 7 static void TestIterator() { | |
| 8 List<int> a = new List<int>(10); | |
| 9 int count = 0; | |
| 10 | |
| 11 // Basic iteration over ObjectList. | |
| 12 for (int elem in a) { | |
| 13 Expect.equals(null, elem); | |
| 14 count++; | |
| 15 } | |
| 16 Expect.equals(10, count); | |
| 17 | |
| 18 // List length is 0. | |
| 19 List<int> fa = new List<int>(); | |
| 20 count = 0; | |
| 21 for (int elem in fa) { | |
| 22 count++; | |
| 23 } | |
| 24 Expect.equals(0, count); | |
| 25 | |
| 26 // Iterate over ImmutableList. | |
| 27 List<int> ca = const [0, 1, 2, 3, 4, 5]; | |
| 28 int sum = 0; | |
| 29 for (int elem in ca) { | |
| 30 sum += elem; | |
| 31 fa.add(elem); | |
| 32 } | |
| 33 Expect.equals(15, sum); | |
| 34 | |
| 35 // Iterate over List. | |
| 36 int sum2 = 0; | |
| 37 for (int elem in fa) { | |
| 38 sum2 += elem; | |
| 39 } | |
| 40 Expect.equals(sum, sum2); | |
| 41 } | |
| 42 | |
| 43 static void testMain() { | |
| 44 int len = 10; | |
| 45 List a = new List(len); | |
| 46 Expect.equals(true, a is List); | |
| 47 Expect.equals(len, a.length); | |
| 48 a.forEach(f(element) { Expect.equals(null, element); }); | |
| 49 a[1] = 1; | |
| 50 Expect.equals(1, a[1]); | |
| 51 bool exception_caught = false; | |
| 52 try { | |
| 53 var x = a[len]; | |
| 54 } catch (IndexOutOfRangeException e) { | |
| 55 exception_caught = true; | |
| 56 } | |
| 57 Expect.equals(true, exception_caught); | |
| 58 | |
| 59 exception_caught = false; | |
| 60 try { | |
| 61 List a = new List(4); | |
| 62 a.copyFrom(a, null, 1, 1); | |
| 63 } catch (IllegalArgumentException e) { | |
| 64 exception_caught = true; | |
| 65 } | |
| 66 Expect.equals(true, exception_caught); | |
| 67 | |
| 68 exception_caught = false; | |
| 69 try { | |
| 70 List a = new List(4); | |
| 71 a.copyFrom(a, 10, 1, 1); | |
| 72 } catch (IndexOutOfRangeException e) { | |
| 73 exception_caught = true; | |
| 74 } | |
| 75 Expect.equals(true, exception_caught); | |
| 76 | |
| 77 exception_caught = false; | |
| 78 try { | |
| 79 List a = new List(4); | |
| 80 List b = new List(4); | |
| 81 b.copyFrom(a, 0, 0, 4); | |
| 82 } catch (var e) { | |
| 83 exception_caught = true; | |
| 84 } | |
| 85 Expect.equals(false, exception_caught); | |
| 86 | |
| 87 List<int> unsorted = [4, 3, 9, 12, -4, 9]; | |
| 88 int compare(a, b) { | |
| 89 if (a < b) return -1; | |
| 90 if (a > b) return 1; | |
| 91 return 0; | |
| 92 } | |
| 93 unsorted.sort(compare); | |
| 94 Expect.equals(6, unsorted.length); | |
| 95 Expect.equals(-4, unsorted[0]); | |
| 96 Expect.equals(12, unsorted[unsorted.length - 1]); | |
| 97 int compare2(a, b) { | |
| 98 if (a < b) return 1; | |
| 99 if (a > b) return -1; | |
| 100 return 0; | |
| 101 } | |
| 102 unsorted.sort(compare2); | |
| 103 Expect.equals(12, unsorted[0]); | |
| 104 Expect.equals(-4, unsorted[unsorted.length - 1]); | |
| 105 Set<int> t = new Set<int>.from(unsorted); | |
| 106 Expect.equals(true, t.contains(9)); | |
| 107 Expect.equals(true, t.contains(-4)); | |
| 108 Expect.equals(false, t.contains(-3)); | |
| 109 Expect.equals(6, unsorted.length); | |
| 110 Expect.equals(5, t.length); | |
| 111 TestIterator(); | |
| 112 int element = unsorted[2]; | |
| 113 Expect.equals(9, element); | |
| 114 bool exceptionCaught = false; | |
| 115 try { | |
| 116 element = unsorted[2.1]; | |
| 117 } catch (IllegalArgumentException e) { | |
| 118 exceptionCaught = true; | |
| 119 } catch (TypeError e) { | |
| 120 // For type checked mode. | |
| 121 exceptionCaught = true; | |
| 122 } | |
| 123 Expect.equals(true, exceptionCaught); | |
| 124 | |
| 125 exceptionCaught = false; | |
| 126 try { | |
| 127 var a = new List(-1); | |
| 128 } catch (Exception e) { // Must agree which exception to throw. | |
| 129 exceptionCaught = true; | |
| 130 } | |
| 131 Expect.equals(true, exceptionCaught); | |
| 132 | |
| 133 exceptionCaught = false; | |
| 134 try { | |
| 135 var a = new List(99999999999999999999999); // Non-Smi. | |
| 136 } catch (Exception e) { // Must agree which exception to throw. | |
| 137 exceptionCaught = true; | |
| 138 } | |
| 139 Expect.equals(true, exceptionCaught); | |
| 140 | |
| 141 exceptionCaught = false; | |
| 142 List list = new List(); | |
| 143 try { | |
| 144 list.removeLast(); | |
| 145 } catch (IndexOutOfRangeException e) { | |
| 146 exceptionCaught = true; | |
| 147 } | |
| 148 Expect.equals(0, list.length); | |
| 149 Expect.equals(true, exceptionCaught); | |
| 150 } | |
| 151 } | |
| 152 | |
| 153 main() { | |
| 154 ListTest.testMain(); | |
| 155 } | |
| OLD | NEW |