| 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 | |
| 5 library sort_helper; | |
| 6 | |
| 7 import "package:expect/expect.dart"; | |
| 8 | |
| 9 typedef Sorter | |
| 10 = void Function(List<num>); | |
| 11 typedef Comparer | |
| 12 = int Function(num, num); | |
| 13 | |
| 14 class SortHelper { | |
| 15 SortHelper(this.sortFunction, this.compareFunction) {} | |
| 16 | |
| 17 void run() { | |
| 18 testSortIntLists(); | |
| 19 testSortDoubleLists(); | |
| 20 } | |
| 21 | |
| 22 bool isSorted(List<num> a) { | |
| 23 for (int i = 1; i < a.length; i++) { | |
| 24 if (compareFunction(a[i - 1], a[i]) > 0) { | |
| 25 return false; | |
| 26 } | |
| 27 } | |
| 28 return true; | |
| 29 } | |
| 30 | |
| 31 void testSortIntLists() { | |
| 32 var a = new List<int>(40); | |
| 33 | |
| 34 for (int i = 0; i < a.length; i++) { | |
| 35 a[i] = i; | |
| 36 } | |
| 37 testSort(a); | |
| 38 | |
| 39 for (int i = 0; i < a.length; i++) { | |
| 40 a[a.length - i - 1] = i; | |
| 41 } | |
| 42 testSort(a); | |
| 43 | |
| 44 for (int i = 0; i < 21; i++) { | |
| 45 a[i] = 1; | |
| 46 } | |
| 47 for (int i = 21; i < a.length; i++) { | |
| 48 a[i] = 2; | |
| 49 } | |
| 50 testSort(a); | |
| 51 | |
| 52 // Same with bad pivot-choices. | |
| 53 for (int i = 0; i < 21; i++) { | |
| 54 a[i] = 1; | |
| 55 } | |
| 56 for (int i = 21; i < a.length; i++) { | |
| 57 a[i] = 2; | |
| 58 } | |
| 59 a[6] = 1; | |
| 60 a[13] = 1; | |
| 61 a[19] = 1; | |
| 62 a[25] = 1; | |
| 63 a[33] = 2; | |
| 64 testSort(a); | |
| 65 | |
| 66 for (int i = 0; i < 21; i++) { | |
| 67 a[i] = 2; | |
| 68 } | |
| 69 for (int i = 21; i < a.length; i++) { | |
| 70 a[i] = 1; | |
| 71 } | |
| 72 testSort(a); | |
| 73 | |
| 74 // Same with bad pivot-choices. | |
| 75 for (int i = 0; i < 21; i++) { | |
| 76 a[i] = 2; | |
| 77 } | |
| 78 for (int i = 21; i < a.length; i++) { | |
| 79 a[i] = 1; | |
| 80 } | |
| 81 a[6] = 2; | |
| 82 a[13] = 2; | |
| 83 a[19] = 2; | |
| 84 a[25] = 2; | |
| 85 a[33] = 1; | |
| 86 testSort(a); | |
| 87 | |
| 88 var a2 = new List<int>(0); | |
| 89 testSort(a2); | |
| 90 | |
| 91 var a3 = new List<int>(1); | |
| 92 a3[0] = 1; | |
| 93 testSort(a3); | |
| 94 | |
| 95 // -------- | |
| 96 // Test insertion sort. | |
| 97 testInsertionSort(0, 1, 2, 3); | |
| 98 testInsertionSort(0, 1, 3, 2); | |
| 99 testInsertionSort(0, 3, 2, 1); | |
| 100 testInsertionSort(0, 3, 1, 2); | |
| 101 testInsertionSort(0, 2, 1, 3); | |
| 102 testInsertionSort(0, 2, 3, 1); | |
| 103 testInsertionSort(1, 0, 2, 3); | |
| 104 testInsertionSort(1, 0, 3, 2); | |
| 105 testInsertionSort(1, 2, 3, 0); | |
| 106 testInsertionSort(1, 2, 0, 3); | |
| 107 testInsertionSort(1, 3, 2, 0); | |
| 108 testInsertionSort(1, 3, 0, 2); | |
| 109 testInsertionSort(2, 0, 1, 3); | |
| 110 testInsertionSort(2, 0, 3, 1); | |
| 111 testInsertionSort(2, 1, 3, 0); | |
| 112 testInsertionSort(2, 1, 0, 3); | |
| 113 testInsertionSort(2, 3, 1, 0); | |
| 114 testInsertionSort(2, 3, 0, 1); | |
| 115 testInsertionSort(3, 0, 1, 2); | |
| 116 testInsertionSort(3, 0, 2, 1); | |
| 117 testInsertionSort(3, 1, 2, 0); | |
| 118 testInsertionSort(3, 1, 0, 2); | |
| 119 testInsertionSort(3, 2, 1, 0); | |
| 120 testInsertionSort(3, 2, 0, 1); | |
| 121 } | |
| 122 | |
| 123 void testSort(List<num> a) { | |
| 124 sortFunction(a); | |
| 125 Expect.isTrue(isSorted(a)); | |
| 126 } | |
| 127 | |
| 128 void testInsertionSort(int i1, int i2, int i3, int i4) { | |
| 129 var a = new List<int>(4); | |
| 130 a[0] = i1; | |
| 131 a[1] = i2; | |
| 132 a[2] = i3; | |
| 133 a[3] = i4; | |
| 134 testSort(a); | |
| 135 } | |
| 136 | |
| 137 void testSortDoubleLists() { | |
| 138 var a = new List<double>(40); | |
| 139 for (int i = 0; i < a.length; i++) { | |
| 140 a[i] = 1.0 * i + 0.5; | |
| 141 } | |
| 142 testSort(a); | |
| 143 | |
| 144 for (int i = 0; i < a.length; i++) { | |
| 145 a[i] = 1.0 * (a.length - i) + 0.5; | |
| 146 } | |
| 147 testSort(a); | |
| 148 | |
| 149 for (int i = 0; i < a.length; i++) { | |
| 150 a[i] = 1.5; | |
| 151 } | |
| 152 testSort(a); | |
| 153 } | |
| 154 | |
| 155 Sorter sortFunction; | |
| 156 Comparer compareFunction; | |
| 157 } | |
| OLD | NEW |