From 1125872312d3b0aef04af1f2faef3b8e71160f76 Mon Sep 17 00:00:00 2001 From: Z Toups Date: Wed, 15 Jul 2020 15:06:33 -0600 Subject: [PATCH] load in Coverage and Circles programs; use /src directories --- Circles/Readme | 95 +++++++++++++++++++++++++++++++++++ Circles/run-test | 1 + Circles/set-path.csh | 1 + Circles/set-path.sh | 1 + Circles/src/Circle.java | 59 ++++++++++++++++++++++ Circles/src/Circle1.java | 19 +++++++ Circles/src/Circle1Test.java | 77 ++++++++++++++++++++++++++++ Circles/src/Circle2.java | 23 +++++++++ Circles/src/CircleRun.java | 38 ++++++++++++++ Circles/src/Point.java | 7 +++ Coverage/build.xml | 77 ++++++++++++++++++++++++++++ Coverage/src/IglooScore1.java | 71 ++++++++++++++++++++++++++ Coverage/src/IglooScore2.java | 82 ++++++++++++++++++++++++++++++ 13 files changed, 551 insertions(+) create mode 100644 Circles/Readme create mode 100644 Circles/run-test create mode 100644 Circles/set-path.csh create mode 100644 Circles/set-path.sh create mode 100644 Circles/src/Circle.java create mode 100644 Circles/src/Circle1.java create mode 100644 Circles/src/Circle1Test.java create mode 100644 Circles/src/Circle2.java create mode 100644 Circles/src/CircleRun.java create mode 100644 Circles/src/Point.java create mode 100644 Coverage/build.xml create mode 100644 Coverage/src/IglooScore1.java create mode 100644 Coverage/src/IglooScore2.java diff --git a/Circles/Readme b/Circles/Readme new file mode 100644 index 0000000..440a56a --- /dev/null +++ b/Circles/Readme @@ -0,0 +1,95 @@ + +CS 371 Assignment: Junit +======================== + +This assignment involves using JUnit to perform automated +unit testing on some classes. + +JUnit is available at www.junit.org. + +Select "Download and Install" and follow the "Plain-old JAR" commands. +You need junit-4.12.jar and hamcrest-core-1.3.jar. Do not grab anything +related to "Maven". Do NOT put the jarfiles in your CS371 workspace and +definitely do NOT commit them into the course repository! I do not want +35 copies of JUnit jarfiles in the repository! Place them elsewhere and +then set your classpath. There are two scripts that show examples of +setting your classpath (or you can set it on your Java command line with +-cp). Learning to use your classpath is part of learning Java. + +From the JUnit home page, click on "Getting Started" in the upper right, +and read the short "JUnit Cookbook" to see a short introduction. The +Javadoc-generated API documentation is also useful. You can find many +other resources and tutorials on the web that describe JUnit. As part of +this assignment you must generate the Javadoc files in a subdirectory named +"apidoc" and it must include documentation for all class members, including +private members (you need to figure out how to do these things; it's easy). + +If you have set your classpath, you should be able to compile and run the +example test in the directory: see the file "run-test" to see an example +of how the Junit tests are run (this can also be done in a Java main, +which is in the Circle1Test.java code, but is commented out). + +Your task for this assignment is to create unit tests which accomplish +certain goals, mainly ensuring good testing coverage of the classes under +test, for one particular type of testing. + +You have three classes to test: an abstract class Circle and two +subclasses, Circle1 and Circle2. The requirements for all three are in +Javadoc-able comments in the Circle class. All three classes may have +errors; as you encounter errors, you MUST document the error, including +which test found the error, and then you must fix the error. There is also +a "CircleRun" class which demonstrates some example execution, but your +unit tests using JUnit will exercise an object (or more) of the class(es) +directly, and will not use the CircleRun class. You do not have to test +this class, it is there just so that you can run the classes in a +standalone matter to see what they do. + +When creating test cases you should not have "redundant" test cases which +do not exercise anything new. Having more tests than you need will REDUCE +your grade, not enhance it! + +You must write a comment header for each test case that succinctly describes +that test case. Just write a short description -- a long verbose description +is no good! + +This assignment: Black box testing +================================== + +Black box testing aims to test the code according to its requirements, +without being able to look into the code and see how things are implemented. +The code is treated as a "black box" -- in the case of testing an individual +class, only its public API is considered. + +Suggested reading: + +textbook, section 8.2 (p410 in particular) +http://en.wikipedia.org/wiki/Black_box_testing +http://en.wikipedia.org/wiki/Equivalence_Partitioning +http://en.wikipedia.org/wiki/Boundary-value_analysis + +Use the ideas from equivalence partitioning and boundary value analysis to +create JUnit test cases which fully test the three classes under black box +testing. The JUnit test classes must be named "Circle1Test" and +"Circle2Test". Note that since this is black box testing and both concrete +classes under test should implement the same requirements, your two test +classes should be exactly the same except for which class they reference +(Circle1 and Circle2). + +Submitting the assignment +========================= + +To submit this assignment, upload a zip file of a +directory named "assignment-junit1" containing the following: + +1. Your java source files for Circle1Test and Circle2Test including all the + test cases you created (they should contain the same exact tests). +2. The possibly updated Java source files for Circle, Circle1, and Circle2. +3. The documentation generated by JavaDoc (as described above; normally it + is not appropriate to commit auto-generated files into a repository, but + this assignment is an exception.) +4. Your plain text file that documents any errors you found, which test cases + found them, and how you fixed them. + +Do NOT commit any .class files, nor any libraries. + + diff --git a/Circles/run-test b/Circles/run-test new file mode 100644 index 0000000..f5ac3d1 --- /dev/null +++ b/Circles/run-test @@ -0,0 +1 @@ +java org.junit.runner.JUnitCore Circle1Test diff --git a/Circles/set-path.csh b/Circles/set-path.csh new file mode 100644 index 0000000..cb5e185 --- /dev/null +++ b/Circles/set-path.csh @@ -0,0 +1 @@ +setenv CLASSPATH .:/home/jcook/lib/junit-4.12.jar:/home/jcook/lib/hamcrest-core-1.3.jar diff --git a/Circles/set-path.sh b/Circles/set-path.sh new file mode 100644 index 0000000..43f3a47 --- /dev/null +++ b/Circles/set-path.sh @@ -0,0 +1 @@ +export CLASSPATH=.:/home/jcook/lib/junit-4.12.jar:/home/jcook/lib/hamcrest-core-1.3.jar diff --git a/Circles/src/Circle.java b/Circles/src/Circle.java new file mode 100644 index 0000000..ac94cea --- /dev/null +++ b/Circles/src/Circle.java @@ -0,0 +1,59 @@ +/** +* Implements a base abstract class for a circle. Everything except +* a intersects() method is implemented here. Subclass must implement +* the intersects() method. +**/ +public abstract class Circle +{ + protected Point center; + protected double radius; + +/** +* Create new circle +* @param x is the x coordinate of the center +* @param y is the y coordinate of the center +* @param radius is the radius +**/ +public Circle(double x, double y, double radius) +{ + center = new Point(); + center.x = x; + center.y = y; + this.radius = radius; +} + +/** +* Change size of circle +* @param factor is the scaling factor (0.8 +* make it 80% as big, 2.0 doubles its size) +* @return the new radius +**/ +public double scale(double factor) +{ + radius = radius + factor; + return radius; +} + +/** +* Change position of circle relative to current position +* @param xOffset is amount to change x coordinate +* @param yOffset is amount to change y coordinate +* @return the new center coordinate +**/ +public Point moveBy(double xOffset, double yOffset) +{ + center.x = center.x + xOffset; + center.y = center.y + xOffset; + return center; +} + +/** +* Test if this circle intersects another circle. +* @param other is the other circle +* @return True if the circles' outer edges intersect +* at all, False otherwise +**/ +public abstract boolean intersects(Circle other); + +} + diff --git a/Circles/src/Circle1.java b/Circles/src/Circle1.java new file mode 100644 index 0000000..33c7cef --- /dev/null +++ b/Circles/src/Circle1.java @@ -0,0 +1,19 @@ + +public class Circle1 extends Circle +{ + +public Circle1(double x, double y, double radius) +{ + super(x,y,radius); +} + +public boolean intersects(Circle other) +{ + if (Math.abs(center.x - other.center.x) < radius && + Math.abs(center.y - other.center.y) < radius) + return true; + return false; +} + +} + diff --git a/Circles/src/Circle1Test.java b/Circles/src/Circle1Test.java new file mode 100644 index 0000000..b980658 --- /dev/null +++ b/Circles/src/Circle1Test.java @@ -0,0 +1,77 @@ + +/*** +* Example JUnit testing class for Circle1 (and Circle) +* +* - must have your classpath set to include the JUnit jarfiles +* - to run the test do: +* java org.junit.runner.JUnitCore Circle1Test +* - note that the commented out main is another way to run tests +* - note that normally you would not have print statements in +* a JUnit testing class; they are here just so you see what is +* happening. You should not have them in your test cases. +***/ + +import org.junit.*; + +public class Circle1Test +{ + // Data you need for each test case + private Circle1 circle1; + +// +// Stuff you want to do before each test case +// +@Before +public void setup() +{ + System.out.println("\nTest starting..."); + circle1 = new Circle1(1,2,3); +} + +// +// Stuff you want to do after each test case +// +@After +public void teardown() +{ + System.out.println("\nTest finished."); +} + +// +// Test a simple positive move +// +@Test +public void simpleMove() +{ + Point p; + System.out.println("Running test simpleMove."); + p = circle1.moveBy(1,1); + Assert.assertTrue(p.x == 2 && p.y == 3); +} + +// +// Test a simple negative move +// +@Test +public void simpleMoveNeg() +{ + Point p; + System.out.println("Running test simpleMoveNeg."); + p = circle1.moveBy(-1,-1); + Assert.assertTrue(p.x == 0 && p.y == 1); +} + +/*** NOT USED +public static void main(String args[]) +{ + try { + org.junit.runner.JUnitCore.runClasses( + java.lang.Class.forName("Circle1Test")); + } catch (Exception e) { + System.out.println("Exception: " + e); + } +} +***/ + +} + diff --git a/Circles/src/Circle2.java b/Circles/src/Circle2.java new file mode 100644 index 0000000..a84f941 --- /dev/null +++ b/Circles/src/Circle2.java @@ -0,0 +1,23 @@ + + +public class Circle2 extends Circle +{ + +public Circle2(double x, double y, double radius) +{ + super(y,x,radius); +} + +public boolean intersects(Circle other) +{ + double d; + d = Math.sqrt(Math.pow(center.x - other.center.x, 2) + + Math.pow(center.y - other.center.y, 2)); + if (d < radius) + return true; + else + return false; +} + +} + diff --git a/Circles/src/CircleRun.java b/Circles/src/CircleRun.java new file mode 100644 index 0000000..7688769 --- /dev/null +++ b/Circles/src/CircleRun.java @@ -0,0 +1,38 @@ +/** +* Sample tester for Circle1 and Circle2 +**/ +public class CircleRun +{ + +/** +* Accept command line args for two circles and then run their +* intersect() methods. +**/ +public static void main(String args[]) +{ + Circle1 c1; + Circle2 c2; + if (args.length != 6) { + System.out.println("Error: args must be x1 y1 r1 x2 y2 r2"); + return; + } + try { + double x, y, r; + x = Double.parseDouble(args[0]); + y = Double.parseDouble(args[1]); + r = Double.parseDouble(args[2]); + c1 = new Circle1(x,y,r); + x = Double.parseDouble(args[3]); + y = Double.parseDouble(args[4]); + r = Double.parseDouble(args[5]); + c2 = new Circle2(x,y,r); + } catch (Exception e) { + System.out.println("Bad arguments! " + e); + e.printStackTrace(); + return; + } + System.out.println("Circle 1 says: " + c1.intersects(c2)); + System.out.println("Circle 2 says: " + c2.intersects(c1)); +} + +} diff --git a/Circles/src/Point.java b/Circles/src/Point.java new file mode 100644 index 0000000..c1a5bf2 --- /dev/null +++ b/Circles/src/Point.java @@ -0,0 +1,7 @@ + +public class Point +{ + double x; + double y; +} + diff --git a/Coverage/build.xml b/Coverage/build.xml new file mode 100644 index 0000000..ed99bbd --- /dev/null +++ b/Coverage/build.xml @@ -0,0 +1,77 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Coverage/src/IglooScore1.java b/Coverage/src/IglooScore1.java new file mode 100644 index 0000000..46b6484 --- /dev/null +++ b/Coverage/src/IglooScore1.java @@ -0,0 +1,71 @@ + +/*** +* Olympic Igloo Scoring Class +* +* For the Winter Olympics igloo building event there are +* three judges, each of which gives a score from 0 to 50 +* (inclusive), but the lowest score is thrown out and the +* competitor's overall score is just the sum of the two +* highest scores. This class supports the recording of the +* three judge's scores, and the computing of the competitor's +* overall score. +* +***/ + +public class IglooScore1 +{ + + int score1; + int score2; + int score3; + +public IglooScore1() +{ + score1 = 0; + score2 = 0; + score3 = 0; +} + +public void recordScores(int s1, int s2, int s3) +{ + score1 = s1; + score2 = s2; + score3 = s3; +} + +public int overallScore() +{ + int s; + if (score1 < score2) + s = score2; + else + s = score1; + if (s > score3) + s = score3; + s = (score1+score2+score3) - s; + return s; +} + +public static void main(String args[]) +{ + int s1, s2, s3; + if (args.length != 3) { + System.err.println("Error: must supply three arguments!"); + return; + } + try { + s1 = Integer.parseInt(args[0]); + s2 = Integer.parseInt(args[1]); + s3 = Integer.parseInt(args[2]); + } catch (Exception e) { + System.err.println("Error: arguments must be integers!"); + return; + } + IglooScore1 score = new IglooScore1(); + score.recordScores(s1,s2,s3); + System.out.println("Overall score: " + score.overallScore()); + return; +} + +} // end class + diff --git a/Coverage/src/IglooScore2.java b/Coverage/src/IglooScore2.java new file mode 100644 index 0000000..4193b52 --- /dev/null +++ b/Coverage/src/IglooScore2.java @@ -0,0 +1,82 @@ + +/*** +* Olympic Igloo Scoring Class +* +* For the Winter Olympics igloo building event there are +* three judges, each of which gives a score from 0 to 50 +* (inclusive), but the lowest score is thrown out and the +* competitor's overall score is just the sum of the two +* highest scores. This class supports the recording of the +* three judge's scores, and the computing of the competitor's +* overall score. +* +***/ + +public class IglooScore2 +{ + + int score1; + int score2; + int score3; + +public IglooScore2() +{ + score1 = 0; + score2 = 0; + score3 = 0; +} + +public void recordScores(int s1, int s2, int s3) +{ + score1 = s1; + score2 = s2; + score3 = s3; +} + +public int overallScore() +{ + int s, s1, s2; + if (score1 < score2 && score1 < score3) { + s1 = score2; + s2 = score3; + } else if (score2 < score1 && score2 < score3) { + s1 = score1; + s2 = score2; + } else if (score3 < score1 && score3 < score2) { + s1 = score1; + s2 = score2; + } else { + s1 = 99; + s2 = 99; + } + s = s1 + s2; + return s; +} + +public static void main(String args[]) +{ + int s1, s2, s3; + if (args==null || args.length != 3) { + System.err.println("Error: must supply three arguments!"); + return; + } + try { + s1 = Integer.parseInt(args[0]); + s2 = Integer.parseInt(args[1]); + s3 = Integer.parseInt(args[2]); + } catch (Exception e) { + System.err.println("Error: arguments must be integers!"); + return; + } + if (s1<0 || s1>50 || s2<0 || s2>50 || s3<0 || s3>50) { + System.err.println("Error: scores must be between 0 and 50!"); + return; + } + IglooScore2 score = new IglooScore2(); + score.recordScores(s1,s2,s3); + System.out.println("Overall score: " + score.overallScore()); + return; +} + +} // end class +