< prev index next >
test/java/util/Comparator/BasicTest.java
Print this page
rev 17471 : [mq]: 8134512-provide-Alpha-Numeric-logical-Comparator
*** 1,7 ****
/*
! * Copyright (c) 2013, 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.
--- 1,7 ----
/*
! * Copyright (c) 2013, 2017, 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.
*** 21,38 ****
--- 21,43 ----
* questions.
*/
/**
* @test
+ * @bug 8134512
* @summary Comparator default method tests
* @run testng BasicTest
+ * @key randomness
*/
import java.util.TreeMap;
import java.util.Comparator;
+ import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
+ import java.util.Arrays;
+ import java.util.Collections;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import java.util.function.ToLongFunction;
import java.util.function.ToDoubleFunction;
*** 364,369 ****
--- 369,477 ----
try {
Comparator<People> cmp = Comparator.comparingDouble(null);
fail("comparing(null) should throw NPE");
} catch (NullPointerException npe) {}
}
+
+ @Test(dataProvider = "presorted")
+ public void testNumerical(String[] sorted, Comparator<String> cmp) {
+ String[] shuffled = sorted.clone();
+ Collections.shuffle(Arrays.asList(shuffled));
+ Arrays.sort(shuffled, cmp);
+ assertEquals(shuffled, sorted);
+ }
+
+ // Simple case: sorting 1 .. 11
+ private static String[] arr0 = new String[] {
+ "java 1", "java 2", "java 3",
+ "java 4", "java 5", "java 6",
+ "java 7", "java 8", "java 9",
+ "java 10", "java 11" };
+
+ // Leading zeros and multiple numeric parts
+ private static String[] arr1 = new String[] {
+ "string", "string0", "string00",
+ "string1", "string01", "string001",
+ "string2", "string02", "string002",
+ "string002.a", "string002.a0", "string002.a1",
+ "string0002", "string0002" };
+
+ // Leading zeros ahead
+ private static String[] arr2 = new String[] {
+ "string", "string00", "string0",
+ "string001", "string01", "string1",
+ "string0002", "string0002", "string002",
+ "string002.a", "string002.a0", "string002.a1",
+ "string02", "string2" };
+
+ // Sample from MSDN
+ private static String[] arr3 = new String[] {
+ "2string", "3string", "20string",
+ "st2ring", "st3ring", "st20ring",
+ "string2", "string3", "string20" };
+
+ // Fullwidth characters
+ private static String[] arr4 = new String[] {
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff10",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff10\uff10",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff11",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff12",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff13",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff14",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff15",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff16",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff17",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff18",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff19",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff10",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff11",
+ "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff12" };
+
+ // Very long numbers
+ private static String[] arr5 = new String[] {
+ "q_0",
+ "q_1",
+ "q_1_",
+ "q_10000000000000000000000000000000000000000",
+ "q_20000000000000000000000000000000000000000",
+ "q_100000000000000000000000000000000000000000",
+ "q_500000000000000000000000000000000000000000",
+ "q_10000000000000000000000000000000000000000000000000000000000000000000000000001",
+ "q_20000000000000000000000000000000000000000000000000000000000000000000000000000",
+ "q_20000000000000000000000000000000000000000000000000000000000000000000000000001",
+ "q_200000000000000000000000000000000000000000000000000000000000000000000000000000",
+ "y_1",
+ "y_10000000000000000000000000000000000000000000000000000000000000000000000000000" };
+
+ @DataProvider(name = "presorted")
+ public Object[][] createPresortedArrays() {
+ String[][] rev = {
+ arr0.clone(), arr1.clone(), arr2.clone(),
+ arr3.clone(), arr4.clone(), arr5.clone(),
+ };
+ for (String[] r : rev) {
+ Collections.reverse(Arrays.asList(r));
+ }
+ return new Object[][] {
+ {arr0, Comparator.comparingNumerically()},
+ {arr0, Comparator.comparingNumericallyLeadingZerosAhead()},
+ {arr1, Comparator.comparingNumerically()},
+ {arr2, Comparator.comparingNumericallyLeadingZerosAhead()},
+ {arr3, Comparator.comparingNumerically()},
+ {arr3, Comparator.comparingNumericallyLeadingZerosAhead()},
+ {arr4, Comparator.comparingNumerically()},
+ {arr5, Comparator.comparingNumerically()},
+ {arr5, Comparator.comparingNumericallyLeadingZerosAhead()},
+ {rev[0], Comparator.comparingNumerically().reversed()},
+ {rev[0], Comparator.comparingNumericallyLeadingZerosAhead().reversed()},
+ {rev[1], Comparator.comparingNumerically().reversed()},
+ {rev[2], Comparator.comparingNumericallyLeadingZerosAhead().reversed()},
+ {rev[3], Comparator.comparingNumerically().reversed()},
+ {rev[3], Comparator.comparingNumericallyLeadingZerosAhead().reversed()},
+ {rev[4], Comparator.comparingNumerically().reversed()},
+ {rev[5], Comparator.comparingNumerically().reversed()},
+ {rev[5], Comparator.comparingNumericallyLeadingZerosAhead().reversed()},
+ };
+ }
}
< prev index next >