GPOS_RESULT CStackTest::EresUnittest_PushPop() { // create memory pool CAutoMemoryPool amp; IMemoryPool *pmp = amp.Pmp(); ULONG rgul[4] = {1,2,3,4}; CStack<ULONG> *pstk = GPOS_NEW(pmp) CStack<ULONG> (pmp, 4); // scope for auto trace { CAutoTrace trace(pmp); IOstream &oss(trace.Os()); oss << "Pushing " << rgul[0] << " and " << rgul[1] << std::endl; pstk->Push(&rgul[0]); // stack is 1 pstk->Push(&rgul[1]); // stack is 1,2 ULONG ul = *(pstk->Peek()); // ul should be 2 oss << "Top of stack is " << ul << std::endl; GPOS_ASSERT(ul == rgul[1]); oss << "Popping stack" << std::endl; ul = *(pstk->Pop()); //ul should be 2 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[1]); oss << "Pushing " << rgul[2] << std::endl; pstk->Push(&rgul[2]); // stack is 1,3 ul = *(pstk->Peek()); // ul is 3 oss << "Top of stack is " << ul << std::endl; GPOS_ASSERT(ul == rgul[2]); oss << "Popping stack" << std::endl; ul = *(pstk->Pop()); // ul is 3 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[2]); oss << "Popping stack " << std::endl; ul = *(pstk->Pop()); // ul is 1 oss << "Popped element is " << ul << std::endl; GPOS_ASSERT(ul == rgul[0]); } GPOS_DELETE(pstk); return GPOS_OK; }
//--------------------------------------------------------------------------- // @function: // CStackTest::EresUnittest_Pop // // @doc: // This test should assert. // //--------------------------------------------------------------------------- GPOS_RESULT CStackTest::EresUnittest_Pop() { // create memory pool CAutoMemoryPool amp; IMemoryPool *pmp = amp.Pmp(); ULONG rgsz[] = {1, 2, 3, 4}; CStack<ULONG> *pstk = GPOS_NEW(pmp) CStack<ULONG> (pmp, 4); CAutoP<CStack<ULONG> > cAP; cAP = pstk; // add elements incl trigger resize of array for (ULONG i = 0; i < 4; i++) { pstk->Push(&rgsz[i]); GPOS_ASSERT( (* pstk->Peek()) == 4 - i); GPOS_ASSERT(pstk->Pop() == &rgsz[i]); } // now deliberately pop when the stack is empty pstk->Pop(); return GPOS_FAILED; }