@Test public void testCompareBytes() { byte[] raw1 = new byte[] {1, 2, 3}; byte[] raw2 = new byte[] {1, 2, 2}; byte[] raw3 = new byte[] {1, 2}; Formatter f; assertThat(Misc.compare(raw1, null, null)).isFalse(); f = new Formatter(); assertThat(Misc.compare(raw1, raw1, f)).isTrue(); assertThat(f.toString()).isEmpty(); f = new Formatter(); assertThat(Misc.compare(raw1, raw2, f)).isFalse(); assertThat(f.toString()) .isEqualTo(String.format(" 2 : 3 != 2%n" + "Misc.compare 3 bytes, 1 are different%n")); f = new Formatter(); assertThat(Misc.compare(raw1, raw3, f)).isFalse(); assertThat(f.toString()).isEqualTo(String.format("length 1= 3 != length 2= 2%n")); }
public static boolean compare(byte[] raw1, byte[] raw2, Formatter f) { if (raw1 == null || raw2 == null) return false; if (raw1.length != raw2.length) { f.format("length 1= %3d != length 2=%3d%n", raw1.length, raw2.length); } int len = Math.min(raw1.length, raw2.length); int ndiff = 0; for (int i = 0; i < len; i++) { if (raw1[i] != raw2[i]) { f.format(" %3d : %3d != %3d%n", i, raw1[i], raw2[i]); ndiff++; } } if (ndiff > 0) f.format("Misc.compare %d bytes, %d are different%n", len, ndiff); return ndiff == 0 && (raw1.length == raw2.length); }
Misc { public static boolean compare(byte[] raw1, byte[] raw2, Formatter f) { if (raw1 == null || raw2 == null) return false; if (raw1.length != raw2.length) { f.format("length 1= %3d != length 2=%3d%n", raw1.length, raw2.length); } int len = Math.min(raw1.length, raw2.length); int ndiff = 0; for (int i = 0; i < len; i++) { if (raw1[i] != raw2[i]) { f.format(" %3d : %3d != %3d%n", i, raw1[i], raw2[i]); ndiff++; } } if (ndiff > 0) f.format("Misc.compare %d bytes, %d are different%n", len, ndiff); return ndiff == 0 && (raw1.length == raw2.length); } }
Misc { public static boolean compare(byte[] raw1, byte[] raw2, Formatter f) { if (raw1 == null || raw2 == null) return false; if (raw1.length != raw2.length) { f.format("length 1= %3d != length 2=%3d%n", raw1.length, raw2.length); } int len = Math.min(raw1.length, raw2.length); int ndiff = 0; for (int i = 0; i < len; i++) { if (raw1[i] != raw2[i]) { f.format(" %3d : %3d != %3d%n", i, raw1[i], raw2[i]); ndiff++; } } if (ndiff > 0) f.format("Misc.compare %d bytes, %d are different%n", len, ndiff); return ndiff == 0 && (raw1.length == raw2.length); }  }
Misc { public static boolean compare(byte[] raw1, byte[] raw2, Formatter f) { if (raw1 == null || raw2 == null) return false; if (raw1.length != raw2.length) { f.format("length 1= %3d != length 2=%3d%n", raw1.length, raw2.length); } int len = Math.min(raw1.length, raw2.length); int ndiff = 0; for (int i = 0; i < len; i++) { if (raw1[i] != raw2[i]) { f.format(" %3d : %3d != %3d%n", i, raw1[i], raw2[i]); ndiff++; } } if (ndiff > 0) f.format("Misc.compare %d bytes, %d are different%n", len, ndiff); return ndiff == 0 && (raw1.length == raw2.length); }  static float absoluteDifference(float a, float b); static double absoluteDifference(double a, double b); static float relativeDifference(float a, float b); static double relativeDifference(double a, double b); static boolean nearlyEquals(float a, float b); static boolean nearlyEquals(float a, float b, float maxRelDiff); static boolean nearlyEquals(double a, double b); static boolean nearlyEquals(double a, double b, double maxRelDiff); static boolean nearlyEqualsAbs(float a, float b, float maxAbsDiff); static boolean nearlyEqualsAbs(double a, double b, double maxAbsDiff); static String showBits(byte[] bytes); static boolean compare(byte[] raw1, byte[] raw2, Formatter f); static boolean compare(float[] raw1, float[] raw2, Formatter f); static boolean compare(int[] raw1, int[] raw2, Formatter f); }
Misc { public static boolean compare(byte[] raw1, byte[] raw2, Formatter f) { if (raw1 == null || raw2 == null) return false; if (raw1.length != raw2.length) { f.format("length 1= %3d != length 2=%3d%n", raw1.length, raw2.length); } int len = Math.min(raw1.length, raw2.length); int ndiff = 0; for (int i = 0; i < len; i++) { if (raw1[i] != raw2[i]) { f.format(" %3d : %3d != %3d%n", i, raw1[i], raw2[i]); ndiff++; } } if (ndiff > 0) f.format("Misc.compare %d bytes, %d are different%n", len, ndiff); return ndiff == 0 && (raw1.length == raw2.length); }  static float absoluteDifference(float a, float b); static double absoluteDifference(double a, double b); static float relativeDifference(float a, float b); static double relativeDifference(double a, double b); static boolean nearlyEquals(float a, float b); static boolean nearlyEquals(float a, float b, float maxRelDiff); static boolean nearlyEquals(double a, double b); static boolean nearlyEquals(double a, double b, double maxRelDiff); static boolean nearlyEqualsAbs(float a, float b, float maxAbsDiff); static boolean nearlyEqualsAbs(double a, double b, double maxAbsDiff); static String showBits(byte[] bytes); static boolean compare(byte[] raw1, byte[] raw2, Formatter f); static boolean compare(float[] raw1, float[] raw2, Formatter f); static boolean compare(int[] raw1, int[] raw2, Formatter f); static final int referenceSize; static final int objectSize; static final float defaultMaxRelativeDiffFloat; static final double defaultMaxRelativeDiffDouble; }