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