smedegaard

smedegaard

PropCheck "list of minimum two elements"-generator

I’m trying to write property based tests for a Reverse Polish Notation calculator.

I’ve implemented the RPN such that it will return an error tuple if you try to push a operator to the stack when the stack has less than two numbers.

defmodule ReversePolishNotation do
  @moduledoc """
  Documentation for ReversePolishNotation.
  """

  def push(list, number) when number |> is_number do
    {:ok, [number | list]}
  end

  @operators [:+, :/, :-, :*]
  def push([x, y | list], op) when op in @operators do
    result = apply(Kernel, op, [x, y])
    {:ok, [result | list]}
  end

  def push(list, input) do
    {:error, list, {:unexpected_input, input}}
  end

end

Now I want to test the happy path of pushing an operator to a stack (list) of numbers, where the list has at least two numbers in it.

since number() |> list() |> non_empty() will shrink to a one element list it does not work for me.

so I tried to to use the such_that macro to generate lists of numbers with length(l) > 1 but that ends with {:error, :cant_generate}

My test and generator

  property "can push operator to a list with minimum 2 numbers" do
    forall {list, operator} <- {list_min_two(), operator()} do
      {:ok, _new_list} = RPN.push(list, operator)
    end
  end

  # Generators

  def operator() do
    oneof([:+, :-, :/, :*])
  end

  def list_min_two() do
    such_that l <- non_empty(list(number())), when: length(l) > 1
  end

Maybe @alfert has some insight?

Marked As Solved

smedegaard

smedegaard

I thought I tried this yesterday but I Solved i with [ number() | list_of_numbers ]

where list_of_numbers = non_empty(list(number()))

I also filtered 0 out to avoid division by zero errors, but that’s another story.

Also Liked

alfert

alfert

Sorry for my long delay. I cannot reproduce your problem with the such_that operator. I did the following:

iex> use PropCheck
iex> produce(such_that l <- non_empty(list(number())), when: length(l) > 1)
{:ok,
 [-10, -12.498990234832617, 26.565811750847487, -0.8553774801624835,  -11.727179060408071, 11, 6, -6, 0, -18]}

iex> sample_shrink(such_that l <- non_empty(list(number())), when: length(l) > 1)
[-10.085757951113234,4,0.49263719097867137,-8.994420603636804,-4,-33,
 -1.3484964548787592,-2,-28,0.772165396869481]
[-10.085757951113234,4,0.49263719097867137,-8.994420603636804,-4]
[-10.085757951113234,4,0.49263719097867137]
[4,0.49263719097867137]
[0,0.49263719097867137]
[0,21]
[0,0]
:ok

This would be the expected behaviour. Using the produce and sample_shrink helps to find bugs in the generators.

But your approach using

iex> sample_shrink([number() | non_empty(list(number))])
[-6,-129.9346393942964,1,9]
[0,-129.9346393942964,1,9]
[0,1,9]
[0,9]
[0,0]
:ok

works equally well: a list of generators is also a generator and will not shrink towards the empty list. The combination of generators works on the generator and not on the data level, therefore both generators must be present when shrinking.

Hope that explains the approach.

In your case, I would go for several generators:

def non_zero_number, do: such_that n <- number(), where: n != 0
def list_min_two(elem_gen), do: 
   such_that l <- non_empty(list(elem_gen() )), when: length(l) > 1
def safe_numbers, do: list_min_two(non_zero_number())
def numbers, do: list_min_two(number())

But I assume your approach is similar.

If this still fails, then please file a bug report on GitHub! In the best case, we have a documentation issue.

ferd

ferd

Author of Property-Based Testing with PropEr, LYSE, & Erlang in Anger

you may want to use some cheats that let you properly ensure that you never generate useless sequences, using let macros. You can do something like let l <- list(elem()), do: [elem(), elem() | l] which will give a 2-or-more elements list no matter what happens, without needing retried generators in a such_that macro.

smedegaard

smedegaard

Thanks for the reply!

such_that l <- non_empty(list(number())), when: length(l) > 1

I was trying to achieve your first point with the above code.
The such_that macro should discard generated values when the anonymous function returns false.

I thought about combining generators in the way you suggest in point 2. But I have not found out how. I’ll dig a bit more into that :+1:

Where Next?

Popular in Questions Top

nobody
How to bind a phoenix app to a specific ip address? could not find anything about that, nowhere, unfortunately, but for me this is quite...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a &gt; b) do {:ok, "a"} end if (a &lt; b) do {:ok, b} end if (a == b) do {:ok, "equa...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lists...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
JulienCorb
I am trying to implement my new.html.eex file to create new posts on my website. new.html.eex: &lt;h1&gt;Create Post&lt;/h1&gt; &lt;%= ...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New

Other popular topics Top

Nvim
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help. Where are they similar? Where do they differ the m...
New
lessless
I believe there are people here who are dealing with CSV files import on the daily basis, and since Excel is a really popular tool there ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New

We're in Beta

About us Mission Statement