@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()); }
@Test public void testValueOfBytes() { assertTrue(ArbitraryInteger.ZERO == ArbitraryInteger.valueOf(null)); assertTrue( ArbitraryInteger.MINUS_ONE == ArbitraryInteger.valueOf(BigInteger.valueOf(-1).toByteArray())); assertTrue( ArbitraryInteger.ZERO == ArbitraryInteger.valueOf(BigInteger.valueOf(0).toByteArray())); assertTrue( ArbitraryInteger.ONE == ArbitraryInteger.valueOf(BigInteger.valueOf(1).toByteArray())); assertTrue( ArbitraryInteger.TWO == ArbitraryInteger.valueOf(BigInteger.valueOf(2).toByteArray())); assertEquals( BigInteger.valueOf(-2), ArbitraryInteger.valueOf(BigInteger.valueOf(-2).toByteArray()).toNumber()); assertEquals( BigInteger.valueOf(3), ArbitraryInteger.valueOf(BigInteger.valueOf(3).toByteArray()).toNumber()); for (int i = 0; i < this.iterations; i++) { BigInteger reference = BigInteger.valueOf(this.randomizer.nextLong()).pow(this.randomizer.nextInt(20) + 10); ArbitraryInteger proband = ArbitraryInteger.valueOf(reference.toByteArray()); assertEquals(reference, proband.toNumber()); } }
@Test public void testAddAndSubtract() { for (int i = 0; i < this.iterations; i++) { BigInteger referenceBase = BigInteger.valueOf(this.randomizer.nextLong()).pow(this.randomizer.nextInt(20) + 10); BigInteger referenceSummand = BigInteger.valueOf(this.randomizer.nextLong()).pow(this.randomizer.nextInt(20) + 15); BigInteger referenceSubtrahend = BigInteger.valueOf(this.randomizer.nextLong()).pow(this.randomizer.nextInt(20) + 15); BigInteger referenceAdd = referenceBase.add(referenceSummand); BigInteger referenceSubtract = referenceBase.subtract(referenceSubtrahend); ArbitraryInteger base = ArbitraryInteger.valueOf(referenceBase.toByteArray()); ArbitraryInteger summand = ArbitraryInteger.valueOf(referenceSummand.toByteArray()); ArbitraryInteger subtrahend = ArbitraryInteger.valueOf(referenceSubtrahend.toByteArray()); ArbitraryInteger add = base.add(summand); ArbitraryInteger subtract = base.subtract(subtrahend); // this.logger.log(Level.FINE, "---- Add/Subtract Test Result ----"); // this.logger.log(Level.FINE, "R.Base: {0}", referenceBase.toString()); // this.logger.log(Level.FINE, "Base: {0}", base.toNumber().toString()); // this.logger.log(Level.FINE, "-"); // this.logger.log(Level.FINE, "R.Summand: {0}", referenceSummand.toString()); // this.logger.log(Level.FINE, "Summand: {0}", summand.toNumber().toString()); // this.logger.log(Level.FINE, "R.Add: {0}", referenceAdd.toString()); // this.logger.log(Level.FINE, "Add: {0}", add.toNumber().toString()); // this.logger.log(Level.FINE, "-"); // this.logger.log(Level.FINE, "R.Subtrahend: {0}", referenceSubtrahend.toString()); // this.logger.log(Level.FINE, "Subtrahend: {0}", subtrahend.toNumber().toString()); // this.logger.log(Level.FINE, "R.Subtract: {0}", referenceSubtract.toString()); // this.logger.log(Level.FINE, "Subtract: {0}", subtract.toNumber().toString()); assertEquals(referenceAdd, add.toNumber()); assertEquals(referenceSubtract, subtract.toNumber()); } }