-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsets_eqc.erl
executable file
·146 lines (111 loc) · 4.92 KB
/
sets_eqc.erl
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
%%% File : sets_eqc.erl
%%% Author : Thomas Arts <[email protected]>
%%% Description : QuickCheck properties for sets.erl
%%% Based on "Testing Data Structures with QuickCheck"
%%% Created : 24 Mar 2010 by Thomas Arts
-module(sets_eqc).
-include_lib("eqc/include/eqc.hrl").
-compile(export_all).
%% Create a generator for the opaque type "set". The generator will generate
%% symbolic calls which when evaluated computes a set. Each symbolic call has
%% the form {call, Module, Function, Arguments} and are evaluated using the
%% function eval/1.
%% To avoid generating infinite symbolic representations we pass the size
%% parameter to the generator and use it to make sure we stop eventually.
set(G) ->
?SIZED(Size,well_defined(set(Size,G))).
set(0,G) ->
oneof([{call,sets,new,[]},
{call,sets,from_list,[list(G)]}]);
set(N,G) ->
frequency(
[{5,set(0,G)},
{3, ?LAZY(?LETSHRINK([Set],[set(N-1,G)],
{call,sets,add_element,[G, Set]}))},
{1, ?LAZY(?LETSHRINK([Set],[set(N-1,G)],
{call,sets,del_element,[G, Set]}))},
{1, ?LAZY(?LETSHRINK([Set1,Set2],[set(N div 2,G),set(N div 2,G)],
{call,sets,union,[Set1, Set2]}))},
{1, ?LAZY(?LETSHRINK(Sets,list(set(N div 3,G)),
{call,sets,union,[Sets]}))},
{1, ?LAZY(?LETSHRINK([Set1,Set2],[set(N div 2,G),set(N div 2,G)],
{call,sets,intersection,[Set1, Set2]}))},
{1, ?LAZY(?LETSHRINK(Sets,?LET(L,nat(),vector(L+1,set(N div (L+1),G))),
{call,sets,intersection,[Sets]}))},
{1, ?LAZY(?LETSHRINK([Set1,Set2],[set(N div 2,G),set(N div 2,G)],
{call,sets,subtract,[Set1, Set2]}))},
{1, ?LAZY(?LETSHRINK([Set],[set(N div 2,G)],
{call,sets,filter,[function1(bool()), Set]}))}]).
%% The next step is to define a model interpretation, i.e. a simplified,
%% obviously correct implementation of the data type. In this case we use
%% usorted lists.
model(S) ->
lists:sort(sets:to_list(S)).
%% Define the set operations on the model.
madd_element(E,S) ->
lists:usort([E|S]).
mdel_element(E,S) ->
S -- [E].
msize(S) ->
length(S).
mis_element(E,S) ->
lists:member(E,S).
munion(Ss) ->
lists:usort(lists:append(Ss)).
mintersection(Sets) ->
[ E || E <- lists:usort(lists:append(Sets)),
lists:all(fun(Set) -> lists:member(E,Set) end, Sets)].
mis_disjoint(S1,S2) ->
mintersection([S1,S2]) == [].
mfilter(Pred,S) ->
[ E || E <- S, Pred(E)].
%% Define one property for each operation. We parameterize the properties on
%% the generator for the elements. To make it easy to run the properties we
%% also define special versions that use integers.
%% Each property have the same basic form: we check that a given operation
%% on sets has the same behaviour as the corresponding model operation.
prop_create() -> prop_create(int()).
prop_create(G) ->
?FORALL(Set,set(G),
sets:is_set(eval(Set))).
prop_add_element() -> prop_add_element(int()).
prop_add_element(G) ->
?FORALL({E,Set},{G,set(G)},
model(sets:add_element(E,eval(Set))) == madd_element(E,model(eval(Set)))).
prop_del_element() -> prop_del_element(int()).
prop_del_element(G) ->
?FORALL({E,Set},{G,set(G)},
model(sets:del_element(E,eval(Set))) == mdel_element(E,model(eval(Set)))).
prop_union2() -> prop_union2(int()).
prop_union2(G) ->
?FORALL({Set1,Set2},{set(G),set(G)},
model(sets:union(eval(Set1),eval(Set2))) == munion([model(eval(Set1)),model(eval(Set2))])).
prop_union() -> prop_union(int()).
prop_union(G) ->
?FORALL(Sets,list(set(G)),
model(sets:union(eval(Sets))) == munion([model(Set) || Set<-eval(Sets)])).
prop_intersection2() -> prop_intersection2(int()).
prop_intersection2(G) ->
?FORALL({Set1,Set2},{set(G),set(G)},
model(sets:intersection(eval(Set1),eval(Set2))) ==
mintersection([model(eval(Set1)),model(eval(Set2))])).
prop_intersection() -> prop_intersection(int()).
prop_intersection(G) ->
?FORALL(Sets,eqc_gen:non_empty(list(set(G))),
model(sets:intersection(eval(Sets))) == mintersection([model(Set) || Set<-eval(Sets)])).
prop_size() -> prop_size(int()).
prop_size(G) ->
?FORALL(Set,set(G),
sets:size(eval(Set)) == msize(model(eval(Set)))).
prop_is_element() -> prop_is_element(int()).
prop_is_element(G) ->
?FORALL({E,Set},{G,set(G)},
sets:is_element(E,eval(Set)) == mis_element(E,model(eval(Set)))).
prop_is_disjoint() -> prop_is_disjoint(int()).
prop_is_disjoint(G) ->
?FORALL({Set1,Set2},{set(G),set(G)},
sets:is_disjoint(eval(Set1),eval(Set2)) == mis_disjoint(model(eval(Set1)),model(eval(Set2)))).
prop_filter() -> prop_filter(int()).
prop_filter(G) ->
?FORALL({Pred,Set},{function1(bool()),set(G)},
model(sets:filter(Pred,eval(Set))) == mfilter(Pred,model(eval(Set)))).