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 if (errors > 0 )
41 throw new RuntimeException();
42 }
43
44 private static int testEquals() {
45 int errors = 0;
46 Object[] values = {null, "42", 42};
47 for(int i = 0; i < values.length; i++)
48 for(int j = 0; j < values.length; j++) {
49 boolean expected = (i == j);
50 Object a = values[i];
51 Object b = values[j];
52 boolean result = Objects.equals(a, b);
53 if (result != expected) {
54 errors++;
55 System.err.printf("When equating %s to %s, got %b instead of %b%n.",
56 a, b, result, expected);
57 }
58 }
59 return errors;
85 errors += compareTest(a, a, 0);
86 for(int j = 0; j < VALUES.length; j++) {
87 int expected = Integer.compare(i, j);
88 String b = VALUES[j];
89 errors += compareTest(a, b, expected);
90 }
91 }
92 return errors;
93 }
94
95 private static int compareTest(String a, String b, int expected) {
96 int errors = 0;
97 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
98 if (Integer.signum(result) != Integer.signum(expected)) {
99 errors++;
100 System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
101 a, b, result, expected);
102 }
103 return errors;
104 }
105 }
|
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;
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);
99 if (Integer.signum(result) != Integer.signum(expected)) {
100 errors++;
101 System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
102 a, b, result, expected);
103 }
104 return errors;
105 }
106
107 private static int testNonNull() {
108 int errors = 0;
109 String s;
110
111 // Test 1-arg variant
112 try {
113 s = Objects.nonNull("pants");
114 if (s != "pants") {
115 System.err.printf("1-arg non-null failed to return its arg");
116 errors++;
117 }
118 } catch (NullPointerException e) {
119 System.err.printf("1-arg nonNull threw unexpected NPE");
120 errors++;
121 }
122
123 try {
124 s = Objects.nonNull(null);
125 System.err.printf("1-arg nonNull failed to throw NPE");
126 errors++;
127 } catch (NullPointerException e) {
128 // Expected
129 }
130
131 // Test 2-arg variant
132 try {
133 s = Objects.nonNull("pants", "trousers");
134 if (s != "pants") {
135 System.err.printf("2-arg nonNull failed to return its arg");
136 errors++;
137 }
138 } catch (NullPointerException e) {
139 System.err.printf("2-arg nonNull threw unexpected NPE");
140 errors++;
141 }
142
143 try {
144 s = Objects.nonNull(null, "pantaloons");
145 System.err.printf("2-arg nonNull failed to throw NPE");
146 errors++;
147 } catch (NullPointerException e) {
148 if (e.getMessage() != "pantaloons") {
149 System.err.printf("2-arg nonNull threw NPE w/ bad detail msg");
150 errors++;
151 }
152 }
153 return errors;
154 }
155 }
|