Software tools and cross products

A colleague approached me today regarding a unit test he was writing. He was constructing a series of test cases from a data structure; his code at the time used a multi-line string of which each line had several fields which together described a test to run.

He pointed to the right-most field, and said that for every test he already had with a 1 in that field, he’d also like to run a test with a 0 in that field. I looked at the other fields, and observed that for each field, there was only a small set of values in use.

“How about just testing the cross product of all the sets?”, I suggested. Then off to CPAN, and, hey, there’s Set::CrossProduct. Knock up a Debian package for it, add it to our internal Apt repository, and we’re done. How splendid.

I found it very pleasing how little effort was needed to get all that going. I think there are three factors that made it so straightforward:

  1. It’s often useful to have some maths/computer-science background; in this case, it was important to be able to identify the situation as needing a cross product.

  2. CPAN has, oh, everything. ’Nuff said.

  3. Tools make it easy to package and deploy software (and without such tools, using smaller off-the-shelf third-party libraries for some particular task can end up needing more effort than reimplementing it)

As an aside, I also note that Perl 6 makes that task much easier. Subject to errors in my understanding of Perl 6, I think the code looks something like this:

my @cross_product = @@( [X] @field_lists );