How to use Assert class of junit.framework package

Best junit code snippet using junit.framework.Assert

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");1018 } catch (java.lang.IllegalArgumentException e) {1019 }1020 try {1021 org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ 3 , 3 , -5.5 , -11 , -27.5 }, -1, true);1022 junit.framework.Assert.fail("an exception should have been thrown");1023 } catch (java.lang.IllegalArgumentException e) {1024 }1025 try {1026 org.apache.commons.math.util.MathUtils.checkOrder(new double[]{ 3 , -1 , 0 , -5.5 , -11 , -27.5 }, -1, false);1027 junit.framework.Assert.fail("an exception should have been thrown");1028 } catch (java.lang.IllegalArgumentException e) {1029 }1030 }1031}...

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:SummaryStatisticsTest.java Github

copy

Full Screen

...20 return new org.apache.commons.math.stat.descriptive.SummaryStatistics();21 }22 public void testStats() {23 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();24 junit.framework.Assert.assertEquals("total count", 0, u.getN(), tolerance);25 u.addValue(one);26 u.addValue(twoF);27 u.addValue(twoL);28 u.addValue(three);29 junit.framework.Assert.assertEquals("N", n, u.getN(), tolerance);30 junit.framework.Assert.assertEquals("sum", sum, u.getSum(), tolerance);31 junit.framework.Assert.assertEquals("sumsq", sumSq, u.getSumsq(), tolerance);32 junit.framework.Assert.assertEquals("var", var, u.getVariance(), tolerance);33 junit.framework.Assert.assertEquals("std", std, u.getStandardDeviation(), tolerance);34 junit.framework.Assert.assertEquals("mean", mean, u.getMean(), tolerance);35 junit.framework.Assert.assertEquals("min", min, u.getMin(), tolerance);36 junit.framework.Assert.assertEquals("max", max, u.getMax(), tolerance);37 u.clear();38 junit.framework.Assert.assertEquals("total count", 0, u.getN(), tolerance);39 }40 public void testN0andN1Conditions() throws java.lang.Exception {41 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();42 junit.framework.Assert.assertTrue("Mean of n = 0 set should be NaN", java.lang.Double.isNaN(u.getMean()));43 junit.framework.Assert.assertTrue("Standard Deviation of n = 0 set should be NaN", java.lang.Double.isNaN(u.getStandardDeviation()));44 junit.framework.Assert.assertTrue("Variance of n = 0 set should be NaN", java.lang.Double.isNaN(u.getVariance()));45 u.addValue(one);46 junit.framework.Assert.assertTrue("mean should be one (n = 1)", ((u.getMean()) == (one)));47 junit.framework.Assert.assertTrue(("geometric should be one (n = 1) instead it is " + (u.getGeometricMean())), ((u.getGeometricMean()) == (one)));48 junit.framework.Assert.assertTrue("Std should be zero (n = 1)", ((u.getStandardDeviation()) == 0.0));49 junit.framework.Assert.assertTrue("variance should be zero (n = 1)", ((u.getVariance()) == 0.0));50 u.addValue(twoF);51 junit.framework.Assert.assertTrue("Std should not be zero (n = 2)", ((u.getStandardDeviation()) != 0.0));52 junit.framework.Assert.assertTrue("variance should not be zero (n = 2)", ((u.getVariance()) != 0.0));53 }54 public void testProductAndGeometricMean() throws java.lang.Exception {55 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();56 u.addValue(1.0);57 u.addValue(2.0);58 u.addValue(3.0);59 u.addValue(4.0);60 junit.framework.Assert.assertEquals("Geometric mean not expected", 2.213364, u.getGeometricMean(), 1.0E-5);61 }62 public void testNaNContracts() {63 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();64 junit.framework.Assert.assertTrue("mean not NaN", java.lang.Double.isNaN(u.getMean()));65 junit.framework.Assert.assertTrue("min not NaN", java.lang.Double.isNaN(u.getMin()));66 junit.framework.Assert.assertTrue("std dev not NaN", java.lang.Double.isNaN(u.getStandardDeviation()));67 junit.framework.Assert.assertTrue("var not NaN", java.lang.Double.isNaN(u.getVariance()));68 junit.framework.Assert.assertTrue("geom mean not NaN", java.lang.Double.isNaN(u.getGeometricMean()));69 u.addValue(1.0);70 junit.framework.Assert.assertEquals("mean not expected", 1.0, u.getMean(), java.lang.Double.MIN_VALUE);71 junit.framework.Assert.assertEquals("variance not expected", 0.0, u.getVariance(), java.lang.Double.MIN_VALUE);72 junit.framework.Assert.assertEquals("geometric mean not expected", 1.0, u.getGeometricMean(), java.lang.Double.MIN_VALUE);73 u.addValue(-1.0);74 junit.framework.Assert.assertTrue("geom mean not NaN", java.lang.Double.isNaN(u.getGeometricMean()));75 u.addValue(0.0);76 junit.framework.Assert.assertTrue("geom mean not NaN", java.lang.Double.isNaN(u.getGeometricMean()));77 }78 public void testGetSummary() {79 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();80 org.apache.commons.math.stat.descriptive.StatisticalSummary summary = u.getSummary();81 verifySummary(u, summary);82 u.addValue(1.0);83 summary = u.getSummary();84 verifySummary(u, summary);85 u.addValue(2.0);86 summary = u.getSummary();87 verifySummary(u, summary);88 u.addValue(2.0);89 summary = u.getSummary();90 verifySummary(u, summary);91 }92 public void testSerialization() {93 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();94 org.apache.commons.math.TestUtils.checkSerializedEquality(u);95 org.apache.commons.math.stat.descriptive.SummaryStatistics s = ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(org.apache.commons.math.TestUtils.serializeAndRecover(u)));96 org.apache.commons.math.stat.descriptive.StatisticalSummary summary = s.getSummary();97 verifySummary(u, summary);98 u.addValue(2.0);99 u.addValue(1.0);100 u.addValue(3.0);101 u.addValue(4.0);102 u.addValue(5.0);103 org.apache.commons.math.TestUtils.checkSerializedEquality(u);104 s = ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(org.apache.commons.math.TestUtils.serializeAndRecover(u)));105 summary = s.getSummary();106 verifySummary(u, summary);107 }108 public void testEqualsAndHashCode() {109 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();110 org.apache.commons.math.stat.descriptive.SummaryStatistics t = null;111 int emptyHash = u.hashCode();112 junit.framework.Assert.assertTrue("reflexive", u.equals(u));113 junit.framework.Assert.assertFalse("non-null compared to null", u.equals(t));114 junit.framework.Assert.assertFalse("wrong type", u.equals(java.lang.Double.valueOf(0)));115 t = createSummaryStatistics();116 junit.framework.Assert.assertTrue("empty instances should be equal", t.equals(u));117 junit.framework.Assert.assertTrue("empty instances should be equal", u.equals(t));118 junit.framework.Assert.assertEquals("empty hash code", emptyHash, t.hashCode());119 u.addValue(2.0);120 u.addValue(1.0);121 u.addValue(3.0);122 u.addValue(4.0);123 junit.framework.Assert.assertFalse("different n's should make instances not equal", t.equals(u));124 junit.framework.Assert.assertFalse("different n's should make instances not equal", u.equals(t));125 junit.framework.Assert.assertTrue("different n's should make hashcodes different", ((u.hashCode()) != (t.hashCode())));126 t.addValue(2.0);127 t.addValue(1.0);128 t.addValue(3.0);129 t.addValue(4.0);130 junit.framework.Assert.assertTrue("summaries based on same data should be equal", t.equals(u));131 junit.framework.Assert.assertTrue("summaries based on same data should be equal", u.equals(t));132 junit.framework.Assert.assertEquals("summaries based on same data should have same hashcodes", u.hashCode(), t.hashCode());133 u.clear();134 t.clear();135 junit.framework.Assert.assertTrue("empty instances should be equal", t.equals(u));136 junit.framework.Assert.assertTrue("empty instances should be equal", u.equals(t));137 junit.framework.Assert.assertEquals("empty hash code", emptyHash, t.hashCode());138 junit.framework.Assert.assertEquals("empty hash code", emptyHash, u.hashCode());139 }140 public void testCopy() throws java.lang.Exception {141 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();142 u.addValue(2.0);143 u.addValue(1.0);144 u.addValue(3.0);145 u.addValue(4.0);146 org.apache.commons.math.stat.descriptive.SummaryStatistics v = new org.apache.commons.math.stat.descriptive.SummaryStatistics(u);147 junit.framework.Assert.assertEquals(u, v);148 junit.framework.Assert.assertEquals(v, u);149 junit.framework.Assert.assertTrue(((v.geoMean) == (v.getGeoMeanImpl())));150 junit.framework.Assert.assertTrue(((v.mean) == (v.getMeanImpl())));151 junit.framework.Assert.assertTrue(((v.min) == (v.getMinImpl())));152 junit.framework.Assert.assertTrue(((v.max) == (v.getMaxImpl())));153 junit.framework.Assert.assertTrue(((v.sum) == (v.getSumImpl())));154 junit.framework.Assert.assertTrue(((v.sumsq) == (v.getSumsqImpl())));155 junit.framework.Assert.assertTrue(((v.sumLog) == (v.getSumLogImpl())));156 junit.framework.Assert.assertTrue(((v.variance) == (v.getVarianceImpl())));157 u.addValue(7.0);158 u.addValue(9.0);159 u.addValue(11.0);160 u.addValue(23.0);161 v.addValue(7.0);162 v.addValue(9.0);163 v.addValue(11.0);164 v.addValue(23.0);165 junit.framework.Assert.assertEquals(u, v);166 junit.framework.Assert.assertEquals(v, u);167 u.clear();168 u.setSumImpl(new org.apache.commons.math.stat.descriptive.summary.Sum());169 org.apache.commons.math.stat.descriptive.SummaryStatistics.copy(u, v);170 junit.framework.Assert.assertEquals(u.sum, v.sum);171 junit.framework.Assert.assertEquals(u.getSumImpl(), v.getSumImpl());172 }173 private void verifySummary(org.apache.commons.math.stat.descriptive.SummaryStatistics u, org.apache.commons.math.stat.descriptive.StatisticalSummary s) {174 junit.framework.Assert.assertEquals("N", s.getN(), u.getN());175 org.apache.commons.math.TestUtils.assertEquals("sum", s.getSum(), u.getSum(), tolerance);176 org.apache.commons.math.TestUtils.assertEquals("var", s.getVariance(), u.getVariance(), tolerance);177 org.apache.commons.math.TestUtils.assertEquals("std", s.getStandardDeviation(), u.getStandardDeviation(), tolerance);178 org.apache.commons.math.TestUtils.assertEquals("mean", s.getMean(), u.getMean(), tolerance);179 org.apache.commons.math.TestUtils.assertEquals("min", s.getMin(), u.getMin(), tolerance);180 org.apache.commons.math.TestUtils.assertEquals("max", s.getMax(), u.getMax(), tolerance);181 }182 public void testSetterInjection() throws java.lang.Exception {183 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();184 u.setMeanImpl(new org.apache.commons.math.stat.descriptive.summary.Sum());185 u.setSumLogImpl(new org.apache.commons.math.stat.descriptive.summary.Sum());186 u.addValue(1);187 u.addValue(3);188 junit.framework.Assert.assertEquals(4, u.getMean(), 1.0E-14);189 junit.framework.Assert.assertEquals(4, u.getSumOfLogs(), 1.0E-14);190 junit.framework.Assert.assertEquals(java.lang.Math.exp(2), u.getGeometricMean(), 1.0E-14);191 u.clear();192 u.addValue(1);193 u.addValue(2);194 junit.framework.Assert.assertEquals(3, u.getMean(), 1.0E-14);195 u.clear();196 u.setMeanImpl(new org.apache.commons.math.stat.descriptive.moment.Mean());197 }198 public void testSetterIllegalState() throws java.lang.Exception {199 org.apache.commons.math.stat.descriptive.SummaryStatistics u = createSummaryStatistics();200 u.addValue(1);201 u.addValue(3);202 try {203 u.setMeanImpl(new org.apache.commons.math.stat.descriptive.summary.Sum());204 junit.framework.Assert.fail("Expecting IllegalStateException");205 } catch (java.lang.IllegalStateException ex) {206 }207 }208}...

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

