com.pholser.junit.quickcheck.Property Java Examples

The following examples show how to use com.pholser.junit.quickcheck.Property. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void clearsToZero(int[] keys) {
    // Fill counter with some data
    Counter counter = new NonZeroCachingCounter(COUNTER_SIZE);
    for (int key : keys) {
        int before = counter.get(key);
        int after = counter.increment(key);
        assertEquals(before + 1, after);
    }

    // Clear it
    counter.clear();

    // Check that all are zero
    for (int i = 0; i < COUNTER_SIZE; i++) {
        assertEquals(0, counter.get(i));
    }

    for (int index : keys) {
        assertEquals(0, counter.get(index));
    }

    assertThat(counter.getNonZeroSize(), is(0));
    assertThat(counter.getNonZeroIndices(), iterableWithSize(0));
    assertThat(counter.getNonZeroValues(), iterableWithSize(0));
}
 
Example #2
Source File: CountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void incrementDeltaWorks(int[] keys, int delta) {
    Counter counter = new Counter(COUNTER_SIZE);
    // Check local state
    for (int key : keys) {
        int before = counter.get(key);
        int after = counter.increment(key, delta);
        assertEquals(before+delta, after);
    }
    // Check global state
    int sum = 0;
    for (int i = 0; i < COUNTER_SIZE; i++) {
        sum += counter.get(i);
    }
    assertEquals(keys.length*delta, sum);
}
 
Example #3
Source File: CountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void incrementDeltaWorks(int key, int delta, @InRange(minInt=2, maxInt=42) int times) {
    Counter counter = new Counter(COUNTER_SIZE);
    // Check local state
    for (int j = 0; j < times; j++) {
        int before = counter.get(key);
        int after = counter.increment(key, delta);
        assertEquals(before+delta, after);
    }
    // Check global state
    int sum = 0;
    for (int i = 0; i < COUNTER_SIZE; i++) {
        sum += counter.get(i);
    }
    assertEquals(delta*times, sum);
}
 
Example #4
Source File: CountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void nonZeroValuesIsAccurate(int[] keys, int delta) {
    Counter counter = new Counter(COUNTER_SIZE);
    for (int key : keys) {
        counter.increment(key, delta);
    }

    Collection<Integer> nonZeroValues = counter.getNonZeroValues();
    assertThat(nonZeroValues.size(), lessThanOrEqualTo(keys.length));


    int sum = 0;
    for (int v : nonZeroValues) {
        sum += v;
    }

    assertEquals(keys.length * delta, sum);

}
 
Example #5
Source File: CombinatorsTest.java    From funcj with MIT License 6 votes vote down vote up
@Property
public void satisfyAppliesPredicate(char c0) {
    final char even;
    final char odd;
    if (c0 % 2 == 0) {
        even = c0;
        odd = (char)((even + 1) % 0xffff);
    } else {
        odd = c0;
        even = (char)((odd + 1) % 0xffff);
    }

    final Input<Chr> input1 = Input.of(String.valueOf(even));
    final Input<Chr> input2 = Input.of(String.valueOf(odd));

    final Parser<Chr, Chr> parser =
            Combinators.satisfy("even", Predicate.of((Chr c) -> c.charValue() % 2 == 0));

    TestUtils.ParserCheck.parser(parser)
            .withInput(input1)
            .succeedsWithResult(Chr.valueOf(even), input1.next());

    TestUtils.ParserCheck.parser(parser)
            .withInput(input2)
            .fails();
}
 
Example #6
Source File: CountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void setAtIndexWorks(@InRange(minInt=0, maxInt=COUNTER_SIZE-1) int index, int value) {
    Counter counter = new Counter(COUNTER_SIZE);
    counter.setAtIndex(index, value);
    assertEquals(value, counter.getAtIndex(index));


    int nonZeroSize = counter.getNonZeroSize();
    Collection<Integer> nonZeroIndices = counter.getNonZeroIndices();
    Collection<Integer> nonZeroValues = counter.getNonZeroValues();
    if (value == 0) {
        assertThat(nonZeroSize, is(0));
        assertThat(nonZeroIndices, iterableWithSize(0));
        assertThat(nonZeroValues, iterableWithSize(0));
    } else {
        assertThat(nonZeroSize, is(1));
        assertThat(nonZeroIndices, iterableWithSize(1));
        assertThat(nonZeroValues, iterableWithSize(1));
    }

}
 
