Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Objects/BasicObjectsTest.java
38811 views
/*1* Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 6797535 6889858 6891113 8013712 8011800 801436526* @summary Basic tests for methods in java.util.Objects27* @author Joseph D. Darcy28*/2930import java.util.*;31import java.util.function.*;3233public class BasicObjectsTest {34public static void main(String... args) {35int errors = 0;36errors += testEquals();37errors += testDeepEquals();38errors += testHashCode();39errors += testHash();40errors += testToString();41errors += testToString2();42errors += testCompare();43errors += testRequireNonNull();44errors += testIsNull();45errors += testNonNull();46if (errors > 0 )47throw new RuntimeException();48}4950private static int testEquals() {51int errors = 0;52Object[] values = {null, "42", 42};53for(int i = 0; i < values.length; i++)54for(int j = 0; j < values.length; j++) {55boolean expected = (i == j);56Object a = values[i];57Object b = values[j];58boolean result = Objects.equals(a, b);59if (result != expected) {60errors++;61System.err.printf("When equating %s to %s, got %b instead of %b%n.",62a, b, result, expected);63}64}65return errors;66}6768private static int testDeepEquals() {69int errors = 0;70Object[] values = {null,71null, // Change to values later72new byte[] {(byte)1},73new short[] {(short)1},74new int[] {1},75new long[] {1L},76new char[] {(char)1},77new float[] {1.0f},78new double[]{1.0d},79new String[]{"one"}};80values[1] = values;8182for(int i = 0; i < values.length; i++)83for(int j = 0; j < values.length; j++) {84boolean expected = (i == j);85Object a = values[i];86Object b = values[j];87boolean result = Objects.deepEquals(a, b);88if (result != expected) {89errors++;90System.err.printf("When equating %s to %s, got %b instead of %b%n.",91a, b, result, expected);92}93}9495return errors;96}9798private static int testHashCode() {99int errors = 0;100errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;101String s = "42";102errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;103return errors;104}105106private static int testHash() {107int errors = 0;108109Object[] data = new String[]{"perfect", "ham", "THC"};110111errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);112113errors += (Objects.hash("perfect", "ham", "THC") ==114Arrays.hashCode(data)) ? 0 : 1;115116return errors;117}118119private static int testToString() {120int errors = 0;121errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;122String s = "Some string";123errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;124return errors;125}126127private static int testToString2() {128int errors = 0;129String s = "not the default";130errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;131errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;132return errors;133}134135private static int testCompare() {136int errors = 0;137String[] values = {"e. e. cummings", "zzz"};138String[] VALUES = {"E. E. Cummings", "ZZZ"};139errors += compareTest(null, null, 0);140for(int i = 0; i < values.length; i++) {141String a = values[i];142errors += compareTest(a, a, 0);143for(int j = 0; j < VALUES.length; j++) {144int expected = Integer.compare(i, j);145String b = VALUES[j];146errors += compareTest(a, b, expected);147}148}149return errors;150}151152private static int compareTest(String a, String b, int expected) {153int errors = 0;154int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);155if (Integer.signum(result) != Integer.signum(expected)) {156errors++;157System.err.printf("When comparing %s to %s, got %d instead of %d%n.",158a, b, result, expected);159}160return errors;161}162163private static int testRequireNonNull() {164int errors = 0;165166final String RNN_1 = "1-arg requireNonNull";167final String RNN_2 = "2-arg requireNonNull";168final String RNN_3 = "Supplier requireNonNull";169170Function<String, String> rnn1 = s -> Objects.requireNonNull(s);171Function<String, String> rnn2 = s -> Objects.requireNonNull(s, "trousers");172Function<String, String> rnn3 = s -> Objects.requireNonNull(s, () -> "trousers");173174errors += testRNN_NonNull(rnn1, RNN_1);175errors += testRNN_NonNull(rnn2, RNN_2);176errors += testRNN_NonNull(rnn3, RNN_3);177178errors += testRNN_Null(rnn1, RNN_1, null);179errors += testRNN_Null(rnn2, RNN_2, "trousers");180errors += testRNN_Null(rnn3, RNN_3, "trousers");181return errors;182}183184private static int testRNN_NonNull(Function<String, String> testFunc,185String testFuncName) {186int errors = 0;187try {188String s = testFunc.apply("pants");189if (s != "pants") {190System.err.printf(testFuncName + " failed to return its arg");191errors++;192}193} catch (NullPointerException e) {194System.err.printf(testFuncName + " threw unexpected NPE");195errors++;196}197return errors;198}199200private static int testRNN_Null(Function<String, String> testFunc,201String testFuncName,202String expectedMessage) {203int errors = 0;204try {205String s = testFunc.apply(null);206System.err.printf(testFuncName + " failed to throw NPE");207errors++;208} catch (NullPointerException e) {209if (e.getMessage() != expectedMessage) {210System.err.printf(testFuncName + " threw NPE w/ bad detail msg");211errors++;212}213}214return errors;215}216217private static int testIsNull() {218int errors = 0;219220errors += Objects.isNull(null) ? 0 : 1;221errors += Objects.isNull(Objects.class) ? 1 : 0;222223return errors;224}225226private static int testNonNull() {227int errors = 0;228229errors += Objects.nonNull(null) ? 1 : 0;230errors += Objects.nonNull(Objects.class) ? 0 : 1;231232return errors;233}234}235236237