Skip to content

Commit 11880c4

Browse files
committed
Got tests to run.
1 parent 24d6846 commit 11880c4

File tree

7 files changed

+57
-11
lines changed

7 files changed

+57
-11
lines changed

mask.sh

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#!/bin/sh
2+
3+
# Call your program here instead of cat.
4+
cat

run.sh

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!/bin/sh
2+
3+
# Passes test data through mask.sh and verifies the output.
4+
java -jar tests/target/luhnybin-1.0-SNAPSHOT.jar $@
5+
6+

tests/pom.xml

+25
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,36 @@
1212
<groupId>junit</groupId>
1313
<artifactId>junit</artifactId>
1414
<version>4.8.2</version>
15+
<scope>test</scope>
1516
</dependency>
1617
<dependency>
1718
<groupId>com.google.guava</groupId>
1819
<artifactId>guava</artifactId>
1920
<version>10.0.1</version>
2021
</dependency>
2122
</dependencies>
23+
<build>
24+
<plugins>
25+
<plugin>
26+
<groupId>org.apache.maven.plugins</groupId>
27+
<artifactId>maven-shade-plugin</artifactId>
28+
<version>1.5</version>
29+
<executions>
30+
<execution>
31+
<phase>package</phase>
32+
<goals>
33+
<goal>shade</goal>
34+
</goals>
35+
<configuration>
36+
<transformers>
37+
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
38+
<mainClass>com.squareup.luhnybin.Main</mainClass>
39+
</transformer>
40+
</transformers>
41+
</configuration>
42+
</execution>
43+
</executions>
44+
</plugin>
45+
</plugins>
46+
</build>
2247
</project>

tests/src/main/java/com/squareup/luhnybin/LuhnTests.java

-2
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ private void testOverlappingMatches() {
135135
StringBuilder output = new StringBuilder(randomNumber(MAX_LENGTH));
136136
for (int i = 0; i < 1000 - MAX_LENGTH; i++) {
137137
output.append(computeLast(output.subSequence(i + 1, i + MAX_LENGTH)));
138-
if (random.nextInt(20) == 0) output.append(' ');
139-
if (random.nextInt(20) == 0) output.append('-');
140138
}
141139

142140
test("sequence of overlapping, valid #s")

tests/src/main/java/com/squareup/luhnybin/Main.java

+21-8
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@
1616
package com.squareup.luhnybin;
1717

1818
import com.google.common.io.ByteStreams;
19+
import java.io.ByteArrayOutputStream;
20+
import java.io.EOFException;
1921
import java.io.File;
2022
import java.io.IOException;
2123
import java.io.OutputStream;
24+
import java.util.Arrays;
2225

2326
/**
2427
* Runs the test suite against mask.sh.
@@ -33,8 +36,9 @@ public static void main(String[] args) throws IOException {
3336
System.exit(1);
3437
}
3538

36-
int iterations = 1;
39+
final int iterations;
3740
if (args.length > 0) {
41+
System.out.println(Arrays.toString(args));
3842
if (args.length > 1) {
3943
System.err.println("Usage: ./run.sh [iterations]");
4044
System.exit(1);
@@ -45,18 +49,24 @@ public static void main(String[] args) throws IOException {
4549
System.err.println("Iterations must be >= 1.");
4650
System.exit(1);
4751
}
52+
} else {
53+
iterations = 1;
4854
}
4955

5056
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);
5262

5363
long start = System.nanoTime();
5464

5565
new Thread() {
5666
@Override public void run() {
5767
try {
5868
OutputStream out = process.getOutputStream();
59-
luhnTests.writeTo(out);
69+
for (int i = 0; i < iterations; i++) bout.writeTo(out);
6070
out.close();
6171
} catch (IOException e) {
6272
e.printStackTrace();
@@ -76,14 +86,17 @@ public static void main(String[] args) throws IOException {
7686

7787
try {
7888
for (int i = 0; i < iterations; i++) luhnTests.check(process.getInputStream());
79-
// TODO: Check for extraneous output.
8089
long elapsed = (System.nanoTime() - start) / 1000000;
8190
System.out.println("Tests passed! (" + elapsed + "ms)");
91+
} catch (EOFException e) {
92+
System.err.println("mask.sh didn't send enough output.");
8293
} 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");
87100
}
88101
}
89102
}

tests/src/test/java/com/squareup/luhnybin/LuhnTest.java tests/src/test/java/com/squareup/luhnybin/LastDigitTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import org.junit.Assert;
1919
import org.junit.Test;
2020

21-
public class LuhnTest {
21+
public class LastDigitTest {
2222

2323
@Test public void computeLastDigit() {
2424
Assert.assertEquals('1', LuhnTests.computeLast("411111111111111"));
1.5 MB
Binary file not shown.

0 commit comments

Comments
 (0)