How to use assertEquals method of junit.framework.Assert class

Best junit code snippet using junit.framework.Assert.assertEquals

Source:MathUtilsTest.java Github

copy

Full Screen

...41		}42		return result;43	}44	public void test0Choose0() {45		junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(0, 0), 1.0, 0);46		junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.binomialCoefficientLog(0, 0), 0.0, 0);47		junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.binomialCoefficient(0, 0), 1);48	}49	public void testAddAndCheck() {50		int big = java.lang.Integer.MAX_VALUE;51		int bigNeg = java.lang.Integer.MIN_VALUE;52		junit.framework.Assert.assertEquals(big, org.apache.commons.math.util.MathUtils.addAndCheck(big, 0));53		try {54			org.apache.commons.math.util.MathUtils.addAndCheck(big, 1);55			junit.framework.Assert.fail("Expecting ArithmeticException");56		} catch (java.lang.ArithmeticException ex) {57		}58		try {59			org.apache.commons.math.util.MathUtils.addAndCheck(bigNeg, -1);60			junit.framework.Assert.fail("Expecting ArithmeticException");61		} catch (java.lang.ArithmeticException ex) {62		}63	}64	public void testAddAndCheckLong() {65		long max = java.lang.Long.MAX_VALUE;66		long min = java.lang.Long.MIN_VALUE;67		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.addAndCheck(max, 0L));68		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.addAndCheck(min, 0L));69		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.addAndCheck(0L, max));70		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.addAndCheck(0L, min));71		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.addAndCheck(-1L, 2L));72		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.addAndCheck(2L, -1L));73		junit.framework.Assert.assertEquals(-3, org.apache.commons.math.util.MathUtils.addAndCheck(-2L, -1L));74		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.addAndCheck((min + 1), -1L));75		testAddAndCheckLongFailure(max, 1L);76		testAddAndCheckLongFailure(min, -1L);77		testAddAndCheckLongFailure(1L, max);78		testAddAndCheckLongFailure(-1L, min);79	}80	private void testAddAndCheckLongFailure(long a, long b) {81		try {82			org.apache.commons.math.util.MathUtils.addAndCheck(a, b);83			junit.framework.Assert.fail("Expecting ArithmeticException");84		} catch (java.lang.ArithmeticException ex) {85		}86	}87	public void testBinomialCoefficient() {88		long[] bcoef5 = new long[]{ 1 , 5 , 10 , 10 , 5 , 1 };89		long[] bcoef6 = new long[]{ 1 , 6 , 15 , 20 , 15 , 6 , 1 };90		for (int i = 0 ; i < 6 ; i++) {91			junit.framework.Assert.assertEquals(("5 choose " + i), bcoef5[i], org.apache.commons.math.util.MathUtils.binomialCoefficient(5, i));92		}93		for (int i = 0 ; i < 7 ; i++) {94			junit.framework.Assert.assertEquals(("6 choose " + i), bcoef6[i], org.apache.commons.math.util.MathUtils.binomialCoefficient(6, i));95		}96		for (int n = 1 ; n < 10 ; n++) {97			for (int k = 0 ; k <= n ; k++) {98				junit.framework.Assert.assertEquals(((n + " choose ") + k), binomialCoefficient(n, k), org.apache.commons.math.util.MathUtils.binomialCoefficient(n, k));99				junit.framework.Assert.assertEquals(((n + " choose ") + k), binomialCoefficient(n, k), org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(n, k), java.lang.Double.MIN_VALUE);100				junit.framework.Assert.assertEquals(((n + " choose ") + k), java.lang.Math.log(binomialCoefficient(n, k)), org.apache.commons.math.util.MathUtils.binomialCoefficientLog(n, k), 1.0E-11);101			}102		}103		int[] n = new int[]{ 34 , 66 , 100 , 1500 , 1500 };104		int[] k = new int[]{ 17 , 33 , 10 , 1500 - 4 , 4 };105		for (int i = 0 ; i < (n.length) ; i++) {106			long expected = binomialCoefficient(n[i], k[i]);107			junit.framework.Assert.assertEquals((((n[i]) + " choose ") + (k[i])), expected, org.apache.commons.math.util.MathUtils.binomialCoefficient(n[i], k[i]));108			junit.framework.Assert.assertEquals((((n[i]) + " choose ") + (k[i])), expected, org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(n[i], k[i]), 0.0);109			junit.framework.Assert.assertEquals((((("log(" + (n[i])) + " choose ") + (k[i])) + ")"), java.lang.Math.log(expected), org.apache.commons.math.util.MathUtils.binomialCoefficientLog(n[i], k[i]), 0.0);110		}111	}112	public void testBinomialCoefficientLarge() throws java.lang.Exception {113		for (int n = 0 ; n <= 200 ; n++) {114			for (int k = 0 ; k <= n ; k++) {115				long ourResult = -1;116				long exactResult = -1;117				boolean shouldThrow = false;118				boolean didThrow = false;119				try {120					ourResult = org.apache.commons.math.util.MathUtils.binomialCoefficient(n, k);121				} catch (java.lang.ArithmeticException ex) {122					didThrow = true;123				}124				try {125					exactResult = binomialCoefficient(n, k);126				} catch (java.lang.ArithmeticException ex) {127					shouldThrow = true;128				}129				junit.framework.Assert.assertEquals(((n + " choose ") + k), exactResult, ourResult);130				junit.framework.Assert.assertEquals(((n + " choose ") + k), shouldThrow, didThrow);131				junit.framework.Assert.assertTrue(((n + " choose ") + k), ((n > 66) || (!didThrow)));132				if ((!shouldThrow) && (exactResult > 1)) {133					junit.framework.Assert.assertEquals(((n + " choose ") + k), 1.0, ((org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(n, k)) / exactResult), 1.0E-10);134					junit.framework.Assert.assertEquals(((n + " choose ") + k), 1, ((org.apache.commons.math.util.MathUtils.binomialCoefficientLog(n, k)) / (java.lang.Math.log(exactResult))), 1.0E-10);135				} 136			}137		}138		long ourResult = org.apache.commons.math.util.MathUtils.binomialCoefficient(300, 3);139		long exactResult = binomialCoefficient(300, 3);140		junit.framework.Assert.assertEquals(exactResult, ourResult);141		ourResult = org.apache.commons.math.util.MathUtils.binomialCoefficient(700, 697);142		exactResult = binomialCoefficient(700, 697);143		junit.framework.Assert.assertEquals(exactResult, ourResult);144		try {145			org.apache.commons.math.util.MathUtils.binomialCoefficient(700, 300);146			junit.framework.Assert.fail("Expecting ArithmeticException");147		} catch (java.lang.ArithmeticException ex) {148		}149		int n = 10000;150		ourResult = org.apache.commons.math.util.MathUtils.binomialCoefficient(n, 3);151		exactResult = binomialCoefficient(n, 3);152		junit.framework.Assert.assertEquals(exactResult, ourResult);153		junit.framework.Assert.assertEquals(1, ((org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(n, 3)) / exactResult), 1.0E-10);154		junit.framework.Assert.assertEquals(1, ((org.apache.commons.math.util.MathUtils.binomialCoefficientLog(n, 3)) / (java.lang.Math.log(exactResult))), 1.0E-10);155	}156	public void testBinomialCoefficientFail() {157		try {158			org.apache.commons.math.util.MathUtils.binomialCoefficient(4, 5);159			junit.framework.Assert.fail("expecting IllegalArgumentException");160		} catch (java.lang.IllegalArgumentException ex) {161		}162		try {163			org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(4, 5);164			junit.framework.Assert.fail("expecting IllegalArgumentException");165		} catch (java.lang.IllegalArgumentException ex) {166		}167		try {168			org.apache.commons.math.util.MathUtils.binomialCoefficientLog(4, 5);169			junit.framework.Assert.fail("expecting IllegalArgumentException");170		} catch (java.lang.IllegalArgumentException ex) {171		}172		try {173			org.apache.commons.math.util.MathUtils.binomialCoefficient(-1, -2);174			junit.framework.Assert.fail("expecting IllegalArgumentException");175		} catch (java.lang.IllegalArgumentException ex) {176		}177		try {178			org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(-1, -2);179			junit.framework.Assert.fail("expecting IllegalArgumentException");180		} catch (java.lang.IllegalArgumentException ex) {181		}182		try {183			org.apache.commons.math.util.MathUtils.binomialCoefficientLog(-1, -2);184			junit.framework.Assert.fail("expecting IllegalArgumentException");185		} catch (java.lang.IllegalArgumentException ex) {186		}187		try {188			org.apache.commons.math.util.MathUtils.binomialCoefficient(67, 30);189			junit.framework.Assert.fail("expecting ArithmeticException");190		} catch (java.lang.ArithmeticException ex) {191		}192		try {193			org.apache.commons.math.util.MathUtils.binomialCoefficient(67, 34);194			junit.framework.Assert.fail("expecting ArithmeticException");195		} catch (java.lang.ArithmeticException ex) {196		}197		double x = org.apache.commons.math.util.MathUtils.binomialCoefficientDouble(1030, 515);198		junit.framework.Assert.assertTrue("expecting infinite binomial coefficient", java.lang.Double.isInfinite(x));199	}200	public void testCompareTo() {201		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.compareTo(152.33, 152.32, 0.011));202		junit.framework.Assert.assertTrue(((org.apache.commons.math.util.MathUtils.compareTo(152.308, 152.32, 0.011)) < 0));203		junit.framework.Assert.assertTrue(((org.apache.commons.math.util.MathUtils.compareTo(152.33, 152.318, 0.011)) > 0));204	}205	public void testCosh() {206		double x = 3.0;207		double expected = 10.06766;208		junit.framework.Assert.assertEquals(expected, org.apache.commons.math.util.MathUtils.cosh(x), 1.0E-5);209	}210	public void testCoshNaN() {211		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.cosh(java.lang.Double.NaN)));212	}213	public void testEquals() {214		double[] testArray = new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 };215		for (int i = 0 ; i < (testArray.length) ; i++) {216			for (int j = 0 ; j < (testArray.length) ; j++) {217				if (i == j) {218					junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(testArray[i], testArray[j]));219					junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(testArray[j], testArray[i]));220				} else {221					junit.framework.Assert.assertTrue(!(org.apache.commons.math.util.MathUtils.equals(testArray[i], testArray[j])));222					junit.framework.Assert.assertTrue(!(org.apache.commons.math.util.MathUtils.equals(testArray[j], testArray[i])));223				}224			}225		}226	}227	public void testEqualsWithAllowedDelta() {228		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(153.0, 153.0, 0.0625));229		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(153.0, 153.0625, 0.0625));230		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(152.9375, 153.0, 0.0625));231		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NaN, java.lang.Double.NaN, 1.0));232		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.POSITIVE_INFINITY, java.lang.Double.POSITIVE_INFINITY, 1.0));233		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NEGATIVE_INFINITY, java.lang.Double.NEGATIVE_INFINITY, 1.0));234		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NEGATIVE_INFINITY, java.lang.Double.POSITIVE_INFINITY, 1.0));235		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(153.0, 153.0625, 0.0624));236		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(152.9374, 153.0, 0.0625));237	}238	public void testEqualsWithAllowedUlps() {239		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(153, 153, 1));240		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(153, 153.00000000000003, 1));241		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(153, 153.00000000000006, 1));242		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(153, 152.99999999999997, 1));243		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(153, 152.99999999999994, 1));244		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(-128, -127.99999999999999, 1));245		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(-128, -127.99999999999997, 1));246		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(-128, -128.00000000000003, 1));247		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(-128, -128.00000000000006, 1));248		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.POSITIVE_INFINITY, java.lang.Double.POSITIVE_INFINITY, 1));249		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.MAX_VALUE, java.lang.Double.POSITIVE_INFINITY, 1));250		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NEGATIVE_INFINITY, java.lang.Double.NEGATIVE_INFINITY, 1));251		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(-(java.lang.Double.MAX_VALUE), java.lang.Double.NEGATIVE_INFINITY, 1));252		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NaN, java.lang.Double.NaN, 1));253		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(java.lang.Double.NEGATIVE_INFINITY, java.lang.Double.POSITIVE_INFINITY, 100000));254	}255	public void testArrayEquals() {256		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(new double[]{ 1.0 }, null));257		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(null, new double[]{ 1.0 }));258		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(((double[])(null)), ((double[])(null))));259		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(new double[]{ 1.0 }, new double[0]));260		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(new double[]{ 1.0 }, new double[]{ 1.0 }));261		junit.framework.Assert.assertTrue(org.apache.commons.math.util.MathUtils.equals(new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 }, new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 }));262		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(new double[]{ java.lang.Double.POSITIVE_INFINITY }, new double[]{ java.lang.Double.NEGATIVE_INFINITY }));263		junit.framework.Assert.assertFalse(org.apache.commons.math.util.MathUtils.equals(new double[]{ 1.0 }, new double[]{ org.apache.commons.math.util.MathUtils.nextAfter(1.0, 2.0) }));264	}265	public void testFactorial() {266		for (int i = 1 ; i < 21 ; i++) {267			junit.framework.Assert.assertEquals((i + "! "), factorial(i), org.apache.commons.math.util.MathUtils.factorial(i));268			junit.framework.Assert.assertEquals((i + "! "), factorial(i), org.apache.commons.math.util.MathUtils.factorialDouble(i), java.lang.Double.MIN_VALUE);269			junit.framework.Assert.assertEquals((i + "! "), java.lang.Math.log(factorial(i)), org.apache.commons.math.util.MathUtils.factorialLog(i), 1.0E-11);270		}271		junit.framework.Assert.assertEquals("0", 1, org.apache.commons.math.util.MathUtils.factorial(0));272		junit.framework.Assert.assertEquals("0", 1.0, org.apache.commons.math.util.MathUtils.factorialDouble(0), 1.0E-14);273		junit.framework.Assert.assertEquals("0", 0.0, org.apache.commons.math.util.MathUtils.factorialLog(0), 1.0E-14);274	}275	public void testFactorialFail() {276		try {277			org.apache.commons.math.util.MathUtils.factorial(-1);278			junit.framework.Assert.fail("expecting IllegalArgumentException");279		} catch (java.lang.IllegalArgumentException ex) {280		}281		try {282			org.apache.commons.math.util.MathUtils.factorialDouble(-1);283			junit.framework.Assert.fail("expecting IllegalArgumentException");284		} catch (java.lang.IllegalArgumentException ex) {285		}286		try {287			org.apache.commons.math.util.MathUtils.factorialLog(-1);288			junit.framework.Assert.fail("expecting IllegalArgumentException");289		} catch (java.lang.IllegalArgumentException ex) {290		}291		try {292			org.apache.commons.math.util.MathUtils.factorial(21);293			junit.framework.Assert.fail("expecting ArithmeticException");294		} catch (java.lang.ArithmeticException ex) {295		}296		junit.framework.Assert.assertTrue("expecting infinite factorial value", java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.factorialDouble(171)));297	}298	public void testGcd() {299		int a = 30;300		int b = 50;301		int c = 77;302		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.gcd(0, 0));303		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.gcd(0, b));304		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.gcd(a, 0));305		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.gcd(0, -b));306		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.gcd(-a, 0));307		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(a, b));308		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(-a, b));309		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(a, -b));310		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(-a, -b));311		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(a, c));312		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(-a, c));313		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(a, -c));314		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(-a, -c));315		junit.framework.Assert.assertEquals((3 * (1 << 15)), org.apache.commons.math.util.MathUtils.gcd((3 * (1 << 20)), (9 * (1 << 15))));316		junit.framework.Assert.assertEquals(java.lang.Integer.MAX_VALUE, org.apache.commons.math.util.MathUtils.gcd(java.lang.Integer.MAX_VALUE, 0));317		junit.framework.Assert.assertEquals(java.lang.Integer.MAX_VALUE, org.apache.commons.math.util.MathUtils.gcd(-(java.lang.Integer.MAX_VALUE), 0));318		junit.framework.Assert.assertEquals((1 << 30), org.apache.commons.math.util.MathUtils.gcd((1 << 30), -(java.lang.Integer.MIN_VALUE)));319		try {320			org.apache.commons.math.util.MathUtils.gcd(java.lang.Integer.MIN_VALUE, 0);321			junit.framework.Assert.fail("expecting ArithmeticException");322		} catch (java.lang.ArithmeticException expected) {323		}324		try {325			org.apache.commons.math.util.MathUtils.gcd(0, java.lang.Integer.MIN_VALUE);326			junit.framework.Assert.fail("expecting ArithmeticException");327		} catch (java.lang.ArithmeticException expected) {328		}329		try {330			org.apache.commons.math.util.MathUtils.gcd(java.lang.Integer.MIN_VALUE, java.lang.Integer.MIN_VALUE);331			junit.framework.Assert.fail("expecting ArithmeticException");332		} catch (java.lang.ArithmeticException expected) {333		}334	}335	public void testGcdLong() {336		long a = 30;337		long b = 50;338		long c = 77;339		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.gcd(0L, 0));340		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.gcd(0, b));341		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.gcd(a, 0));342		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.gcd(0, -b));343		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.gcd(-a, 0));344		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(a, b));345		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(-a, b));346		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(a, -b));347		junit.framework.Assert.assertEquals(10, org.apache.commons.math.util.MathUtils.gcd(-a, -b));348		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(a, c));349		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(-a, c));350		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(a, -c));351		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(-a, -c));352		junit.framework.Assert.assertEquals((3L * (1L << 45)), org.apache.commons.math.util.MathUtils.gcd((3L * (1L << 50)), (9L * (1L << 45))));353		junit.framework.Assert.assertEquals((1L << 45), org.apache.commons.math.util.MathUtils.gcd((1L << 45), java.lang.Long.MIN_VALUE));354		junit.framework.Assert.assertEquals(java.lang.Long.MAX_VALUE, org.apache.commons.math.util.MathUtils.gcd(java.lang.Long.MAX_VALUE, 0L));355		junit.framework.Assert.assertEquals(java.lang.Long.MAX_VALUE, org.apache.commons.math.util.MathUtils.gcd(-(java.lang.Long.MAX_VALUE), 0L));356		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.gcd(60247241209L, 153092023L));357		try {358			org.apache.commons.math.util.MathUtils.gcd(java.lang.Long.MIN_VALUE, 0);359			junit.framework.Assert.fail("expecting ArithmeticException");360		} catch (java.lang.ArithmeticException expected) {361		}362		try {363			org.apache.commons.math.util.MathUtils.gcd(0, java.lang.Long.MIN_VALUE);364			junit.framework.Assert.fail("expecting ArithmeticException");365		} catch (java.lang.ArithmeticException expected) {366		}367		try {368			org.apache.commons.math.util.MathUtils.gcd(java.lang.Long.MIN_VALUE, java.lang.Long.MIN_VALUE);369			junit.framework.Assert.fail("expecting ArithmeticException");370		} catch (java.lang.ArithmeticException expected) {371		}372	}373	public void testGcdConsistency() {374		int[] primeList = new int[]{ 19 , 23 , 53 , 67 , 73 , 79 , 101 , 103 , 111 , 131 };375		java.util.ArrayList<java.lang.Integer> primes = new java.util.ArrayList<java.lang.Integer>();376		for (int i = 0 ; i < (primeList.length) ; i++) {377			primes.add(java.lang.Integer.valueOf(primeList[i]));378		}379		org.apache.commons.math.random.RandomDataImpl randomData = new org.apache.commons.math.random.RandomDataImpl();380		for (int i = 0 ; i < 20 ; i++) {381			java.lang.Object[] sample = randomData.nextSample(primes, 4);382			int p1 = ((java.lang.Integer)(sample[0])).intValue();383			int p2 = ((java.lang.Integer)(sample[1])).intValue();384			int p3 = ((java.lang.Integer)(sample[2])).intValue();385			int p4 = ((java.lang.Integer)(sample[3])).intValue();386			int i1 = (p1 * p2) * p3;387			int i2 = (p1 * p2) * p4;388			int gcd = p1 * p2;389			junit.framework.Assert.assertEquals(gcd, org.apache.commons.math.util.MathUtils.gcd(i1, i2));390			long l1 = i1;391			long l2 = i2;392			junit.framework.Assert.assertEquals(gcd, org.apache.commons.math.util.MathUtils.gcd(l1, l2));393		}394	}395	public void testHash() {396		double[] testArray = new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 , 1.0E-14 , 1 + 1.0E-14 , java.lang.Double.MIN_VALUE , java.lang.Double.MAX_VALUE };397		for (int i = 0 ; i < (testArray.length) ; i++) {398			for (int j = 0 ; j < (testArray.length) ; j++) {399				if (i == j) {400					junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.hash(testArray[i]), org.apache.commons.math.util.MathUtils.hash(testArray[j]));401					junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.hash(testArray[j]), org.apache.commons.math.util.MathUtils.hash(testArray[i]));402				} else {403					junit.framework.Assert.assertTrue(((org.apache.commons.math.util.MathUtils.hash(testArray[i])) != (org.apache.commons.math.util.MathUtils.hash(testArray[j]))));404					junit.framework.Assert.assertTrue(((org.apache.commons.math.util.MathUtils.hash(testArray[j])) != (org.apache.commons.math.util.MathUtils.hash(testArray[i]))));405				}406			}407		}408	}409	public void testArrayHash() {410		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.hash(((double[])(null))));411		junit.framework.Assert.assertEquals(org.apache.commons.math.util.MathUtils.hash(new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 }), org.apache.commons.math.util.MathUtils.hash(new double[]{ java.lang.Double.NaN , java.lang.Double.POSITIVE_INFINITY , java.lang.Double.NEGATIVE_INFINITY , 1.0 , 0.0 }));412		junit.framework.Assert.assertFalse(((org.apache.commons.math.util.MathUtils.hash(new double[]{ 1.0 })) == (org.apache.commons.math.util.MathUtils.hash(new double[]{ org.apache.commons.math.util.MathUtils.nextAfter(1.0, 2.0) }))));413		junit.framework.Assert.assertFalse(((org.apache.commons.math.util.MathUtils.hash(new double[]{ 1.0 })) == (org.apache.commons.math.util.MathUtils.hash(new double[]{ 1.0 , 1.0 }))));414	}415	public void testPermutedArrayHash() {416		double[] original = new double[10];417		double[] permuted = new double[10];418		org.apache.commons.math.random.RandomDataImpl random = new org.apache.commons.math.random.RandomDataImpl();419		for (int i = 0 ; i < 10 ; i++) {420			original[i] = random.nextUniform((i + 0.5), (i + 0.75));421		}422		boolean isIdentity = true;423		do {424			int[] permutation = random.nextPermutation(10, 10);425			for (int i = 0 ; i < 10 ; i++) {426				if (i != (permutation[i])) {427					isIdentity = false;428				} 429				permuted[i] = original[permutation[i]];430			}431		} while (isIdentity );432		junit.framework.Assert.assertFalse(((org.apache.commons.math.util.MathUtils.hash(original)) == (org.apache.commons.math.util.MathUtils.hash(permuted))));433	}434	public void testIndicatorByte() {435		junit.framework.Assert.assertEquals(((byte)(1)), org.apache.commons.math.util.MathUtils.indicator(((byte)(2))));436		junit.framework.Assert.assertEquals(((byte)(1)), org.apache.commons.math.util.MathUtils.indicator(((byte)(0))));437		junit.framework.Assert.assertEquals(((byte)(-1)), org.apache.commons.math.util.MathUtils.indicator(((byte)(-2))));438	}439	public void testIndicatorDouble() {440		double delta = 0.0;441		junit.framework.Assert.assertEquals(1.0, org.apache.commons.math.util.MathUtils.indicator(2.0), delta);442		junit.framework.Assert.assertEquals(1.0, org.apache.commons.math.util.MathUtils.indicator(0.0), delta);443		junit.framework.Assert.assertEquals(-1.0, org.apache.commons.math.util.MathUtils.indicator(-2.0), delta);444		junit.framework.Assert.assertEquals(java.lang.Double.NaN, org.apache.commons.math.util.MathUtils.indicator(java.lang.Double.NaN));445	}446	public void testIndicatorFloat() {447		float delta = 0.0F;448		junit.framework.Assert.assertEquals(1.0F, org.apache.commons.math.util.MathUtils.indicator(2.0F), delta);449		junit.framework.Assert.assertEquals(1.0F, org.apache.commons.math.util.MathUtils.indicator(0.0F), delta);450		junit.framework.Assert.assertEquals(-1.0F, org.apache.commons.math.util.MathUtils.indicator(-2.0F), delta);451	}452	public void testIndicatorInt() {453		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.indicator(2));454		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.indicator(0));455		junit.framework.Assert.assertEquals(-1, org.apache.commons.math.util.MathUtils.indicator(-2));456	}457	public void testIndicatorLong() {458		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.indicator(2L));459		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.indicator(0L));460		junit.framework.Assert.assertEquals(-1L, org.apache.commons.math.util.MathUtils.indicator(-2L));461	}462	public void testIndicatorShort() {463		junit.framework.Assert.assertEquals(((short)(1)), org.apache.commons.math.util.MathUtils.indicator(((short)(2))));464		junit.framework.Assert.assertEquals(((short)(1)), org.apache.commons.math.util.MathUtils.indicator(((short)(0))));465		junit.framework.Assert.assertEquals(((short)(-1)), org.apache.commons.math.util.MathUtils.indicator(((short)(-2))));466	}467	public void testLcm() {468		int a = 30;469		int b = 50;470		int c = 77;471		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.lcm(0, b));472		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.lcm(a, 0));473		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.lcm(1, b));474		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.lcm(a, 1));475		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(a, b));476		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(-a, b));477		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(a, -b));478		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(-a, -b));479		junit.framework.Assert.assertEquals(2310, org.apache.commons.math.util.MathUtils.lcm(a, c));480		junit.framework.Assert.assertEquals(((1 << 20) * 15), org.apache.commons.math.util.MathUtils.lcm(((1 << 20) * 3), ((1 << 20) * 5)));481		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.lcm(0, 0));482		try {483			org.apache.commons.math.util.MathUtils.lcm(java.lang.Integer.MIN_VALUE, 1);484			junit.framework.Assert.fail("Expecting ArithmeticException");485		} catch (java.lang.ArithmeticException expected) {486		}487		try {488			org.apache.commons.math.util.MathUtils.lcm(java.lang.Integer.MIN_VALUE, (1 << 20));489			junit.framework.Assert.fail("Expecting ArithmeticException");490		} catch (java.lang.ArithmeticException expected) {491		}492		try {493			org.apache.commons.math.util.MathUtils.lcm(java.lang.Integer.MAX_VALUE, ((java.lang.Integer.MAX_VALUE) - 1));494			junit.framework.Assert.fail("Expecting ArithmeticException");495		} catch (java.lang.ArithmeticException expected) {496		}497	}498	public void testLcmLong() {499		long a = 30;500		long b = 50;501		long c = 77;502		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.lcm(0, b));503		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.lcm(a, 0));504		junit.framework.Assert.assertEquals(b, org.apache.commons.math.util.MathUtils.lcm(1, b));505		junit.framework.Assert.assertEquals(a, org.apache.commons.math.util.MathUtils.lcm(a, 1));506		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(a, b));507		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(-a, b));508		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(a, -b));509		junit.framework.Assert.assertEquals(150, org.apache.commons.math.util.MathUtils.lcm(-a, -b));510		junit.framework.Assert.assertEquals(2310, org.apache.commons.math.util.MathUtils.lcm(a, c));511		junit.framework.Assert.assertEquals(java.lang.Long.MAX_VALUE, org.apache.commons.math.util.MathUtils.lcm(60247241209L, 153092023L));512		junit.framework.Assert.assertEquals(((1L << 50) * 15), org.apache.commons.math.util.MathUtils.lcm(((1L << 45) * 3), ((1L << 50) * 5)));513		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.lcm(0L, 0L));514		try {515			org.apache.commons.math.util.MathUtils.lcm(java.lang.Long.MIN_VALUE, 1);516			junit.framework.Assert.fail("Expecting ArithmeticException");517		} catch (java.lang.ArithmeticException expected) {518		}519		try {520			org.apache.commons.math.util.MathUtils.lcm(java.lang.Long.MIN_VALUE, (1 << 20));521			junit.framework.Assert.fail("Expecting ArithmeticException");522		} catch (java.lang.ArithmeticException expected) {523		}524		junit.framework.Assert.assertEquals((((long)(java.lang.Integer.MAX_VALUE)) * ((java.lang.Integer.MAX_VALUE) - 1)), org.apache.commons.math.util.MathUtils.lcm(((long)(java.lang.Integer.MAX_VALUE)), ((java.lang.Integer.MAX_VALUE) - 1)));525		try {526			org.apache.commons.math.util.MathUtils.lcm(java.lang.Long.MAX_VALUE, ((java.lang.Long.MAX_VALUE) - 1));527			junit.framework.Assert.fail("Expecting ArithmeticException");528		} catch (java.lang.ArithmeticException expected) {529		}530	}531	public void testLog() {532		junit.framework.Assert.assertEquals(2.0, org.apache.commons.math.util.MathUtils.log(2, 4), 0);533		junit.framework.Assert.assertEquals(3.0, org.apache.commons.math.util.MathUtils.log(2, 8), 0);534		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.log(-1, 1)));535		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.log(1, -1)));536		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.log(0, 0)));537		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.log(0, 10), 0);538		junit.framework.Assert.assertEquals(java.lang.Double.NEGATIVE_INFINITY, org.apache.commons.math.util.MathUtils.log(10, 0), 0);539	}540	public void testMulAndCheck() {541		int big = java.lang.Integer.MAX_VALUE;542		int bigNeg = java.lang.Integer.MIN_VALUE;543		junit.framework.Assert.assertEquals(big, org.apache.commons.math.util.MathUtils.mulAndCheck(big, 1));544		try {545			org.apache.commons.math.util.MathUtils.mulAndCheck(big, 2);546			junit.framework.Assert.fail("Expecting ArithmeticException");547		} catch (java.lang.ArithmeticException ex) {548		}549		try {550			org.apache.commons.math.util.MathUtils.mulAndCheck(bigNeg, 2);551			junit.framework.Assert.fail("Expecting ArithmeticException");552		} catch (java.lang.ArithmeticException ex) {553		}554	}555	public void testMulAndCheckLong() {556		long max = java.lang.Long.MAX_VALUE;557		long min = java.lang.Long.MIN_VALUE;558		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.mulAndCheck(max, 1L));559		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.mulAndCheck(min, 1L));560		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.mulAndCheck(max, 0L));561		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.mulAndCheck(min, 0L));562		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.mulAndCheck(1L, max));563		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.mulAndCheck(1L, min));564		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.mulAndCheck(0L, max));565		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.mulAndCheck(0L, min));566		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.mulAndCheck(-1L, -1L));567		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.mulAndCheck((min / 2), 2));568		testMulAndCheckLongFailure(max, 2L);569		testMulAndCheckLongFailure(2L, max);570		testMulAndCheckLongFailure(min, 2L);571		testMulAndCheckLongFailure(2L, min);572		testMulAndCheckLongFailure(min, -1L);573		testMulAndCheckLongFailure(-1L, min);574	}575	private void testMulAndCheckLongFailure(long a, long b) {576		try {577			org.apache.commons.math.util.MathUtils.mulAndCheck(a, b);578			junit.framework.Assert.fail("Expecting ArithmeticException");579		} catch (java.lang.ArithmeticException ex) {580		}581	}582	public void testNextAfter() {583		junit.framework.Assert.assertEquals(16.0, org.apache.commons.math.util.MathUtils.nextAfter(15.999999999999998, 34.27555555555555), 0.0);584		junit.framework.Assert.assertEquals(-15.999999999999996, org.apache.commons.math.util.MathUtils.nextAfter(-15.999999999999998, 34.27555555555555), 0.0);585		junit.framework.Assert.assertEquals(15.999999999999996, org.apache.commons.math.util.MathUtils.nextAfter(15.999999999999998, 2.142222222222222), 0.0);586		junit.framework.Assert.assertEquals(-15.999999999999996, org.apache.commons.math.util.MathUtils.nextAfter(-15.999999999999998, 2.142222222222222), 0.0);587		junit.framework.Assert.assertEquals(8.000000000000002, org.apache.commons.math.util.MathUtils.nextAfter(8.0, 34.27555555555555), 0.0);588		junit.framework.Assert.assertEquals(-7.999999999999999, org.apache.commons.math.util.MathUtils.nextAfter(-8.0, 34.27555555555555), 0.0);589		junit.framework.Assert.assertEquals(7.999999999999999, org.apache.commons.math.util.MathUtils.nextAfter(8.0, 2.142222222222222), 0.0);590		junit.framework.Assert.assertEquals(-7.999999999999999, org.apache.commons.math.util.MathUtils.nextAfter(-8.0, 2.142222222222222), 0.0);591		junit.framework.Assert.assertEquals(2.308922399667661E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, 2.308922399667661E-4), 0.0);592		junit.framework.Assert.assertEquals(2.308922399667661E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, 2.3089223996676606E-4), 0.0);593		junit.framework.Assert.assertEquals(2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, 2.3089223996676603E-4), 0.0);594		junit.framework.Assert.assertEquals(2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, -2.308922399667661E-4), 0.0);595		junit.framework.Assert.assertEquals(2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, -2.3089223996676606E-4), 0.0);596		junit.framework.Assert.assertEquals(2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(2.3089223996676606E-4, -2.3089223996676603E-4), 0.0);597		junit.framework.Assert.assertEquals(-2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, 2.308922399667661E-4), 0.0);598		junit.framework.Assert.assertEquals(-2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, 2.3089223996676606E-4), 0.0);599		junit.framework.Assert.assertEquals(-2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, 2.3089223996676603E-4), 0.0);600		junit.framework.Assert.assertEquals(-2.308922399667661E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, -2.308922399667661E-4), 0.0);601		junit.framework.Assert.assertEquals(-2.308922399667661E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, -2.3089223996676606E-4), 0.0);602		junit.framework.Assert.assertEquals(-2.3089223996676603E-4, org.apache.commons.math.util.MathUtils.nextAfter(-2.3089223996676606E-4, -2.3089223996676603E-4), 0.0);603	}604	public void testNextAfterSpecialCases() {605		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.nextAfter(java.lang.Double.NEGATIVE_INFINITY, 0)));606		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.nextAfter(java.lang.Double.POSITIVE_INFINITY, 0)));607		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.nextAfter(java.lang.Double.NaN, 0)));608		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.nextAfter(java.lang.Double.MAX_VALUE, java.lang.Double.POSITIVE_INFINITY)));609		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.nextAfter(-(java.lang.Double.MAX_VALUE), java.lang.Double.NEGATIVE_INFINITY)));610		junit.framework.Assert.assertEquals(java.lang.Double.MIN_VALUE, org.apache.commons.math.util.MathUtils.nextAfter(0, 1), 0);611		junit.framework.Assert.assertEquals(-(java.lang.Double.MIN_VALUE), org.apache.commons.math.util.MathUtils.nextAfter(0, -1), 0);612		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.nextAfter(java.lang.Double.MIN_VALUE, -1), 0);613		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.nextAfter(-(java.lang.Double.MIN_VALUE), 1), 0);614	}615	public void testScalb() {616		junit.framework.Assert.assertEquals(0.0, org.apache.commons.math.util.MathUtils.scalb(0.0, 5), 1.0E-15);617		junit.framework.Assert.assertEquals(32.0, org.apache.commons.math.util.MathUtils.scalb(1.0, 5), 1.0E-15);618		junit.framework.Assert.assertEquals((1.0 / 32.0), org.apache.commons.math.util.MathUtils.scalb(1.0, -5), 1.0E-15);619		junit.framework.Assert.assertEquals(java.lang.Math.PI, org.apache.commons.math.util.MathUtils.scalb(java.lang.Math.PI, 0), 1.0E-15);620		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.scalb(java.lang.Double.POSITIVE_INFINITY, 1)));621		junit.framework.Assert.assertTrue(java.lang.Double.isInfinite(org.apache.commons.math.util.MathUtils.scalb(java.lang.Double.NEGATIVE_INFINITY, 1)));622		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.scalb(java.lang.Double.NaN, 1)));623	}624	public void testNormalizeAngle() {625		for (double a = -15.0 ; a <= 15.0 ; a += 0.1) {626			for (double b = -15.0 ; b <= 15.0 ; b += 0.2) {627				double c = org.apache.commons.math.util.MathUtils.normalizeAngle(a, b);628				junit.framework.Assert.assertTrue(((b - (java.lang.Math.PI)) <= c));629				junit.framework.Assert.assertTrue((c <= (b + (java.lang.Math.PI))));630				double twoK = java.lang.Math.rint(((a - c) / (java.lang.Math.PI)));631				junit.framework.Assert.assertEquals(c, (a - (twoK * (java.lang.Math.PI))), 1.0E-14);632			}633		}634	}635	public void testNormalizeArray() {636		double[] testValues1 = new double[]{ 1 , 1 , 2 };637		org.apache.commons.math.TestUtils.assertEquals(new double[]{ 0.25 , 0.25 , 0.5 }, org.apache.commons.math.util.MathUtils.normalizeArray(testValues1, 1), java.lang.Double.MIN_VALUE);638		double[] testValues2 = new double[]{ -1 , -1 , 1 };639		org.apache.commons.math.TestUtils.assertEquals(new double[]{ 1 , 1 , -1 }, org.apache.commons.math.util.MathUtils.normalizeArray(testValues2, 1), java.lang.Double.MIN_VALUE);640		double[] testValues3 = new double[]{ -1 , -1 , java.lang.Double.NaN , 1 , java.lang.Double.NaN };641		org.apache.commons.math.TestUtils.assertEquals(new double[]{ 1 , 1 , java.lang.Double.NaN , -1 , java.lang.Double.NaN }, org.apache.commons.math.util.MathUtils.normalizeArray(testValues3, 1), java.lang.Double.MIN_VALUE);642		double[] zeroSum = new double[]{ -1 , 1 };643		try {644			org.apache.commons.math.util.MathUtils.normalizeArray(zeroSum, 1);645			junit.framework.Assert.fail("expecting ArithmeticException");646		} catch (java.lang.ArithmeticException ex) {647		}648		double[] hasInf = new double[]{ 1 , 2 , 1 , java.lang.Double.NEGATIVE_INFINITY };649		try {650			org.apache.commons.math.util.MathUtils.normalizeArray(hasInf, 1);651			junit.framework.Assert.fail("expecting ArithmeticException");652		} catch (java.lang.ArithmeticException ex) {653		}654		try {655			org.apache.commons.math.util.MathUtils.normalizeArray(testValues1, java.lang.Double.POSITIVE_INFINITY);656			junit.framework.Assert.fail("expecting IllegalArgumentException");657		} catch (java.lang.IllegalArgumentException ex) {658		}659		try {660			org.apache.commons.math.util.MathUtils.normalizeArray(testValues1, java.lang.Double.NaN);661			junit.framework.Assert.fail("expecting IllegalArgumentException");662		} catch (java.lang.IllegalArgumentException ex) {663		}664	}665	public void testRoundDouble() {666		double x = 1.23456789;667		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2), 0.0);668		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3), 0.0);669		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4), 0.0);670		junit.framework.Assert.assertEquals(39.25, org.apache.commons.math.util.MathUtils.round(39.245, 2), 0.0);671		junit.framework.Assert.assertEquals(39.24, org.apache.commons.math.util.MathUtils.round(39.245, 2, java.math.BigDecimal.ROUND_DOWN), 0.0);672		double xx = 39.0;673		xx = xx + (245.0 / 1000.0);674		junit.framework.Assert.assertEquals(39.25, org.apache.commons.math.util.MathUtils.round(xx, 2), 0.0);675		junit.framework.Assert.assertEquals(30.1, org.apache.commons.math.util.MathUtils.round(30.095, 2), 0.0);676		junit.framework.Assert.assertEquals(30.1, org.apache.commons.math.util.MathUtils.round(30.095, 1), 0.0);677		junit.framework.Assert.assertEquals(33.1, org.apache.commons.math.util.MathUtils.round(33.095, 1), 0.0);678		junit.framework.Assert.assertEquals(33.1, org.apache.commons.math.util.MathUtils.round(33.095, 2), 0.0);679		junit.framework.Assert.assertEquals(50.09, org.apache.commons.math.util.MathUtils.round(50.085, 2), 0.0);680		junit.framework.Assert.assertEquals(50.19, org.apache.commons.math.util.MathUtils.round(50.185, 2), 0.0);681		junit.framework.Assert.assertEquals(50.01, org.apache.commons.math.util.MathUtils.round(50.005, 2), 0.0);682		junit.framework.Assert.assertEquals(30.01, org.apache.commons.math.util.MathUtils.round(30.005, 2), 0.0);683		junit.framework.Assert.assertEquals(30.65, org.apache.commons.math.util.MathUtils.round(30.645, 2), 0.0);684		junit.framework.Assert.assertEquals(1.24, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_CEILING), 0.0);685		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_CEILING), 0.0);686		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_CEILING), 0.0);687		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_CEILING), 0.0);688		junit.framework.Assert.assertEquals(-1.234, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_CEILING), 0.0);689		junit.framework.Assert.assertEquals(-1.2345, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_CEILING), 0.0);690		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_DOWN), 0.0);691		junit.framework.Assert.assertEquals(1.234, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_DOWN), 0.0);692		junit.framework.Assert.assertEquals(1.2345, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_DOWN), 0.0);693		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_DOWN), 0.0);694		junit.framework.Assert.assertEquals(-1.234, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_DOWN), 0.0);695		junit.framework.Assert.assertEquals(-1.2345, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_DOWN), 0.0);696		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_FLOOR), 0.0);697		junit.framework.Assert.assertEquals(1.234, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_FLOOR), 0.0);698		junit.framework.Assert.assertEquals(1.2345, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_FLOOR), 0.0);699		junit.framework.Assert.assertEquals(-1.24, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_FLOOR), 0.0);700		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_FLOOR), 0.0);701		junit.framework.Assert.assertEquals(-1.2346, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_FLOOR), 0.0);702		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);703		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);704		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);705		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);706		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);707		junit.framework.Assert.assertEquals(-1.2346, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);708		junit.framework.Assert.assertEquals(1.234, org.apache.commons.math.util.MathUtils.round(1.2345, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);709		junit.framework.Assert.assertEquals(-1.234, org.apache.commons.math.util.MathUtils.round(-1.2345, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);710		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);711		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);712		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);713		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);714		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);715		junit.framework.Assert.assertEquals(-1.2346, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);716		junit.framework.Assert.assertEquals(1.234, org.apache.commons.math.util.MathUtils.round(1.2345, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);717		junit.framework.Assert.assertEquals(-1.234, org.apache.commons.math.util.MathUtils.round(-1.2345, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);718		junit.framework.Assert.assertEquals(1.236, org.apache.commons.math.util.MathUtils.round(1.2355, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);719		junit.framework.Assert.assertEquals(-1.236, org.apache.commons.math.util.MathUtils.round(-1.2355, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);720		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_UP), 0.0);721		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);722		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_UP), 0.0);723		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_UP), 0.0);724		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);725		junit.framework.Assert.assertEquals(-1.2346, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_UP), 0.0);726		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(1.2345, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);727		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-1.2345, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);728		junit.framework.Assert.assertEquals(-1.23, org.apache.commons.math.util.MathUtils.round(-1.23, 2, java.math.BigDecimal.ROUND_UNNECESSARY), 0.0);729		junit.framework.Assert.assertEquals(1.23, org.apache.commons.math.util.MathUtils.round(1.23, 2, java.math.BigDecimal.ROUND_UNNECESSARY), 0.0);730		try {731			org.apache.commons.math.util.MathUtils.round(1.234, 2, java.math.BigDecimal.ROUND_UNNECESSARY);732			junit.framework.Assert.fail();733		} catch (java.lang.ArithmeticException ex) {734		}735		junit.framework.Assert.assertEquals(1.24, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_UP), 0.0);736		junit.framework.Assert.assertEquals(1.235, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_UP), 0.0);737		junit.framework.Assert.assertEquals(1.2346, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_UP), 0.0);738		junit.framework.Assert.assertEquals(-1.24, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_UP), 0.0);739		junit.framework.Assert.assertEquals(-1.235, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_UP), 0.0);740		junit.framework.Assert.assertEquals(-1.2346, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_UP), 0.0);741		try {742			org.apache.commons.math.util.MathUtils.round(1.234, 2, 1923);743			junit.framework.Assert.fail();744		} catch (java.lang.IllegalArgumentException ex) {745		}746		junit.framework.Assert.assertEquals(39.25, org.apache.commons.math.util.MathUtils.round(39.245, 2, java.math.BigDecimal.ROUND_HALF_UP), 0.0);747		org.apache.commons.math.TestUtils.assertEquals(java.lang.Double.NaN, org.apache.commons.math.util.MathUtils.round(java.lang.Double.NaN, 2), 0.0);748		junit.framework.Assert.assertEquals(0.0, org.apache.commons.math.util.MathUtils.round(0.0, 2), 0.0);749		junit.framework.Assert.assertEquals(java.lang.Double.POSITIVE_INFINITY, org.apache.commons.math.util.MathUtils.round(java.lang.Double.POSITIVE_INFINITY, 2), 0.0);750		junit.framework.Assert.assertEquals(java.lang.Double.NEGATIVE_INFINITY, org.apache.commons.math.util.MathUtils.round(java.lang.Double.NEGATIVE_INFINITY, 2), 0.0);751	}752	public void testRoundFloat() {753		float x = 1.2345679F;754		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2), 0.0);755		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3), 0.0);756		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4), 0.0);757		junit.framework.Assert.assertEquals(30.1F, org.apache.commons.math.util.MathUtils.round(30.095F, 2), 0.0F);758		junit.framework.Assert.assertEquals(30.1F, org.apache.commons.math.util.MathUtils.round(30.095F, 1), 0.0F);759		junit.framework.Assert.assertEquals(50.09F, org.apache.commons.math.util.MathUtils.round(50.085F, 2), 0.0F);760		junit.framework.Assert.assertEquals(50.19F, org.apache.commons.math.util.MathUtils.round(50.185F, 2), 0.0F);761		junit.framework.Assert.assertEquals(50.01F, org.apache.commons.math.util.MathUtils.round(50.005F, 2), 0.0F);762		junit.framework.Assert.assertEquals(30.01F, org.apache.commons.math.util.MathUtils.round(30.005F, 2), 0.0F);763		junit.framework.Assert.assertEquals(30.65F, org.apache.commons.math.util.MathUtils.round(30.645F, 2), 0.0F);764		junit.framework.Assert.assertEquals(1.24F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_CEILING), 0.0);765		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_CEILING), 0.0);766		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_CEILING), 0.0);767		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_CEILING), 0.0);768		junit.framework.Assert.assertEquals(-1.234F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_CEILING), 0.0);769		junit.framework.Assert.assertEquals(-1.2345F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_CEILING), 0.0);770		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_DOWN), 0.0);771		junit.framework.Assert.assertEquals(1.234F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_DOWN), 0.0);772		junit.framework.Assert.assertEquals(1.2345F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_DOWN), 0.0);773		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_DOWN), 0.0);774		junit.framework.Assert.assertEquals(-1.234F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_DOWN), 0.0);775		junit.framework.Assert.assertEquals(-1.2345F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_DOWN), 0.0);776		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_FLOOR), 0.0);777		junit.framework.Assert.assertEquals(1.234F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_FLOOR), 0.0);778		junit.framework.Assert.assertEquals(1.2345F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_FLOOR), 0.0);779		junit.framework.Assert.assertEquals(-1.24F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_FLOOR), 0.0);780		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_FLOOR), 0.0);781		junit.framework.Assert.assertEquals(-1.2346F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_FLOOR), 0.0);782		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);783		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);784		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);785		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);786		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);787		junit.framework.Assert.assertEquals(-1.2346F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);788		junit.framework.Assert.assertEquals(1.234F, org.apache.commons.math.util.MathUtils.round(1.2345F, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);789		junit.framework.Assert.assertEquals(-1.234F, org.apache.commons.math.util.MathUtils.round(-1.2345F, 3, java.math.BigDecimal.ROUND_HALF_DOWN), 0.0);790		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);791		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);792		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);793		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);794		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);795		junit.framework.Assert.assertEquals(-1.2346F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);796		junit.framework.Assert.assertEquals(1.234F, org.apache.commons.math.util.MathUtils.round(1.2345F, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);797		junit.framework.Assert.assertEquals(-1.234F, org.apache.commons.math.util.MathUtils.round(-1.2345F, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);798		junit.framework.Assert.assertEquals(1.236F, org.apache.commons.math.util.MathUtils.round(1.2355F, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);799		junit.framework.Assert.assertEquals(-1.236F, org.apache.commons.math.util.MathUtils.round(-1.2355F, 3, java.math.BigDecimal.ROUND_HALF_EVEN), 0.0);800		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_HALF_UP), 0.0);801		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);802		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_HALF_UP), 0.0);803		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_HALF_UP), 0.0);804		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);805		junit.framework.Assert.assertEquals(-1.2346F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_HALF_UP), 0.0);806		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(1.2345F, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);807		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-1.2345F, 3, java.math.BigDecimal.ROUND_HALF_UP), 0.0);808		junit.framework.Assert.assertEquals(-1.23F, org.apache.commons.math.util.MathUtils.round(-1.23F, 2, java.math.BigDecimal.ROUND_UNNECESSARY), 0.0);809		junit.framework.Assert.assertEquals(1.23F, org.apache.commons.math.util.MathUtils.round(1.23F, 2, java.math.BigDecimal.ROUND_UNNECESSARY), 0.0);810		try {811			org.apache.commons.math.util.MathUtils.round(1.234F, 2, java.math.BigDecimal.ROUND_UNNECESSARY);812			junit.framework.Assert.fail();813		} catch (java.lang.ArithmeticException ex) {814		}815		junit.framework.Assert.assertEquals(1.24F, org.apache.commons.math.util.MathUtils.round(x, 2, java.math.BigDecimal.ROUND_UP), 0.0);816		junit.framework.Assert.assertEquals(1.235F, org.apache.commons.math.util.MathUtils.round(x, 3, java.math.BigDecimal.ROUND_UP), 0.0);817		junit.framework.Assert.assertEquals(1.2346F, org.apache.commons.math.util.MathUtils.round(x, 4, java.math.BigDecimal.ROUND_UP), 0.0);818		junit.framework.Assert.assertEquals(-1.24F, org.apache.commons.math.util.MathUtils.round(-x, 2, java.math.BigDecimal.ROUND_UP), 0.0);819		junit.framework.Assert.assertEquals(-1.235F, org.apache.commons.math.util.MathUtils.round(-x, 3, java.math.BigDecimal.ROUND_UP), 0.0);820		junit.framework.Assert.assertEquals(-1.2346F, org.apache.commons.math.util.MathUtils.round(-x, 4, java.math.BigDecimal.ROUND_UP), 0.0);821		try {822			org.apache.commons.math.util.MathUtils.round(1.234F, 2, 1923);823			junit.framework.Assert.fail();824		} catch (java.lang.IllegalArgumentException ex) {825		}826		org.apache.commons.math.TestUtils.assertEquals(java.lang.Float.NaN, org.apache.commons.math.util.MathUtils.round(java.lang.Float.NaN, 2), 0.0F);827		junit.framework.Assert.assertEquals(0.0F, org.apache.commons.math.util.MathUtils.round(0.0F, 2), 0.0F);828		junit.framework.Assert.assertEquals(java.lang.Float.POSITIVE_INFINITY, org.apache.commons.math.util.MathUtils.round(java.lang.Float.POSITIVE_INFINITY, 2), 0.0F);829		junit.framework.Assert.assertEquals(java.lang.Float.NEGATIVE_INFINITY, org.apache.commons.math.util.MathUtils.round(java.lang.Float.NEGATIVE_INFINITY, 2), 0.0F);830	}831	public void testSignByte() {832		junit.framework.Assert.assertEquals(((byte)(1)), org.apache.commons.math.util.MathUtils.sign(((byte)(2))));833		junit.framework.Assert.assertEquals(((byte)(0)), org.apache.commons.math.util.MathUtils.sign(((byte)(0))));834		junit.framework.Assert.assertEquals(((byte)(-1)), org.apache.commons.math.util.MathUtils.sign(((byte)(-2))));835	}836	public void testSignDouble() {837		double delta = 0.0;838		junit.framework.Assert.assertEquals(1.0, org.apache.commons.math.util.MathUtils.sign(2.0), delta);839		junit.framework.Assert.assertEquals(0.0, org.apache.commons.math.util.MathUtils.sign(0.0), delta);840		junit.framework.Assert.assertEquals(-1.0, org.apache.commons.math.util.MathUtils.sign(-2.0), delta);841		org.apache.commons.math.TestUtils.assertSame(((-0.0) / 0.0), org.apache.commons.math.util.MathUtils.sign(java.lang.Double.NaN));842	}843	public void testSignFloat() {844		float delta = 0.0F;845		junit.framework.Assert.assertEquals(1.0F, org.apache.commons.math.util.MathUtils.sign(2.0F), delta);846		junit.framework.Assert.assertEquals(0.0F, org.apache.commons.math.util.MathUtils.sign(0.0F), delta);847		junit.framework.Assert.assertEquals(-1.0F, org.apache.commons.math.util.MathUtils.sign(-2.0F), delta);848		org.apache.commons.math.TestUtils.assertSame(java.lang.Float.NaN, org.apache.commons.math.util.MathUtils.sign(java.lang.Float.NaN));849	}850	public void testSignInt() {851		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.sign(2));852		junit.framework.Assert.assertEquals(0, org.apache.commons.math.util.MathUtils.sign(0));853		junit.framework.Assert.assertEquals(-1, org.apache.commons.math.util.MathUtils.sign(-2));854	}855	public void testSignLong() {856		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.sign(2L));857		junit.framework.Assert.assertEquals(0L, org.apache.commons.math.util.MathUtils.sign(0L));858		junit.framework.Assert.assertEquals(-1L, org.apache.commons.math.util.MathUtils.sign(-2L));859	}860	public void testSignShort() {861		junit.framework.Assert.assertEquals(((short)(1)), org.apache.commons.math.util.MathUtils.sign(((short)(2))));862		junit.framework.Assert.assertEquals(((short)(0)), org.apache.commons.math.util.MathUtils.sign(((short)(0))));863		junit.framework.Assert.assertEquals(((short)(-1)), org.apache.commons.math.util.MathUtils.sign(((short)(-2))));864	}865	public void testSinh() {866		double x = 3.0;867		double expected = 10.01787;868		junit.framework.Assert.assertEquals(expected, org.apache.commons.math.util.MathUtils.sinh(x), 1.0E-5);869	}870	public void testSinhNaN() {871		junit.framework.Assert.assertTrue(java.lang.Double.isNaN(org.apache.commons.math.util.MathUtils.sinh(java.lang.Double.NaN)));872	}873	public void testSubAndCheck() {874		int big = java.lang.Integer.MAX_VALUE;875		int bigNeg = java.lang.Integer.MIN_VALUE;876		junit.framework.Assert.assertEquals(big, org.apache.commons.math.util.MathUtils.subAndCheck(big, 0));877		junit.framework.Assert.assertEquals((bigNeg + 1), org.apache.commons.math.util.MathUtils.subAndCheck(bigNeg, -1));878		junit.framework.Assert.assertEquals(-1, org.apache.commons.math.util.MathUtils.subAndCheck(bigNeg, -big));879		try {880			org.apache.commons.math.util.MathUtils.subAndCheck(big, -1);881			junit.framework.Assert.fail("Expecting ArithmeticException");882		} catch (java.lang.ArithmeticException ex) {883		}884		try {885			org.apache.commons.math.util.MathUtils.subAndCheck(bigNeg, 1);886			junit.framework.Assert.fail("Expecting ArithmeticException");887		} catch (java.lang.ArithmeticException ex) {888		}889	}890	public void testSubAndCheckErrorMessage() {891		int big = java.lang.Integer.MAX_VALUE;892		try {893			org.apache.commons.math.util.MathUtils.subAndCheck(big, -1);894			junit.framework.Assert.fail("Expecting ArithmeticException");895		} catch (java.lang.ArithmeticException ex) {896			junit.framework.Assert.assertEquals("overflow: subtract", ex.getMessage());897		}898	}899	public void testSubAndCheckLong() {900		long max = java.lang.Long.MAX_VALUE;901		long min = java.lang.Long.MIN_VALUE;902		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.subAndCheck(max, 0));903		junit.framework.Assert.assertEquals(min, org.apache.commons.math.util.MathUtils.subAndCheck(min, 0));904		junit.framework.Assert.assertEquals(-max, org.apache.commons.math.util.MathUtils.subAndCheck(0, max));905		junit.framework.Assert.assertEquals((min + 1), org.apache.commons.math.util.MathUtils.subAndCheck(min, -1));906		junit.framework.Assert.assertEquals(-1, org.apache.commons.math.util.MathUtils.subAndCheck(((-max) - 1), -max));907		junit.framework.Assert.assertEquals(max, org.apache.commons.math.util.MathUtils.subAndCheck(-1, ((-1) - max)));908		testSubAndCheckLongFailure(0L, min);909		testSubAndCheckLongFailure(max, -1L);910		testSubAndCheckLongFailure(min, 1L);911	}912	private void testSubAndCheckLongFailure(long a, long b) {913		try {914			org.apache.commons.math.util.MathUtils.subAndCheck(a, b);915			junit.framework.Assert.fail("Expecting ArithmeticException");916		} catch (java.lang.ArithmeticException ex) {917		}918	}919	public void testPow() {920		junit.framework.Assert.assertEquals(1801088541, org.apache.commons.math.util.MathUtils.pow(21, 7));921		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.pow(21, 0));922		try {923			org.apache.commons.math.util.MathUtils.pow(21, -7);924			junit.framework.Assert.fail("Expecting IllegalArgumentException");925		} catch (java.lang.IllegalArgumentException e) {926		}927		junit.framework.Assert.assertEquals(1801088541, org.apache.commons.math.util.MathUtils.pow(21, 7L));928		junit.framework.Assert.assertEquals(1, org.apache.commons.math.util.MathUtils.pow(21, 0L));929		try {930			org.apache.commons.math.util.MathUtils.pow(21, -7L);931			junit.framework.Assert.fail("Expecting IllegalArgumentException");932		} catch (java.lang.IllegalArgumentException e) {933		}934		junit.framework.Assert.assertEquals(1801088541L, org.apache.commons.math.util.MathUtils.pow(21L, 7));935		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.pow(21L, 0));936		try {937			org.apache.commons.math.util.MathUtils.pow(21L, -7);938			junit.framework.Assert.fail("Expecting IllegalArgumentException");939		} catch (java.lang.IllegalArgumentException e) {940		}941		junit.framework.Assert.assertEquals(1801088541L, org.apache.commons.math.util.MathUtils.pow(21L, 7L));942		junit.framework.Assert.assertEquals(1L, org.apache.commons.math.util.MathUtils.pow(21L, 0L));943		try {944			org.apache.commons.math.util.MathUtils.pow(21L, -7L);945			junit.framework.Assert.fail("Expecting IllegalArgumentException");946		} catch (java.lang.IllegalArgumentException e) {947		}948		java.math.BigInteger twentyOne = java.math.BigInteger.valueOf(21L);949		junit.framework.Assert.assertEquals(java.math.BigInteger.valueOf(1801088541L), org.apache.commons.math.util.MathUtils.pow(twentyOne, 7));950		junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, org.apache.commons.math.util.MathUtils.pow(twentyOne, 0));951		try {952			org.apache.commons.math.util.MathUtils.pow(twentyOne, -7);953			junit.framework.Assert.fail("Expecting IllegalArgumentException");954		} catch (java.lang.IllegalArgumentException e) {955		}956		junit.framework.Assert.assertEquals(java.math.BigInteger.valueOf(1801088541L), org.apache.commons.math.util.MathUtils.pow(twentyOne, 7L));957		junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, org.apache.commons.math.util.MathUtils.pow(twentyOne, 0L));958		try {959			org.apache.commons.math.util.MathUtils.pow(twentyOne, -7L);960			junit.framework.Assert.fail("Expecting IllegalArgumentException");961		} catch (java.lang.IllegalArgumentException e) {962		}963		junit.framework.Assert.assertEquals(java.math.BigInteger.valueOf(1801088541L), org.apache.commons.math.util.MathUtils.pow(twentyOne, java.math.BigInteger.valueOf(7L)));964		junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, org.apache.commons.math.util.MathUtils.pow(twentyOne, java.math.BigInteger.ZERO));965		try {966			org.apache.commons.math.util.MathUtils.pow(twentyOne, java.math.BigInteger.valueOf(-7L));967			junit.framework.Assert.fail("Expecting IllegalArgumentException");968		} catch (java.lang.IllegalArgumentException e) {969		}970		java.math.BigInteger bigOne = new java.math.BigInteger(("1543786922199448028351389769265814882661837148" + ("4763915343722775611762713982220306372888519211" + "560905579993523402015636025177602059044911261")));971		junit.framework.Assert.assertEquals(bigOne, org.apache.commons.math.util.MathUtils.pow(twentyOne, 103));972		junit.framework.Assert.assertEquals(bigOne, org.apache.commons.math.util.MathUtils.pow(twentyOne, 103L));973		junit.framework.Assert.assertEquals(bigOne, org.apache.commons.math.util.MathUtils.pow(twentyOne, java.math.BigInteger.valueOf(103L)));974	}975	public void testL1DistanceDouble() {976		double[] p1 = new double[]{ 2.5 , 0.0 };977		double[] p2 = new double[]{ -0.5 , 4.0 };978		junit.framework.Assert.assertEquals(7.0, org.apache.commons.math.util.MathUtils.distance1(p1, p2));979	}980	public void testL1DistanceInt() {981		int[] p1 = new int[]{ 3 , 0 };982		int[] p2 = new int[]{ 0 , 4 };983		junit.framework.Assert.assertEquals(7, org.apache.commons.math.util.MathUtils.distance1(p1, p2));984	}985	public void testL2DistanceDouble() {986		double[] p1 = new double[]{ 2.5 , 0.0 };987		double[] p2 = new double[]{ -0.5 , 4.0 };988		junit.framework.Assert.assertEquals(5.0, org.apache.commons.math.util.MathUtils.distance(p1, p2));989	}990	public void testL2DistanceInt() {991		int[] p1 = new int[]{ 3 , 0 };992		int[] p2 = new int[]{ 0 , 4 };993		junit.framework.Assert.assertEquals(5.0, org.apache.commons.math.util.MathUtils.distance(p1, p2));994	}995	public void testLInfDistanceDouble() {996		double[] p1 = new double[]{ 2.5 , 0.0 };997		double[] p2 = new double[]{ -0.5 , 4.0 };998		junit.framework.Assert.assertEquals(4.0, org.apache.commons.math.util.MathUtils.distanceInf(p1, p2));999	}1000	public void testLInfDistanceInt() {1001		int[] p1 = new int[]{ 3 , 0 };1002		int[] p2 = new int[]{ 0 , 4 };1003		junit.framework.Assert.assertEquals(4, org.apache.commons.math.util.MathUtils.distanceInf(p1, p2));1004	}1005	public void testCheckOrder() {1006		org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ -15 , -5.5 , -1 , 2 , 15 }, 1, true);1007		org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ -15 , -5.5 , -1 , 2 , 2 }, 1, false);1008		org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ 3 , -5.5 , -11 , -27.5 }, -1, true);1009		org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ 3 , 0 , 0 , -5.5 , -11 , -27.5 }, -1, false);1010		try {1011			org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ -15 , -5.5 , -1 , -1 , 2 , 15 }, 1, true);1012			junit.framework.Assert.fail("an exception should have been thrown");1013		} catch (java.lang.IllegalArgumentException e) {1014		}1015		try {1016			org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ -15 , -5.5 , -1 , -2 , 2 }, 1, false);1017			junit.framework.Assert.fail("an exception should have been thrown");...

