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 }
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);
153 if (Integer.signum(result) != Integer.signum(expected)) {
154 errors++;
155 System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
156 a, b, result, expected);
157 }
158 return errors;
159 }
160
161 private static int testNonNull() {
162 int errors = 0;
163 String s;
164
165 // Test 1-arg variant
166 try {
167 s = Objects.requireNonNull("pants");
168 if (s != "pants") {
169 System.err.printf("1-arg non-null failed to return its arg");
170 errors++;
171 }
172 } catch (NullPointerException e) {
173 System.err.printf("1-arg nonNull threw unexpected NPE");
174 errors++;
175 }
176
177 try {
178 s = Objects.requireNonNull(null);
179 System.err.printf("1-arg nonNull failed to throw NPE");
180 errors++;
181 } catch (NullPointerException e) {
187 s = Objects.requireNonNull("pants", "trousers");
188 if (s != "pants") {
189 System.err.printf("2-arg nonNull failed to return its arg");
190 errors++;
191 }
192 } catch (NullPointerException e) {
193 System.err.printf("2-arg nonNull threw unexpected NPE");
194 errors++;
195 }
196
197 try {
198 s = Objects.requireNonNull(null, "pantaloons");
199 System.err.printf("2-arg nonNull failed to throw NPE");
200 errors++;
201 } catch (NullPointerException e) {
202 if (e.getMessage() != "pantaloons") {
203 System.err.printf("2-arg nonNull threw NPE w/ bad detail msg");
204 errors++;
205 }
206 }
207 return errors;
208 }
209 }
|
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 += testRequireNonNull();
44 errors += testIsNull();
45 errors += testNonNull();
46 if (errors > 0 )
47 throw new RuntimeException();
48 }
49
50 private static int testEquals() {
51 int errors = 0;
52 Object[] values = {null, "42", 42};
53 for(int i = 0; i < values.length; i++)
54 for(int j = 0; j < values.length; j++) {
55 boolean expected = (i == j);
56 Object a = values[i];
57 Object b = values[j];
58 boolean result = Objects.equals(a, b);
59 if (result != expected) {
60 errors++;
61 System.err.printf("When equating %s to %s, got %b instead of %b%n.",
62 a, b, result, expected);
63 }
64 }
143 for(int j = 0; j < VALUES.length; j++) {
144 int expected = Integer.compare(i, j);
145 String b = VALUES[j];
146 errors += compareTest(a, b, expected);
147 }
148 }
149 return errors;
150 }
151
152 private static int compareTest(String a, String b, int expected) {
153 int errors = 0;
154 int result = Objects.compare(a, b, String.CASE_INSENSITIVE_ORDER);
155 if (Integer.signum(result) != Integer.signum(expected)) {
156 errors++;
157 System.err.printf("When comparing %s to %s, got %d instead of %d%n.",
158 a, b, result, expected);
159 }
160 return errors;
161 }
162
163 private static int testRequireNonNull() {
164 int errors = 0;
165 String s;
166
167 // Test 1-arg variant
168 try {
169 s = Objects.requireNonNull("pants");
170 if (s != "pants") {
171 System.err.printf("1-arg non-null failed to return its arg");
172 errors++;
173 }
174 } catch (NullPointerException e) {
175 System.err.printf("1-arg nonNull threw unexpected NPE");
176 errors++;
177 }
178
179 try {
180 s = Objects.requireNonNull(null);
181 System.err.printf("1-arg nonNull failed to throw NPE");
182 errors++;
183 } catch (NullPointerException e) {
189 s = Objects.requireNonNull("pants", "trousers");
190 if (s != "pants") {
191 System.err.printf("2-arg nonNull failed to return its arg");
192 errors++;
193 }
194 } catch (NullPointerException e) {
195 System.err.printf("2-arg nonNull threw unexpected NPE");
196 errors++;
197 }
198
199 try {
200 s = Objects.requireNonNull(null, "pantaloons");
201 System.err.printf("2-arg nonNull failed to throw NPE");
202 errors++;
203 } catch (NullPointerException e) {
204 if (e.getMessage() != "pantaloons") {
205 System.err.printf("2-arg nonNull threw NPE w/ bad detail msg");
206 errors++;
207 }
208 }
209 return errors;
210 }
211
212 private static int testIsNull() {
213 int errors = 0;
214
215 errors += Objects.isNull(null) ? 0 : 1;
216 errors += Objects.isNull(Objects.class) ? 1 : 0;
217
218 return errors;
219 }
220
221 private static int testNonNull() {
222 int errors = 0;
223
224 errors += Objects.nonNull(null) ? 1 : 0;
225 errors += Objects.nonNull(Objects.class) ? 0 : 1;
226
227 return errors;
228 }
229 }
|