author | jlahoda |
Mon, 21 Oct 2019 15:38:26 +0200 | |
changeset 58713 | ad69fd32778e |
parent 55261 | dff30b1557ee |
child 59037 | 3d2575331a41 |
permissions | -rw-r--r-- |
55261 | 1 |
/* |
2 |
* Copyright (c) 2019, 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 8223780 |
|
27 |
* @summary This exercises String#translateEscapes patterns and limits. |
|
58713
ad69fd32778e
8226585: Improve javac messages for using a preview API
jlahoda
parents:
55261
diff
changeset
|
28 |
* @compile --enable-preview -source 14 TranslateEscapes.java |
ad69fd32778e
8226585: Improve javac messages for using a preview API
jlahoda
parents:
55261
diff
changeset
|
29 |
* @run main/othervm --enable-preview TranslateEscapes |
55261 | 30 |
*/ |
31 |
||
32 |
public class TranslateEscapes { |
|
33 |
public static void main(String... arg) { |
|
34 |
test1(); |
|
35 |
test2(); |
|
36 |
test3(); |
|
37 |
} |
|
38 |
||
39 |
/* |
|
40 |
* Standard escapes. |
|
41 |
*/ |
|
42 |
static void test1() { |
|
43 |
verifyEscape("b", '\b'); |
|
44 |
verifyEscape("f", '\f'); |
|
45 |
verifyEscape("n", '\n'); |
|
46 |
verifyEscape("r", '\r'); |
|
47 |
verifyEscape("t", '\t'); |
|
48 |
verifyEscape("'", '\''); |
|
49 |
verifyEscape("\"", '\"'); |
|
50 |
verifyEscape("\\", '\\'); |
|
51 |
} |
|
52 |
||
53 |
/* |
|
54 |
* Octal escapes. |
|
55 |
*/ |
|
56 |
static void test2() { |
|
57 |
verifyOctalEscape("0", 0); |
|
58 |
verifyOctalEscape("3", 03); |
|
59 |
verifyOctalEscape("7", 07); |
|
60 |
verifyOctalEscape("07", 07); |
|
61 |
verifyOctalEscape("17", 017); |
|
62 |
verifyOctalEscape("27", 027); |
|
63 |
verifyOctalEscape("37", 037); |
|
64 |
verifyOctalEscape("377", 0377); |
|
65 |
||
66 |
verifyOctalEscape("777", 077); |
|
67 |
verifyOctalEscape("78", 07); |
|
68 |
} |
|
69 |
||
70 |
/* |
|
71 |
* Exceptions. |
|
72 |
*/ |
|
73 |
static void test3() { |
|
74 |
exceptionThrown("+"); |
|
75 |
exceptionThrown("\n"); |
|
76 |
} |
|
77 |
||
78 |
static void verifyEscape(String string, char ch) { |
|
79 |
String escapes = "\\" + string; |
|
80 |
if (escapes.translateEscapes().charAt(0) != ch) { |
|
81 |
System.err.format("\"%s\" not escape \"%s\"'%n", string, escapes); |
|
82 |
throw new RuntimeException(); |
|
83 |
} |
|
84 |
} |
|
85 |
||
86 |
static void verifyOctalEscape(String string, int octal) { |
|
87 |
String escapes = "\\" + string; |
|
88 |
if (escapes.translateEscapes().charAt(0) != octal) { |
|
89 |
System.err.format("\"%s\" not octal %o%n", string, octal); |
|
90 |
throw new RuntimeException(); |
|
91 |
} |
|
92 |
} |
|
93 |
||
94 |
static void exceptionThrown(String string) { |
|
95 |
String escapes = "\\" + string; |
|
96 |
try { |
|
97 |
escapes.translateEscapes(); |
|
98 |
System.err.format("escape not thrown for %s%n", string); |
|
99 |
throw new RuntimeException(); |
|
100 |
||
101 |
} catch (IllegalArgumentException ex) { |
|
102 |
// okay |
|
103 |
} |
|
104 |
} |
|
105 |
} |