diff --git a/.gitignore b/.gitignore index 1cdc9f7fd45e8003ad15bea0eaf7c76cd09c8e42..99031acf83eb8e0971cc4724143bd966fe162038 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,23 @@ release.properties dependency-reduced-pom.xml buildNumber.properties .mvn/timing.properties + +# Mac file finder metadata +.DS_Store +# MS Office temporary file +~* +# Emacs backup file +*~ + +# JetBrains (IntelliJ IDEA, PyCharm, etc) files +.idea/ +out/ +*.iml +*.iws +*.ipr + +# Eclipse files +bin/ +.settings/ +.classpath +.project diff --git a/pom.xml b/pom.xml index 407bc362cb6140e92981f9f9a2fd376c2eb9590a..90a29831417d2baee29c0ff8d3488bc190e93c42 100644 --- a/pom.xml +++ b/pom.xml @@ -11,7 +11,7 @@ <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> - <version>3.8.1</version> + <version>4.12</version> <scope>test</scope> </dependency> </dependencies> diff --git a/src/main/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairs.java b/src/main/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairs.java index 3af555451c3cb5c8b9e262b6a0af7e8070300567..88b5ccf8bae6c259e1c89560aacebd4e79104a99 100644 --- a/src/main/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairs.java +++ b/src/main/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairs.java @@ -1,77 +1,77 @@ package edu.unl.cse.csce361.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.csce361.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.csce361.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/csce361/sort_three_pairs/SortThreePairsTest.java b/src/test/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairsTest.java index c319b87c68600e5250375b80bf38f434c1548241..a97cd390553f473990c8e0878d5ad5c21ec46d6c 100644 --- a/src/test/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairsTest.java +++ b/src/test/java/edu/unl/cse/csce361/sort_three_pairs/SortThreePairsTest.java @@ -1,23 +1,18 @@ package edu.unl.cse.csce361.sort_three_pairs; -import junit.framework.Test; -import junit.framework.TestCase; -import junit.framework.TestSuite; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; -import java.io.InputStream; import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; import java.io.PrintStream; import java.security.Permission; -import java.io.ByteArrayOutputStream; - -public class SortThreePairsTest extends TestCase { - public SortThreePairsTest(String testName) { - super(testName); - } - public static Test suite() { - return new TestSuite(SortThreePairsTest.class); - } +public class SortThreePairsTest { protected static String assemble(String... lines) { return String.join("\n", lines) + "\n"; @@ -61,16 +56,14 @@ public class SortThreePairsTest extends TestCase { } } - @Override - protected void setUp() throws Exception { - super.setUp(); + @Before + public void setUp() { System.setSecurityManager(new TestingSecurityManager()); } - @Override - protected void tearDown() throws Exception { + @After + public void tearDown() { System.setSecurityManager(null); - super.tearDown(); } protected static String runMainForError(int expectedStatus, String... arguments) { @@ -93,66 +86,79 @@ public class SortThreePairsTest extends TestCase { 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.",