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

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

Source:Array2DRowRealMatrixTest.java Github

copy

Full Screen

...71		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);72		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);73		try {74			m.add(m2);75			junit.framework.Assert.fail("IllegalArgumentException expected");76		} catch (java.lang.IllegalArgumentException ex) {77		}78	}79	public void testNorm() {80		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);81		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);82		junit.framework.Assert.assertEquals("testData norm", 14.0, m.getNorm(), entryTolerance);83		junit.framework.Assert.assertEquals("testData2 norm", 7.0, m2.getNorm(), entryTolerance);84	}85	public void testFrobeniusNorm() {86		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);87		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);88		junit.framework.Assert.assertEquals("testData Frobenius norm", java.lang.Math.sqrt(117.0), m.getFrobeniusNorm(), entryTolerance);89		junit.framework.Assert.assertEquals("testData2 Frobenius norm", java.lang.Math.sqrt(52.0), m2.getFrobeniusNorm(), entryTolerance);90	}91	public void testPlusMinus() {92		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);93		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix(testDataInv);94		org.apache.commons.math.TestUtils.assertEquals("m-n = m + -n", m.subtract(m2), m2.scalarMultiply(-1.0).add(m), entryTolerance);95		try {96			m.subtract(new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2));97			junit.framework.Assert.fail("Expecting illegalArgumentException");98		} catch (java.lang.IllegalArgumentException ex) {99		}100	}101	public void testMultiply() {102		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);103		org.apache.commons.math.linear.Array2DRowRealMatrix mInv = new org.apache.commons.math.linear.Array2DRowRealMatrix(testDataInv);104		org.apache.commons.math.linear.Array2DRowRealMatrix identity = new org.apache.commons.math.linear.Array2DRowRealMatrix(id);105		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);106		org.apache.commons.math.TestUtils.assertEquals("inverse multiply", m.multiply(mInv), identity, entryTolerance);107		org.apache.commons.math.TestUtils.assertEquals("inverse multiply", mInv.multiply(m), identity, entryTolerance);108		org.apache.commons.math.TestUtils.assertEquals("identity multiply", m.multiply(identity), m, entryTolerance);109		org.apache.commons.math.TestUtils.assertEquals("identity multiply", identity.multiply(mInv), mInv, entryTolerance);110		org.apache.commons.math.TestUtils.assertEquals("identity multiply", m2.multiply(identity), m2, entryTolerance);111		try {112			m.multiply(new org.apache.commons.math.linear.Array2DRowRealMatrix(bigSingular));113			junit.framework.Assert.fail("Expecting illegalArgumentException");114		} catch (java.lang.IllegalArgumentException ex) {115		}116	}117	private double[][] d3 = new double[][]{ new double[]{ 1 , 2 , 3 , 4 } , new double[]{ 5 , 6 , 7 , 8 } };118	private double[][] d4 = new double[][]{ new double[]{ 1 } , new double[]{ 2 } , new double[]{ 3 } , new double[]{ 4 } };119	private double[][] d5 = new double[][]{ new double[]{ 30 } , new double[]{ 70 } };120	public void testMultiply2() {121		org.apache.commons.math.linear.RealMatrix m3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d3);122		org.apache.commons.math.linear.RealMatrix m4 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d4);123		org.apache.commons.math.linear.RealMatrix m5 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d5);124		org.apache.commons.math.TestUtils.assertEquals("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);125	}126	public void testTrace() {127		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(id);128		junit.framework.Assert.assertEquals("identity trace", 3.0, m.getTrace(), entryTolerance);129		m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);130		try {131			m.getTrace();132			junit.framework.Assert.fail("Expecting NonSquareMatrixException");133		} catch (org.apache.commons.math.linear.NonSquareMatrixException ex) {134		}135	}136	public void testScalarAdd() {137		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);138		org.apache.commons.math.TestUtils.assertEquals("scalar add", new org.apache.commons.math.linear.Array2DRowRealMatrix(testDataPlus2), m.scalarAdd(2.0), entryTolerance);139	}140	public void testOperate() {141		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(id);142		org.apache.commons.math.TestUtils.assertEquals("identity operate", testVector, m.operate(testVector), entryTolerance);143		org.apache.commons.math.TestUtils.assertEquals("identity operate", testVector, m.operate(new org.apache.commons.math.linear.ArrayRealVector(testVector)).getData(), entryTolerance);144		m = new org.apache.commons.math.linear.Array2DRowRealMatrix(bigSingular);145		try {146			m.operate(testVector);147			junit.framework.Assert.fail("Expecting illegalArgumentException");148		} catch (java.lang.IllegalArgumentException ex) {149		}150	}151	public void testMath209() {152		org.apache.commons.math.linear.RealMatrix a = new org.apache.commons.math.linear.Array2DRowRealMatrix(new double[][]{ new double[]{ 1 , 2 } , new double[]{ 3 , 4 } , new double[]{ 5 , 6 } } , false);153		double[] b = a.operate(new double[]{ 1 , 1 });154		junit.framework.Assert.assertEquals(a.getRowDimension(), b.length);155		junit.framework.Assert.assertEquals(3.0, b[0], 1.0E-12);156		junit.framework.Assert.assertEquals(7.0, b[1], 1.0E-12);157		junit.framework.Assert.assertEquals(11.0, b[2], 1.0E-12);158	}159	public void testTranspose() {160		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);161		org.apache.commons.math.linear.RealMatrix mIT = new org.apache.commons.math.linear.LUDecompositionImpl(m).getSolver().getInverse().transpose();162		org.apache.commons.math.linear.RealMatrix mTI = new org.apache.commons.math.linear.LUDecompositionImpl(m.transpose()).getSolver().getInverse();163		org.apache.commons.math.TestUtils.assertEquals("inverse-transpose", mIT, mTI, normTolerance);164		m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2);165		org.apache.commons.math.linear.RealMatrix mt = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData2T);166		org.apache.commons.math.TestUtils.assertEquals("transpose", mt, m.transpose(), normTolerance);167	}168	public void testPremultiplyVector() {169		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);170		org.apache.commons.math.TestUtils.assertEquals("premultiply", m.preMultiply(testVector), preMultTest, normTolerance);171		org.apache.commons.math.TestUtils.assertEquals("premultiply", m.preMultiply(new org.apache.commons.math.linear.ArrayRealVector(testVector).getData()), preMultTest, normTolerance);172		m = new org.apache.commons.math.linear.Array2DRowRealMatrix(bigSingular);173		try {174			m.preMultiply(testVector);175			junit.framework.Assert.fail("expecting IllegalArgumentException");176		} catch (java.lang.IllegalArgumentException ex) {177		}178	}179	public void testPremultiply() {180		org.apache.commons.math.linear.RealMatrix m3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d3);181		org.apache.commons.math.linear.RealMatrix m4 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d4);182		org.apache.commons.math.linear.RealMatrix m5 = new org.apache.commons.math.linear.Array2DRowRealMatrix(d5);183		org.apache.commons.math.TestUtils.assertEquals("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);184		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);185		org.apache.commons.math.linear.Array2DRowRealMatrix mInv = new org.apache.commons.math.linear.Array2DRowRealMatrix(testDataInv);186		org.apache.commons.math.linear.Array2DRowRealMatrix identity = new org.apache.commons.math.linear.Array2DRowRealMatrix(id);187		org.apache.commons.math.TestUtils.assertEquals("inverse multiply", m.preMultiply(mInv), identity, entryTolerance);188		org.apache.commons.math.TestUtils.assertEquals("inverse multiply", mInv.preMultiply(m), identity, entryTolerance);189		org.apache.commons.math.TestUtils.assertEquals("identity multiply", m.preMultiply(identity), m, entryTolerance);190		org.apache.commons.math.TestUtils.assertEquals("identity multiply", identity.preMultiply(mInv), mInv, entryTolerance);191		try {192			m.preMultiply(new org.apache.commons.math.linear.Array2DRowRealMatrix(bigSingular));193			junit.framework.Assert.fail("Expecting illegalArgumentException");194		} catch (java.lang.IllegalArgumentException ex) {195		}196	}197	public void testGetVectors() {198		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);199		org.apache.commons.math.TestUtils.assertEquals("get row", m.getRow(0), testDataRow1, entryTolerance);200		org.apache.commons.math.TestUtils.assertEquals("get col", m.getColumn(2), testDataCol3, entryTolerance);201		try {202			m.getRow(10);203			junit.framework.Assert.fail("expecting MatrixIndexException");204		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {205		}206		try {207			m.getColumn(-1);208			junit.framework.Assert.fail("expecting MatrixIndexException");209		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {210		}211	}212	public void testGetEntry() {213		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);214		junit.framework.Assert.assertEquals("get entry", m.getEntry(0, 1), 2.0, entryTolerance);215		try {216			m.getEntry(10, 4);217			junit.framework.Assert.fail("Expecting MatrixIndexException");218		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {219		}220	}221	public void testExamples() {222		double[][] matrixData = new double[][]{ new double[]{ 1.0 , 2.0 , 3.0 } , new double[]{ 2.0 , 5.0 , 3.0 } };223		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(matrixData);224		double[][] matrixData2 = new double[][]{ new double[]{ 1.0 , 2.0 } , new double[]{ 2.0 , 5.0 } , new double[]{ 1.0 , 7.0 } };225		org.apache.commons.math.linear.RealMatrix n = new org.apache.commons.math.linear.Array2DRowRealMatrix(matrixData2);226		org.apache.commons.math.linear.RealMatrix p = m.multiply(n);227		junit.framework.Assert.assertEquals(2, p.getRowDimension());228		junit.framework.Assert.assertEquals(2, p.getColumnDimension());229		org.apache.commons.math.linear.RealMatrix pInverse = new org.apache.commons.math.linear.LUDecompositionImpl(p).getSolver().getInverse();230		junit.framework.Assert.assertEquals(2, pInverse.getRowDimension());231		junit.framework.Assert.assertEquals(2, pInverse.getColumnDimension());232		double[][] coefficientsData = new double[][]{ new double[]{ 2 , 3 , -2 } , new double[]{ -1 , 7 , 6 } , new double[]{ 4 , -3 , -5 } };233		org.apache.commons.math.linear.RealMatrix coefficients = new org.apache.commons.math.linear.Array2DRowRealMatrix(coefficientsData);234		double[] constants = new double[]{ 1 , -2 , 1 };235		double[] solution = new org.apache.commons.math.linear.LUDecompositionImpl(coefficients).getSolver().solve(constants);236		junit.framework.Assert.assertEquals((((2 * (solution[0])) + (3 * (solution[1]))) - (2 * (solution[2]))), constants[0], 1.0E-12);237		junit.framework.Assert.assertEquals(((((-1) * (solution[0])) + (7 * (solution[1]))) + (6 * (solution[2]))), constants[1], 1.0E-12);238		junit.framework.Assert.assertEquals((((4 * (solution[0])) - (3 * (solution[1]))) - (5 * (solution[2]))), constants[2], 1.0E-12);239	}240	public void testGetSubMatrix() {241		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);242		checkGetSubMatrix(m, subRows23Cols00, 2, 3, 0, 0, false);243		checkGetSubMatrix(m, subRows00Cols33, 0, 0, 3, 3, false);244		checkGetSubMatrix(m, subRows01Cols23, 0, 1, 2, 3, false);245		checkGetSubMatrix(m, subRows02Cols13, new int[]{ 0 , 2 }, new int[]{ 1 , 3 }, false);246		checkGetSubMatrix(m, subRows03Cols12, new int[]{ 0 , 3 }, new int[]{ 1 , 2 }, false);247		checkGetSubMatrix(m, subRows03Cols123, new int[]{ 0 , 3 }, new int[]{ 1 , 2 , 3 }, false);248		checkGetSubMatrix(m, subRows20Cols123, new int[]{ 2 , 0 }, new int[]{ 1 , 2 , 3 }, false);249		checkGetSubMatrix(m, subRows31Cols31, new int[]{ 3 , 1 }, new int[]{ 3 , 1 }, false);250		checkGetSubMatrix(m, subRows31Cols31, new int[]{ 3 , 1 }, new int[]{ 3 , 1 }, false);251		checkGetSubMatrix(m, null, 1, 0, 2, 4, true);252		checkGetSubMatrix(m, null, -1, 1, 2, 2, true);253		checkGetSubMatrix(m, null, 1, 0, 2, 2, true);254		checkGetSubMatrix(m, null, 1, 0, 2, 4, true);255		checkGetSubMatrix(m, null, new int[]{  }, new int[]{ 0 }, true);256		checkGetSubMatrix(m, null, new int[]{ 0 }, new int[]{ 4 }, true);257	}258	private void checkGetSubMatrix(org.apache.commons.math.linear.RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn, boolean mustFail) {259		try {260			org.apache.commons.math.linear.RealMatrix sub = m.getSubMatrix(startRow, endRow, startColumn, endColumn);261			junit.framework.Assert.assertEquals(new org.apache.commons.math.linear.Array2DRowRealMatrix(reference), sub);262			if (mustFail) {263				junit.framework.Assert.fail("Expecting MatrixIndexException");264			} 265		} catch (org.apache.commons.math.linear.MatrixIndexException e) {266			if (!mustFail) {267				throw e;268			} 269		}270	}271	private void checkGetSubMatrix(org.apache.commons.math.linear.RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns, boolean mustFail) {272		try {273			org.apache.commons.math.linear.RealMatrix sub = m.getSubMatrix(selectedRows, selectedColumns);274			junit.framework.Assert.assertEquals(new org.apache.commons.math.linear.Array2DRowRealMatrix(reference), sub);275			if (mustFail) {276				junit.framework.Assert.fail("Expecting MatrixIndexException");277			} 278		} catch (org.apache.commons.math.linear.MatrixIndexException e) {279			if (!mustFail) {280				throw e;281			} 282		}283	}284	public void testCopySubMatrix() {285		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);286		checkCopy(m, subRows23Cols00, 2, 3, 0, 0, false);287		checkCopy(m, subRows00Cols33, 0, 0, 3, 3, false);288		checkCopy(m, subRows01Cols23, 0, 1, 2, 3, false);289		checkCopy(m, subRows02Cols13, new int[]{ 0 , 2 }, new int[]{ 1 , 3 }, false);290		checkCopy(m, subRows03Cols12, new int[]{ 0 , 3 }, new int[]{ 1 , 2 }, false);291		checkCopy(m, subRows03Cols123, new int[]{ 0 , 3 }, new int[]{ 1 , 2 , 3 }, false);292		checkCopy(m, subRows20Cols123, new int[]{ 2 , 0 }, new int[]{ 1 , 2 , 3 }, false);293		checkCopy(m, subRows31Cols31, new int[]{ 3 , 1 }, new int[]{ 3 , 1 }, false);294		checkCopy(m, subRows31Cols31, new int[]{ 3 , 1 }, new int[]{ 3 , 1 }, false);295		checkCopy(m, null, 1, 0, 2, 4, true);296		checkCopy(m, null, -1, 1, 2, 2, true);297		checkCopy(m, null, 1, 0, 2, 2, true);298		checkCopy(m, null, 1, 0, 2, 4, true);299		checkCopy(m, null, new int[]{  }, new int[]{ 0 }, true);300		checkCopy(m, null, new int[]{ 0 }, new int[]{ 4 }, true);301	}302	private void checkCopy(org.apache.commons.math.linear.RealMatrix m, double[][] reference, int startRow, int endRow, int startColumn, int endColumn, boolean mustFail) {303		try {304			double[][] sub = reference == null ? new double[1][1] : new double[reference.length][reference[0].length];305			m.copySubMatrix(startRow, endRow, startColumn, endColumn, sub);306			junit.framework.Assert.assertEquals(new org.apache.commons.math.linear.Array2DRowRealMatrix(reference), new org.apache.commons.math.linear.Array2DRowRealMatrix(sub));307			if (mustFail) {308				junit.framework.Assert.fail("Expecting MatrixIndexException");309			} 310		} catch (org.apache.commons.math.linear.MatrixIndexException e) {311			if (!mustFail) {312				throw e;313			} 314		}315	}316	private void checkCopy(org.apache.commons.math.linear.RealMatrix m, double[][] reference, int[] selectedRows, int[] selectedColumns, boolean mustFail) {317		try {318			double[][] sub = reference == null ? new double[1][1] : new double[reference.length][reference[0].length];319			m.copySubMatrix(selectedRows, selectedColumns, sub);320			junit.framework.Assert.assertEquals(new org.apache.commons.math.linear.Array2DRowRealMatrix(reference), new org.apache.commons.math.linear.Array2DRowRealMatrix(sub));321			if (mustFail) {322				junit.framework.Assert.fail("Expecting MatrixIndexException");323			} 324		} catch (org.apache.commons.math.linear.MatrixIndexException e) {325			if (!mustFail) {326				throw e;327			} 328		}329	}330	public void testGetRowMatrix() {331		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);332		org.apache.commons.math.linear.RealMatrix mRow0 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subRow0);333		org.apache.commons.math.linear.RealMatrix mRow3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subRow3);334		junit.framework.Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));335		junit.framework.Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));336		try {337			m.getRowMatrix(-1);338			junit.framework.Assert.fail("Expecting MatrixIndexException");339		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {340		}341		try {342			m.getRowMatrix(4);343			junit.framework.Assert.fail("Expecting MatrixIndexException");344		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {345		}346	}347	public void testSetRowMatrix() {348		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);349		org.apache.commons.math.linear.RealMatrix mRow3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subRow3);350		junit.framework.Assert.assertNotSame(mRow3, m.getRowMatrix(0));351		m.setRowMatrix(0, mRow3);352		junit.framework.Assert.assertEquals(mRow3, m.getRowMatrix(0));353		try {354			m.setRowMatrix(-1, mRow3);355			junit.framework.Assert.fail("Expecting MatrixIndexException");356		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {357		}358		try {359			m.setRowMatrix(0, m);360			junit.framework.Assert.fail("Expecting InvalidMatrixException");361		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {362		}363	}364	public void testGetColumnMatrix() {365		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);366		org.apache.commons.math.linear.RealMatrix mColumn1 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subColumn1);367		org.apache.commons.math.linear.RealMatrix mColumn3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subColumn3);368		junit.framework.Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));369		junit.framework.Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));370		try {371			m.getColumnMatrix(-1);372			junit.framework.Assert.fail("Expecting MatrixIndexException");373		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {374		}375		try {376			m.getColumnMatrix(4);377			junit.framework.Assert.fail("Expecting MatrixIndexException");378		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {379		}380	}381	public void testSetColumnMatrix() {382		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);383		org.apache.commons.math.linear.RealMatrix mColumn3 = new org.apache.commons.math.linear.Array2DRowRealMatrix(subColumn3);384		junit.framework.Assert.assertNotSame(mColumn3, m.getColumnMatrix(1));385		m.setColumnMatrix(1, mColumn3);386		junit.framework.Assert.assertEquals(mColumn3, m.getColumnMatrix(1));387		try {388			m.setColumnMatrix(-1, mColumn3);389			junit.framework.Assert.fail("Expecting MatrixIndexException");390		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {391		}392		try {393			m.setColumnMatrix(0, m);394			junit.framework.Assert.fail("Expecting InvalidMatrixException");395		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {396		}397	}398	public void testGetRowVector() {399		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);400		org.apache.commons.math.linear.RealVector mRow0 = new org.apache.commons.math.linear.ArrayRealVector(subRow0[0]);401		org.apache.commons.math.linear.RealVector mRow3 = new org.apache.commons.math.linear.ArrayRealVector(subRow3[0]);402		junit.framework.Assert.assertEquals("Row0", mRow0, m.getRowVector(0));403		junit.framework.Assert.assertEquals("Row3", mRow3, m.getRowVector(3));404		try {405			m.getRowVector(-1);406			junit.framework.Assert.fail("Expecting MatrixIndexException");407		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {408		}409		try {410			m.getRowVector(4);411			junit.framework.Assert.fail("Expecting MatrixIndexException");412		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {413		}414	}415	public void testSetRowVector() {416		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);417		org.apache.commons.math.linear.RealVector mRow3 = new org.apache.commons.math.linear.ArrayRealVector(subRow3[0]);418		junit.framework.Assert.assertNotSame(mRow3, m.getRowMatrix(0));419		m.setRowVector(0, mRow3);420		junit.framework.Assert.assertEquals(mRow3, m.getRowVector(0));421		try {422			m.setRowVector(-1, mRow3);423			junit.framework.Assert.fail("Expecting MatrixIndexException");424		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {425		}426		try {427			m.setRowVector(0, new org.apache.commons.math.linear.ArrayRealVector(5));428			junit.framework.Assert.fail("Expecting InvalidMatrixException");429		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {430		}431	}432	public void testGetColumnVector() {433		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);434		org.apache.commons.math.linear.RealVector mColumn1 = columnToVector(subColumn1);435		org.apache.commons.math.linear.RealVector mColumn3 = columnToVector(subColumn3);436		junit.framework.Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));437		junit.framework.Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));438		try {439			m.getColumnVector(-1);440			junit.framework.Assert.fail("Expecting MatrixIndexException");441		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {442		}443		try {444			m.getColumnVector(4);445			junit.framework.Assert.fail("Expecting MatrixIndexException");446		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {447		}448	}449	public void testSetColumnVector() {450		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);451		org.apache.commons.math.linear.RealVector mColumn3 = columnToVector(subColumn3);452		junit.framework.Assert.assertNotSame(mColumn3, m.getColumnVector(1));453		m.setColumnVector(1, mColumn3);454		junit.framework.Assert.assertEquals(mColumn3, m.getColumnVector(1));455		try {456			m.setColumnVector(-1, mColumn3);457			junit.framework.Assert.fail("Expecting MatrixIndexException");458		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {459		}460		try {461			m.setColumnVector(0, new org.apache.commons.math.linear.ArrayRealVector(5));462			junit.framework.Assert.fail("Expecting InvalidMatrixException");463		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {464		}465	}466	private org.apache.commons.math.linear.RealVector columnToVector(double[][] column) {467		double[] data = new double[column.length];468		for (int i = 0 ; i < (data.length) ; ++i) {469			data[i] = column[i][0];470		}471		return new org.apache.commons.math.linear.ArrayRealVector(data , false);472	}473	public void testGetRow() {474		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);475		checkArrays(subRow0[0], m.getRow(0));476		checkArrays(subRow3[0], m.getRow(3));477		try {478			m.getRow(-1);479			junit.framework.Assert.fail("Expecting MatrixIndexException");480		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {481		}482		try {483			m.getRow(4);484			junit.framework.Assert.fail("Expecting MatrixIndexException");485		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {486		}487	}488	public void testSetRow() {489		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);490		junit.framework.Assert.assertTrue(((subRow3[0][0]) != (m.getRow(0)[0])));491		m.setRow(0, subRow3[0]);492		checkArrays(subRow3[0], m.getRow(0));493		try {494			m.setRow(-1, subRow3[0]);495			junit.framework.Assert.fail("Expecting MatrixIndexException");496		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {497		}498		try {499			m.setRow(0, new double[5]);500			junit.framework.Assert.fail("Expecting InvalidMatrixException");501		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {502		}503	}504	public void testGetColumn() {505		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);506		double[] mColumn1 = columnToArray(subColumn1);507		double[] mColumn3 = columnToArray(subColumn3);508		checkArrays(mColumn1, m.getColumn(1));509		checkArrays(mColumn3, m.getColumn(3));510		try {511			m.getColumn(-1);512			junit.framework.Assert.fail("Expecting MatrixIndexException");513		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {514		}515		try {516			m.getColumn(4);517			junit.framework.Assert.fail("Expecting MatrixIndexException");518		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {519		}520	}521	public void testSetColumn() {522		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(subTestData);523		double[] mColumn3 = columnToArray(subColumn3);524		junit.framework.Assert.assertTrue(((mColumn3[0]) != (m.getColumn(1)[0])));525		m.setColumn(1, mColumn3);526		checkArrays(mColumn3, m.getColumn(1));527		try {528			m.setColumn(-1, mColumn3);529			junit.framework.Assert.fail("Expecting MatrixIndexException");530		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {531		}532		try {533			m.setColumn(0, new double[5]);534			junit.framework.Assert.fail("Expecting InvalidMatrixException");535		} catch (org.apache.commons.math.linear.InvalidMatrixException ex) {536		}537	}538	private double[] columnToArray(double[][] column) {539		double[] data = new double[column.length];540		for (int i = 0 ; i < (data.length) ; ++i) {541			data[i] = column[i][0];542		}543		return data;544	}545	private void checkArrays(double[] expected, double[] actual) {546		junit.framework.Assert.assertEquals(expected.length, actual.length);547		for (int i = 0 ; i < (expected.length) ; ++i) {548			junit.framework.Assert.assertEquals(expected[i], actual[i]);549		}550	}551	public void testEqualsAndHashCode() {552		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);553		org.apache.commons.math.linear.Array2DRowRealMatrix m1 = ((org.apache.commons.math.linear.Array2DRowRealMatrix)(m.copy()));554		org.apache.commons.math.linear.Array2DRowRealMatrix mt = ((org.apache.commons.math.linear.Array2DRowRealMatrix)(m.transpose()));555		junit.framework.Assert.assertTrue(((m.hashCode()) != (mt.hashCode())));556		junit.framework.Assert.assertEquals(m.hashCode(), m1.hashCode());557		junit.framework.Assert.assertEquals(m, m);558		junit.framework.Assert.assertEquals(m, m1);559		junit.framework.Assert.assertFalse(m.equals(null));560		junit.framework.Assert.assertFalse(m.equals(mt));561		junit.framework.Assert.assertFalse(m.equals(new org.apache.commons.math.linear.Array2DRowRealMatrix(bigSingular)));562	}563	public void testToString() {564		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);565		junit.framework.Assert.assertEquals("Array2DRowRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString());566		m = new org.apache.commons.math.linear.Array2DRowRealMatrix();567		junit.framework.Assert.assertEquals("Array2DRowRealMatrix{}", m.toString());568	}569	public void testSetSubMatrix() throws java.lang.Exception {570		org.apache.commons.math.linear.Array2DRowRealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(testData);571		m.setSubMatrix(detData2, 1, 1);572		org.apache.commons.math.linear.RealMatrix expected = org.apache.commons.math.linear.MatrixUtils.createRealMatrix(new double[][]{ new double[]{ 1.0 , 2.0 , 3.0 } , new double[]{ 2.0 , 1.0 , 3.0 } , new double[]{ 1.0 , 2.0 , 4.0 } });573		junit.framework.Assert.assertEquals(expected, m);574		m.setSubMatrix(detData2, 0, 0);575		expected = org.apache.commons.math.linear.MatrixUtils.createRealMatrix(new double[][]{ new double[]{ 1.0 , 3.0 , 3.0 } , new double[]{ 2.0 , 4.0 , 3.0 } , new double[]{ 1.0 , 2.0 , 4.0 } });576		junit.framework.Assert.assertEquals(expected, m);577		m.setSubMatrix(testDataPlus2, 0, 0);578		expected = org.apache.commons.math.linear.MatrixUtils.createRealMatrix(new double[][]{ new double[]{ 3.0 , 4.0 , 5.0 } , new double[]{ 4.0 , 7.0 , 5.0 } , new double[]{ 3.0 , 2.0 , 10.0 } });579		junit.framework.Assert.assertEquals(expected, m);580		try {581			m.setSubMatrix(testData, 1, 1);582			junit.framework.Assert.fail("expecting MatrixIndexException");583		} catch (org.apache.commons.math.linear.MatrixIndexException e) {584		}585		try {586			m.setSubMatrix(testData, -1, 1);587			junit.framework.Assert.fail("expecting MatrixIndexException");588		} catch (org.apache.commons.math.linear.MatrixIndexException e) {589		}590		try {591			m.setSubMatrix(testData, 1, -1);592			junit.framework.Assert.fail("expecting MatrixIndexException");593		} catch (org.apache.commons.math.linear.MatrixIndexException e) {594		}595		try {596			m.setSubMatrix(null, 1, 1);597			junit.framework.Assert.fail("expecting NullPointerException");598		} catch (java.lang.NullPointerException e) {599		}600		org.apache.commons.math.linear.Array2DRowRealMatrix m2 = new org.apache.commons.math.linear.Array2DRowRealMatrix();601		try {602			m2.setSubMatrix(testData, 0, 1);603			junit.framework.Assert.fail("expecting IllegalStateException");604		} catch (java.lang.IllegalStateException e) {605		}606		try {607			m2.setSubMatrix(testData, 1, 0);608			junit.framework.Assert.fail("expecting IllegalStateException");609		} catch (java.lang.IllegalStateException e) {610		}611		try {612			m.setSubMatrix(new double[][]{ new double[]{ 1 } , new double[]{ 2 , 3 } }, 0, 0);613			junit.framework.Assert.fail("expecting IllegalArgumentException");614		} catch (java.lang.IllegalArgumentException e) {615		}616		try {617			m.setSubMatrix(new double[][]{ new double[]{  } }, 0, 0);618			junit.framework.Assert.fail("expecting IllegalArgumentException");619		} catch (java.lang.IllegalArgumentException e) {620		}621	}622	public void testWalk() {623		int rows = 150;624		int columns = 75;625		org.apache.commons.math.linear.RealMatrix m = new org.apache.commons.math.linear.Array2DRowRealMatrix(rows , columns);626		m.walkInRowOrder(new org.apache.commons.math.linear.Array2DRowRealMatrixTest.SetVisitor());627		org.apache.commons.math.linear.Array2DRowRealMatrixTest.GetVisitor getVisitor = new org.apache.commons.math.linear.Array2DRowRealMatrixTest.GetVisitor();628		m.walkInOptimizedOrder(getVisitor);629		junit.framework.Assert.assertEquals((rows * columns), getVisitor.getCount());630		m = new org.apache.commons.math.linear.Array2DRowRealMatrix(rows , columns);631		m.walkInRowOrder(new org.apache.commons.math.linear.Array2DRowRealMatrixTest.SetVisitor(), 1, (rows - 2), 1, (columns - 2));632		getVisitor = new org.apache.commons.math.linear.Array2DRowRealMatrixTest.GetVisitor();...

Full Screen

Full Screen

Source:SparseRealMatrixTest.java Github

copy

Full Screen

...73		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);74		org.apache.commons.math.linear.OpenMapRealMatrix m2 = createSparseMatrix(testData2);75		try {76			m.add(m2);77			junit.framework.Assert.fail("IllegalArgumentException expected");78		} catch (java.lang.IllegalArgumentException ex) {79		}80	}81	public void testNorm() {82		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);83		org.apache.commons.math.linear.OpenMapRealMatrix m2 = createSparseMatrix(testData2);84		junit.framework.Assert.assertEquals("testData norm", 14.0, m.getNorm(), entryTolerance);85		junit.framework.Assert.assertEquals("testData2 norm", 7.0, m2.getNorm(), entryTolerance);86	}87	public void testPlusMinus() {88		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);89		org.apache.commons.math.linear.OpenMapRealMatrix n = createSparseMatrix(testDataInv);90		assertClose("m-n = m + -n", m.subtract(n), n.scalarMultiply(-1.0).add(m), entryTolerance);91		try {92			m.subtract(createSparseMatrix(testData2));93			junit.framework.Assert.fail("Expecting illegalArgumentException");94		} catch (java.lang.IllegalArgumentException ex) {95		}96	}97	public void testMultiply() {98		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);99		org.apache.commons.math.linear.OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);100		org.apache.commons.math.linear.OpenMapRealMatrix identity = createSparseMatrix(id);101		org.apache.commons.math.linear.OpenMapRealMatrix m2 = createSparseMatrix(testData2);102		assertClose("inverse multiply", m.multiply(mInv), identity, entryTolerance);103		assertClose("inverse multiply", m.multiply(new org.apache.commons.math.linear.BlockRealMatrix(testDataInv)), identity, entryTolerance);104		assertClose("inverse multiply", mInv.multiply(m), identity, entryTolerance);105		assertClose("identity multiply", m.multiply(identity), m, entryTolerance);106		assertClose("identity multiply", identity.multiply(mInv), mInv, entryTolerance);107		assertClose("identity multiply", m2.multiply(identity), m2, entryTolerance);108		try {109			m.multiply(createSparseMatrix(bigSingular));110			junit.framework.Assert.fail("Expecting illegalArgumentException");111		} catch (java.lang.IllegalArgumentException ex) {112		}113	}114	private double[][] d3 = new double[][]{ new double[]{ 1 , 2 , 3 , 4 } , new double[]{ 5 , 6 , 7 , 8 } };115	private double[][] d4 = new double[][]{ new double[]{ 1 } , new double[]{ 2 } , new double[]{ 3 } , new double[]{ 4 } };116	private double[][] d5 = new double[][]{ new double[]{ 30 } , new double[]{ 70 } };117	public void testMultiply2() {118		org.apache.commons.math.linear.RealMatrix m3 = createSparseMatrix(d3);119		org.apache.commons.math.linear.RealMatrix m4 = createSparseMatrix(d4);120		org.apache.commons.math.linear.RealMatrix m5 = createSparseMatrix(d5);121		assertClose("m3*m4=m5", m3.multiply(m4), m5, entryTolerance);122	}123	public void testTrace() {124		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(id);125		junit.framework.Assert.assertEquals("identity trace", 3.0, m.getTrace(), entryTolerance);126		m = createSparseMatrix(testData2);127		try {128			m.getTrace();129			junit.framework.Assert.fail("Expecting NonSquareMatrixException");130		} catch (org.apache.commons.math.linear.NonSquareMatrixException ex) {131		}132	}133	public void testScalarAdd() {134		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(testData);135		assertClose("scalar add", createSparseMatrix(testDataPlus2), m.scalarAdd(2.0), entryTolerance);136	}137	public void testOperate() {138		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(id);139		assertClose("identity operate", testVector, m.operate(testVector), entryTolerance);140		assertClose("identity operate", testVector, m.operate(new org.apache.commons.math.linear.ArrayRealVector(testVector)).getData(), entryTolerance);141		m = createSparseMatrix(bigSingular);142		try {143			m.operate(testVector);144			junit.framework.Assert.fail("Expecting illegalArgumentException");145		} catch (java.lang.IllegalArgumentException ex) {146		}147	}148	public void testMath209() {149		org.apache.commons.math.linear.RealMatrix a = createSparseMatrix(new double[][]{ new double[]{ 1 , 2 } , new double[]{ 3 , 4 } , new double[]{ 5 , 6 } });150		double[] b = a.operate(new double[]{ 1 , 1 });151		junit.framework.Assert.assertEquals(a.getRowDimension(), b.length);152		junit.framework.Assert.assertEquals(3.0, b[0], 1.0E-12);153		junit.framework.Assert.assertEquals(7.0, b[1], 1.0E-12);154		junit.framework.Assert.assertEquals(11.0, b[2], 1.0E-12);155	}156	public void testTranspose() {157		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(testData);158		org.apache.commons.math.linear.RealMatrix mIT = new org.apache.commons.math.linear.LUDecompositionImpl(m).getSolver().getInverse().transpose();159		org.apache.commons.math.linear.RealMatrix mTI = new org.apache.commons.math.linear.LUDecompositionImpl(m.transpose()).getSolver().getInverse();160		assertClose("inverse-transpose", mIT, mTI, normTolerance);161		m = createSparseMatrix(testData2);162		org.apache.commons.math.linear.RealMatrix mt = createSparseMatrix(testData2T);163		assertClose("transpose", mt, m.transpose(), normTolerance);164	}165	public void testPremultiplyVector() {166		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(testData);167		assertClose("premultiply", m.preMultiply(testVector), preMultTest, normTolerance);168		assertClose("premultiply", m.preMultiply(new org.apache.commons.math.linear.ArrayRealVector(testVector).getData()), preMultTest, normTolerance);169		m = createSparseMatrix(bigSingular);170		try {171			m.preMultiply(testVector);172			junit.framework.Assert.fail("expecting IllegalArgumentException");173		} catch (java.lang.IllegalArgumentException ex) {174		}175	}176	public void testPremultiply() {177		org.apache.commons.math.linear.RealMatrix m3 = createSparseMatrix(d3);178		org.apache.commons.math.linear.RealMatrix m4 = createSparseMatrix(d4);179		org.apache.commons.math.linear.RealMatrix m5 = createSparseMatrix(d5);180		assertClose("m3*m4=m5", m4.preMultiply(m3), m5, entryTolerance);181		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);182		org.apache.commons.math.linear.OpenMapRealMatrix mInv = createSparseMatrix(testDataInv);183		org.apache.commons.math.linear.OpenMapRealMatrix identity = createSparseMatrix(id);184		assertClose("inverse multiply", m.preMultiply(mInv), identity, entryTolerance);185		assertClose("inverse multiply", mInv.preMultiply(m), identity, entryTolerance);186		assertClose("identity multiply", m.preMultiply(identity), m, entryTolerance);187		assertClose("identity multiply", identity.preMultiply(mInv), mInv, entryTolerance);188		try {189			m.preMultiply(createSparseMatrix(bigSingular));190			junit.framework.Assert.fail("Expecting illegalArgumentException");191		} catch (java.lang.IllegalArgumentException ex) {192		}193	}194	public void testGetVectors() {195		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(testData);196		assertClose("get row", m.getRow(0), testDataRow1, entryTolerance);197		assertClose("get col", m.getColumn(2), testDataCol3, entryTolerance);198		try {199			m.getRow(10);200			junit.framework.Assert.fail("expecting MatrixIndexException");201		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {202		}203		try {204			m.getColumn(-1);205			junit.framework.Assert.fail("expecting MatrixIndexException");206		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {207		}208	}209	public void testGetEntry() {210		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(testData);211		junit.framework.Assert.assertEquals("get entry", m.getEntry(0, 1), 2.0, entryTolerance);212		try {213			m.getEntry(10, 4);214			junit.framework.Assert.fail("Expecting MatrixIndexException");215		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {216		}217	}218	public void testExamples() {219		double[][] matrixData = new double[][]{ new double[]{ 1.0 , 2.0 , 3.0 } , new double[]{ 2.0 , 5.0 , 3.0 } };220		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(matrixData);221		double[][] matrixData2 = new double[][]{ new double[]{ 1.0 , 2.0 } , new double[]{ 2.0 , 5.0 } , new double[]{ 1.0 , 7.0 } };222		org.apache.commons.math.linear.RealMatrix n = createSparseMatrix(matrixData2);223		org.apache.commons.math.linear.RealMatrix p = m.multiply(n);224		junit.framework.Assert.assertEquals(2, p.getRowDimension());225		junit.framework.Assert.assertEquals(2, p.getColumnDimension());226		org.apache.commons.math.linear.RealMatrix pInverse = new org.apache.commons.math.linear.LUDecompositionImpl(p).getSolver().getInverse();227		junit.framework.Assert.assertEquals(2, pInverse.getRowDimension());228		junit.framework.Assert.assertEquals(2, pInverse.getColumnDimension());229		double[][] coefficientsData = new double[][]{ new double[]{ 2 , 3 , -2 } , new double[]{ -1 , 7 , 6 } , new double[]{ 4 , -3 , -5 } };230		org.apache.commons.math.linear.RealMatrix coefficients = createSparseMatrix(coefficientsData);231		double[] constants = new double[]{ 1 , -2 , 1 };232		double[] solution = new org.apache.commons.math.linear.LUDecompositionImpl(coefficients).getSolver().solve(constants);233		junit.framework.Assert.assertEquals((((2 * (solution[0])) + (3 * (solution[1]))) - (2 * (solution[2]))), constants[0], 1.0E-12);234		junit.framework.Assert.assertEquals(((((-1) * (solution[0])) + (7 * (solution[1]))) + (6 * (solution[2]))), constants[1], 1.0E-12);235		junit.framework.Assert.assertEquals((((4 * (solution[0])) - (3 * (solution[1]))) - (5 * (solution[2]))), constants[2], 1.0E-12);236	}237	public void testSubMatrix() {238		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(subTestData);239		org.apache.commons.math.linear.RealMatrix mRows23Cols00 = createSparseMatrix(subRows23Cols00);240		org.apache.commons.math.linear.RealMatrix mRows00Cols33 = createSparseMatrix(subRows00Cols33);241		org.apache.commons.math.linear.RealMatrix mRows01Cols23 = createSparseMatrix(subRows01Cols23);242		org.apache.commons.math.linear.RealMatrix mRows02Cols13 = createSparseMatrix(subRows02Cols13);243		org.apache.commons.math.linear.RealMatrix mRows03Cols12 = createSparseMatrix(subRows03Cols12);244		org.apache.commons.math.linear.RealMatrix mRows03Cols123 = createSparseMatrix(subRows03Cols123);245		org.apache.commons.math.linear.RealMatrix mRows20Cols123 = createSparseMatrix(subRows20Cols123);246		org.apache.commons.math.linear.RealMatrix mRows31Cols31 = createSparseMatrix(subRows31Cols31);247		junit.framework.Assert.assertEquals("Rows23Cols00", mRows23Cols00, m.getSubMatrix(2, 3, 0, 0));248		junit.framework.Assert.assertEquals("Rows00Cols33", mRows00Cols33, m.getSubMatrix(0, 0, 3, 3));249		junit.framework.Assert.assertEquals("Rows01Cols23", mRows01Cols23, m.getSubMatrix(0, 1, 2, 3));250		junit.framework.Assert.assertEquals("Rows02Cols13", mRows02Cols13, m.getSubMatrix(new int[]{ 0 , 2 }, new int[]{ 1 , 3 }));251		junit.framework.Assert.assertEquals("Rows03Cols12", mRows03Cols12, m.getSubMatrix(new int[]{ 0 , 3 }, new int[]{ 1 , 2 }));252		junit.framework.Assert.assertEquals("Rows03Cols123", mRows03Cols123, m.getSubMatrix(new int[]{ 0 , 3 }, new int[]{ 1 , 2 , 3 }));253		junit.framework.Assert.assertEquals("Rows20Cols123", mRows20Cols123, m.getSubMatrix(new int[]{ 2 , 0 }, new int[]{ 1 , 2 , 3 }));254		junit.framework.Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[]{ 3 , 1 }, new int[]{ 3 , 1 }));255		junit.framework.Assert.assertEquals("Rows31Cols31", mRows31Cols31, m.getSubMatrix(new int[]{ 3 , 1 }, new int[]{ 3 , 1 }));256		try {257			m.getSubMatrix(1, 0, 2, 4);258			junit.framework.Assert.fail("Expecting MatrixIndexException");259		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {260		}261		try {262			m.getSubMatrix(-1, 1, 2, 2);263			junit.framework.Assert.fail("Expecting MatrixIndexException");264		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {265		}266		try {267			m.getSubMatrix(1, 0, 2, 2);268			junit.framework.Assert.fail("Expecting MatrixIndexException");269		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {270		}271		try {272			m.getSubMatrix(1, 0, 2, 4);273			junit.framework.Assert.fail("Expecting MatrixIndexException");274		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {275		}276		try {277			m.getSubMatrix(new int[]{  }, new int[]{ 0 });278			junit.framework.Assert.fail("Expecting MatrixIndexException");279		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {280		}281		try {282			m.getSubMatrix(new int[]{ 0 }, new int[]{ 4 });283			junit.framework.Assert.fail("Expecting MatrixIndexException");284		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {285		}286	}287	public void testGetRowMatrix() {288		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(subTestData);289		org.apache.commons.math.linear.RealMatrix mRow0 = createSparseMatrix(subRow0);290		org.apache.commons.math.linear.RealMatrix mRow3 = createSparseMatrix(subRow3);291		junit.framework.Assert.assertEquals("Row0", mRow0, m.getRowMatrix(0));292		junit.framework.Assert.assertEquals("Row3", mRow3, m.getRowMatrix(3));293		try {294			m.getRowMatrix(-1);295			junit.framework.Assert.fail("Expecting MatrixIndexException");296		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {297		}298		try {299			m.getRowMatrix(4);300			junit.framework.Assert.fail("Expecting MatrixIndexException");301		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {302		}303	}304	public void testGetColumnMatrix() {305		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(subTestData);306		org.apache.commons.math.linear.RealMatrix mColumn1 = createSparseMatrix(subColumn1);307		org.apache.commons.math.linear.RealMatrix mColumn3 = createSparseMatrix(subColumn3);308		junit.framework.Assert.assertEquals("Column1", mColumn1, m.getColumnMatrix(1));309		junit.framework.Assert.assertEquals("Column3", mColumn3, m.getColumnMatrix(3));310		try {311			m.getColumnMatrix(-1);312			junit.framework.Assert.fail("Expecting MatrixIndexException");313		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {314		}315		try {316			m.getColumnMatrix(4);317			junit.framework.Assert.fail("Expecting MatrixIndexException");318		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {319		}320	}321	public void testGetRowVector() {322		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(subTestData);323		org.apache.commons.math.linear.RealVector mRow0 = new org.apache.commons.math.linear.ArrayRealVector(subRow0[0]);324		org.apache.commons.math.linear.RealVector mRow3 = new org.apache.commons.math.linear.ArrayRealVector(subRow3[0]);325		junit.framework.Assert.assertEquals("Row0", mRow0, m.getRowVector(0));326		junit.framework.Assert.assertEquals("Row3", mRow3, m.getRowVector(3));327		try {328			m.getRowVector(-1);329			junit.framework.Assert.fail("Expecting MatrixIndexException");330		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {331		}332		try {333			m.getRowVector(4);334			junit.framework.Assert.fail("Expecting MatrixIndexException");335		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {336		}337	}338	public void testGetColumnVector() {339		org.apache.commons.math.linear.RealMatrix m = createSparseMatrix(subTestData);340		org.apache.commons.math.linear.RealVector mColumn1 = columnToVector(subColumn1);341		org.apache.commons.math.linear.RealVector mColumn3 = columnToVector(subColumn3);342		junit.framework.Assert.assertEquals("Column1", mColumn1, m.getColumnVector(1));343		junit.framework.Assert.assertEquals("Column3", mColumn3, m.getColumnVector(3));344		try {345			m.getColumnVector(-1);346			junit.framework.Assert.fail("Expecting MatrixIndexException");347		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {348		}349		try {350			m.getColumnVector(4);351			junit.framework.Assert.fail("Expecting MatrixIndexException");352		} catch (org.apache.commons.math.linear.MatrixIndexException ex) {353		}354	}355	private org.apache.commons.math.linear.RealVector columnToVector(double[][] column) {356		double[] data = new double[column.length];357		for (int i = 0 ; i < (data.length) ; ++i) {358			data[i] = column[i][0];359		}360		return new org.apache.commons.math.linear.ArrayRealVector(data , false);361	}362	public void testEqualsAndHashCode() {363		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);364		org.apache.commons.math.linear.OpenMapRealMatrix m1 = m.copy();365		org.apache.commons.math.linear.OpenMapRealMatrix mt = ((org.apache.commons.math.linear.OpenMapRealMatrix)(m.transpose()));366		junit.framework.Assert.assertTrue(((m.hashCode()) != (mt.hashCode())));367		junit.framework.Assert.assertEquals(m.hashCode(), m1.hashCode());368		junit.framework.Assert.assertEquals(m, m);369		junit.framework.Assert.assertEquals(m, m1);370		junit.framework.Assert.assertFalse(m.equals(null));371		junit.framework.Assert.assertFalse(m.equals(mt));372		junit.framework.Assert.assertFalse(m.equals(createSparseMatrix(bigSingular)));373	}374	public void testToString() {375		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);376		junit.framework.Assert.assertEquals("OpenMapRealMatrix{{1.0,2.0,3.0},{2.0,5.0,3.0},{1.0,0.0,8.0}}", m.toString());377		m = new org.apache.commons.math.linear.OpenMapRealMatrix(1 , 1);378		junit.framework.Assert.assertEquals("OpenMapRealMatrix{{0.0}}", m.toString());379	}380	public void testSetSubMatrix() throws java.lang.Exception {381		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);382		m.setSubMatrix(detData2, 1, 1);383		org.apache.commons.math.linear.RealMatrix expected = createSparseMatrix(new double[][]{ new double[]{ 1.0 , 2.0 , 3.0 } , new double[]{ 2.0 , 1.0 , 3.0 } , new double[]{ 1.0 , 2.0 , 4.0 } });384		junit.framework.Assert.assertEquals(expected, m);385		m.setSubMatrix(detData2, 0, 0);386		expected = createSparseMatrix(new double[][]{ new double[]{ 1.0 , 3.0 , 3.0 } , new double[]{ 2.0 , 4.0 , 3.0 } , new double[]{ 1.0 , 2.0 , 4.0 } });387		junit.framework.Assert.assertEquals(expected, m);388		m.setSubMatrix(testDataPlus2, 0, 0);389		expected = createSparseMatrix(new double[][]{ new double[]{ 3.0 , 4.0 , 5.0 } , new double[]{ 4.0 , 7.0 , 5.0 } , new double[]{ 3.0 , 2.0 , 10.0 } });390		junit.framework.Assert.assertEquals(expected, m);391		org.apache.commons.math.linear.OpenMapRealMatrix matrix = createSparseMatrix(new double[][]{ new double[]{ 1 , 2 , 3 , 4 } , new double[]{ 5 , 6 , 7 , 8 } , new double[]{ 9 , 0 , 1 , 2 } });392		matrix.setSubMatrix(new double[][]{ new double[]{ 3 , 4 } , new double[]{ 5 , 6 } }, 1, 1);393		expected = createSparseMatrix(new double[][]{ new double[]{ 1 , 2 , 3 , 4 } , new double[]{ 5 , 3 , 4 , 8 } , new double[]{ 9 , 5 , 6 , 2 } });394		junit.framework.Assert.assertEquals(expected, matrix);395		try {396			m.setSubMatrix(testData, 1, 1);397			junit.framework.Assert.fail("expecting MatrixIndexException");398		} catch (org.apache.commons.math.linear.MatrixIndexException e) {399		}400		try {401			m.setSubMatrix(testData, -1, 1);402			junit.framework.Assert.fail("expecting MatrixIndexException");403		} catch (org.apache.commons.math.linear.MatrixIndexException e) {404		}405		try {406			m.setSubMatrix(testData, 1, -1);407			junit.framework.Assert.fail("expecting MatrixIndexException");408		} catch (org.apache.commons.math.linear.MatrixIndexException e) {409		}410		try {411			m.setSubMatrix(null, 1, 1);412			junit.framework.Assert.fail("expecting NullPointerException");413		} catch (java.lang.NullPointerException e) {414		}415		try {416			new org.apache.commons.math.linear.OpenMapRealMatrix(0 , 0);417			junit.framework.Assert.fail("expecting IllegalArgumentException");418		} catch (java.lang.IllegalArgumentException e) {419		}420		try {421			m.setSubMatrix(new double[][]{ new double[]{ 1 } , new double[]{ 2 , 3 } }, 0, 0);422			junit.framework.Assert.fail("expecting IllegalArgumentException");423		} catch (java.lang.IllegalArgumentException e) {424		}425		try {426			m.setSubMatrix(new double[][]{ new double[]{  } }, 0, 0);427			junit.framework.Assert.fail("expecting IllegalArgumentException");428		} catch (java.lang.IllegalArgumentException e) {429		}430	}431	public void testSerial() {432		org.apache.commons.math.linear.OpenMapRealMatrix m = createSparseMatrix(testData);433		junit.framework.Assert.assertEquals(m, org.apache.commons.math.TestUtils.serializeAndRecover(m));434	}435	protected void assertClose(java.lang.String msg, org.apache.commons.math.linear.RealMatrix m, org.apache.commons.math.linear.RealMatrix n, double tolerance) {436		junit.framework.Assert.assertTrue(msg, ((m.subtract(n).getNorm()) < tolerance));437	}438	protected void assertClose(java.lang.String msg, double[] m, double[] n, double tolerance) {439		if ((m.length) != (n.length)) {440			junit.framework.Assert.fail("vectors not same length");441		} 442		for (int i = 0 ; i < (m.length) ; i++) {443			junit.framework.Assert.assertEquals((((msg + " ") + i) + " elements differ"), m[i], n[i], tolerance);444		}445	}446	private org.apache.commons.math.linear.OpenMapRealMatrix createSparseMatrix(double[][] data) {447		org.apache.commons.math.linear.OpenMapRealMatrix matrix = new org.apache.commons.math.linear.OpenMapRealMatrix(data.length , data[0].length);448		for (int row = 0 ; row < (data.length) ; row++) {449			for (int col = 0 ; col < (data[row].length) ; col++) {450				matrix.setEntry(row, col, data[row][col]);451			}452		}453		return matrix;454	}...

Full Screen

Full Screen

Source:RandomDataTest.java Github

copy

Full Screen

...22	}23	public void testNextInt() {24		try {25			randomData.nextInt(4, 3);26			junit.framework.Assert.fail("IllegalArgumentException expected");27		} catch (java.lang.IllegalArgumentException ex) {28		}29		org.apache.commons.math.stat.Frequency freq = new org.apache.commons.math.stat.Frequency();30		int value = 0;31		for (int i = 0 ; i < (smallSampleSize) ; i++) {32			value = randomData.nextInt(0, 3);33			junit.framework.Assert.assertTrue("nextInt range", ((value >= 0) && (value <= 3)));34			freq.addValue(value);35		}36		long[] observed = new long[4];37		for (int i = 0 ; i < 4 ; i++) {38			observed[i] = freq.getCount(i);39		}40		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 16.27));41	}42	public void testNextLong() {43		try {44			randomData.nextLong(4, 3);45			junit.framework.Assert.fail("IllegalArgumentException expected");46		} catch (java.lang.IllegalArgumentException ex) {47		}48		org.apache.commons.math.stat.Frequency freq = new org.apache.commons.math.stat.Frequency();49		long value = 0;50		for (int i = 0 ; i < (smallSampleSize) ; i++) {51			value = randomData.nextLong(0, 3);52			junit.framework.Assert.assertTrue("nextInt range", ((value >= 0) && (value <= 3)));53			freq.addValue(value);54		}55		long[] observed = new long[4];56		for (int i = 0 ; i < 4 ; i++) {57			observed[i] = freq.getCount(i);58		}59		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 16.27));60	}61	public void testNextSecureLong() {62		try {63			randomData.nextSecureLong(4, 3);64			junit.framework.Assert.fail("IllegalArgumentException expected");65		} catch (java.lang.IllegalArgumentException ex) {66		}67		org.apache.commons.math.stat.Frequency freq = new org.apache.commons.math.stat.Frequency();68		long value = 0;69		for (int i = 0 ; i < (smallSampleSize) ; i++) {70			value = randomData.nextSecureLong(0, 3);71			junit.framework.Assert.assertTrue("nextInt range", ((value >= 0) && (value <= 3)));72			freq.addValue(value);73		}74		long[] observed = new long[4];75		for (int i = 0 ; i < 4 ; i++) {76			observed[i] = freq.getCount(i);77		}78		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 16.27));79	}80	public void testNextSecureInt() {81		try {82			randomData.nextSecureInt(4, 3);83			junit.framework.Assert.fail("IllegalArgumentException expected");84		} catch (java.lang.IllegalArgumentException ex) {85		}86		org.apache.commons.math.stat.Frequency freq = new org.apache.commons.math.stat.Frequency();87		int value = 0;88		for (int i = 0 ; i < (smallSampleSize) ; i++) {89			value = randomData.nextSecureInt(0, 3);90			junit.framework.Assert.assertTrue("nextInt range", ((value >= 0) && (value <= 3)));91			freq.addValue(value);92		}93		long[] observed = new long[4];94		for (int i = 0 ; i < 4 ; i++) {95			observed[i] = freq.getCount(i);96		}97		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 16.27));98	}99	public void testNextPoisson() {100		try {101			randomData.nextPoisson(0);102			junit.framework.Assert.fail("zero mean -- expecting IllegalArgumentException");103		} catch (java.lang.IllegalArgumentException ex) {104		}105		org.apache.commons.math.stat.Frequency f = new org.apache.commons.math.stat.Frequency();106		for (int i = 0 ; i < (largeSampleSize) ; i++) {107			try {108				f.addValue(randomData.nextPoisson(4.0));109			} catch (java.lang.Exception ex) {110				junit.framework.Assert.fail(ex.getMessage());111			}112		}113		long cumFreq = (((((f.getCount(0)) + (f.getCount(1))) + (f.getCount(2))) + (f.getCount(3))) + (f.getCount(4))) + (f.getCount(5));114		long sumFreq = f.getSumFreq();115		double cumPct = (java.lang.Double.valueOf(cumFreq).doubleValue()) / (java.lang.Double.valueOf(sumFreq).doubleValue());116		junit.framework.Assert.assertEquals("cum Poisson(4)", cumPct, 0.7851, 0.2);117		try {118			randomData.nextPoisson(-1);119			junit.framework.Assert.fail("negative mean supplied -- IllegalArgumentException expected");120		} catch (java.lang.IllegalArgumentException ex) {121		}122		try {123			randomData.nextPoisson(0);124			junit.framework.Assert.fail("0 mean supplied -- IllegalArgumentException expected");125		} catch (java.lang.IllegalArgumentException ex) {126		}127	}128	public void _testNextPoissonConsistency() throws java.lang.Exception {129		for (int i = 1 ; i < 100 ; i++) {130			checkNextPoissonConsistency(i);131		}132		org.apache.commons.math.random.RandomData randomData = new org.apache.commons.math.random.RandomDataImpl();133		for (int i = 1 ; i < 10 ; i++) {134			checkNextPoissonConsistency(randomData.nextUniform(1, 1000));135		}136		for (int i = 1 ; i < 10 ; i++) {137			checkNextPoissonConsistency(randomData.nextUniform(1000, 3000));138		}139	}140	public void checkNextPoissonConsistency(double mean) throws java.lang.Exception {141		int sampleSize = 1000;142		int minExpectedCount = 7;143		long maxObservedValue = 0;144		double alpha = 0.001;145		org.apache.commons.math.stat.Frequency frequency = new org.apache.commons.math.stat.Frequency();146		for (int i = 0 ; i < sampleSize ; i++) {147			long value = randomData.nextPoisson(mean);148			if (value > maxObservedValue) {149				maxObservedValue = value;150			} 151			frequency.addValue(value);152		}153		org.apache.commons.math.distribution.PoissonDistribution poissonDistribution = new org.apache.commons.math.distribution.PoissonDistributionImpl(mean);154		int lower = 1;155		while (((poissonDistribution.cumulativeProbability((lower - 1))) * sampleSize) < minExpectedCount) {156			lower++;157		}158		int upper = ((int)(5 * mean));159		while (((1 - (poissonDistribution.cumulativeProbability((upper - 1)))) * sampleSize) < minExpectedCount) {160			upper--;161		}162		int binWidth = 1;163		boolean widthSufficient = false;164		double lowerBinMass = 0;165		double upperBinMass = 0;166		while (!widthSufficient) {167			lowerBinMass = poissonDistribution.cumulativeProbability(lower, ((lower + binWidth) - 1));168			upperBinMass = poissonDistribution.cumulativeProbability(((upper - binWidth) + 1), upper);169			widthSufficient = ((java.lang.Math.min(lowerBinMass, upperBinMass)) * sampleSize) >= minExpectedCount;170			binWidth++;171		}172		java.util.List<java.lang.Integer> binBounds = new java.util.ArrayList<java.lang.Integer>();173		binBounds.add(lower);174		int bound = lower + binWidth;175		while (bound < (upper - binWidth)) {176			binBounds.add(bound);177			bound += binWidth;178		}179		binBounds.add(bound);180		binBounds.add(upper);181		final int binCount = (binBounds.size()) + 1;182		long[] observed = new long[binCount];183		double[] expected = new double[binCount];184		observed[0] = 0;185		for (int i = 0 ; i < lower ; i++) {186			observed[0] += frequency.getCount(i);187		}188		expected[0] = (poissonDistribution.cumulativeProbability((lower - 1))) * sampleSize;189		observed[(binCount - 1)] = 0;190		for (int i = upper ; i <= maxObservedValue ; i++) {191			observed[(binCount - 1)] += frequency.getCount(i);192		}193		expected[(binCount - 1)] = (1 - (poissonDistribution.cumulativeProbability((upper - 1)))) * sampleSize;194		for (int i = 1 ; i < (binCount - 1) ; i++) {195			observed[i] = 0;196			for (int j = binBounds.get((i - 1)) ; j < (binBounds.get(i)) ; j++) {197				observed[i] += frequency.getCount(j);198			}199			expected[i] = ((poissonDistribution.cumulativeProbability(((binBounds.get(i)) - 1))) - (poissonDistribution.cumulativeProbability(((binBounds.get((i - 1))) - 1)))) * sampleSize;200		}201		org.apache.commons.math.stat.inference.ChiSquareTest chiSquareTest = new org.apache.commons.math.stat.inference.ChiSquareTestImpl();202		try {203			junit.framework.Assert.assertFalse(chiSquareTest.chiSquareTest(expected, observed, alpha));204		} catch (junit.framework.AssertionFailedError ex) {205			java.lang.StringBuffer msgBuffer = new java.lang.StringBuffer();206			java.text.DecimalFormat df = new java.text.DecimalFormat("#.##");207			msgBuffer.append("Chisquare test failed for mean = ");208			msgBuffer.append(mean);209			msgBuffer.append(" p-value = ");210			msgBuffer.append(chiSquareTest.chiSquareTest(expected, observed));211			msgBuffer.append(" chisquare statistic = ");212			msgBuffer.append(chiSquareTest.chiSquare(expected, observed));213			msgBuffer.append(". \n");214			msgBuffer.append("bin\t\texpected\tobserved\n");215			for (int i = 0 ; i < (expected.length) ; i++) {216				msgBuffer.append("[");217				msgBuffer.append((i == 0 ? 1 : binBounds.get((i - 1))));218				msgBuffer.append(",");219				msgBuffer.append((i == (binBounds.size()) ? "inf" : binBounds.get(i)));220				msgBuffer.append(")");221				msgBuffer.append("\t\t");222				msgBuffer.append(df.format(expected[i]));223				msgBuffer.append("\t\t");224				msgBuffer.append(observed[i]);225				msgBuffer.append("\n");226			}227			msgBuffer.append("This test can fail randomly due to sampling error with probability ");228			msgBuffer.append(alpha);229			msgBuffer.append(".");230			junit.framework.Assert.fail(msgBuffer.toString());231		}232	}233	public void testNextHex() {234		try {235			randomData.nextHexString(-1);236			junit.framework.Assert.fail("negative length supplied -- IllegalArgumentException expected");237		} catch (java.lang.IllegalArgumentException ex) {238		}239		try {240			randomData.nextHexString(0);241			junit.framework.Assert.fail("zero length supplied -- IllegalArgumentException expected");242		} catch (java.lang.IllegalArgumentException ex) {243		}244		java.lang.String hexString = randomData.nextHexString(3);245		if ((hexString.length()) != 3) {246			junit.framework.Assert.fail("incorrect length for generated string");247		} 248		hexString = randomData.nextHexString(1);249		if ((hexString.length()) != 1) {250			junit.framework.Assert.fail("incorrect length for generated string");251		} 252		try {253			hexString = randomData.nextHexString(0);254			junit.framework.Assert.fail("zero length requested -- expecting IllegalArgumentException");255		} catch (java.lang.IllegalArgumentException ex) {256		}257		if ((hexString.length()) != 1) {258			junit.framework.Assert.fail("incorrect length for generated string");259		} 260		org.apache.commons.math.stat.Frequency f = new org.apache.commons.math.stat.Frequency();261		for (int i = 0 ; i < (smallSampleSize) ; i++) {262			hexString = randomData.nextHexString(100);263			if ((hexString.length()) != 100) {264				junit.framework.Assert.fail("incorrect length for generated string");265			} 266			for (int j = 0 ; j < (hexString.length()) ; j++) {267				f.addValue(hexString.substring(j, (j + 1)));268			}269		}270		double[] expected = new double[16];271		long[] observed = new long[16];272		for (int i = 0 ; i < 16 ; i++) {273			expected[i] = (((double)(smallSampleSize)) * 100) / 16;274			observed[i] = f.getCount(hex[i]);275		}276		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 37.7));277	}278	public void testNextSecureHex() {279		try {280			randomData.nextSecureHexString(-1);281			junit.framework.Assert.fail("negative length -- IllegalArgumentException expected");282		} catch (java.lang.IllegalArgumentException ex) {283		}284		try {285			randomData.nextSecureHexString(0);286			junit.framework.Assert.fail("zero length -- IllegalArgumentException expected");287		} catch (java.lang.IllegalArgumentException ex) {288		}289		java.lang.String hexString = randomData.nextSecureHexString(3);290		if ((hexString.length()) != 3) {291			junit.framework.Assert.fail("incorrect length for generated string");292		} 293		hexString = randomData.nextSecureHexString(1);294		if ((hexString.length()) != 1) {295			junit.framework.Assert.fail("incorrect length for generated string");296		} 297		try {298			hexString = randomData.nextSecureHexString(0);299			junit.framework.Assert.fail("zero length requested -- expecting IllegalArgumentException");300		} catch (java.lang.IllegalArgumentException ex) {301		}302		if ((hexString.length()) != 1) {303			junit.framework.Assert.fail("incorrect length for generated string");304		} 305		org.apache.commons.math.stat.Frequency f = new org.apache.commons.math.stat.Frequency();306		for (int i = 0 ; i < (smallSampleSize) ; i++) {307			hexString = randomData.nextSecureHexString(100);308			if ((hexString.length()) != 100) {309				junit.framework.Assert.fail("incorrect length for generated string");310			} 311			for (int j = 0 ; j < (hexString.length()) ; j++) {312				f.addValue(hexString.substring(j, (j + 1)));313			}314		}315		double[] expected = new double[16];316		long[] observed = new long[16];317		for (int i = 0 ; i < 16 ; i++) {318			expected[i] = (((double)(smallSampleSize)) * 100) / 16;319			observed[i] = f.getCount(hex[i]);320		}321		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 37.7));322	}323	public void testNextUniform() {324		try {325			randomData.nextUniform(4, 3);326			junit.framework.Assert.fail("IllegalArgumentException expected");327		} catch (java.lang.IllegalArgumentException ex) {328		}329		try {330			randomData.nextUniform(3, 3);331			junit.framework.Assert.fail("IllegalArgumentException expected");332		} catch (java.lang.IllegalArgumentException ex) {333		}334		double[] expected = new double[]{ 500 , 500 };335		long[] observed = new long[]{ 0 , 0 };336		double lower = -1.0;337		double upper = 20.0;338		double midpoint = (lower + upper) / 2.0;339		double result = 0;340		for (int i = 0 ; i < 1000 ; i++) {341			result = randomData.nextUniform(lower, upper);342			if ((result == lower) || (result == upper)) {343				junit.framework.Assert.fail(("generated value equal to an endpoint: " + result));344			} 345			if (result < midpoint) {346				(observed[0])++;347			} else {348				(observed[1])++;349			}350		}351		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 10.83));352	}353	public void testNextUniformExclusiveEndpoints() {354		for (int i = 0 ; i < 1000 ; i++) {355			double u = randomData.nextUniform(0.99, 1);356			junit.framework.Assert.assertTrue(((u > 0.99) && (u < 1)));357		}358	}359	public void testNextGaussian() {360		try {361			randomData.nextGaussian(0, 0);362			junit.framework.Assert.fail("zero sigma -- IllegalArgumentException expected");363		} catch (java.lang.IllegalArgumentException ex) {364		}365		org.apache.commons.math.stat.descriptive.SummaryStatistics u = new org.apache.commons.math.stat.descriptive.SummaryStatistics();366		for (int i = 0 ; i < (largeSampleSize) ; i++) {367			u.addValue(randomData.nextGaussian(0, 1));368		}369		double xbar = u.getMean();370		double s = u.getStandardDeviation();371		double n = u.getN();372		junit.framework.Assert.assertTrue((((java.lang.Math.abs(xbar)) / (s / (java.lang.Math.sqrt(n)))) < 3.29));373	}374	public void testNextExponential() {375		try {376			randomData.nextExponential(-1);377			junit.framework.Assert.fail("negative mean -- expecting IllegalArgumentException");378		} catch (java.lang.IllegalArgumentException ex) {379		}380		try {381			randomData.nextExponential(0);382			junit.framework.Assert.fail("zero mean -- expecting IllegalArgumentException");383		} catch (java.lang.IllegalArgumentException ex) {384		}385		long cumFreq = 0;386		double v = 0;387		for (int i = 0 ; i < (largeSampleSize) ; i++) {388			v = randomData.nextExponential(1);389			junit.framework.Assert.assertTrue("exponential deviate postive", (v > 0));390			if (v < 2) {391				cumFreq++;392			} 393		}394		junit.framework.Assert.assertEquals("exponential cumulative distribution", (((double)(cumFreq)) / ((double)(largeSampleSize))), 0.8646647167633873, 0.2);395	}396	public void testConfig() {397		randomData.reSeed(1000);398		double v = randomData.nextUniform(0, 1);399		randomData.reSeed();400		junit.framework.Assert.assertTrue("different seeds", ((java.lang.Math.abs((v - (randomData.nextUniform(0, 1))))) > 1.0E-11));401		randomData.reSeed(1000);402		junit.framework.Assert.assertEquals("same seeds", v, randomData.nextUniform(0, 1), 1.0E-11);403		randomData.reSeedSecure(1000);404		java.lang.String hex = randomData.nextSecureHexString(40);405		randomData.reSeedSecure();406		junit.framework.Assert.assertTrue("different seeds", !(hex.equals(randomData.nextSecureHexString(40))));407		randomData.reSeedSecure(1000);408		junit.framework.Assert.assertTrue("same seeds", !(hex.equals(randomData.nextSecureHexString(40))));409		org.apache.commons.math.random.RandomDataImpl rd = new org.apache.commons.math.random.RandomDataImpl();410		rd.reSeed(100);411		rd.nextLong(1, 2);412		org.apache.commons.math.random.RandomDataImpl rd2 = new org.apache.commons.math.random.RandomDataImpl();413		rd2.reSeedSecure(2000);414		rd2.nextSecureLong(1, 2);415		rd = new org.apache.commons.math.random.RandomDataImpl();416		rd.reSeed();417		rd.nextLong(1, 2);418		rd2 = new org.apache.commons.math.random.RandomDataImpl();419		rd2.reSeedSecure();420		rd2.nextSecureLong(1, 2);421	}422	public void testNextSample() {423		java.lang.Object[][] c = new java.lang.Object[][]{ new java.lang.Object[]{ "0" , "1" } , new java.lang.Object[]{ "0" , "2" } , new java.lang.Object[]{ "0" , "3" } , new java.lang.Object[]{ "0" , "4" } , new java.lang.Object[]{ "1" , "2" } , new java.lang.Object[]{ "1" , "3" } , new java.lang.Object[]{ "1" , "4" } , new java.lang.Object[]{ "2" , "3" } , new java.lang.Object[]{ "2" , "4" } , new java.lang.Object[]{ "3" , "4" } };424		long[] observed = new long[]{ 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 , 0 };425		double[] expected = new double[]{ 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 , 100 };426		java.util.HashSet<java.lang.Object> cPop = new java.util.HashSet<java.lang.Object>();427		for (int i = 0 ; i < 5 ; i++) {428			cPop.add(java.lang.Integer.toString(i));429		}430		java.lang.Object[] sets = new java.lang.Object[10];431		for (int i = 0 ; i < 10 ; i++) {432			java.util.HashSet<java.lang.Object> hs = new java.util.HashSet<java.lang.Object>();433			hs.add(c[i][0]);434			hs.add(c[i][1]);435			sets[i] = hs;436		}437		for (int i = 0 ; i < 1000 ; i++) {438			java.lang.Object[] cSamp = randomData.nextSample(cPop, 2);439			(observed[findSample(sets, cSamp)])++;440		}441		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 27.88));442		java.util.HashSet<java.lang.Object> hs = new java.util.HashSet<java.lang.Object>();443		hs.add("one");444		java.lang.Object[] one = randomData.nextSample(hs, 1);445		java.lang.String oneString = ((java.lang.String)(one[0]));446		if (((one.length) != 1) || (!(oneString.equals("one")))) {447			junit.framework.Assert.fail("bad sample for set size = 1, sample size = 1");448		} 449		try {450			one = randomData.nextSample(hs, 2);451			junit.framework.Assert.fail("sample size > set size, expecting IllegalArgumentException");452		} catch (java.lang.IllegalArgumentException ex) {453		}454		try {455			hs = new java.util.HashSet<java.lang.Object>();456			one = randomData.nextSample(hs, 0);457			junit.framework.Assert.fail("n = k = 0, expecting IllegalArgumentException");458		} catch (java.lang.IllegalArgumentException ex) {459		}460	}461	@java.lang.SuppressWarnings(value = "unchecked")462	private int findSample(java.lang.Object[] u, java.lang.Object[] samp) {463		for (int i = 0 ; i < (u.length) ; i++) {464			java.util.HashSet<java.lang.Object> set = ((java.util.HashSet<java.lang.Object>)(u[i]));465			java.util.HashSet<java.lang.Object> sampSet = new java.util.HashSet<java.lang.Object>();466			for (int j = 0 ; j < (samp.length) ; j++) {467				sampSet.add(samp[j]);468			}469			if (set.equals(sampSet)) {470				return i;471			} 472		}473		junit.framework.Assert.fail((((("sample not found:{" + (samp[0])) + ",") + (samp[1])) + "}"));474		return -1;475	}476	public void testNextPermutation() {477		int[][] p = new int[][]{ new int[]{ 0 , 1 , 2 } , new int[]{ 0 , 2 , 1 } , new int[]{ 1 , 0 , 2 } , new int[]{ 1 , 2 , 0 } , new int[]{ 2 , 0 , 1 } , new int[]{ 2 , 1 , 0 } };478		long[] observed = new long[]{ 0 , 0 , 0 , 0 , 0 , 0 };479		double[] expected = new double[]{ 100 , 100 , 100 , 100 , 100 , 100 };480		for (int i = 0 ; i < 600 ; i++) {481			int[] perm = randomData.nextPermutation(3, 3);482			(observed[findPerm(p, perm)])++;483		}484		junit.framework.Assert.assertTrue("chi-square test -- will fail about 1 in 1000 times", ((testStatistic.chiSquare(expected, observed)) < 20.52));485		int[] perm = randomData.nextPermutation(1, 1);486		if (((perm.length) != 1) || ((perm[0]) != 0)) {487			junit.framework.Assert.fail("bad permutation for n = 1, sample k = 1");488			try {489				perm = randomData.nextPermutation(2, 3);490				junit.framework.Assert.fail("permutation k > n, expecting IllegalArgumentException");491			} catch (java.lang.IllegalArgumentException ex) {492			}493			try {494				perm = randomData.nextPermutation(0, 0);495				junit.framework.Assert.fail("permutation k = n = 0, expecting IllegalArgumentException");496			} catch (java.lang.IllegalArgumentException ex) {497			}498			try {499				perm = randomData.nextPermutation(-1, -3);500				junit.framework.Assert.fail("permutation k < n < 0, expecting IllegalArgumentException");501			} catch (java.lang.IllegalArgumentException ex) {502			}503		} 504	}505	private int findPerm(int[][] p, int[] samp) {506		for (int i = 0 ; i < (p.length) ; i++) {507			boolean good = true;508			for (int j = 0 ; j < (samp.length) ; j++) {509				if ((samp[j]) != (p[i][j])) {510					good = false;511				} 512			}513			if (good) {514				return i;515			} 516		}517		junit.framework.Assert.fail("permutation not found");518		return -1;519	}520}...

Full Screen

Full Screen

Source:TestUtilsTest.java Github

copy

Full Screen

...15		junit.framework.Assert.assertTrue("chi-square test reject", org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(expected1, observed1, 0.07));16		junit.framework.Assert.assertTrue("chi-square test accept", !(org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(expected1, observed1, 0.05)));17		try {18			org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(expected1, observed1, 95);19			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");20		} catch (java.lang.IllegalArgumentException ex) {21		}22		long[] tooShortObs = new long[]{ 0 };23		double[] tooShortEx = new double[]{ 1 };24		try {25			org.apache.commons.math.stat.inference.TestUtils.chiSquare(tooShortEx, tooShortObs);26			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");27		} catch (java.lang.IllegalArgumentException ex) {28		}29		long[] unMatchedObs = new long[]{ 0 , 1 , 2 , 3 };30		double[] unMatchedEx = new double[]{ 1 , 1 , 2 };31		try {32			org.apache.commons.math.stat.inference.TestUtils.chiSquare(unMatchedEx, unMatchedObs);33			junit.framework.Assert.fail("arrays have different lengths, IllegalArgumentException expected");34		} catch (java.lang.IllegalArgumentException ex) {35		}36		expected[0] = 0;37		try {38			org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(expected, observed, 0.01);39			junit.framework.Assert.fail("bad expected count, IllegalArgumentException expected");40		} catch (java.lang.IllegalArgumentException ex) {41		}42		expected[0] = 1;43		observed[0] = -1;44		try {45			org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(expected, observed, 0.01);46			junit.framework.Assert.fail("bad expected count, IllegalArgumentException expected");47		} catch (java.lang.IllegalArgumentException ex) {48		}49	}50	public void testChiSquareIndependence() throws java.lang.Exception {51		long[][] counts = new long[][]{ new long[]{ 40 , 22 , 43 } , new long[]{ 91 , 21 , 28 } , new long[]{ 60 , 10 , 22 } };52		junit.framework.Assert.assertEquals("chi-square test statistic", 22.709027688, org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts), 1.0E-9);53		junit.framework.Assert.assertEquals("chi-square p-value", 1.44751460134E-4, org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts), 1.0E-9);54		junit.framework.Assert.assertTrue("chi-square test reject", org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts, 2.0E-4));55		junit.framework.Assert.assertTrue("chi-square test accept", !(org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts, 1.0E-4)));56		long[][] counts2 = new long[][]{ new long[]{ 10 , 15 } , new long[]{ 30 , 40 } , new long[]{ 60 , 90 } };57		junit.framework.Assert.assertEquals("chi-square test statistic", 0.168965517241, org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts2), 1.0E-9);58		junit.framework.Assert.assertEquals("chi-square p-value", 0.918987499852, org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts2), 1.0E-9);59		junit.framework.Assert.assertTrue("chi-square test accept", !(org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts2, 0.1)));60		long[][] counts3 = new long[][]{ new long[]{ 40 , 22 , 43 } , new long[]{ 91 , 21 , 28 } , new long[]{ 60 , 10 } };61		try {62			org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts3);63			junit.framework.Assert.fail("Expecting IllegalArgumentException");64		} catch (java.lang.IllegalArgumentException ex) {65		}66		long[][] counts4 = new long[][]{ new long[]{ 40 , 22 , 43 } };67		try {68			org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts4);69			junit.framework.Assert.fail("Expecting IllegalArgumentException");70		} catch (java.lang.IllegalArgumentException ex) {71		}72		long[][] counts5 = new long[][]{ new long[]{ 40 } , new long[]{ 40 } , new long[]{ 30 } , new long[]{ 10 } };73		try {74			org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts5);75			junit.framework.Assert.fail("Expecting IllegalArgumentException");76		} catch (java.lang.IllegalArgumentException ex) {77		}78		long[][] counts6 = new long[][]{ new long[]{ 10 , -2 } , new long[]{ 30 , 40 } , new long[]{ 60 , 90 } };79		try {80			org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts6);81			junit.framework.Assert.fail("Expecting IllegalArgumentException");82		} catch (java.lang.IllegalArgumentException ex) {83		}84		try {85			org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts, 0);86			junit.framework.Assert.fail("Expecting IllegalArgumentException");87		} catch (java.lang.IllegalArgumentException ex) {88		}89	}90	public void testChiSquareLargeTestStatistic() throws java.lang.Exception {91		double[] exp = new double[]{ 3389119.5 , 649136.6 , 285745.4 , 2.535736476E7 , 1.129118978E7 , 543628.0 , 232921.0 , 437665.75 };92		long[] obs = new long[]{ 2372383 , 584222 , 257170 , 17750155 , 7903832 , 489265 , 209628 , 393899 };93		org.apache.commons.math.stat.inference.ChiSquareTestImpl csti = new org.apache.commons.math.stat.inference.ChiSquareTestImpl();94		double cst = csti.chiSquareTest(exp, obs);95		junit.framework.Assert.assertEquals("chi-square p-value", 0.0, cst, 0.001);96		junit.framework.Assert.assertEquals("chi-square test statistic", 114875.90421929007, org.apache.commons.math.stat.inference.TestUtils.chiSquare(exp, obs), 1.0E-9);97	}98	public void testChiSquareZeroCount() throws java.lang.Exception {99		long[][] counts = new long[][]{ new long[]{ 40 , 0 , 4 } , new long[]{ 91 , 1 , 2 } , new long[]{ 60 , 2 , 0 } };100		junit.framework.Assert.assertEquals("chi-square test statistic", 9.67444662263, org.apache.commons.math.stat.inference.TestUtils.chiSquare(counts), 1.0E-9);101		junit.framework.Assert.assertEquals("chi-square p-value", 0.0462835770603, org.apache.commons.math.stat.inference.TestUtils.chiSquareTest(counts), 1.0E-9);102	}103	private double[] tooShortObs = new double[]{ 1.0 };104	private double[] emptyObs = new double[]{  };105	private org.apache.commons.math.stat.descriptive.SummaryStatistics emptyStats = new org.apache.commons.math.stat.descriptive.SummaryStatistics();106	public void testOneSampleT() throws java.lang.Exception {107		double[] observed = new double[]{ 93.0 , 103.0 , 95.0 , 101.0 , 91.0 , 105.0 , 96.0 , 94.0 , 101.0 , 88.0 , 98.0 , 94.0 , 101.0 , 92.0 , 95.0 };108		double mu = 100.0;109		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats = null;110		sampleStats = new org.apache.commons.math.stat.descriptive.SummaryStatistics();111		for (int i = 0 ; i < (observed.length) ; i++) {112			sampleStats.addValue(observed[i]);113		}114		junit.framework.Assert.assertEquals("t statistic", -2.81976445346, org.apache.commons.math.stat.inference.TestUtils.t(mu, observed), 1.0E-9);115		junit.framework.Assert.assertEquals("t statistic", -2.81976445346, org.apache.commons.math.stat.inference.TestUtils.t(mu, sampleStats), 1.0E-9);116		junit.framework.Assert.assertEquals("p value", 0.0136390585873, org.apache.commons.math.stat.inference.TestUtils.tTest(mu, observed), 1.0E-9);117		junit.framework.Assert.assertEquals("p value", 0.0136390585873, org.apache.commons.math.stat.inference.TestUtils.tTest(mu, sampleStats), 1.0E-9);118		try {119			org.apache.commons.math.stat.inference.TestUtils.t(mu, ((double[])(null)));120			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");121		} catch (java.lang.IllegalArgumentException ex) {122		}123		try {124			org.apache.commons.math.stat.inference.TestUtils.t(mu, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));125			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");126		} catch (java.lang.IllegalArgumentException ex) {127		}128		try {129			org.apache.commons.math.stat.inference.TestUtils.t(mu, emptyObs);130			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");131		} catch (java.lang.IllegalArgumentException ex) {132		}133		try {134			org.apache.commons.math.stat.inference.TestUtils.t(mu, emptyStats);135			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");136		} catch (java.lang.IllegalArgumentException ex) {137		}138		try {139			org.apache.commons.math.stat.inference.TestUtils.t(mu, tooShortObs);140			junit.framework.Assert.fail("insufficient data to compute t statistic, IllegalArgumentException expected");141		} catch (java.lang.IllegalArgumentException ex) {142		}143		try {144			org.apache.commons.math.stat.inference.TestUtils.tTest(mu, tooShortObs);145			junit.framework.Assert.fail("insufficient data to perform t test, IllegalArgumentException expected");146		} catch (java.lang.IllegalArgumentException ex) {147		}148		try {149			org.apache.commons.math.stat.inference.TestUtils.t(mu, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));150			junit.framework.Assert.fail("insufficient data to compute t statistic, IllegalArgumentException expected");151		} catch (java.lang.IllegalArgumentException ex) {152		}153		try {154			org.apache.commons.math.stat.inference.TestUtils.tTest(mu, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));155			junit.framework.Assert.fail("insufficient data to perform t test, IllegalArgumentException expected");156		} catch (java.lang.IllegalArgumentException ex) {157		}158	}159	public void testOneSampleTTest() throws java.lang.Exception {160		double[] oneSidedP = new double[]{ 2.0 , 0.0 , 6.0 , 6.0 , 3.0 , 3.0 , 2.0 , 3.0 , -6.0 , 6.0 , 6.0 , 6.0 , 3.0 , 0.0 , 1.0 , 1.0 , 0.0 , 2.0 , 3.0 , 3.0 };161		org.apache.commons.math.stat.descriptive.SummaryStatistics oneSidedPStats = new org.apache.commons.math.stat.descriptive.SummaryStatistics();162		for (int i = 0 ; i < (oneSidedP.length) ; i++) {163			oneSidedPStats.addValue(oneSidedP[i]);164		}165		junit.framework.Assert.assertEquals("one sample t stat", 3.86485535541, org.apache.commons.math.stat.inference.TestUtils.t(0.0, oneSidedP), 1.0E-9);166		junit.framework.Assert.assertEquals("one sample t stat", 3.86485535541, org.apache.commons.math.stat.inference.TestUtils.t(0.0, oneSidedPStats), 1.0E-10);167		junit.framework.Assert.assertEquals("one sample p value", 5.21637019637E-4, ((org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedP)) / 2.0), 1.0E-9);168		junit.framework.Assert.assertEquals("one sample p value", 5.21637019637E-4, ((org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedPStats)) / 2.0), 1.0E-4);169		junit.framework.Assert.assertTrue("one sample t-test reject", org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedP, 0.01));170		junit.framework.Assert.assertTrue("one sample t-test reject", org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedPStats, 0.01));171		junit.framework.Assert.assertTrue("one sample t-test accept", !(org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedP, 1.0E-4)));172		junit.framework.Assert.assertTrue("one sample t-test accept", !(org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedPStats, 1.0E-4)));173		try {174			org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedP, 95);175			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");176		} catch (java.lang.IllegalArgumentException ex) {177		}178		try {179			org.apache.commons.math.stat.inference.TestUtils.tTest(0.0, oneSidedPStats, 95);180			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");181		} catch (java.lang.IllegalArgumentException ex) {182		}183	}184	public void testTwoSampleTHeterscedastic() throws java.lang.Exception {185		double[] sample1 = new double[]{ 7.0 , -4.0 , 18.0 , 17.0 , -3.0 , -5.0 , 1.0 , 10.0 , 11.0 , -2.0 };186		double[] sample2 = new double[]{ -1.0 , 12.0 , -1.0 , -3.0 , 3.0 , -5.0 , 5.0 , 2.0 , -11.0 , -1.0 , -3.0 };187		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats1 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();188		for (int i = 0 ; i < (sample1.length) ; i++) {189			sampleStats1.addValue(sample1[i]);190		}191		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats2 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();192		for (int i = 0 ; i < (sample2.length) ; i++) {193			sampleStats2.addValue(sample2[i]);194		}195		junit.framework.Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, org.apache.commons.math.stat.inference.TestUtils.t(sample1, sample2), 1.0E-10);196		junit.framework.Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, org.apache.commons.math.stat.inference.TestUtils.t(sampleStats1, sampleStats2), 1.0E-10);197		junit.framework.Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, sample2), 1.0E-10);198		junit.framework.Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, sampleStats2), 1.0E-10);199		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test reject", org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, sample2, 0.2));200		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test reject", org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, sampleStats2, 0.2));201		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test accept", !(org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, sample2, 0.1)));202		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test accept", !(org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, sampleStats2, 0.1)));203		try {204			org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, sample2, 0.95);205			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");206		} catch (java.lang.IllegalArgumentException ex) {207		}208		try {209			org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, sampleStats2, 0.95);210			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");211		} catch (java.lang.IllegalArgumentException ex) {212		}213		try {214			org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, tooShortObs, 0.01);215			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");216		} catch (java.lang.IllegalArgumentException ex) {217		}218		try {219			org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)), 0.01);220			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");221		} catch (java.lang.IllegalArgumentException ex) {222		}223		try {224			org.apache.commons.math.stat.inference.TestUtils.tTest(sample1, tooShortObs);225			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");226		} catch (java.lang.IllegalArgumentException ex) {227		}228		try {229			org.apache.commons.math.stat.inference.TestUtils.tTest(sampleStats1, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));230			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");231		} catch (java.lang.IllegalArgumentException ex) {232		}233		try {234			org.apache.commons.math.stat.inference.TestUtils.t(sample1, tooShortObs);235			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");236		} catch (java.lang.IllegalArgumentException ex) {237		}238		try {239			org.apache.commons.math.stat.inference.TestUtils.t(sampleStats1, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));240			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");241		} catch (java.lang.IllegalArgumentException ex) {242		}243	}244	public void testTwoSampleTHomoscedastic() throws java.lang.Exception {245		double[] sample1 = new double[]{ 2 , 4 , 6 , 8 , 10 , 97 };246		double[] sample2 = new double[]{ 4 , 6 , 8 , 10 , 16 };247		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats1 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();248		for (int i = 0 ; i < (sample1.length) ; i++) {249			sampleStats1.addValue(sample1[i]);250		}251		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats2 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();252		for (int i = 0 ; i < (sample2.length) ; i++) {253			sampleStats2.addValue(sample2[i]);254		}...

Full Screen

Full Screen

Source:TTestTest.java Github

copy

Full Screen

...26		junit.framework.Assert.assertEquals("p value", 0.0136390585873, testStatistic.tTest(mu, observed), 1.0E-9);27		junit.framework.Assert.assertEquals("p value", 0.0136390585873, testStatistic.tTest(mu, sampleStats), 1.0E-9);28		try {29			testStatistic.t(mu, ((double[])(null)));30			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");31		} catch (java.lang.IllegalArgumentException ex) {32		}33		try {34			testStatistic.t(mu, ((org.apache.commons.math.stat.descriptive.SummaryStatistics)(null)));35			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");36		} catch (java.lang.IllegalArgumentException ex) {37		}38		try {39			testStatistic.t(mu, emptyObs);40			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");41		} catch (java.lang.IllegalArgumentException ex) {42		}43		try {44			testStatistic.t(mu, emptyStats);45			junit.framework.Assert.fail("arguments too short, IllegalArgumentException expected");46		} catch (java.lang.IllegalArgumentException ex) {47		}48		try {49			testStatistic.t(mu, tooShortObs);50			junit.framework.Assert.fail("insufficient data to compute t statistic, IllegalArgumentException expected");51		} catch (java.lang.IllegalArgumentException ex) {52		}53		try {54			testStatistic.tTest(mu, tooShortObs);55			junit.framework.Assert.fail("insufficient data to perform t test, IllegalArgumentException expected");56		} catch (java.lang.IllegalArgumentException ex) {57		}58		try {59			testStatistic.t(mu, tooShortStats);60			junit.framework.Assert.fail("insufficient data to compute t statistic, IllegalArgumentException expected");61		} catch (java.lang.IllegalArgumentException ex) {62		}63		try {64			testStatistic.tTest(mu, tooShortStats);65			junit.framework.Assert.fail("insufficient data to perform t test, IllegalArgumentException expected");66		} catch (java.lang.IllegalArgumentException ex) {67		}68	}69	public void testOneSampleTTest() throws java.lang.Exception {70		double[] oneSidedP = new double[]{ 2.0 , 0.0 , 6.0 , 6.0 , 3.0 , 3.0 , 2.0 , 3.0 , -6.0 , 6.0 , 6.0 , 6.0 , 3.0 , 0.0 , 1.0 , 1.0 , 0.0 , 2.0 , 3.0 , 3.0 };71		org.apache.commons.math.stat.descriptive.SummaryStatistics oneSidedPStats = new org.apache.commons.math.stat.descriptive.SummaryStatistics();72		for (int i = 0 ; i < (oneSidedP.length) ; i++) {73			oneSidedPStats.addValue(oneSidedP[i]);74		}75		junit.framework.Assert.assertEquals("one sample t stat", 3.86485535541, testStatistic.t(0.0, oneSidedP), 1.0E-9);76		junit.framework.Assert.assertEquals("one sample t stat", 3.86485535541, testStatistic.t(0.0, oneSidedPStats), 1.0E-10);77		junit.framework.Assert.assertEquals("one sample p value", 5.21637019637E-4, ((testStatistic.tTest(0.0, oneSidedP)) / 2.0), 1.0E-9);78		junit.framework.Assert.assertEquals("one sample p value", 5.21637019637E-4, ((testStatistic.tTest(0.0, oneSidedPStats)) / 2.0), 1.0E-4);79		junit.framework.Assert.assertTrue("one sample t-test reject", testStatistic.tTest(0.0, oneSidedP, 0.01));80		junit.framework.Assert.assertTrue("one sample t-test reject", testStatistic.tTest(0.0, oneSidedPStats, 0.01));81		junit.framework.Assert.assertTrue("one sample t-test accept", !(testStatistic.tTest(0.0, oneSidedP, 1.0E-4)));82		junit.framework.Assert.assertTrue("one sample t-test accept", !(testStatistic.tTest(0.0, oneSidedPStats, 1.0E-4)));83		try {84			testStatistic.tTest(0.0, oneSidedP, 95);85			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");86		} catch (java.lang.IllegalArgumentException ex) {87		}88		try {89			testStatistic.tTest(0.0, oneSidedPStats, 95);90			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");91		} catch (java.lang.IllegalArgumentException ex) {92		}93	}94	public void testTwoSampleTHeterscedastic() throws java.lang.Exception {95		double[] sample1 = new double[]{ 7.0 , -4.0 , 18.0 , 17.0 , -3.0 , -5.0 , 1.0 , 10.0 , 11.0 , -2.0 };96		double[] sample2 = new double[]{ -1.0 , 12.0 , -1.0 , -3.0 , 3.0 , -5.0 , 5.0 , 2.0 , -11.0 , -1.0 , -3.0 };97		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats1 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();98		for (int i = 0 ; i < (sample1.length) ; i++) {99			sampleStats1.addValue(sample1[i]);100		}101		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats2 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();102		for (int i = 0 ; i < (sample2.length) ; i++) {103			sampleStats2.addValue(sample2[i]);104		}105		junit.framework.Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, testStatistic.t(sample1, sample2), 1.0E-10);106		junit.framework.Assert.assertEquals("two sample heteroscedastic t stat", 1.60371728768, testStatistic.t(sampleStats1, sampleStats2), 1.0E-10);107		junit.framework.Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, testStatistic.tTest(sample1, sample2), 1.0E-10);108		junit.framework.Assert.assertEquals("two sample heteroscedastic p value", 0.128839369622, testStatistic.tTest(sampleStats1, sampleStats2), 1.0E-10);109		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test reject", testStatistic.tTest(sample1, sample2, 0.2));110		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test reject", testStatistic.tTest(sampleStats1, sampleStats2, 0.2));111		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test accept", !(testStatistic.tTest(sample1, sample2, 0.1)));112		junit.framework.Assert.assertTrue("two sample heteroscedastic t-test accept", !(testStatistic.tTest(sampleStats1, sampleStats2, 0.1)));113		try {114			testStatistic.tTest(sample1, sample2, 0.95);115			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");116		} catch (java.lang.IllegalArgumentException ex) {117		}118		try {119			testStatistic.tTest(sampleStats1, sampleStats2, 0.95);120			junit.framework.Assert.fail("alpha out of range, IllegalArgumentException expected");121		} catch (java.lang.IllegalArgumentException ex) {122		}123		try {124			testStatistic.tTest(sample1, tooShortObs, 0.01);125			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");126		} catch (java.lang.IllegalArgumentException ex) {127		}128		try {129			testStatistic.tTest(sampleStats1, tooShortStats, 0.01);130			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");131		} catch (java.lang.IllegalArgumentException ex) {132		}133		try {134			testStatistic.tTest(sample1, tooShortObs);135			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");136		} catch (java.lang.IllegalArgumentException ex) {137		}138		try {139			testStatistic.tTest(sampleStats1, tooShortStats);140			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");141		} catch (java.lang.IllegalArgumentException ex) {142		}143		try {144			testStatistic.t(sample1, tooShortObs);145			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");146		} catch (java.lang.IllegalArgumentException ex) {147		}148		try {149			testStatistic.t(sampleStats1, tooShortStats);150			junit.framework.Assert.fail("insufficient data, IllegalArgumentException expected");151		} catch (java.lang.IllegalArgumentException ex) {152		}153	}154	public void testTwoSampleTHomoscedastic() throws java.lang.Exception {155		double[] sample1 = new double[]{ 2 , 4 , 6 , 8 , 10 , 97 };156		double[] sample2 = new double[]{ 4 , 6 , 8 , 10 , 16 };157		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats1 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();158		for (int i = 0 ; i < (sample1.length) ; i++) {159			sampleStats1.addValue(sample1[i]);160		}161		org.apache.commons.math.stat.descriptive.SummaryStatistics sampleStats2 = new org.apache.commons.math.stat.descriptive.SummaryStatistics();162		for (int i = 0 ; i < (sample2.length) ; i++) {163			sampleStats2.addValue(sample2[i]);164		}...

Full Screen

Full Screen

Source:BigFractionFormatTest.java Github

copy

Full Screen

...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();...

Full Screen

Full Screen

Source:FractionFormatTest.java Github

copy

Full Screen

...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);...

