OK! So say you finished writing your code and it’s looking good. Let’s say it’s this incredible sum function:

module Sum { sub sum( $ a , $ b ) is export { $ a + $ b } }

Great, isn’t it?! Let’s use it:

use Sum ; say sum 2 , 3 ; # 5

That worked! We summed the number 2 with the number 3 as you saw. If you carefully read the function you’ll see the variables $a and $b haven’t a type set. If you don’t type a variable it’s, by default, of type Any. 2 and 3 are Ints… Ints are Any. So that’s OK! But do you know what’s Any too? Str (just a example)!

Let’s try using strings?

use Sum ; say sum " bla " , " ble " ;

We got a big error:

Cannot convert string to number: base-10 number must begin with valid digits or '.' in ' ⏏ bla ' (indicated by ⏏) in sub sum at sum.p6 line 1 in block at sum.p6 line 7 Actually thrown at: in sub sum at sum.p6 line 1 in block at sum.p6 line 7

Looks like it does not accept Strs… It seems like Any may not be the best type to use in this case.

Worrying about every possible input type for all our functions can prove to demand way too much work, as well as still being prone to human error. Thankfully there’s a module to help us with that! Test::Fuzz is a perl6 module that implements the “technique” of generative testing/fuzz testing.

Generative testing or Fuzz Testing is a technique of generating random/extreme data and using this data to call the function being tested.

Test::Fuzz gets the signature of your functions and decides what generators it should use to test it. After that it runs your functions giving it (100, by default) different arguments and testing if it will break.

To test our function, all that’s required is:

module Sum { use Test::Fuzz ; sub sum( $ a , $ b ) is export is fuzzed { $ a + $ b } } multi MAIN( : $ fuzz ! ) { run-tests }

And run:

perl6 Sum.pm6 --fuzz

This case will still show a lot of errors:

Use of uninitialized value of type Thread in numeric context in sub sum at Sum.pm6 line 4 Use of uninitialized value of type int in numeric context in sub sum at Sum.pm6 line 4 ok 1 - sum(Thread, int) Use of uninitialized value of type X::IO::Symlink in numeric context in sub sum at Sum.pm6 line 4 ok 2 - sum(X::IO::Symlink, -3222031972) Use of uninitialized value of type X::Attribute::Package in numeric context in sub sum at Sum.pm6 line 4 ok 3 - sum(X::Attribute::Package, -9999999999) Use of uninitialized value of type Routine in numeric context in sub sum at Sum.pm6 line 4 not ok 4 - sum(áéíóú, (Routine)) ...

What does that mean?

That means we should use one of the big features of perl6: Gradual typing. $a and $b should have types.

So, let’s modify the function and test again:

module Sum { use Test::Fuzz ; sub sum( Int $ a , Int $ b ) is export is fuzzed { $ a + $ b } } multi MAIN( : $ fuzz ! ) { run-tests }

ok 1 - sum(-2991774675, 0) ok 2 - sum(5471569889, 7905158424) ok 3 - sum(8930867907, 5132583935) ok 4 - sum(-6390728076, -1) ok 5 - sum(-3558165707, 4067089440) ok 6 - sum(-8930867907, -5471569889) ok 7 - sum(3090653502, -2099633631) ok 8 - sum(-2255887318, 1517560219) ok 9 - sum(-6085119010, -3942121686) ok 10 - sum(-7059342689, 8930867907) ok 11 - sum(-2244597851, -6390728076) ok 12 - sum(-5948408450, 2244597851) ok 13 - sum(0, -5062049498) ok 14 - sum(-7229942697, 3090653502) not ok 15 - sum((Int), 1) # Failed test 'sum((Int), 1)' # at site#sources/FB587F3186E6B6BDDB9F5C5F8E73C55195B73C86 (Test::Fuzz) line 62 # Invocant requires an instance of type Int, but a type object was passed. Did you forget a .new? ...

A lot of OKs! \o/

But there’re still some errors… We can’t sum undefined values…

We didn’t say the attributes should be defined (with :D). So Test::Fuzz generated every undefined sub-type of Int that it could find. It uses every generator of a sub-type of Int to generate values. It also works if you use a subset or even if you use a where in your signature. It’ll use a super-type generator and grep the valid values.

So, let’s change it again!

module Sum { use Test::Fuzz ; sub sum( Int : D $ a , Int : D $ b ) is export is fuzzed { $ a + $ b } } multi MAIN( : $ fuzz ! ) { run-tests }

ok 1 - sum(6023702597, -8270141809) ok 2 - sum(-8270141809, -3762529280) ok 3 - sum(242796759, -7408209799) ok 4 - sum(-5813412117, -5280261945) ok 5 - sum(2623325683, 2015644992) ok 6 - sum(-696696815, -7039670011) ok 7 - sum(1, -4327620877) ok 8 - sum(-7712774875, 349132637) ok 9 - sum(3956553645, -7039670011) ok 10 - sum(-8554836757, 7039670011) ok 11 - sum(1170220615, -3) ok 12 - sum(-242796759, 2015644992) ok 13 - sum(-9558159978, -8442233570) ok 14 - sum(-3937367230, 349132637) ok 15 - sum(5813412117, 1170220615) ok 16 - sum(-7408209799, 6565554452) ok 17 - sum(2474679799, -3099404826) ok 18 - sum(-5813412117, 9524548586) ok 19 - sum(-6770230387, -7408209799) ok 20 - sum(-7712774875, -2015644992) ok 21 - sum(8442233570, -1) ok 22 - sum(-6565554452, 9999999999) ok 23 - sum(242796759, 5719635608) ok 24 - sum(-7712774875, 7039670011) ok 25 - sum(7408209799, -8235752818) ok 26 - sum(5719635608, -8518891049) ok 27 - sum(8518891049, -242796759) ok 28 - sum(-2474679799, 2299757592) ok 29 - sum(5356064609, 349132637) ok 30 - sum(-3491438968, 3438417115) ok 31 - sum(-2299757592, 7580671928) ok 32 - sum(-8104597621, -8158438801) ok 33 - sum(-2015644992, -3) ok 34 - sum(-6023702597, 8104597621) ok 35 - sum(2474679799, -2623325683) ok 36 - sum(8270141809, 7039670011) ok 37 - sum(-1534092807, -8518891049) ok 38 - sum(3551099668, 0) ok 39 - sum(7039670011, 4327620877) ok 40 - sum(9524548586, -8235752818) ok 41 - sum(6151880628, 3762529280) ok 42 - sum(-8518891049, 349132637) ok 43 - sum(7580671928, 9999999999) ok 44 - sum(-8235752818, -7645883481) ok 45 - sum(6460424228, 9999999999) ok 46 - sum(7039670011, -7788162753) ok 47 - sum(-9999999999, 5356064609) ok 48 - sum(8510706378, -2474679799) ok 49 - sum(242796759, -5813412117) ok 50 - sum(-3438417115, 9558159978) ok 51 - sum(8554836757, -7788162753) ok 52 - sum(-9999999999, 3956553645) ok 53 - sum(-6460424228, -8442233570) ok 54 - sum(7039670011, -7712774875) ok 55 - sum(-3956553645, 1577669672) ok 56 - sum(0, 9524548586) ok 57 - sum(242796759, -6151880628) ok 58 - sum(7580671928, 3937367230) ok 59 - sum(-8554836757, 7712774875) ok 60 - sum(9524548586, 2474679799) ok 61 - sum(-7712774875, 2450227203) ok 62 - sum(3, 1257247905) ok 63 - sum(8270141809, -2015644992) ok 64 - sum(242796759, -3937367230) ok 65 - sum(6770230387, -6023702597) ok 66 - sum(2623325683, -3937367230) ok 67 - sum(-5719635608, -7645883481) ok 68 - sum(1, 6770230387) ok 69 - sum(3937367230, 7712774875) ok 70 - sum(6565554452, -5813412117) ok 71 - sum(7039670011, -8104597621) ok 72 - sum(7645883481, 9558159978) ok 73 - sum(-6023702597, 6770230387) ok 74 - sum(-3956553645, -7788162753) ok 75 - sum(-7712774875, 8518891049) ok 76 - sum(-6770230387, 6565554452) ok 77 - sum(-8554836757, 5356064609) ok 78 - sum(6460424228, 8518891049) ok 79 - sum(-3438417115, -9999999999) ok 80 - sum(-1577669672, -1257247905) ok 81 - sum(-5813412117, -3099404826) ok 82 - sum(8158438801, -3551099668) ok 83 - sum(-8554836757, 1534092807) ok 84 - sum(6565554452, -5719635608) ok 85 - sum(-5813412117, -2623325683) ok 86 - sum(-8158438801, -3937367230) ok 87 - sum(5813412117, -46698532) ok 88 - sum(9524548586, -2474679799) ok 89 - sum(3762529280, -2474679799) ok 90 - sum(7788162753, 9558159978) ok 91 - sum(6770230387, -46698532) ok 92 - sum(1577669672, 6460424228) ok 93 - sum(4327620877, 3762529280) ok 94 - sum(-6023702597, -2299757592) ok 95 - sum(1257247905, -8518891049) ok 96 - sum(-8235752818, -6151880628) ok 97 - sum(1577669672, 7408209799) ok 98 - sum(349132637, 6770230387) ok 99 - sum(-7788162753, 46698532) ok 100 - sum(-7408209799, 0) 1..100 ok 1 - sum

No errors!!!

Currently Test::Fuzz only implement generators for Int and Str, but as I said, it will be used for its super and sub classes. If you want to have generators for your custom class, you just need to implement a “static” method called generate-samples that returns sample instances of your class, infinite number of instances if possible.

Test::Fuzz is under development and isn’t in perl6 ecosystem yet. And we’re needing some help!

EDITED: New now you can only call run-tests()