| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 | 4 |
| 5 /// Tests algorithm utilities. | 5 /// Tests algorithm utilities. |
| 6 | 6 |
| 7 import "package:collection/collection.dart"; | 7 import "package:collection/collection.dart"; |
| 8 import "package:test/test.dart"; | 8 import "package:test/test.dart"; |
| 9 import 'dart:math'; | 9 import 'dart:math'; |
| 10 | 10 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 36 if (!const ListEquality().equals(c, l)) return; | 36 if (!const ListEquality().equals(c, l)) return; |
| 37 // Odds of not changing the order should be one in ~ 16! ~= 2e+13. | 37 // Odds of not changing the order should be one in ~ 16! ~= 2e+13. |
| 38 // Repeat this 10 times, and the odds of accidentally shuffling to the | 38 // Repeat this 10 times, and the odds of accidentally shuffling to the |
| 39 // same result every time is disappearingly tiny. | 39 // same result every time is disappearingly tiny. |
| 40 count++; | 40 count++; |
| 41 // If this happens even once, it's ok to report it. | 41 // If this happens even once, it's ok to report it. |
| 42 print("Failed shuffle $count times"); | 42 print("Failed shuffle $count times"); |
| 43 if (count == 10) fail("Shuffle didn't change order."); | 43 if (count == 10) fail("Shuffle didn't change order."); |
| 44 } while (true); | 44 } while (true); |
| 45 }); | 45 }); |
| 46 test("Shuffle sublist", (){ | 46 test("Shuffle sublist", () { |
| 47 List l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; | 47 List l = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16]; |
| 48 List c = l.toList(); | 48 List c = l.toList(); |
| 49 shuffle(l, 4, 12); | 49 shuffle(l, 4, 12); |
| 50 expect(const IterableEquality().equals(l.getRange(0, 4), | 50 expect(const IterableEquality().equals(l.getRange(0, 4), c.getRange(0, 4)), |
| 51 c.getRange(0, 4)), isTrue); | 51 isTrue); |
| 52 expect(const IterableEquality().equals(l.getRange(12, 16), | 52 expect( |
| 53 c.getRange(12, 16)), isTrue); | 53 const IterableEquality().equals(l.getRange(12, 16), c.getRange(12, 16)), |
| 54 expect(const UnorderedIterableEquality().equals(l.getRange(4, 12), | 54 isTrue); |
| 55 c.getRange(4, 12)), | 55 expect( |
| 56 isTrue); | 56 const UnorderedIterableEquality() |
| 57 | 57 .equals(l.getRange(4, 12), c.getRange(4, 12)), |
| 58 isTrue); |
| 58 }); | 59 }); |
| 59 | 60 |
| 60 test("binsearch0", () { | 61 test("binsearch0", () { |
| 61 expect(binarySearch([], 2), equals(-1)); | 62 expect(binarySearch([], 2), equals(-1)); |
| 62 }); | 63 }); |
| 63 | 64 |
| 64 test("binsearch1", () { | 65 test("binsearch1", () { |
| 65 expect(binarySearch([5], 2), equals(-1)); | 66 expect(binarySearch([5], 2), equals(-1)); |
| 66 expect(binarySearch([5], 5), equals(0)); | 67 expect(binarySearch([5], 5), equals(0)); |
| 67 expect(binarySearch([5], 7), equals(-1)); | 68 expect(binarySearch([5], 7), equals(-1)); |
| 68 }); | 69 }); |
| 69 | 70 |
| 70 test("binsearch3", () { | 71 test("binsearch3", () { |
| 71 expect(binarySearch([0, 5, 10], -1), equals(-1)); | 72 expect(binarySearch([0, 5, 10], -1), equals(-1)); |
| 72 expect(binarySearch([0, 5, 10], 0), equals(0)); | 73 expect(binarySearch([0, 5, 10], 0), equals(0)); |
| 73 expect(binarySearch([0, 5, 10], 2), equals(-1)); | 74 expect(binarySearch([0, 5, 10], 2), equals(-1)); |
| 74 expect(binarySearch([0, 5, 10], 5), equals(1)); | 75 expect(binarySearch([0, 5, 10], 5), equals(1)); |
| 75 expect(binarySearch([0, 5, 10], 7), equals(-1)); | 76 expect(binarySearch([0, 5, 10], 7), equals(-1)); |
| 76 expect(binarySearch([0, 5, 10], 10), equals(2)); | 77 expect(binarySearch([0, 5, 10], 10), equals(2)); |
| 77 expect(binarySearch([0, 5, 10], 12), equals(-1)); | 78 expect(binarySearch([0, 5, 10], 12), equals(-1)); |
| 78 }); | 79 }); |
| 79 | 80 |
| 80 test("binsearchCompare0", () { | 81 test("binsearchCompare0", () { |
| 81 expect(binarySearch([], new C(2), compare: compareC), equals(-1)); | 82 expect(binarySearch(<C>[], new C(2), compare: compareC), equals(-1)); |
| 82 }); | 83 }); |
| 83 | 84 |
| 84 test("binsearchCompare1", () { | 85 test("binsearchCompare1", () { |
| 85 var l1 = [new C(5)]; | 86 var l1 = [new C(5)]; |
| 86 expect(binarySearch(l1, new C(2), compare: compareC), equals(-1)); | 87 expect(binarySearch(l1, new C(2), compare: compareC), equals(-1)); |
| 87 expect(binarySearch(l1, new C(5), compare: compareC), equals(0)); | 88 expect(binarySearch(l1, new C(5), compare: compareC), equals(0)); |
| 88 expect(binarySearch(l1, new C(7), compare: compareC), equals(-1)); | 89 expect(binarySearch(l1, new C(7), compare: compareC), equals(-1)); |
| 89 }); | 90 }); |
| 90 | 91 |
| 91 test("binsearchCompare3", () { | 92 test("binsearchCompare3", () { |
| 92 var l3 = [new C(0), new C(5), new C(10)]; | 93 var l3 = [new C(0), new C(5), new C(10)]; |
| 93 expect(binarySearch(l3, new C(-1), compare: compareC), equals(-1)); | 94 expect(binarySearch(l3, new C(-1), compare: compareC), equals(-1)); |
| 94 expect(binarySearch(l3, new C(0), compare: compareC), equals(0)); | 95 expect(binarySearch(l3, new C(0), compare: compareC), equals(0)); |
| 95 expect(binarySearch(l3, new C(2), compare: compareC), equals(-1)); | 96 expect(binarySearch(l3, new C(2), compare: compareC), equals(-1)); |
| 96 expect(binarySearch(l3, new C(5), compare: compareC), equals(1)); | 97 expect(binarySearch(l3, new C(5), compare: compareC), equals(1)); |
| 97 expect(binarySearch(l3, new C(7), compare: compareC), equals(-1)); | 98 expect(binarySearch(l3, new C(7), compare: compareC), equals(-1)); |
| 98 expect(binarySearch(l3, new C(10), compare: compareC), equals(2)); | 99 expect(binarySearch(l3, new C(10), compare: compareC), equals(2)); |
| 99 expect(binarySearch(l3, new C(12), compare: compareC), equals(-1)); | 100 expect(binarySearch(l3, new C(12), compare: compareC), equals(-1)); |
| 100 }); | 101 }); |
| 101 | 102 |
| 103 test("lowerbound0", () { |
| 104 expect(lowerBound([], 2), equals(0)); |
| 105 }); |
| 106 |
| 107 test("lowerbound1", () { |
| 108 expect(lowerBound([5], 2), equals(0)); |
| 109 expect(lowerBound([5], 5), equals(0)); |
| 110 expect(lowerBound([5], 7), equals(1)); |
| 111 }); |
| 112 |
| 113 test("lowerbound3", () { |
| 114 expect(lowerBound([0, 5, 10], -1), equals(0)); |
| 115 expect(lowerBound([0, 5, 10], 0), equals(0)); |
| 116 expect(lowerBound([0, 5, 10], 2), equals(1)); |
| 117 expect(lowerBound([0, 5, 10], 5), equals(1)); |
| 118 expect(lowerBound([0, 5, 10], 7), equals(2)); |
| 119 expect(lowerBound([0, 5, 10], 10), equals(2)); |
| 120 expect(lowerBound([0, 5, 10], 12), equals(3)); |
| 121 }); |
| 122 |
| 123 test("lowerboundRepeat", () { |
| 124 expect(lowerBound([5, 5, 5], 5), equals(0)); |
| 125 expect(lowerBound([0, 5, 5, 5, 10], 5), equals(1)); |
| 126 }); |
| 127 |
| 128 test("lowerboundCompare0", () { |
| 129 expect(lowerBound(<C>[], new C(2), compare: compareC), equals(0)); |
| 130 }); |
| 131 |
| 132 test("lowerboundCompare1", () { |
| 133 var l1 = [new C(5)]; |
| 134 expect(lowerBound(l1, new C(2), compare: compareC), equals(0)); |
| 135 expect(lowerBound(l1, new C(5), compare: compareC), equals(0)); |
| 136 expect(lowerBound(l1, new C(7), compare: compareC), equals(1)); |
| 137 }); |
| 138 |
| 139 test("lowerboundCompare3", () { |
| 140 var l3 = [new C(0), new C(5), new C(10)]; |
| 141 expect(lowerBound(l3, new C(-1), compare: compareC), equals(0)); |
| 142 expect(lowerBound(l3, new C(0), compare: compareC), equals(0)); |
| 143 expect(lowerBound(l3, new C(2), compare: compareC), equals(1)); |
| 144 expect(lowerBound(l3, new C(5), compare: compareC), equals(1)); |
| 145 expect(lowerBound(l3, new C(7), compare: compareC), equals(2)); |
| 146 expect(lowerBound(l3, new C(10), compare: compareC), equals(2)); |
| 147 expect(lowerBound(l3, new C(12), compare: compareC), equals(3)); |
| 148 }); |
| 149 |
| 150 test("lowerboundCompareRepeat", () { |
| 151 var l1 = [new C(5), new C(5), new C(5)]; |
| 152 var l2 = [new C(0), new C(5), new C(5), new C(5), new C(10)]; |
| 153 expect(lowerBound(l1, new C(5), compare: compareC), equals(0)); |
| 154 expect(lowerBound(l2, new C(5), compare: compareC), equals(1)); |
| 155 }); |
| 156 |
| 102 test("insertionSortRandom", () { | 157 test("insertionSortRandom", () { |
| 103 Random random = new Random(); | 158 Random random = new Random(); |
| 104 for (int i = 0; i < 25; i++) { | 159 for (int i = 0; i < 25; i++) { |
| 105 List list = new List(i); | 160 List list = new List(i); |
| 106 for (int j = 0; j < i; j++) { | 161 for (int j = 0; j < i; j++) { |
| 107 list[j] = random.nextInt(25); // Expect some equal elements. | 162 list[j] = random.nextInt(25); // Expect some equal elements. |
| 108 } | 163 } |
| 109 insertionSort(list); | 164 insertionSort(list); |
| 110 for (int j = 1; j < i; j++) { | 165 for (int j = 1; j < i; j++) { |
| 111 expect(list[j - 1], lessThanOrEqualTo(list[j])); | 166 expect(list[j - 1], lessThanOrEqualTo(list[j])); |
| 112 } | 167 } |
| 113 } | 168 } |
| 114 }); | 169 }); |
| 115 | 170 |
| 116 test("insertionSortSubRanges", () { | 171 test("insertionSortSubRanges", () { |
| 117 List l = [6, 5, 4, 3, 2, 1]; | 172 List l = [6, 5, 4, 3, 2, 1]; |
| (...skipping 17 matching lines...) Expand all Loading... |
| 135 l = [6, 6, 3, 3, 0, 0]; | 190 l = [6, 6, 3, 3, 0, 0]; |
| 136 insertionSort(l); | 191 insertionSort(l); |
| 137 expect(l, equals([0, 0, 3, 3, 6, 6])); | 192 expect(l, equals([0, 0, 3, 3, 6, 6])); |
| 138 }); | 193 }); |
| 139 | 194 |
| 140 test("MergeSortRandom", () { | 195 test("MergeSortRandom", () { |
| 141 Random random = new Random(); | 196 Random random = new Random(); |
| 142 for (int i = 0; i < 250; i += 1) { | 197 for (int i = 0; i < 250; i += 1) { |
| 143 List list = new List(i); | 198 List list = new List(i); |
| 144 for (int j = 0; j < i; j++) { | 199 for (int j = 0; j < i; j++) { |
| 145 list[j] = random.nextInt(i); // Expect some equal elements. | 200 list[j] = random.nextInt(i); // Expect some equal elements. |
| 146 } | 201 } |
| 147 mergeSort(list); | 202 mergeSort(list); |
| 148 for (int j = 1; j < i; j++) { | 203 for (int j = 1; j < i; j++) { |
| 149 expect(list[j - 1], lessThanOrEqualTo(list[j])); | 204 expect(list[j - 1], lessThanOrEqualTo(list[j])); |
| 150 } | 205 } |
| 151 } | 206 } |
| 152 }); | 207 }); |
| 153 | 208 |
| 154 test("MergeSortPreservesOrder", () { | 209 test("MergeSortPreservesOrder", () { |
| 155 Random random = new Random(); | 210 Random random = new Random(); |
| (...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 253 expect(l, equals([5, 6, 3, 4, 1, 2])); | 308 expect(l, equals([5, 6, 3, 4, 1, 2])); |
| 254 reverse(l, 0, 6); | 309 reverse(l, 0, 6); |
| 255 expect(l, equals([2, 1, 4, 3, 6, 5])); | 310 expect(l, equals([2, 1, 4, 3, 6, 5])); |
| 256 }); | 311 }); |
| 257 } | 312 } |
| 258 | 313 |
| 259 class C { | 314 class C { |
| 260 final int id; | 315 final int id; |
| 261 C(this.id); | 316 C(this.id); |
| 262 } | 317 } |
| 318 |
| 263 int compareC(C one, C other) => one.id - other.id; | 319 int compareC(C one, C other) => one.id - other.id; |
| 264 | 320 |
| 265 class OC implements Comparable<OC> { | 321 class OC implements Comparable<OC> { |
| 266 final int id; | 322 final int id; |
| 267 final int order; | 323 final int order; |
| 268 OC(this.id, this.order); | 324 OC(this.id, this.order); |
| 269 int compareTo(OC other) => id - other.id; | 325 int compareTo(OC other) => id - other.id; |
| 270 String toString() => "OC[$id,$order]"; | 326 String toString() => "OC[$id,$order]"; |
| 271 } | 327 } |
| OLD | NEW |