Name:
PID: (page 1)
Symptom: The behavior you see (terminal output, error messages, web page contents)
Bug: A flaw in a program that causes symptoms
Failure-inducing input: Data, values, or other input(s) that demonstrate a bug's symptom(s)
Definitions from John Regehr https://blog.regehr.org/archives/199
class MainExample1 {
public static void main(String[] args) {
int current = 0;
while(current < args.length) {
System.out.println(args[current]);
}
}
}
class MainExample2 {
public static void main(String[] args) {
int length = args.length;
for(int i = 0; i < length; length += 1) {
System.out.println(args[i]);
}
}
}
$ javac Examples.java
$ java MainExample1 apple banana cranberry
$ java MainExample2 apple banana cranberry
class EvensExample {
static int sumEvenIndices(int[] nums) {
int sum = 0;
for(int i = 0; i < nums.length; i += 2) {
sum += nums[i + 1];
}
return sum;
}
}
What are two different failure-inducing inputs for sumEvenIndices that demonstrate different symptoms?
How many bugs are there?
class NumsExample {
static double reciprocal(int n) {
return 1 / n;
}
static double ratio(int n, int d) {
return n / d;
}
static double formula(int a, int b, int c) {
return ratio(a, b) * reciprocal(c);
}
}
What are two different failure-inducing inputs for formula that demonstrate the same symptom?
How many bugs are there?
Name:
PID: (page 2)
import static org.junit.Assert.*;
import org.junit.*;
public class EvensExampleTests {
@Test
public void testSumEvensLength4() {
int[] input1 = { 12, 13, 7, 2};
assertEquals(19, EvensExample.sumEvenIndices(input1));
}
@Test
public void testSumEvenLength5() {
int[] input1 = { 12, 13, 7, 2, 33};
assertEquals(52, EvensExample.sumEvenIndices(input1));
}
@Test
public void testSumEvenLength6() {
int[] input1 = { 12, 13, 7, 8, 5, 3};
assertEquals(24, EvensExample.sumEvenIndices(input1));
}
}
$ javac -cp .:lib/hamcrest-core-1.3.jar:lib/junit-4.13.2.jar EvensExample.java EvensExampleTests.java
$ java -cp .:lib/hamcrest-core-1.3.jar:lib/junit-4.13.2.jar org.junit.runner.JUnitCore EvensExampleTests
JUnit version 4.13.2
.E..E
Time: 0.007
There were 2 failures:
1) testSumEvenLength5(MethodsTests)
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5
at EvensExample.sumEvenIndices(Methods.java:23)
at MethodsTests.testSumEvenLength5(MethodsTests.java:13)
2) testSumEvensLength4(MethodsTests)
java.lang.AssertionError: expected:<19> but was:<15>
at org.junit.Assert.fail(Assert.java:89)
at org.junit.Assert.failNotEquals(Assert.java:835)
at org.junit.Assert.assertEquals(Assert.java:647)
at org.junit.Assert.assertEquals(Assert.java:633)
at MethodsTests.testSumEvensLength4(MethodsTests.java:8)
FAILURES!!!
Tests run: 3, Failures: 2
MethodsTests.java
Which tests passed? Which failed?
What other tests would be important to write? Any way to structure the choice of tests?
Symptom: The behavior you see (terminal output, error messages, web page contents)
Bug: A flaw in a program that causes symptoms
Failure-inducing input: Data, values, or other input(s) that demonstrate a bug's symptom(s)