Reputation: 232
I am learning data structure so I made a Stack class via template and use gtest to test it.
While in the 2 ways using EXPECT_EQ macro it shows difference testing result.
stack->pop
method as long as I totally clear the stack and there are more than one elements in the stack.
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.
}
And the exception shows that:
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
Upvotes: -1
Views: 1126
Reputation: 1704
Try the following for your push method:
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;
}
When you add an integer x
to a pointer of type T*
, it does not literally increase the value of the pointer by x
. It moves the pointer x
"slots to the right", where one "slot" is the space occupied by one T
.
So when you write top = base + stack_size;
then the actual value of top
is base + (stack_size * sizeof(T))
.
For more information, Google "pointer arithmetic" or see this reference
Upvotes: 1