1 /*
2 * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /*
25 * @test
26 * @bug 8302202
27 * @summary Testing record patterns with null components
28 * @enablePreview
29 * @compile NullsInDeconstructionPatterns2.java
30 * @run main NullsInDeconstructionPatterns2
31 * @ignore Verifier error
32 */
33
34 import java.util.Objects;
35 import java.util.function.Function;
36
37 public class NullsInDeconstructionPatterns2 {
38
39 public static void main(String[] args) {
40 new NullsInDeconstructionPatterns2().run();
41 }
42
43 private void run() {
44 run1(this::test1a);
45 run1(this::test1b);
46 run2(this::test2a);
47 run2(this::test2b);
48 run3(this::test3a);
49 run3(this::test3b);
50 run4();
51 }
52
53 private void run1(Function<Object, String> method) {
54 assertEquals("R1(null)", method.apply(new R1(null)));
55 assertEquals("R1(!null)", method.apply(new R1("")));
56 }
57
58 private void run2(Function<Object, String> method) {
59 assertEquals("R2(null, null)", method.apply(new R2(null, null)));
60 assertEquals("R2(!null, null)", method.apply(new R2("", null)));
61 assertEquals("R2(null, !null)", method.apply(new R2(null, "")));
62 assertEquals("R2(!null, !null)", method.apply(new R2("", "")));
63 }
64
65 private void run3(Function<Object, String> method) {
66 assertEquals("R3(null, null, null)", method.apply(new R3(null, null, null)));
67 assertEquals("R3(!null, null, null)", method.apply(new R3("", null, null)));
68 assertEquals("R3(null, !null, null)", method.apply(new R3(null, "", null)));
69 assertEquals("R3(!null, !null, null)", method.apply(new R3("", "", null)));
70 assertEquals("R3(null, null, !null)", method.apply(new R3(null, null, "")));
71 assertEquals("R3(!null, null, !null)", method.apply(new R3("", null, "")));
72 assertEquals("R3(null, !null, !null)", method.apply(new R3(null, "", "")));
73 assertEquals("R3(!null, !null, !null)", method.apply(new R3("", "", "")));
74 }
75
76 private void run4() {
77 assertEquals("integer", test4(new R1(0)));
78 assertEquals("empty", test4(new R1("")));
79 assertEquals("default", test4(new R1("a")));
80 }
81 private String test1a(Object i) {
82 return switch (i) {
83 case R1(Object o) when o == null -> "R1(null)";
84 case R1(Object o) when o != null -> "R1(!null)";
85 default -> "default";
86 };
87 }
88
89 private String test1b(Object i) {
90 return switch (i) {
91 case R1(Object o) when o == null -> "R1(null)";
92 case R1(Object o) -> "R1(!null)";
93 default -> "default";
94 };
95 }
96
97 private String test2a(Object i) {
98 return switch (i) {
99 case R2(Object o1, Object o2) when o1 == null && o2 == null -> "R2(null, null)";
100 case R2(Object o1, Object o2) when o1 != null && o2 == null -> "R2(!null, null)";
101 case R2(Object o1, Object o2) when o1 == null && o2 != null -> "R2(null, !null)";
102 case R2(Object o1, Object o2) when o1 != null && o2 != null -> "R2(!null, !null)";
103 default -> "default";
104 };
105 }
106
107 private String test2b(Object i) {
108 return switch (i) {
109 case R2(Object o1, Object o2) when o1 == null && o2 == null -> "R2(null, null)";
110 case R2(Object o1, Object o2) when o1 != null && o2 == null -> "R2(!null, null)";
111 case R2(Object o1, Object o2) when o1 == null && o2 != null -> "R2(null, !null)";
112 case R2(Object o1, Object o2) -> "R2(!null, !null)";
113 default -> "default";
114 };
115 }
116
117 private String test3a(Object i) {
118 return switch (i) {
119 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 == null && o3 == null -> "R3(null, null, null)";
120 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 == null && o3 == null -> "R3(!null, null, null)";
121 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 != null && o3 == null -> "R3(null, !null, null)";
122 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 != null && o3 == null -> "R3(!null, !null, null)";
123 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 == null && o3 != null -> "R3(null, null, !null)";
124 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 == null && o3 != null -> "R3(!null, null, !null)";
125 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 != null && o3 != null -> "R3(null, !null, !null)";
126 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 != null && o3 != null -> "R3(!null, !null, !null)";
127 default -> "default";
128 };
129 }
130
131 private String test3b(Object i) {
132 return switch (i) {
133 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 == null && o3 == null -> "R3(null, null, null)";
134 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 == null && o3 == null -> "R3(!null, null, null)";
135 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 != null && o3 == null -> "R3(null, !null, null)";
136 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 != null && o3 == null -> "R3(!null, !null, null)";
137 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 == null && o3 != null -> "R3(null, null, !null)";
138 case R3(Object o1, Object o2, Object o3) when o1 != null && o2 == null && o3 != null -> "R3(!null, null, !null)";
139 case R3(Object o1, Object o2, Object o3) when o1 == null && o2 != null && o3 != null -> "R3(null, !null, !null)";
140 case R3(Object o1, Object o2, Object o3) -> "R3(!null, !null, !null)";
141 default -> "default";
142 };
143 }
144
145 private String test4(Object i) {
146 return switch (i) {
147 case R1(Integer o) -> "integer";
148 case R1(Object o) when o.toString().isEmpty() -> "empty";
149 default -> "default";
150 };
151 }
152
153 private static void assertEquals(String expected, String actual) {
154 if (!Objects.equals(expected, actual)) {
155 throw new AssertionError("Unexpected result, expected: " + expected + "," +
156 " actual: " + actual);
157 }
158 }
159
160 record R1(Object o) {}
161 record R2(Object o1, Object o2) {}
162 record R3(Object o1, Object o2, Object o3) {}
163
164 }
--- EOF ---