Hacker News new | ask | show | jobs
by chriswarbo 1876 days ago
Probably worth mentioning that we sometimes need a normalisation step. The most common example is comparing two lists for equality, when their order doesn't actually matter; this can produce false-positives if some later refactor changes the order of the elements. In this case we can normalise the values by sorting them before comparing.

One example I keep hitting is a widely used JSON library for Haskell, whose test suite checks a pair of values for equality, when they just-so-happen to be in the opposite order on 32bit x86. Since I do lots of dependency pinning, this same bug keeps cropping up in different contexts :(

3 comments

I think that using a better datastructure is often better than normalizing in tests: if order doesn’t matter, use a bag or set instead of arrays. I’ve generally found that this sort of thinking reduces boilerplate across the board.
Subsetting, mapping and normalization.

I think about it like running "an SQL query" on the thing I'm comparing and the resulting value structure (made up of maps/lists/numbers/bool/string) is the result set of the query. Then I compare queries.

I wrote a hacky helper for this sort of thing the other day. It takes a vector of values and a vector of functions. For each value it scans the functions until one returns true, and then pops off that function. If either list isn't empty it prints the unmatched items and panics.