jdk/test/java/util/function/BinaryOperator/BasicTest.java
author ksrini
Mon, 14 Mar 2016 15:04:57 -0700
changeset 36705 890c250d8da8
parent 23010 6dadb192ad81
permissions -rw-r--r--
8071982: Update tests for revamped Doclet API 8071984: Update test cases for repeating and type annotations output in javadoc Reviewed-by: ksrini, bpatel Contributed-by: oleg.barbashov@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 19605
diff changeset
     2
 * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     4
 *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     8
 *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    13
 * accompanied this code).
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    14
 *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    18
 *
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    21
 * questions.
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    22
 */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    23
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    24
/*
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    25
 * @test
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    26
 * @bug 8009736 8010279
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    27
 * @run testng BasicTest
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    28
 */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    29
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    30
import java.util.Comparator;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    31
import java.util.function.BinaryOperator;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    32
import java.util.function.Function;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    33
import java.util.function.ToIntFunction;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    34
import org.testng.annotations.Test;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    35
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    36
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    37
import static java.util.function.BinaryOperator.minBy;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    38
import static java.util.function.BinaryOperator.maxBy;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    39
import static org.testng.Assert.assertSame;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    40
import static org.testng.Assert.fail;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    41
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    42
/**
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    43
 * Unit tests for helper methods in Comparators
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    44
 */
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    45
@Test(groups = "unit")
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    46
public class BasicTest {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    47
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    48
    private static class People {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    49
        final String firstName;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    50
        final String lastName;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    51
        final int age;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    52
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    53
        People(String first, String last, int age) {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    54
            firstName = first;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    55
            lastName = last;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    56
            this.age = age;
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    57
        }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    58
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    59
        String getFirstName() { return firstName; }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    60
        String getLastName() { return lastName; }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    61
        int getAge() { return age; }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    62
    }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    63
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    64
    private final People people[] = {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    65
        new People("John", "Doe", 34),
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    66
        new People("Mary", "Doe", 30),
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    67
    };
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    68
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    69
    public void testMaxBy() {
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    70
        Comparator<People> cmp = Comparator.comparing(People::getFirstName);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    71
        // lesser
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    72
        assertSame(maxBy(cmp).apply(people[0], people[1]), people[1]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    73
        // euqal
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    74
        cmp = Comparator.comparing(People::getLastName);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    75
        assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    76
        // greater
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    77
        cmp = Comparator.comparingInt(People::getAge);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    78
        assertSame(maxBy(cmp).apply(people[0], people[1]), people[0]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    79
    }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    80
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    81
    public void testMinBy() {
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    82
        Comparator<People> cmp = Comparator.comparing(People::getFirstName);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    83
        // lesser
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    84
        assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    85
        // euqal
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    86
        cmp = Comparator.comparing(People::getLastName);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    87
        assertSame(minBy(cmp).apply(people[0], people[1]), people[0]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    88
        // greater
19605
0f98f8e3bdcb 8023528: Rename Comparator combinators to disambiguate overloading methods
henryjen
parents: 18571
diff changeset
    89
        cmp = Comparator.comparingInt(People::getAge);
18571
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    90
        assertSame(minBy(cmp).apply(people[0], people[1]), people[1]);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    91
    }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    92
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    93
    public void testNulls() {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    94
        try {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    95
            BinaryOperator<String> op = minBy(null);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    96
            fail("minBy(null) should throw NPE");
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    97
        } catch (NullPointerException npe) {}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    98
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
    99
        try {
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
   100
            BinaryOperator<String> op = maxBy(null);
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
   101
            fail("maxBy(null) should throw NPE");
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
   102
        } catch (NullPointerException npe) {}
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
   103
    }
8e3cb3c46ae8 8009736: Comparator API cleanup
henryjen
parents:
diff changeset
   104
}