## Stream: general

### Topic: proptest: numbers that sum to a number

Jake Goulding (Jun 24 2019 at 00:55, on Zulip):

@centril I’d like to write a proptest that takes as input a `Vec<u8>` and a `Vec<usize>` such that the sum of the latter equals the length of the former. This is for testing that processing a blob of bytes in arbitrary chunks does not affect my algorithm. Any suggestions on how to create that?

centril (Jun 24 2019 at 00:56, on Zulip):

hmm; well avoid filtering :slight_smile:

centril (Jun 24 2019 at 00:58, on Zulip):

@Jake Goulding So if I understand the test correctly, I would generate the `vu: Vec<usize>`, fetch the length, and then generate a random `Vec<u8>` from that

Jake Goulding (Jun 24 2019 at 00:59, on Zulip):

hmm, do it backwards. Interesting.

centril (Jun 24 2019 at 00:59, on Zulip):

Yeah because constructing a sum from a length seems... harder?

centril (Jun 24 2019 at 00:59, on Zulip):

Tho possibly cheaper

centril (Jun 24 2019 at 01:02, on Zulip):

Naively, constructing a sum from a length sounds like iteratively and randomly generating numbers that is within the interval of the length and then adding them up until the sum is zero

centril (Jun 24 2019 at 01:02, on Zulip):

Maybe proptest has something for that but I don't remember ^^

Jake Goulding (Jun 24 2019 at 01:07, on Zulip):

yeah, it felt like an iterative/recursive concept to me: pick a random number 0..len, repeat until 0, but I couldn’t figure out how to bend the recursive functionality

centril (Jun 24 2019 at 01:08, on Zulip):

I would do some searching in the docs to find what you want :D

centril (Jun 24 2019 at 01:09, on Zulip):

or make a PR if you didn't find it

Jake Goulding (Jun 24 2019 at 01:11, on Zulip):

you mean an issue? Hard to PR if I don’t know what the code is :-)

Jake Goulding (Jun 24 2019 at 01:14, on Zulip):

Amusingly, this “write it backwards” advice was something I read recently in a protesting article. Too bad I didn’t connect the dots

Jake Goulding (Jun 24 2019 at 01:16, on Zulip):

thread 'same_results_as_c_for_64_bit_with_varying_chunks' panicked at 'capacity overflow', src/liballoc/raw_vec.rs:748:5

Stupid property-based testing… testing all the properties

centril (Jun 24 2019 at 01:24, on Zulip):

Yea figure out the code first :D

Jake Goulding (Jun 24 2019 at 01:25, on Zulip):

Well, that was because I was creating a `Vec<u8>` with a Very Large Capacity via the props

Jake Goulding (Jun 24 2019 at 01:39, on Zulip):

In case you are interested, this article calls this technique an “oracle generator”

https://wickstrom.tech/programming/2019/04/17/property-based-testing-in-a-screencast-editor-case-study-2.html

Last update: Aug 13 2020 at 09:15UTC