by kgeist · 3y ago · 0 comments
>I was ending up with garbage, and not realizing it until I had visualized it with Graphviz!
>Imagine if we had invariants that we could assert after every property change to the tree.
Have you tried writing unit tests? What didn't work about them that you decided to try invariants?
Those testers may hit cases you forgot to write unit tests for.
Of course, you can also forget to write invariants, or write invariants that are less tight than they should be, but I think it often is easier to write invariants than to write exhaustive unit tests.
Firstly, writing “p should always be a prime” is clearer than writing “if p is a prime, and you call f, p should be a prime afterwards”, and secondly, invariants can apply to multiple methods that you, otherwise, would have to write separate tests for (“foo keeps p a prime”, “bar keeps p a prime”, “bar keeps p a prime when called with a null argument”, “baz keeps p a prime if it throws an exception”, etc)
Also, invariants, IMO, can be way better documentation than unit tests.
Finally, invariants leave open the possibility of using a theorem prover to (dis)prove that they hold.
Tests are fine too but more tedious work.
I studied Eiffel in university under Bertrand Meyer, and here's his (probably unique) point of view.
If someone hands you their library code and their unit tests, you have to understand their unit tests - which involves understanding why they chose the values that they did. There's also nothing stopping someone from testing the internals of their library as opposed to the publicly exposed behavior.
With contracts, imagine that to understand their library, you only see the method declarations with accompanying contracts. You don't see how HashSet.Add(x) is implemented, but you do see "if this.Contains(x) old(this.Count) == this.Count".
You don't have to see a test where this is tested with 5 example values, on an empty set, on a large set, whatever. You can rid yourself of that cognitive load by thinking, as sibling points out really well, declaratively over imperatively.
For unit testing, there's icontract-hypothesis; with the Preconditions and Postconditions delineated by e.g. decorators, it's possible to generate many of the fuzz tests from the additional Design by Contract structure of the source.
From https://github.com/mristin/icontract-hypothesis :
> icontract-hypothesis combines design-by-contract with automatic testing.
> It is an integration between icontract library for design-by-contract and Hypothesis library for property-based testing.
> The result is a powerful combination that allows you to automatically test your code. Instead of writing manually the Hypothesis search strategies for a function, icontract-hypothesis infers them based on the function’s [sic] precondition
This seems like it’s mostly syntactic sugar for assertions, keeping them at the interfaces of the function (in and out).
It can also be sometimes useful to have these conditions right there alongside the implementation and not just somewhere else in your unit tests.
Again, I believe that this is a PL research topic, but I'm not super well versed in it, so take that with a grain of salt.
https://learn.adacore.com/courses/intro-to-spark/chapters/03...
Unit testing requires someone to enforce they get updated.