diff --git a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/stack/StackUtil.java b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/stack/StackUtil.java index 3b3dc501c7..c4e253d92b 100644 --- a/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/stack/StackUtil.java +++ b/group02/812350401/src/main/java/com/github/miniyk2012/coding2017/basic/stack/StackUtil.java @@ -52,7 +52,7 @@ public static void remove(Stack s,Object o) { */ public static Object[] getTop(Stack s, int len) { if (len < 0) { - throw new IndexOutOfBoundsException(); + return null; } Stack tempS = new Stack(); while (tempS.size() * @since
Apr 6, 2017
* @version 1.0 -*/ -public class StackUtilTest { +*/ +public class StackUtilTest { -@Before -public void before() throws Exception { -} + private StackUtil sk; + @Before + public void setUp() throws Exception { + } -@After -public void after() throws Exception { -} + @After + public void tearDown() throws Exception { + } -/** -* -* Method: reverse(Stack s) -* -*/ -@Test -public void testReverse() throws Exception { -//TODO: Test goes here... -} - -/** -* -* Method: remove(Stack s, Object o) -* -*/ -@Test -public void testRemove() throws Exception { -//TODO: Test goes here... -} - -/** -* -* Method: getTop(Stack s, int len) -* -*/ -@Test -public void testGetTop() throws Exception { -//TODO: Test goes here... -} + @Test + public void testReverse() { + sk = new StackUtil(); + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + sk.reverse(stack); + assertEquals(1, stack.pop()); + assertEquals(2, stack.pop()); + assertEquals(3, stack.pop()); + } + @Test + public void testRemove() { + sk = new StackUtil(); + Stack stack = new Stack(); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(2); + stack.push(5); + sk.remove(stack, 2); + assertEquals(5, stack.pop()); + assertEquals(3, stack.pop()); + assertEquals(2, stack.pop()); + assertEquals(1, stack.pop()); + } + @Test + public void testGetTop() { + sk = new StackUtil(); + Stack stack = new Stack(); -/** -* -* Method: isValidPairs(String s) -* -*/ -@Test -public void testIsValidPairs() throws Exception { -//TODO: Test goes here... -} - -/** -* -* Method: main(String[] args) -* -*/ -@Test -public void testMain() throws Exception { -//TODO: Test goes here... -} + Object[] array = sk.getTop(stack, 3); + assertArrayEquals(array, new Object[0]); + stack.push(1); + stack.push(2); + stack.push(3); + stack.push(2); + stack.push(5); + array = sk.getTop(stack, 3); + assertArrayEquals(array, new Object[] {5, 2, 3}); + array = sk.getTop(stack, 6); + assertArrayEquals(new Object[] {5, 2, 3, 2, 1}, array); + array = sk.getTop(stack, -1); + assertNull(array); + } + @Test + public void testIsValidPairs() { + sk = new StackUtil(); + String expr = ""; + assertEquals(true, sk.isValidPairs(expr)); + expr = "{xx[])yyza]}"; + assertEquals(false, sk.isValidPairs(expr)); + expr = "asd{[(asds)]sx}"; + assertEquals(true, sk.isValidPairs(expr)); + } -} +}