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 Community topics Top

mafinar
Concurrent Data Processing in Elixir is now content complete and I finally found the time I’ve been looking for to dedicate behind readin...
New
mafinar
I am going to dump my thoughts, methods, codes, experiences and rants while learning OCaml into this thread. This is probably the 5th or...
New
RobertKielty
My overall initial first impressions of this book are very good. I will document my local spacemacs setup to as I work through the book.
New
mafinar
TL;DR I am reading “Domain Modeling Made Functional” and discussing and keeping a journal of what I learned from it, any co-readers welco...
New
TwistingTwists
I have read first chapter. Will add my notes / code tries / self exploration as I go along! Thank you @AstonJ for encouraging to start ...
New
RomanTurner
Agile Web Development with Rails 6 Chapter 11. Task F Currently reading and working through AWDR6 by Sam Ruby, David Bryant Copeland, a...
New
ggarnier
In Aborting Multiple Fetch Requests with One Signal section, the code in abort/abort_ex09.js doesn’t show the downloaded images until Pro...
New
rgerardi
Hello all. Creating this space here for general discussion and chat about Powerful Command-Line Applications In Go In particular, we ca...
New
PragmaticBookshelf
When the pandemic, heart disease, and personal tragedy threatened to steal everything the Tates spent years building, they found hope, he...
New
TomMahon
How did a sleepy valley become the epicenter of the technological world as we know it? In the 40th Anniversary Edition of my book, “Charg...
New

Other popular topics Top

brentjanderson
Bought the Moonlander mechanical keyboard. Cherry Brown MX switches. Arms and wrists have been hurting enough that it’s time I did someth...
New
AstonJ
You might be thinking we should just ask who’s not using VSCode :joy: however there are some new additions in the space that might give V...
New
AstonJ
We have a thread about the keyboards we have, but what about nice keyboards we come across that we want? If you have seen any that look n...
New
AstonJ
I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me. I think the Planck and the Preonic (o...
New
AstonJ
This looks like a stunning keycap set :orange_heart: A LEGENDARY KEYBOARD LIVES ON When you bought an Apple Macintosh computer in the e...
New
AstonJ
Do the test and post your score :nerd_face: :keyboard: If possible, please add info such as the keyboard you’re using, the layout (Qw...
New
AstonJ
Seems like a lot of people caught it - just wondered whether any of you did? As far as I know I didn’t, but it wouldn’t surprise me if I...
New
New
PragmaticBookshelf
Author Spotlight: Peter Ullrich @PJUllrich Data is at the core of every business, but it is useless if nobody can access and analyze ...
New
AstonJ
This is cool! DEEPSEEK-V3 ON M4 MAC: BLAZING FAST INFERENCE ON APPLE SILICON We just witnessed something incredible: the largest open-s...
New