-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckD.java
More file actions
65 lines (61 loc) · 2.28 KB
/
CheckD.java
File metadata and controls
65 lines (61 loc) · 2.28 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
import java.util.ArrayList;
import java.util.List;
/**
* @author Georgiy Korneev (kgeorgiy@kgeorgiy.info)
*/
public class CheckD extends BaseCheck {
public static void main(String[] args) {
new CheckD().testAll();
}
@Override
protected void testAll() {
for (int n = 10; n <= 100_000; n *= 10) {
for (int follow = 0; follow <= 10; follow += 2) {
for (int push = 1; push <= 10; push += 2) {
random(n, follow * 0.1, push);
}
}
}
}
private void random(int n, double follow, int push) {
test("n=" + n, () -> {
List<Integer> expected = generate(n, follow, push);
run(() -> D.main(null));
assertEquals(expected, readInts());
});
}
private List<Integer> generate(int n, double follow, int push) {
return write(writer -> {
List<Integer> results = new ArrayList<>();
List<PersistentStack> stacks = new ArrayList<>();
stacks.add(new PersistentStack());
for (int i = 0; i < n; i++) {
int version = random.nextDouble() <= follow ? i : random.nextInt(i + 1);
PersistentStack stack = stacks.get(version);
switch (stack.getSize() == 0 ? 4 : random.nextInt(3 + push)) {
case 0:
writer.println("- " + version);
results.add(stack.getValue());
stacks.add(stack.pop());
break;
case 1:
writer.println("max " + version);
results.add(stack.getMax());
stacks.add(stack);
break;
case 2:
writer.println("min " + version);
results.add(stack.getMin());
stacks.add(stack);
break;
default:
int value = random.nextInt();
writer.println("+ " + version + " " + value);
results.add(stack.getSize() + 1);
stacks.add(stack.push(value));
}
}
return results;
});
}
}