我正在学习数据结构,因此我通过模板制作了一个Stack类,并使用gtest对其进行了测试。
使用EXPECT_EQ宏以两种方式显示差异测试结果。
stack->pop
方法中发生。
TEST(Stack,isOK){
auto stack = new Stack<int>();
stack->Push(123);
stack->Push(123);
stack->Push(123);
auto ret = stack->Pop();
EXPECT_EQ(ret, 123);
EXPECT_EQ(stack->Pop(), 123);
EXPECT_EQ(stack->Pop(), 123); // this line.
}
该异常表明:
Running main() from D:\Homework\DataStructure\C++\Test\lib\googletest-master\googletest\src\gtest_main.cc
Running 1 test from 1 test suite.D:\Homework\DataStructure\C++\Test\tests\StackTest.cpp:15: Failure
Expected equality of these values:
stack->Pop()
Which is: -2147483136
123
1 test from 1 test suite ran. (1 ms total)
TEST(Stack,isOK){
auto stack = new Stack<int>();
stack->Push(123);
stack->Push(123);
stack->Push(123);
auto ret = stack->Pop();
EXPECT_EQ(ret, 123);
EXPECT_EQ(stack->Pop(), 123);
ret = stack->Pop();
EXPECT_EQ(ret, 123); // ALL IS OK.
}
Stack.h
//
// Created by anon on 2019/4/15.
//
#ifndef DATA_STRUCTURE_STACK_H
#define DATA_STRUCTURE_STACK_H
#include "Definition.h"
template<class T>
class Stack {
const int stack_init_size = 1;
const int stack_increment = 1;
protected:
T *base;
T *top;
int stack_size;
public:
Stack();
~Stack();
Status Clear();
bool empty();
int length();
T GetTop();
Status Push(T e);
T Pop();
};
template<class T>
Stack<T>::Stack() {
stack_size = stack_init_size;
base = (T *)malloc(sizeof(T) * stack_size);
top = base;
}
template<class T>
Status Stack<T>::Clear() {
return 0;
}
template<class T>
bool Stack<T>::empty() {
return false;
}
template<class T>
int Stack<T>::length() {
if (top == nullptr) {
return 0;
}
else{
auto val = (top - base);
return val;
}
}
template<class T>
Status Stack<T>::Push(T e) {
if (length() == stack_size){
auto old_base = base;
base = (T *)realloc(base,(stack_size + stack_increment)* sizeof(T));
if (!base) exit(ERROR);
if (base != old_base){
top = base + (stack_size * sizeof(T));
}
stack_size += stack_increment;
}
*top = e;
top++;
return OK;
}
template<class T>
T Stack<T>::Pop() {
if (length() == 0)
exit(ERROR);
auto ret = *(top-1);
top--;
return ret;
}
template<class T>
Stack<T>::~Stack() {
delete base;
}
template<class T>
T Stack<T>::GetTop() {
if (length() == 0)
exit(ERROR);
return *(top-1);
}
#endif //DATA_STRUCTURE_STACK_H
答案 0 :(得分:1)
尝试以下方法作为推送方法:
template<class T>
Status Stack<T>::Push(T e) {
if (length() == stack_size) {
stack_size += stack_increment;
base = (T *)realloc(base, stack_size * sizeof(T));
if (!base) std::exit(ERROR);
top = base + stack_size;
}
*top++ = e;
return OK;
}
当您将整数x
添加到类型T*
的指针时,它实际上不会将指针的值增加x
。它将指针x
“向右移动插槽”,其中一个“插槽”是一个T
所占用的空间。
因此,当您写top = base + stack_size;
时,top
的实际值为base + (stack_size * sizeof(T))
。
有关更多信息,请使用Google“指针算术”或参阅this reference