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