aaronrenner

aaronrenner

Phoenix Core Team

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

Last Post!

alfert

alfert

I see this question here for the first time. It would be helpful if you try this out and open a bug report on github (Issues · alfert/propcheck · GitHub), if utf8 does not behave as expected. It might be helpful to check against the master, since we are implementing a bunch of features.

But from the docs, you can see this:

@spec utf8[ext_non_neg_intege, 1..4) :: type()
utf8(n, max_codepoint_size)

Bounded upper size utf8 binary, codepoint length =< MaxCodePointSize .

Limiting codepoint size can be useful when applications do not accept full unicode range. For example, MySQL in utf8 encoding accepts only 3-byte unicode codepoints in VARCHAR fields.

If unbounded length is needed, use :inf as first argument.

Where Next? Top

Trending in Questions Top

RSP87
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
nseaSeb
Hello, I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
Hi there! We created Gust: A task orchestrator inspired by Airflow. For those who have never heard about Aiflow, it’s a Python-based wor...
New
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews