@SuppressWarnings("unchecked") public static <T> void assertSortsCorrectly(T[] expectedOrder, Comparator<T> comparator) throws ArrayComparisonFailure { List<T> versions = new ArrayList<>(Arrays.asList(expectedOrder)); T[] sortedVersions; for (int i = 0; i < SHUFFLES; ++i) { Collections.shuffle(versions); sortedVersions = (T[]) versions.stream().sorted(comparator).toArray(Object[]::new); try { assertArrayEquals(expectedOrder, sortedVersions); } catch (ArrayComparisonFailure e) { assertArrayEquals("Sorting failed for shuffle:\n" + versions + "\nSorted result:\n" + Arrays.toString(sortedVersions) + '\n', expectedOrder, sortedVersions); } } }
/** * as assert function I added that compares hash maps * @param msg * * @param twoLevelMap * @param expectedMap * @throws ArrayComparisonFailure */ private <K1, K2, V> void myAssertMapsEqual( String msg, HashMap<K1, HashMap<K2, V>> expectedMap, HashMap<K1, HashMap<K2, V>> revertedTwoLevelMap ) throws ArrayComparisonFailure { // compare key1 assertTrue(msg, expectedMap.keySet().equals(revertedTwoLevelMap.keySet())); // for each key1, compare all <key2, value>s for (K1 key1 : expectedMap.keySet()) { assertTrue(msg + " comparing key2", expectedMap.get(key1).keySet().equals(revertedTwoLevelMap.get(key1).keySet())); for (K2 key2 : expectedMap.get(key1).keySet()) { assertEquals(msg + " comparing values", expectedMap.get(key1).get(key2), revertedTwoLevelMap.get(key1).get(key2)); } } }
private void sumIsCorrect(float[] data, int width, int height, float boxSize, boolean internal, BlockSumDataFilter filter) throws ArrayComparisonFailure { float[] data1 = data.clone(); float[] data2 = data.clone(); sum(data1, width, height, boxSize); if (internal) { filter.filterInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, 0); } }
private void sumIsCorrect(int[] data, int width, int height, int boxSize, boolean internal, BlockSumDataFilter filter) throws ArrayComparisonFailure { int[] data1 = data.clone(); int[] data2 = data.clone(); sum(data1, width, height, boxSize); if (internal) { filter.filterInternal(data2, width, height, boxSize); intArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %d", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); intArrayEquals(String.format("Arrays do not match: [%dx%d] @ %d", width, height, boxSize), data1, data2, width, height, 0); } }
private void averageIsCorrect(int width, int height, float boxSize, boolean internal, DataFilter filter) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051976); float[] data1 = createData(width, height); float[] data2 = data1.clone(); AverageFilterTest.average(data1, width, height, boxSize); if (internal) { filter.filterInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, 0); } }
private void meanIsCorrect(float[] data, int width, int height, float boxSize, boolean internal, BlockMeanDataFilter filter) throws ArrayComparisonFailure { float[] data1 = data.clone(); float[] data2 = data.clone(); mean(data1, width, height, boxSize); if (internal) { filter.filterInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, 0); } }
private static void arrayNotEquals(String message, Object expecteds, Object actuals) throws ArrayComparisonFailure { if (expecteds == actuals || Arrays.deepEquals(new Object[] {expecteds}, new Object[] {actuals})) { // The reflection-based loop below is potentially very slow, especially for primitive // arrays. The deepEquals check allows us to circumvent it in the usual case where // the arrays are exactly equal. fail(message); } }
protected void assertArrayEquals(final String message, final char[] expecteds, final char[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final float[] expecteds, final float[] actuals, final float delta) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals, delta); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final double[] expecteds, final double[] actuals, final double delta) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals, delta); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final Object[] expecteds, final Object[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final short[] expecteds, final short[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final byte[] expecteds, final byte[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final long[] expecteds, final long[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
protected void assertArrayEquals(final String message, final int[] expecteds, final int[] actuals) throws ArrayComparisonFailure { checkThread(); try { Assert.assertArrayEquals(message, expecteds, actuals); } catch (final AssertionError e) { handleThrowable(e); } }
private static int checkTLV(final byte[] serializedData, final int offset, final byte typeTLVBits, final String typeTLVName, final short lengthTLV, final byte[] valueTLV, final byte... bytesBeforeValue) throws ArrayComparisonFailure { byte[] concreteTlvAwaited = awaitedBytes(typeTLVBits, lengthTLV, valueTLV, bytesBeforeValue); int concreteTlvAwaitLength = concreteTlvAwaited.length; assertArrayEquals("Serialization problem " + typeTLVName, concreteTlvAwaited, ArrayUtils.subarray(serializedData, offset, offset + concreteTlvAwaitLength)); return offset + concreteTlvAwaitLength; }
public static boolean assertArrayEquals(boolean[] expecteds, boolean[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(byte[] expecteds, byte[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(char[] expecteds, char[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(double[] expecteds, double[] actuals, double delta) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals, delta); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(float[] expecteds, float[] actuals, float delta) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals, delta); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(int[] expecteds, int[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(long[] expecteds, long[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(Object[] expecteds, Object[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
public static boolean assertArrayEquals(short[] expecteds, short[] actuals) { try { org.junit.Assert.assertArrayEquals(expecteds, actuals); return true; } catch(ArrayComparisonFailure ignored) { return false; } }
protected void verifyBean(final TestDummyBean beanToVerify, final TestDummyBean refBean1, final TestDummyBean refBean2, final TestDummyBean refBean3) throws ArrayComparisonFailure { Assert.assertArrayEquals("stringList does not contain expected values", new String[] { "value 1", "value 2" }, beanToVerify.getStringList().toArray(new String[0])); Assert.assertArrayEquals("numberList does not contain expected values", new Integer[] { Integer.valueOf(1), Integer.valueOf(2) }, beanToVerify.getIntegerList().toArray(new Integer[0])); Assert.assertArrayEquals("booleanList does not contain expected values", new Boolean[] { Boolean.TRUE, Boolean.FALSE }, beanToVerify.getBooleanList().toArray(new Boolean[0])); Assert.assertArrayEquals("beanList does not contain expected bean", new Object[] { refBean1, refBean2 }, beanToVerify.getBeanList().toArray(new Object[0])); Assert.assertTrue("stringMap does not contain expected keys", beanToVerify.getStringMap().keySet().containsAll(Arrays.asList("strkey1", "strkey2"))); Assert.assertEquals("stringMap[strkey1] does match expected value", "value 1", beanToVerify.getStringMap().get("strkey1")); Assert.assertEquals("stringMap[strkey2] does match expected value", "value 2", beanToVerify.getStringMap().get("strkey2")); Assert.assertTrue("integerMap does not contain expected keys", beanToVerify.getIntegerMap().keySet().containsAll(Arrays.asList("intkey1", "intkey2"))); Assert.assertEquals("integerMap[intkey1] does match expected value", Integer.valueOf(1), beanToVerify.getIntegerMap().get("intkey1")); Assert.assertEquals("integerMap[intkey2] does match expected value", Integer.valueOf(2), beanToVerify.getIntegerMap().get("intkey2")); Assert.assertTrue("booleanMap does not contain expected keys", beanToVerify.getBooleanMap().keySet().containsAll(Arrays.asList("boolkey1", "boolkey2"))); Assert.assertEquals("booleanMap[boolkey1] does match expected value", Boolean.TRUE, beanToVerify.getBooleanMap().get("boolkey1")); Assert.assertEquals("booleanMap[boolkey2] does match expected value", Boolean.FALSE, beanToVerify.getBooleanMap().get("boolkey2")); Assert.assertTrue("beanMap does not contain expected keys", beanToVerify.getBeanMap().keySet().containsAll(Arrays.asList("beankey1", "beankey2"))); Assert.assertSame("beanMap[beankey1] does match expected bean", refBean1, beanToVerify.getBeanMap().get("beankey1")); Assert.assertSame("beanMap[beankey2] does match expected bean", refBean2, beanToVerify.getBeanMap().get("beankey2")); Assert.assertEquals("stringValue does not match expected value", "dummy", beanToVerify.getStringValue()); Assert.assertEquals("numberValue does not match expected value", Integer.valueOf(3), beanToVerify.getIntegerValue()); Assert.assertEquals("booleanValue does not match expected value", Boolean.TRUE, beanToVerify.getBooleanValue()); Assert.assertSame("beanReference does not match expected bean", refBean3, beanToVerify.getBeanReference()); }
@Override protected boolean elementsAreEqual(Suggestion o1, Suggestion o2) { if (!o1.getText().equals(o2.getText())) return false; try { new UnorderedSuggestionParamComparison().arrayEquals("Suggestion.params not equal", o1.paramsArray(), o2.paramsArray()); } catch (ArrayComparisonFailure ignored) { return false; } return true; }
@Override public void arrayEquals(String message, Object expecteds, Object actuals) throws ArrayComparisonFailure { if (expecteds != actuals) { String header = message == null ? "" : message + "\n: "; int expectedsLength = this.assertArraysAreSameLength(expecteds, actuals, header); for (int i = 0; i < expectedsLength; ++i) { Object expected = Array.get(expecteds, i); Object actual = Array.get(actuals, i); if (this.isArray(expected) && this.isArray(actual)) { try { this.arrayEquals(message, expected, actual); } catch (ArrayComparisonFailure var10) { //var10.addDimension(i); //throw var10; throw new ComparisonFailure(header + "array differed first at element [" + i + "]\n", TestUtils.arrayAsString(expected), TestUtils.arrayAsString(actual)); } } else { try { this.assertElementsEqual(expected, actual); } catch (AssertionError var11) { //throw new ArrayComparisonFailure(header, var11, i); throw new ComparisonFailure(header + "array differed first at element [" + i + "]\n", TestUtils.arrayAsString(expecteds), TestUtils.arrayAsString(actuals)); } } } } }
protected void serializeObject( final Object referenceObject, final QExpressions qe, final String expr ) throws IOException, QException, ArrayComparisonFailure { final ByteArrayOutputStream stream = new ByteArrayOutputStream(); final QWriter writer = new DefaultQWriter(); writer.setStream(stream); writer.setEncoding("ISO-8859-1"); writer.write(referenceObject, QConnection.MessageType.SYNC); final byte[] out = stream.toByteArray(); assertArrayEquals("Serialization failed for q expression: " + expr, qe.getBinaryExpression(expr), copyOfRange(out, 8, out.length)); }
private void checkEqual(Spot[] spots, int channel, int slice, int position, int type, MemoryPeakResults actualResults) throws ArrayComparisonFailure { Assert.assertNotNull("Input results are null", actualResults); MemoryPeakResults expectedResults = extract(spots, channel, slice, position, type); Assert.assertEquals("Size differ", expectedResults.size(), actualResults.size()); final float delta = 0; PeakResult[] expected = expectedResults.toArray(); PeakResult[] actual = actualResults.toArray(); for (int i = 0; i < actualResults.size(); i++) { PeakResult p1 = expected[i]; PeakResult p2 = actual[i]; Assert.assertEquals("Peak mismatch @ " + i, p1.getFrame(), p2.getFrame()); Assert.assertEquals("Orig X mismatch @ " + i, p1.getOrigX(), p2.getOrigX()); Assert.assertEquals("Orig Y mismatch @ " + i, p1.getOrigY(), p2.getOrigY()); Assert.assertEquals("Orig value mismatch @ " + i, p1.getOrigValue(), p2.getOrigValue(), delta); Assert.assertEquals("Error mismatch @ " + i, p1.getError(), p2.getError(), 1e-6); Assert.assertEquals("Noise mismatch @ " + i, p1.getNoise(), p2.getNoise(), delta); Assert.assertNotNull("Params is null @ " + i, p2.getParameters()); Assert.assertEquals("Background mismatch @ " + i, p1.getBackground(), p2.getBackground(), delta); Assert.assertEquals("Signal mismatch @ " + i, p1.getSignal(), p2.getSignal(), delta); Assert.assertEquals("XPosition mismatch @ " + i, p1.getXPosition(), p2.getXPosition(), delta); Assert.assertEquals("YPosition mismatch @ " + i, p1.getYPosition(), p2.getYPosition(), delta); Assert.assertEquals("ZPosition mismatch @ " + i, p1.getZPosition(), p2.getZPosition(), delta); for (int j = PeakResult.STANDARD_PARAMETERS, size = p1.getNumberOfParameters(); j < size; j++) { Assert.assertEquals("Parameter mismatch @ " + i, p1.getParameter(j), p2.getParameter(j), 1e-6); } Assert.assertEquals("ID mismatch @ " + i, p1.getId(), p2.getId()); } }
private void weightedSumIsCorrect(float[] data, float[] w, int width, int height, float boxSize, boolean internal, BlockSumDataFilter filter) throws ArrayComparisonFailure { float[] data1 = data.clone(); float[] data2 = data.clone(); weightedSum(data1, w, width, height, boxSize); //// Check the weights do not alter the image sum //double u1 = gdsc.core.utils.Maths.sum(sum(data.clone(), width, height, boxSize)); //double u2 = gdsc.core.utils.Maths.sum(data1); //System.out.printf("[%dx%d] @ %.1f : %g => %g (%g)\n", width, height, boxSize, u1, u2, // gdsc.core.utils.DoubleEquality.relativeError(u1, u2)); if (internal) { filter.filterInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, 0); } }
private void floatCompareBlockMedianNxNInternalAndRollingMedianNxNInternal(MedianFilter filter, int width, int height, int boxSize) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051976); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.blockMedianNxNInternal(data1, width, height, boxSize); filter.rollingMedianNxNInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %d", width, height, boxSize), data1, data2, boxSize); }
private void floatCompareBlockMedian3x3InternalAndRollingMedianNxNInternal(MedianFilter filter, int width, int height) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051977); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.blockMedian3x3Internal(data1, width, height); filter.rollingMedianNxNInternal(data2, width, height, 1); floatArrayEquals(String.format("Arrays do not match: [%dx%d]", width, height), data1, data2, 1); }
private void floatCompareBlockMedian3x3InternalAndBlockMedianNxNInternal(MedianFilter filter, int width, int height) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051977); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.blockMedian3x3Internal(data1, width, height); filter.blockMedianNxNInternal(data2, width, height, 1); floatArrayEquals(String.format("Arrays do not match: [%dx%d]", width, height), data1, data2, 1); }
private void floatCompareRollingMedian3x3InternalAndRollingMedianNxNInternal(MedianFilter filter, int width, int height) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051977); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.rollingMedian3x3Internal(data1, width, height); filter.rollingMedianNxNInternal(data2, width, height, 1); floatArrayEquals(String.format("Arrays do not match: [%dx%d]", width, height), data1, data2, 1); }
private void floatCompareBlockMedianNxNAndRollingMedianNxN(MedianFilter filter, int width, int height, int boxSize) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051976); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.blockMedianNxN(data1, width, height, boxSize); filter.rollingMedianNxN(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %d", width, height, boxSize), data1, data2, 0); }
private void floatCompareBlockMedian3x3AndBlockMedianNxN(MedianFilter filter, int width, int height) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051977); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.blockMedian3x3(data1, width, height); filter.blockMedianNxN(data2, width, height, 1); floatArrayEquals(String.format("Arrays do not match: [%dx%d]", width, height), data1, data2, 1); }
private void floatCompareRollingMedian3x3AndRollingMedianNxN(MedianFilter filter, int width, int height) throws ArrayComparisonFailure { rand = new gdsc.core.utils.Random(-30051977); float[] data1 = floatCreateData(width, height); float[] data2 = floatClone(data1); filter.rollingMedian3x3(data1, width, height); filter.rollingMedianNxN(data2, width, height, 1); floatArrayEquals(String.format("Arrays do not match: [%dx%d]", width, height), data1, data2, 1); }
private void weightedMeanIsCorrect(float[] data, float[] w, int width, int height, float boxSize, boolean internal, BlockMeanDataFilter filter) throws ArrayComparisonFailure { float[] data1 = data.clone(); float[] data2 = data.clone(); weightedMean(data1, w, width, height, boxSize); //// Check the weights do not alter the image mean //double u1 = Maths.sum(data) / data.length; //double u2 = Maths.sum(data1) / data.length; //System.out.printf("[%dx%d] @ %.1f : %g => %g (%g)\n", width, height, boxSize, u1, u2, // DoubleEquality.relativeError(u1, u2)); if (internal) { filter.filterInternal(data2, width, height, boxSize); floatArrayEquals(String.format("Internal arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, boxSize); } else { filter.filter(data2, width, height, boxSize); floatArrayEquals(String.format("Arrays do not match: [%dx%d] @ %.1f", width, height, boxSize), data1, data2, width, height, 0); } }