Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 180 lines (148 sloc) 4.734 kB
d47b328 @EiriniArvaniti added copyright notice
EiriniArvaniti authored
1 %%% Copyright 2010-2011 Manolis Papadakis <manopapad@gmail.com>,
2 %%% Eirini Arvaniti <eirinibob@gmail.com>
3 %%% and Kostis Sagonas <kostis@cs.ntua.gr>
4 %%%
5 %%% This file is part of PropEr.
6 %%%
7 %%% PropEr is free software: you can redistribute it and/or modify
8 %%% it under the terms of the GNU General Public License as published by
9 %%% the Free Software Foundation, either version 3 of the License, or
10 %%% (at your option) any later version.
11 %%%
12 %%% PropEr is distributed in the hope that it will be useful,
13 %%% but WITHOUT ANY WARRANTY; without even the implied warranty of
14 %%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 %%% GNU General Public License for more details.
16 %%%
17 %%% You should have received a copy of the GNU General Public License
18 %%% along with PropEr. If not, see <http://www.gnu.org/licenses/>.
19
b36c594 @manopapad Remove e-mail addresses from EDoc output
authored
20 %%% @copyright 2010-2011 Manolis Papadakis, Eirini Arvaniti and Kostis Sagonas
d47b328 @EiriniArvaniti added copyright notice
EiriniArvaniti authored
21 %%% @version {@version}
b36c594 @manopapad Remove e-mail addresses from EDoc output
authored
22 %%% @author Eirini Arvaniti
d47b328 @EiriniArvaniti added copyright notice
EiriniArvaniti authored
23 %%% @doc Tests for fsm transition targets
24
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
25 -module(numbers_fsm).
26 -compile(export_all).
27
28 -include_lib("proper/include/proper.hrl").
29
30 -define(STATES, [zero, one, two, three, four]).
31 -define(KEYS, [a,b,c,d,e,f]).
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
32 -define(LOOKUP, [{zero,0}, {one,1}, {two,2}, {three,3}, {four,4}]).
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
33
34
35 %%% Fsm callbacks
36
37 zero(S) ->
38 idle_transition() ++
39 [{four, {call,?MODULE,dec,[]}},
40 {one, {call,?MODULE,inc,[]}},
41 {history, {call,?MODULE,insert,[key()]}},
42 {history, {call,?MODULE,delete,[key(S)]}}].
43
44 one(S) ->
45 idle_transition() ++
46 [{zero, {call,?MODULE,dec,[]}},
47 {two, {call,?MODULE,inc,[]}},
48 {history, {call,?MODULE,insert,[key()]}},
49 {history, {call,?MODULE,delete,[key(S)]}}].
50
51 two(S) ->
52 idle_transition() ++
53 [{one, {call,?MODULE,dec,[]}},
54 {three, {call,?MODULE,inc,[]}},
55 {history, {call,?MODULE,insert,[key()]}},
56 {history, {call,?MODULE,delete,[key(S)]}}].
57
58 three(S) ->
59 idle_transition() ++
60 [{two, {call,?MODULE,dec,[]}},
61 {four, {call,?MODULE,inc,[]}},
62 {history, {call,?MODULE,insert,[key()]}},
63 {history, {call,?MODULE,delete,[key(S)]}}].
64
65 four(S) ->
66 idle_transition() ++
67 [{three, {call,?MODULE,dec,[]}},
68 {zero, {call,?MODULE,inc,[]}},
69 {history, {call,?MODULE,insert,[key()]}},
70 {history, {call,?MODULE,delete,[key(S)]}}].
71
72 num(N, _, _, _S) ->
73 idle_transition() ++
74 [{{num,N+1,dummy,dummy}, {call,?MODULE,inc,[]}} || N < 4] ++
75 [{{num,N-1,dummy,dummy}, {call,?MODULE,dec,[]}} || N > 0] ++
76 [{{num,0,dummy,dummy}, {call,?MODULE,inc,[]}} || N =:= 4] ++
77 [{{num,4,dummy,dummy}, {call,?MODULE,dec,[]}} || N =:= 0].
78
79 idle_transition() ->
80 [{history, {call,?MODULE,idle,[]}}].
81
82 initial_state() -> zero.
83 initial_state_data() -> [].
84
2e26ece @EiriniArvaniti clean-ups in fsm examples and tests
EiriniArvaniti authored
85 precondition(_, _, S, {call,_,delete,[Key]}) ->
86 lists:member(Key, S);
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
87 precondition(zero, _To , _S, _Call) ->
88 true;
89 precondition(_From, _To, S, {call,_,dec,_}) ->
90 S =/= [];
91 precondition(_From, _To, S, {call,_,inc,_}) ->
92 S =/= [];
93 precondition(_, _, _, _) ->
94 true.
95
96 next_state_data(_, _, S, _, {call,_,insert,[Key]}) ->
97 [Key|S];
98 next_state_data(_, _, S, _, {call,_,delete,[Key]}) ->
99 lists:delete(Key, S);
100 next_state_data(_, _, S, _, _) ->
101 S.
102
103 postcondition(_, _, _, _, _) ->
104 true.
105
106 weight(_, _, {call,_,insert,_}) -> 2;
107 weight(_, _, _) -> 1.
108
109
110 %%% Generators
111
112 key() ->
113 elements(?KEYS).
114
115 key(S) ->
116 elements(S).
117
118 action() ->
119 oneof([idle, inc, dec, foo]).
120
121 call() ->
122 {call,?MODULE,action(),[]}.
123
124 mod(X, Y) when X > 0 -> X rem Y;
125 mod(X, Y) when X < 0 -> Y + X rem Y;
126 mod(0, _Y) -> 0.
127
128 mod_add(X, Y) -> mod(X+Y, 5).
129 mod_sub(X, Y) -> mod(X-Y, 5).
130
131 inc() -> ok.
132 idle() -> ok.
133 dec() -> ok.
134 insert(_) -> ok.
135 delete(_) -> ok.
136
137
138 %%% Properties
139
140 prop_target_states_atom() ->
141 ?FORALL([From,Call], [elements(?STATES),call()],
142 begin
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
143 Res = proper_fsm:target_states(?MODULE, From, [], Call),
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
144 {call,_,Action,[]} = Call,
145 Target = case Action of
146 idle ->
147 [history];
148 inc ->
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
149 Sum = mod_add(proplists:get_value(From, ?LOOKUP), 1),
150 [element(1, lists:keyfind(Sum, 2, ?LOOKUP))];
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
151 dec ->
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
152 Diff = mod_sub(proplists:get_value(From, ?LOOKUP), 1),
153 [element(1, lists:keyfind(Diff, 2, ?LOOKUP))];
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
154 foo ->
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
155 []
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
156 end,
157 collect({From,Action}, Target =:= Res)
158 end).
159
160 prop_target_states_tuple() ->
161 ?FORALL([From,Call], [{num,range(0,4),dummy,dummy},call()],
162 begin
163 {num,N,_,_} = From,
164 Res = proper_fsm:target_states(?MODULE, From, dummy, Call),
165 {call,_,Action,[]} = Call,
166 Target = case Action of
167 idle ->
168 [history];
169 inc ->
170 Sum = mod_add(N, 1),
171 [{num,Sum,dummy,dummy}];
172 dec ->
173 Diff = mod_sub(N, 1),
174 [{num,Diff,dummy,dummy}];
175 foo ->
ba93bfc @EiriniArvaniti more clean-ups
EiriniArvaniti authored
176 []
0cc4bea @EiriniArvaniti first unit tests for proper_fsm
EiriniArvaniti authored
177 end,
178 collect({From,Action}, Target =:= Res)
179 end).
Something went wrong with that request. Please try again.