diff --git a/src/main/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairs.java b/src/main/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairs.java index afde0c8dcccb7dcb3b2aede01b1d2f81dee8490e..8e56ae3588ab96dbed7c23a2daddc41b6c223c99 100644 --- a/src/main/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairs.java +++ b/src/main/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairs.java @@ -1,77 +1,77 @@ package edu.unl.cse.soft160.sort_three_pairs; public class SortThreePairs { - public static double readArgument(int position, String... arguments) { - return Double.parseDouble(arguments[position]); - } + public static double readArgument(int position, String... arguments) { + return Double.parseDouble(arguments[position]); + } - public static String formatPair(double x, double y) { - return "(" + x + ", " + y + ")"; - } + public static String formatPair(double x, double y) { + return "(" + x + ", " + y + ")"; + } - public static int getPositionOfMinimum(double x0, double y0, double x1, double y1, double x2, double y2) { - int result = 0; - double xmin = x0; - double ymin = y0; - if (x1 < xmin || x1 == xmin && y1 < ymin) { - result = result + 1; - xmin = x1; - ymin = y1; - } - if (x2 < xmin || x2 == xmin && y2 < ymin) { - result = result + 1; - xmin = x2; - ymin = y2; - } - return result; - } + public static int getPositionOfMinimum(double x0, double y0, double x1, double y1, double x2, double y2) { + int result = 0; + double xmin = x0; + double ymin = y0; + if (x1 < xmin || x1 == xmin && y1 < ymin) { + result = result + 1; + xmin = x1; + ymin = y1; + } + if (x2 < xmin || x2 == xmin && y2 < ymin) { + result = result + 1; + xmin = x2; + ymin = y2; + } + return result; + } - public static void main(String... arguments) { - if (arguments.length != 6) { - System.err.println("Sorts three points by their x coordinates, breaking ties using y coordinates."); - System.err.println( - "Usage: java edu.unl.cse.soft160.sort_three_pairs.SortThreePairs [X0] [Y0] [X1] [Y1] [X2] [Y2]"); - System.exit(1); - } - double x0 = readArgument(0, arguments); - double y0 = readArgument(1, arguments); - double x1 = readArgument(2, arguments); - double y1 = readArgument(3, arguments); - double x2 = readArgument(4, arguments); - double y2 = readArgument(5, arguments); - double x3 = x0; - double y3 = y0; - // move the smallest pair to (x0, y0) - int firstPositionOfMinimum = getPositionOfMinimum(x0, y0, x1, y1, x2, y2); - switch (firstPositionOfMinimum) { - case 2: - x3 = x1; - x1 = x2; - x2 = x3; - y3 = y1; - y1 = y2; - y2 = y3; - case 1: - x3 = x0; - x0 = x1; - x1 = x3; - y3 = y0; - y0 = y1; - y1 = y3; - } - // move the second-smallest pair to (x1, y1) - int secondPositionOfMinimum = getPositionOfMinimum(Double.MAX_VALUE, Double.MAX_VALUE, x1, y1, x2, y2); - switch (secondPositionOfMinimum) { - case 2: - x3 = x1; - x1 = x2; - x2 = x3; - y3 = y1; - y1 = y2; - y2 = y3; - } - System.out.println(formatPair(x1, y1)); - System.out.println(formatPair(x2, y2)); - System.out.println(formatPair(x3, y3)); - } + public static void main(String... arguments) { + if (arguments.length != 6) { + System.err.println("Sorts three points by their x coordinates, breaking ties using y coordinates."); + System.err.println( + "Usage: java edu.unl.cse.soft160.sort_three_pairs.SortThreePairs [X0] [Y0] [X1] [Y1] [X2] [Y2]"); + System.exit(1); + } + double x0 = readArgument(0, arguments); + double y0 = readArgument(1, arguments); + double x1 = readArgument(2, arguments); + double y1 = readArgument(3, arguments); + double x2 = readArgument(4, arguments); + double y2 = readArgument(5, arguments); + double x3 = x0; + double y3 = y0; + // move the smallest pair to (x0, y0) + int firstPositionOfMinimum = getPositionOfMinimum(x0, y0, x1, y1, x2, y2); + switch (firstPositionOfMinimum) { + case 2: + x3 = x1; + x1 = x2; + x2 = x3; + y3 = y1; + y1 = y2; + y2 = y3; + case 1: + x3 = x0; + x0 = x1; + x1 = x3; + y3 = y0; + y0 = y1; + y1 = y3; + } + // move the second-smallest pair to (x1, y1) + int secondPositionOfMinimum = getPositionOfMinimum(Double.MAX_VALUE, Double.MAX_VALUE, x1, y1, x2, y2); + switch (secondPositionOfMinimum) { + case 2: + x3 = x1; + x1 = x2; + x2 = x3; + y3 = y1; + y1 = y2; + y2 = y3; + } + System.out.println(formatPair(x1, y1)); + System.out.println(formatPair(x2, y2)); + System.out.println(formatPair(x3, y3)); + } } diff --git a/src/test/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairsTest.java b/src/test/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairsTest.java index 5835e09cd52cbcf7b2e3c3701cfb372a917a9893..5c425e59dc97b1aa6d0b6e50e5e1bd57b9532b71 100644 --- a/src/test/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairsTest.java +++ b/src/test/java/edu/unl/cse/soft160/sort_three_pairs/SortThreePairsTest.java @@ -13,155 +13,155 @@ import java.io.ByteArrayOutputStream; import static org.junit.Assert.*; public class SortThreePairsTest { - protected static String assemble(String... lines) { - return String.join("\n", lines) + "\n"; - } - - protected static String runMain(String... arguments) { - InputStream in = System.in; - PrintStream out = System.out; - System.setIn(new ByteArrayInputStream("".getBytes())); - ByteArrayOutputStream collector = new ByteArrayOutputStream(); - System.setOut(new PrintStream(collector)); - SortThreePairs.main(arguments); - System.setIn(in); - System.setOut(out); - return collector.toString(); - } - - @SuppressWarnings("serial") - protected static class ExitException extends SecurityException { - public final int status; - - public ExitException(int status) { - super("Exit with status " + status); - this.status = status; - } - } - - private static class TestingSecurityManager extends SecurityManager { - @Override - public void checkPermission(Permission perm) { - } - - @Override - public void checkPermission(Permission perm, Object context) { - } - - @Override - public void checkExit(int status) { - super.checkExit(status); - throw new ExitException(status); - } - } - - @Before - public void setUp() throws SecurityException { - System.setSecurityManager(new TestingSecurityManager()); - } - - @After - public void tearDown() throws SecurityException { - System.setSecurityManager(null); - } - - protected static String runMainForError(int expectedStatus, String... arguments) { - InputStream in = System.in; - PrintStream err = System.err; - System.setIn(new ByteArrayInputStream("".getBytes())); - ByteArrayOutputStream collector = new ByteArrayOutputStream(); - System.setErr(new PrintStream(collector)); - boolean exited = false; - try { - SortThreePairs.main(arguments); - } catch (ExitException expected) { - assertEquals(expectedStatus, expected.status); - exited = true; - } finally { - System.setIn(in); - System.setErr(err); - } - assertTrue(exited); - return collector.toString(); - } - - @Test - public void testZeros() { - assertEquals(assemble("(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)"), - runMain("0", "0", "0", "0", "0", "0")); - } - - @Test - public void testAlreadySorted() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("0", "0", "1", "1", "2", "2")); - } - - @Test - public void testReversed() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("2", "2", "1", "1", "0", "0")); - } - - @Test - public void testJumbled() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("1", "1", "2", "2", "0", "0")); - } - - @Test - public void testTieBreaking() { - assertEquals(assemble("(0.0, 1.0)", "(1.0, 2.0)", "(1.0, 3.0)"), - runMain("1", "3", "0", "1", "1", "2")); - } - - @Test - public void testMultipleTieBreaking() { - assertEquals(assemble("(0.0, 0.0)", "(0.0, 1.0)", "(0.0, 3.0)"), - runMain("0", "1", "0", "0", "0", "3")); - } - - @Test - public void testDuplicates() { - assertEquals(assemble("(1.0, 1.0)", "(1.0, 1.0)", "(1.0, 2.0)"), - runMain("1", "1", "1", "2", "1", "1")); - } - - @Test - public void testPermutation1() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("0", "0", "2", "2", "1", "1")); - } - - @Test - public void testPermutation2() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("2", "2", "0", "0", "1", "1")); - } - - @Test - public void testPermutation3() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("2", "2", "1", "1", "0", "0")); - } - - @Test - public void testPermutation16() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("1", "1", "2", "2", "0", "0")); - } - - @Test - public void testPermutation20() { - assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), - runMain("1", "1", "0", "0", "2", "2")); - } - - @Test - public void testTooFewArguments() { - assertEquals( - assemble("Sorts three points by their x coordinates, breaking ties using y coordinates.", - "Usage: java edu.unl.cse.soft160.sort_three_pairs.SortThreePairs [X0] [Y0] [X1] [Y1] [X2] [Y2]"), - runMainForError(1, "0", "0", "1", "1", "2", "2", "3")); - } + protected static String assemble(String... lines) { + return String.join("\n", lines) + "\n"; + } + + protected static String runMain(String... arguments) { + InputStream in = System.in; + PrintStream out = System.out; + System.setIn(new ByteArrayInputStream("".getBytes())); + ByteArrayOutputStream collector = new ByteArrayOutputStream(); + System.setOut(new PrintStream(collector)); + SortThreePairs.main(arguments); + System.setIn(in); + System.setOut(out); + return collector.toString(); + } + + @SuppressWarnings("serial") + protected static class ExitException extends SecurityException { + public final int status; + + public ExitException(int status) { + super("Exit with status " + status); + this.status = status; + } + } + + private static class TestingSecurityManager extends SecurityManager { + @Override + public void checkPermission(Permission perm) { + } + + @Override + public void checkPermission(Permission perm, Object context) { + } + + @Override + public void checkExit(int status) { + super.checkExit(status); + throw new ExitException(status); + } + } + + @Before + public void setUp() throws SecurityException { + System.setSecurityManager(new TestingSecurityManager()); + } + + @After + public void tearDown() throws SecurityException { + System.setSecurityManager(null); + } + + protected static String runMainForError(int expectedStatus, String... arguments) { + InputStream in = System.in; + PrintStream err = System.err; + System.setIn(new ByteArrayInputStream("".getBytes())); + ByteArrayOutputStream collector = new ByteArrayOutputStream(); + System.setErr(new PrintStream(collector)); + boolean exited = false; + try { + SortThreePairs.main(arguments); + } catch (ExitException expected) { + assertEquals(expectedStatus, expected.status); + exited = true; + } finally { + System.setIn(in); + System.setErr(err); + } + assertTrue(exited); + return collector.toString(); + } + + @Test + public void testZeros() { + assertEquals(assemble("(0.0, 0.0)", "(0.0, 0.0)", "(0.0, 0.0)"), + runMain("0", "0", "0", "0", "0", "0")); + } + + @Test + public void testAlreadySorted() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("0", "0", "1", "1", "2", "2")); + } + + @Test + public void testReversed() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("2", "2", "1", "1", "0", "0")); + } + + @Test + public void testJumbled() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("1", "1", "2", "2", "0", "0")); + } + + @Test + public void testTieBreaking() { + assertEquals(assemble("(0.0, 1.0)", "(1.0, 2.0)", "(1.0, 3.0)"), + runMain("1", "3", "0", "1", "1", "2")); + } + + @Test + public void testMultipleTieBreaking() { + assertEquals(assemble("(0.0, 0.0)", "(0.0, 1.0)", "(0.0, 3.0)"), + runMain("0", "1", "0", "0", "0", "3")); + } + + @Test + public void testDuplicates() { + assertEquals(assemble("(1.0, 1.0)", "(1.0, 1.0)", "(1.0, 2.0)"), + runMain("1", "1", "1", "2", "1", "1")); + } + + @Test + public void testPermutation1() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("0", "0", "2", "2", "1", "1")); + } + + @Test + public void testPermutation2() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("2", "2", "0", "0", "1", "1")); + } + + @Test + public void testPermutation3() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("2", "2", "1", "1", "0", "0")); + } + + @Test + public void testPermutation16() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("1", "1", "2", "2", "0", "0")); + } + + @Test + public void testPermutation20() { + assertEquals(assemble("(0.0, 0.0)", "(1.0, 1.0)", "(2.0, 2.0)"), + runMain("1", "1", "0", "0", "2", "2")); + } + + @Test + public void testTooFewArguments() { + assertEquals( + assemble("Sorts three points by their x coordinates, breaking ties using y coordinates.", + "Usage: java edu.unl.cse.soft160.sort_three_pairs.SortThreePairs [X0] [Y0] [X1] [Y1] [X2] [Y2]"), + runMainForError(1, "0", "0", "1", "1", "2", "2", "3")); + } }