Example #7
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void incrementWorks(int[] keys) {
    Counter counter = new NonZeroCachingCounter(COUNTER_SIZE);
    // Check local state
    for (int key : keys) {
        int before = counter.get(key);
        int after = counter.increment(key);
        assertEquals(before+1, after);
    }
    // Check global state
    int sum = 0;
    for (int i = 0; i < COUNTER_SIZE; i++) {
        sum += counter.get(i);
    }
    assertEquals(keys.length, sum);
}
 
Example #8
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void incrementDeltaWorks(int[] keys, int delta) {
    Counter counter = new NonZeroCachingCounter(COUNTER_SIZE);
    // Check local state
    for (int key : keys) {
        int before = counter.get(key);
        int after = counter.increment(key, delta);
        assertEquals(before+delta, after);
    }
    // Check global state
    int sum = 0;
    for (int i = 0; i < COUNTER_SIZE; i++) {
        sum += counter.get(i);
    }
    assertEquals(keys.length*delta, sum);
}
 
Example #9
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 6 votes vote down vote up
@Property
public void incrementDeltaWorks(int key, int delta, @InRange(minInt=2, maxInt=42) int times) {
    Counter counter = new NonZeroCachingCounter(COUNTER_SIZE);
    // Check local state
    for (int j = 0; j < times; j++) {
        int before = counter.get(key);
        int after = counter.increment(key, delta);
        assertEquals(before+delta, after);
    }
    // Check global state
    int sum = 0;
    for (int i = 0; i < COUNTER_SIZE; i++) {
        sum += counter.get(i);
    }
    assertEquals(delta*times, sum);
}
 
Example #10
Source File: TryTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void flatMap(char c) {
    final char e = c == 'X' ? 'x' : 'X';
    final String cs = String.valueOf(c);
    assertEquals(Try.success(e), Try.success(c).flatMap(d -> Try.success(e)));
    assertEquals(failure(cs), Try.success(c).flatMap(d -> failure(cs)));
    assertEquals(failure(cs), failure(cs).flatMap(d -> Try.success(e)));
    assertEquals(failure(cs), failure(cs).flatMap(d -> failure("error")));
}
 
Example #11
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void nonZeroIndicesIsAccurate(int[] keys, int delta) {
    Counter counter1 = new Counter(COUNTER_SIZE);
    Counter counter2 = new NonZeroCachingCounter(COUNTER_SIZE);
    for (int key : keys) {
        counter1.increment(key, delta);
        counter2.increment(key, delta);
    }

    Collection<Integer> nonZeroIndices1 = counter1.getNonZeroIndices();
    Collection<Integer> nonZeroIndices2 = counter2.getNonZeroIndices();
    assertEquals(new HashSet<>(nonZeroIndices1), new HashSet<>(nonZeroIndices2));

}
 
Example #12
Source File: OptionTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void kleisliIsAssociative(int i) {
    check(
            "Kleisli Associativity",
            i,
            isPositive.andThen(isEven).andThen(upToFirstZero),
            isPositive.andThen(isEven.andThen(upToFirstZero)));
}
 
Example #13
Source File: ParserTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void apChainsParsers(char c1, char c2) {
    Assume.assumeThat(c1, not(c2));

    final Chr cc1 = Chr.valueOf(c1);
    final Chr cc2 = Chr.valueOf(c2);

    // String.toCharArray creates a new array each time, so ensure we call it only once.
    final char[] ca12 = ("" + c1 + c2).toCharArray();
    final char[] ca11 = ("" + c1 + c1).toCharArray();

    final Parser<Chr, Tuple2<Chr, Chr>> parser =
            ap(
                    ap(
                            a -> b -> Tuple2.of(a, b),
                            value(cc1)
                    ), value(cc2)
            );

    TestUtils.ParserCheck.parser(parser)
            .withInput(Input.of(ca12))
            .succeedsWithResult(Tuple2.of(cc1, cc2), Input.of(ca12).next().next());

    TestUtils.ParserCheck.parser(parser)
            .withInput(Input.of(ca11))
            .fails();
}
 
Example #14
Source File: ParserTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void apAppliesF(char c1) {
    final Input<Chr> input = Input.of(String.valueOf(c1));

    final F<Chr, Chr> f = c -> Chr.valueOf(c.charValue() + 1);

    final Parser<Chr, Chr> parser = ap(f, any());

    TestUtils.ParserCheck.parser(parser)
            .withInput(input)
            .succeedsWithResult(Chr.valueOf(c1 + 1), input.next());
}
 
Example #15
Source File: CombinatorsTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void anyFailsOnNonEmptyInput() {

    final Parser<Chr, Chr> parser = Combinators.any();

    TestUtils.ParserCheck.parser(parser)
            .withInput(Input.of(""))
            .fails();
}
 
Example #16
Source File: CombinatorsTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void eofSucceedsOnEmptyInput(char c1) {
    final Input<Chr> input = Input.of("");

    final Parser<Chr, Unit> parser = Combinators.eof();

    TestUtils.ParserCheck.parser(parser)
            .withInput(input)
            .succeedsWithResult(Unit.UNIT, input);
}
 
Example #17
Source File: StateRTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void testStateR(String a, String b, String c) {
    final String r =
            StateR.put(a).flatMap(u -> StateR.get())
                    .flatMap(s -> StateR.put(s + b))
                    .flatMap(u -> StateR.get())
                    .eval(c);

    assertEquals(a+b, r);
}
 
Example #18
Source File: TextTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void testString(String s) {
    assumeFalse(s.isEmpty());

    final Parser<Chr, String> p = Text.string(s);
    final Result<Chr, String> r = p.parse(Input.of(s));
    assertEquals("string(" + s + ").parse(" + s + ")", s, r.getOrThrow());
}
 
Example #19
Source File: CombinatorsTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void failAlwaysFails(char c1) {
    final Input<Chr> input = Input.of(String.valueOf(c1));

    final Parser<Chr, Chr> parser = Combinators.fail();

    TestUtils.ParserCheck.parser(parser)
            .withInput(input)
            .fails();
}
 
Example #20
Source File: NonZeroCachingCountersTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void nonZeroSizeIsAccurate(int[] keys, int delta) {
    Counter counter1 = new Counter(COUNTER_SIZE);
    Counter counter2 = new NonZeroCachingCounter(COUNTER_SIZE);
    for (int key : keys) {
        counter1.increment(key, delta);
        counter2.increment(key, delta);
    }

    int nonZeroSize1 = counter1.getNonZeroSize();
    int nonZeroSize2 = counter2.getNonZeroSize();
    assertEquals(nonZeroSize1, nonZeroSize2);

}
 
Example #21
Source File: ParserTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void manySucceedsOnNonEmptyInput() {
    final Input<Chr> input = Input.of("");

    final Parser<Chr, String> parser = any(Chr.class).many().map(Chr::listToString);

    TestUtils.ParserCheck.parser(parser)
            .withInput(input)
            .succeedsWithResult("", input);
}
 
Example #22
Source File: ParserTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void manyTillMatches(char c1, char c2) {
    Assume.assumeThat(c1, not(c2));

    final String s = "" + c1 + c1 + c1 + c1;
    final char[] ca = (s + c2).toCharArray();

    final Parser<Chr, String> parser =
            Text.chr(c1).manyTill(Text.chr(c2))
                    .map(Chr::listToString);

    TestUtils.ParserCheck.parser(parser)
            .withInput(Input.of(ca))
            .succeedsWithResult(s, Input.of(ca).next().next().next().next().next());
}
 
Example #23
Source File: CombinatorsTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void anyMatchesAnything(char c1) {
    final Input<Chr> input = Input.of(String.valueOf(c1));

    final Parser<Chr, Chr> parser = Combinators.any();

    TestUtils.ParserCheck.parser(parser)
            .withInput(input)
            .succeedsWithResult(Chr.valueOf(c1), input.next());
}
 
