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