-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWalkTest.java
More file actions
233 lines (203 loc) · 7.6 KB
/
WalkTest.java
File metadata and controls
233 lines (203 loc) · 7.6 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
package info.kgeorgiy.java.advanced.walk;
import info.kgeorgiy.java.advanced.base.BaseTest;
import org.junit.Assert;
import org.junit.FixMethodOrder;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TestName;
import org.junit.runners.MethodSorters;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
/**
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class WalkTest extends BaseTest {
protected static final Path DIR = Paths.get("__Test__Walk__");
private static String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
protected static final Random RANDOM = new Random(23084701432182342L);
@Rule
public TestName name = new TestName();
@Test
public void test01_oneEmptyFile() throws IOException {
test(randomFiles(1, 0));
}
@Test
public void test02_tenEmptyFiles() throws IOException {
test(randomFiles(10, 0));
}
@Test
public void test03_missingFiles() throws IOException {
final Map<String, Integer> files = randomFiles(3, 0);
files.put(randomFileName(), 0);
files.put(randomFileName(), 0);
files.put(randomFileName(), 0);
test(files);
}
@Test
public void test04_errorReading() throws IOException {
final Map<String, Integer> files = randomFiles(3, 0);
files.put(DIR.toString() + "..", 0);
files.put(DIR.toString() + "@", 0);
test(files);
}
@Test
public void test05_smallRandomFiles() throws IOException {
test(randomFiles(10, 100));
}
@Test
public void test06_mediumRandomFiles() throws IOException {
test(randomFiles(10, 100));
}
@Test
public void test07_largeRandomFiles() throws IOException {
test(randomFiles(10, 1_000_000));
}
@Test
public void test08_chineseSupport() throws IOException {
final String alphabet = ALPHABET;
ALPHABET = "\u8acb\u554f\u4f60\u7684\u7a0b\u5e8f\u652f\u6301\u4e2d\u570b";
test(randomFiles(10, 100));
ALPHABET = alphabet;
}
@Test
public void test09_noInput() {
runRaw(randomFileName(), randomFileName());
}
@Test
public void test10_invalidInput() {
runRaw("/", randomFileName());
runRaw("\0*", randomFileName());
}
@Test
public void test11_invalidOutput() throws IOException {
final String input = createEmptyFile(name.getMethodName());
runRaw(input, "/");
runRaw(input, "\0*");
final String file = createEmptyFile(name.getMethodName());
runRaw(input, file + "/" + randomFileName());
}
@Test
public void test12_singleArgument() throws IOException {
runRaw(createEmptyFile(name.getMethodName()));
}
@Test
public void test13_veryLargeFile() throws IOException {
test(randomFiles(1, 100_000_00));
}
@Test
public void test14_invalidFiles() throws IOException {
final String alphabet = ALPHABET;
ALPHABET = "\0\\*";
test(randomFiles(1, 10));
ALPHABET = alphabet;
}
private String createEmptyFile(final String name) throws IOException {
final Path input = DIR.resolve(name);
Files.write(input, new byte[0]);
return input.toString();
}
protected void test(final Map<String, Integer> files) {
test(files.keySet(), files);
}
protected void test(final Collection<String> inputs, final Map<String, Integer> files) {
final Path inputFile = DIR.resolve(name.getMethodName() + ".in");
final Path outputFile = DIR.resolve(name.getMethodName() + ".out");
try {
Files.write(inputFile, generateInput(inputs).getBytes("UTF-8"));
} catch (final IOException e) {
throw new AssertionError("Cannot write input file " + inputFile);
}
run(inputFile, outputFile);
try {
for (final String line : Files.readAllLines(outputFile, Charset.forName("UTF-8"))) {
final String[] parts = line.split(" ");
Assert.assertEquals("Invalid line format\n" + line, 2, parts.length);
Assert.assertTrue("Unexpected file " + parts[1], files.containsKey(parts[1]));
Assert.assertEquals("Wrong hash", String.format("%08x", files.remove(parts[1])), parts[0]);
}
} catch (final IOException e) {
throw new AssertionError("Cannot write output file " + outputFile);
}
Assert.assertTrue("Some files missing: \n " + String.join("\n ", files.keySet()), files.isEmpty());
}
private void run(final Path inputFile, final Path outputFile) {
runRaw(inputFile.toString(), outputFile.toString());
}
private void runRaw(final String... args) {
final Method method;
final Class<?> cut = loadClass();
try {
method = cut.getMethod("main", String[].class);
} catch (final NoSuchMethodException e) {
throw new AssertionError("Cannot find method main(String[]) of " + cut, e);
}
System.out.println("Running " + name.getMethodName());
try {
method.invoke(null, (Object) args);
syncErr();
} catch (final IllegalAccessException e) {
throw new AssertionError("Cannot call main(String[]) of " + cut, e);
} catch (final InvocationTargetException e) {
throw new AssertionError("Error thrown", e.getCause());
}
}
private void syncErr() {
// try {
// Thread.sleep(1000);
// } catch (final InterruptedException e) {
// throw new AssertionError(e);
// }
}
private String generateInput(final Collection<String> files) {
final StringWriter stringWriter = new StringWriter();
final PrintWriter writer = new PrintWriter(stringWriter);
files.forEach(writer::println);
writer.close();
return stringWriter.toString();
}
private Map<String, Integer> randomFiles(final int n, final int maxL) throws IOException {
final Path dir = DIR.resolve(name.getMethodName());
return randomFiles(n, maxL, dir);
}
protected Map<String, Integer> randomFiles(final int n, final int maxL, final Path dir) throws IOException {
Files.createDirectories(dir);
final Map<String, Integer> result = new HashMap<>();
for (int i = 0; i < n; i++) {
final String name = randomFileName();
try {
final Path file = dir.resolve(name);
final byte[] bytes = new byte[RANDOM.nextInt(maxL + 1)];
RANDOM.nextBytes(bytes);
Files.write(file, bytes);
result.put(file.toString(), hash(bytes));
} catch (final InvalidPathException ignore) {
result.put(dir + "/" + name, 0);
}
}
return result;
}
protected String randomFileName() {
final StringBuilder sb = new StringBuilder();
for (int j = 0; j < 30; j++) {
sb.append(ALPHABET.charAt(RANDOM.nextInt(ALPHABET.length())));
}
return sb.toString();
}
private int hash(final byte[] bytes) {
int h = 0x811c9dc5;
for (final byte b : bytes) {
h = (h * 0x01000193) ^ (b & 0xff);
}
return h;
}
}