▲ | Rendello 5 days ago | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
This situation where two paths produce the same output but one is optimized is the easiest case in property-based testing, as the property is just:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | Stratoscope 5 days ago | parent | next [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
I have sometimes done just this. First I write the simplest possible brute force code, something that any competent programmer can look at and say, "Yes, this may be slow, but it is straightforward and obvious that it will handle all cases." Then I write the optimized code and use a test like yours to compare the results between the simple code and the optimized code. One time I needed to write a function to search for a specific pattern in a binary file and change it. So I wrote the brute force code as a first step, the same code that anyone would probably write as a simple solution. It worked the first time, and a couple of people reviewed the code and said "yep, even if it's slow, it is correct." But this code took more than a second to run! Of course I thought about optimizing it with Boyer-Moore or the like. Then I went, "Hold on to your horses. This isn't something like a web page load where one second matters. It's part of a build process that only runs a few times a day and already takes several minutes to run. One extra second is nothing!" In the wise words of Kenny Rogers in The Gambler:
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
▲ | andrepd 4 days ago | parent | prev [-] | |||||||||||||||||||||||||||||||||||||||||||||||||||||||
Absolutely! Proptesting is a great but underutilised feature. I use it a lot wherever I'm in a language which makes it easy (and with a mature library for that), like Haskell, Ocaml, or Rust. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|