smedegaard
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?
Trending in Questions
I having some trouble figuring out if I have set myself too strict of standards for my production server. Currently I can handle 75% of r...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hello,
I’m trying to build a basic Phoenix web-app, and I’d like to use Tailwind.
However, when I launch mix phx.server, I get an error...
New
Hi everyone,
I am toying with the idea of building a “match maker” for giving personal help to people that wants to start coding.
I sta...
New
I recently noticed that Elixir’s Logger defaults its primary log level to :debug when no :logger, :level application configuration is pre...
New
I’m working on a small exercise involving update_in/3, and I came up with this solution:
data = %{
name: "Periodic Table",
category:...
New
I’ve got trouble wrapping my head around the order in which functions are called in this snippet (from Phoenix’s authentication):
toke...
New
Other Trending Topics
Edit: 2026 May 15 - This post is archived.
Mob is alive!!
Main docs: mob v0.7.11 — Documentation
A bit of explanation for the slightly c...
New
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
New
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hobbes is a low-level distributed database for the Elixir programming language.
Hobbes provides a simple, safe, and scalable storage lay...
New
Hi everyone!
The first release candidate for the Expert language server project is now available!
We’ve published a press release detai...
New
A little off-topic, but I feel like people here have a good head on their shoulders.
I used to be quite good at making software. Was luc...
New
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ai
- #ecto-query
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #elixirconf-eu
- #api
- #forms
- #metaprogramming
- #hex










Showing Posts 1 to 8- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
Qqwy
I have only used StreamData before, but I PropCheck is probably similar. I see two possibilities for the generator:
a = non_empty(list(number()))and a separateb = number(), which you then combine (using a map orbindoperation to turn them into[a | b]if PropCheck has something like that, I did not find it right away, or otherwise just inside your tests).smedegaard
Thanks for the reply!
I was trying to achieve your first point with the above code.
The
such_thatmacro should discard generated values when the anonymous function returnsfalse.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
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
0out to avoid division by zero errors, but that’s another story.alfert
Sorry for my long delay. I cannot reproduce your problem with the
such_thatoperator. I did the following:This would be the expected behaviour. Using the
produceandsample_shrinkhelps to find bugs in the generators.But your approach using
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:
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.
smedegaard
Thanks a lot for the answer. Your examples in iex works fine. Don’t know why the function didn’t work…
ferd
you may want to use some cheats that let you properly ensure that you never generate useless sequences, using
letmacros. You can do something likelet 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 asuch_thatmacro.smedegaard
Cool. Thanks @fred!
bibekp
For posterity
https://github.com/alfert/propcheck/issues/139