@Test public void testModInverseSmallExhaustive() {

  for (int val = 10; val >= 0; --val) {
    for (int mod = 10; mod >= -1; --mod) {
      BigInteger bVal = BigInteger.valueOf(val);
      BigInteger bMod = BigInteger.valueOf(mod);
      try {
        BigInteger expected = bVal.modInverse(bMod);
        BigInteger actual = modInverse(bVal, bMod);
        assertEquals(String.format("val %d, mod %d", val, mod) + mod, expected, actual);
      } catch (ArithmeticException e) {
        try {
          modInverse(bVal, bMod);
          fail("ArithmeticException expected");
        } catch (ArithmeticException expected) {
        }
      }
    }
  }
}