| 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 class SortHelper { | 5 class SortHelper { |
| 6 | 6 |
| 7 SortHelper(this.sortFunction, this.compareFunction) {} | 7 SortHelper(this.sortFunction, this.compareFunction) {} |
| 8 | 8 |
| 9 void run() { | 9 void run() { |
| 10 testSortIntLists(); | 10 testSortIntLists(); |
| 11 testSortDoubleLists(); | 11 testSortDoubleLists(); |
| 12 } | 12 } |
| 13 | 13 |
| 14 void printList(List a) { | |
| 15 StringBuffer buffer = new StringBuffer(); | |
| 16 for (int i = 0; i < a.length; i++) { | |
| 17 if (i != 0) buffer.add(","); | |
| 18 buffer.add(a[i]); | |
| 19 } | |
| 20 print("[$buffer]"); | |
| 21 } | |
| 22 | |
| 23 bool isSorted(List a) { | 14 bool isSorted(List a) { |
| 24 for (int i = 1; i < a.length; i++) { | 15 for (int i = 1; i < a.length; i++) { |
| 25 if (compareFunction(a[i - 1], a[i]) > 0) { | 16 if (compareFunction(a[i - 1], a[i]) > 0) { |
| 26 return false; | 17 return false; |
| 27 } | 18 } |
| 28 } | 19 } |
| 29 return true; | 20 return true; |
| 30 } | 21 } |
| 31 | 22 |
| 32 void testSortIntLists() { | 23 void testSortIntLists() { |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 testInsertionSort(2, 3, 0, 1); | 106 testInsertionSort(2, 3, 0, 1); |
| 116 testInsertionSort(3, 0, 1, 2); | 107 testInsertionSort(3, 0, 1, 2); |
| 117 testInsertionSort(3, 0, 2, 1); | 108 testInsertionSort(3, 0, 2, 1); |
| 118 testInsertionSort(3, 1, 2, 0); | 109 testInsertionSort(3, 1, 2, 0); |
| 119 testInsertionSort(3, 1, 0, 2); | 110 testInsertionSort(3, 1, 0, 2); |
| 120 testInsertionSort(3, 2, 1, 0); | 111 testInsertionSort(3, 2, 1, 0); |
| 121 testInsertionSort(3, 2, 0, 1); | 112 testInsertionSort(3, 2, 0, 1); |
| 122 } | 113 } |
| 123 | 114 |
| 124 void testSort(List a) { | 115 void testSort(List a) { |
| 125 final bool log = false; | |
| 126 if (log) printList(a); | |
| 127 sortFunction(a); | 116 sortFunction(a); |
| 128 if (log) printList(a); | 117 Expect.isTrue(isSorted(a)); |
| 129 bool sorted = isSorted(a); | |
| 130 Expect.equals(true, sorted); | |
| 131 if (log) print(sorted); | |
| 132 } | 118 } |
| 133 | 119 |
| 134 void testInsertionSort(int i1, int i2, int i3, int i4) { | 120 void testInsertionSort(int i1, int i2, int i3, int i4) { |
| 135 var a = new List(4); | 121 var a = new List(4); |
| 136 a[0] = i1; | 122 a[0] = i1; |
| 137 a[1] = i2; | 123 a[1] = i2; |
| 138 a[2] = i3; | 124 a[2] = i3; |
| 139 a[3] = i4; | 125 a[3] = i4; |
| 140 testSort(a); | 126 testSort(a); |
| 141 } | 127 } |
| (...skipping 12 matching lines...) Expand all Loading... |
| 154 | 140 |
| 155 for (int i = 0; i < a.length; i++) { | 141 for (int i = 0; i < a.length; i++) { |
| 156 a[i] = 1.5; | 142 a[i] = 1.5; |
| 157 } | 143 } |
| 158 testSort(a); | 144 testSort(a); |
| 159 } | 145 } |
| 160 | 146 |
| 161 Function sortFunction; | 147 Function sortFunction; |
| 162 Function compareFunction; | 148 Function compareFunction; |
| 163 } | 149 } |
| OLD | NEW |