Full Screen

Full Screen

Source:ResizableDoubleArrayTest.java Github

copy

Full Screen

...17		float defaultExpansionFactor = 2.0F;18		float defaultContractionCriteria = 2.5F;19		int defaultMode = org.apache.commons.math.util.ResizableDoubleArray.MULTIPLICATIVE_MODE;20		org.apache.commons.math.util.ResizableDoubleArray testDa = new org.apache.commons.math.util.ResizableDoubleArray(2);21		junit.framework.Assert.assertEquals(0, testDa.getNumElements());22		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());23		junit.framework.Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);24		junit.framework.Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0);25		junit.framework.Assert.assertEquals(defaultMode, testDa.getExpansionMode());26		try {27			da = new org.apache.commons.math.util.ResizableDoubleArray(-1);28			junit.framework.Assert.fail("Expecting IllegalArgumentException");29		} catch (java.lang.IllegalArgumentException ex) {30		}31		testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F);32		junit.framework.Assert.assertEquals(0, testDa.getNumElements());33		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());34		junit.framework.Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);35		junit.framework.Assert.assertEquals(defaultContractionCriteria, testDa.getContractionCriteria(), 0);36		junit.framework.Assert.assertEquals(defaultMode, testDa.getExpansionMode());37		try {38			da = new org.apache.commons.math.util.ResizableDoubleArray(2 , 0.5F);39			junit.framework.Assert.fail("Expecting IllegalArgumentException");40		} catch (java.lang.IllegalArgumentException ex) {41		}42		testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 3.0F);43		junit.framework.Assert.assertEquals(3.0F, testDa.getExpansionFactor(), 0);44		junit.framework.Assert.assertEquals(3.5F, testDa.getContractionCriteria(), 0);45		testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 3.0F);46		junit.framework.Assert.assertEquals(0, testDa.getNumElements());47		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());48		junit.framework.Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);49		junit.framework.Assert.assertEquals(3.0F, testDa.getContractionCriteria(), 0);50		junit.framework.Assert.assertEquals(defaultMode, testDa.getExpansionMode());51		try {52			da = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 1.5F);53			junit.framework.Assert.fail("Expecting IllegalArgumentException");54		} catch (java.lang.IllegalArgumentException ex) {55		}56		testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 3.0F , org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE);57		junit.framework.Assert.assertEquals(0, testDa.getNumElements());58		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());59		junit.framework.Assert.assertEquals(defaultExpansionFactor, testDa.getExpansionFactor(), 0);60		junit.framework.Assert.assertEquals(3.0F, testDa.getContractionCriteria(), 0);61		junit.framework.Assert.assertEquals(org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE, testDa.getExpansionMode());62		try {63			da = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 2.5F , -1);64			junit.framework.Assert.fail("Expecting IllegalArgumentException");65		} catch (java.lang.IllegalArgumentException ex) {66		}67		testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 3.0F , org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE);68		testDa.addElement(2.0);69		testDa.addElement(3.2);70		org.apache.commons.math.util.ResizableDoubleArray copyDa = new org.apache.commons.math.util.ResizableDoubleArray(testDa);71		junit.framework.Assert.assertEquals(copyDa, testDa);72		junit.framework.Assert.assertEquals(testDa, copyDa);73	}74	public void testSetElementArbitraryExpansion() {75		da.addElement(2.0);76		da.addElement(4.0);77		da.addElement(6.0);78		da.setElement(1, 3.0);79		da.setElement(1000, 3.4);80		junit.framework.Assert.assertEquals("The number of elements should now be 1001, it isn't", da.getNumElements(), 1001);81		junit.framework.Assert.assertEquals("Uninitialized Elements are default value of 0.0, index 766 wasn't", 0.0, da.getElement(760), java.lang.Double.MIN_VALUE);82		junit.framework.Assert.assertEquals("The 1000th index should be 3.4, it isn't", 3.4, da.getElement(1000), java.lang.Double.MIN_VALUE);83		junit.framework.Assert.assertEquals("The 0th index should be 2.0, it isn't", 2.0, da.getElement(0), java.lang.Double.MIN_VALUE);84		da.clear();85		da.addElement(2.0);86		da.addElement(4.0);87		da.addElement(6.0);88		junit.framework.Assert.assertEquals(4, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getInternalLength());89		junit.framework.Assert.assertEquals(3, da.getNumElements());90		da.setElement(3, 7.0);91		junit.framework.Assert.assertEquals(4, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getInternalLength());92		junit.framework.Assert.assertEquals(4, da.getNumElements());93		da.setElement(10, 10.0);94		junit.framework.Assert.assertEquals(11, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getInternalLength());95		junit.framework.Assert.assertEquals(11, da.getNumElements());96		da.setElement(9, 10.0);97		junit.framework.Assert.assertEquals(11, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getInternalLength());98		junit.framework.Assert.assertEquals(11, da.getNumElements());99		try {100			da.setElement(-2, 3);101			junit.framework.Assert.fail("Expecting ArrayIndexOutOfBoundsException for negative index");102		} catch (java.lang.ArrayIndexOutOfBoundsException ex) {103		}104		org.apache.commons.math.util.ResizableDoubleArray testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 3.0F , org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE);105		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());106		testDa.addElement(1.0);107		testDa.addElement(1.0);108		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());109		testDa.addElement(1.0);110		junit.framework.Assert.assertEquals(4, testDa.getInternalLength());111	}112	@java.lang.Override113	public void testAdd1000() {114		super.testAdd1000();115		junit.framework.Assert.assertEquals(("Internal Storage length should be 1024 if we started out with initial capacity of " + "16 and an expansion factor of 2.0"), 1024, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getInternalLength());116	}117	@java.lang.Override118	public void testAddElementRolling() {119		super.testAddElementRolling();120		da.clear();121		da.addElement(1);122		da.addElement(2);123		da.addElementRolling(3);124		junit.framework.Assert.assertEquals(3, da.getElement(1), 0);125		da.addElementRolling(4);126		junit.framework.Assert.assertEquals(3, da.getElement(0), 0);127		junit.framework.Assert.assertEquals(4, da.getElement(1), 0);128		da.addElement(5);129		junit.framework.Assert.assertEquals(5, da.getElement(2), 0);130		da.addElementRolling(6);131		junit.framework.Assert.assertEquals(4, da.getElement(0), 0);132		junit.framework.Assert.assertEquals(5, da.getElement(1), 0);133		junit.framework.Assert.assertEquals(6, da.getElement(2), 0);134		org.apache.commons.math.util.ResizableDoubleArray testDa = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 2.5F , org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE);135		junit.framework.Assert.assertEquals(2, testDa.getInternalLength());136		testDa.addElement(1.0);137		testDa.addElement(2.0);138		testDa.addElement(3.0);139		junit.framework.Assert.assertEquals(1.0, testDa.getElement(0), 0);140		junit.framework.Assert.assertEquals(2.0, testDa.getElement(1), 0);141		junit.framework.Assert.assertEquals(3.0, testDa.getElement(2), 0);142		junit.framework.Assert.assertEquals(4, testDa.getInternalLength());143		junit.framework.Assert.assertEquals(3, testDa.getNumElements());144		testDa.addElementRolling(4.0);145		junit.framework.Assert.assertEquals(2.0, testDa.getElement(0), 0);146		junit.framework.Assert.assertEquals(3.0, testDa.getElement(1), 0);147		junit.framework.Assert.assertEquals(4.0, testDa.getElement(2), 0);148		junit.framework.Assert.assertEquals(4, testDa.getInternalLength());149		junit.framework.Assert.assertEquals(3, testDa.getNumElements());150		testDa.addElementRolling(5.0);151		junit.framework.Assert.assertEquals(3.0, testDa.getElement(0), 0);152		junit.framework.Assert.assertEquals(4.0, testDa.getElement(1), 0);153		junit.framework.Assert.assertEquals(5.0, testDa.getElement(2), 0);154		junit.framework.Assert.assertEquals(4, testDa.getInternalLength());155		junit.framework.Assert.assertEquals(3, testDa.getNumElements());156		try {157			testDa.getElement(4);158			junit.framework.Assert.fail("Expecting ArrayIndexOutOfBoundsException");159		} catch (java.lang.ArrayIndexOutOfBoundsException ex) {160		}161		try {162			testDa.getElement(-1);163			junit.framework.Assert.fail("Expecting ArrayIndexOutOfBoundsException");164		} catch (java.lang.ArrayIndexOutOfBoundsException ex) {165		}166	}167	public void testSetNumberOfElements() {168		da.addElement(1.0);169		da.addElement(1.0);170		da.addElement(1.0);171		da.addElement(1.0);172		da.addElement(1.0);173		da.addElement(1.0);174		junit.framework.Assert.assertEquals("Number of elements should equal 6", da.getNumElements(), 6);175		((org.apache.commons.math.util.ResizableDoubleArray)(da)).setNumElements(3);176		junit.framework.Assert.assertEquals("Number of elements should equal 3", da.getNumElements(), 3);177		try {178			((org.apache.commons.math.util.ResizableDoubleArray)(da)).setNumElements(-3);179			junit.framework.Assert.fail("Setting number of elements to negative should've thrown an exception");180		} catch (java.lang.IllegalArgumentException iae) {181		}182		((org.apache.commons.math.util.ResizableDoubleArray)(da)).setNumElements(1024);183		junit.framework.Assert.assertEquals("Number of elements should now be 1024", da.getNumElements(), 1024);184		junit.framework.Assert.assertEquals("Element 453 should be a default double", da.getElement(453), 0.0, java.lang.Double.MIN_VALUE);185	}186	public void testWithInitialCapacity() {187		org.apache.commons.math.util.ResizableDoubleArray eDA2 = new org.apache.commons.math.util.ResizableDoubleArray(2);188		junit.framework.Assert.assertEquals("Initial number of elements should be 0", 0, eDA2.getNumElements());189		org.apache.commons.math.random.RandomData randomData = new org.apache.commons.math.random.RandomDataImpl();190		int iterations = randomData.nextInt(100, 1000);191		for (int i = 0 ; i < iterations ; i++) {192			eDA2.addElement(i);193		}194		junit.framework.Assert.assertEquals(("Number of elements should be equal to " + iterations), iterations, eDA2.getNumElements());195		eDA2.addElement(2.0);196		junit.framework.Assert.assertEquals(("Number of elements should be equals to " + (iterations + 1)), (iterations + 1), eDA2.getNumElements());197	}198	public void testWithInitialCapacityAndExpansionFactor() {199		org.apache.commons.math.util.ResizableDoubleArray eDA3 = new org.apache.commons.math.util.ResizableDoubleArray(3 , 3.0F , 3.5F);200		junit.framework.Assert.assertEquals("Initial number of elements should be 0", 0, eDA3.getNumElements());201		org.apache.commons.math.random.RandomData randomData = new org.apache.commons.math.random.RandomDataImpl();202		int iterations = randomData.nextInt(100, 3000);203		for (int i = 0 ; i < iterations ; i++) {204			eDA3.addElement(i);205		}206		junit.framework.Assert.assertEquals(("Number of elements should be equal to " + iterations), iterations, eDA3.getNumElements());207		eDA3.addElement(2.0);208		junit.framework.Assert.assertEquals(("Number of elements should be equals to " + (iterations + 1)), (iterations + 1), eDA3.getNumElements());209		junit.framework.Assert.assertEquals("Expansion factor should equal 3.0", 3.0F, eDA3.getExpansionFactor(), java.lang.Double.MIN_VALUE);210	}211	public void testDiscard() {212		da.addElement(2.0);213		da.addElement(2.0);214		da.addElement(2.0);215		da.addElement(2.0);216		da.addElement(2.0);217		da.addElement(2.0);218		da.addElement(2.0);219		da.addElement(2.0);220		da.addElement(2.0);221		da.addElement(2.0);222		da.addElement(2.0);223		junit.framework.Assert.assertEquals("Number of elements should be 11", 11, da.getNumElements());224		((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardFrontElements(5);225		junit.framework.Assert.assertEquals("Number of elements should be 6", 6, da.getNumElements());226		da.addElement(2.0);227		da.addElement(2.0);228		da.addElement(2.0);229		da.addElement(2.0);230		junit.framework.Assert.assertEquals("Number of elements should be 10", 10, da.getNumElements());231		((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardMostRecentElements(2);232		junit.framework.Assert.assertEquals("Number of elements should be 8", 8, da.getNumElements());233		try {234			((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardFrontElements(-1);235			junit.framework.Assert.fail("Trying to discard a negative number of element is not allowed");236		} catch (java.lang.Exception e) {237		}238		try {239			((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardMostRecentElements(-1);240			junit.framework.Assert.fail("Trying to discard a negative number of element is not allowed");241		} catch (java.lang.Exception e) {242		}243		try {244			((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardFrontElements(10000);245			junit.framework.Assert.fail("You can't discard more elements than the array contains");246		} catch (java.lang.Exception e) {247		}248		try {249			((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardMostRecentElements(10000);250			junit.framework.Assert.fail("You can't discard more elements than the array contains");251		} catch (java.lang.Exception e) {252		}253	}254	public void testSubstitute() {255		da.addElement(2.0);256		da.addElement(2.0);257		da.addElement(2.0);258		da.addElement(2.0);259		da.addElement(2.0);260		da.addElement(2.0);261		da.addElement(2.0);262		da.addElement(2.0);263		da.addElement(2.0);264		da.addElement(2.0);265		da.addElement(2.0);266		junit.framework.Assert.assertEquals("Number of elements should be 11", 11, da.getNumElements());267		((org.apache.commons.math.util.ResizableDoubleArray)(da)).substituteMostRecentElement(24);268		junit.framework.Assert.assertEquals("Number of elements should be 11", 11, da.getNumElements());269		try {270			((org.apache.commons.math.util.ResizableDoubleArray)(da)).discardMostRecentElements(10);271		} catch (java.lang.Exception e) {272			junit.framework.Assert.fail("Trying to discard a negative number of element is not allowed");273		}274		((org.apache.commons.math.util.ResizableDoubleArray)(da)).substituteMostRecentElement(24);275		junit.framework.Assert.assertEquals("Number of elements should be 1", 1, da.getNumElements());276	}277	public void testMutators() {278		((org.apache.commons.math.util.ResizableDoubleArray)(da)).setContractionCriteria(10.0F);279		junit.framework.Assert.assertEquals(10.0F, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getContractionCriteria(), 0);280		((org.apache.commons.math.util.ResizableDoubleArray)(da)).setExpansionFactor(8.0F);281		junit.framework.Assert.assertEquals(8.0F, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getExpansionFactor(), 0);282		try {283			((org.apache.commons.math.util.ResizableDoubleArray)(da)).setExpansionFactor(11.0F);284			junit.framework.Assert.fail("Expecting IllegalArgumentException");285		} catch (java.lang.IllegalArgumentException ex) {286		}287		((org.apache.commons.math.util.ResizableDoubleArray)(da)).setExpansionMode(org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE);288		junit.framework.Assert.assertEquals(org.apache.commons.math.util.ResizableDoubleArray.ADDITIVE_MODE, ((org.apache.commons.math.util.ResizableDoubleArray)(da)).getExpansionMode());289		try {290			((org.apache.commons.math.util.ResizableDoubleArray)(da)).setExpansionMode(-1);291			junit.framework.Assert.fail("Expecting IllegalArgumentException");292		} catch (java.lang.IllegalArgumentException ex) {293		}294	}295	public void testEqualsAndHashCode() throws java.lang.Exception {296		org.apache.commons.math.util.ResizableDoubleArray first = new org.apache.commons.math.util.ResizableDoubleArray();297		java.lang.Double other = new java.lang.Double(2);298		junit.framework.Assert.assertFalse(first.equals(other));299		other = null;300		junit.framework.Assert.assertFalse(first.equals(other));301		junit.framework.Assert.assertTrue(first.equals(first));302		org.apache.commons.math.util.ResizableDoubleArray second = new org.apache.commons.math.util.ResizableDoubleArray();303		verifyEquality(first, second);304		org.apache.commons.math.util.ResizableDoubleArray third = new org.apache.commons.math.util.ResizableDoubleArray(3 , 2.0F , 2.0F);305		verifyInequality(third, first);306		org.apache.commons.math.util.ResizableDoubleArray fourth = new org.apache.commons.math.util.ResizableDoubleArray(3 , 2.0F , 2.0F);307		org.apache.commons.math.util.ResizableDoubleArray fifth = new org.apache.commons.math.util.ResizableDoubleArray(2 , 2.0F , 2.0F);308		verifyEquality(third, fourth);309		verifyInequality(third, fifth);310		third.addElement(4.1);311		third.addElement(4.2);312		third.addElement(4.3);313		fourth.addElement(4.1);314		fourth.addElement(4.2);315		fourth.addElement(4.3);316		verifyEquality(third, fourth);317		fourth.addElement(4.4);318		verifyInequality(third, fourth);319		third.addElement(4.4);320		verifyEquality(third, fourth);321		fourth.addElement(4.4);322		verifyInequality(third, fourth);323		third.addElement(4.4);324		verifyEquality(third, fourth);325		fourth.addElementRolling(4.5);326		third.addElementRolling(4.5);327		verifyEquality(third, fourth);328		third.discardFrontElements(1);329		verifyInequality(third, fourth);330		fourth.discardFrontElements(1);331		verifyEquality(third, fourth);332		third.discardMostRecentElements(2);333		fourth.discardMostRecentElements(2);334		verifyEquality(third, fourth);335		third.addElement(18);336		fourth.addElement(17);337		third.addElement(17);338		fourth.addElement(18);339		verifyInequality(third, fourth);340		org.apache.commons.math.util.ResizableDoubleArray.copy(fourth, fifth);341		verifyEquality(fourth, fifth);342		verifyEquality(fourth, new org.apache.commons.math.util.ResizableDoubleArray(fourth));343		verifyEquality(fourth, fourth.copy());344	}345	private void verifyEquality(org.apache.commons.math.util.ResizableDoubleArray a, org.apache.commons.math.util.ResizableDoubleArray b) {346		junit.framework.Assert.assertTrue(b.equals(a));347		junit.framework.Assert.assertTrue(a.equals(b));348		junit.framework.Assert.assertEquals(a.hashCode(), b.hashCode());349	}350	private void verifyInequality(org.apache.commons.math.util.ResizableDoubleArray a, org.apache.commons.math.util.ResizableDoubleArray b) {351		junit.framework.Assert.assertFalse(b.equals(a));352		junit.framework.Assert.assertFalse(a.equals(b));353		junit.framework.Assert.assertFalse(((a.hashCode()) == (b.hashCode())));354	}355}...

Full Screen

Full Screen

Source:BrentSolverTest.java Github

copy

Full Screen

...8		org.apache.commons.math.analysis.UnivariateRealFunction f = new org.apache.commons.math.analysis.SinFunction();9		double result;10		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver(f);11		result = solver.solve(3, 4);12		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());13		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 5));14		result = solver.solve(1, 4);15		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());16		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));17		solver = new org.apache.commons.math.analysis.solvers.SecantSolver(f);18		result = solver.solve(3, 4);19		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());20		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 5));21		result = solver.solve(1, 4);22		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());23		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));24		junit.framework.Assert.assertEquals(result, solver.getResult(), 0);25	}26	public void testSinZero() throws org.apache.commons.math.MathException {27		org.apache.commons.math.analysis.UnivariateRealFunction f = new org.apache.commons.math.analysis.SinFunction();28		double result;29		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver();30		result = solver.solve(f, 3, 4);31		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());32		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 5));33		result = solver.solve(f, 1, 4);34		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());35		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));36		solver = new org.apache.commons.math.analysis.solvers.SecantSolver();37		result = solver.solve(f, 3, 4);38		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());39		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 5));40		result = solver.solve(f, 1, 4);41		junit.framework.Assert.assertEquals(result, java.lang.Math.PI, solver.getAbsoluteAccuracy());42		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));43		junit.framework.Assert.assertEquals(result, solver.getResult(), 0);44	}45	public void testQuinticZero() throws org.apache.commons.math.MathException {46		org.apache.commons.math.analysis.UnivariateRealFunction f = new org.apache.commons.math.analysis.QuinticFunction();47		double result;48		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver();49		result = solver.solve(f, -0.2, 0.2);50		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());51		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 2));52		result = solver.solve(f, -0.1, 0.3);53		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());54		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));55		result = solver.solve(f, -0.3, 0.45);56		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());57		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 7));58		result = solver.solve(f, 0.3, 0.7);59		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());60		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 7));61		result = solver.solve(f, 0.2, 0.6);62		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());63		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 7));64		result = solver.solve(f, 0.05, 0.95);65		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());66		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 9));67		result = solver.solve(f, 0.85, 1.25);68		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());69		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 9));70		result = solver.solve(f, 0.8, 1.2);71		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());72		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 9));73		result = solver.solve(f, 0.85, 1.75);74		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());75		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 11));76		result = solver.solve(f, 0.55, 1.45);77		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());78		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 8));79		result = solver.solve(f, 0.85, 5);80		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());81		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 13));82		solver = new org.apache.commons.math.analysis.solvers.SecantSolver();83		result = solver.solve(f, -0.2, 0.2);84		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());85		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 2));86		result = solver.solve(f, -0.1, 0.3);87		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());88		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 6));89		result = solver.solve(f, -0.3, 0.45);90		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());91		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 7));92		result = solver.solve(f, 0.3, 0.7);93		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());94		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 8));95		result = solver.solve(f, 0.2, 0.6);96		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());97		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 7));98		result = solver.solve(f, 0.05, 0.95);99		junit.framework.Assert.assertEquals(result, 0.5, solver.getAbsoluteAccuracy());100		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 9));101		result = solver.solve(f, 0.85, 1.25);102		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());103		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 11));104		result = solver.solve(f, 0.8, 1.2);105		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());106		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 9));107		result = solver.solve(f, 0.85, 1.75);108		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());109		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 15));110		result = solver.solve(f, 0.55, 1.45);111		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());112		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 8));113		result = solver.solve(f, 0.85, 5);114		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());115		junit.framework.Assert.assertTrue(((solver.getIterationCount()) <= 15));116		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, -0.2, 0.2);117		junit.framework.Assert.assertEquals(result, 0, solver.getAbsoluteAccuracy());118		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, -0.1, 0.3);119		junit.framework.Assert.assertEquals(result, 0, 1.0E-8);120		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, -0.3, 0.45);121		junit.framework.Assert.assertEquals(result, 0, 1.0E-6);122		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.3, 0.7);123		junit.framework.Assert.assertEquals(result, 0.5, 1.0E-6);124		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.2, 0.6);125		junit.framework.Assert.assertEquals(result, 0.5, 1.0E-6);126		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.05, 0.95);127		junit.framework.Assert.assertEquals(result, 0.5, 1.0E-6);128		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.85, 1.25);129		junit.framework.Assert.assertEquals(result, 1.0, 1.0E-6);130		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.8, 1.2);131		junit.framework.Assert.assertEquals(result, 1.0, 1.0E-6);132		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.85, 1.75);133		junit.framework.Assert.assertEquals(result, 1.0, 1.0E-6);134		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.55, 1.45);135		junit.framework.Assert.assertEquals(result, 1.0, 1.0E-6);136		result = org.apache.commons.math.analysis.solvers.UnivariateRealSolverUtils.solve(f, 0.85, 5);137		junit.framework.Assert.assertEquals(result, 1.0, 1.0E-6);138	}139	public void testRootEndpoints() throws java.lang.Exception {140		org.apache.commons.math.analysis.UnivariateRealFunction f = new org.apache.commons.math.analysis.SinFunction();141		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver();142		double result = solver.solve(f, java.lang.Math.PI, 4);143		junit.framework.Assert.assertEquals(java.lang.Math.PI, result, solver.getAbsoluteAccuracy());144		result = solver.solve(f, 3, java.lang.Math.PI);145		junit.framework.Assert.assertEquals(java.lang.Math.PI, result, solver.getAbsoluteAccuracy());146		result = solver.solve(f, java.lang.Math.PI, 4, 3.5);147		junit.framework.Assert.assertEquals(java.lang.Math.PI, result, solver.getAbsoluteAccuracy());148		result = solver.solve(f, 3, java.lang.Math.PI, 3.07);149		junit.framework.Assert.assertEquals(java.lang.Math.PI, result, solver.getAbsoluteAccuracy());150	}151	public void testBadEndpoints() throws java.lang.Exception {152		org.apache.commons.math.analysis.UnivariateRealFunction f = new org.apache.commons.math.analysis.SinFunction();153		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver();154		try {155			solver.solve(f, 1, -1);156			junit.framework.Assert.fail("Expecting IllegalArgumentException - bad interval");157		} catch (java.lang.IllegalArgumentException ex) {158		}159		try {160			solver.solve(f, 1, 1.5);161			junit.framework.Assert.fail("Expecting IllegalArgumentException - non-bracketing");162		} catch (java.lang.IllegalArgumentException ex) {163		}164		try {165			solver.solve(f, 1, 1.5, 1.2);166			junit.framework.Assert.fail("Expecting IllegalArgumentException - non-bracketing");167		} catch (java.lang.IllegalArgumentException ex) {168		}169	}170	public void testInitialGuess() throws org.apache.commons.math.MathException {171		org.apache.commons.math.analysis.MonitoredFunction f = new org.apache.commons.math.analysis.MonitoredFunction(new org.apache.commons.math.analysis.QuinticFunction());172		org.apache.commons.math.analysis.solvers.UnivariateRealSolver solver = new org.apache.commons.math.analysis.solvers.BrentSolver();173		double result;174		result = solver.solve(f, 0.6, 7.0);175		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());176		int referenceCallsCount = f.getCallsCount();177		junit.framework.Assert.assertTrue((referenceCallsCount >= 13));178		try {179			result = solver.solve(f, 0.6, 7.0, 0.0);180			junit.framework.Assert.fail("an IllegalArgumentException was expected");181		} catch (java.lang.IllegalArgumentException iae) {182		} catch (java.lang.Exception e) {183			junit.framework.Assert.fail(("wrong exception caught: " + (e.getMessage())));184		}185		f.setCallsCount(0);186		result = solver.solve(f, 0.6, 7.0, 0.61);187		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());188		junit.framework.Assert.assertTrue(((f.getCallsCount()) > referenceCallsCount));189		f.setCallsCount(0);190		result = solver.solve(f, 0.6, 7.0, 0.999999);191		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());192		junit.framework.Assert.assertTrue(((f.getCallsCount()) < referenceCallsCount));193		f.setCallsCount(0);194		result = solver.solve(f, 0.6, 7.0, 1.0);195		junit.framework.Assert.assertEquals(result, 1.0, solver.getAbsoluteAccuracy());196		junit.framework.Assert.assertEquals(0, solver.getIterationCount());197		junit.framework.Assert.assertEquals(1, f.getCallsCount());198	}199}...

Full Screen

Full Screen

Source:BigFractionFormatTest.java Github

copy

Full Screen

...13	public void testFormat() {14		org.apache.commons.math.fraction.BigFraction c = new org.apache.commons.math.fraction.BigFraction(1 , 2);15		java.lang.String expected = "1 / 2";16		java.lang.String actual = properFormat.format(c);17		junit.framework.Assert.assertEquals(expected, actual);18		actual = improperFormat.format(c);19		junit.framework.Assert.assertEquals(expected, actual);20	}21	public void testFormatNegative() {22		org.apache.commons.math.fraction.BigFraction c = new org.apache.commons.math.fraction.BigFraction(-1 , 2);23		java.lang.String expected = "-1 / 2";24		java.lang.String actual = properFormat.format(c);25		junit.framework.Assert.assertEquals(expected, actual);26		actual = improperFormat.format(c);27		junit.framework.Assert.assertEquals(expected, actual);28	}29	public void testFormatZero() {30		org.apache.commons.math.fraction.BigFraction c = new org.apache.commons.math.fraction.BigFraction(0 , 1);31		java.lang.String expected = "0 / 1";32		java.lang.String actual = properFormat.format(c);33		junit.framework.Assert.assertEquals(expected, actual);34		actual = improperFormat.format(c);35		junit.framework.Assert.assertEquals(expected, actual);36	}37	public void testFormatImproper() {38		org.apache.commons.math.fraction.BigFraction c = new org.apache.commons.math.fraction.BigFraction(5 , 3);39		java.lang.String actual = properFormat.format(c);40		junit.framework.Assert.assertEquals("1 2 / 3", actual);41		actual = improperFormat.format(c);42		junit.framework.Assert.assertEquals("5 / 3", actual);43	}44	public void testFormatImproperNegative() {45		org.apache.commons.math.fraction.BigFraction c = new org.apache.commons.math.fraction.BigFraction(-5 , 3);46		java.lang.String actual = properFormat.format(c);47		junit.framework.Assert.assertEquals("-1 2 / 3", actual);48		actual = improperFormat.format(c);49		junit.framework.Assert.assertEquals("-5 / 3", actual);50	}51	public void testParse() {52		java.lang.String source = "1 / 2";53		try {54			org.apache.commons.math.fraction.BigFraction c = properFormat.parse(source);55			junit.framework.Assert.assertNotNull(c);56			junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, c.getNumerator());57			junit.framework.Assert.assertEquals(java.math.BigInteger.valueOf(2L), c.getDenominator());58			c = improperFormat.parse(source);59			junit.framework.Assert.assertNotNull(c);60			junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, c.getNumerator());61			junit.framework.Assert.assertEquals(java.math.BigInteger.valueOf(2L), c.getDenominator());62		} catch (java.text.ParseException ex) {63			junit.framework.Assert.fail(ex.getMessage());64		}65	}66	public void testParseInteger() {67		java.lang.String source = "10";68		try {69			org.apache.commons.math.fraction.BigFraction c = properFormat.parse(source);70			junit.framework.Assert.assertNotNull(c);71			junit.framework.Assert.assertEquals(java.math.BigInteger.TEN, c.getNumerator());72			junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, c.getDenominator());73		} catch (java.text.ParseException ex) {74			junit.framework.Assert.fail(ex.getMessage());75		}76		try {77			org.apache.commons.math.fraction.BigFraction c = improperFormat.parse(source);78			junit.framework.Assert.assertNotNull(c);79			junit.framework.Assert.assertEquals(java.math.BigInteger.TEN, c.getNumerator());80			junit.framework.Assert.assertEquals(java.math.BigInteger.ONE, c.getDenominator());81		} catch (java.text.ParseException ex) {82			junit.framework.Assert.fail(ex.getMessage());83		}84	}85	public void testParseInvalid() {86		java.lang.String source = "a";87		java.lang.String msg = "should not be able to parse '10 / a'.";88		try {89			properFormat.parse(source);90			junit.framework.Assert.fail(msg);91		} catch (java.text.ParseException ex) {92		}93		try {94			improperFormat.parse(source);95			junit.framework.Assert.fail(msg);96		} catch (java.text.ParseException ex) {97		}98	}99	public void testParseInvalidDenominator() {100		java.lang.String source = "10 / a";101		java.lang.String msg = "should not be able to parse '10 / a'.";102		try {103			properFormat.parse(source);104			junit.framework.Assert.fail(msg);105		} catch (java.text.ParseException ex) {106		}107		try {108			improperFormat.parse(source);109			junit.framework.Assert.fail(msg);110		} catch (java.text.ParseException ex) {111		}112	}113	public void testParseNegative() {114		try {115			java.lang.String source = "-1 / 2";116			org.apache.commons.math.fraction.BigFraction c = properFormat.parse(source);117			junit.framework.Assert.assertNotNull(c);118			junit.framework.Assert.assertEquals(-1, c.getNumeratorAsInt());119			junit.framework.Assert.assertEquals(2, c.getDenominatorAsInt());120			c = improperFormat.parse(source);121			junit.framework.Assert.assertNotNull(c);122			junit.framework.Assert.assertEquals(-1, c.getNumeratorAsInt());123			junit.framework.Assert.assertEquals(2, c.getDenominatorAsInt());124			source = "1 / -2";125			c = properFormat.parse(source);126			junit.framework.Assert.assertNotNull(c);127			junit.framework.Assert.assertEquals(-1, c.getNumeratorAsInt());128			junit.framework.Assert.assertEquals(2, c.getDenominatorAsInt());129			c = improperFormat.parse(source);130			junit.framework.Assert.assertNotNull(c);131			junit.framework.Assert.assertEquals(-1, c.getNumeratorAsInt());132			junit.framework.Assert.assertEquals(2, c.getDenominatorAsInt());133		} catch (java.text.ParseException ex) {134			junit.framework.Assert.fail(ex.getMessage());135		}136	}137	public void testParseProper() {138		java.lang.String source = "1 2 / 3";139		try {140			org.apache.commons.math.fraction.BigFraction c = properFormat.parse(source);141			junit.framework.Assert.assertNotNull(c);142			junit.framework.Assert.assertEquals(5, c.getNumeratorAsInt());143			junit.framework.Assert.assertEquals(3, c.getDenominatorAsInt());144		} catch (java.text.ParseException ex) {145			junit.framework.Assert.fail(ex.getMessage());146		}147		try {148			improperFormat.parse(source);149			junit.framework.Assert.fail("invalid improper fraction.");150		} catch (java.text.ParseException ex) {151		}152	}153	public void testParseProperNegative() {154		java.lang.String source = "-1 2 / 3";155		try {156			org.apache.commons.math.fraction.BigFraction c = properFormat.parse(source);157			junit.framework.Assert.assertNotNull(c);158			junit.framework.Assert.assertEquals(-5, c.getNumeratorAsInt());159			junit.framework.Assert.assertEquals(3, c.getDenominatorAsInt());160		} catch (java.text.ParseException ex) {161			junit.framework.Assert.fail(ex.getMessage());162		}163		try {164			improperFormat.parse(source);165			junit.framework.Assert.fail("invalid improper fraction.");166		} catch (java.text.ParseException ex) {167		}168	}169	public void testParseProperInvalidMinus() {170		java.lang.String source = "2 -2 / 3";171		try {172			properFormat.parse(source);173			junit.framework.Assert.fail("invalid minus in improper fraction.");174		} catch (java.text.ParseException ex) {175		}176		source = "2 2 / -3";177		try {178			properFormat.parse(source);179			junit.framework.Assert.fail("invalid minus in improper fraction.");180		} catch (java.text.ParseException ex) {181		}182	}183	public void testParseBig() throws java.text.ParseException {184		org.apache.commons.math.fraction.BigFraction f1 = improperFormat.parse(("167213075789791382630275400487886041651764456874403" + (" / " + "53225575123090058458126718248444563466137046489291")));185		junit.framework.Assert.assertEquals(java.lang.Math.PI, f1.doubleValue(), 0.0);186		org.apache.commons.math.fraction.BigFraction f2 = properFormat.parse(("3 " + ("7536350420521207255895245742552351253353317406530" + (" / " + "53225575123090058458126718248444563466137046489291"))));187		junit.framework.Assert.assertEquals(java.lang.Math.PI, f2.doubleValue(), 0.0);188		junit.framework.Assert.assertEquals(f1, f2);189		java.math.BigDecimal pi = new java.math.BigDecimal("3.141592653589793238462643383279502884197169399375105820974944592307816406286208998628034825342117068");190		junit.framework.Assert.assertEquals(pi, f1.bigDecimalValue(99, java.math.BigDecimal.ROUND_HALF_EVEN));191	}192	public void testNumeratorFormat() {193		java.text.NumberFormat old = properFormat.getNumeratorFormat();194		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();195		nf.setParseIntegerOnly(true);196		properFormat.setNumeratorFormat(nf);197		junit.framework.Assert.assertEquals(nf, properFormat.getNumeratorFormat());198		properFormat.setNumeratorFormat(old);199		old = improperFormat.getNumeratorFormat();200		nf = java.text.NumberFormat.getInstance();201		nf.setParseIntegerOnly(true);202		improperFormat.setNumeratorFormat(nf);203		junit.framework.Assert.assertEquals(nf, improperFormat.getNumeratorFormat());204		improperFormat.setNumeratorFormat(old);205	}206	public void testDenominatorFormat() {207		java.text.NumberFormat old = properFormat.getDenominatorFormat();208		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();209		nf.setParseIntegerOnly(true);210		properFormat.setDenominatorFormat(nf);211		junit.framework.Assert.assertEquals(nf, properFormat.getDenominatorFormat());212		properFormat.setDenominatorFormat(old);213		old = improperFormat.getDenominatorFormat();214		nf = java.text.NumberFormat.getInstance();215		nf.setParseIntegerOnly(true);216		improperFormat.setDenominatorFormat(nf);217		junit.framework.Assert.assertEquals(nf, improperFormat.getDenominatorFormat());218		improperFormat.setDenominatorFormat(old);219	}220	public void testWholeFormat() {221		org.apache.commons.math.fraction.ProperBigFractionFormat format = ((org.apache.commons.math.fraction.ProperBigFractionFormat)(properFormat));222		java.text.NumberFormat old = format.getWholeFormat();223		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();224		nf.setParseIntegerOnly(true);225		format.setWholeFormat(nf);226		junit.framework.Assert.assertEquals(nf, format.getWholeFormat());227		format.setWholeFormat(old);228	}229	public void testLongFormat() {230		junit.framework.Assert.assertEquals("10 / 1", improperFormat.format(10L));231	}232	public void testDoubleFormat() {233		junit.framework.Assert.assertEquals("1 / 16", improperFormat.format(0.0625));234	}235}...

