alshabib | 1f44e8e | 2014-08-14 15:19:57 -0700 | [diff] [blame] | 1 | package org.projectfloodlight.test; |
| 2 | |
| 3 | import java.util.ArrayList; |
| 4 | import java.util.List; |
| 5 | |
| 6 | import org.junit.Assert; |
| 7 | |
| 8 | import com.google.common.base.Function; |
| 9 | import com.google.common.base.Joiner; |
| 10 | import com.google.common.collect.Lists; |
| 11 | import com.google.common.primitives.Bytes; |
| 12 | |
| 13 | public class TestUtils { |
| 14 | private TestUtils() {} |
| 15 | |
| 16 | private static final int PER_LINE = 8; |
| 17 | |
| 18 | public static void betterAssertArrayEquals(byte[] expected, byte[] got) { |
| 19 | int maxlen = Math.max(expected.length, got.length); |
| 20 | |
| 21 | List<String> expectedList = formatHex(Bytes.asList(expected)); |
| 22 | List<String> gotList = formatHex(Bytes.asList(got)); |
| 23 | |
| 24 | boolean fail = false; |
| 25 | for (int i = 0; i < maxlen;i+= PER_LINE) { |
| 26 | int maxThisLine = Math.min(maxlen, PER_LINE); |
| 27 | boolean print = false; |
| 28 | |
| 29 | ArrayList<String> changeMarkers = new ArrayList<String>(); |
| 30 | |
| 31 | for (int j = i; j < maxThisLine; j++) { |
| 32 | if (j >= expected.length || j >= got.length || expected[j] != got[j]) { |
| 33 | print = true; |
| 34 | fail = true; |
| 35 | changeMarkers.add("=="); |
| 36 | break; |
| 37 | } else { |
| 38 | changeMarkers.add(" "); |
| 39 | } |
| 40 | } |
| 41 | if(print) { |
| 42 | System.out.println(String.format("%4x: %s", i, Joiner.on(" ").join(expectedList.subList(i, Math.min(expectedList.size(), i+PER_LINE))))); |
| 43 | System.out.println(String.format("%4x: %s", i, Joiner.on(" ").join(gotList.subList(i, Math.min(gotList.size(), i+PER_LINE))))); |
| 44 | System.out.println(String.format("%4s %s", "", Joiner.on(" ").join(changeMarkers))); |
| 45 | System.out.println("\n"); |
| 46 | } |
| 47 | } |
| 48 | if(fail) { |
| 49 | Assert.fail("Array comparison failed"); |
| 50 | } |
| 51 | |
| 52 | } |
| 53 | |
| 54 | private static List<String> formatHex(List<Byte> b) { |
| 55 | return Lists.transform(b, new Function<Byte, String>() { |
| 56 | @Override |
| 57 | public String apply(Byte input) { |
| 58 | return String.format("%02x", input); |
| 59 | } |
| 60 | }); |
| 61 | } |
| 62 | } |