private void assertTimestampsAreCloseEnough(long timestamp1, long timestamp2) { assertTrue( String.format( "timestamps %s and %s are not close enough diff: %s", timestamp1, timestamp2, Math.abs(timestamp1 - timestamp2)), Math.abs(timestamp1 - timestamp2) < 50); }
// Note this is a bit of a integration test not a strict unit test @Test public void testFillingAMoreComplicatedBoundingRectangle() throws Exception { double xStart = 0.0; double xStop = 25.5; double yStart = 0.0; double yStop = 33.33; double xStep = 0.4; double yStep = 0.6; RectangularROI roi = new RectangularROI(); roi.setPoint(Math.min(xStart, xStop), Math.min(yStart, yStop)); roi.setLengths(Math.abs(xStop - xStart), Math.abs(yStop - yStart)); RasterModel model = new RasterModel(); model.setxStep(xStep); model.setyStep(yStep); // Get the point list IPointGenerator<RasterModel, Point> gen = service.createGenerator(model, roi); List<Point> pointList = gen.createPoints(); int rows = (int) (Math.floor((xStop - xStart) / xStep) + 1); int cols = (int) (Math.floor((yStop - yStart) / yStep) + 1); // Check the list size assertEquals("Point list size should be correct", rows * cols, pointList.size()); // Check some points assertEquals(new Point(0, xStart, 0, yStart), pointList.get(0)); assertEquals(xStart + 3 * xStep, pointList.get(3).getX(), 1e-8); // TODO more GeneratorUtil.testGeneratorPoints(gen); }
@Test public void testProductResult() { int tS = (int) Math.pow(2, 12); int tSum = tS * (tS + 1) / 2; for (short k = 0; k < tGP12.getBladeCount(); k++) { short[] tSpot = tGP12.getResult(k); int tSumP = 0; for (int j = 0; j < tSpot.length; j++) { tSumP += Math.abs(tSpot[j]); } assertTrue(tSum == tSumP); } tS = (int) Math.pow(2, 8); tSum = tS * (tS + 1) / 2; for (short k = 0; k < tGP8.getBladeCount(); k++) { short[] tSpot = tGP8.getResult(k); int tSumP = 0; for (int j = 0; j < tSpot.length; j++) { tSumP += Math.abs(tSpot[j]); } assertTrue(tSum == tSumP); } tS = (int) Math.pow(2, 4); tSum = tS * (tS + 1) / 2; for (short k = 0; k < tGP4.getBladeCount(); k++) { short[] tSpot = tGP4.getResult(k); int tSumP = 0; for (int j = 0; j < tSpot.length; j++) { tSumP += Math.abs(tSpot[j]); } assertTrue(tSum == tSumP); } }
@Test public void testMerge() throws LinearCountingMergeException { int numToMerge = 5; int size = 65536; int cardinality = 1000; LinearCounting[] lcs = new LinearCounting[numToMerge]; LinearCounting baseline = new LinearCounting(size); for (int i = 0; i < numToMerge; i++) { lcs[i] = new LinearCounting(size); for (int j = 0; j < cardinality; j++) { double val = Math.random(); lcs[i].offer(val); baseline.offer(val); } } int expectedCardinality = numToMerge * cardinality; long mergedEstimate = LinearCounting.mergeEstimators(lcs).cardinality(); double error = Math.abs(mergedEstimate - expectedCardinality) / (double) expectedCardinality; assertEquals(0.01, error, 0.01); LinearCounting lc = lcs[0]; lcs = Arrays.asList(lcs).subList(1, lcs.length).toArray(new LinearCounting[0]); mergedEstimate = lc.merge(lcs).cardinality(); error = Math.abs(mergedEstimate - expectedCardinality) / (double) expectedCardinality; assertEquals(0.01, error, 0.01); long baselineEstimate = baseline.cardinality(); assertEquals(baselineEstimate, mergedEstimate); }
private boolean near(double d1, double d2, double tol) { if (d1 == 0 || d2 == 0) { return Math.abs(d1 - d2) < tol; } double ratio = d1 / d2; return (Math.abs(ratio) - 1) < tol; }
@Test public void test() { assertEquals(0, Math.abs(0)); assertEquals(1, Math.abs(-1)); assertEquals(1, Math.abs(1)); assertEquals(Integer.MAX_VALUE, Math.abs(Integer.MAX_VALUE)); assertEquals(-Integer.MIN_VALUE, Math.abs(Integer.MIN_VALUE)); }
@Test public void test9680_WhichHasAOneThousanthFraction() throws Exception { String TEST_REACH_CLIENT_ID = "9680"; Long TEST_REACH_SYSTEM_ID = 9680L; Long ONLY_REACH_UPSTREAM_OF_TEST_REACH_ID = 9681L; // Create terminal reaches and put in cache List<String> targetList = new ArrayList<String>(); targetList.add(TEST_REACH_CLIENT_ID); TerminalReaches targets = new TerminalReaches(TEST_MODEL_ID, targetList); ConfiguredCache.TerminalReaches.put(targets.getId(), targets); // Load predict data and model incremental areas - used for comparison PredictData pd = SharedApplication.getInstance().getPredictData(TEST_MODEL_ID); DataTable incrementalReachAreas = SharedApplication.getInstance() .getCatchmentAreas(new UnitAreaRequest(TEST_MODEL_ID, AreaType.INCREMENTAL)); // Stats on the test reach int testReachRowNumber = pd.getRowForReachID(TEST_REACH_SYSTEM_ID); Double testReachFrac = pd.getTopo().getDouble(testReachRowNumber, PredictData.TOPO_FRAC_COL); Double testReachFractionedWatershedArea = new CalcFractionedWatershedArea( new FractionedWatershedAreaRequest( new ReachID(TEST_MODEL_ID, TEST_REACH_SYSTEM_ID))) .run(); Double testReachUnfractionedWatershedArea = new CalcFractionedWatershedArea( new FractionedWatershedAreaRequest( new ReachID(TEST_MODEL_ID, TEST_REACH_SYSTEM_ID), false, false, true)) .run(); Double testReachIncrementalArea = incrementalReachAreas.getDouble(testReachRowNumber, 1); // load stats on the only reach immediately upstream of the test reach Double upstreamReachFractionedWatershedArea = new CalcFractionedWatershedArea( new FractionedWatershedAreaRequest( new ReachID(TEST_MODEL_ID, ONLY_REACH_UPSTREAM_OF_TEST_REACH_ID))) .run(); // Test the assumptions about the basic (non-table form) of the area data assertTrue( Math.abs(testReachFractionedWatershedArea - testReachUnfractionedWatershedArea) > 1d); // These should be different values assertTrue(Math.abs(testReachFractionedWatershedArea - testReachIncrementalArea) > 1d); assertEquals( testReachFrac * upstreamReachFractionedWatershedArea + testReachIncrementalArea, testReachFractionedWatershedArea, .0001d); // Do the same calculation via the Table version of the action CalcFractionedWatershedAreaTable action = new CalcFractionedWatershedAreaTable(targets.getId()); ColumnData data = action.run(); assertEquals(testReachFractionedWatershedArea, data.getDouble(testReachRowNumber), .0001d); }
public static int getIdOf(Graph g, double latitude, double longitude) { int s = g.getNodes(); NodeAccess na = g.getNodeAccess(); for (int i = 0; i < s; i++) { if (Math.abs(na.getLatitude(i) - latitude) < 1e-4 && Math.abs(na.getLongitude(i) - longitude) < 1e-4) { return i; } } return -1; }
@Test public void test() { List<IndicatorDatum> RSIPoints = rsi.getRSIPoints(); assertTrue(RSIPoints.size() == 3); assertTrue(Math.abs(RSIPoints.get(0).getValue() - 91.37931) < 0.00001); assertTrue(Math.abs(RSIPoints.get(1).getValue() - 95.0331125) < 0.00001); assertTrue(Math.abs(RSIPoints.get(2).getValue() - 38.81875) < 0.00001); rsi.incrementalUpdate(new StockTimeFrameData("8", 0, 0, 0, 0, 0, 89.2, false)); assertTrue(Math.abs(RSIPoints.get(3).getValue() - 49.09977) < 0.00001); }
@Test public void scalarProduct() { double[] vectorA = new double[] {4.0, 0.0}; double[] vectorB = new double[] {2.0, 5.0}; double[] vectorC = new double[] {-1.0, 2.0}; double[] vectorD = new double[] {-2.5, -1.5}; assertEquals(8.0, Equation.scalarProduct(vectorA, vectorB), 0.001); assertEquals(8.0, Equation.scalarProduct(vectorB, vectorC), 0.001); assertEquals(-0.5, Equation.scalarProduct(vectorC, vectorD), 0.001); assertFalse(Math.abs(Equation.scalarProduct(vectorA, vectorC) - (-3.0)) < 0.001); assertFalse(Math.abs(Equation.scalarProduct(vectorB, vectorD) - 1.0) < 0.001); assertFalse(Math.abs(Equation.scalarProduct(vectorA, vectorB) - 0.5) < 0.001); }
@Test public void getNormalVector() { DR = DataReader.use("equationTestNodes.txt", "equationTestEdges.txt"); DR.createNodeList(); Edge edge = new Edge( DR.getNodes().get(1), DR.getNodes().get(2), 0, "", 0, "0", "0", 0.1, 0, 0, 0, 0, true); double[] expArr = new double[] {-3.0, 4.0}; double[] expArr2 = new double[] {-3.3, 4.1}; assertArrayEquals(expArr, Equation.getNormalVector(edge), 0.001); assertFalse(Math.abs(expArr2[0] - Equation.getNormalVector(edge)[0]) < 0.001); assertFalse(Math.abs(expArr2[1] - Equation.getNormalVector(edge)[1]) < 0.001); }
@Test public void pluckTest() { SineString ss440 = new SineString(440.0); assertEquals(0.0, ss440.sample(), 0.0001); ss440.pluck(); double sample1 = ss440.sample(); ss440.tic(); double sample2 = ss440.sample(); ss440.tic(); double sample3 = ss440.sample(); assertFalse(Math.abs(sample1 - sample2) < 0.000001); assertFalse(Math.abs(sample1 - sample3) < 0.000001); assertFalse(Math.abs(sample2 - sample3) < 0.000001); }
@Test public void nodesToVector() { DR = DataReader.use("equationTestNodes.txt", "equationTestEdges.txt"); DR.createNodeList(); Node node5 = DR.getNodes().get(5); Node node1 = DR.getNodes().get(1); double[] expArr = new double[] {-3.0, 1.0}; assertArrayEquals(expArr, Equation.nodesToVector(node5, node1), 0.01); Node node2 = DR.getNodes().get(2); Node node3 = DR.getNodes().get(3); double[] expArr2 = new double[] {4.3, 3.2}; assertFalse(Math.abs(expArr2[0] - Equation.nodesToVector(node2, node3)[0]) < 0.001); assertFalse(Math.abs(expArr2[1] - Equation.nodesToVector(node2, node3)[1]) < 0.001); }
@Test public void cosVectorAngle() { double[] vectorA = new double[] {4.0, 0.0}; double[] vectorB = new double[] {2.0, 2.0}; double[] vectorC = new double[] {-3.0, 3.0}; double[] vectorD = new double[] {1.0, 1.73205}; double[] vectorE = new double[] {5.0, 5.0}; assertEquals(0.5, Equation.cosVectorAngle(vectorA, vectorD), 0.001); assertEquals(0.0, Equation.cosVectorAngle(vectorB, vectorC), 0.001); assertEquals(1.0, Equation.cosVectorAngle(vectorB, vectorE), 0.001); assertFalse(Math.abs(Equation.cosVectorAngle(vectorA, vectorB) - 0.5) < 0.001); assertFalse(Math.abs(Equation.cosVectorAngle(vectorB, vectorC) - (-0.5)) < 0.001); assertFalse(Math.abs(Equation.cosVectorAngle(vectorA, vectorB) - 0.5) < 0.001); }
@Test public void testGetBalancedFactor() throws Exception { for (int i = 0; i < 10; i++) { avlTree.insert(i); } Assert.assertTrue(Math.abs(avlTree.getBalancedFactor()) <= 1); }
@Test public void pointsToVector() { double x1 = 2.0; double y1 = 2.0; double x2 = 6.0; double y2 = 5.0; double[] expArr = new double[] {4.0, 3.0}; assertArrayEquals(expArr, Equation.pointsToVector(x1, y1, x2, y2), 0.01); double x3 = 1.0; double y3 = 2.0; double x4 = 6.0; double y4 = 5.0; double[] expArr2 = new double[] {4.3, 3.2}; assertFalse(Math.abs(expArr2[0] - Equation.pointsToVector(x3, y3, x4, y4)[0]) < 0.001); assertFalse(Math.abs(expArr2[1] - Equation.pointsToVector(x3, y3, x4, y4)[1]) < 0.001); }
public boolean containsLatitude(Graph g, EdgeIterator iter, double latitude) { NodeAccess na = g.getNodeAccess(); while (iter.next()) { if (Math.abs(na.getLatitude(iter.getAdjNode()) - latitude) < 1e-4) return true; } return false; }
private void testRetrievePost(List<String> tags) { Post newPost = getPostWithTags(tags); Post.create(newPost); Post retrievedPost = Post.get(newPost.id); assertNotNull("An post should have been returned", retrievedPost); assertEquals("title of retrieved post should have been same", title, retrievedPost.title); assertTrue( "title of retrieved post should have been same", Math.abs(price - retrievedPost.price) < DELTA); assertEquals( "title of retrieved post should have been same", postDuration, retrievedPost.postDuration); assertEquals("title of retrieved post should have been same", city, retrievedPost.city); assertEquals("title of retrieved post should have been same", state, retrievedPost.state); assertEquals("title of retrieved post should have been same", country, retrievedPost.country); assertEquals("title of retrieved post should have been same", zipcode, retrievedPost.zipcode); if (tags == null || tags.size() == 0) { assertEquals("There should have been same zero tags", 0, retrievedPost.tags.size()); } else { assertEquals( "There should have been same number of tags", tags.size(), retrievedPost.tags.size()); for (PostTag postTag : retrievedPost.tags) { assertTrue("The tag was not found", tags.contains(postTag.tag)); } } }
@Test public void vectorLength() { double[] expVector1 = new double[] {-5.0, 0.0}; double[] expVector2 = new double[] {3.0, -1.0}; assertEquals(5.0, Equation.vectorLength(expVector1), 0.001); assertFalse(Math.abs(Equation.vectorLength(expVector2) - 5.0) < 0.001); }
@Test public void triangulate1() throws OutOfRangeException { DataPoint a = new DataPoint(51, 0.25, 108); DataPoint b = new DataPoint(49, 2.5, 32); Coordinate intersection = DataPoint.triangulate(a, b); double expectedLat = 50.23; double expectedLon = 3.7075; double calculatedLat = intersection.getLatitude(); double calculatedLon = intersection.getLongitude(); double faultLat = Math.abs(expectedLat - calculatedLat); double faultLon = Math.abs(expectedLon - calculatedLon); assertTrue(faultLat < TRIANGULATE_TOLERANCE && faultLon < TRIANGULATE_TOLERANCE); }
@Test public void triangulate2() throws OutOfRangeException { DataPoint a = new DataPoint(43.074553, -75.759592, 62.583); DataPoint b = new DataPoint(43.134420, -75.228998, 302.2); Coordinate intersection = DataPoint.triangulate(a, b); double expectedLat = 43.211699; double expectedLon = -75.397193; double calculatedLat = intersection.getLatitude(); double calculatedLon = intersection.getLongitude(); double faultLat = Math.abs(expectedLat - calculatedLat); double faultLon = Math.abs(expectedLon - calculatedLon); assertTrue(faultLat < TRIANGULATE_TOLERANCE && faultLon < TRIANGULATE_TOLERANCE); }
@Override public void testOutputs(HashMap<Variable, Double> outputs) { assertTrue(outputs.containsKey(new MultivectorComponent("abstand", 0))); assertTrue(outputs.containsKey(new MultivectorComponent("nor", 1))); assertTrue(outputs.containsKey(new MultivectorComponent("nor", 2))); assertTrue(outputs.containsKey(new MultivectorComponent("nor", 3))); // assertTrue(outputs.containsKey(new MultivectorComponent("", ))); double abstand = outputs.get(new MultivectorComponent("abstand", 0)); Vec3D nor = new Vec3D( outputs.get(new MultivectorComponent("nor", 1)), outputs.get(new MultivectorComponent("nor", 2)), outputs.get(new MultivectorComponent("nor", 3))); nor.normalize(); nor.scalarMultiplication(abstand); Point3D pBase = nor.applyToPoint(new Point3D(pTest.x, pTest.y, pTest.z)); // pBase must lie on plane and on line // test if on line, assume that dx,dy,dz not zero double tx = (pBase.x - p1.x) / ((p2.x - p1.x)); double ty = (pBase.y - p1.y) / ((p2.y - p1.y)); double tz = (pBase.z - p1.z) / ((p2.z - p1.z)); // since the normal is unique except of a sign: if (Math.abs(tx - ty) > 0.001 || Math.abs(tz - ty) > 0.001) { nor = new Vec3D( outputs.get(new MultivectorComponent("nor", 1)), outputs.get(new MultivectorComponent("nor", 2)), outputs.get(new MultivectorComponent("nor", 3))); nor.normalize(); nor.scalarMultiplication(-abstand); pBase = nor.applyToPoint(new Point3D(pTest.x, pTest.y, pTest.z)); // pBase must lie on plane and on line // test if on line, assume that dx,dy,dz not zero tx = (pBase.x - p1.x) / ((p2.x - p1.x)); ty = (pBase.y - p1.y) / ((p2.y - p1.y)); tz = (pBase.z - p1.z) / ((p2.z - p1.z)); } assertEquals(tx, ty, 0.001); assertEquals(ty, tz, 0.001); // test if on plane Vec3D r = new Vec3D((p2.x - p1.x), (p2.y - p1.y), (p2.z - p1.z)); r.normalize(); Vec3D xmpTest = new Vec3D(pBase.x - pTest.x, pBase.y - pTest.y, pBase.z - pTest.z); double dp = xmpTest.dotProduct(r); assertEquals(0, dp, 0.001); }
protected boolean sufficientlyEqual(Object value1, Object value2) { if (value1 == null) return value2 == null; if (value1 instanceof Date && value2 instanceof Date) { // for dynamic dates. return Math.abs(((Date) value1).getTime() - ((Date) value2).getTime()) < 200L; } else { return value1.equals(value2); } }
@Test public void test() { WorkerConfig workerConfig = new WorkerConfig(); workerConfig.randomizationInterval = 5000L; for (int i = 0; i < 1000; i++) { long t = workerConfig.callRandomizationTerm(); Assert.assertTrue(Math.abs(t) < 2501L); } }
@Test public void distanceBetweenNodes() { DR = DataReader.use("equationTestNodes.txt", "equationTestEdges.txt"); DR.createNodeList(); Node node1 = DR.getNodes().get(1); Node node2 = DR.getNodes().get(2); Node node5 = DR.getNodes().get(5); assertEquals(5.0, Equation.distanceBetweenNodes(node1, node2), 0.001); assertFalse(Math.abs(Equation.distanceBetweenNodes(node2, node5) - 5.0) < 0.001); }
@Test public void distanceBetweenPoints() { double x1 = 1.0; double y1 = 7.5; double x2 = 6.0; double y2 = 5.0; double x3 = 1.0; double y3 = 9.5; assertEquals(2.0, Equation.distanceBetweenPoints(x1, y1, x3, y3), 0.001); assertFalse(Math.abs(Equation.distanceBetweenPoints(x1, y1, x2, y2) - 5.0) < 0.001); }
@Test public void edgeToVector() { DR = DataReader.use("equationTestNodes.txt", "equationTestEdges.txt"); DR.createNodeList(); Edge edge1 = new Edge( DR.getNodes().get(1), DR.getNodes().get(2), 0, "", 0, "0", "0", 0.1, 0, 0, 0, 0, true); Edge edge2 = new Edge( DR.getNodes().get(2), DR.getNodes().get(3), 0, "", 0, "0", "0", 0.1, 0, 0, 0, 0, true); Edge edge3 = new Edge( DR.getNodes().get(3), DR.getNodes().get(4), 0, "", 0, "0", "0", 0.1, 0, 0, 0, 0, true); double[] expArr1 = new double[] {4.0, 3.0}; double[] expArr2 = new double[] {-7.0, 1.0}; double[] expArr3 = new double[] {-2.0, -4.0}; assertArrayEquals(expArr1, Equation.edgeToVector(edge1), 0.001); assertArrayEquals(expArr2, Equation.edgeToVector(edge2), 0.001); assertArrayEquals(expArr3, Equation.edgeToVector(edge3), 0.001); double[] expArr4 = new double[] {1.0, 3.1}; double[] expArr5 = new double[] {-7.5, 2.0}; double[] expArr6 = new double[] {2.0, -4.7}; assertFalse(Math.abs(expArr4[0] - Equation.edgeToVector(edge1)[0]) < 0.001); assertFalse(Math.abs(expArr4[1] - Equation.edgeToVector(edge1)[1]) < 0.001); assertFalse(Math.abs(expArr5[0] - Equation.edgeToVector(edge2)[0]) < 0.001); assertFalse(Math.abs(expArr5[1] - Equation.edgeToVector(edge2)[1]) < 0.001); assertFalse(Math.abs(expArr6[0] - Equation.edgeToVector(edge3)[0]) < 0.001); assertFalse(Math.abs(expArr6[1] - Equation.edgeToVector(edge3)[1]) < 0.001); }
@Test public void testCriaFuncionarioSalarioNegativo() { int salario = 0; try { salario = 0 - Math.abs(random.nextInt(100)); this.factory.criaFuncionario("Nome", salario); fail("Não pode aceitar funcionario com salario negativo: " + salario); } catch (IllegalArgumentException e) { Assert.assertTrue(true); } }
@Test public void distanceBetweenPointAndLine() { DR = DataReader.use("equationTestNodes.txt", "equationTestEdges.txt"); DR.createNodeList(); Edge edge = new Edge( DR.getNodes().get(1), DR.getNodes().get(2), 0, "", 0, "0", "0", 0.1, 0, 0, 0, 0, true); double x1 = 1.0; double y1 = 7.5; assertEquals(5.0, Equation.distanceBetweenPointAndLine(edge, x1, y1), 0.001); double x2 = 1.0; double y2 = 9.5; assertFalse(Math.abs(Equation.distanceBetweenPointAndLine(edge, x2, y2) - 7.0) < 0.001); }
public static void execute( final KyotoTycoonClient client, final int numOperations, final int numThreads) throws Exception { final CountDownLatch latch = new CountDownLatch(numOperations); client.start(); client.clear(); long start = System.currentTimeMillis(); final AtomicInteger errorCount = new AtomicInteger(0); ExecutorService executor = Executors.newFixedThreadPool(numThreads); Random r = new Random(); for (int i = 0; i < numOperations; ++i) { final String key = "key" + i; final String value = String.valueOf(Math.abs(r.nextInt())); executor.submit( new Runnable() { @Override public void run() { try { client.set(key, value); assertEquals(value, client.get(key)); // assertTrue(client.remove(key)); } catch (RuntimeException e) { e.printStackTrace(); fail(); } finally { latch.countDown(); } } }); } executor.shutdown(); latch.await(); if (errorCount.intValue() > 0) { fail(); } long timeElapsed = System.currentTimeMillis() - start; int qps = (int) ((numOperations / (float) timeElapsed) * 1000); System.out.println( client.getClass().getSimpleName() + " - result: " + timeElapsed + "ms, " + qps + "qps"); client.clear(); client.stop(); }