|
1 | 1 | package com.fishercoder;
|
2 | 2 |
|
3 | 3 | import com.fishercoder.solutions._49;
|
| 4 | + |
4 | 5 | import java.util.ArrayList;
|
5 | 6 | import java.util.Arrays;
|
6 | 7 | import java.util.List;
|
| 8 | + |
| 9 | +import org.apache.commons.collections4.CollectionUtils; |
7 | 10 | import org.junit.BeforeClass;
|
8 | 11 | import org.junit.Test;
|
9 | 12 |
|
10 | 13 | import static junit.framework.TestCase.assertEquals;
|
| 14 | +import static junit.framework.TestCase.assertTrue; |
11 | 15 |
|
12 | 16 | public class _49Test {
|
13 |
| - private static _49.Solution1 solution1; |
14 |
| - private static String[] words; |
15 |
| - private static List<List<String>> expected; |
16 |
| - private static List<List<String>> actual; |
| 17 | + private static _49.Solution1 solution1; |
| 18 | + private static String[] words; |
| 19 | + private static List<List<String>> expected; |
| 20 | + private static List<List<String>> actual; |
17 | 21 |
|
18 |
| - @BeforeClass |
19 |
| - public static void setup() { |
20 |
| - solution1 = new _49.Solution1(); |
21 |
| - } |
| 22 | + @BeforeClass |
| 23 | + public static void setup() { |
| 24 | + solution1 = new _49.Solution1(); |
| 25 | + } |
22 | 26 |
|
23 |
| - @Test |
24 |
| - public void test1() { |
25 |
| - words = new String[] {"eat", "tea", "tan", "ate", "nat", "bat"}; |
26 |
| - expected = new ArrayList<>(); |
27 |
| - expected.add(Arrays.asList("ate", "eat", "tea")); |
28 |
| - expected.add(Arrays.asList("nat", "tan")); |
29 |
| - expected.add(Arrays.asList("bat")); |
30 |
| - actual = solution1.groupAnagrams(words); |
31 |
| - assertEquals(expected.size(), actual.size()); |
32 |
| - assertEquals(expected.containsAll(actual), actual.containsAll(expected)); |
33 |
| - } |
| 27 | + @Test |
| 28 | + public void test1() { |
| 29 | + words = new String[]{"eat", "tea", "tan", "ate", "nat", "bat"}; |
| 30 | + List<String> e1 = Arrays.asList("bat"); |
| 31 | + List<String> e2 = Arrays.asList("tan", "nat"); |
| 32 | + List<String> e3 = Arrays.asList("ate", "eat", "tea"); |
| 33 | + expected = Arrays.asList(e1, e2, e3); |
| 34 | + actual = solution1.groupAnagrams(words); |
| 35 | + assertEquals(expected.size(), actual.size()); |
| 36 | + assertEquals(expected.containsAll(actual), actual.containsAll(expected)); |
| 37 | + for (List<String> a : actual) { |
| 38 | + switch (a.size()) { |
| 39 | + case 1: |
| 40 | + assertTrue(CollectionUtils.isEqualCollection(e1, a)); |
| 41 | + break; |
| 42 | + case 2: |
| 43 | + assertTrue(CollectionUtils.isEqualCollection(e2, a)); |
| 44 | + break; |
| 45 | + case 3: |
| 46 | + assertTrue(CollectionUtils.isEqualCollection(e3, a)); |
| 47 | + break; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
34 | 51 | }
|
0 commit comments