< prev index next >

test/jdk/java/util/Objects/BasicObjectsTest.java

Print this page

 28  */
 29 
 30 import java.util.*;
 31 import java.util.function.*;
 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 += testToIdentityString();
 43         errors += testCompare();
 44         errors += testRequireNonNull();
 45         errors += testIsNull();
 46         errors += testNonNull();
 47         errors += testNonNullOf();

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

289         errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
290         errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
291         errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
292 
293         try {
294             Objects.requireNonNullElseGet(null, () -> null);
295             errors += 1;
296         } catch (NullPointerException npe) {
297             // expected
298             errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
299         }
300         try {       // supplier is null
301             Objects.requireNonNullElseGet(null, null);
302             errors += 1;
303         } catch (NullPointerException npe) {
304             // expected
305             errors += npe.getMessage().equals("supplier") ? 0 : 1;
306         }
307         return errors;
308     }















309 }

 28  */
 29 
 30 import java.util.*;
 31 import java.util.function.*;
 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 += testToIdentityString();
 43         errors += testCompare();
 44         errors += testRequireNonNull();
 45         errors += testIsNull();
 46         errors += testNonNull();
 47         errors += testNonNullOf();
 48         errors += testNewIdentity();
 49         if (errors > 0 )
 50             throw new RuntimeException();
 51     }
 52 
 53     private static int testEquals() {
 54         int errors = 0;
 55         Object[] values = {null, "42", 42};
 56         for(int i = 0; i < values.length; i++)
 57             for(int j = 0; j < values.length; j++) {
 58                 boolean expected = (i == j);
 59                 Object a = values[i];
 60                 Object b = values[j];
 61                 boolean result = Objects.equals(a, b);
 62                 if (result != expected) {
 63                     errors++;
 64                     System.err.printf("When equating %s to %s, got %b instead of %b%n.",
 65                                       a, b, result, expected);
 66                 }
 67             }
 68         return errors;

290         errors += (Objects.requireNonNullElseGet(nullString, () -> defString) == defString) ? 0 : 1;
291         errors += (Objects.requireNonNullElseGet(nonNullString, () -> defString) == nonNullString) ? 0 : 1;
292         errors += (Objects.requireNonNullElseGet(nonNullString, () -> null) == nonNullString) ? 0 : 1;
293 
294         try {
295             Objects.requireNonNullElseGet(null, () -> null);
296             errors += 1;
297         } catch (NullPointerException npe) {
298             // expected
299             errors += npe.getMessage().equals("supplier.get()") ? 0 : 1;
300         }
301         try {       // supplier is null
302             Objects.requireNonNullElseGet(null, null);
303             errors += 1;
304         } catch (NullPointerException npe) {
305             // expected
306             errors += npe.getMessage().equals("supplier") ? 0 : 1;
307         }
308         return errors;
309     }
310 
311     private static int testNewIdentity() {
312         int errors = 0;
313 
314         Object o1 = Objects.newIdentity();
315         Object o2 = Objects.newIdentity();
316 
317         if (o1 == null || o2 == null)
318             errors += 1;
319 
320         if (o1 == o2)
321             errors += 1;
322 
323         return errors;
324     }
325 }
< prev index next >