forked from functionaljava/functionaljava
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReflect.java
More file actions
153 lines (139 loc) · 6.15 KB
/
Reflect.java
File metadata and controls
153 lines (139 loc) · 6.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package fj.demo.test;
import static fj.Bottom.error;
import fj.Effect;
import fj.F;
import fj.F2;
import fj.P2;
import fj.data.List;
import static fj.data.List.list;
import static fj.Equal.stringBuilderEqual;
import static fj.test.Arbitrary.arbCharacter;
import static fj.test.Arbitrary.arbInteger;
import static fj.test.Arbitrary.arbIntegerBoundaries;
import static fj.test.Arbitrary.arbStringBuilder;
import static fj.test.Bool.bool;
import fj.test.CheckResult;
import static fj.test.CheckResult.summary;
import fj.test.Property;
import static fj.test.Property.prop;
import static fj.test.Property.property;
import static fj.test.Shrink.shrinkInteger;
import fj.test.reflect.Category;
import static fj.test.reflect.Check.check;
import fj.test.reflect.CheckParams;
import fj.test.reflect.Name;
import fj.test.reflect.NoCheck;
import static java.lang.System.out;
/*
Performs four test runs of these properties using various test parameters and categories.
A test property is any of the following:
- a static field of type reductio.Property.
- a static no-argument method that returns reductio.Property.
- a non-static field of type reductio.Property in a class with a no-argument constructor.
- a non-static no-argument method that returns reductio.Property in a class with a no-argument
constructor.
Any property annotated with reductio.reflect.NoCheck will not be checked. A property's categories
are the union of the reduction.reflect.Category values of the enclosing class and itself. Default
check parameters can be overridden with the reduction.reflect.CheckParams annotation. A property
can have a name associated with it, that is first taken from its reductio.reflect.Name
annotation and if it doesn't exist, then the same annotation on the enclosing class.
*/
@SuppressWarnings({"PackageVisibleField", "MethodMayBeStatic"})
@CheckParams(minSuccessful = 500)
@Category("reductio.reflect demo")
public final class Reflect {
@Name("Integer Addition Commutes")
@Category("Passes for demo")
@CheckParams(minSuccessful = 20)
final Property p1 = property(arbInteger, arbInteger, new F2<Integer, Integer, Property>() {
public Property f(final Integer a, final Integer b) {
return prop(a + b == b + a);
}
});
@Name("Natural Integer Addition yields Natural Integer")
@Category("Fails for demo")
final Property p2 = property(arbIntegerBoundaries, arbIntegerBoundaries, shrinkInteger, shrinkInteger,
new F2<Integer, Integer, Property>() {
public Property f(final Integer a, final Integer b) {
return bool(a > 0 && b > 0).implies(a + b > 0);
}
});
@Category("Passes for demo")
final Property p3 = property(arbStringBuilder, new F<StringBuilder, Property>() {
public Property f(final StringBuilder sb) {
return prop(stringBuilderEqual.eq(sb, sb.reverse().reverse()));
}
});
@Category("Passes for demo")
final Property p4 = property(arbCharacter, new F<Character, Property>() {
public Property f(final Character c) {
return prop(stringBuilderEqual.eq(new StringBuilder().append(c), new StringBuilder().append(c).reverse()));
}
});
@Category("Passes for demo")
@CheckParams(minSuccessful = 750, maxSize = 40)
final Property p5 = property(arbStringBuilder, arbStringBuilder, new F2<StringBuilder, StringBuilder, Property>() {
public Property f(final StringBuilder x, final StringBuilder y) {
// copy the string builders before performing updates on x and y.
final StringBuilder xx = new StringBuilder(x);
final StringBuilder yy = new StringBuilder(y);
return prop(stringBuilderEqual.eq(x.append(y).reverse(), yy.reverse().append(xx.reverse())));
}
});
@Name("Triangulation")
@Category("Fails for demo")
final Property p6 = property(arbInteger, new F<Integer, Property>() {
public Property f(final Integer a) {
return prop(Triangulation.isPositive(a) == (a != 0 && !Triangulation.isNegative(a)));
}
});
@NoCheck
Property leave() {
throw error("this should not be executed");
}
@SuppressWarnings({"UnusedDeclaration"})
Property leave(final int i) {
throw error("this should not be executed");
}
/*
OK, passed 20 tests. (Integer Addition Commutes)
Falsified after 22 passed tests with arguments: [2147483646,13] (Natural Integer Addition yields Natural Integer)
OK, passed 500 tests. (p3)
OK, passed 500 tests. (p4)
OK, passed 750 tests. (p5)
Falsified after 0 passed tests with argument: 0 (Triangulation)
--------------------------------------------------------------------------------
OK, passed 20 tests. (Integer Addition Commutes)
OK, passed 500 tests. (p3)
OK, passed 500 tests. (p4)
OK, passed 750 tests. (p5)
--------------------------------------------------------------------------------
Falsified after 0 passed tests with arguments: [2,2147483646] (Natural Integer Addition yields Natural Integer)
Falsified after 0 passed tests with argument: 0 (Triangulation)
--------------------------------------------------------------------------------
OK, passed 20 tests. (Integer Addition Commutes)
Falsified after 0 passed tests with arguments: [2147483647,1] (Natural Integer Addition yields Natural Integer)
OK, passed 500 tests. (p3)
OK, passed 500 tests. (p4)
OK, passed 750 tests. (p5)
Falsified after 0 passed tests with arguments: 0 (Triangulation)
--------------------------------------------------------------------------------
*/
@SuppressWarnings("unchecked")
public static void main(final String[] args) {
printResults(check(list(Reflect.class)));
// execute only those in the given category
printResults(check(list(Reflect.class), "Passes for demo"));
printResults(check(list(Reflect.class), "Fails for demo"));
printResults(check(list(Reflect.class), "reductio.reflect demo"));
}
private static void printResults(final List<P2<String,CheckResult>> results) {
results.foreach(new Effect<P2<String, CheckResult>>() {
public void e(final P2<String, CheckResult> result) {
summary.print(result._2());
out.println(" (" + result._1() + ')');
}
});
out.println("--------------------------------------------------------------------------------");
}
}