1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/*
* Copyright 2015 Goldman Sachs.
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8154049
* @summary Tests the sorting of a large array of sorted primitive values,
* predominently for cases where the array is nearly sorted. This tests
* code that detects patterns in the array to determine if it is nearly
* sorted and if so employs and optimizes merge sort rather than a
* Dual-Pivot QuickSort.
*
* @run testng SortingNearlySortedPrimitive
*/
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringJoiner;
import java.util.function.IntFunction;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class SortingNearlySortedPrimitive {
static final int BASE = 3;
static final int WIDTH = 4;
// Should be > DualPivotQuicksort.QUICKSORT_THRESHOLD
static final int PAD = 300;
Stream<int[]> createCombinations() {
// Create all combinations for the BASE value and double the WIDTH
// elements
// This is create various combinations of ascending, descending and
// equal runs to exercise the nearly sorted code paths
return IntStream.range(0, (int) Math.pow(BASE, 2 * WIDTH)).
mapToObj(this::createArray);
}
// Create an array which at either end is filled with -ve and +ve elements
// according to the base value and padded with zeros in between
int[] createArray(int v) {
int[] a = new int[WIDTH + PAD + WIDTH];
// Fill head of array
for (int j = 0; j < WIDTH; j++) {
a[j] = (v % BASE) - (BASE / 2);
v /= BASE;
}
// Fill tail of array
for (int j = 0; j < WIDTH; j++) {
a[WIDTH + PAD + j] = (v % BASE) - (BASE / 2);
v /= BASE;
}
return a;
}
@Test
public void testCombination() {
createCombinations().forEach(a -> {
try {
// Clone source array to ensure it is not modified
this.sortAndAssert(a.clone());
this.sortAndAssert(floatCopyFromInt(a));
this.sortAndAssert(doubleCopyFromInt(a));
this.sortAndAssert(longCopyFromInt(a));
this.sortAndAssert(shortCopyFromInt(a));
this.sortAndAssert(charCopyFromInt(a));
} catch (AssertionError sae) {
AssertionError ae = new AssertionError("Sort failed for " + arrayToString(a));
ae.addSuppressed(sae);
throw ae;
}
});
}
String arrayToString(int[] a) {
int[] l = Arrays.copyOfRange(a, 0, WIDTH + 2);
int[] r = Arrays.copyOfRange(a, a.length - (WIDTH + 2), a.length);
StringJoiner sj = new StringJoiner(",", "[", "]");
for (int i : l) {
sj.add(Integer.toString(i));
}
sj.add("...");
for (int i : r) {
sj.add(Integer.toString(i));
}
return sj.toString();
}
@DataProvider(name = "shapes")
public Object[][] createShapes() {
Stream<List<Object>> baseCases = Stream.of(
List.of("hiZeroLowTest", (IntFunction<int[]>) this::hiZeroLowData),
List.of("endLessThanTest", (IntFunction<int[]>) this::endLessThanData),
List.of("highFlatLowTest", (IntFunction<int[]>) this::highFlatLowData),
List.of("identicalTest", (IntFunction<int[]>) this::identicalData),
List.of("sortedReversedSortedTest", (IntFunction<int[]>) this::sortedReversedSortedData),
List.of("pairFlipTest", (IntFunction<int[]>) this::pairFlipData),
List.of("zeroHiTest", (IntFunction<int[]>) this::zeroHiData)
);
// Ensure the following inequality holds for certain sizes
// DualPivotQuicksort.QUICKSORT_THRESHOLD <= size - 1
// < DualPivotQuicksort.COUNTING_SORT_THRESHOLD_FOR_SHORT_OR_CHAR
// This guarantees that code paths are taken for checking nearly sorted
// arrays for all primitive types
List<Integer> sizes = List.of(100, 1_000, 10_000, 1_000_000);
return baseCases.
flatMap(l -> sizes.stream().map(s -> append(l, s))).
toArray(Object[][]::new);
}
Object[] append(List<Object> l, Object value) {
List<Object> nl = new ArrayList<>(l);
nl.add(value);
return nl.toArray();
}
@Test(dataProvider = "shapes")
public void testShapes(String testName, IntFunction<int[]> dataMethod, int size) {
int[] intSourceArray = dataMethod.apply(size);
// Clone source array to ensure it is not modified
this.sortAndAssert(intSourceArray.clone());
this.sortAndAssert(floatCopyFromInt(intSourceArray));
this.sortAndAssert(doubleCopyFromInt(intSourceArray));
this.sortAndAssert(longCopyFromInt(intSourceArray));
this.sortAndAssert(shortCopyFromInt(intSourceArray));
this.sortAndAssert(charCopyFromInt(intSourceArray));
}
private float[] floatCopyFromInt(int[] src) {
float[] result = new float[src.length];
for (int i = 0; i < result.length; i++) {
result[i] = src[i];
}
return result;
}
private double[] doubleCopyFromInt(int[] src) {
double[] result = new double[src.length];
for (int i = 0; i < result.length; i++) {
result[i] = src[i];
}
return result;
}
private long[] longCopyFromInt(int[] src) {
long[] result = new long[src.length];
for (int i = 0; i < result.length; i++) {
result[i] = src[i];
}
return result;
}
private short[] shortCopyFromInt(int[] src) {
short[] result = new short[src.length];
for (int i = 0; i < result.length; i++) {
result[i] = (short) src[i];
}
return result;
}
private char[] charCopyFromInt(int[] src) {
char[] result = new char[src.length];
for (int i = 0; i < result.length; i++) {
result[i] = (char) src[i];
}
return result;
}
private void sortAndAssert(int[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private void sortAndAssert(char[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private void sortAndAssert(short[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private void sortAndAssert(double[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private void sortAndAssert(float[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private void sortAndAssert(long[] array) {
Arrays.sort(array);
for (int i = 1; i < array.length; i++) {
if (array[i] < array[i - 1]) {
throw new AssertionError("not sorted");
}
}
}
private int[] zeroHiData(int size) {
int[] array = new int[size];
int threeQuarters = (int) (size * 0.75);
for (int i = 0; i < threeQuarters; i++) {
array[i] = 0;
}
int k = 1;
for (int i = threeQuarters; i < size; i++) {
array[i] = k;
k++;
}
return array;
}
private int[] hiZeroLowData(int size) {
int[] array = new int[size];
int oneThird = size / 3;
for (int i = 0; i < oneThird; i++) {
array[i] = i;
}
int twoThirds = oneThird * 2;
for (int i = oneThird; i < twoThirds; i++) {
array[i] = 0;
}
for (int i = twoThirds; i < size; i++) {
array[i] = oneThird - i + twoThirds;
}
return array;
}
private int[] highFlatLowData(int size) {
int[] array = new int[size];
int oneThird = size / 3;
for (int i = 0; i < oneThird; i++) {
array[i] = i;
}
int twoThirds = oneThird * 2;
int constant = oneThird - 1;
for (int i = oneThird; i < twoThirds; i++) {
array[i] = constant;
}
for (int i = twoThirds; i < size; i++) {
array[i] = constant - i + twoThirds;
}
return array;
}
private int[] identicalData(int size) {
int[] array = new int[size];
int listNumber = 24;
for (int i = 0; i < size; i++) {
array[i] = listNumber;
}
return array;
}
private int[] endLessThanData(int size) {
int[] array = new int[size];
for (int i = 0; i < size - 1; i++) {
array[i] = 3;
}
array[size - 1] = 1;
return array;
}
private int[] sortedReversedSortedData(int size) {
int[] array = new int[size];
for (int i = 0; i < size / 2; i++) {
array[i] = i;
}
int num = 0;
for (int i = size / 2; i < size; i++) {
array[i] = size - num;
num++;
}
return array;
}
private int[] pairFlipData(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = i;
}
for (int i = 0; i < size; i += 2) {
int temp = array[i];
array[i] = array[i + 1];
array[i + 1] = temp;
}
return array;
}
}
|