实现我自己的堆栈时,测试用例将不起作用
Test cases won't work when implementing my own stack
所以我们的作业是在我们的 won 上实现一个堆栈,然后为它编写测试用例。
这是堆栈:
import java.util.Vector;
class Stack<T> extends Vector<T> {
private Vector<T> stack;
Stack() {
stack = new Vector<T>();
}
// returns false or true, given the stack is empty or not.
public boolean isEmpty() {
return stack.size() == 0;
}
//returns the top element of the stack without removing it.
public T peek() {
return stack.get(stack.size()-1);
}
//puts a new element to the top of the stack
public void push(T element) {
stack.add(element);
}
//returns and removes the top element of the stack
public T pop() {
return stack.get(stack.size()-1);
}
}
这是我目前的测试class。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StackTest {
@Test
void isEmpty() {
stack s = new stack<Integer>;
assertEquals(true, s.isEmpty());
}
@Test
void peek() {
Stack t = new Stack(1);
}
@Test
void push() {
}
@Test
void pop() {
}
}
我真的很难弄清楚前两种测试方法有什么问题。还有其他人有想法吗?
这是错误的:
//returns and removes the top element of the stack
public T pop() {
return stack.get(stack.size()-1);
}
您不删除元素,使用 remove 而不是 get
其他错误:
void isEmpty() {
//Typo errors here, s is uppercase and missing parenthesis
stack s = new stack<Integer>;
assertEquals(true, s.isEmpty());
}
@Test
void peek() {
//What does Stack(1) mean? There is no such constructor in class
Stack t = new Stack(1);
}
另外:
//If you already extend Vector you don't need a Vector field for the data
class Stack<T> extends Vector<T> {
private Vector<T> stack;
所以我们的作业是在我们的 won 上实现一个堆栈,然后为它编写测试用例。 这是堆栈:
import java.util.Vector;
class Stack<T> extends Vector<T> {
private Vector<T> stack;
Stack() {
stack = new Vector<T>();
}
// returns false or true, given the stack is empty or not.
public boolean isEmpty() {
return stack.size() == 0;
}
//returns the top element of the stack without removing it.
public T peek() {
return stack.get(stack.size()-1);
}
//puts a new element to the top of the stack
public void push(T element) {
stack.add(element);
}
//returns and removes the top element of the stack
public T pop() {
return stack.get(stack.size()-1);
}
}
这是我目前的测试class。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class StackTest {
@Test
void isEmpty() {
stack s = new stack<Integer>;
assertEquals(true, s.isEmpty());
}
@Test
void peek() {
Stack t = new Stack(1);
}
@Test
void push() {
}
@Test
void pop() {
}
}
我真的很难弄清楚前两种测试方法有什么问题。还有其他人有想法吗?
这是错误的:
//returns and removes the top element of the stack
public T pop() {
return stack.get(stack.size()-1);
}
您不删除元素,使用 remove 而不是 get
其他错误:
void isEmpty() {
//Typo errors here, s is uppercase and missing parenthesis
stack s = new stack<Integer>;
assertEquals(true, s.isEmpty());
}
@Test
void peek() {
//What does Stack(1) mean? There is no such constructor in class
Stack t = new Stack(1);
}
另外:
//If you already extend Vector you don't need a Vector field for the data
class Stack<T> extends Vector<T> {
private Vector<T> stack;