| 1 |
package org.mozilla.javascript.tests; |
| 2 |
|
| 3 |
import org.junit.BeforeClass; |
| 4 |
import org.junit.Test; |
| 5 |
import org.mozilla.javascript.Sorting; |
| 6 |
|
| 7 |
import java.util.Arrays; |
| 8 |
import java.util.Comparator; |
| 9 |
import java.util.Random; |
| 10 |
|
| 11 |
import static org.junit.Assert.*; |
| 12 |
|
| 13 |
public class SortingTest { |
| 14 |
private static final int BIG_ARRAY = 100000; |
| 15 |
private static final int ITERATIONS = 4; |
| 16 |
|
| 17 |
private static final Random rand = new Random(); |
| 18 |
|
| 19 |
private static Object[] bigRandom; |
| 20 |
|
| 21 |
@BeforeClass |
| 22 |
public static void init() |
| 23 |
{ |
| 24 |
bigRandom = randomArray(BIG_ARRAY); |
| 25 |
} |
| 26 |
|
| 27 |
private void insertionSort(Object[] expected) |
| 28 |
{ |
| 29 |
Object[] after = Arrays.copyOf(expected, expected.length); |
| 30 |
Sorting.insertionSort(after, new IntComparator()); |
| 31 |
Arrays.sort(expected, new IntComparator()); |
| 32 |
assertArrayEquals(expected, after); |
| 33 |
} |
| 34 |
|
| 35 |
@Test |
| 36 |
public void testInsertionSort() |
| 37 |
{ |
| 38 |
insertionSort(forwardArray(100)); |
| 39 |
insertionSort(reverseArray(100)); |
| 40 |
insertionSort(randomArray(100)); |
| 41 |
insertionSort(sameArray(100)); |
| 42 |
insertionSort(new Object[] {}); |
| 43 |
insertionSort(randomArray(10000)); |
| 44 |
} |
| 45 |
|
| 46 |
private void hybridSort(Object[] expected) |
| 47 |
{ |
| 48 |
Object[] after = Arrays.copyOf(expected, expected.length); |
| 49 |
Sorting.hybridSort(after, new IntComparator()); |
| 50 |
Arrays.sort(expected, new IntComparator()); |
| 51 |
assertArrayEquals(expected, after); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void testHybridSort() |
| 56 |
{ |
| 57 |
hybridSort(randomArray(10)); |
| 58 |
hybridSort(forwardArray(100)); |
| 59 |
hybridSort(reverseArray(100)); |
| 60 |
hybridSort(randomArray(100)); |
| 61 |
hybridSort(sameArray(100)); |
| 62 |
hybridSort(new Object[] {}); |
| 63 |
hybridSort(randomArray(10000)); |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
@Test |
| 76 |
public void testBenchRandomHybrid() |
| 77 |
{ |
| 78 |
for (int i = 0; i < ITERATIONS; i++) { |
| 79 |
Object[] a = Arrays.copyOf(bigRandom, bigRandom.length); |
| 80 |
Sorting.hybridSort(a, new IntComparator()); |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
@Test |
| 85 |
public void testBenchRandomJavaUtil() |
| 86 |
{ |
| 87 |
for (int i = 0; i < ITERATIONS; i++) { |
| 88 |
Object[] a = Arrays.copyOf(bigRandom, bigRandom.length); |
| 89 |
Arrays.sort(a, new IntComparator()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
@Test |
| 105 |
public void testBenchReverseHybrid() |
| 106 |
{ |
| 107 |
for (int i = 0; i < ITERATIONS; i++) { |
| 108 |
Object[] a = reverseArray(BIG_ARRAY); |
| 109 |
Sorting.hybridSort(a, new IntComparator()); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
@Test |
| 114 |
public void testBenchReverseJavaUtil() |
| 115 |
{ |
| 116 |
for (int i = 0; i < ITERATIONS; i++) { |
| 117 |
Object[] a = reverseArray(BIG_ARRAY); |
| 118 |
Arrays.sort(a, new IntComparator()); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
@Test |
| 123 |
public void testBenchSequentialInsertion() |
| 124 |
{ |
| 125 |
for (int i = 0; i < ITERATIONS; i++) { |
| 126 |
Object[] a = forwardArray(BIG_ARRAY); |
| 127 |
Sorting.insertionSort(a, new IntComparator()); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
@Test |
| 132 |
public void testBenchSequentialHybrid() |
| 133 |
{ |
| 134 |
for (int i = 0; i < ITERATIONS; i++) { |
| 135 |
Object[] a = forwardArray(BIG_ARRAY); |
| 136 |
Sorting.hybridSort(a, new IntComparator()); |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
@Test |
| 141 |
public void testBenchSequentialJavaUtil() |
| 142 |
{ |
| 143 |
for (int i = 0; i < ITERATIONS; i++) { |
| 144 |
Object[] a = forwardArray(BIG_ARRAY); |
| 145 |
Arrays.sort(a, new IntComparator()); |
| 146 |
} |
| 147 |
} |
| 148 |
|
| 149 |
private static Integer[] forwardArray(int length) |
| 150 |
{ |
| 151 |
Integer[] a = new Integer[length]; |
| 152 |
for (int i = 0; i < length; i++) { |
| 153 |
a[i] = i; |
| 154 |
} |
| 155 |
return a; |
| 156 |
} |
| 157 |
|
| 158 |
private static Integer[] reverseArray(int length) |
| 159 |
{ |
| 160 |
Integer[] a = new Integer[length]; |
| 161 |
for (int i = 0; i < length; i++) { |
| 162 |
a[i] = length - i - 1; |
| 163 |
} |
| 164 |
return a; |
| 165 |
} |
| 166 |
|
| 167 |
private static Integer[] randomArray(int length) |
| 168 |
{ |
| 169 |
Integer[] a = new Integer[length]; |
| 170 |
for (int i = 0; i < length; i++) { |
| 171 |
a[i] = rand.nextInt(); |
| 172 |
} |
| 173 |
return a; |
| 174 |
} |
| 175 |
|
| 176 |
private static Integer[] sameArray(int length) |
| 177 |
{ |
| 178 |
Integer[] a = new Integer[length]; |
| 179 |
Arrays.fill(a, 1); |
| 180 |
return a; |
| 181 |
} |
| 182 |
|
| 183 |
private final class IntComparator |
| 184 |
implements Comparator<Object> |
| 185 |
{ |
| 186 |
@Override |
| 187 |
public int compare(Object a, Object b) { |
| 188 |
Integer ia = (Integer)a; |
| 189 |
Integer ib = (Integer)b; |
| 190 |
if (ia < ib) { |
| 191 |
return -1; |
| 192 |
} |
| 193 |
if (ia > ib) { |
| 194 |
return 1; |
| 195 |
} |
| 196 |
return 0; |
| 197 |
} |
| 198 |
} |
| 199 |
} |