Full Screen

Full Screen

Source:FrequencyTest.java Github

copy

Full Screen

...16		f = new org.apache.commons.math.stat.Frequency();17	}18	@java.lang.SuppressWarnings(value = "deprecation")19	public void testCounts() {20		junit.framework.Assert.assertEquals("total count", 0, f.getSumFreq());21		f.addValue(oneL);22		f.addValue(twoL);23		f.addValue(1);24		f.addValue(oneI);25		junit.framework.Assert.assertEquals("one frequency count", 3, f.getCount(1));26		junit.framework.Assert.assertEquals("two frequency count", 1, f.getCount(2));27		junit.framework.Assert.assertEquals("three frequency count", 0, f.getCount(3));28		junit.framework.Assert.assertEquals("total count", 4, f.getSumFreq());29		junit.framework.Assert.assertEquals("zero cumulative frequency", 0, f.getCumFreq(0));30		junit.framework.Assert.assertEquals("one cumulative frequency", 3, f.getCumFreq(1));31		junit.framework.Assert.assertEquals("two cumulative frequency", 4, f.getCumFreq(2));32		junit.framework.Assert.assertEquals("Integer argument cum freq", 4, f.getCumFreq(java.lang.Integer.valueOf(2)));33		junit.framework.Assert.assertEquals("five cumulative frequency", 4, f.getCumFreq(5));34		junit.framework.Assert.assertEquals("foo cumulative frequency", 0, f.getCumFreq("foo"));35		f.clear();36		junit.framework.Assert.assertEquals("total count", 0, f.getSumFreq());37		f.addValue("one");38		f.addValue("One");39		f.addValue("oNe");40		f.addValue("Z");41		junit.framework.Assert.assertEquals("one cumulative frequency", 1, f.getCount("one"));42		junit.framework.Assert.assertEquals("Z cumulative pct", 0.5, f.getCumPct("Z"), tolerance);43		junit.framework.Assert.assertEquals("z cumulative pct", 1.0, f.getCumPct("z"), tolerance);44		junit.framework.Assert.assertEquals("Ot cumulative pct", 0.25, f.getCumPct("Ot"), tolerance);45		f.clear();46		f = null;47		org.apache.commons.math.stat.Frequency f = new org.apache.commons.math.stat.Frequency();48		f.addValue(1);49		f.addValue(java.lang.Integer.valueOf(1));50		f.addValue(java.lang.Long.valueOf(1));51		f.addValue(2);52		f.addValue(java.lang.Integer.valueOf(-1));53		junit.framework.Assert.assertEquals("1 count", 3, f.getCount(1));54		junit.framework.Assert.assertEquals("1 count", 3, f.getCount(java.lang.Integer.valueOf(1)));55		junit.framework.Assert.assertEquals("0 cum pct", 0.2, f.getCumPct(0), tolerance);56		junit.framework.Assert.assertEquals("1 pct", 0.6, f.getPct(java.lang.Integer.valueOf(1)), tolerance);57		junit.framework.Assert.assertEquals("-2 cum pct", 0, f.getCumPct(-2), tolerance);58		junit.framework.Assert.assertEquals("10 cum pct", 1, f.getCumPct(10), tolerance);59		f = null;60		f = new org.apache.commons.math.stat.Frequency(java.lang.String.CASE_INSENSITIVE_ORDER);61		f.addValue("one");62		f.addValue("One");63		f.addValue("oNe");64		f.addValue("Z");65		junit.framework.Assert.assertEquals("one count", 3, f.getCount("one"));66		junit.framework.Assert.assertEquals("Z cumulative pct -- case insensitive", 1, f.getCumPct("Z"), tolerance);67		junit.framework.Assert.assertEquals("z cumulative pct -- case insensitive", 1, f.getCumPct("z"), tolerance);68		f = null;69		f = new org.apache.commons.math.stat.Frequency();70		junit.framework.Assert.assertEquals(0L, f.getCount('a'));71		junit.framework.Assert.assertEquals(0L, f.getCumFreq('b'));72		org.apache.commons.math.TestUtils.assertEquals(java.lang.Double.NaN, f.getPct('a'), 0.0);73		org.apache.commons.math.TestUtils.assertEquals(java.lang.Double.NaN, f.getCumPct('b'), 0.0);74		f.addValue('a');75		f.addValue('b');76		f.addValue('c');77		f.addValue('d');78		junit.framework.Assert.assertEquals(1L, f.getCount('a'));79		junit.framework.Assert.assertEquals(2L, f.getCumFreq('b'));80		junit.framework.Assert.assertEquals(0.25, f.getPct('a'), 0.0);81		junit.framework.Assert.assertEquals(0.5, f.getCumPct('b'), 0.0);82		junit.framework.Assert.assertEquals(1.0, f.getCumPct('e'), 0.0);83	}84	@java.lang.SuppressWarnings(value = "deprecation")85	public void testPcts() {86		f.addValue(oneL);87		f.addValue(twoL);88		f.addValue(oneI);89		f.addValue(twoI);90		f.addValue(threeL);91		f.addValue(threeL);92		f.addValue(3);93		f.addValue(threeI);94		junit.framework.Assert.assertEquals("one pct", 0.25, f.getPct(1), tolerance);95		junit.framework.Assert.assertEquals("two pct", 0.25, f.getPct(java.lang.Long.valueOf(2)), tolerance);96		junit.framework.Assert.assertEquals("three pct", 0.5, f.getPct(threeL), tolerance);97		junit.framework.Assert.assertEquals("three (Object) pct", 0.5, f.getPct(((java.lang.Object)(java.lang.Integer.valueOf(3)))), tolerance);98		junit.framework.Assert.assertEquals("five pct", 0, f.getPct(5), tolerance);99		junit.framework.Assert.assertEquals("foo pct", 0, f.getPct("foo"), tolerance);100		junit.framework.Assert.assertEquals("one cum pct", 0.25, f.getCumPct(1), tolerance);101		junit.framework.Assert.assertEquals("two cum pct", 0.5, f.getCumPct(java.lang.Long.valueOf(2)), tolerance);102		junit.framework.Assert.assertEquals("Integer argument", 0.5, f.getCumPct(java.lang.Integer.valueOf(2)), tolerance);103		junit.framework.Assert.assertEquals("three cum pct", 1.0, f.getCumPct(threeL), tolerance);104		junit.framework.Assert.assertEquals("five cum pct", 1.0, f.getCumPct(5), tolerance);105		junit.framework.Assert.assertEquals("zero cum pct", 0.0, f.getCumPct(0), tolerance);106		junit.framework.Assert.assertEquals("foo cum pct", 0, f.getCumPct("foo"), tolerance);107	}108	@java.lang.SuppressWarnings(value = "deprecation")109	public void testAdd() {110		char aChar = 'a';111		char bChar = 'b';112		java.lang.String aString = "a";113		f.addValue(aChar);114		f.addValue(bChar);115		try {116			f.addValue(aString);117			junit.framework.Assert.fail("Expecting IllegalArgumentException");118		} catch (java.lang.IllegalArgumentException ex) {119		}120		try {121			f.addValue(2);122			junit.framework.Assert.fail("Expecting IllegalArgumentException");123		} catch (java.lang.IllegalArgumentException ex) {124		}125		junit.framework.Assert.assertEquals("a pct", 0.5, f.getPct(aChar), tolerance);126		junit.framework.Assert.assertEquals("b cum pct", 1.0, f.getCumPct(bChar), tolerance);127		junit.framework.Assert.assertEquals("a string pct", 0.0, f.getPct(aString), tolerance);128		junit.framework.Assert.assertEquals("a string cum pct", 0.0, f.getCumPct(aString), tolerance);129		f = new org.apache.commons.math.stat.Frequency();130		f.addValue("One");131		try {132			f.addValue(new java.lang.Integer("One"));133			junit.framework.Assert.fail("Expecting IllegalArgumentException");134		} catch (java.lang.IllegalArgumentException ex) {135		}136	}137	@java.lang.SuppressWarnings(value = "deprecation")138	public void testAddNonComparable() {139		try {140			f.addValue(new java.lang.Object());141			junit.framework.Assert.fail("Expected IllegalArgumentException");142		} catch (java.lang.IllegalArgumentException expected) {143		}144		f.clear();145		f.addValue(1);146		try {147			f.addValue(new java.lang.Object());148			junit.framework.Assert.fail("Expected IllegalArgumentException");149		} catch (java.lang.IllegalArgumentException expected) {150		}151	}152	public void testEmptyTable() {153		junit.framework.Assert.assertEquals("freq sum, empty table", 0, f.getSumFreq());154		junit.framework.Assert.assertEquals("count, empty table", 0, f.getCount(0));155		junit.framework.Assert.assertEquals("count, empty table", 0, f.getCount(java.lang.Integer.valueOf(0)));156		junit.framework.Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq(0));157		junit.framework.Assert.assertEquals("cum freq, empty table", 0, f.getCumFreq("x"));158		junit.framework.Assert.assertTrue("pct, empty table", java.lang.Double.isNaN(f.getPct(0)));159		junit.framework.Assert.assertTrue("pct, empty table", java.lang.Double.isNaN(f.getPct(java.lang.Integer.valueOf(0))));160		junit.framework.Assert.assertTrue("cum pct, empty table", java.lang.Double.isNaN(f.getCumPct(0)));161		junit.framework.Assert.assertTrue("cum pct, empty table", java.lang.Double.isNaN(f.getCumPct(java.lang.Integer.valueOf(0))));162	}163	public void testToString() {164		f.addValue(oneL);165		f.addValue(twoL);166		f.addValue(oneI);167		f.addValue(twoI);168		java.lang.String s = f.toString();169		junit.framework.Assert.assertNotNull(s);170		java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.StringReader(s));171		try {172			java.lang.String line = reader.readLine();173			junit.framework.Assert.assertNotNull(line);174			line = reader.readLine();175			junit.framework.Assert.assertNotNull(line);176			line = reader.readLine();177			junit.framework.Assert.assertNotNull(line);178			line = reader.readLine();179			junit.framework.Assert.assertNull(line);180		} catch (java.io.IOException ex) {181			junit.framework.Assert.fail(ex.getMessage());182		}183	}184	@java.lang.SuppressWarnings(value = "deprecation")185	public void testIntegerValues() {186		java.lang.Comparable<?> obj1 = null;187		obj1 = java.lang.Integer.valueOf(1);188		java.lang.Integer int1 = java.lang.Integer.valueOf(1);189		f.addValue(obj1);190		f.addValue(int1);191		f.addValue(2);192		f.addValue(java.lang.Long.valueOf(2));193		junit.framework.Assert.assertEquals("Integer 1 count", 2, f.getCount(1));194		junit.framework.Assert.assertEquals("Integer 1 count", 2, f.getCount(java.lang.Integer.valueOf(1)));195		junit.framework.Assert.assertEquals("Integer 1 count", 2, f.getCount(java.lang.Long.valueOf(1)));196		junit.framework.Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(1), tolerance);197		junit.framework.Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(java.lang.Long.valueOf(1)), tolerance);198		junit.framework.Assert.assertEquals("Integer 1 cumPct", 0.5, f.getCumPct(java.lang.Integer.valueOf(1)), tolerance);199		java.util.Iterator<?> it = f.valuesIterator();200		while (it.hasNext()) {201			junit.framework.Assert.assertTrue(((it.next()) instanceof java.lang.Long));202		}203	}204	public void testSerial() {205		f.addValue(oneL);206		f.addValue(twoL);207		f.addValue(oneI);208		f.addValue(twoI);209		junit.framework.Assert.assertEquals(f, org.apache.commons.math.TestUtils.serializeAndRecover(f));210	}211}...

