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