< prev index next >

test/java/util/Comparator/BasicTest.java

Print this page


   1 /*
   2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test

  26  * @summary Comparator default method tests
  27  * @run testng BasicTest
  28  */
  29 
  30 import java.util.TreeMap;
  31 import java.util.Comparator;
  32 import org.testng.annotations.Test;
  33 





  34 import java.util.function.Function;
  35 import java.util.function.ToIntFunction;
  36 import java.util.function.ToLongFunction;
  37 import java.util.function.ToDoubleFunction;
  38 
  39 import static org.testng.Assert.assertEquals;
  40 import static org.testng.Assert.assertTrue;
  41 import static org.testng.Assert.fail;
  42 
  43 @Test(groups = "unit")
  44 public class BasicTest {
  45     private static class Thing {
  46         public final int intField;
  47         public final long longField;
  48         public final double doubleField;
  49         public final String stringField;
  50 
  51         private Thing(int intField, long longField, double doubleField, String stringField) {
  52             this.intField = intField;
  53             this.longField = longField;


 348             Comparator<People> cmp = Comparator.comparing(People::getFirstName, null);
 349             fail("comparing(f, null) should throw NPE");
 350         } catch (NullPointerException npe) {}
 351 
 352         try {
 353             Comparator<People> cmp = Comparator.comparing(null);
 354             fail("comparing(null) should throw NPE");
 355         } catch (NullPointerException npe) {}
 356         try {
 357             Comparator<People> cmp = Comparator.comparingInt(null);
 358             fail("comparing(null) should throw NPE");
 359         } catch (NullPointerException npe) {}
 360         try {
 361             Comparator<People> cmp = Comparator.comparingLong(null);
 362             fail("comparing(null) should throw NPE");
 363         } catch (NullPointerException npe) {}
 364         try {
 365             Comparator<People> cmp = Comparator.comparingDouble(null);
 366             fail("comparing(null) should throw NPE");
 367         } catch (NullPointerException npe) {}







































































































































































































































 368     }
 369 }
   1 /*
   2  * Copyright (c) 2013, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 8134512
  27  * @summary Comparator default method tests
  28  * @run testng BasicTest
  29  */
  30 
  31 import org.testng.annotations.DataProvider;

  32 import org.testng.annotations.Test;
  33 
  34 import java.util.Arrays;
  35 import java.util.ArrayList;
  36 import java.util.Collections;
  37 import java.util.Comparator;
  38 import java.util.List;
  39 import java.util.function.Function;
  40 import java.util.function.ToIntFunction;
  41 import java.util.function.ToLongFunction;
  42 import java.util.function.ToDoubleFunction;
  43 
  44 import static org.testng.Assert.assertEquals;
  45 import static org.testng.Assert.assertTrue;
  46 import static org.testng.Assert.fail;
  47 
  48 @Test(groups = "unit")
  49 public class BasicTest {
  50     private static class Thing {
  51         public final int intField;
  52         public final long longField;
  53         public final double doubleField;
  54         public final String stringField;
  55 
  56         private Thing(int intField, long longField, double doubleField, String stringField) {
  57             this.intField = intField;
  58             this.longField = longField;


 353             Comparator<People> cmp = Comparator.comparing(People::getFirstName, null);
 354             fail("comparing(f, null) should throw NPE");
 355         } catch (NullPointerException npe) {}
 356 
 357         try {
 358             Comparator<People> cmp = Comparator.comparing(null);
 359             fail("comparing(null) should throw NPE");
 360         } catch (NullPointerException npe) {}
 361         try {
 362             Comparator<People> cmp = Comparator.comparingInt(null);
 363             fail("comparing(null) should throw NPE");
 364         } catch (NullPointerException npe) {}
 365         try {
 366             Comparator<People> cmp = Comparator.comparingLong(null);
 367             fail("comparing(null) should throw NPE");
 368         } catch (NullPointerException npe) {}
 369         try {
 370             Comparator<People> cmp = Comparator.comparingDouble(null);
 371             fail("comparing(null) should throw NPE");
 372         } catch (NullPointerException npe) {}
 373     }
 374 
 375     @Test(dataProvider = "presorted")
 376     public void testAlphaDecimal(String[] sorted, Comparator<String> cmp) {
 377         for (int i = 0; i < sorted.length; ++i) {
 378             for (int j = 0; j < sorted.length; ++j) {
 379                 int res = cmp.compare(sorted[i], sorted[j]);
 380                 boolean ok = (res == 0 && i == j) ||
 381                         (res <= 0 && i < j) ||
 382                         (res >= 0 && i > j);
 383                 assertTrue(ok);
 384                 if (res == 0 && i != j) {
 385                     res = cmp.compare(sorted[j], sorted[i]);
 386                     assertTrue(res == 0);
 387                 }
 388             }
 389         }
 390     }
 391 
 392     // Simple case: sorting 1 .. 11
 393     private static String[] arr0 = new String[] {
 394             "java 1", "java 2", "java 3",
 395             "java 4", "java 5", "java 6",
 396             "java 7", "java 8", "java 9",
 397             "java 10", "java 11", "java 11" };
 398 
 399     // Leading zeros and multiple numeric parts
 400     private static String[] arr1 = new String[] {
 401             "string", "string0", "string00",
 402             "string1", "string01", "string001",
 403             "string2", "string02", "string002",
 404             "string002.a", "string002.a0", "string002.a1",
 405             "string0002", "string0002",
 406             "string1234", "string10234",
 407             "string100234", "string100234",
 408             "string1000234", "string1000234" };
 409 
 410     // Leading zeros first
 411     private static String[] arr2 = new String[] {
 412             "string", "string00", "string0",
 413             "string001", "string01", "string1",
 414             "string0002", "string0002", "string002",
 415             "string002.a", "string002.a0", "string002.a1",
 416             "string02", "string02", "string2", "string2" };
 417 
 418     // Sample from MSDN
 419     private static String[] arr3 = new String[] {
 420             "2string", "3string", "20string",
 421             "st2ring", "st3ring", "st20ring",
 422             "string2", "string3", "string20" };
 423 
 424     // Very long numbers
 425     private static String[] arr4 = new String[] {
 426             "q_0",
 427             "q_1",
 428             "q_1_",
 429             "q_10000000000000000000000000000000000000000",
 430             "q_10000000000000000000000000000000000000000",
 431             "q_20000000000000000000000000000000000000000",
 432             "q_100000000000000000000000000000000000000000",
 433             "q_500000000000000000000000000000000000000000",
 434             "q_10000000000000000000000000000000000000000000000000000000000000000000000000001",
 435             "q_10000000000000000000000000000000000000000000000000000000000000000000000000001",
 436             "q_20000000000000000000000000000000000000000000000000000000000000000000000000000",
 437             "q_20000000000000000000000000000000000000000000000000000000000000000000000000001",
 438             "q_200000000000000000000000000000000000000000000000000000000000000000000000000000",
 439             "y_1",
 440             "y_10000000000000000000000000000000000000000000000000000000000000000000000000000" };
 441 
 442     // Plain numbers
 443     private static String[] arr5 = new String[] {
 444             "",
 445             "",
 446             "0",
 447             "1",
 448             "2",
 449             "10",
 450             "10",
 451             "11",
 452             "15",
 453             "19",
 454             "20",
 455             "90",
 456             "100",
 457             "101",
 458             "1024",
 459             "2042",
 460             "2147483647",
 461             "2147483648", // Integer.MAX_VALUE
 462             "2147483649",
 463             "4294967296",
 464             "9223372036854775806",
 465             "9223372036854775807", // Long.MAX_VALUE
 466             "9223372036854775808",
 467             "18446744073709551615",
 468             "18446744073709551616",
 469             "8000000000000000000000000000000000000000000",
 470             "10000000000000000000000000000000000000000500",
 471             "10000000000000000000000000000000000000000500",
 472             "10000000000000000000000000000000000000000501",
 473             "20000000000000000000000000000000000000000500",
 474             "20000000000000000000700000000000000000000000",
 475     };
 476 
 477     // Plain alphabetical strings
 478     private static String[] arr6 = new String[] {
 479             "",
 480             "a",
 481             "a",
 482             "abcd",
 483             "abcdefgh",
 484             "b",
 485             "bcd",
 486             "bcdefgh",
 487             "z",
 488             "z ",
 489             "zabcd",
 490             "zbcd",
 491             "zcd",
 492             "zz",
 493     };
 494 
 495     // Case insensitive
 496     private static String[] arr7 = new String[] {
 497             "",
 498             "5",
 499             "42",
 500             "309",
 501             "a",
 502             "A",
 503             "a 1",
 504             "A 2",
 505             "a 15",
 506             "A 21",
 507             "case 42 Insensitive",
 508             "CaSe 42 inSEnsItiVe",
 509             "CAsE 42 INSENSitive",
 510             "caSe 42 INSENSITIVE",
 511             "caSe 42 INSENSITIVE",
 512     };
 513 
 514     private static String[] replaceDigits(String[] arr, int zeroCp) {
 515         return Arrays.asList(arr)
 516                 .stream()
 517                 .map(str -> {
 518                             StringBuilder sb = new StringBuilder();
 519                             int len = Character.codePointCount(str, 0, str.length());
 520                             for (int i = 0; i < len; ) {
 521                                 int cp = str.codePointAt(i);
 522                                 i += Character.charCount(cp);
 523                                 if (Character.isDigit(cp))
 524                                     cp = zeroCp + Character.digit(cp, 10);
 525                                 sb.appendCodePoint(cp);
 526                             }
 527                             return sb.toString();
 528                         }
 529                 ).toArray(String[]::new);
 530     }
 531 
 532     @DataProvider(name = "presorted")
 533     public Object[][] createPresortedArrays() {
 534         String[][] rev = {
 535             arr0.clone(), arr1.clone(), arr2.clone(), arr3.clone(),
 536             arr4.clone(), arr5.clone(), arr6.clone(), arr7.clone(),
 537         };
 538         for (String[] r : rev) {
 539             Collections.reverse(Arrays.asList(r));
 540         }
 541         Comparator<CharSequence> naturalOrder = Comparator.comparing(
 542                 CharSequence::toString, Comparator.naturalOrder());
 543         Comparator<CharSequence> caseInsOrder = Comparator.comparing(
 544                 CharSequence::toString, String::compareToIgnoreCase);
 545         int[] digitRangeStart = {
 546                 0x0030, // Latin1
 547                 0x0660, // Arabic-Indic
 548                 0x06f0, // Extended Arabic-Indic
 549                 0x0b66, // Oriya
 550                 0x0e50, // Thai
 551                 0xa9d0, // Javanese
 552                 0xff10, // Fullwidth
 553                 0x104a0, // Osmanya
 554                 0x118e0, // Warang
 555                 0x1d7ce, // Mathematical bold
 556                 0x1d7d8, // Mathematical double-struck
 557                 0x1d7e2, // Mathematical Sans-serif
 558                 0x1d7ec, // Mathematical Sans-serif bold
 559                 0x1d7f6, // Mathematical Monospace
 560         };
 561         List<Object[]> res = new ArrayList<>();
 562         for (int i = 0; i < digitRangeStart.length; ++i) {
 563             res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 564             res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed()});
 565             res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 566             res.add(new Object[] {replaceDigits(arr0, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed()});
 567             res.add(new Object[] {replaceDigits(arr1, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 568             res.add(new Object[] {replaceDigits(arr1, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed().reversed()});
 569             res.add(new Object[] {replaceDigits(arr2, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 570             res.add(new Object[] {replaceDigits(arr2, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed().reversed()});
 571             res.add(new Object[] {replaceDigits(arr3, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 572             res.add(new Object[] {replaceDigits(arr3, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 573             res.add(new Object[] {replaceDigits(arr4, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 574             res.add(new Object[] {replaceDigits(arr4, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 575             res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 576             res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed()});
 577             res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 578             res.add(new Object[] {replaceDigits(arr5, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed()});
 579             res.add(new Object[] {replaceDigits(arr6, digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder)});
 580             res.add(new Object[] {replaceDigits(arr6, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder)});
 581             res.add(new Object[] {replaceDigits(arr7, digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder)});
 582             res.add(new Object[] {replaceDigits(arr7, digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(caseInsOrder)});
 583             res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 584             res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed()});
 585             res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 586             res.add(new Object[] {replaceDigits(rev[0], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed()});
 587             res.add(new Object[] {replaceDigits(rev[1], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 588             res.add(new Object[] {replaceDigits(rev[2], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 589             res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 590             res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed().reversed().reversed().reversed().reversed()});
 591             res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 592             res.add(new Object[] {replaceDigits(rev[3], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed().reversed().reversed().reversed().reversed()});
 593             res.add(new Object[] {replaceDigits(rev[4], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 594             res.add(new Object[] {replaceDigits(rev[4], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 595             res.add(new Object[] {replaceDigits(rev[5], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 596             res.add(new Object[] {replaceDigits(rev[5], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 597             res.add(new Object[] {replaceDigits(rev[6], digitRangeStart[i]), Comparator.comparingAlphaDecimal(naturalOrder).reversed()});
 598             res.add(new Object[] {replaceDigits(rev[6], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(naturalOrder).reversed()});
 599             res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder).reversed()});
 600             res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimal(caseInsOrder).reversed().reversed().reversed()});
 601             res.add(new Object[] {replaceDigits(rev[7], digitRangeStart[i]), Comparator.comparingAlphaDecimalLeadingZeroesFirst(caseInsOrder).reversed()});
 602         }
 603         return res.toArray(new Object[res.size()][]);
 604     }
 605 }
< prev index next >