mirror of
https://github.com/RustyRaptor/Programs.git
synced 2026-12-03 15:45:21 -07:00
load in Coverage and Circles programs; use /src directories
This commit is contained in:
1 parent
cdbc8c2939
commit
1125872312
13 files changed
+551
No files matched your search
@@ -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.
|
||||
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
java org.junit.runner.JUnitCore Circle1Test
|
||||
@@ -0,0 +1 @@
|
||||
setenv CLASSPATH .:/home/jcook/lib/junit-4.12.jar:/home/jcook/lib/hamcrest-core-1.3.jar
|
||||
@@ -0,0 +1 @@
|
||||
export CLASSPATH=.:/home/jcook/lib/junit-4.12.jar:/home/jcook/lib/hamcrest-core-1.3.jar
|
||||
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
***/
|
||||
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
|
||||
public class Point
|
||||
{
|
||||
double x;
|
||||
double y;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
<!-- Sample Ant build file for running IglooScore under JaCoCo -->
|
||||
<!-- Jonathan Cook, 17 Sep 2015 -->
|
||||
<project name="iglooscore" default="jacocoreport" xmlns:jacoco="antlib:org.jacoco.ant">
|
||||
|
||||
<!-- property to allow ant to find jacoco: CHANGE THE PATH to point to yours -->
|
||||
<property name="jacocodir" location="/home/jcook/tools/jacoco/lib"/>
|
||||
|
||||
<taskdef uri="antlib:org.jacoco.ant" resource="org/jacoco/ant/antlib.xml">
|
||||
<classpath path="${jacocodir}/jacocoant.jar"/>
|
||||
</taskdef>
|
||||
|
||||
<!-- standard compile task, but turn debugging on for source viewing -->
|
||||
<target name="compile" description="compile the source">
|
||||
<javac srcdir="." includeantruntime="false" debug="true"/>
|
||||
</target>
|
||||
|
||||
<!-- tests if new jacoco run is needed by checking if the jacoco.exec file is
|
||||
newer than the class files or this build file -->
|
||||
<target name="checkRun">
|
||||
<uptodate property="run.notneeded" targetfile="jacoco.exec">
|
||||
<srcfiles dir="." includes="*.class build.xml" />
|
||||
</uptodate>
|
||||
</target>
|
||||
|
||||
<!-- run program under jacoco: this example has two runs, you'll need to perhaps
|
||||
modify them, and then add more; this only runs if needed -->
|
||||
<target name="jacocorun" depends="compile,checkRun" unless="run.notneeded">
|
||||
<delete>
|
||||
<!-- must delete coverage data file to start afresh (otherwise coverage accumulates) -->
|
||||
<fileset dir="." includes="jacoco.exec"/>
|
||||
</delete>
|
||||
<echo message="TEST IglooScore1: 8 9 6"/>
|
||||
<java classname="IglooScore1" fork="true">
|
||||
<arg value="8"/><arg value="9"/><arg value="6"/>
|
||||
<jvmarg line="-javaagent:${jacocodir}/jacocoagent.jar"/>
|
||||
</java>
|
||||
<echo message="TEST IglooScore2: 8 9 6"/>
|
||||
<java classname="IglooScore2" fork="true">
|
||||
<arg value="8"/><arg value="9"/><arg value="6"/>
|
||||
<jvmarg line="-javaagent:${jacocodir}/jacocoagent.jar"/>
|
||||
</java>
|
||||
</target>
|
||||
|
||||
<!-- tests if new jacoco report needs generated by checking if top report file
|
||||
(index.html) is newer than the run data file (jacoco.exec) -->
|
||||
<target name="checkReport">
|
||||
<uptodate property="report.notneeded" targetfile="report/index.html" srcfile="jacoco.exec" />
|
||||
</target>
|
||||
|
||||
<!-- generate a new jacoco report from a new data file; only runs if needed -->
|
||||
<target name="jacocoreport" depends="jacocorun,checkReport" unless="report.notneeded">
|
||||
<jacoco:report>
|
||||
<executiondata>
|
||||
<file file="jacoco.exec"/>
|
||||
</executiondata>
|
||||
<structure name="iglooscore">
|
||||
<classfiles>
|
||||
<fileset dir="."/>
|
||||
</classfiles>
|
||||
<sourcefiles encoding="UTF-8">
|
||||
<fileset dir="."/>
|
||||
</sourcefiles>
|
||||
</structure>
|
||||
<html destdir="report"/>
|
||||
</jacoco:report>
|
||||
</target>
|
||||
|
||||
<!-- clean up by removing all generated files; do "ant clean" to start afresh -->
|
||||
<target name="clean">
|
||||
<delete>
|
||||
<fileset dir="." includes="*.class jacoco.exec"/>
|
||||
</delete>
|
||||
<delete dir="report" />
|
||||
</target>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user