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

more IntCharSet quickcheck #680

Merged
merged 6 commits into from
Dec 12, 2019
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
2 changes: 1 addition & 1 deletion jflex/src/main/java/jflex/core/unicode/CharClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CharClasses {
private static final boolean DEBUG = false;

/** the largest character that can be used in char classes */
static final int maxChar = 0x10FFFF;
public static final int maxChar = 0x10FFFF;

/** the char classes */
private List<IntCharSet> classes;
Expand Down
11 changes: 11 additions & 0 deletions jflex/src/test/java/jflex/chars/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
java_library(
name = "chars",
testonly = True,
srcs = ["IntervalGen.java"],
visibility = ["//jflex:visibility"],
deps = [
"//jflex/src/main/java/jflex/chars",
"//jflex/src/main/java/jflex/core/unicode",
"//third_party/com/pholser/quickcheck",
],
)
60 changes: 60 additions & 0 deletions jflex/src/test/java/jflex/chars/IntervalGen.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* JFlex 1.8.0-SNAPSHOT *
* Copyright (C) 1998-2019 Gerwin Klein <[email protected]> *
* All rights reserved. *
* *
* License: BSD *
* *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */

package jflex.chars;

import com.pholser.junit.quickcheck.generator.GenerationStatus;
import com.pholser.junit.quickcheck.generator.Generator;
import com.pholser.junit.quickcheck.generator.InRange;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import jflex.core.unicode.CharClasses;

/**
* Generator for random {@link Interval} instances.
*
* @author Gerwin Klein
* @version JFlex 1.8.0-SNAPSHOT
* @see Interval
*/
public class IntervalGen extends Generator<Interval> {

/** Min bound for intervals */
private int minChar = 0;
/** Max bound for intervals. Small for speed, and more likely edge cases. */
private int maxChar = 50;

/** How often to return single-character intervals. */
private float singleCharRatio = 0.2f;

/** Constructs and registers generator for Interval */
public IntervalGen() {
super(Interval.class);
}

@Override
public Interval generate(SourceOfRandomness random, GenerationStatus status) {
int a = random.nextInt(minChar, maxChar);
if (random.nextFloat(0, 1) > singleCharRatio) {
int b = random.nextInt(minChar, maxChar);
return a <= b ? new Interval(a, b) : new Interval(b, a);
} else {
return Interval.ofCharacter(a);
}
}

/**
* Configure this generator to only produce intervals in the given range.
*
* @param range annotation that contains the intervals constraints
*/
public void configure(InRange range) {
minChar = Math.max(0, range.minInt());
maxChar = Math.min(range.maxInt(), CharClasses.maxChar);
}
}
3 changes: 3 additions & 0 deletions jflex/src/test/java/jflex/core/unicode/BUILD
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
java_test(
name = "IntCharSetQuickcheck",
timeout = "short",
srcs = [
"IntCharSetGen.java",
"IntCharSetQuickcheck.java",
Expand All @@ -8,13 +9,15 @@ java_test(
"//jflex/src/main/java/jflex/chars",
"//jflex/src/main/java/jflex/core",
"//jflex/src/main/java/jflex/core/unicode",
"//jflex/src/test/java/jflex/chars",
"//third_party/com/google/truth",
"//third_party/com/pholser/quickcheck",
],
)

java_test(
name = "IntCharSetTest",
timeout = "short",
srcs = ["IntCharSetTest.java"],
deps = [
"//jflex/src/main/java/jflex/chars",
Expand Down
30 changes: 7 additions & 23 deletions jflex/src/test/java/jflex/core/unicode/IntCharSetGen.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
import com.pholser.junit.quickcheck.generator.InRange;
import com.pholser.junit.quickcheck.generator.Size;
import com.pholser.junit.quickcheck.random.SourceOfRandomness;
import jflex.chars.Interval;
import jflex.chars.IntervalGen;

/**
* Generator for random {@link IntCharSet} instances.
Expand All @@ -25,19 +25,18 @@
*/
public class IntCharSetGen extends Generator<IntCharSet> {

/** Min bound for intervals */
private int minChar = 0;
/** Max bound for intervals. Small for speed, and more likely edge cases. */
private int maxChar = 50;

/** Min bound for number of intervals (0 = empty set) */
private int minSize = 0;
/** Max bound for number of intervals */
private int maxSize = 5;

/** Generator for Intervals */
private IntervalGen intervals;

/** Constructs generator for IntCharSet */
public IntCharSetGen() {
super(IntCharSet.class);
this.intervals = new IntervalGen();
}

@Override
Expand All @@ -46,21 +45,7 @@ public IntCharSet generate(SourceOfRandomness r, GenerationStatus status) {

int numIntervals = r.nextInt(minSize, maxSize);
for (int i = 0; i < numIntervals; i++) {
int start = r.nextInt(minChar, maxChar);
int end = r.nextInt(start, maxChar);

// pick default with higher probability
switch (r.nextInt(0, 4)) {
case 0:
result.add(IntCharSet.ofCharacter(start));
break;
case 1:
result.add(start);
break;
default:
result.add(new Interval(start, end));
break;
}
result.add(intervals.generate(r, status));
}

return result;
Expand All @@ -72,8 +57,7 @@ public IntCharSet generate(SourceOfRandomness r, GenerationStatus status) {
* @param range annotation that contains the intervals constraints
*/
public void configure(InRange range) {
minChar = Math.max(0, range.minInt());
maxChar = Math.min(range.maxInt(), CharClasses.maxChar);
intervals.configure(range);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

import com.pholser.junit.quickcheck.Property;
import com.pholser.junit.quickcheck.generator.InRange;
import com.pholser.junit.quickcheck.generator.Size;
import com.pholser.junit.quickcheck.runner.JUnitQuickcheck;
import jflex.chars.Interval;
import org.junit.runner.RunWith;

/**
Expand Down Expand Up @@ -168,4 +170,27 @@ public void complement(IntCharSet set) {
comp.add(set);
assertThat(comp).isEqualTo(IntCharSet.allChars());
}

@Property
public void invariants(@Size(max = 15) @InRange(maxInt = CharClasses.maxChar) IntCharSet set) {
assertThat(set.invariants()).isTrue();
}

@Property
public void addConsistent(
@Size(max = 10) @InRange(maxInt = CharClasses.maxChar) IntCharSet set,
@InRange(maxInt = CharClasses.maxChar) int c) {
IntCharSet set2 = IntCharSet.copyOf(set);
set.add(c);
set2.add(Interval.ofCharacter(c));
assertThat(set).isEqualTo(set2);
}

@Property
public void singleInterval(@InRange(maxInt = CharClasses.maxChar) Interval i) {
IntCharSet set1 = IntCharSet.of(i);
IntCharSet set2 = new IntCharSet();
set2.add(i);
assertThat(set1).isEqualTo(set2);
}
}