-
Notifications
You must be signed in to change notification settings - Fork 0
/
HashChecker.java
71 lines (63 loc) · 2.57 KB
/
HashChecker.java
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
package hash;
import base.MainChecker;
import base.Triple;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Georgiy Korneev ([email protected])
*/
public class HashChecker extends MainChecker {
public HashChecker(final String className) {
super(className);
}
/** Test triple layout: <code>(file, contents, hash code)</code>. */
@SafeVarargs
protected final void test(final String input, final Triple<String, String, String>... tests) throws IOException {
testArray(input, tests);
}
protected void testArray(final String input, final Triple<String, String, String>[] tests) throws IOException {
writeFiles(tests);
write(input, Arrays.stream(tests).map(Triple::first).collect(Collectors.joining("\n")));
checkEquals(Arrays.stream(tests).map(Triple::third).collect(Collectors.toList()), run(input));
}
protected void writeFiles(final Triple<String, String, String>[] tests) throws IOException {
for (final Triple<String, String, String> test : tests) {
write(test.first, test.second);
}
}
protected HashChecker test(
final Triple<String, String, String> input,
final Triple<String, String, String> hello,
final Triple<String, String, String> empty,
final Triple<String, String, String> hash,
final Triple<String, String, String> spaces,
final String... random
) throws IOException {
test("input.txt", input);
test("input.txt", hello);
test("input.txt", empty);
test("input.txt", hash);
test("input.txt", spaces);
test("input.txt", hello, empty, hash, spaces);
test("input.txt", empty, empty, empty);
test("__test__input", hello, empty, hash, empty, spaces);
final List<Triple<String, String, String>> randoms = new ArrayList<>();
for (int i = 10, j = 0; i <= 10_000_000; i *= 100, j++) {
randoms.add(Triple.of("__test__random" + i, randomString(i), random[j]));
}
@SuppressWarnings("unchecked")
final Triple<String, String, String>[] randomTriples = randoms.stream().toArray(Triple[]::new);
test("__test__random", randomTriples);
return this;
}
protected String randomString(final int length) {
final StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append((char) random.nextInt());
}
return sb.toString();
}
}