Full Screen

Full Screen

Source:AssertionsWithoutMessageCheck.java Github

copy

Full Screen

...24    junit.framework.Assert.assertNotSame("foo", "bar"); // Noncompliant25    junit.framework.Assert.assertNotSame("foo", "foo", "bar");26    junit.framework.Assert.assertSame("foo", "bar"); // Noncompliant27    junit.framework.Assert.assertSame("foo", "foo", "bar");28    org.junit.Assert.fail(); // Noncompliant29    org.junit.Assert.fail("Foo");30    junit.framework.Assert.fail(); // Noncompliant31    junit.framework.Assert.fail("Foo");32    org.fest.assertions.Fail.fail(); // Noncompliant33    org.fest.assertions.Fail.fail("foo");34    org.fest.assertions.Fail.fail("foo",  null);35    org.fest.assertions.Fail.failure("foo");36  }37}...

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1junit.framework.Assert.fail("message");2junit.framework.TestCase.fail("message");3junit.framework.Assert.fail("message");4junit.framework.TestCase.fail("message");5junit.framework.Assert.fail("message");6junit.framework.TestCase.fail("message");7junit.framework.Assert.fail("message");8junit.framework.TestCase.fail("message");9junit.framework.Assert.fail("message");10junit.framework.TestCase.fail("message");11junit.framework.Assert.fail("message");12junit.framework.TestCase.fail("message");13junit.framework.Assert.fail("message");14junit.framework.TestCase.fail("message");15junit.framework.Assert.fail("message");16junit.framework.TestCase.fail("message");17junit.framework.Assert.fail("message");18junit.framework.TestCase.fail("message");19junit.framework.Assert.fail("message");20junit.framework.TestCase.fail("message");21junit.framework.Assert.fail("message");22junit.framework.TestCase.fail("message");23junit.framework.Assert.fail("message");24junit.framework.TestCase.fail("message");25junit.framework.Assert.fail("message");

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1import org.junit.Test;2import org.junit.Assert;3public class TestFail {4    public void testFail() {5        Assert.fail("This is a failure");6    }7}8	at org.junit.Assert.fail(Assert.java:86)9	at org.junit.Assert.fail(Assert.java:95)10	at TestFail.testFail(TestFail.java:10)11	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)12	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)13	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)14	at java.lang.reflect.Method.invoke(Method.java:597)15	at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)16	at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)17	at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)18	at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)19	at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)20	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)21	at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)22	at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)23	at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)24	at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)25	at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)26	at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)27	at org.junit.runners.ParentRunner.run(ParentRunner.java:292)28	at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)29	at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)30	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)31	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)32	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)33	at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2public class Test {3   public void testAssert() {4      String str1 = "Junit";5      String str2 = "Junit";6      String str3 = "test";7      String str4 = "test";8      String str5 = null;9      int val1 = 5;10      int val2 = 6;11      String[] expectedArray = {"one", "two", "three"};12      String[] resultArray = {"one", "two", "three"};13      Assert.assertEquals(str1, str2);14      Assert.assertTrue(val1 < val2);15      Assert.assertFalse(val1 > val2);16      Assert.assertNotNull(str1);17      Assert.assertNull(str5);18      Assert.assertSame(str3, str4);19      Assert.assertNotSame(str1, str3);20      Assert.assertArrayEquals(expectedArray, resultArray);21   }22}

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1import junit.framework.Assert;2import org.testng.annotations.Test;3public class TestNGTest {4    public void testFail() {5        Assert.fail("This test is supposed to fail.");6    }7}8import junit.framework.Assert;9import org.testng.annotations.Test;10public class TestNGTest {11    public void testSkip() {12        Assert.skip("This test is skipped.");13    }14}15import org.testng.ITestResult;16import org.testng.annotations.Test;17public class TestNGTest {18    public void testSkip() {19        ITestResult result = Reporter.getCurrentTestResult();20        if (result.isTestSkipped()) {21        }22    }23}24import org.testng.ITestResult;25import org.testng.annotations.Test;26public class TestNGTest {27    public void testSkip() {28        ITestResult result = Reporter.getCurrentTestResult();29        if (result.isSuccess()) {30        }31    }32}33import org.testng.ITestResult;34import org.testng.annotations.Test;35public class TestNGTest {

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1import org.junit.*;2import static org.junit.Assert.*;3public class TestFail {4   public void testFail() {5      fail("This test always fails");6   }7}8import org.junit.*;9import static org.junit.Assert.*;10public class TestAssertTrue {11   public void testAssertTrue() {12      assertTrue("This test always passes", true);13   }14}15import org.junit.*;16import static org.junit.Assert.*;17public class TestAssertFalse {18   public void testAssertFalse() {19      assertFalse("This test always fails", false);20   }21}22import org.junit.*;23import static org.junit.Assert.*;24public class TestAssertEquals {25   public void testAssertEquals() {26      assertEquals("This test always passes", "string1", "string1");27   }28}29import org.junit.*;30import static org.junit.Assert.*;31public class TestAssertSame {32   public void testAssertSame() {33      Integer aNumber = Integer.valueOf(768);34      assertSame("Should be same", aNumber, aNumber);35   }36}37import org.junit.*;38import static org.junit.Assert.*;39public class TestAssertNotSame {40   public void testAssertNotSame() {41      Integer aNumber = Integer.valueOf(768);42      assertNotSame("Should not be same Object", aNumber, Integer.valueOf(768));43   }44}45import org.junit.*;46import static org.junit.Assert.*;47public class TestAssertNull {48   public void testAssertNull() {49      assertNull("Should be null", null);50   }51}52import org.junit.*;53import static org.junit.Assert.*;54public class TestAssertNotNull {55   public void testAssertNotNull() {56      assertNotNull("Should not be null", new Object());57   }58}

Full Screen

Full Screen

fail

Using AI Code Generation

copy

Full Screen

1public void test() {2    Assert.fail("Not yet implemented");3}4public void test() {5    Assert.fail("Not yet implemented");6}7public void test() {8    Assert.fail("Not yet implemented");9}10public void test() {11    Assert.fail("Not yet implemented");12}

Full Screen

Full Screen

JUnit Tutorial:

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

JUnit Tutorial Chapters:

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

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

JUnit Certification:

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

Run junit automation tests on LambdaTest cloud grid

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

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful