test/java/util/Objects/BasicObjectsTest.java

Print this page




   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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6797535
  27  * @summary Basic tests for methods in java.util.Objects
  28  * @author  Joseph D. Darcy
  29  */
  30 
  31 import java.util.*;
  32 
  33 public class BasicObjectsTest {
  34     public static void main(String... args) {
  35         int errors = 0;
  36         errors += testEquals();

  37         errors += testHashCode();

  38         errors += testToString();

  39         errors += testCompare();
  40         errors += testNonNull();
  41         if (errors > 0 )
  42             throw new RuntimeException();
  43     }
  44 
  45     private static int testEquals() {
  46         int errors = 0;
  47         Object[] values = {null, "42", 42};
  48         for(int i = 0; i < values.length; i++)
  49             for(int j = 0; j < values.length; j++) {
  50                 boolean expected = (i == j);
  51                 Object a = values[i];
  52                 Object b = values[j];
  53                 boolean result = Objects.equals(a, b);
  54                 if (result != expected) {
  55                     errors++;
  56                     System.err.printf("When equating %s to %s, got %b instead of %b%n.",
  57                                       a, b, result, expected);
  58                 }
  59             }
  60         return errors;
  61     }
  62 






























  63     private static int testHashCode() {
  64         int errors = 0;
  65         errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
  66         String s = "42";
  67         errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
  68         return errors;
  69     }
  70 













  71     private static int testToString() {
  72         int errors = 0;
  73         errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
  74         String s = "Some string";
  75         errors += (s.equals(Objects.toString(s)) ) ? 0 : 1;
  76         return errors;
  77     }
  78 








  79     private static int testCompare() {
  80         int errors = 0;
  81         String[] values = {"e. e. cummings", "zzz"};
  82         String[] VALUES = {"E. E. Cummings", "ZZZ"};
  83         errors += compareTest(null, null, 0);
  84         for(int i = 0; i < values.length; i++) {
  85             String a = values[i];
  86             errors += compareTest(a, a, 0);
  87             for(int j = 0; j < VALUES.length; j++) {
  88                 int expected = Integer.compare(i, j);
  89                 String b = VALUES[j];
  90                 errors += compareTest(a, b, expected);
  91             }
  92         }
  93         return errors;
  94     }
  95 
  96     private static int compareTest(String a, String b, int expected) {
  97         int errors = 0;
  98         int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);




   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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 6797535 6889858 6891113
  27  * @summary Basic tests for methods in java.util.Objects
  28  * @author  Joseph D. Darcy
  29  */
  30 
  31 import java.util.*;
  32 
  33 public class BasicObjectsTest {
  34     public static void main(String... args) {
  35         int errors = 0;
  36         errors += testEquals();
  37         errors += testDeepEquals();
  38         errors += testHashCode();
  39         errors += testHash();
  40         errors += testToString();
  41         errors += testToString2();
  42         errors += testCompare();
  43         errors += testNonNull();
  44         if (errors > 0 )
  45             throw new RuntimeException();
  46     }
  47 
  48     private static int testEquals() {
  49         int errors = 0;
  50         Object[] values = {null, "42", 42};
  51         for(int i = 0; i < values.length; i++)
  52             for(int j = 0; j < values.length; j++) {
  53                 boolean expected = (i == j);
  54                 Object a = values[i];
  55                 Object b = values[j];
  56                 boolean result = Objects.equals(a, b);
  57                 if (result != expected) {
  58                     errors++;
  59                     System.err.printf("When equating %s to %s, got %b instead of %b%n.",
  60                                       a, b, result, expected);
  61                 }
  62             }
  63         return errors;
  64     }
  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 
  96     private static int testHashCode() {
  97         int errors = 0;
  98         errors += (Objects.hashCode(null) == 0 ) ? 0 : 1;
  99         String s = "42";
 100         errors += (Objects.hashCode(s) == s.hashCode() ) ? 0 : 1;
 101         return errors;
 102     }
 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 
 117     private static int testToString() {
 118         int errors = 0;
 119         errors += ("null".equals(Objects.toString(null)) ) ? 0 : 1;
 120         String s = "Some string";
 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;
 130         return errors;
 131     }
 132 
 133     private static int testCompare() {
 134         int errors = 0;
 135         String[] values = {"e. e. cummings", "zzz"};
 136         String[] VALUES = {"E. E. Cummings", "ZZZ"};
 137         errors += compareTest(null, null, 0);
 138         for(int i = 0; i < values.length; i++) {
 139             String a = values[i];
 140             errors += compareTest(a, a, 0);
 141             for(int j = 0; j < VALUES.length; j++) {
 142                 int expected = Integer.compare(i, j);
 143                 String b = VALUES[j];
 144                 errors += compareTest(a, b, expected);
 145             }
 146         }
 147         return errors;
 148     }
 149 
 150     private static int compareTest(String a, String b, int expected) {
 151         int errors = 0;
 152         int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);