Example #24
Source File: ExecutionIndexingTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void comparesLexicographically(@InRange(minInt=1, maxInt=32) int @Size(min=1, max=20)[] v1) {
    assumeTrue(v1.length % 2 == 0);
    int[] v2 = Arrays.copyOf(v1, v1.length);
    v2[v1.length/2]--; // Make v2 less than v1

    ExecutionIndex e1 = new ExecutionIndex(v1);
    ExecutionIndex e2 = new ExecutionIndex(v2);
    assertThat(e1.compareTo(e2), greaterThan(0));
    assertThat(e2.compareTo(e1), lessThan(0));
}
 
Example #25
Source File: TryTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void andMap(int a, int b) {
    final Try<Integer> tiA = Try.success(a);
    final Try<Integer> tiB = Try.success(b);

    assertEquals("", Try.success(a+b), tiA.and(tiB).map(iA -> iB -> iA + iB));
    assertEquals("", fail, fail.and(tiB).map(iA -> iB -> iA + iB));
    assertEquals("", fail, tiA.and(fail).map(iA -> iB -> iA + iB));
}
 
Example #26
Source File: StateRTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void testStateR2(String a, String b) {
    final String r =
            StateR.<String>get()
                    .flatMap(s -> StateR.put(s + a))
                    .flatMap(u -> StateR.get())
                    .eval(b);
    assertEquals(b+a, r);
}
 
Example #27
Source File: ExecutionIndexingTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void splitsAndJoinsPrefixSuffix(@InRange(minInt=1, maxInt=32) int @Size(min=20, max=20)[] v,
                                       @InRange(minInt=0, maxInt=10) int offset) {
    assumeTrue(v.length % 2 == 0);
    ExecutionIndex e = new ExecutionIndex(v);

    ExecutionIndex.Prefix prefix = new ExecutionIndex.Prefix(e, offset);
    ExecutionIndex.Suffix suffix = e.getSuffixOfPrefix(prefix);

    assertEquals(e, new ExecutionIndex(prefix, suffix));

}
 
Example #28
Source File: ExecutionIndexingTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void hasOwnPrefixAndSuffix(@InRange(minInt=1, maxInt=32) int @Size(min=4, max=20)[] v) {
    assumeTrue(v.length % 2 == 0);
    ExecutionIndex e = new ExecutionIndex(v);

    ExecutionIndex.Suffix selfCommonSuffix = e.getCommonSuffix(e);
    ExecutionIndex.Prefix suffixMinusPrefix = e.getPrefixOfSuffix(selfCommonSuffix);

    assertEquals(v.length/2, selfCommonSuffix.size());
    assertEquals(0, suffixMinusPrefix.size());
    assertEquals(true, e.hasPrefix(suffixMinusPrefix));
}
 
Example #29
Source File: ExecutionIndexingTest.java    From JQF with BSD 2-Clause "Simplified" License 5 votes vote down vote up
@Property
public void comparesLength(@InRange(minInt=1, maxInt=32) int @Size(min=4, max=20)[] v1) {
    assumeTrue(v1.length % 2 == 0);
    int[] v2 = Arrays.copyOf(v1, v1.length-2); // v2 is smaller

    ExecutionIndex e1 = new ExecutionIndex(v1);
    ExecutionIndex e2 = new ExecutionIndex(v2);
    assertThat(e1.compareTo(e2), greaterThan(0));
    assertThat(e2.compareTo(e1), lessThan(0));
}
 
Example #30
Source File: ParserTest.java    From funcj with MIT License 5 votes vote down vote up
@Property
public void many1MatchesMany(char c1, char c2) {
    final String s = "" + c1 + c2;
    final char[] ca = s.toCharArray();

    final Parser<Chr, String> parser = any(Chr.class).many1().map(Chr::listToString);

    TestUtils.ParserCheck.parser(parser)
            .withInput(Input.of(ca))
            .succeedsWithResult(s, Input.of(ca).next().next());
}