From 3ac0e7c046437189cf24764898d9be3bfd539b76 Mon Sep 17 00:00:00 2001 From: Carly Thorpe Date: Thu, 19 Feb 2015 13:00:22 -0500 Subject: [PATCH 1/2] Answers to Testing Lab, Average and Range files, Average and Range Test files --- src/Answers.md | 18 ++++++++++++++++++ src/Average.java | 23 +++++++++++++++++++++++ src/AverageTest.java | 23 +++++++++++++++++++++++ src/Range.java | 40 ++++++++++++++++++++++++++++++++++++++++ src/RangeTest.java | 14 ++++++++++++++ 5 files changed, 118 insertions(+) create mode 100644 src/Answers.md create mode 100644 src/Average.java create mode 100644 src/AverageTest.java create mode 100644 src/Range.java create mode 100644 src/RangeTest.java diff --git a/src/Answers.md b/src/Answers.md new file mode 100644 index 0000000..e32b43a --- /dev/null +++ b/src/Answers.md @@ -0,0 +1,18 @@ +# Answers to Testing Lab + +###Carly Thorpe +#### 2/10/15 + + +**Question 1:** Three mistakes you can make when using an array are misinterpreting what the position is in an array (Ex: If the array is histogram[4], you could make the mistake of thinking it is in index 4), you can mix up variables in the code that you are writing (Ex: an array of ints vs an array of strings), and passing a parameter of the wrong type to a method. + +**Question 2:** For a new instance of a class, one configuration of a `Scoreboard` would include both `getScore()` and `getAverage()` methods. This `Scoreboard` would be empty. `getScore()` would return nothing because there are no positions that have a score yet and `getAverage()` would be 0.0 because there are no scores added to the scoreboard as soon as a new instance is created. In an instance with a single mutation, `Scoreboard` would have `getScore()` with a parameter passed and would return the score that is in that specific position. It would only have one postition because it was only mutated once. `getAverage()` would return whatever that score in that postition is because it is the sum/num of mutations and there is only one mutation to the method. In an instance with multiple mutations, `Scoreboard` would have multiple scores and when `getScore()` is called with a parameter, it would return the score in that postition. `getAverage()` would change depending on how many mutations are made to the instance. + +**Question 3:** Testing Questions for `Range` + +1. A brand new instance of the `Range` class would be empty. There are no values added to the class so there would be no methods. +2. There would be no way to simply mutate the instance. All of the methods in the class `Range` are query methods, so you can only ask questions of them. The range is established in the constructor and it can't change unless you create a new instance. You can only test the intersection method. This is because you can change the range that you are comparing the established range to and get different range answers depending on the "other" range you compare it to. +3. The only other thing you can do to the instance of the class is call the other methods and have them return a value. The rest of the methods are query methods so you can only ask questions of them. There's no more mutator methods to change the instance. +4. The boundary conditions of the class are that the range is inclusive at the lower starting value and exclusive at the upper value. Where in the `Average` class, the boundaries depended on if count was equal to zero or not. If count was 0, it returned 0. But if count was >0, it would divide sum by count. + + diff --git a/src/Average.java b/src/Average.java new file mode 100644 index 0000000..b9cbfa6 --- /dev/null +++ b/src/Average.java @@ -0,0 +1,23 @@ +public class Average { + double sum = 0; + int count = 0; + + public Average() { + } + + public void addValue(double value) { + sum += value; + count += 1; + } + + public double getAverage() { + if (count == 0) + return 0; + else + return (double)sum/count; + } + + public int getCount() { + return count; + } +} diff --git a/src/AverageTest.java b/src/AverageTest.java new file mode 100644 index 0000000..c7fbad0 --- /dev/null +++ b/src/AverageTest.java @@ -0,0 +1,23 @@ +import junit.framework.TestCase; + +public class AverageTest extends TestCase { + public void testNewAverage() { + Average avg = new Average(); + + assertEquals(0, avg.getCount()); + assertEquals(0, avg.getAverage(), 0.001); + } + + public void testSingleValue() { + Average avg = new Average(); + + avg.addValue(10.0); + avg.addValue(10.0); + avg.addValue(10.0); + avg.addValue(10.0); + avg.addValue(10.0); + + assertEquals(10.0, avg.getAverage(), 0.001); + assertEquals(5, avg.getCount()); + } +} diff --git a/src/Range.java b/src/Range.java new file mode 100644 index 0000000..55dfd6a --- /dev/null +++ b/src/Range.java @@ -0,0 +1,40 @@ +public class Range { + private double start; + private double stop; + + public Range(double min, double max) { + start = min; + stop = max; + } + + + public boolean contains(double value) { + System.out.println(start); + System.out.println(stop); + System.out.println(value); + + if (value >= start && value < stop) { + return true; + } + else{ + return false; + } + } + + public double getWidth() { + return Math.abs(start - stop); + } + + public double getMin() { + return start; + } + + public double getMax() { + return stop; + } +} + + + + + \ No newline at end of file diff --git a/src/RangeTest.java b/src/RangeTest.java new file mode 100644 index 0000000..e900354 --- /dev/null +++ b/src/RangeTest.java @@ -0,0 +1,14 @@ +import junit.framework.TestCase; + +public class RangeTest extends TestCase { + public void testNewRange() { + Range r = new Range(0,2); + + assertEquals(true, r.contains(1)); + assertEquals(2, r.getWidth()); + assertEquals(0, r.getMin()); + assertEquals(2, r.getMax()); + + } + +} From 49e9160268e32f7c339fb88765968a463b0e6a57 Mon Sep 17 00:00:00 2001 From: Carly Thorpe Date: Mon, 2 Mar 2015 18:07:27 -0500 Subject: [PATCH 2/2] Changes made to Testing Lab. Includes answers, test files and codes. --- src/Answers.md | 2 +- src/Range.java | 63 ++++++++++++++++++++++++++++++ src/RangeTest.java | 96 ++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 157 insertions(+), 4 deletions(-) diff --git a/src/Answers.md b/src/Answers.md index e32b43a..84182db 100644 --- a/src/Answers.md +++ b/src/Answers.md @@ -4,7 +4,7 @@ #### 2/10/15 -**Question 1:** Three mistakes you can make when using an array are misinterpreting what the position is in an array (Ex: If the array is histogram[4], you could make the mistake of thinking it is in index 4), you can mix up variables in the code that you are writing (Ex: an array of ints vs an array of strings), and passing a parameter of the wrong type to a method. +**Question 1:** Three mistakes you can make when using an array are misinterpreting what the position is in an array (Ex: If the array is histogram[4], you could make the mistake of thinking it is in index 4), you can use doubles and ints at the wrong time, or forget to keep them consistent, and passing a parameter of the wrong type to a method. **Question 2:** For a new instance of a class, one configuration of a `Scoreboard` would include both `getScore()` and `getAverage()` methods. This `Scoreboard` would be empty. `getScore()` would return nothing because there are no positions that have a score yet and `getAverage()` would be 0.0 because there are no scores added to the scoreboard as soon as a new instance is created. In an instance with a single mutation, `Scoreboard` would have `getScore()` with a parameter passed and would return the score that is in that specific position. It would only have one postition because it was only mutated once. `getAverage()` would return whatever that score in that postition is because it is the sum/num of mutations and there is only one mutation to the method. In an instance with multiple mutations, `Scoreboard` would have multiple scores and when `getScore()` is called with a parameter, it would return the score in that postition. `getAverage()` would change depending on how many mutations are made to the instance. diff --git a/src/Range.java b/src/Range.java index 55dfd6a..3ad0355 100644 --- a/src/Range.java +++ b/src/Range.java @@ -1,6 +1,8 @@ public class Range { private double start; private double stop; + private double start2; + private double stop2; public Range(double min, double max) { start = min; @@ -31,6 +33,67 @@ public double getMin() { public double getMax() { return stop; + + } + + public Range intersection(Range other) { + start2 = other.getMin(); + stop2 = other.getMax(); + + if (start == start2 && stop == stop2) { + Range s = new Range(start, stop); + return s; + } + + else if (start == start2 && stop > stop2) { + Range s = new Range(start, stop2); + return s; + } + + else if (start == start2 && stop < stop2) { + Range s = new Range(start, stop); + return s; + } + + else if (stop == stop2 && start < start2) { + Range s = new Range(start2, stop); + return s; + } + + else if (stop == stop2 && start > start2) { + Range s = new Range(start, stop); + return s; + } + + else if (start < start2 && stop < stop2) { + Range s = new Range(start2, stop); + return s; + } + + else if (start > start2 && stop > stop2) { + Range s = new Range(start, stop2); + return s; + } + + else if (start > start2 && stop < stop2) { + Range s = new Range(start, stop); + return s; + } + + else if (start < start2 && stop > stop2) { + Range s = new Range(start2, stop2); + return s; + } + + else if (stop < start2 || stop2 < start) { + Range s = new Range(0,0); + return s; + } + + else { + Range s = new Range(0,0); + return s; + } } } diff --git a/src/RangeTest.java b/src/RangeTest.java index e900354..5ffb8fe 100644 --- a/src/RangeTest.java +++ b/src/RangeTest.java @@ -5,10 +5,100 @@ public void testNewRange() { Range r = new Range(0,2); assertEquals(true, r.contains(1)); - assertEquals(2, r.getWidth()); - assertEquals(0, r.getMin()); - assertEquals(2, r.getMax()); + assertEquals(2.0, r.getWidth()); + assertEquals(0.0, r.getMin()); + assertEquals(2.0, r.getMax()); } + public void testIntersectionEqual() { + Range r = new Range(1.0,3.0); + Range t = new Range(1.0,3.0); + + Range intersect = r.intersection(t); + assertEquals(1.0, intersect.getMin()); + assertEquals(3.0, intersect.getMax()); + } + + public void testIntersectionSameStartDifferentEnd() { + Range r = new Range(1.0,5.0); + Range t = new Range(1.0,4.0); + + Range intersect = r.intersection(t); + assertEquals(1.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + + public void testIntersectionSameStartDifferentStop2() { + Range r = new Range(1.0,4.0); + Range t = new Range(1.0,5.0); + + Range intersect = r.intersection(t); + assertEquals(1.0, intersect.getMin()); + assertEquals(4.0, intersect.getMax()); + } + + public void testIntersectionSameStopDifferentStart() { + Range r = new Range(1.0,5.0); + Range t = new Range(2.0,5.0); + + Range intersect = r.intersection(t); + assertEquals(2.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionSAmeStopDifferentStart2() { + Range r = new Range(2.0,5.0); + Range t = new Range(1.0,5.0); + + Range intersect = r.intersection(t); + assertEquals(2.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionStartLessStopLess() { + Range r = new Range(1.0,5.0); + Range t = new Range(2.0,6.0); + + Range intersect = r.intersection(t); + assertEquals(2.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionStartGreaterStopGreater() { + Range r = new Range(2.0, 6.0); + Range t = new Range(1.0, 5.0); + + Range intersect = r.intersection(t); + assertEquals(2.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionRangeInRange2() { + Range r = new Range(2.0, 5.0); + Range t = new Range(1.0, 6.0); + + Range intersect = r.intersection(t); + assertEquals(2.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionRange2InRange1() { + Range r = new Range(2.0, 6.0); + Range t = new Range(3.0, 5.0); + + Range intersect = r.intersection(t); + assertEquals(3.0, intersect.getMin()); + assertEquals(5.0, intersect.getMax()); + } + + public void testIntersectionDNE() { + Range r = new Range(2.0, 5.0); + Range t = new Range(7.0, 9.0); + + Range intersect = r.intersection(t); + assertEquals(0.0, intersect.getMin()); + assertEquals(0.0, intersect.getMax()); + + } }