-
Notifications
You must be signed in to change notification settings - Fork 0
/
Test.java
116 lines (113 loc) · 3.64 KB
/
Test.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
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
import mua.Main;
import java.io.*;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Scanner;
public class Test {
private static enum Type {
WORD {
public boolean compare(String a, String b) {
return a.trim().equals(b.trim());
}
},
NUMBER {
public boolean compare(String a, String b) {
boolean ret = false;
try {
BigDecimal d1 = new BigDecimal(Double.parseDouble(a));
BigDecimal d2 = new BigDecimal(Double.parseDouble(b));
ret = d1.equals(d2);
} catch (Exception e) {
}
return ret;
}
};
public boolean compare(String a, String b) {
return false;
}
}
private static class Value {
private Type type;
private String value;
private int mark;
private String comment;
public void read(Scanner in) {
String line = in.nextLine();
String[] a = line.split(",");
this.type = Type.valueOf(a[0].toUpperCase());
this.value = a[1];
this.mark = Integer.valueOf(a[2].trim());
this.comment = a[3];
}
public boolean judge(String line) {
return type.compare(line, value);
}
public int getMark() {
return mark;
}
public String getComment() {
return comment;
}
@Override
public String toString() {
return type+","+value+","+mark+","+comment;
}
}
public static void main(String[] args) throws IOException {
// prepare standard output and input
ArrayList<Value> alValue = new ArrayList<>();
Scanner sout = new Scanner(
new FileInputStream("stdout")
);
while (sout.hasNext()) {
Value v = new Value();
v.read(sout);
alValue.add(v);
}
sout.close();
FileInputStream cin = new FileInputStream("in");
// piped stream to get app's output
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pin = new PipedInputStream(pos);
PrintStream cout = new PrintStream(pos, true);
System.setIn(cin);
System.setOut(cout);
System.setErr(cout);
// for result output
PrintWriter resultout = new PrintWriter(
new OutputStreamWriter(
new FileOutputStream(args[0])
));
// execution
int mark = 0;
try {
Main.main(null);
cout.flush();
cout.close();
// read app's output
Scanner in = new Scanner(pin);
// while ( in.hasNextLine() ) {
// resultout.println(in.nextLine());
// }
Iterator<Value> it = alValue.iterator();
while (in.hasNext() && it.hasNext()) {
String line = in.nextLine();
Value v = it.next();
if ( v.judge(line) ) {
mark += v.getMark();
resultout.println(v.comment+": PASS");
} else {
resultout.println(v.comment+": FAIL");
// resultout.println("###"+line+":"+v.toString()+"###");
}
}
} catch (Throwable e) {
// resultout.println(e.getClass());
e.printStackTrace(resultout);
}
resultout.println(mark);
resultout.close();
cin.close();
}
}