Skip to content

Commutativity

Many operations shouldn’t care which way round they’re given their inputs: adding two totals, merging two sets of changes, combining the counts from two servers. When one quietly does care, the result depends on which input happened to arrive first, and that’s a bug that shows up only sometimes. Commutativity checks that both orders give the same answer.

A greengrocer’s app keeps each basket as fruit and how many of each. Combining two baskets should give everything in both:

Baskets.java
/** Everything in both baskets. */
@SafeVarargs
static Map<String, Integer> combine(Map<String, Integer>... baskets) {
var all = new HashMap<String, Integer>();
for (var basket : baskets) {
all.putAll(basket);
}
return all;
}

Whichever basket comes first, the combined basket should be the same:

CommutativityTest.java
@Test
void combiningBasketsInEitherOrderAgrees() {
forAll(baskets(), baskets())
.check(
(mine, yours) -> {
var oneWay = Baskets.combine(mine, yours);
var otherWay = Baskets.combine(yours, mine);
assertEquals(oneWay, otherWay);
});
}
static Domain<Map<String, Integer>> baskets() {
return maps(elements("apple", "pear"), integers().atLeast(1).atMost(9));
}

With only two kinds of fruit, two baskets often hold the same kind, which is where the order matters.

Terminal window
expected: <{apple=2}> but was: <{apple=1}>
combiningBasketsInEitherOrderAgrees failed after 1 try (seed 0x73cb82e6abd602f4)
shrunk counterexample:
mine = {"apple"=1}, yours = {"apple"=2}
pin it:
.example(/* mine */ Map.of("apple", 1), /* yours */ Map.of("apple", 2))
rerun:
PROPTASTIC_SEED=0x73cb82e6abd602f4 ./gradlew test --tests 'CommutativityTest.combiningBasketsInEitherOrderAgrees'

My basket has one apple and yours has two. putAll doesn’t add the counts: the second basket’s count replaces the first’s. So combining mine with yours gives two apples, yours with mine gives one, and it should be three either way. Baskets with different fruit, the case a hand-written test would likely try, combine correctly.

The assertion’s own message prints the maps as Java does, {apple=2}. The counterexample prints them as source, {"apple"=1}, and the pin it: line as code that builds them.

  • the code combines two things of the same kind: sums, unions, merges, the counts from two workers, two sets of permissions;
  • work is split up and gathered again in whatever order it finishes, as in parallel streams and distributed systems;
  • two operations should be independent: applying a discount then a tax, or the tax then the discount.
  • Combining that’s wrong in both orders. A combine that drops every count but the largest is commutative, and still loses fruit. Check what the result holds too: here, that every fruit’s count is the sum of the two. That’s conservation.
  • Operations that are meant to care about order. Subtraction, string concatenation, applying changes to a document: check those with another pattern.

An operation is commutative when a ∘ b = b ∘ a. Hughes, 2020 in further reading is a guide to choosing properties like this one.

Privacy