Associativity: a + b + c = (a + b) + c = a + (b + c)
Identity element is 0: a + 0 = 0 + a = a
test "add two numbers" {
assert(1 + 1 === 2);
// etc...
}
@given(a number, b number)
test "addition is commutative" {
assert(a + b === b + a);
}
@given(a number, b number, c number)
test "addition is associative" {
assert(a + b + c === (a + b) + c);
assert((a + b) + c === a + (b + c));
assert(a + (b + c) === a + b + c);
}
@given(a number)
test "zero is the identity element of the addition" {
assert(a + 0 === 0 + a);
assert(a === 0 + a);
assert(a + 0 === a);
}
That's it
Asserting that invariants/properties are always true given specific conditions
Common properties
Commutativity
Associativity
Identity element
Successor
Reversibility
Some things never change
Idempotence
Structural induction: Solve a smaller problem first
Test Oracle: Use an alternate version to check the result
Common properties
Reversibility
data === decode(encode(data))
Common properties
Some things never change
an invariant that is preserved after some transformation
length(collection) === length(sort(collection))
Common properties
Idempotence
given the same input, doing an operation twice is the same as doing it once
Associativity: the order of operations does not change the result.
- Special case for integer
- Successor : for any integer `a`, the integer `(a + 1)` is the least integer greater than `a`
shrink: tries to reduce it to a minimal failing subset by removing or simplifying input data that are unneeded to make the test fail.