Full Screen

Full Screen

Source:FractionFormatTest.java Github

copy

Full Screen

...13	public void testFormat() {14		org.apache.commons.math.fraction.Fraction c = new org.apache.commons.math.fraction.Fraction(1 , 2);15		java.lang.String expected = "1 / 2";16		java.lang.String actual = properFormat.format(c);17		junit.framework.Assert.assertEquals(expected, actual);18		actual = improperFormat.format(c);19		junit.framework.Assert.assertEquals(expected, actual);20	}21	public void testFormatNegative() {22		org.apache.commons.math.fraction.Fraction c = new org.apache.commons.math.fraction.Fraction(-1 , 2);23		java.lang.String expected = "-1 / 2";24		java.lang.String actual = properFormat.format(c);25		junit.framework.Assert.assertEquals(expected, actual);26		actual = improperFormat.format(c);27		junit.framework.Assert.assertEquals(expected, actual);28	}29	public void testFormatZero() {30		org.apache.commons.math.fraction.Fraction c = new org.apache.commons.math.fraction.Fraction(0 , 1);31		java.lang.String expected = "0 / 1";32		java.lang.String actual = properFormat.format(c);33		junit.framework.Assert.assertEquals(expected, actual);34		actual = improperFormat.format(c);35		junit.framework.Assert.assertEquals(expected, actual);36	}37	public void testFormatImproper() {38		org.apache.commons.math.fraction.Fraction c = new org.apache.commons.math.fraction.Fraction(5 , 3);39		java.lang.String actual = properFormat.format(c);40		junit.framework.Assert.assertEquals("1 2 / 3", actual);41		actual = improperFormat.format(c);42		junit.framework.Assert.assertEquals("5 / 3", actual);43	}44	public void testFormatImproperNegative() {45		org.apache.commons.math.fraction.Fraction c = new org.apache.commons.math.fraction.Fraction(-5 , 3);46		java.lang.String actual = properFormat.format(c);47		junit.framework.Assert.assertEquals("-1 2 / 3", actual);48		actual = improperFormat.format(c);49		junit.framework.Assert.assertEquals("-5 / 3", actual);50	}51	public void testParse() {52		java.lang.String source = "1 / 2";53		try {54			org.apache.commons.math.fraction.Fraction c = properFormat.parse(source);55			junit.framework.Assert.assertNotNull(c);56			junit.framework.Assert.assertEquals(1, c.getNumerator());57			junit.framework.Assert.assertEquals(2, c.getDenominator());58			c = improperFormat.parse(source);59			junit.framework.Assert.assertNotNull(c);60			junit.framework.Assert.assertEquals(1, c.getNumerator());61			junit.framework.Assert.assertEquals(2, c.getDenominator());62		} catch (java.text.ParseException ex) {63			junit.framework.Assert.fail(ex.getMessage());64		}65	}66	public void testParseInteger() {67		java.lang.String source = "10";68		try {69			org.apache.commons.math.fraction.Fraction c = properFormat.parse(source);70			junit.framework.Assert.assertNotNull(c);71			junit.framework.Assert.assertEquals(10, c.getNumerator());72			junit.framework.Assert.assertEquals(1, c.getDenominator());73		} catch (java.text.ParseException ex) {74			junit.framework.Assert.fail(ex.getMessage());75		}76		try {77			org.apache.commons.math.fraction.Fraction c = improperFormat.parse(source);78			junit.framework.Assert.assertNotNull(c);79			junit.framework.Assert.assertEquals(10, c.getNumerator());80			junit.framework.Assert.assertEquals(1, c.getDenominator());81		} catch (java.text.ParseException ex) {82			junit.framework.Assert.fail(ex.getMessage());83		}84	}85	public void testParseInvalid() {86		java.lang.String source = "a";87		java.lang.String msg = "should not be able to parse '10 / a'.";88		try {89			properFormat.parse(source);90			junit.framework.Assert.fail(msg);91		} catch (java.text.ParseException ex) {92		}93		try {94			improperFormat.parse(source);95			junit.framework.Assert.fail(msg);96		} catch (java.text.ParseException ex) {97		}98	}99	public void testParseInvalidDenominator() {100		java.lang.String source = "10 / a";101		java.lang.String msg = "should not be able to parse '10 / a'.";102		try {103			properFormat.parse(source);104			junit.framework.Assert.fail(msg);105		} catch (java.text.ParseException ex) {106		}107		try {108			improperFormat.parse(source);109			junit.framework.Assert.fail(msg);110		} catch (java.text.ParseException ex) {111		}112	}113	public void testParseNegative() {114		try {115			java.lang.String source = "-1 / 2";116			org.apache.commons.math.fraction.Fraction c = properFormat.parse(source);117			junit.framework.Assert.assertNotNull(c);118			junit.framework.Assert.assertEquals(-1, c.getNumerator());119			junit.framework.Assert.assertEquals(2, c.getDenominator());120			c = improperFormat.parse(source);121			junit.framework.Assert.assertNotNull(c);122			junit.framework.Assert.assertEquals(-1, c.getNumerator());123			junit.framework.Assert.assertEquals(2, c.getDenominator());124			source = "1 / -2";125			c = properFormat.parse(source);126			junit.framework.Assert.assertNotNull(c);127			junit.framework.Assert.assertEquals(-1, c.getNumerator());128			junit.framework.Assert.assertEquals(2, c.getDenominator());129			c = improperFormat.parse(source);130			junit.framework.Assert.assertNotNull(c);131			junit.framework.Assert.assertEquals(-1, c.getNumerator());132			junit.framework.Assert.assertEquals(2, c.getDenominator());133		} catch (java.text.ParseException ex) {134			junit.framework.Assert.fail(ex.getMessage());135		}136	}137	public void testParseProper() {138		java.lang.String source = "1 2 / 3";139		try {140			org.apache.commons.math.fraction.Fraction c = properFormat.parse(source);141			junit.framework.Assert.assertNotNull(c);142			junit.framework.Assert.assertEquals(5, c.getNumerator());143			junit.framework.Assert.assertEquals(3, c.getDenominator());144		} catch (java.text.ParseException ex) {145			junit.framework.Assert.fail(ex.getMessage());146		}147		try {148			improperFormat.parse(source);149			junit.framework.Assert.fail("invalid improper fraction.");150		} catch (java.text.ParseException ex) {151		}152	}153	public void testParseProperNegative() {154		java.lang.String source = "-1 2 / 3";155		try {156			org.apache.commons.math.fraction.Fraction c = properFormat.parse(source);157			junit.framework.Assert.assertNotNull(c);158			junit.framework.Assert.assertEquals(-5, c.getNumerator());159			junit.framework.Assert.assertEquals(3, c.getDenominator());160		} catch (java.text.ParseException ex) {161			junit.framework.Assert.fail(ex.getMessage());162		}163		try {164			improperFormat.parse(source);165			junit.framework.Assert.fail("invalid improper fraction.");166		} catch (java.text.ParseException ex) {167		}168	}169	public void testParseProperInvalidMinus() {170		java.lang.String source = "2 -2 / 3";171		try {172			properFormat.parse(source);173			junit.framework.Assert.fail("invalid minus in improper fraction.");174		} catch (java.text.ParseException ex) {175		}176		source = "2 2 / -3";177		try {178			properFormat.parse(source);179			junit.framework.Assert.fail("invalid minus in improper fraction.");180		} catch (java.text.ParseException ex) {181		}182	}183	public void testNumeratorFormat() {184		java.text.NumberFormat old = properFormat.getNumeratorFormat();185		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();186		nf.setParseIntegerOnly(true);187		properFormat.setNumeratorFormat(nf);188		junit.framework.Assert.assertEquals(nf, properFormat.getNumeratorFormat());189		properFormat.setNumeratorFormat(old);190		old = improperFormat.getNumeratorFormat();191		nf = java.text.NumberFormat.getInstance();192		nf.setParseIntegerOnly(true);193		improperFormat.setNumeratorFormat(nf);194		junit.framework.Assert.assertEquals(nf, improperFormat.getNumeratorFormat());195		improperFormat.setNumeratorFormat(old);196	}197	public void testDenominatorFormat() {198		java.text.NumberFormat old = properFormat.getDenominatorFormat();199		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();200		nf.setParseIntegerOnly(true);201		properFormat.setDenominatorFormat(nf);202		junit.framework.Assert.assertEquals(nf, properFormat.getDenominatorFormat());203		properFormat.setDenominatorFormat(old);204		old = improperFormat.getDenominatorFormat();205		nf = java.text.NumberFormat.getInstance();206		nf.setParseIntegerOnly(true);207		improperFormat.setDenominatorFormat(nf);208		junit.framework.Assert.assertEquals(nf, improperFormat.getDenominatorFormat());209		improperFormat.setDenominatorFormat(old);210	}211	public void testWholeFormat() {212		org.apache.commons.math.fraction.ProperFractionFormat format = ((org.apache.commons.math.fraction.ProperFractionFormat)(properFormat));213		java.text.NumberFormat old = format.getWholeFormat();214		java.text.NumberFormat nf = java.text.NumberFormat.getInstance();215		nf.setParseIntegerOnly(true);216		format.setWholeFormat(nf);217		junit.framework.Assert.assertEquals(nf, format.getWholeFormat());218		format.setWholeFormat(old);219	}220	public void testLongFormat() {221		junit.framework.Assert.assertEquals("10 / 1", improperFormat.format(10L));222	}223	public void testDoubleFormat() {224		junit.framework.Assert.assertEquals("355 / 113", improperFormat.format(java.lang.Math.PI));225	}226}...

