import static org.junit.Assert.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; public class Test2DTest { @Rule public ExpectedException exception = ExpectedException.none(); @Test public void testLast() { final String[][] S = { {"a"}, {"a", "b"}, {"a", "b", "c"}, {"a", "b", "c", "e", "f", "xyz"}, }; for (int i = 0; i < S.length; i++) { String[] strings = S[i]; List t = Arrays.asList(strings); assertEquals(strings[strings.length - 1], Test2D.last(t)); } } @Test public void testSecondLast() { final String[][] S = { {"a", "b"}, {"a", "b", "c"}, {"a", "b", "c", "e", "f", "xyz"}, }; for (int i = 0; i < S.length; i++) { String[] strings = S[i]; List t = Arrays.asList(strings); assertEquals(strings[strings.length - 2], Test2D.secondLast(t)); } } @Test public void testSecondLastThrows1() { exception.expect(IllegalArgumentException.class); Test2D.secondLast(new ArrayList()); } @Test public void testSecondLastThrows2() { exception.expect(IllegalArgumentException.class); List t = new ArrayList(); t.add("hello"); Test2D.secondLast(t); } @Test public void testConcat() { final String[][] S = { {}, {"a"}, {"a", "b"}, {"a", "b", "c"}, {"a", "b", "c", "e", "f", "xyz"} }; final String[] EXP = {"", "a", "ab", "abc", "abcefxyz"}; for (int i = 0; i < S.length; i++) { String[] strings = S[i]; List t = Arrays.asList(strings); assertEquals(EXP[i], Test2D.concat(t)); } } @Test public void testSortByLength() { final String[][] S = { {}, {"a"}, {"a", "also"}, {"also", "a"}, {"bumblebee", "wizard", "been", "bat"}, {"cold", "coat", "cat", "albatross", "cot", "cow" } }; final String[][] EXP = { {}, {"a"}, {"a", "also"}, {"a", "also"}, {"bat", "been", "wizard", "bumblebee"}, {"cat", "cot", "cow", "coat", "cold", "albatross"} }; for (int i = 0; i < S.length; i++) { List t = new ArrayList(Arrays.asList(S[i])); Test2D.sortByLength(t); List exp = Arrays.asList(EXP[i]); assertEquals(exp, t); } } }