aaronrenner

aaronrenner

Phoenix Core Team

PropCheck: utf8 generator doesn't seem to respect resize/2

I’m going through the examples in Property-Based Testing with PropEr, Erlang, and Elixir in the Custom Generators chapter and I’m unable to make the make the generated bio: utf-8 string grow with the resize function.

property "profile 2", [:verbose] do
  forall profile <- [
    name: utf8(),
    age: pos_integer(),
    bio: sized(s, resize(s * 35, utf8()))
  ] do
    name_len = to_range(10, String.length(profile[:name]))
    bio_len = to_range(300, String.length(profile[:bio]))
    aggregate(true, name: name_len, bio: bio_len)
  end
end

According to the book the stats should look something like this with the generated bio data being up various lengths up to 1500 characters.

test output
​ 	
​32% {name,{0,10}}
​28% {bio,{0,300}}
​12% {bio,{300,600}}
​10% {name,{10,20}}
​7% {name,{20,30}}
​4% {bio,{600,900}}
​3% {bio,{900,1200}}
​1% {bio,{1200,1500}}
​1% {name,{30,40}}

However when I actually run the example, all of my bio data ended up in the {0,300} range.


50% {bio,{0,300}}
30% {name,{0,10}}
7% {name,{10,20}}
4% {name,{20,30}}
3% {name,{30,40}}
1% {name,{40,50}}
1% {name,{70,80}}
0% {name,{50,60}}
0% {name,{80,90}}
0% {name,{90,100}}
0% {name,{130,140}}
0% {name,{1130,1140}}

Exploring the issue

After some experimentation, I simplified things and decided to build my own property tests.

property "utf8 with no resizing", [:verbose] do
  forall my_string <- utf8() do
    collect(true, to_range(100, String.length(my_string)))
  end
end

# -- Stats: utf8 with no resizing --
# 95% {0,100}
# 5% {100,200}

property "utf8 large-resizing", [:verbose] do
  forall my_string <- resize(10_000, utf8()) do
    collect(true, to_range(100, String.length(my_string)))
  end
end

# -- Stats: utf8 large-resizing --
# 99% {0,100}
# 1% {100,200}

property "utf8 set utf8-size", [:verbose] do
  forall my_string <- utf8(10_000) do
    collect(true, to_range(100, String.length(my_string)))
  end
end

# -- Stats:  set utf8-size --
# 6% {5800,5900}
# 4% {5900,6000}
# 3% {600,700}
# 3% {1000,1100}
# 3% {1500,1600}
# 3% {2100,2200}
# 3% {4400,4500}
# ...

property "utf8 sized/resize macro", [:verbose] do
  forall my_string <- sized(s, resize(100 * s, utf8())) do
    collect(true, to_range(100, String.length(my_string)))
  end
end

# -- Stats:  utf8 sized/resize macro --
# 98% {0,100}
# 2% {100,200}

property "utf8 sized macro with setting utf8-size", [:verbose] do
  forall my_string <- sized(s, utf8(s * 100)) do
    collect(true, to_range(100, String.length(my_string)))
  end
end

# -- Stats:  utf8 sized macro with setting utf8-size --
# 11% {400,500}
# 10% {0,100}
# 9% {100,200}
# 9% {200,300}
# 6% {500,600}
# ...

It seems like the utf8 generator doesn’t compute it’s length based on the generation size. Instead I have to pass in the desired length as an argument.

# Doesn't work
forall my_string <- resize(10_000, utf8()) do
 #...
end

# Works
forall my_string <- utf8(10_000)) do
 #...
end

The Question

Is it normal for the utf8 generator to not base it length on the generation size? I’m trying to figure out:

  • Am I doing something wrong?
  • Is this a bug that needs to be reported to proper/propcheck?
  • Is this an issue in the book that needs to be reported as errata?

If someone would like to play around with the code from the book themselves, they can download it here, expand the .tgz and view the file in code/CustomGenerators/elixir/pbt/test/generators_test.exs.

Thanks in advance for everyone’s feedback!

First Post!

bibekp

bibekp

I’m just wondering, did this ever get resolved? If so, what was the fix here? Thanks B

Where Next?

Popular in Questions Top

chokchit
** (DBConnection.ConnectionError) connection not available and request was dropped from queue after 2733ms. You can configure how long re...
New
sergio
In Ruby, I can go: User.find_by(email: "foobar@email.com").update(email: "hello@email.com") How can I do something similar in Elixir? ...
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
mcarvalho
What is the difference between System.get_env and Application.get_env? For example, what are best practices to use one versus another.
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
985 42920 311
New
vrod
I am using the Starship cross-shell prompt – it seems pretty nice, but I get some errors: [WARN] - (starship::utils): Executing command ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New
boundedvariable
I am going through the kafka architecture. All the features what the kafka is providing are already in Erlang. I would like hear your opi...
New
AstonJ
We’ve put together this wiki for Phoenix LiveView - please feel free to add any info you feel is worth including. What is Phoenix LiveV...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New

We're in Beta

About us Mission Statement