From f4288e15e464233075757d52baf19e5c0dde092d Mon Sep 17 00:00:00 2001 From: Ao Li Date: Wed, 26 Jan 2022 11:49:59 -0600 Subject: [PATCH 1/2] Add a new test case to test branch coverage. You may use the command `mvn jqf:fuzz -Dengine=zest -Dclass=edu.berkeley.cs.jqf.examples.simple.SimpleClassTest -Dmethod=testWithGenerator` to run the new test. If the instrumentation framework is impelmented correctly, the coverage should be higher than or equal to 6. --- .../cs/jqf/examples/simple/SimpleClass.java | 18 ++++++++++++ .../jqf/examples/simple/SimpleClassTest.java | 29 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClass.java create mode 100644 examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClassTest.java diff --git a/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClass.java b/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClass.java new file mode 100644 index 000000000..17c688a06 --- /dev/null +++ b/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClass.java @@ -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; + } +} diff --git a/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClassTest.java b/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClassTest.java new file mode 100644 index 000000000..a7fde97d3 --- /dev/null +++ b/examples/src/test/java/edu/berkeley/cs/jqf/examples/simple/SimpleClassTest.java @@ -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 { + 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); + } +} \ No newline at end of file From 895715c4ca8c5cc52a49b83a02e35d568185d670 Mon Sep 17 00:00:00 2001 From: Ao Li Date: Mon, 14 Feb 2022 14:16:57 -0500 Subject: [PATCH 2/2] Add tests in integration-test. --- .../cs/jqf/fuzz/guidance/ZestGuidanceIT.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/integration-tests/src/test/java/edu/berkeley/cs/jqf/fuzz/guidance/ZestGuidanceIT.java b/integration-tests/src/test/java/edu/berkeley/cs/jqf/fuzz/guidance/ZestGuidanceIT.java index 50f28251f..059fd7123 100644 --- a/integration-tests/src/test/java/edu/berkeley/cs/jqf/fuzz/guidance/ZestGuidanceIT.java +++ b/integration-tests/src/test/java/edu/berkeley/cs/jqf/fuzz/guidance/ZestGuidanceIT.java @@ -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()); + + } }