Skip to content
This repository was archived by the owner on Dec 8, 2025. It is now read-only.

Commit 23c2d4b

Browse files
authored
Update random generators (#1152)
Update random generators to support long generation and bounded integers.
1 parent d94d9e9 commit 23c2d4b

8 files changed

Lines changed: 88 additions & 16 deletions

File tree

common/src/main/java/com/graphicsfuzz/common/util/CannedRandom.java

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,14 @@ public class CannedRandom implements IRandom {
3030
* Constructs a canned random instance that will iterate over the given lists of integers
3131
* and booleans.
3232
*
33-
* @param intsAndBools Ordered sequence of integers and booleans
33+
* @param intsLongsAndBools Ordered sequence of integers, long and booleans
3434
*/
35-
public CannedRandom(Object... intsAndBools) {
36-
if (Arrays.stream(intsAndBools).anyMatch(item -> !(item instanceof Integer
37-
|| item instanceof Boolean))) {
38-
throw new IllegalArgumentException("Only Integer and Boolean items allowed.");
35+
public CannedRandom(Object... intsLongsAndBools) {
36+
if (Arrays.stream(intsLongsAndBools).anyMatch(item -> !(item instanceof Integer
37+
|| item instanceof Boolean || item instanceof Long))) {
38+
throw new IllegalArgumentException("Only Integer, Long and Boolean items allowed.");
3939
}
40-
this.items = Arrays.asList(intsAndBools).iterator();
40+
this.items = Arrays.asList(intsLongsAndBools).iterator();
4141
}
4242

4343
@Override
@@ -50,6 +50,21 @@ public int nextInt(int bound) {
5050
return Math.abs((Integer)next) % bound;
5151
}
5252

53+
@Override
54+
public int nextInt(int origin, int bound) {
55+
return nextInt(bound);
56+
}
57+
58+
@Override
59+
public long nextLong(long bound) {
60+
Object next = items.next();
61+
if (!(next instanceof Long)) {
62+
throw new UnsupportedOperationException("nextLong failed because next item was a "
63+
+ next.getClass() + "(" + next + ")");
64+
}
65+
return (Long) next;
66+
}
67+
5368
@Override
5469
public Float nextFloat() {
5570
throw new RuntimeException("Not yet supported.");

common/src/main/java/com/graphicsfuzz/common/util/IRandom.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public interface IRandom {
2424

2525
int nextInt(int bound);
2626

27+
/**
28+
* Return an integer in the range [origin, bound) if bound > origin
29+
* @param origin minimal integer that the generator can produce
30+
* @param bound maximal (excluded) integer that the generator can produce
31+
* @return an integer from the range [origin, bound)
32+
*/
33+
default int nextInt(int origin, int bound) {
34+
assert bound > origin;
35+
return (int) (nextLong(((long)bound - (long) origin)) + (long) origin);
36+
}
37+
38+
long nextLong(long bound);
39+
2740
Float nextFloat();
2841

2942
default int nextPositiveInt(int bound) {

common/src/main/java/com/graphicsfuzz/common/util/RandomWrapper.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ public Float nextFloat() {
4343
return provider.nextFloat();
4444
}
4545

46+
@Override
47+
public long nextLong(long bound) {
48+
return provider.nextLong(bound);
49+
}
50+
4651
@Override
4752
public boolean nextBoolean() {
4853
return provider.nextBoolean();

common/src/main/java/com/graphicsfuzz/common/util/SameValueRandom.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@ public class SameValueRandom implements IRandom {
2020

2121
private final boolean boolValue;
2222
private final int intValue;
23+
private final long longValue;
2324

24-
public SameValueRandom(boolean boolValue, int intValue) {
25+
public SameValueRandom(boolean boolValue, int intValue, long longValue) {
2526
this.boolValue = boolValue;
2627
this.intValue = intValue;
28+
this.longValue = longValue;
2729
}
2830

2931
@Override
@@ -32,6 +34,19 @@ public int nextInt(int bound) {
3234
return intValue;
3335
}
3436

37+
@Override
38+
public int nextInt(int origin, int bound) {
39+
assert intValue < bound;
40+
assert intValue >= origin;
41+
return intValue;
42+
}
43+
44+
@Override
45+
public long nextLong(long bound) {
46+
assert longValue < bound;
47+
return longValue;
48+
}
49+
3550
@Override
3651
public Float nextFloat() {
3752
throw new RuntimeException("Not yet supported.");

common/src/main/java/com/graphicsfuzz/common/util/ZeroCannedRandom.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ public int nextInt(int bound) {
2323
return 0;
2424
}
2525

26+
@Override
27+
public int nextInt(int origin, int bound) {
28+
return 0;
29+
}
30+
31+
@Override
32+
public long nextLong(long bound) {
33+
return 0L;
34+
}
35+
2636
@Override
2737
public Float nextFloat() {
2838
return 0.0f;

generator/src/test/java/com/graphicsfuzz/generator/semanticschanging/Expr2BinaryMutationFinderTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,20 @@ public int nextInt(int bound) {
8080
return 2;
8181
}
8282

83+
@Override
84+
public int nextInt(int origin, int bound) {
85+
throw new RuntimeException("This method was added to the IRandom interface after this test"
86+
+ " was written and thus should not be called; if it becomes required the IRandom"
87+
+ " implementation in the test should be revised.");
88+
}
89+
90+
@Override
91+
public long nextLong(long bound) {
92+
throw new RuntimeException("This method was added to the IRandom interface after this test"
93+
+ " was written and thus should not be called; if it becomes required the IRandom"
94+
+ " implementation in the test should be revised.");
95+
}
96+
8397
@Override
8498
public Float nextFloat() {
8599
throw new UnsupportedOperationException();

reducer/src/test/java/com/graphicsfuzz/reducer/reductionopportunities/UnwrapReductionOpportunitiesTest.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void testBlock1() throws Exception {
4545
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
4646
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
4747
new ReducerContext(false,
48-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0), new IdGenerator()));
48+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L), new IdGenerator()));
4949
assertEquals(1, ops.size());
5050
}
5151

@@ -56,7 +56,7 @@ public void testEmptyBlock() throws Exception {
5656
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
5757
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
5858
new ReducerContext(false,
59-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0),
59+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L),
6060
new IdGenerator()));
6161
// No opportunities because the inner block is empty. Another reduction pass may be able to
6262
// delete it, but it cannot be unwrapped.
@@ -70,7 +70,7 @@ public void testNestedEmptyBlocks() throws Exception {
7070
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
7171
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
7272
new ReducerContext(false,
73-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0),
73+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L),
7474
new IdGenerator()));
7575
assertEquals(1, ops.size());
7676
}
@@ -83,7 +83,7 @@ public void testUnwrapWithDeclNoClash() throws Exception {
8383
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
8484
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
8585
new ReducerContext(false,
86-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0), new IdGenerator()));
86+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L), new IdGenerator()));
8787
assertEquals(1, ops.size());
8888
ops.get(0).applyReduction();
8989
CompareAsts.assertEqualAsts(expected, tu);
@@ -97,7 +97,7 @@ public void testNoUnwrapWithDeclClash() throws Exception {
9797
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
9898
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
9999
new ReducerContext(false,
100-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0),
100+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L),
101101
new IdGenerator()));
102102
// The inner block cannot be unwrapped as it would change which variable 'x = 2' refers to.
103103
assertEquals(1, ops.size());
@@ -110,7 +110,7 @@ public void testNoUnwrapWithDeclClash2() throws Exception {
110110
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
111111
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
112112
new ReducerContext(false,
113-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0),
113+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L),
114114
new IdGenerator()));
115115
assertEquals(0, ops.size());
116116
}
@@ -122,7 +122,7 @@ public void testOneUnwrapDisablesAnother() throws Exception {
122122
List<UnwrapReductionOpportunity> ops = UnwrapReductionOpportunities
123123
.findOpportunities(MakeShaderJobFromFragmentShader.make(tu),
124124
new ReducerContext(false,
125-
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0),
125+
ShadingLanguageVersion.GLSL_440, new SameValueRandom(false, 0, 0L),
126126
new IdGenerator()));
127127
assertEquals(2, ops.size());
128128
assertTrue(ops.get(0).preconditionHolds());

tester/src/test/java/com/graphicsfuzz/tester/MiscellaneousGenerateThenReduceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ private void checkControlFlowWrapElimination(String program)
6868
final ShadingLanguageVersion shadingLanguageVersion = ShadingLanguageVersion.GLSL_440;
6969
new AddWrappingConditionalTransformation().apply(tu,
7070
TransformationProbabilities.onlyWrap(),
71-
new SameValueRandom(false, 0),
71+
new SameValueRandom(false, 0, 0L),
7272
GenerationParams.normal(ShaderKind.FRAGMENT, true));
7373

7474
System.out.println(PrettyPrinterVisitor.prettyPrintAsString(tu));
@@ -78,7 +78,7 @@ private void checkControlFlowWrapElimination(String program)
7878
.getReductionOpportunities(new GlslShaderJob(Optional.empty(),
7979
new PipelineInfo(), tu),
8080
new ReducerContext(false, shadingLanguageVersion,
81-
new SameValueRandom(false, 0), new IdGenerator()), fileOps);
81+
new SameValueRandom(false, 0, 0L), new IdGenerator()), fileOps);
8282
if (ops.isEmpty()) {
8383
break;
8484
}

0 commit comments

Comments
 (0)