assertion Failed on testing

I am doing a HW for my data structure class. I got the following errors.

!!!FAILURES!!!
Test Results:
Run: 2 Failures: 2 Errors: 0
1) test: Test_array::test_push_back (F) line: 16 test_array.cpp
assertion failed
- Expression: a[0] == 3
2) test: Test_array::test_pop_back (F) line: 51 test_array.cpp
assertion failed
- Expression: false

The relevant portion of the code is located below. I am not sure what else to do. I am searching all engines and reading the text, no luck solving the problem. Could any one help


CPPUNIT_TEST_SUITE_REGISTRATION (Test_array);

void Test_array::test_push_back ()
{
Array<int> a;
cout << endl << "Push one item into the bag" << endl;
a.push_back (3);
CPPUNIT_ASSERT (a[0] == 3);
CPPUNIT_ASSERT (a.size () == 1);
CPPUNIT_ASSERT (a.max_size () == TEST_MAX);

cout << "Push multiple items into the bag" << endl;
for (int i = a.size (); i < TEST_MAX; i++)
{
a.push_back (i);
CPPUNIT_ASSERT (a[i] == i);
CPPUNIT_ASSERT (a.size () == i+1);
CPPUNIT_ASSERT (a.max_size () == TEST_MAX);
}

cout << "Push overflow exception check" << endl;
try
{
a.push_back (50);
CPPUNIT_ASSERT (false);
}
catch (Exception & e)

{
CPPUNIT_ASSERT (CONTAINER_FULL == e.error_code ());
}
}



void Test_array::test_pop_back ()
{
Array<int> a;

try
{
a.pop_back();
CPPUNIT_ASSERT(false);
}
catch(Exception & e)
{
CPPUNIT_ASSERT(CONTAINER_EMPTY == e.error_code ());
}


}
If this code is to test your implementation of the Array<> template, then your
implementation is wrong. But you didn't post that.
How can I fix that? what code do i need....
I have searching for examples to to figure it out but no such luck
Well, we'd need to know the implementation of your Array<> template.

-Albatross
#ifndef ARRAY_H
#define ARRAY_H

#include "exception.h"

template <class generic>
class Array
{
public:
Array ();
void push_back (generic);
void pop_back ();
void clear ();
generic& operator[] (unsigned int);
generic& operator[] (unsigned int) const;
unsigned int size () const;
unsigned int max_size () const;

private:
unsigned int m_size;
unsigned int m_max_size;
generic m_data[20];
};

#include "array.hpp"
#endif
Erm... that's the declaration...

-Albatross
I am sorry i really do not know what I am doing I am totally lost...
Well, see, what you gave is the declaration. What we want to know is what is the code behind each and every one of those member functions and operators.

-Albatross
template <class generic>
Array<generic>::Array () : m_size (0), m_max_size (20)
{
}

template <class generic>
void Array<generic>::push_back (generic x)
{
if(m_size >= m_max_size)
{
throw exception
(
CONTAINER_FULL;
"You have reached the LAST positionin the ARRAY sorry!!!"
)
}
else
{
m_data[m_size] = x;
m_size++;
}
}

template <class generic>
void Array<generic>::pop_back ()
{
if(m_size < 1);

{
throw exception
(
CONTAINER_EMPTY;
"The array is empty. There are no other elements to remove."
);
}
else
{
m_size--;
}
}

template <class generic>
void Array<generic>::clear ()
{
for(int i = m_size; i > 0; i --)
{
pop_back();
}
}

template <class generic>
generic& Array<generic>::operator[] (unsigned int x)
{

if(x < 0 || x > m_size);
{
throw exception
(
OUT_OF_BOUNDS;
"The position is out of the array bounds"
);
}
return m_data[x];

}

template <class generic>
generic& Array<generic>::operator[] (unsigned int x) const
{
if(x >= m_size || x < 0);
{
throw exception
(
OUT_OF_BOUNDS;
"The position is out of the array bounds"
);
}
return m_data[x];

}

template <class generic>
unsigned int Array<generic>::size () const
{
return m_size;
}

template <class generic>
unsigned int Array<generic>::max_size () const
{
return m_max_size;
}
any one else can assist me I been searching and still no good example to follow
It is hard to follow your code since it is not indented properly, but the first problem I
see is that the correct syntax for an if statement (or if-else) is:

1
2
3
4
5
6
7
8
if( boolean-condition )   /* NO SEMICOLON HERE!!! */
{
     // if-clause
}
else
{
    // else-clause
}


You have this error several places.
thanks
Topic archived. No new replies allowed.