Skip to content

Instantly share code, notes, and snippets.

@petebeal
Last active April 3, 2022 14:11
Show Gist options
  • Save petebeal/5064013 to your computer and use it in GitHub Desktop.
Save petebeal/5064013 to your computer and use it in GitHub Desktop.
Test for Coursera Algorithms1 week 4 assigment
import org.junit.Test;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import static org.junit.Assert.assertArrayEquals;
/**
* Inputs can be found here:
* http://coursera.cs.princeton.edu/algs4/testing/8puzzle.zip
*
* Outputs can be found here
* https://www.dropbox.com/s/vp8utn0vh3fc5qk/week4_outputs.zip
*/
public class FileBasedTest {
private static final String RESOURCE_DIR = "src/test/resources";
private static final String INPUT_SUBDIR = "input";
private static final String OUTPUT_SUBDIR = "output";
// not really a test, creates output files just using junit as a runner
//@Test
public void shouldCreateOutputFiles() throws IOException {
final File[] inputFiles = getFilesInDir(RESOURCE_DIR + "/" + INPUT_SUBDIR);
for (final File inputFile : inputFiles) {
System.out.println("processing " + inputFile.getName());
final Board board = createBoardFromInputFile(inputFile);
final Solver solver = new Solver(board);
createOutputFile(inputFile, createOutput(solver));
}
}
@Test
public void shouldPassAllTests() throws IOException {
final File[] inputFiles = getFilesInDir(RESOURCE_DIR + "/" + INPUT_SUBDIR);
final File[] outputFiles = getFilesInDir(RESOURCE_DIR + "/" + OUTPUT_SUBDIR);
final Map<String, String[]> inputNameToOutputs = createOutputsMap(outputFiles);
for (final File inputFile : inputFiles) {
final Board board = createBoardFromInputFile(inputFile);
final Solver solver = new Solver(board);
final String[] expectedSolution = inputNameToOutputs.get(inputFile.getName());
final String[] actualSolution = createOutput(solver);
assertArrayEquals(actualSolution, expectedSolution);
System.out.println("Passed " + inputFile.getName());
}
}
private Map<String, String[]> createOutputsMap(final File[] outputFiles)
throws IOException {
final Map<String, String[]> map = new HashMap<String, String[]>(outputFiles.length);
for (final File outputFile : outputFiles) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(outputFile));
String line = null;
final List<String> lines = new ArrayList<String>(10);
while ((line = reader.readLine()) != null) {
lines.add(line);
}
map.put(outputFile.getName(), lines.toArray(new String[lines.size()]));
}
finally {
if (reader != null) {
reader.close();
}
}
}
return map;
}
private void createOutputFile(File inputFile, String[] solution)
throws IOException {
PrintWriter writer = null;
try {
String outputFileName = inputFile.getName();
writer =
new PrintWriter(
new BufferedWriter(
new FileWriter(RESOURCE_DIR + "/" +
OUTPUT_SUBDIR + "/" + outputFileName)));
for (final String s : solution) {
writer.println(s);
}
writer.flush();
}
finally {
if (writer != null) {
writer.close();
}
}
}
private String[] createOutput(final Solver solver) {
final List<String> strings = new ArrayList<String>(100);
if (!solver.isSolvable())
strings.add("No solution possible");
else {
strings.add("Minimum number of moves = " + solver.moves());
for (Board board : solver.solution()) {
final String[] lines = board.toString().split("\n");
strings.addAll(Arrays.asList(lines));
strings.add("");
}
}
return strings.toArray(new String[strings.size()]);
}
private File[] getFilesInDir(final String dir) {
final File resourceDir = new File(dir);
return resourceDir.listFiles(
new FilenameFilter() {
@Override
public boolean accept(File file, String s) {
return s.contains(".txt");
}
});
}
private static Board createBoardFromInputFile(File inputFile)
throws FileNotFoundException {
Scanner s = null;
try {
s = new Scanner(inputFile);
final int numberOfBlocks = s.nextInt();
final int[][] blocks = new int[numberOfBlocks][numberOfBlocks];
for (int i = 0; i < numberOfBlocks; i++) {
for (int j = 0; j < numberOfBlocks; j++) {
blocks[i][j] = s.nextInt();
}
}
return new Board(blocks);
} finally {
if (s != null) {
s.close();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment