16
16
package com .squareup .luhnybin ;
17
17
18
18
import com .google .common .io .ByteStreams ;
19
+ import java .io .ByteArrayOutputStream ;
20
+ import java .io .EOFException ;
19
21
import java .io .File ;
20
22
import java .io .IOException ;
21
23
import java .io .OutputStream ;
24
+ import java .util .Arrays ;
22
25
23
26
/**
24
27
* Runs the test suite against mask.sh.
@@ -33,8 +36,9 @@ public static void main(String[] args) throws IOException {
33
36
System .exit (1 );
34
37
}
35
38
36
- int iterations = 1 ;
39
+ final int iterations ;
37
40
if (args .length > 0 ) {
41
+ System .out .println (Arrays .toString (args ));
38
42
if (args .length > 1 ) {
39
43
System .err .println ("Usage: ./run.sh [iterations]" );
40
44
System .exit (1 );
@@ -45,18 +49,24 @@ public static void main(String[] args) throws IOException {
45
49
System .err .println ("Iterations must be >= 1." );
46
50
System .exit (1 );
47
51
}
52
+ } else {
53
+ iterations = 1 ;
48
54
}
49
55
50
56
final LuhnTests luhnTests = new LuhnTests ();
51
- final Process process = new ProcessBuilder ("mask.sh" ).start ();
57
+ final Process process = new ProcessBuilder ("sh" , "mask.sh" ).start ();
58
+
59
+ // Buffer output for maximum efficiency.
60
+ final ByteArrayOutputStream bout = new ByteArrayOutputStream ();
61
+ luhnTests .writeTo (bout );
52
62
53
63
long start = System .nanoTime ();
54
64
55
65
new Thread () {
56
66
@ Override public void run () {
57
67
try {
58
68
OutputStream out = process .getOutputStream ();
59
- luhnTests .writeTo (out );
69
+ for ( int i = 0 ; i < iterations ; i ++) bout .writeTo (out );
60
70
out .close ();
61
71
} catch (IOException e ) {
62
72
e .printStackTrace ();
@@ -76,14 +86,17 @@ public static void main(String[] args) throws IOException {
76
86
77
87
try {
78
88
for (int i = 0 ; i < iterations ; i ++) luhnTests .check (process .getInputStream ());
79
- // TODO: Check for extraneous output.
80
89
long elapsed = (System .nanoTime () - start ) / 1000000 ;
81
90
System .out .println ("Tests passed! (" + elapsed + "ms)" );
91
+ } catch (EOFException e ) {
92
+ System .err .println ("mask.sh didn't send enough output." );
82
93
} catch (TestFailure testFailure ) {
83
- System .err .println ("Test failed: " + testFailure .testCase .description );
84
- System .err .println ("Output: " + testFailure .testCase .output );
85
- System .err .println ("Expected result: " + testFailure .testCase .expectedInput );
86
- System .err .println ("Actual result: " + testFailure .actualInput );
94
+ System .err .println ("Test failed:"
95
+ + "\n Description: " + testFailure .testCase .description
96
+ + "\n Input: " + testFailure .testCase .output
97
+ + "\n Expected result: " + testFailure .testCase .expectedInput
98
+ + "\n Actual result: " + testFailure .actualInput
99
+ + "\n " );
87
100
}
88
101
}
89
102
}
0 commit comments