1 /*******************************************************************************
2  * Copyright (c) 2011, 2014 IBM Corporation and others.
3  *
4  * This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License 2.0
6  * which accompanies this distribution, and is available at
7  * https://www.eclipse.org/legal/epl-2.0/
8  *
9  * SPDX-License-Identifier: EPL-2.0
10  *
11  * Contributors:
12  *     IBM Corporation - initial API and implementation
13  *******************************************************************************/
14 package org.eclipse.jdt.core.tests.compiler.regression;
15 
16 import java.util.Map;
17 
18 import org.eclipse.jdt.internal.compiler.impl.CompilerOptions;
19 
20 import junit.framework.Test;
21 
22 @SuppressWarnings({ "unchecked", "rawtypes" })
23 public class UnderscoresInLiteralsTest extends AbstractRegressionTest {
24 	static {
25 //		TESTS_NUMBERS = new int[] { 24 };
26 	}
UnderscoresInLiteralsTest(String name)27 	public UnderscoresInLiteralsTest(String name) {
28 		super(name);
29 	}
suite()30 	public static Test suite() {
31 		return buildMinimalComplianceTestSuite(testClass(), F_1_7);
32 	}
33 
testClass()34 	public static Class testClass() {
35 		return UnderscoresInLiteralsTest.class;
36 	}
37 
test001()38 	public void test001() {
39 		this.runNegativeTest(
40 			new String[] {
41 				"X.java",
42 				"public class X {\n" +
43 				"	public static void main(String[] args) {\n" +
44 				"		System.out.println(0b_001);\n" +
45 				"	}\n" +
46 				"}"
47 			},
48 			"----------\n" +
49 			"1. ERROR in X.java (at line 3)\n" +
50 			"	System.out.println(0b_001);\n" +
51 			"	                   ^^^\n" +
52 			"Underscores have to be located within digits\n" +
53 			"----------\n");
54 	}
test002()55 	public void test002() {
56 		this.runNegativeTest(
57 			new String[] {
58 				"X.java",
59 				"public class X {\n" +
60 				"	public static void main(String[] args) {\n" +
61 				"		System.out.println(0_b001);\n" +
62 				"	}\n" +
63 				"}"
64 			},
65 			"----------\n" +
66 			"1. ERROR in X.java (at line 3)\n" +
67 			"	System.out.println(0_b001);\n" +
68 			"	                   ^^\n" +
69 			"Underscores have to be located within digits\n" +
70 			"----------\n");
71 	}
test003()72 	public void test003() {
73 		this.runNegativeTest(
74 			new String[] {
75 				"X.java",
76 				"public class X {\n" +
77 				"	public static void main(String[] args) {\n" +
78 				"		System.out.println(0b001_);\n" +
79 				"	}\n" +
80 				"}"
81 			},
82 			"----------\n" +
83 			"1. ERROR in X.java (at line 3)\n" +
84 			"	System.out.println(0b001_);\n" +
85 			"	                   ^^^^^^\n" +
86 			"Underscores have to be located within digits\n" +
87 			"----------\n");
88 	}
test004()89 	public void test004() {
90 		this.runNegativeTest(
91 			new String[] {
92 				"X.java",
93 				"public class X {\n" +
94 				"	public static void main(String[] args) {\n" +
95 				"		System.out.println(0x_11.0p33f);\n" +
96 				"	}\n" +
97 				"}"
98 			},
99 			"----------\n" +
100 			"1. ERROR in X.java (at line 3)\n" +
101 			"	System.out.println(0x_11.0p33f);\n" +
102 			"	                   ^^^\n" +
103 			"Underscores have to be located within digits\n" +
104 			"----------\n");
105 	}
test005()106 	public void test005() {
107 		this.runNegativeTest(
108 			new String[] {
109 				"X.java",
110 				"public class X {\n" +
111 				"	public static void main(String[] args) {\n" +
112 				"		System.out.println(0x11_.0p33f);\n" +
113 				"	}\n" +
114 				"}"
115 			},
116 			"----------\n" +
117 			"1. ERROR in X.java (at line 3)\n" +
118 			"	System.out.println(0x11_.0p33f);\n" +
119 			"	                   ^^^^^\n" +
120 			"Underscores have to be located within digits\n" +
121 			"----------\n");
122 	}
test006()123 	public void test006() {
124 		this.runNegativeTest(
125 			new String[] {
126 				"X.java",
127 				"public class X {\n" +
128 				"	public static void main(String[] args) {\n" +
129 				"		System.out.println(0x11._0p33f);\n" +
130 				"	}\n" +
131 				"}"
132 			},
133 			"----------\n" +
134 			"1. ERROR in X.java (at line 3)\n" +
135 			"	System.out.println(0x11._0p33f);\n" +
136 			"	                   ^^^^^^\n" +
137 			"Underscores have to be located within digits\n" +
138 			"----------\n");
139 	}
test007()140 	public void test007() {
141 		this.runNegativeTest(
142 			new String[] {
143 				"X.java",
144 				"public class X {\n" +
145 				"	public static void main(String[] args) {\n" +
146 				"		System.out.println(0x11.0_p33f);\n" +
147 				"	}\n" +
148 				"}"
149 			},
150 			"----------\n" +
151 			"1. ERROR in X.java (at line 3)\n" +
152 			"	System.out.println(0x11.0_p33f);\n" +
153 			"	                   ^^^^^^^\n" +
154 			"Underscores have to be located within digits\n" +
155 			"----------\n");
156 	}
test008()157 	public void test008() {
158 		this.runNegativeTest(
159 			new String[] {
160 				"X.java",
161 				"public class X {\n" +
162 				"	public static void main(String[] args) {\n" +
163 				"		System.out.println(0x11.0p_33f);\n" +
164 				"	}\n" +
165 				"}"
166 			},
167 			"----------\n" +
168 			"1. ERROR in X.java (at line 3)\n" +
169 			"	System.out.println(0x11.0p_33f);\n" +
170 			"	                   ^^^^^^^^^^\n" +
171 			"Underscores have to be located within digits\n" +
172 			"----------\n");
173 	}
test009()174 	public void test009() {
175 		this.runNegativeTest(
176 			new String[] {
177 				"X.java",
178 				"public class X {\n" +
179 				"	public static void main(String[] args) {\n" +
180 				"		System.out.println(0x11.0p33_f);\n" +
181 				"	}\n" +
182 				"}"
183 			},
184 			"----------\n" +
185 			"1. ERROR in X.java (at line 3)\n" +
186 			"	System.out.println(0x11.0p33_f);\n" +
187 			"	                   ^^^^^^^^^^\n" +
188 			"Underscores have to be located within digits\n" +
189 			"----------\n");
190 	}
test010()191 	public void test010() {
192 		this.runNegativeTest(
193 			new String[] {
194 				"X.java",
195 				"public class X {\n" +
196 				"	public static void main(String[] args) {\n" +
197 				"		System.out.println(0x_0001AEFBBA);\n" +
198 				"	}\n" +
199 				"}"
200 			},
201 			"----------\n" +
202 			"1. ERROR in X.java (at line 3)\n" +
203 			"	System.out.println(0x_0001AEFBBA);\n" +
204 			"	                   ^^^\n" +
205 			"Underscores have to be located within digits\n" +
206 			"----------\n");
207 	}
test011()208 	public void test011() {
209 		this.runNegativeTest(
210 			new String[] {
211 				"X.java",
212 				"public class X {\n" +
213 				"	public static void main(String[] args) {\n" +
214 				"		System.out.println(0_x0001AEFBBA);\n" +
215 				"	}\n" +
216 				"}"
217 			},
218 			"----------\n" +
219 			"1. ERROR in X.java (at line 3)\n" +
220 			"	System.out.println(0_x0001AEFBBA);\n" +
221 			"	                   ^^\n" +
222 			"Underscores have to be located within digits\n" +
223 			"----------\n");
224 	}
test012()225 	public void test012() {
226 		this.runNegativeTest(
227 			new String[] {
228 				"X.java",
229 				"public class X {\n" +
230 				"	public static void main(String[] args) {\n" +
231 				"		System.out.println(0x0001AEFBBA_);\n" +
232 				"	}\n" +
233 				"}"
234 			},
235 			"----------\n" +
236 			"1. ERROR in X.java (at line 3)\n" +
237 			"	System.out.println(0x0001AEFBBA_);\n" +
238 			"	                   ^^^^^^^^^^^^^\n" +
239 			"Underscores have to be located within digits\n" +
240 			"----------\n");
241 	}
test013()242 	public void test013() {
243 		this.runNegativeTest(
244 			new String[] {
245 				"X.java",
246 				"public class X {\n" +
247 				"	public static void main(String[] args) {\n" +
248 				"		System.out.println(_01234567);\n" +
249 				"	}\n" +
250 				"}"
251 			},
252 			"----------\n" +
253 			"1. ERROR in X.java (at line 3)\n" +
254 			"	System.out.println(_01234567);\n" +
255 			"	                   ^^^^^^^^^\n" +
256 			"_01234567 cannot be resolved to a variable\n" +
257 			"----------\n");
258 	}
test014()259 	public void test014() {
260 		this.runNegativeTest(
261 			new String[] {
262 				"X.java",
263 				"public class X {\n" +
264 				"	public static void main(String[] args) {\n" +
265 				"		System.out.println(01234567_);\n" +
266 				"	}\n" +
267 				"}"
268 			},
269 			"----------\n" +
270 			"1. ERROR in X.java (at line 3)\n" +
271 			"	System.out.println(01234567_);\n" +
272 			"	                   ^^^^^^^^^\n" +
273 			"Underscores have to be located within digits\n" +
274 			"----------\n");
275 	}
test015()276 	public void test015() {
277 		this.runNegativeTest(
278 			new String[] {
279 				"X.java",
280 				"public class X {\n" +
281 				"	public static void main(String[] args) {\n" +
282 				"		System.out.println(1_.236589954f);\n" +
283 				"	}\n" +
284 				"}"
285 			},
286 			"----------\n" +
287 			"1. ERROR in X.java (at line 3)\n" +
288 			"	System.out.println(1_.236589954f);\n" +
289 			"	                   ^^\n" +
290 			"Underscores have to be located within digits\n" +
291 			"----------\n");
292 	}
test016()293 	public void test016() {
294 		this.runNegativeTest(
295 			new String[] {
296 				"X.java",
297 				"public class X {\n" +
298 				"	public static void main(String[] args) {\n" +
299 				"		System.out.println(1._236589954f);\n" +
300 				"	}\n" +
301 				"}"
302 			},
303 			"----------\n" +
304 			"1. ERROR in X.java (at line 3)\n" +
305 			"	System.out.println(1._236589954f);\n" +
306 			"	                   ^^^\n" +
307 			"Underscores have to be located within digits\n" +
308 			"----------\n");
309 	}
test017()310 	public void test017() {
311 		this.runNegativeTest(
312 			new String[] {
313 				"X.java",
314 				"public class X {\n" +
315 				"	public static void main(String[] args) {\n" +
316 				"		System.out.println(1_e2);\n" +
317 				"	}\n" +
318 				"}"
319 			},
320 			"----------\n" +
321 			"1. ERROR in X.java (at line 3)\n" +
322 			"	System.out.println(1_e2);\n" +
323 			"	                   ^^\n" +
324 			"Underscores have to be located within digits\n" +
325 			"----------\n");
326 	}
test018()327 	public void test018() {
328 		this.runNegativeTest(
329 			new String[] {
330 				"X.java",
331 				"public class X {\n" +
332 				"	public static void main(String[] args) {\n" +
333 				"		System.out.println(1e_2);\n" +
334 				"	}\n" +
335 				"}"
336 			},
337 			"----------\n" +
338 			"1. ERROR in X.java (at line 3)\n" +
339 			"	System.out.println(1e_2);\n" +
340 			"	                   ^^^^\n" +
341 			"Underscores have to be located within digits\n" +
342 			"----------\n");
343 	}
test019()344 	public void test019() {
345 		this.runNegativeTest(
346 			new String[] {
347 				"X.java",
348 				"public class X {\n" +
349 				"	public static void main(String[] args) {\n" +
350 				"		System.out.println(1e2_);\n" +
351 				"	}\n" +
352 				"}"
353 			},
354 			"----------\n" +
355 			"1. ERROR in X.java (at line 3)\n" +
356 			"	System.out.println(1e2_);\n" +
357 			"	                   ^^^^\n" +
358 			"Underscores have to be located within digits\n" +
359 			"----------\n");
360 	}
test020()361 	public void test020() {
362 		this.runNegativeTest(
363 			new String[] {
364 				"X.java",
365 				"public class X {\n" +
366 				"	public static void main(String[] args) {\n" +
367 				"		System.out.println(01e2_);\n" +
368 				"	}\n" +
369 				"}"
370 			},
371 			"----------\n" +
372 			"1. ERROR in X.java (at line 3)\n" +
373 			"	System.out.println(01e2_);\n" +
374 			"	                   ^^^^^\n" +
375 			"Underscores have to be located within digits\n" +
376 			"----------\n");
377 	}
test021()378 	public void test021() {
379 		this.runNegativeTest(
380 			new String[] {
381 				"X.java",
382 				"public class X {\n" +
383 				"	public static void main(String[] args) {\n" +
384 				"		System.out.println(01_e2_);\n" +
385 				"	}\n" +
386 				"}"
387 			},
388 			"----------\n" +
389 			"1. ERROR in X.java (at line 3)\n" +
390 			"	System.out.println(01_e2_);\n" +
391 			"	                   ^^^\n" +
392 			"Underscores have to be located within digits\n" +
393 			"----------\n");
394 	}
test022()395 	public void test022() {
396 		Map customedOptions = getCompilerOptions();
397 		customedOptions.put(CompilerOptions.OPTION_Compliance, CompilerOptions.VERSION_1_6);
398 		customedOptions.put(CompilerOptions.OPTION_Source, CompilerOptions.VERSION_1_6);
399 		customedOptions.put(CompilerOptions.OPTION_TargetPlatform, CompilerOptions.VERSION_1_6);
400 		this.runNegativeTest(
401 			new String[] {
402 				"X.java",
403 				"public class X {\n" +
404 				"	public static void main(String[] args) {\n" +
405 				"		System.out.println(0b1110000_);\n" +
406 				"	}\n" +
407 				"}"
408 			},
409 			"----------\n" +
410 			"1. ERROR in X.java (at line 3)\n" +
411 			"	System.out.println(0b1110000_);\n" +
412 			"	                   ^^^^^^^^^^\n" +
413 			"Underscores can only be used with source level 1.7 or greater\n" +
414 			"----------\n",
415 			null,
416 			true,
417 			customedOptions);
418 	}
test023()419 	public void test023() {
420 		this.runConformTest(
421 			new String[] {
422 				"X.java",
423 				"public class X {\n" +
424 				"	public static void main(String[] args) {\n" +
425 				"		System.out.println(0x1234____5678____90L);\n" +
426 				"	}\n" +
427 				"}"
428 			},
429 			"78187493520");
430 	}
test024()431 	public void test024() {
432 		this.runConformTest(
433 			new String[] {
434 				"X.java",
435 				"public class X {\n" +
436 				"	public static void main(String[] args) {\n" +
437 				"		System.out.println(90_00__00_0);\n" +
438 				"	}\n" +
439 				"}"
440 			},
441 			"9000000");
442 	}
443 }
444