-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckA.java
More file actions
113 lines (102 loc) · 4.45 KB
/
CheckA.java
File metadata and controls
113 lines (102 loc) · 4.45 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
import java.io.BufferedWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
public class CheckA {
public static final Path INPUT_FILE = Paths.get("a.in");
public static final Path OUTPUT_FILE = Paths.get("a.out");
public static final Random random = new Random();
public static int test = 0;
public static void main(String[] args) {
assertEquals("Single number", false, new int[][]{new int[]{1}});
assertEquals("Single line", false, new int[][]{new int[]{1, 2, 3}});
assertEquals("Single number per line", false, new int[]{1}, new int[]{2}, new int[]{3});
assertEquals("Multiple numbers per line", false, new int[]{1}, new int[]{1, 2}, new int[]{1, 2, 3});
assertEquals("Negative numbers", false, new int[]{-1}, new int[]{-1, 2}, new int[]{1, -2, -3});
assertEquals("Empty lines", false, new int[]{}, new int[]{}, new int[]{});
assertEquals("Crazy empty lines", true, new int[]{}, new int[]{}, new int[]{});
assertEquals("Crazy spaces", true, new int[]{-1}, new int[]{-1, 2}, new int[]{1, -2, -3});
assertEquals("Overflow", true, new int[]{Integer.MAX_VALUE, Integer.MAX_VALUE}, new int[]{Integer.MIN_VALUE, Integer.MIN_VALUE});
for (int n : new int[]{10, 100, 1000, 10000}) {
for (int m : new int[]{10, 100, 1000}) {
for (int v : new int[]{10, 1000, 1000_000, Integer.MAX_VALUE}) {
random(n, m, v);
}
}
}
}
private static void random(int n, int m, int v) {
int[][] valuess = new int[random.nextInt(n) + 1][];
for (int i = 0; i < valuess.length; i++) {
valuess[i] = random.ints(random.nextInt(m + 1)).map(j -> j % v).toArray();
}
assertEquals(String.format("Random lines=%d, numbers=%d, max=%d", n, m, v), true, valuess);
}
private static void assertEquals(String description, boolean crazy, int[]... valuess) {
test++;
System.out.println("Test " + test + ": " + description);
write(crazy, valuess);
run();
assertEquals(Arrays.stream(valuess).map(as -> Arrays.stream(as).mapToLong(v -> (long) v).sum()).collect(Collectors.toList()), read());
}
private static void assertEquals(Object expected, Object found) {
if (!expected.equals(found)) {
throw error("\nExpected " + expected + "\nfound " + found, null);
}
System.out.println("ok");
}
private static List<Long> read() {
try {
try (Scanner in = new Scanner(Files.newBufferedReader(OUTPUT_FILE))) {
List<Long> found = new ArrayList<>();
while (in.hasNext()) {
found.add(in.nextLong());
}
return found;
}
} catch (IOException e) {
throw error("Cannot read output", e);
}
}
private static void run() {
try {
long start = System.currentTimeMillis();
A.main(null);
System.out.println(" Finished in " + (System.currentTimeMillis() - start) + " ms");
} catch (Exception e) {
throw error("Program thrown exception", e);
}
}
private static void write(boolean crazy, int[][] valuess) {
try {
try (BufferedWriter writer = Files.newBufferedWriter(INPUT_FILE)) {
for (int[] values : valuess) {
boolean first = true;
for (int value : values) {
writer.write(first && random.nextBoolean() ? "" : randomSpace(crazy));
first = false;
writer.write("" + value);
}
if (random.nextBoolean()) {
writer.write(randomSpace(crazy));
}
writer.write("\n");
}
}
} catch (IOException e) {
throw error("Error writing input file", e);
}
}
private static AssertionError error(String message, Exception e) {
return new AssertionError("Test " + test + ": " + message, e);
}
private static String randomSpace(boolean crazy) {
return !crazy || random.nextBoolean() ? " " : " \t".charAt(random.nextInt(2)) + randomSpace(crazy);
}
}