Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new example to test branch coverage. #175

Merged
merged 2 commits into from
Feb 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package edu.berkeley.cs.jqf.examples.simple;

public class SimpleClass {
public static int test(int a) {
int b = 0;
if (a > 0) {
b += 1;
} else {
b -= 1;
}
if (a % 2 == 0) {
b += 1;
} else {
b -= 1;
}
return b;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package edu.berkeley.cs.jqf.examples.simple;

import com.pholser.junit.quickcheck.From;
import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import edu.berkeley.cs.jqf.fuzz.Fuzz;
import edu.berkeley.cs.jqf.fuzz.JQF;
import org.junit.runner.RunWith;

@RunWith(JQF.class)
public class SimpleClassTest {

public static class SimpleGenerator extends Generator<Integer> {
public SimpleGenerator() {
super(Integer.class);
}

@Override
public Integer generate(SourceOfRandomness sourceOfRandomness, GenerationStatus generationStatus) {
return sourceOfRandomness.nextInt();
}
}

@Fuzz
public void testWithGenerator(@From(SimpleGenerator.class) Integer a) {
SimpleClass.test(a);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,23 @@ public void testPatriciaTrieFuzzer() throws Exception {
Assert.assertEquals(-684278400, zest.hashTotalCoverage());
Assert.assertEquals(-1096184368, zest.hashValidCoverage());
}


// This function tests if the instrumentation framework of Zest handles
// branch instructions in the target program properly.
@Test
public void testSimpleTestCoverage() throws Exception {
String clazz = "edu.berkeley.cs.jqf.examples.simple.SimpleClassTest";
String method = "testWithGenerator";

long trials = 5000;
Random rnd = new Random(42);

ProbedZestGuidance zest = new ProbedZestGuidance("SimpleClassTest", trials, rnd);
GuidedFuzzing.run(clazz, method, classLoader, zest, null);

// Validate result
Assert.assertEquals(7, zest.getTotalCoverage().getNonZeroCount());

}
}