Full Screen

Full Screen

Source:FunctionEvaluationExceptionTest.java Github

copy

Full Screen

...4		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(0.0);5		junit.framework.Assert.assertNull(ex.getCause());6		junit.framework.Assert.assertNotNull(ex.getMessage());7		junit.framework.Assert.assertTrue(((ex.getMessage().indexOf("0")) > 0));8		junit.framework.Assert.assertEquals(0.0, ex.getArgument()[0], 0);9	}10	public void testConstructorArray() {11		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(new double[]{ 0 , 1 , 2 });12		junit.framework.Assert.assertNull(ex.getCause());13		junit.framework.Assert.assertNotNull(ex.getMessage());14		junit.framework.Assert.assertTrue(((ex.getMessage().indexOf("0")) > 0));15		junit.framework.Assert.assertEquals(0.0, ex.getArgument()[0], 0);16		junit.framework.Assert.assertEquals(1.0, ex.getArgument()[1], 0);17		junit.framework.Assert.assertEquals(2.0, ex.getArgument()[2], 0);18	}19	public void testConstructorPatternArguments() {20		java.lang.String pattern = "evaluation failed for argument = {0}";21		java.lang.Object[] arguments = new java.lang.Object[]{ java.lang.Double.valueOf(0.0) };22		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(0.0 , pattern , arguments);23		junit.framework.Assert.assertNull(ex.getCause());24		junit.framework.Assert.assertEquals(pattern, ex.getPattern());25		junit.framework.Assert.assertEquals(arguments.length, ex.getArguments().length);26		for (int i = 0 ; i < (arguments.length) ; ++i) {27			junit.framework.Assert.assertEquals(arguments[i], ex.getArguments()[i]);28		}29		junit.framework.Assert.assertFalse(pattern.equals(ex.getMessage()));30		junit.framework.Assert.assertFalse(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));31	}32	public void testConstructorArrayPatternArguments() {33		java.lang.String pattern = "evaluation failed for argument = {0}";34		java.lang.Object[] arguments = new java.lang.Object[]{ java.lang.Double.valueOf(0.0) };35		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(new double[]{ 0 , 1 , 2 } , pattern , arguments);36		junit.framework.Assert.assertNull(ex.getCause());37		junit.framework.Assert.assertEquals(pattern, ex.getPattern());38		junit.framework.Assert.assertEquals(arguments.length, ex.getArguments().length);39		for (int i = 0 ; i < (arguments.length) ; ++i) {40			junit.framework.Assert.assertEquals(arguments[i], ex.getArguments()[i]);41		}42		junit.framework.Assert.assertFalse(pattern.equals(ex.getMessage()));43		junit.framework.Assert.assertFalse(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));44		junit.framework.Assert.assertEquals(0.0, ex.getArgument()[0], 0);45		junit.framework.Assert.assertEquals(1.0, ex.getArgument()[1], 0);46		junit.framework.Assert.assertEquals(2.0, ex.getArgument()[2], 0);47	}48	public void testConstructorPatternArgumentsCause() {49		java.lang.String pattern = "evaluation failed for argument = {0}";50		java.lang.Object[] arguments = new java.lang.Object[]{ java.lang.Double.valueOf(0.0) };51		java.lang.String inMsg = "inner message";52		java.lang.Exception cause = new java.lang.Exception(inMsg);53		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(cause , 0.0 , pattern , arguments);54		junit.framework.Assert.assertEquals(cause, ex.getCause());55		junit.framework.Assert.assertEquals(pattern, ex.getPattern());56		junit.framework.Assert.assertEquals(arguments.length, ex.getArguments().length);57		for (int i = 0 ; i < (arguments.length) ; ++i) {58			junit.framework.Assert.assertEquals(arguments[i], ex.getArguments()[i]);59		}60		junit.framework.Assert.assertFalse(pattern.equals(ex.getMessage()));61		junit.framework.Assert.assertFalse(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));62	}63	public void testConstructorArrayPatternArgumentsCause() {64		java.lang.String pattern = "evaluation failed for argument = {0}";65		java.lang.Object[] arguments = new java.lang.Object[]{ java.lang.Double.valueOf(0.0) };66		java.lang.String inMsg = "inner message";67		java.lang.Exception cause = new java.lang.Exception(inMsg);68		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(cause , new double[]{ 0 , 1 , 2 } , pattern , arguments);69		junit.framework.Assert.assertEquals(cause, ex.getCause());70		junit.framework.Assert.assertEquals(pattern, ex.getPattern());71		junit.framework.Assert.assertEquals(arguments.length, ex.getArguments().length);72		for (int i = 0 ; i < (arguments.length) ; ++i) {73			junit.framework.Assert.assertEquals(arguments[i], ex.getArguments()[i]);74		}75		junit.framework.Assert.assertFalse(pattern.equals(ex.getMessage()));76		junit.framework.Assert.assertFalse(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));77		junit.framework.Assert.assertEquals(0.0, ex.getArgument()[0], 0);78		junit.framework.Assert.assertEquals(1.0, ex.getArgument()[1], 0);79		junit.framework.Assert.assertEquals(2.0, ex.getArgument()[2], 0);80	}81	public void testConstructorArgumentCause() {82		java.lang.String inMsg = "inner message";83		java.lang.Exception cause = new java.lang.Exception(inMsg);84		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(cause , 0.0);85		junit.framework.Assert.assertEquals(cause, ex.getCause());86		junit.framework.Assert.assertTrue(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));87	}88	public void testConstructorArrayArgumentCause() {89		java.lang.String inMsg = "inner message";90		java.lang.Exception cause = new java.lang.Exception(inMsg);91		org.apache.commons.math.FunctionEvaluationException ex = new org.apache.commons.math.FunctionEvaluationException(cause , new double[]{ 0 , 1 , 2 });92		junit.framework.Assert.assertEquals(cause, ex.getCause());93		junit.framework.Assert.assertTrue(ex.getMessage().equals(ex.getMessage(java.util.Locale.FRENCH)));94		junit.framework.Assert.assertEquals(0.0, ex.getArgument()[0], 0);95		junit.framework.Assert.assertEquals(1.0, ex.getArgument()[1], 0);96		junit.framework.Assert.assertEquals(2.0, ex.getArgument()[2], 0);97	}98}...

