Example #1
0
  @Test
  public void testCheckFunctions() {
    assertFalse(ArbitraryInteger.ZERO.isNegativ());
    assertTrue(ArbitraryInteger.ZERO.isZero());
    assertFalse(ArbitraryInteger.ZERO.isPositiv());
    assertEquals(0, ArbitraryInteger.ZERO.signum());

    assertFalse(ArbitraryInteger.ONE.isNegativ());
    assertFalse(ArbitraryInteger.ONE.isZero());
    assertTrue(ArbitraryInteger.ONE.isPositiv());
    assertEquals(1, ArbitraryInteger.ONE.signum());

    assertTrue(ArbitraryInteger.MINUS_ONE.isNegativ());
    assertFalse(ArbitraryInteger.MINUS_ONE.isZero());
    assertFalse(ArbitraryInteger.MINUS_ONE.isPositiv());
    assertEquals(-1, ArbitraryInteger.MINUS_ONE.signum());

    for (int i = 0; i < this.iterations; i++) {
      BigInteger reference =
          BigInteger.valueOf(this.randomizer.nextLong() - Long.MAX_VALUE)
              .pow(this.randomizer.nextInt(20) + 10);
      ArbitraryInteger proband = ArbitraryInteger.valueOf(reference.toByteArray());
      assertEquals(reference.signum(), proband.signum());
      assertTrue(
          reference.signum() < 0 && proband.isNegativ()
              || reference.signum() > 0 && !proband.isNegativ());
      assertTrue(
          reference.signum() > 0 && proband.isPositiv()
              || reference.signum() < 0 && !proband.isPositiv());
      assertTrue(reference.bitLength() == proband.bitLength());
    }
  }
Example #2
0
 @Test
 public void testUnaryFunctions() {
   for (int i = 0; i < this.iterations; i++) {
     ArbitraryInteger proband = ArbitraryInteger.valueOf(this.randomizer.nextInt());
     assertTrue(proband.signum() * -1 == proband.negate().signum());
     BigInteger t = proband.toNumber();
     assertEquals(t.pow(2), proband.square().toNumber());
     assertTrue(
         proband.isNegativ() && proband.abs().isPositiv()
             || proband.isPositiv() && proband.abs().isPositiv());
     if (!proband.isZero()) {
       ArbitraryRational ratProband = proband.reciprocal();
       if (proband.isPositiv()) {
         assertTrue(ArbitraryInteger.ONE == ratProband.numerator());
         assertTrue(proband == ratProband.denominator());
       } else {
         assertTrue(ArbitraryInteger.MINUS_ONE == ratProband.numerator());
         assertEquals(proband.negate().toNumber(), ratProband.denominator().toNumber());
       }
     }
     assertEquals(proband.toNumber().add(BigInteger.ONE), proband.increment().toNumber());
     assertEquals(proband.toNumber().subtract(BigInteger.ONE), proband.decrement().toNumber());
   }
   try {
     ArbitraryRational ratProband = ArbitraryInteger.ZERO.reciprocal();
     fail("Reciprocal of zero should yield an ArithmeticException");
   } catch (ArithmeticException e) {
     // thats good
   }
   assertTrue(ArbitraryInteger.ONE == ArbitraryInteger.ZERO.increment());
   assertTrue(ArbitraryInteger.TWO == ArbitraryInteger.ONE.increment());
   assertTrue(ArbitraryInteger.MINUS_ONE == ArbitraryInteger.ZERO.decrement());
   assertTrue(ArbitraryInteger.ONE == ArbitraryInteger.TWO.decrement());
 }