| 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 | 4 |
| 5 library sort_helper; | 5 library sort_helper; |
| 6 | 6 |
| 7 import "package:expect/expect.dart"; | 7 import "package:expect/expect.dart"; |
| 8 | 8 |
| 9 typedef Sorter = void Function(List<num>); |
| 10 typedef Comparer = int Function(num, num); |
| 11 |
| 9 class SortHelper { | 12 class SortHelper { |
| 10 SortHelper(this.sortFunction, this.compareFunction) {} | 13 SortHelper(this.sortFunction, this.compareFunction) {} |
| 11 | 14 |
| 12 void run() { | 15 void run() { |
| 13 testSortIntLists(); | 16 testSortIntLists(); |
| 14 testSortDoubleLists(); | 17 testSortDoubleLists(); |
| 15 } | 18 } |
| 16 | 19 |
| 17 bool isSorted(List a) { | 20 bool isSorted(List<num> a) { |
| 18 for (int i = 1; i < a.length; i++) { | 21 for (int i = 1; i < a.length; i++) { |
| 19 if (compareFunction(a[i - 1], a[i]) > 0) { | 22 if (compareFunction(a[i - 1], a[i]) > 0) { |
| 20 return false; | 23 return false; |
| 21 } | 24 } |
| 22 } | 25 } |
| 23 return true; | 26 return true; |
| 24 } | 27 } |
| 25 | 28 |
| 26 void testSortIntLists() { | 29 void testSortIntLists() { |
| 27 List a = new List(40); | 30 var a = new List<int>(40); |
| 28 | 31 |
| 29 for (int i = 0; i < a.length; i++) { | 32 for (int i = 0; i < a.length; i++) { |
| 30 a[i] = i; | 33 a[i] = i; |
| 31 } | 34 } |
| 32 testSort(a); | 35 testSort(a); |
| 33 | 36 |
| 34 for (int i = 0; i < a.length; i++) { | 37 for (int i = 0; i < a.length; i++) { |
| 35 a[a.length - i - 1] = i; | 38 a[a.length - i - 1] = i; |
| 36 } | 39 } |
| 37 testSort(a); | 40 testSort(a); |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 73 for (int i = 21; i < a.length; i++) { | 76 for (int i = 21; i < a.length; i++) { |
| 74 a[i] = 1; | 77 a[i] = 1; |
| 75 } | 78 } |
| 76 a[6] = 2; | 79 a[6] = 2; |
| 77 a[13] = 2; | 80 a[13] = 2; |
| 78 a[19] = 2; | 81 a[19] = 2; |
| 79 a[25] = 2; | 82 a[25] = 2; |
| 80 a[33] = 1; | 83 a[33] = 1; |
| 81 testSort(a); | 84 testSort(a); |
| 82 | 85 |
| 83 var a2 = new List(0); | 86 var a2 = new List<int>(0); |
| 84 testSort(a2); | 87 testSort(a2); |
| 85 | 88 |
| 86 var a3 = new List(1); | 89 var a3 = new List<int>(1); |
| 87 a3[0] = 1; | 90 a3[0] = 1; |
| 88 testSort(a3); | 91 testSort(a3); |
| 89 | 92 |
| 90 // -------- | 93 // -------- |
| 91 // Test insertion sort. | 94 // Test insertion sort. |
| 92 testInsertionSort(0, 1, 2, 3); | 95 testInsertionSort(0, 1, 2, 3); |
| 93 testInsertionSort(0, 1, 3, 2); | 96 testInsertionSort(0, 1, 3, 2); |
| 94 testInsertionSort(0, 3, 2, 1); | 97 testInsertionSort(0, 3, 2, 1); |
| 95 testInsertionSort(0, 3, 1, 2); | 98 testInsertionSort(0, 3, 1, 2); |
| 96 testInsertionSort(0, 2, 1, 3); | 99 testInsertionSort(0, 2, 1, 3); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 108 testInsertionSort(2, 3, 1, 0); | 111 testInsertionSort(2, 3, 1, 0); |
| 109 testInsertionSort(2, 3, 0, 1); | 112 testInsertionSort(2, 3, 0, 1); |
| 110 testInsertionSort(3, 0, 1, 2); | 113 testInsertionSort(3, 0, 1, 2); |
| 111 testInsertionSort(3, 0, 2, 1); | 114 testInsertionSort(3, 0, 2, 1); |
| 112 testInsertionSort(3, 1, 2, 0); | 115 testInsertionSort(3, 1, 2, 0); |
| 113 testInsertionSort(3, 1, 0, 2); | 116 testInsertionSort(3, 1, 0, 2); |
| 114 testInsertionSort(3, 2, 1, 0); | 117 testInsertionSort(3, 2, 1, 0); |
| 115 testInsertionSort(3, 2, 0, 1); | 118 testInsertionSort(3, 2, 0, 1); |
| 116 } | 119 } |
| 117 | 120 |
| 118 void testSort(List a) { | 121 void testSort(List<num> a) { |
| 119 sortFunction(a); | 122 sortFunction(a); |
| 120 Expect.isTrue(isSorted(a)); | 123 Expect.isTrue(isSorted(a)); |
| 121 } | 124 } |
| 122 | 125 |
| 123 void testInsertionSort(int i1, int i2, int i3, int i4) { | 126 void testInsertionSort(int i1, int i2, int i3, int i4) { |
| 124 var a = new List(4); | 127 var a = new List<int>(4); |
| 125 a[0] = i1; | 128 a[0] = i1; |
| 126 a[1] = i2; | 129 a[1] = i2; |
| 127 a[2] = i3; | 130 a[2] = i3; |
| 128 a[3] = i4; | 131 a[3] = i4; |
| 129 testSort(a); | 132 testSort(a); |
| 130 } | 133 } |
| 131 | 134 |
| 132 void testSortDoubleLists() { | 135 void testSortDoubleLists() { |
| 133 List a = new List(40); | 136 var a = new List<double>(40); |
| 134 for (int i = 0; i < a.length; i++) { | 137 for (int i = 0; i < a.length; i++) { |
| 135 a[i] = 1.0 * i + 0.5; | 138 a[i] = 1.0 * i + 0.5; |
| 136 } | 139 } |
| 137 testSort(a); | 140 testSort(a); |
| 138 | 141 |
| 139 for (int i = 0; i < a.length; i++) { | 142 for (int i = 0; i < a.length; i++) { |
| 140 a[i] = 1.0 * (a.length - i) + 0.5; | 143 a[i] = 1.0 * (a.length - i) + 0.5; |
| 141 } | 144 } |
| 142 testSort(a); | 145 testSort(a); |
| 143 | 146 |
| 144 for (int i = 0; i < a.length; i++) { | 147 for (int i = 0; i < a.length; i++) { |
| 145 a[i] = 1.5; | 148 a[i] = 1.5; |
| 146 } | 149 } |
| 147 testSort(a); | 150 testSort(a); |
| 148 } | 151 } |
| 149 | 152 |
| 150 Function sortFunction; | 153 Sorter sortFunction; |
| 151 Function compareFunction; | 154 Comparer compareFunction; |
| 152 } | 155 } |
| OLD | NEW |