Skip to content

Commit

Permalink
增加测试用例,修改getTop方法
Browse files Browse the repository at this point in the history
  • Loading branch information
miniyk2012 committed Apr 9, 2017
1 parent d273b09 commit 15447d7
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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()<len && !s.isEmpty()) {
Expand All @@ -62,8 +62,11 @@ public static Object[] getTop(Stack s, int len) {
Object[] o = new Object[actualLen];
int i = actualLen;
while (!tempS.isEmpty()) {
o[--i] = tempS.pop();
Object temp = tempS.pop();
o[--i] = temp;
s.push(temp);
}

return o;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,74 +2,85 @@

import org.junit.Test;
import org.junit.Before;
import org.junit.After;
import org.junit.After;
import static junit.framework.TestCase.assertEquals;
import static junit.framework.TestCase.assertNull;
import static org.junit.Assert.assertArrayEquals;

/**
* StackUtil Tester.
*
* @author <Authors name>
* @since <pre>Apr 6, 2017</pre>
* @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));
}

}
}

0 comments on commit 15447d7

Please sign in to comment.