Full Screen

Full Screen

Source:PolynomialFunctionLagrangeFormTest.java Github

copy

Full Screen

...12		p = new org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm(x , y);13		z = 2.0;14		expected = -1.0;15		result = p.value(z);16		junit.framework.Assert.assertEquals(expected, result, tolerance);17		z = 4.5;18		expected = 2.75;19		result = p.value(z);20		junit.framework.Assert.assertEquals(expected, result, tolerance);21		z = 6.0;22		expected = 5.0;23		result = p.value(z);24		junit.framework.Assert.assertEquals(expected, result, tolerance);25		junit.framework.Assert.assertEquals(1, p.degree());26		c = p.getCoefficients();27		junit.framework.Assert.assertEquals(2, c.length);28		junit.framework.Assert.assertEquals(-4.0, c[0], tolerance);29		junit.framework.Assert.assertEquals(1.5, c[1], tolerance);30	}31	public void testQuadraticFunction() throws org.apache.commons.math.MathException {32		org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm p;33		double[] c;34		double z;35		double expected;36		double result;37		double tolerance = 1.0E-12;38		double[] x = new double[]{ 0.0 , -1.0 , 0.5 };39		double[] y = new double[]{ -3.0 , -6.0 , 0.0 };40		p = new org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm(x , y);41		z = 1.0;42		expected = 4.0;43		result = p.value(z);44		junit.framework.Assert.assertEquals(expected, result, tolerance);45		z = 2.5;46		expected = 22.0;47		result = p.value(z);48		junit.framework.Assert.assertEquals(expected, result, tolerance);49		z = -2.0;50		expected = -5.0;51		result = p.value(z);52		junit.framework.Assert.assertEquals(expected, result, tolerance);53		junit.framework.Assert.assertEquals(2, p.degree());54		c = p.getCoefficients();55		junit.framework.Assert.assertEquals(3, c.length);56		junit.framework.Assert.assertEquals(-3.0, c[0], tolerance);57		junit.framework.Assert.assertEquals(5.0, c[1], tolerance);58		junit.framework.Assert.assertEquals(2.0, c[2], tolerance);59	}60	public void testQuinticFunction() throws org.apache.commons.math.MathException {61		org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm p;62		double[] c;63		double z;64		double expected;65		double result;66		double tolerance = 1.0E-12;67		double[] x = new double[]{ 1.0 , -1.0 , 2.0 , 3.0 , -3.0 , 0.5 };68		double[] y = new double[]{ 0.0 , 0.0 , -24.0 , 0.0 , -144.0 , 2.34375 };69		p = new org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm(x , y);70		z = 0.0;71		expected = 0.0;72		result = p.value(z);73		junit.framework.Assert.assertEquals(expected, result, tolerance);74		z = -2.0;75		expected = 0.0;76		result = p.value(z);77		junit.framework.Assert.assertEquals(expected, result, tolerance);78		z = 4.0;79		expected = 360.0;80		result = p.value(z);81		junit.framework.Assert.assertEquals(expected, result, tolerance);82		junit.framework.Assert.assertEquals(5, p.degree());83		c = p.getCoefficients();84		junit.framework.Assert.assertEquals(6, c.length);85		junit.framework.Assert.assertEquals(0.0, c[0], tolerance);86		junit.framework.Assert.assertEquals(6.0, c[1], tolerance);87		junit.framework.Assert.assertEquals(1.0, c[2], tolerance);88		junit.framework.Assert.assertEquals(-7.0, c[3], tolerance);89		junit.framework.Assert.assertEquals(-1.0, c[4], tolerance);90		junit.framework.Assert.assertEquals(1.0, c[5], tolerance);91	}92	public void testParameters() throws java.lang.Exception {93		try {94			double[] x = new double[]{ 1.0 };95			double[] y = new double[]{ 2.0 };96			new org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm(x , y);97			junit.framework.Assert.fail("Expecting IllegalArgumentException - bad input array length");98		} catch (java.lang.IllegalArgumentException ex) {99		}100		try {101			double[] x = new double[]{ 1.0 , 2.0 , 3.0 , 4.0 };102			double[] y = new double[]{ 0.0 , -4.0 , -24.0 };103			new org.apache.commons.math.analysis.polynomials.PolynomialFunctionLagrangeForm(x , y);104			junit.framework.Assert.fail("Expecting IllegalArgumentException - mismatch input arrays");...

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import static org.junit.Assert.assertEquals;2import org.junit.Test;3public class TestJunit1 {4   String message = "Robert";	5   MessageUtil messageUtil = new MessageUtil(message);6   public void testPrintMessage() {	7      System.out.println("Inside testPrintMessage()");    8      assertEquals(message,messageUtil.printMessage());9   }10}11import static org.junit.Assert.assertEquals;12import org.junit.Test;13import org.junit.Ignore;14import org.junit.Before;15import org.junit.After;16public class TestJunit2 {17   String message = "Robert";	18   MessageUtil messageUtil = new MessageUtil(message);19   public void setUp(){20      System.out.println("Inside setUp()");      21   }22   public void testPrintMessage() {	23      System.out.println("Inside testPrintMessage()");    24      message = "Robert";     25      assertEquals(message,messageUtil.printMessage());26   }27   public void testSalutationMessage(){28      System.out.println("Inside testSalutationMessage()");29      message = "Hi!" + "Robert";30      assertEquals(message,messageUtil.salutationMessage());31   }32   public void tearDown(){33      System.out.println("Inside tearDown()");34   }35}36import junit.framework.TestCase;37public class TestJunit3 extends TestCase {38   String message = "Robert";	39   MessageUtil messageUtil = new MessageUtil(message);40   public TestJunit3(String testName){41      super(testName);42   }43   public void testPrintMessage() {	44      System.out.println("Inside testPrintMessage()");    45      assertEquals(message,messageUtil.printMessage());46   }47}48import junit.framework.TestCase;49public class TestJunit4 extends TestCase {50   String message = "Robert";	51   MessageUtil messageUtil = new MessageUtil(message);52   public TestJunit4(String testName){53      super(testName);54   }55   public void testPrintMessage() {	56      System.out.println("Inside testPrintMessage()");    57      assertEquals(message,messageUtil.printMessage());58   }59}60import junit.framework.TestCase;61public class TestJunit5 extends TestCase {62   String message = "Robert";	63   MessageUtil messageUtil = new MessageUtil(message

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2public class TestJunit{3   public void testAdd() {4      String str = "Junit is working fine";5      assertEquals("Junit is working fine",str);6   }7}8import junit.framework.Assert;9public class TestJunit{10   public void testAdd() {11      String str = "Junit is working fine";12      Assert.assertEquals("Junit is working fine",str);13   }14}15import org.junit.Assert;16public class TestJunit{17   public void testAdd() {18      String str = "Junit is working fine";19      Assert.assertEquals("Junit is working fine",str);20   }21}22import org.junit.Assert;23public class TestJunit{24   public void testAdd() {25      String str = "Junit is working fine";26      assertEquals("Junit is working fine",str);27   }28}29import org.junit.Assert;30public class TestJunit{31   public void testAdd() {32      String str = "Junit is working fine";33      assertEquals("Junit is working fine",str);34      Assert.assertEquals("Junit is working fine",str);35   }36}37import org.junit.Assert;38public class TestJunit{39   public void testAdd() {40      String str = "Junit is working fine";41      Assert.assertEquals("Junit is working fine",str);42      assertEquals("Junit is working fine",str);43   }44}45import org.junit.Assert;46public class TestJunit{47   public void testAdd() {48      String str = "Junit is working fine";49      Assert.assertEquals("Junit is working fine",str);50      Assert.assertEquals("Junit is working fine",str);51   }52}53import org.junit.Assert;54public class TestJunit{55   public void testAdd() {56      String str = "Junit is working fine";57      assertEquals("Junit is working fine",str);

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2public class TestCalculator {3    private Calculator calculator;4    public void setUp() {5        calculator = new Calculator();6    }7    public void testAdd() {8        int result = calculator.add(4, 5);9        Assert.assertEquals(9, result);10    }11}12package com.javatpoint;13import org.junit.Assert;14import org.junit.Before;15import org.junit.Test;16public class TestCalculator {17    private Calculator calculator;18    public void setUp() {19        calculator = new Calculator();20    }21    public void testAdd() {22        int result = calculator.add(4, 5);23        Assert.assertEquals(9, result);24    }25}26package com.javatpoint;27import org.junit.Assert;28import org.junit.Before;29import org.junit.Test;30public class TestCalculator {31    private Calculator calculator;32    public void setUp() {33        calculator = new Calculator();34    }35    public void testAdd() {36        int result = calculator.add(4, 5);37        Assert.assertNotEquals(10, result);38    }39}40package com.javatpoint;41import org.junit.Assert;42import org.junit.Before;43import org.junit.Test;44public class TestCalculator {45    private Calculator calculator;46    public void setUp() {47        calculator = new Calculator();48    }49    public void testAdd() {50        int result = calculator.add(4, 5);

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2public void testAssertEquals() {3  String str1 = "junit";4  String str2 = "junit";5  assertEquals(str1, str2);6}7import static org.junit.Assert.*;8public void testAssertEquals() {9  String str1 = "junit";10  String str2 = "junit";11  assertEquals(str1, str2);12}13import org.testng.Assert;14public void testAssertEquals() {15  String str1 = "junit";16  String str2 = "junit";17  Assert.assertEquals(str1, str2);18}19import static org.testng.Assert.*;20public void testAssertEquals() {21  String str1 = "junit";22  String str2 = "junit";23  assertEquals(str1, str2);24}25import static org.testng.AssertJUnit.*;26public void testAssertEquals() {27  String str1 = "junit";28  String str2 = "junit";29  assertEquals(str1, str2);30}31import static org.testng.AssertJUnit.*;32public void testAssertEquals() {33  String str1 = "junit";34  String str2 = "junit";35  assertEquals(str1, str2);36}37import static org.testng.AssertJUnit.*;38public void testAssertEquals() {39  String str1 = "junit";40  String str2 = "junit";41  assertEquals(str1, str2);42}43import static org.testng.AssertJUnit.*;44public void testAssertEquals() {45  String str1 = "junit";46  String str2 = "junit";47  assertEquals(str1, str2);48}49import static org.testng.AssertJUnit.*;50public void testAssertEquals() {51  String str1 = "junit";52  String str2 = "junit";53  assertEquals(str1, str2);54}55import static org.testng.AssertJUnit.*;

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.*;2public class TestAssert extends TestCase {3   protected int value1, value2;4   protected void setUp(){5      value1 = 3;6      value2 = 3;7   }8   public void testAdd(){9      double result = value1 + value2;10      assertTrue(result == 6);11   }12}13java -cp .;junit-4.10.jar TestAssert14OK (1 test)15package com.javatpoint.junit;16import org.junit.jupiter.api.Test;17import static org.junit.jupiter.api.Assertions.assertEquals;18public class TestAssert {19   public void testAdd() {20      String str = "Junit is working fine";21      assertEquals("Junit is working fine",str);22   }23}

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2import org.junit.Test;3public class TestJunit {4   String message = "Robert";	5   MessageUtil messageUtil = new MessageUtil(message);6   public void testPrintMessage() {	7      System.out.println("Inside testPrintMessage()");    8      assertEquals(message,messageUtil.printMessage());9   }10}11Inside testPrintMessage()12OK (1 test)13package com.tutorialspoint.junit;14import org.junit.runner.RunWith;15import org.junit.runners.Suite;16@RunWith(Suite.class)17@Suite.SuiteClasses( { TestJunit1.class, TestJunit2.class })18public class TestSuite {   19}20OK (8 tests)21package com.tutorialspoint.junit;22import org.junit.runner.JUnitCore;23import org.junit.runner.Result;24import org.junit.runner.notification.Failure;25public class TestRunner {26   public static void main(String[] args) {27      Result result = JUnitCore.runClasses(TestJunit.class);28      for (Failure failure : result.getFailures()) {29         System.out.println(failure.toString());30      }31      System.out.println(result.wasSuccessful());32   }33}34Inside testPrintMessage()35OK (1 test)

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2public class Test {3    public static void main(String[] args) {4        String str = "Junit is working fine";5        Assert.assertEquals("Junit is working fine",str);6    }7}8import org.junit.jupiter.api.Assertions;9public class Test {10    public static void main(String[] args) {11        String str = "Junit is working fine";12        Assertions.assertEquals("Junit is working fine",str);13    }14}15import org.junit.jupiter.api.Assertions;16public class Test {17    public static void main(String[] args) {18        double a = 1.0;19        double b = 1.000000000000001;20        Assertions.assertEquals(a,b,0.001);21    }22}

Full Screen

Full Screen

assertEquals

Using AI Code Generation

copy

Full Screen

1package com.javacodegeeks.junit; 2import junit.framework.Assert; 3import org.junit.Test; 4public class JUnitStringTest { 5public void testStringEquals() { 6String str1 = "Junit is working fine"; 7String str2 = "Junit is working fine"; 8Assert.assertEquals(str1, str2); 9} 10}11package com.javacodegeeks.junit; 12import junit.framework.Assert; 13import org.junit.Test; 14public class JUnitStringTest { 15public void testStringEquals() { 16String str1 = "Junit is working fine"; 17String str2 = "Junit is working fine"; 18Assert.assertEquals(str1, str2); 19} 20}

Full Screen

Full Screen

JUnit Tutorial:

LambdaTest also has a detailed JUnit tutorial explaining its features, importance, advanced use cases, best practices, and more to help you get started with running your automation testing scripts.

JUnit Tutorial Chapters:

Here are the detailed JUnit testing chapters to help you get started:

  • Importance of Unit testing - Learn why Unit testing is essential during the development phase to identify bugs and errors.
  • Top Java Unit testing frameworks - Here are the upcoming JUnit automation testing frameworks that you can use in 2023 to boost your unit testing.
  • What is the JUnit framework
  • Why is JUnit testing important - Learn the importance and numerous benefits of using the JUnit testing framework.
  • Features of JUnit - Learn about the numerous features of JUnit and why developers prefer it.
  • JUnit 5 vs. JUnit 4: Differences - Here is a complete comparison between JUnit 5 and JUnit 4 testing frameworks.
  • Setting up the JUnit environment - Learn how to set up your JUnit testing environment.
  • Getting started with JUnit testing - After successfully setting up your JUnit environment, this chapter will help you get started with JUnit testing in no time.
  • Parallel testing with JUnit - Parallel Testing can be used to reduce test execution time and improve test efficiency. Learn how to perform parallel testing with JUnit.
  • Annotations in JUnit - When writing automation scripts with JUnit, we can use JUnit annotations to specify the type of methods in our test code. This helps us identify those methods when we run JUnit tests using Selenium WebDriver. Learn in detail what annotations are in JUnit.
  • Assertions in JUnit - Assertions are used to validate or test that the result of an action/functionality is the same as expected. Learn in detail what assertions are and how to use them while performing JUnit testing.
  • Parameterization in JUnit - Parameterized Test enables you to run the same automated test scripts with different variables. By collecting data on each method's test parameters, you can minimize time spent on writing tests. Learn how to use parameterization in JUnit.
  • Nested Tests In JUnit 5 - A nested class is a non-static class contained within another class in a hierarchical structure. It can share the state and setup of the outer class. Learn about nested annotations in JUnit 5 with examples.
  • Best practices for JUnit testing - Learn about the best practices, such as always testing key methods and classes, integrating JUnit tests with your build, and more to get the best possible results.
  • Advanced Use Cases for JUnit testing - Take a deep dive into the advanced use cases, such as how to run JUnit tests in Jupiter, how to use JUnit 5 Mockito for Unit testing, and more for JUnit testing.

JUnit Certification:

You can also check out our JUnit certification if you wish to take your career in Selenium automation testing with JUnit to the next level.

Run junit automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful