aboutsummaryrefslogtreecommitdiff
path: root/aoc2023/build/packages/pqueue/test
diff options
context:
space:
mode:
authorHJ <thechairman@thechairman.info>2024-02-03 15:09:54 -0500
committerHJ <thechairman@thechairman.info>2024-02-03 15:09:54 -0500
commit96a3c5c179d8d3fff24eb2953e45f8dd15e2714c (patch)
tree0a67bc0cfeabe51740bb049c61f16f1ac3bdd4ff /aoc2023/build/packages/pqueue/test
parent547fe03cf43105f46160e2dd9afff21637eaaf47 (diff)
downloadgleam_aoc-96a3c5c179d8d3fff24eb2953e45f8dd15e2714c.tar.gz
gleam_aoc-96a3c5c179d8d3fff24eb2953e45f8dd15e2714c.zip
cleanup
Diffstat (limited to 'aoc2023/build/packages/pqueue/test')
-rw-r--r--aoc2023/build/packages/pqueue/test/pqueue_proper.erl156
-rw-r--r--aoc2023/build/packages/pqueue/test/queue_srv.erl183
2 files changed, 339 insertions, 0 deletions
diff --git a/aoc2023/build/packages/pqueue/test/pqueue_proper.erl b/aoc2023/build/packages/pqueue/test/pqueue_proper.erl
new file mode 100644
index 0000000..6702960
--- /dev/null
+++ b/aoc2023/build/packages/pqueue/test/pqueue_proper.erl
@@ -0,0 +1,156 @@
+-module(pqueue_proper).
+-ifdef(TEST).
+-include_lib("proper/include/proper.hrl").
+
+-behaviour(proper_statem).
+
+-export([qc_pq/0, qc_pq2/0, qc_pq3/0, qc_pq4/0, correct/1]).
+
+-export([command/1, initial_state/0, next_state/3, postcondition/3,
+ precondition/2]).
+
+-type value() :: integer().
+-record(state, { in_queue :: [{value(), term()}] }).
+-define(SERVER, queue_srv).
+
+priority() ->
+ integer(-20, 20).
+
+%% Selects priorities we have added
+priority(InQ) ->
+ elements([P || {P, _} <- InQ]).
+
+value() ->
+ integer().
+
+initial_state() ->
+ #state { in_queue = [] }.
+
+command(#state { in_queue = InQ }) ->
+ oneof([{call, ?SERVER, in, [value()]},
+ {call, ?SERVER, in, [value(), priority()]},
+ {call, ?SERVER, is_empty, []},
+ {call, ?SERVER, is_queue, []},
+ {call, ?SERVER, len, []},
+ {call, ?SERVER, out, []}] ++
+ [{call, ?SERVER, out, [priority(InQ)]} || InQ =/= []] ++
+ [{call, ?SERVER, pout, []},
+ {call, ?SERVER, to_list, []}]).
+
+next_state(#state { in_queue = InQ } = S, _V, {call, _, out, []}) ->
+ S#state { in_queue = listq_rem(InQ) };
+next_state(#state { in_queue = InQ } = S, _V, {call, _, out, [Prio]}) ->
+ S#state { in_queue = listq_rem(InQ, Prio) };
+next_state(#state { in_queue = InQ } = S, _V, {call, _, pout, _}) ->
+ S#state { in_queue = listq_rem(InQ) };
+next_state(S, _V, {call, _, to_list, _}) -> S;
+next_state(S, _V, {call, _, is_queue, _}) -> S;
+next_state(S, _V, {call, _, is_empty, _}) -> S;
+next_state(S, _V, {call, _, len, _}) -> S;
+next_state(#state { in_queue = InQ } = S, _V, {call, _, in, [Value, Prio]}) ->
+ S#state { in_queue = listq_insert({Prio, Value}, InQ) };
+next_state(#state { in_queue = InQ } = S, _V, {call, _, in, [Value]}) ->
+ S#state { in_queue = listq_insert({0, Value}, InQ) }.
+
+precondition(_S, _Call) ->
+ true. % No limitation on the things we can call at all.
+
+postcondition(#state { in_queue = InQ }, {call, _, out, [Prio]}, R) ->
+ R == listq_prio_peek(InQ, Prio);
+postcondition(#state { in_queue = InQ }, {call, _, pout, _}, R) ->
+ R == listq_ppeek(InQ);
+postcondition(#state { in_queue = InQ }, {call, _, out, _}, R) ->
+ R == listq_peek(InQ);
+postcondition(S, {call, _, to_list, _}, R) ->
+ R == listq_to_list(S#state.in_queue);
+postcondition(S, {call, _, len, _}, L) ->
+ L == listq_length(S#state.in_queue);
+postcondition(_S, {call, _, is_queue, _}, true) -> true;
+postcondition(S, {call, _, is_empty, _}, Res) ->
+ Res == (S#state.in_queue == []);
+postcondition(_S, {call, _, in, _}, _) ->
+ true;
+postcondition(_, _, _) ->
+ false.
+
+correct(M) ->
+ ?FORALL(Cmds, commands(?MODULE),
+ ?TRAPEXIT(
+ begin
+ ?SERVER:start_link(M),
+ {History,State,Result} = run_commands(?MODULE, Cmds),
+ ?SERVER:stop(),
+ ?WHENFAIL(io:format("History: ~w\nState: ~w\nResult: ~w\n",
+ [History,State,Result]),
+ aggregate(command_names(Cmds), Result =:= ok))
+ end)).
+
+qc_opts() ->
+ [{numtests, 10000}].
+
+qc_pq() ->
+ proper:quickcheck(pqueue_proper:correct(pqueue), qc_opts()).
+
+qc_pq2() ->
+ proper:quickcheck(pqueue_proper:correct(pqueue2), qc_opts()).
+
+qc_pq3() ->
+ proper:quickcheck(pqueue_proper:correct(pqueue3), qc_opts()).
+
+qc_pq4() ->
+ proper:quickcheck(pqueue_proper:correct(pqueue4), qc_opts()).
+
+%% ----------------------------------------------------------------------
+
+%% A listq is a sorted list of priorities
+listq_insert({P, V}, []) ->
+ [{P, [V]}];
+listq_insert({P, V}, [{P1, _} | _] = LQ) when P < P1 ->
+ [{P, [V]} | LQ];
+listq_insert({P, V}, [{P1, Vs} | Next]) when P == P1 ->
+ [{P, Vs ++ [V]} | Next];
+listq_insert({P, V}, [{P1, Vs} | Next]) when P > P1 ->
+ [{P1, Vs} | listq_insert({P, V}, Next)].
+
+listq_to_list(L) ->
+ lists:concat(
+ [ Vals || {_Prio, Vals} <- L]).
+
+listq_length(L) ->
+ lists:sum(
+ [ length(Vs) || {_Prio, Vs} <- L]).
+
+listq_rem([]) ->
+ [];
+listq_rem([{_P, [_V]} | Next]) ->
+ Next;
+listq_rem([{P, [_V1 | Vs]} | Next]) ->
+ [{P, Vs} | Next].
+
+listq_rem([], _P) ->
+ [];
+listq_rem([{P, [_]} | Next], P) ->
+ Next;
+listq_rem([{P, [_ | Vs]} | Next], P) ->
+ [{P, Vs} | Next];
+listq_rem([{P1, Vs} | Next], P) ->
+ [{P1, Vs} | listq_rem(Next, P)].
+
+listq_peek([]) ->
+ empty;
+listq_peek([{_P, [V | _]} | _]) ->
+ {value, V}.
+
+listq_prio_peek([{P, [V | _]} | _], P) ->
+ {value, V};
+listq_prio_peek([{_P1, _} | Next], P) ->
+ listq_prio_peek(Next, P);
+listq_prio_peek([], _P) ->
+ empty.
+
+listq_ppeek([]) ->
+ empty;
+listq_ppeek([{P, [V | _]} | _]) ->
+ {value, V, P}.
+
+-endif.
diff --git a/aoc2023/build/packages/pqueue/test/queue_srv.erl b/aoc2023/build/packages/pqueue/test/queue_srv.erl
new file mode 100644
index 0000000..7fcb0a1
--- /dev/null
+++ b/aoc2023/build/packages/pqueue/test/queue_srv.erl
@@ -0,0 +1,183 @@
+%%%-------------------------------------------------------------------
+%%% @author Jesper Louis andersen <>
+%%% @copyright (C) 2011, Jesper Louis andersen
+%%% @doc
+%%%
+%%% @end
+%%% Created : 11 Nov 2011 by Jesper Louis andersen <>
+%%%-------------------------------------------------------------------
+-module(queue_srv).
+
+-behaviour(gen_server).
+
+%% API
+-export([start_link/1, stop/0, len/0, in/1, in/2, is_empty/0,
+ out/0, out/1, pout/0,
+ is_queue/0, to_list/0]).
+
+%% gen_server callbacks
+-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
+ terminate/2, code_change/3]).
+
+-define(SERVER, ?MODULE).
+
+-record(state, { mod, q }).
+
+%%%===================================================================
+%%% API
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @doc
+%% Starts the server
+%%
+%% @spec start_link(Mod) -> {ok, Pid} | ignore | {error, Error}
+%% @end
+%%--------------------------------------------------------------------
+start_link(Mod) ->
+ gen_server:start_link({local, ?SERVER}, ?MODULE, [Mod], []).
+
+stop() ->
+ gen_server:stop(?SERVER).
+
+call(M) ->
+ gen_server:call(?SERVER, M, infinity).
+
+in(I) ->
+ call({in, I}).
+
+in(I, P) ->
+ call({in, I, P}).
+
+len() ->
+ call(len).
+
+is_empty() ->
+ call(is_empty).
+
+is_queue() ->
+ call(is_queue).
+
+to_list() ->
+ call(to_list).
+
+out() ->
+ call(out).
+
+out(P) ->
+ call({out, P}).
+
+pout() ->
+ call(pout).
+
+%%%===================================================================
+%%% gen_server callbacks
+%%%===================================================================
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Initializes the server
+%%
+%% @spec init(Args) -> {ok, State} |
+%% {ok, State, Timeout} |
+%% ignore |
+%% {stop, Reason}
+%% @end
+%%--------------------------------------------------------------------
+init([Mod]) ->
+ {ok, #state{ mod = Mod,
+ q = Mod:new() }}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling call messages
+%%
+%% @spec handle_call(Request, From, State) ->
+%% {reply, Reply, State} |
+%% {reply, Reply, State, Timeout} |
+%% {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, Reply, State} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_call({in, Item}, _F, #state { q = Q, mod = M } = S) ->
+ NQ = M:in(Item, Q),
+ {reply, ok, S#state { q = NQ }};
+handle_call({in, Item, Prio}, _F, #state { q = Q, mod = M } = S) ->
+ NQ = M:in(Item, Prio, Q),
+ {reply, ok, S#state { q = NQ }};
+handle_call({out, P}, _F, #state { q = Q, mod = M } = S) ->
+ {R, NQ} = M:out(P, Q),
+ {reply, R, S#state { q = NQ }};
+handle_call(Ty, _F, #state { q = Q, mod = M } = S) when Ty == out;
+ Ty == pout ->
+ {R, NQ} = M:Ty(Q),
+ {reply, R, S#state { q = NQ }};
+handle_call(Ty, _F, #state { q = Q, mod = M } = S) when Ty == is_queue;
+ Ty == is_empty;
+ Ty == len;
+ Ty == to_list ->
+ R = M:Ty(Q),
+ {reply, R, S};
+handle_call(Req, From, State) ->
+ error_logger:info_report([{handle_call, Req, From, State}]),
+ Reply = ok,
+ {reply, Reply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling cast messages
+%%
+%% @spec handle_cast(Msg, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_cast(_Msg, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Handling all non call/cast messages
+%%
+%% @spec handle_info(Info, State) -> {noreply, State} |
+%% {noreply, State, Timeout} |
+%% {stop, Reason, State}
+%% @end
+%%--------------------------------------------------------------------
+handle_info(_Info, State) ->
+ {noreply, State}.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% This function is called by a gen_server when it is about to
+%% terminate. It should be the opposite of Module:init/1 and do any
+%% necessary cleaning up. When it returns, the gen_server terminates
+%% with Reason. The return value is ignored.
+%%
+%% @spec terminate(Reason, State) -> void()
+%% @end
+%%--------------------------------------------------------------------
+terminate(_Reason, _State) ->
+ ok.
+
+%%--------------------------------------------------------------------
+%% @private
+%% @doc
+%% Convert process state when code is changed
+%%
+%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
+%% @end
+%%--------------------------------------------------------------------
+code_change(_OldVsn, State, _Extra) ->
+ {ok, State}.
+
+%%%===================================================================
+%%% Internal functions
+%%%===================================================================