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 * @key randomness
30 */
31
32 import java.util.TreeMap;
33 import java.util.Comparator;
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36
37 import java.util.Arrays;
38 import java.util.Collections;
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 testNumerical(String[] sorted, Comparator<String> cmp) {
377 String[] shuffled = sorted.clone();
378 Collections.shuffle(Arrays.asList(shuffled));
379 Arrays.sort(shuffled, cmp);
380 assertEquals(shuffled, sorted);
381 }
382
383 // Simple case: sorting 1 .. 11
384 private static String[] arr0 = new String[] {
385 "java 1", "java 2", "java 3",
386 "java 4", "java 5", "java 6",
387 "java 7", "java 8", "java 9",
388 "java 10", "java 11" };
389
390 // Leading zeros and multiple numeric parts
391 private static String[] arr1 = new String[] {
392 "string", "string0", "string00",
393 "string1", "string01", "string001",
394 "string2", "string02", "string002",
395 "string002.a", "string002.a0", "string002.a1",
396 "string0002", "string0002" };
397
398 // Leading zeros first
399 private static String[] arr2 = new String[] {
400 "string", "string00", "string0",
401 "string001", "string01", "string1",
402 "string0002", "string0002", "string002",
403 "string002.a", "string002.a0", "string002.a1",
404 "string02", "string2" };
405
406 // Sample from MSDN
407 private static String[] arr3 = new String[] {
408 "2string", "3string", "20string",
409 "st2ring", "st3ring", "st20ring",
410 "string2", "string3", "string20" };
411
412 // Fullwidth characters
413 private static String[] arr4 = new String[] {
414 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10",
415 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff10",
416 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff10\uff10",
417 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11",
418 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff10\uff11",
419 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff12",
420 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff13",
421 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff14",
422 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff15",
423 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff16",
424 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff17",
425 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff18",
426 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff19",
427 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff10",
428 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff11",
429 "\uff53\uff54\uff52\uff49\uff4e\uff47 \uff11\uff12" };
430
431 // Very long numbers
432 private static String[] arr5 = new String[] {
433 "q_0",
434 "q_1",
435 "q_1_",
436 "q_10000000000000000000000000000000000000000",
437 "q_20000000000000000000000000000000000000000",
438 "q_100000000000000000000000000000000000000000",
439 "q_500000000000000000000000000000000000000000",
440 "q_10000000000000000000000000000000000000000000000000000000000000000000000000001",
441 "q_20000000000000000000000000000000000000000000000000000000000000000000000000000",
442 "q_20000000000000000000000000000000000000000000000000000000000000000000000000001",
443 "q_200000000000000000000000000000000000000000000000000000000000000000000000000000",
444 "y_1",
445 "y_10000000000000000000000000000000000000000000000000000000000000000000000000000" };
446
447 @DataProvider(name = "presorted")
448 public Object[][] createPresortedArrays() {
449 String[][] rev = {
450 arr0.clone(), arr1.clone(), arr2.clone(),
451 arr3.clone(), arr4.clone(), arr5.clone(),
452 };
453 for (String[] r : rev) {
454 Collections.reverse(Arrays.asList(r));
455 }
456 return new Object[][] {
457 {arr0, Comparator.comparingNumerically()},
458 {arr0, Comparator.comparingNumerically().reversed().reversed()},
459 {arr0, Comparator.comparingNumericallyLeadingZerosFirst()},
460 {arr0, Comparator.comparingNumericallyLeadingZerosFirst().reversed().reversed()},
461 {arr1, Comparator.comparingNumerically()},
462 {arr1, Comparator.comparingNumerically().reversed().reversed().reversed().reversed()},
463 {arr2, Comparator.comparingNumericallyLeadingZerosFirst()},
464 {arr2, Comparator.comparingNumericallyLeadingZerosFirst().reversed().reversed().reversed().reversed()},
465 {arr3, Comparator.comparingNumerically()},
466 {arr3, Comparator.comparingNumericallyLeadingZerosFirst()},
467 {arr4, Comparator.comparingNumerically()},
468 {arr5, Comparator.comparingNumerically()},
469 {arr5, Comparator.comparingNumericallyLeadingZerosFirst()},
470 {rev[0], Comparator.comparingNumerically().reversed()},
471 {rev[0], Comparator.comparingNumerically().reversed().reversed().reversed()},
472 {rev[0], Comparator.comparingNumericallyLeadingZerosFirst().reversed()},
473 {rev[0], Comparator.comparingNumericallyLeadingZerosFirst().reversed().reversed().reversed()},
474 {rev[1], Comparator.comparingNumerically().reversed()},
475 {rev[2], Comparator.comparingNumericallyLeadingZerosFirst().reversed()},
476 {rev[3], Comparator.comparingNumerically().reversed()},
477 {rev[3], Comparator.comparingNumerically().reversed().reversed().reversed().reversed().reversed()},
478 {rev[3], Comparator.comparingNumericallyLeadingZerosFirst().reversed()},
479 {rev[3], Comparator.comparingNumericallyLeadingZerosFirst().reversed().reversed().reversed().reversed().reversed()},
480 {rev[4], Comparator.comparingNumerically().reversed()},
481 {rev[5], Comparator.comparingNumerically().reversed()},
482 {rev[5], Comparator.comparingNumericallyLeadingZerosFirst().reversed()},
483 };
484 }
485 }
|