/
ets_counter.erl
87 lines (74 loc) · 2.35 KB
/
ets_counter.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
%%% 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
-module(ets_counter).
-include_lib("proper/include/proper.hrl").
-compile(export_all).
-define(KEYS, lists:seq(1,10)).
ets_inc(Key, Inc) ->
case ets:lookup(counter, Key) of
[] ->
erlang:yield(),
ets:insert(counter, {Key,Inc}),
Inc;
[{Key,OldValue}] ->
NewValue = OldValue + Inc,
ets:insert(counter, {Key,NewValue}),
NewValue
end.
prop_ets_counter() ->
?FORALL(Commands, parallel_commands(?MODULE),
begin
set_up(),
{Seq,P,Result} = run_parallel_commands(?MODULE, Commands),
clean_up(),
?WHENFAIL(io:format("Seq: ~w\nPar: ~w\nRes: ~w\n",
[Seq, P, Result]),
Result =:= ok)
end).
set_up() ->
ets:new(counter, [public, named_table]).
clean_up() ->
catch ets:delete(counter).
key() ->
elements(?KEYS).
initial_state() -> [].
precondition(_S, _C) ->
true.
command(_S) ->
{call,?MODULE,ets_inc,[key(),non_neg_integer()]}.
postcondition(S, {call,_,ets_inc,[Key, Inc]}, Res) ->
case proplists:is_defined(Key, S) of
true ->
OldValue = proplists:get_value(Key, S),
Res =:= OldValue + Inc;
false ->
Res =:= Inc
end.
next_state(S, _Res, {call,_,ets_inc,[Key, Inc]}) ->
case proplists:is_defined(Key, S) of
true ->
OldValue = proplists:get_value(Key, S),
NewValue = OldValue + Inc,
[{Key,NewValue}|proplists:delete(Key, S)];
false ->
[{Key,Inc}|S]
end.