Easy test coverage with constant Hamming distance of 1
I left a terrible bug in version 1.3 of my replacement Gina. I didn’t want to miss any test case this time, so I wrote a batch file that tests every one of them. That batch file adds a user to a group and a group to the registry. There are two possible groups in the registry, and the user can be a member of either two groups, making 2^(2+2) possibilities, 16 use cases.
After a few lines in, I realized that I would be less work to order the tests in a way that would minimize the change to the configuration between any two tests. In other words, when a n+1 test case required a change to the registry, then the user group membership should not change, and vice-versa. That would also make it easy to investigate a failed test, because only one thing would change between any two tests.
Then it hit me.
Well actually, I had to stop and think for a while. Kind of like my mind restoring a dusty old tape archive… I remembered that mathematician Richard Hamming had a code for that. It’s a numbering scheme where only 1 bit changes between any two numbers. The number of bits that change is the Hamming distance between two numbers. Using four information bits to represent each possible use case, I came up with the following table. The first two rows (MSB, in blue) are user membership to a group, and the two last rows (LSB, in green) is the presence of that group in the registry. Ordering my tests that way gave me a constant Hamming distance of 1.
**Decimal value** | **0** | **1** | **3** | **2** | **6** | **7** | **5** | **4** | **12** | **13** | **15** | **14** | **10** | **11** | **9** | **8** |
Unlock | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 | 1 | **1** | **1** | **1** | **1** | 1 | 1 |
Logoff | 0 | 0 | 0 | 0 | 1 | **1** | **1** | 1 | 1 | **1** | 1 | 1 | 0 | 0 | 0 | 0 |
Unlock | 0 | 0 | 1 | 1 | 1 | 1 | 0 | 0 | 0 | 0 | **1** | **1** | **1** | **1** | 0 | 0 |
Logoff | 0 | 1 | 1 | 0 | 0 | **1** | **1** | 0 | 0 | **1** | 1 | 0 | 0 | 1 | 1 | 0 |
Expected test result | Gina | Gina | Gina | Gina | Gina | **Force logoff** | **Force logoff** | Gina | Gina | **Force logoff** | **Unlock** | **Unlock** | **Unlock** | **Unlock** | Gina | Gina |
The only drawback to this is that all the typing I saved writing my test batch file, I wasted on this blog post !