forked from runtimeverification/k
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStringUtilTest.java
More file actions
104 lines (90 loc) · 4.17 KB
/
StringUtilTest.java
File metadata and controls
104 lines (90 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Copyright (c) 2014-2018 K Team. All Rights Reserved.
package org.kframework.utils;
import junit.framework.Assert;
import org.junit.Test;
public class StringUtilTest {
@Test
public void StringUtilUnquote() throws Exception {
Assert.assertTrue(StringUtil.unquoteKString("\"\\n\"").equals("\n"));
Assert.assertTrue(StringUtil.unquoteKString("\"\\r\"").equals("\r"));
Assert.assertTrue(StringUtil.unquoteKString("\"\\f\"").equals("\f"));
Assert.assertTrue(StringUtil.unquoteKString("\"\\t\"").equals("\t"));
Assert.assertTrue(StringUtil.unquoteKString("\"\\x20\"").equals(" "));
Assert.assertTrue(StringUtil.unquoteKString("\"\\u0020\"").equals(" "));
Assert.assertTrue(StringUtil.unquoteKString("\"\\U00000020\"").equals(" "));
Assert.assertTrue(StringUtil.unquoteKString("\"\\U00000020\"").equals(" "));
try {
StringUtil.unquoteKString("\"\\U00110000\"");
throw new AssertionError();
} catch (IllegalArgumentException e) {
}
try {
StringUtil.unquoteKString("\"\\U0000d801\"");
throw new AssertionError();
} catch (IllegalArgumentException e) {
}
}
@Test
public void StringUtilEscapeShell() throws Exception {
String[] cmd1 = new String[] {"foo", "bar \" baz"};
String[] cmd2 = new String[] {"'"};
Assert.assertEquals("\"foo\" \"bar \\\" baz\"", StringUtil.escapeShell(cmd1, OS.WINDOWS));
Assert.assertEquals("'foo' 'bar \" baz'", StringUtil.escapeShell(cmd1, OS.LINUX));
Assert.assertEquals("''\\'''", StringUtil.escapeShell(cmd2, OS.LINUX));
}
@Test
public void testBijection() {
char[] all = new char[256];
for (int i = 0; i < 256; i++)
all[i] = (char) i;
String str = String.valueOf(all);
String enquoted = StringUtil.enquoteCString(str);
String backToAll = StringUtil.unquoteCString(enquoted);
char[] all2 = backToAll.toCharArray();
Assert.assertEquals("Different sizes after running quote unquote.", all.length , all2.length);
for (int i = 0; i < 256; i++)
Assert.assertEquals("Different values at position: " + i, all[i] , all2[i]);
}
@Test
public void testKBijection() {
char[] all = new char[257];
for (int i = 0; i < 256; i++)
all[i] = (char) i;
all[256] = 0xffff;
String str = String.valueOf(all);
String enquoted = StringUtil.enquoteKString(str);
String backToAll = StringUtil.unquoteKString(enquoted);
char[] all2 = backToAll.toCharArray();
Assert.assertEquals("Different sizes after running quote unquote.", all.length , all2.length);
for (int i = 0; i < 257; i++)
Assert.assertEquals("Different values at position: " + i, all[i] , all2[i]);
}
@Test
public void testSplitLine() {
String longLine = "http://www.kframework.org should be splitted";
Assert.assertEquals(
"http://www.kframework.org\nshould be\nsplitted",
StringUtil.splitLines(longLine, 10));
Assert.assertEquals(
"http://www.kframework.org\nshould be splitted",
StringUtil.splitLines(longLine, 20));
Assert.assertEquals(
"http://www.kframework.org\nshould\nbe\nsplitted",
StringUtil.splitLines(longLine, 0));
Assert.assertEquals(
"http://www.kframework.org should be\nsplitted",
StringUtil.splitLines(longLine, longLine.length()));
Assert.assertEquals(longLine, StringUtil.splitLines(longLine, longLine.length() + 1));
String multiLine =
"http://www.kframework.org\nshort enough line";
Assert.assertEquals(
"http://www.kframework.org\nshort enough line",
StringUtil.splitLines(multiLine, 30));
Assert.assertEquals(
"http://www.kframework.org\nshort enough\nline",
StringUtil.splitLines(multiLine, 13));
Assert.assertEquals(
"http://www.kframework.org\nshort\nenough\nline",
StringUtil.splitLines(multiLine, 10));
}
}