|
| 1 | +/** |
| 2 | + *************************************************************************** |
| 3 | + * Some supplements to assertion in Junit Test |
| 4 | + * |
| 5 | + *************************************************************************** |
| 6 | + */ |
| 7 | +packagecom.lintcode; |
| 8 | + |
| 9 | +importjava.util.ArrayList; |
| 10 | +importjava.util.Arrays; |
| 11 | +importjava.util.Collections; |
| 12 | +importjava.util.List; |
| 13 | + |
| 14 | +importstaticorg.junit.Assert.*; |
| 15 | + |
| 16 | +publicclassTest { |
| 17 | + |
| 18 | +/** |
| 19 | + * Asserts two lists are equals ignoring order of elements in list |
| 20 | + * |
| 21 | + * @param actual actual list value |
| 22 | + * @param expected expected list value |
| 23 | + */ |
| 24 | +publicstatic <T>voidassertEqualsIgnoreOrder(List<T>expected, |
| 25 | +List<T>actual) { |
| 26 | +List<String>act =toStringList(actual); |
| 27 | +List<String>exp =toStringList(expected); |
| 28 | +Collections.sort(act); |
| 29 | +Collections.sort(exp); |
| 30 | +assertEquals(exp,act); |
| 31 | + } |
| 32 | + |
| 33 | +// convert to a list of string in order to compare |
| 34 | +privatestatic <T>List<String>toStringList(List<T>list) { |
| 35 | +List<String>result =newArrayList<>(); |
| 36 | +for (Tl :list) { |
| 37 | +result.add(l.toString()); |
| 38 | + } |
| 39 | +returnresult; |
| 40 | + } |
| 41 | + |
| 42 | +/** |
| 43 | + * Asserts two lists of array are equals ignoring order of elements in list |
| 44 | + * |
| 45 | + * @param expected |
| 46 | + * @param actual |
| 47 | + */ |
| 48 | +publicstatic <T>voidassertArrayEqualsIgnoreOrder(List<T[]>expected, |
| 49 | +List<T[]>actual) { |
| 50 | +List<String>act =arrayToStringList(actual); |
| 51 | +List<String>exp =arrayToStringList(expected); |
| 52 | +Collections.sort(act); |
| 53 | +Collections.sort(exp); |
| 54 | +assertEquals(exp,act); |
| 55 | + } |
| 56 | + |
| 57 | +privatestatic <T>List<String>arrayToStringList(List<T[]>list) { |
| 58 | +List<String>result =newArrayList<>(); |
| 59 | +for (T[]l :list) { |
| 60 | +result.add(Arrays.toString(l)); |
| 61 | + } |
| 62 | +returnresult; |
| 63 | + } |
| 64 | + |
| 65 | +} |