1package org.apache.commons.math;2public class FunctionEvaluationExceptionTest extends junit.framework.TestCase {3 public void testConstructor() {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:StackFilterTest.java Github

copy

Full Screen

...9 @Override10 protected void setUp() {11 StringWriter swin = new StringWriter();12 PrintWriter pwin = new PrintWriter(swin);13 pwin.println("junit.framework.AssertionFailedError");14 pwin.println(" at junit.framework.Assert.fail(Assert.java:144)");15 pwin.println(" at junit.framework.Assert.assert(Assert.java:19)");16 pwin.println(" at junit.framework.Assert.assert(Assert.java:26)");17 pwin.println(" at MyTest.f(MyTest.java:13)");18 pwin.println(" at MyTest.testStackTrace(MyTest.java:8)");19 pwin.println(" at java.lang.reflect.Method.invoke(Native Method)");20 pwin.println(" at junit.framework.TestCase.runTest(TestCase.java:156)");21 pwin.println(" at junit.framework.TestCase.runBare(TestCase.java:130)");22 pwin.println(" at junit.framework.TestResult$1.protect(TestResult.java:100)");23 pwin.println(" at junit.framework.TestResult.runProtected(TestResult.java:118)");24 pwin.println(" at junit.framework.TestResult.run(TestResult.java:103)");25 pwin.println(" at junit.framework.TestCase.run(TestCase.java:121)");26 pwin.println(" at junit.framework.TestSuite.runTest(TestSuite.java:157)");27 pwin.println(" at junit.framework.TestSuite.run(TestSuite.java, Compiled Code)");28 pwin.println(" at junit.swingui.TestRunner$17.run(TestRunner.java:669)");29 fUnfiltered = swin.toString();30 StringWriter swout = new StringWriter();31 PrintWriter pwout = new PrintWriter(swout);32 pwout.println("junit.framework.AssertionFailedError");33 pwout.println(" at MyTest.f(MyTest.java:13)");34 pwout.println(" at MyTest.testStackTrace(MyTest.java:8)");35 fFiltered = swout.toString();36 }37 public void testFilter() {38 assertEquals(fFiltered, BaseTestRunner.getFilteredTrace(fUnfiltered));39 }40}

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2import junit.framework.TestCase;3public class TestJunit1 extends TestCase {4 protected int value1, value2;5 protected void setUp(){6 value1 = 3;7 value2 = 3;8 }9 public void testAdd(){10 double result = value1 + value2;11 assertTrue(result == 6);12 }13}14import org.junit.Assert;15import org.junit.Test;16public class TestJunit2 {17 public void testAdd() {18 String str= "Junit is working fine";19 Assert.assertEquals("Junit is working fine",str);20 }21}22javac -cp .;junit-4.13-beta-3.jar TestJunit1.java TestJunit2.java23java -cp .;junit-4.13-beta-3.jar;hamcrest-core-1.3.jar org.junit.runner.JUnitCore TestJunit1 TestJunit2

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2import org.junit.Test;3public class TestAssert {4 public void testAssertArrayEquals() {5 byte[] expected = "trial".getBytes();6 byte[] actual = "trial".getBytes();7 Assert.assertArrayEquals("failure - byte arrays not same", expected, actual);8 }9 public void testAssertEquals() {10 Assert.assertEquals("failure - strings are not equal", "text", "text");11 }12 public void testAssertFalse() {13 Assert.assertFalse("failure - should be false", false);14 }15 public void testAssertNotNull() {16 Assert.assertNotNull("should not be null", new Object());17 }18 public void testAssertNotSame() {19 Assert.assertNotSame("should not be same Object", new Object(), new Object());20 }21 public void testAssertNull() {22 Assert.assertNull("should be null", null);23 }24 public void testAssertSame() {25 Integer aNumber = Integer.valueOf(768);26 Assert.assertSame("should be same", aNumber, aNumber);27 }28 public void testAssertThatBothContainsString() {29 Assert.assertThat("albumen", both(containsString("a")).and(containsString("b")));30 }31 public void testAssertThathasItemsContainsString() {32 Assert.assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));33 }34 public void testAssertThatEveryItemContainsString() {35 Assert.assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));36 }37 public void testAssertThatHamcrestCoreMatchers() {38 assertThat("good", allOf(equalTo("good"), startsWith("good")));39 assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));40 assertThat("good", anyOf(equalTo("bad"), equalTo("good")));41 assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));42 assertThat(new Object(), not(sameInstance(new Object())));43 }44 public void testAssertTrue()

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import static org.junit.Assert.*;2import org.junit.Test;3public class TestJunit1 {4 String message = "Robert"; 5 MessageUtil messageUtil = new MessageUtil(message);6 public void testPrintMessage() {7 assertEquals(message,messageUtil.printMessage());8 }9}10package com.javatpoint.junit;11import org.junit.Test;12import static org.junit.Assert.*;13public class TestJunit4 {14 String message = "Robert"; 15 MessageUtil messageUtil = new MessageUtil(message);16 public void testPrintMessage() { 17 assertEquals(message,messageUtil.printMessage());18 }19}20public void starting(Description description)21public void finished(Description description)

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import static org.junit.Assert.*;2public class AssertTest {3 public void testAssertArrayEquals() {4 byte[] expected = "trial".getBytes();5 byte[] actual = "trial".getBytes();6 assertArrayEquals("failure - byte arrays not same", expected, actual);7 }8 public void testAssertEquals() {9 assertEquals("failure - strings are not equal", "text", "text");10 }11 public void testAssertFalse() {12 assertFalse("failure - should be false", false);13 }14 public void testAssertNotNull() {15 assertNotNull("should not be null", new Object());16 }17 public void testAssertNotSame() {18 assertNotSame("should not be same Object", new Object(), new Object());19 }20 public void testAssertNull() {21 assertNull("should be null", null);22 }23 public void testAssertSame() {24 Integer aNumber = Integer.valueOf(768);25 assertSame("should be same", aNumber, aNumber);26 }27 public void testAssertThatBothContainsString() {28 assertThat("albumen", both(containsString("a")).and(containsString("b")));29 }30 public void testAssertThathasItemsContainsString() {31 assertThat(Arrays.asList("one", "two", "three"), hasItems("one", "three"));32 }33 public void testAssertThatEveryItemContainsString() {34 assertThat(Arrays.asList(new String[] { "fun", "ban", "net" }), everyItem(containsString("n")));35 }36 public void testAssertThatHamcrestCoreMatchers() {37 assertThat("good", allOf(equalTo("good"), startsWith("good")));38 assertThat("good", not(allOf(equalTo("bad"), equalTo("good"))));39 assertThat("good", anyOf(equalTo("bad"), equalTo("good")));40 assertThat(7, not(CombinableMatcher.<Integer> either(equalTo(3)).or(equalTo(4))));41 assertThat(new Object(), not(sameInstance(new Object())));42 }43 public void testAssertTrue() {44 assertTrue("failure - should be true", true);

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2import org.junit.Test;3public class TestAssert {4public void testAssert() {5String str1 = new String ("abc");6String str2 = new String ("abc");7String str3 = null;8String str4 = "abc";9String str5 = "abc";10int val1 = 5;11int val2 = 6;12String[] expectedArray = {"one", "two", "three"};13String[] resultArray = {"one", "two", "three"};14Assert.assertEquals(str1, str2);15Assert.assertTrue (val1 < val2);16Assert.assertFalse (val1 > val2);17Assert.assertNotNull(str1);18Assert.assertSame(str4,str5);19Assert.assertNotSame(str1,str3);20Assert.assertArrayEquals(expectedArray, resultArray);21}22}23java -cp .;junit-4.12.jar;hamcrest-core-1.3.jar TestAssert24OK (1 test)25import org.junit.Assert;26import org.junit.Test;27public class TestAssert {28public void testAssert() {29String str1 = new String ("abc");30String str2 = new String ("abc");31String str3 = null;32String str4 = "abc";33String str5 = "abc";34int val1 = 5;35int val2 = 6;36String[] expectedArray = {"one", "two", "three"};37String[] resultArray = {"one", "two", "three"};38Assert.assertEquals(str1, str2);39Assert.assertTrue (val1 < val2);40Assert.assertFalse (val1 > val2);41Assert.assertNotNull(str1);

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1public class AssertTest {2 public static void main(String[] args) {3 Assert.assertEquals(1, 1);4 }5}6 at AssertTest.main(AssertTest.java:5)7 at java.net.URLClassLoader$1.run(URLClassLoader.java:202)8 at java.security.AccessController.doPrivileged(Native Method)9 at java.net.URLClassLoader.findClass(URLClassLoader.java:190)10 at java.lang.ClassLoader.loadClass(ClassLoader.java:306)11 at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)12 at java.lang.ClassLoader.loadClass(ClassLoader.java:247)

Full Screen

Full Screen

Assert

Using AI Code Generation

copy

Full Screen

1import static org.junit.Assert.*;2import org.junit.Test;3import org.junit.Before;4import org.junit.After;5public class CalculatorTest {6 private Calculator calculator;7 public void setUp() throws Exception {8 calculator = new Calculator();9 }10 public void tearDown() throws Exception {11 calculator = null;12 }13 public void testAdd() {14 int result = calculator.add(10, 50);15 assertEquals(60, result);16 }17 public void testSubtract() {18 int result = calculator.subtract(10, 50);19 assertEquals(-40, result);20 }21 public void testMultiply() {22 int result = calculator.multiply(10, 50);23 assertEquals(500, result);24 }25 public void testDivide() {26 int result = calculator.divide(10, 50);27 assertEquals(0, result);28 }29}

Full Screen

Full Screen
copy
1a.f(b); <-> b.f(a);2
Full Screen
copy
1static <T> T checkNotNull(T e) {2 if (e == null) {3 throw new NullPointerException();4 }5 return e;6}7
Full Screen
copy
1import static com.googlecode.catchexception.apis.BDDCatchException.*;23@Test4public void testFooThrowsIndexOutOfBoundsException() {56 when(() -> foo.doStuff());78 then(caughtException()).isInstanceOf(IndexOutOfBoundsException.class);910}11
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.

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful