rustkas

rustkas

Journal: Property-Based Testing with PropEr, Erlang, and Elixir

To be a more productive reader when rereading a book, it is very convenient to create small rebar3 projects based on books’ samples and ideas. Here’s what I’ve already made.

Most Liked

rustkas

rustkas

Exercise 2

I add output and then run it to see it execute.
There is peace of my code with output:

increments([Head | Tail]) -> increments(Head, Tail).

increments(_, []) ->
    io:format("~n"),
    true;
increments(N, [Head | Tail]) when Head == N + 1 ->
    io:format("~p", [Head]),
    if length(Tail) > 0 -> io:format(", ");
       true -> io:format("|")
    end,
    increments(Head, Tail);
increments(_, _) -> false.
rustkas

rustkas

Erlang
(page 88)
code/CustomGenerators/erlang/pbt/test/prop_generators.erl

I would like to share inner step calculation while gathering values (by groups of 10). I was confused a litter how is the calculation going.

I have inserted an expression to print the parameters.

prop_collect2() ->
    ?FORALL(Bin, (binary()),
            (collect(to_range(10, byte_size(Bin)),
                     is_binary(Bin)))).

%%%%%%%%%%%%%%%
%%% Helpers %%%
%%%%%%%%%%%%%%%
to_range(M, N) ->
    Base = N div M,
    io:format("~2B = ~2B div ~B | {~2B, ~B} = {~2B * ~B, (~B + 1)*~B~n",
              [Base, N, M, Base * M, (Base + 1) * M, Base, M, Base, M]),
    {Base * M, (Base + 1) * M}.

Command:
rebar3 proper​ -p prop_collect2

Output (with the intermediate calculations):

 0 =   0 div 10 | { 0, 10} = { 0 * 10, (0 + 1)*10
. 0 =  1 div 10 | { 0, 10} = { 0 * 10, (0 + 1)*10
. 0 =  0 div 10 | { 0, 10} = { 0 * 10, (0 + 1)*10
. 0 =  0 div 10 | { 0, 10} = { 0 * 10, (0 + 1)*10
...
. 3 = 38 div 10 | {30, 40} = { 3 * 10, (3 + 1)*10
. 3 = 33 div 10 | {30, 40} = { 3 * 10, (3 + 1)*10
. 1 = 15 div 10 | {10, 20} = { 1 * 10, (1 + 1)*10
. 2 = 22 div 10 | {20, 30} = { 2 * 10, (2 + 1)*10

OK: Passed 100 test(s).

56.00% {0,10}
28.00% {10,20}
 9.00% {20,30}
 7.00% {30,40}
===>
1/1 properties passed
rustkas

rustkas

Erlang
(page 89)
code/CustomGenerators/erlang/pbt/test/prop_generators.erl

While running property based test prop_dupes you will get the same logging info.

prop_dupes() ->
 ?FORALL(KV, list({key(), val()}),
        begin
     M = maps:from_list(KV),
     _ = [maps:get(K, M) || {K, _V} <- KV], % crash if K's not in map
     collect(
       {dupes, to_range(5, length(KV) - length(lists:ukeysort(1,KV)))},
       true
     )
  end).


%%%%%%%%%%%%%%%
%%% Helpers %%%
%%%%%%%%%%%%%%%
key() -> integer().
val() -> term().

to_range(M, N) ->
    Base = N div M,
    io:format("~2B = ~2B div ~B | {~2B, ~2B} = {~2B * ~B, (~B + 1)*~B~n",
              [Base, N, M, Base * M, (Base + 1) * M, Base, M, Base, M]),
    {Base * M, (Base + 1) * M}.

Full code project

maps:from_list/1
maps:get/2
lists:ukeysort/2
length/1

Popular General Dev topics Top

AstonJ
A thread that every forum needs! Simply post a link to a track on YouTube (or SoundCloud or Vimeo amongst others!) on a separate line an...
New
Exadra37
Please tell us what is your preferred monitor setup for programming(not gaming) and why you have chosen it. Does your monitor have eye p...
New
foxtrottwist
Here’s our thread for the Keyboardio Atreus. It is a mechanical keyboard based on and a slight update of the original Atreus (Keyboardio ...
New
New
AstonJ
Please share your favourite Vim tips here :nerd_face:
New
AstonJ
Inspired by some of the comments in our https://forum.devtalk.com/t/your-vim-tips/4748 (in particular those by Mafinar and Hallski) …what...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
Maartz
Hi folks, I don’t know if I saw this here but, here’s a new programming language, called Roc Reminds me a bit of Elm and thus Haskell. ...
New
First poster: bot
Raspberry Pi security alarm — the basics. In November last year — I started building a DIY security alarm system, using a Raspberry Pi a...
New
First poster: bot
Large Language Models like ChatGPT say The Darnedest Things. The Errors They MakeWhy We Need to Document Them, and What We Have Decided ...
New

Other popular topics Top

AstonJ
If you are experiencing Rails console using 100% CPU on your dev machine, then updating your development and test gems might fix the issu...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
wmnnd
Here’s the story how one of the world’s first production deployments of LiveView came to be - and how trying to improve it almost caused ...
New
AstonJ
Saw this on TikTok of all places! :lol: Anyone heard of them before? Lite:
New
foxtrottwist
A few weeks ago I started using Warp a terminal written in rust. Though in it’s current state of development there are a few caveats (tab...
New
PragmaticBookshelf
Author Spotlight James Stanier @jstanier James Stanier, author of Effective Remote Work , discusses how to rethink the office as we e...
New
First poster: joeb
The File System Access API with Origin Private File System. WebKit supports new API that makes it possible for web apps to create, open,...
New
PragmaticBookshelf
Author Spotlight Rebecca Skinner @RebeccaSkinner Welcome to our latest author spotlight, where we sit down with Rebecca Skinner, auth...
New
PragmaticBookshelf
Author Spotlight: VM Brasseur @vmbrasseur We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
First poster: bot
zig/http.zig at 7cf2cbb33ef34c1d211135f56d30fe23b6cacd42 · ziglang/zig. General-purpose programming language and toolchain for maintaini...
New