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

Where Next?

Popular Community topics Top

Tommy
So I have enough money to last a year. Realistically I’m still going to have to work part time painting. I’m so done with it though! I h...
New
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
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
ohm
I would love to begin a book club with Mike Amundsen’s (@mamund) book Design and Build Great Web APIs. It seems that building new syste...
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
AstonJ
With Tailwind now the default CSS framework shipped with Phoenix we thought it would be nice to run this book club on the Elixir Forum. ...
New
AstonJ
With AI set to play a big role in our industry Elixir users are lucky to have Nx, so we’re running our Nx related book club on Genetic Al...
New
AstonJ
With Phoenix and LiveView having recently had a fairly major release, and Programming Phoenix LiveView being updated too, we thought it w...
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
alvinkatojr
https://fs.blog/mental-models/ I’ve been reading Farnham Street for a while, and this topic is the recommended starting point for new re...
New

Other popular topics Top

AstonJ
What chair do you have while working… and why? Is there a ‘best’ type of chair or working position for developers?
New
DevotionGeo
I know that these benchmarks might not be the exact picture of real-world scenario, but still I expect a Rust web framework performing a ...
New
siddhant3030
I’m thinking of buying a monitor that I can rotate to use as a vertical monitor? Also, I want to know if someone is using it for program...
New
New
Margaret
Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New
PragmaticBookshelf
“A Mystical Experience” Hero’s Journey with Paolo Perrotta @nusco Ever wonder how authoring books compares to writing articles?...
New
New
gagan7995
API 4 Path: /user/following/ Method: GET Description: Returns the list of all names of people whom the user follows Response [ { ...
New
AstonJ
If you’re getting errors like this: psql: error: connection to server on socket “/tmp/.s.PGSQL.5432” failed: No such file or directory ...
New
AnfaengerAlex
Hello, I’m a beginner in Android development and I’m facing an issue with my project setup. In my build.gradle.kts file, I have the foll...
New