Skip to content
This repository
branch: parse_transform
Fetching contributors…

Cannot retrieve contributors at this time

file 179 lines (148 sloc) 4.734 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 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
%%% Copyright 2010-2011 Manolis Papadakis <manopapad@gmail.com>,
%%% Eirini Arvaniti <eirinibob@gmail.com>
%%% and Kostis Sagonas <kostis@cs.ntua.gr>
%%%
%%% This file is part of PropEr.
%%%
%%% PropEr is free software: you can redistribute it and/or modify
%%% it under the terms of the GNU General Public License as published by
%%% the Free Software Foundation, either version 3 of the License, or
%%% (at your option) any later version.
%%%
%%% PropEr is distributed in the hope that it will be useful,
%%% but WITHOUT ANY WARRANTY; without even the implied warranty of
%%% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
%%% GNU General Public License for more details.
%%%
%%% You should have received a copy of the GNU General Public License
%%% along with PropEr. If not, see <http://www.gnu.org/licenses/>.

%%% @copyright 2010-2011 Manolis Papadakis, Eirini Arvaniti and Kostis Sagonas
%%% @version {@version}
%%% @author Eirini Arvaniti
%%% @doc Tests for fsm transition targets

-module(numbers_fsm).
-compile(export_all).

-include_lib("proper/include/proper.hrl").

-define(STATES, [zero, one, two, three, four]).
-define(KEYS, [a,b,c,d,e,f]).
-define(LOOKUP, [{zero,0}, {one,1}, {two,2}, {three,3}, {four,4}]).


%%% Fsm callbacks

zero(S) ->
    idle_transition() ++
[{four, {call,?MODULE,dec,[]}},
{one, {call,?MODULE,inc,[]}},
{history, {call,?MODULE,insert,[key()]}},
{history, {call,?MODULE,delete,[key(S)]}}].

one(S) ->
    idle_transition() ++
[{zero, {call,?MODULE,dec,[]}},
{two, {call,?MODULE,inc,[]}},
{history, {call,?MODULE,insert,[key()]}},
{history, {call,?MODULE,delete,[key(S)]}}].

two(S) ->
    idle_transition() ++
[{one, {call,?MODULE,dec,[]}},
{three, {call,?MODULE,inc,[]}},
{history, {call,?MODULE,insert,[key()]}},
{history, {call,?MODULE,delete,[key(S)]}}].

three(S) ->
    idle_transition() ++
[{two, {call,?MODULE,dec,[]}},
{four, {call,?MODULE,inc,[]}},
{history, {call,?MODULE,insert,[key()]}},
{history, {call,?MODULE,delete,[key(S)]}}].

four(S) ->
     idle_transition() ++
[{three, {call,?MODULE,dec,[]}},
{zero, {call,?MODULE,inc,[]}},
{history, {call,?MODULE,insert,[key()]}},
{history, {call,?MODULE,delete,[key(S)]}}].

num(N, _, _, _S) ->
    idle_transition() ++
[{{num,N+1,dummy,dummy}, {call,?MODULE,inc,[]}} || N < 4] ++
[{{num,N-1,dummy,dummy}, {call,?MODULE,dec,[]}} || N > 0] ++
[{{num,0,dummy,dummy}, {call,?MODULE,inc,[]}} || N =:= 4] ++
[{{num,4,dummy,dummy}, {call,?MODULE,dec,[]}} || N =:= 0].

idle_transition() ->
    [{history, {call,?MODULE,idle,[]}}].

initial_state() -> zero.
initial_state_data() -> [].

precondition(_, _, S, {call,_,delete,[Key]}) ->
    lists:member(Key, S);
precondition(zero, _To , _S, _Call) ->
    true;
precondition(_From, _To, S, {call,_,dec,_}) ->
    S =/= [];
precondition(_From, _To, S, {call,_,inc,_}) ->
    S =/= [];
precondition(_, _, _, _) ->
    true.

next_state_data(_, _, S, _, {call,_,insert,[Key]}) ->
    [Key|S];
next_state_data(_, _, S, _, {call,_,delete,[Key]}) ->
    lists:delete(Key, S);
next_state_data(_, _, S, _, _) ->
    S.

postcondition(_, _, _, _, _) ->
    true.

weight(_, _, {call,_,insert,_}) -> 2;
weight(_, _, _) -> 1.


%%% Generators

key() ->
    elements(?KEYS).

key(S) ->
    elements(S).

action() ->
    oneof([idle, inc, dec, foo]).

call() ->
    {call,?MODULE,action(),[]}.

mod(X, Y) when X > 0 -> X rem Y;
mod(X, Y) when X < 0 -> Y + X rem Y;
mod(0, _Y) -> 0.

mod_add(X, Y) -> mod(X+Y, 5).
mod_sub(X, Y) -> mod(X-Y, 5).

inc() -> ok.
idle() -> ok.
dec() -> ok.
insert(_) -> ok.
delete(_) -> ok.


%%% Properties

prop_target_states_atom() ->
    ?FORALL([From,Call], [elements(?STATES),call()],
begin
Res = proper_fsm:target_states(?MODULE, From, [], Call),
{call,_,Action,[]} = Call,
Target = case Action of
idle ->
[history];
inc ->
Sum = mod_add(proplists:get_value(From, ?LOOKUP), 1),
[element(1, lists:keyfind(Sum, 2, ?LOOKUP))];
dec ->
Diff = mod_sub(proplists:get_value(From, ?LOOKUP), 1),
[element(1, lists:keyfind(Diff, 2, ?LOOKUP))];
foo ->
[]
end,
collect({From,Action}, Target =:= Res)
end).

prop_target_states_tuple() ->
    ?FORALL([From,Call], [{num,range(0,4),dummy,dummy},call()],
begin
{num,N,_,_} = From,
Res = proper_fsm:target_states(?MODULE, From, dummy, Call),
{call,_,Action,[]} = Call,
Target = case Action of
idle ->
[history];
inc ->
Sum = mod_add(N, 1),
[{num,Sum,dummy,dummy}];
dec ->
Diff = mod_sub(N, 1),
[{num,Diff,dummy,dummy}];
foo ->
[]
end,
collect({From,Action}, Target =:= Res)
end).
Something went wrong with that request. Please try again.