jdk/test/java/util/Objects/BasicObjectsTest.java
changeset 4165 7cd799c224da
parent 4061 35a627ad2443
child 5506 202f599c92aa
equal deleted inserted replaced
4164:7ba55fbef5be 4165:7cd799c224da
    21  * have any questions.
    21  * have any questions.
    22  */
    22  */
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 6797535
    26  * @bug 6797535 6889858 6891113
    27  * @summary Basic tests for methods in java.util.Objects
    27  * @summary Basic tests for methods in java.util.Objects
    28  * @author  Joseph D. Darcy
    28  * @author  Joseph D. Darcy
    29  */
    29  */
    30 
    30 
    31 import java.util.*;
    31 import java.util.*;
    32 
    32 
    33 public class BasicObjectsTest {
    33 public class BasicObjectsTest {
    34     public static void main(String... args) {
    34     public static void main(String... args) {
    35         int errors = 0;
    35         int errors = 0;
    36         errors += testEquals();
    36         errors += testEquals();
       
    37         errors += testDeepEquals();
    37         errors += testHashCode();
    38         errors += testHashCode();
       
    39         errors += testHash();
    38         errors += testToString();
    40         errors += testToString();
       
    41         errors += testToString2();
    39         errors += testCompare();
    42         errors += testCompare();
    40         errors += testNonNull();
    43         errors += testNonNull();
    41         if (errors > 0 )
    44         if (errors > 0 )
    42             throw new RuntimeException();
    45             throw new RuntimeException();
    43     }
    46     }
    58                 }
    61                 }
    59             }
    62             }
    60         return errors;
    63         return errors;
    61     }
    64     }
    62 
    65 
       
    66     private static int testDeepEquals() {
       
    67         int errors = 0;
       
    68         Object[] values = {null,
       
    69                            null, // Change to values later
       
    70                            new byte[]  {(byte)1},
       
    71                            new short[] {(short)1},
       
    72                            new int[]   {1},
       
    73                            new long[]  {1L},
       
    74                            new char[]  {(char)1},
       
    75                            new float[] {1.0f},
       
    76                            new double[]{1.0d},
       
    77                            new String[]{"one"}};
       
    78         values[1] = values;
       
    79 
       
    80         for(int i = 0; i < values.length; i++)
       
    81             for(int j = 0; j < values.length; j++) {
       
    82                 boolean expected = (i == j);
       
    83                 Object a = values[i];
       
    84                 Object b = values[j];
       
    85                 boolean result = Objects.deepEquals(a, b);
       
    86                 if (result != expected) {
       
    87                     errors++;
       
    88                     System.err.printf("When equating %s to %s, got %b instead of %b%n.",
       
    89                                       a, b, result, expected);
       
    90                 }
       
    91             }
       
    92 
       
    93         return errors;
       
    94     }
       
    95 
    63     private static int testHashCode() {
    96     private static int testHashCode() {
    64         int errors = 0;
    97         int errors = 0;
    65         errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
    98         errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
    66         String s = "42";
    99         String s = "42";
    67         errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
   100         errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
    68         return errors;
   101         return errors;
    69     }
   102     }
    70 
   103 
       
   104     private static int testHash() {
       
   105         int errors = 0;
       
   106 
       
   107         Object[] data = new String[]{"perfect", "ham", "THC"};
       
   108 
       
   109         errors += ((Objects.hash((Object[])null) == 0) ? 0 : 1);
       
   110 
       
   111         errors += (Objects.hash("perfect", "ham", "THC") ==
       
   112                    Arrays.hashCode(data)) ? 0 : 1;
       
   113 
       
   114         return errors;
       
   115     }
       
   116 
    71     private static int testToString() {
   117     private static int testToString() {
    72         int errors = 0;
   118         int errors = 0;
    73         errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
   119         errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
    74         String s = "Some string";
   120         String s = "Some string";
    75         errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
   121         errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
       
   122         return errors;
       
   123     }
       
   124 
       
   125     private static int testToString2() {
       
   126         int errors = 0;
       
   127         String s = "not the default";
       
   128         errors += (s.equals(Objects.toString(null, s)) ) ? 0 : 1;
       
   129         errors += (s.equals(Objects.toString(s, "another string")) ) ? 0 : 1;
    76         return errors;
   130         return errors;
    77     }
   131     }
    78 
   132 
    79     private static int testCompare() {
   133     private static int testCompare() {
    80         int errors = 0;
   134         int errors = 0;