< prev index next >

test/java/util/Comparator/BasicTest.java

Print this page

        

*** 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 **** * questions. */ /** * @test * @summary Comparator default method tests * @run testng BasicTest */ ! import java.util.TreeMap; ! import java.util.Comparator; import org.testng.annotations.Test; import java.util.function.Function; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; import java.util.function.ToDoubleFunction; --- 21,43 ---- * questions. */ /** * @test + * @bug 8134512 * @summary Comparator default method tests * @run testng BasicTest */ ! import org.testng.annotations.DataProvider; import org.testng.annotations.Test; + import java.util.Arrays; + import java.util.ArrayList; + import java.util.Collections; + import java.util.Comparator; + import java.util.List; import java.util.function.Function; import java.util.function.ToIntFunction; import java.util.function.ToLongFunction; import java.util.function.ToDoubleFunction;
*** 364,369 **** --- 369,605 ---- try { Comparator<People> cmp = Comparator.comparingDouble(null); fail("comparing(null) should throw NPE"); } catch (NullPointerException npe) {} } + + @Test(dataProvider = "presorted") + public void testAlphaDecimal(String[] sorted, Comparator<String> cmp) { + for (int i = 0; i < sorted.length; ++i) { + for (int j = 0; j < sorted.length; ++j) { + int res = cmp.compare(sorted[i], sorted[j]); + boolean ok = (res == 0 && i == j) || + (res <= 0 && i < j) || + (res >= 0 && i > j); + assertTrue(ok); + if (res == 0 && i != j) { + res = cmp.compare(sorted[j], sorted[i]); + assertTrue(res == 0); + } + } + } + } + + // 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", "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", + "string1234", "string10234", + "string100234", "string100234", + "string1000234", "string1000234" }; + + // Leading zeros first + private static String[] arr2 = new String[] { + "string", "string00", "string0", + "string001", "string01", "string1", + "string0002", "string0002", "string002", + "string002.a", "string002.a0", "string002.a1", + "string02", "string02", "string2", "string2" }; + + // Sample from MSDN + private static String[] arr3 = new String[] { + "2string", "3string", "20string", + "st2ring", "st3ring", "st20ring", + "string2", "string3", "string20" }; + + // Very long numbers + private static String[] arr4 = new String[] { + "q_0", + "q_1", + "q_1_", + "q_10000000000000000000000000000000000000000", + "q_10000000000000000000000000000000000000000", + "q_20000000000000000000000000000000000000000", + "q_100000000000000000000000000000000000000000", + "q_500000000000000000000000000000000000000000", + "q_10000000000000000000000000000000000000000000000000000000000000000000000000001", + "q_10000000000000000000000000000000000000000000000000000000000000000000000000001", + "q_20000000000000000000000000000000000000000000000000000000000000000000000000000", + "q_20000000000000000000000000000000000000000000000000000000000000000000000000001", + "q_200000000000000000000000000000000000000000000000000000000000000000000000000000", + "y_1", + "y_10000000000000000000000000000000000000000000000000000000000000000000000000000" }; + + // Plain numbers + private static String[] arr5 = new String[] { + "", + "", + "0", + "1", + "2", + "10", + "10", + "11", + "15", + "19", + "20", + "90", + "100", + "101", + "1024", + "2042", + "2147483647", + "2147483648", // Integer.MAX_VALUE + "2147483649", + "4294967296", + "9223372036854775806", + "9223372036854775807", // Long.MAX_VALUE + "9223372036854775808", + "18446744073709551615", + "18446744073709551616", + "8000000000000000000000000000000000000000000", + "10000000000000000000000000000000000000000500", + "10000000000000000000000000000000000000000500", + "10000000000000000000000000000000000000000501", + "20000000000000000000000000000000000000000500", + "20000000000000000000700000000000000000000000", + }; + + // Plain alphabetical strings + private static String[] arr6 = new String[] { + "", + "a", + "a", + "abcd", + "abcdefgh", + "b", + "bcd", + "bcdefgh", + "z", + "z ", + "zabcd", + "zbcd", + "zcd", + "zz", + }; + + // Case insensitive + private static String[] arr7 = new String[] { + "", + "5", + "42", + "309", + "a", + "A", + "a 1", + "A 2", + "a 15", + "A 21", + "case 42 Insensitive", + "CaSe 42 inSEnsItiVe", + "CAsE 42 INSENSitive", + "caSe 42 INSENSITIVE", + "caSe 42 INSENSITIVE", + }; + + private static String[] replaceDigits(String[] arr, int zeroCp) { + return Arrays.asList(arr) + .stream() + .map(str -> { + StringBuilder sb = new StringBuilder(); + int len = Character.codePointCount(str, 0, str.length()); + for (int i = 0; i < len; ) { + int cp = str.codePointAt(i); + i += Character.charCount(cp); + if (Character.isDigit(cp)) + cp = zeroCp + Character.digit(cp, 10); + sb.appendCodePoint(cp); + } + return sb.toString(); + } + ).toArray(String[]::new); + } + + @DataProvider(name = "presorted") + public Object[][] createPresortedArrays() { + String[][] rev = { + arr0.clone(), arr1.clone(), arr2.clone(), arr3.clone(), + arr4.clone(), arr5.clone(), arr6.clone(), arr7.clone(), + }; + for (String[] r : rev) { + Collections.reverse(Arrays.asList(r)); + } + Comparator<CharSequence> naturalOrder = Comparator.comparing( + CharSequence::toString, Comparator.naturalOrder()); + Comparator<CharSequence> caseInsOrder = Comparator.comparing( + CharSequence::toString, String::compareToIgnoreCase); + int[] digitRangeStart = { + 0x0030, // Latin1 + 0x0660, // Arabic-Indic + 0x06f0, // Extended Arabic-Indic + 0x0b66, // Oriya + 0x0e50, // Thai + 0xa9d0, // Javanese + 0xff10, // Fullwidth + 0x104a0, // Osmanya + 0x118e0, // Warang + 0x1d7ce, // Mathematical bold + 0x1d7d8, // Mathematical double-struck + 0x1d7e2, // Mathematical Sans-serif + 0x1d7ec, // Mathematical Sans-serif bold + 0x1d7f6, // Mathematical Monospace + }; + List<Object[]> res = new ArrayList<>(); + for (int i = 0; i < digitRangeStart.length; ++i) { + res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr1, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr1, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr2, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr2, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr3, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr3, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr4, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr4, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed()}); + res.add(new Object[] {replaceDigits(arr6, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr6, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)}); + res.add(new Object[] {replaceDigits(arr7, digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder)}); + res.add(new Object[] {replaceDigits(arr7, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(caseInsOrder)}); + res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(rev[1], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[2], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(rev[4], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[4], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[5], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[5], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[6], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[6], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder).reversed()}); + res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder).reversed().reversed().reversed()}); + res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(caseInsOrder).reversed()}); + } + return res.toArray(new Object[res.size()][]); + } }
< prev index next >