CS173: Intro to Computer Science - Testing
Activity Goals
The goals of this activity are:- To be able to design test cases for unit testing a program
- To design code modules for unit testing
Warmup
We know that a signed 32-bit value, in seconds, will overflow in 68 years. How long would a 64-bit value last before overflowing?The Activity
Directions
Consider the activity models and answer the questions provided. First reflect on these questions on your own briefly, before discussing and comparing your thoughts with your group. Appoint one member of your group to discuss your findings with the class, and the rest of the group should help that member prepare their response. Answer each question individually from the activity, and compare with your group to prepare for our whole-class discussion. After class, think about the questions in the reflective prompt and respond to those individually in your notebook. Report out on areas of disagreement or items for which you and your group identified alternative approaches. Write down and report out questions you encountered along the way for group discussion.Model 1: Choosing Unit Tests
1 2 3 4 5 6 7 8 9 10 11 | public class Main { public static int triangleArea( int base, int height) { // Broken Code! return 1 / 2 * base * height; } public static void main(String[] args) { System.out.println(triangleArea(5, 2)); System.out.println(triangleArea(3, -2)); } } |
Questions
- What's wrong with this code? (there is more than one answer!)
- How many calls would you make to
triangleArea
before you decide that it is "passing?" What parameter inputs would you supply to those calls? - Visit this guide and design a unit test for
triangleArea
. You can just write the code in your notes: there is no need to compile or execute it now (we will do this in lab instead!). - Recall that floating point types cannot always be compared directly for equality, due to rounding and precision limitations. Change this program to use
double
data types, and re-generate unit tests for it. Where do you think a floating point tolerance can be added with theassertEquals
function?
Model 2: Thinking Critically about Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | public class Main { public static double calculateIncomeTax( double income) { double result = 0; double base10 = 9950 * 0.1; double base12 = base10 + (40525 - 9950) * 0.12; double base22 = base12 + (86375 - 40525) * 0.22; double base24 = base22 + (164925 - 86375) * 0.24; double base32 = base24 + (209425 - 164925) * 0.32; double base35 = base32 + (523600 - 209425) * 0.35; if (income < 9950) { result = income * 0.1; } else if (income < 40525) { result = base10 + (income - 9950) * 0.12; } else if (income < 86375) { result = base12 + (income - 40525) * 0.22; } else if (income < 164925) { result = base22 + (income - 86375) * 0.24; } else if (income < 209425) { result = base24 + (income - 164925) * 0.32; } else if (income < 523600) { result = base32 + (income - 209425) * 0.35; } else { result = base35 + (income - 523600) * 0.37; } return result; } public static void main(String[] args) { double taxOwed = calculateIncomeTax(25000); System.out.println( "I owe: $" + taxOwed); } } |
Questions
- What kinds of inputs would make this function fail (or return values that don't make sense)? What can you do about this?
- What tests, at a minimum, would you propose to thoroughly exercise this function?
Model 3: Facilitating Unit Tests
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | import java.util.Random; public class Main { public static boolean isHeads() { Random rng = new Random(); // Use the random number generator (rng) // to generate a value >= 0.0 and < 1.0 double randomValue = rng.nextDouble(); boolean result = false ; if (randomValue > 0.5) { result = true ; } return result; } public static void main(String[] args) { System.out.println(isHeads()); System.out.println(isHeads()); } } |
Questions
- What makes this a difficult function to test?
- What could we do to better facilitate testing a function like this?
- Print the random number
randomValue
in theisHeads
function when you compute it. Then, callrng.setSeed(100);
right before the call torng.nextDouble()
, and try running the program again. What do you notice?