lundi 3 octobre 2016

Test case failing

I'm stumped because my test code is failing even tho the comparison is the same.

It uses my UniqueVector.cpp as an underlying data structure in my Classroom.cpp

UniqueVector.cpp

//Default constructor
template<typename T>
UniqueVector<T>::UniqueVector() {
//Didn't use initializer syntax because caused warnings
arrayCapacity = 3;
arraySize = 0;
array = new T[arrayCapacity];
}

template<typename T>
void UniqueVector<T>::doubleCapacity(){
    //Create temp array with double the capacity
    //delete array in heap and points array to temp
    arrayCapacity*=2;
    T* temp = new T[arrayCapacity];
    for(unsigned int index=0; index<arraySize; index++){
        temp[index] = array[index];
    }
    delete [] array;
    array = temp;
}
//Returns capacity of the array
template<typename T>
unsigned int UniqueVector<T>::capacity(){

return arrayCapacity;
}
//returns size of the array
template<typename T>
unsigned int UniqueVector<T>::size() const{

return arraySize;
}
//Checks if size is 0
template<typename T>
bool UniqueVector<T>::empty(){

if(arraySize==0){
    return true;
}

return false;
}

template<typename T>
bool UniqueVector<T>::contains(const T& data){
//loops through array to check if the index has element data
for(unsigned int index=0; index < arraySize; index++){
    if(array[index] == data){
        return true;
    }
}

return false;
}

template<typename T>
bool UniqueVector<T>::at(unsigned int pos, T& data){
//If position is less than the size of the array
//stores the element of the array position into data
if(pos < arraySize){
    data = array[pos];
    return true;
}

return false;
}

template<typename T>
bool UniqueVector<T>::insert(const T& data){
//insert data to the first pos
if(arraySize==0){
    arraySize++;
    array[0] = data;
    return true;
}   
//return false if index contains data
for(unsigned int index=0; index < arraySize; index++){
    if(array[index] == data){
        return false;
    }
}
//if size of array is equal to the capacity size, double capacity size
if(arraySize == arrayCapacity){
    doubleCapacity();
}
//The last index of the array now contains data
array[arraySize] = data;
arraySize++;
return true;
}

template<typename T>
bool UniqueVector<T>::insert(const T& data, unsigned int pos){

//Checks if the position is valid
if(pos>arraySize){
    return false;
}
//insert data into the 1st position
if(arraySize==0){
    arraySize++;
    array[0] = data;
    return true;
}
//return false if data is in array
for(unsigned int index=0; index < arraySize; index++){
    if(array[index] == data){
        return false;
    }
}
//if size of array is equal to the capacity size, double capacity size
if(arraySize == arrayCapacity)
    doubleCapacity();
//Shifts the element of the index right by 1
arraySize++;  
for(unsigned int index=arraySize-1; index>pos; index=index-1){
            array[index] = array[index-1];
        }

array[pos] = data;
return true;
}

template<typename T>
bool UniqueVector<T>::remove(const T& data){
//Makes sure the array size is not 0
if(arraySize==0){
    return false;
}
if(arraySize==1){
    arraySize--;
    return true;
}
//check if the element of index is the same as data, then shift
//elements starting from index to are shifted left by 1 and decrease size
for(unsigned int index=0; index < arraySize; index++){
    if(array[index] == data){
        //Shifts element of the array to the right
        for(unsigned int index2=index; index2<arraySize-1; index2++)
            array[index2] = array[index2+1];
        arraySize--;
        return true;
        }
}

return false;
}

template<typename T>
bool UniqueVector<T>::remove(unsigned int pos, T& data){
//if position is greater than size return false
if(pos>arraySize){
    return false;
}
//If size of array is 0 return false
if(arraySize==0){
    return false;
}

if(arraySize==1){
    arraySize--;
    return true;
}
//the element in position is stored in data
data = array[pos];
    //overwrite element of the array by shifting by 1,
    // where position is the first index to be overwritten
for(unsigned int index=pos; index<arraySize-1; index++){
    array[index] = array[index+1];  
}
arraySize--;
return true;
}

template<typename T2>
bool UniqueVector<T2>::operator==( const UniqueVector<T2> &rhs) const{
//If the array size is equivalent to the other vector's array size
//return true
if(arraySize == rhs.size()){
    return true;
}
return false;
}

#endif 

UniqueVector.h

#ifndef UNIQUE_VECTOR_H
#define UNIQUE_VECTOR_H

template<typename T>
class UniqueVector{

public:

    UniqueVector(); 
    ~UniqueVector(){
        delete [] array;
    }; 
    unsigned int capacity();
    unsigned int size() const;
    bool empty();
    bool contains(const T& data);
    bool at(unsigned int pos, T& data);
    bool insert(const T& data);
    bool insert(const T& data, unsigned int pos);
    bool push_front(const T& data);
    bool remove(const T& data);
    bool remove(unsigned int pos, T& data);
    bool pop_back(T& data);
    void clear();
    bool operator==(const UniqueVector &rhs) const;
    void doubleCapacity();

private:
    T *array; // dynamically allocated array
    int arraySize; //The dynamic created array is initially size 3
    int arrayCapacity; //The capacity of the dynamic array  
};

#include "UniqueVector.cpp"
#endif

Classroom.h

#ifndef CLASSROOM_H
#define CLASSROOM_H
#include "UniqueVector.h"

class Classroom{

public:
    Classroom(){};
    bool addStudent(const string& name);
    bool removeStudent(const string& name);
    bool containsStudent(const string& name);
    string listAllStudents();

private:
    UniqueVector<string>classRoster;
};

#endif 

Classroom.cpp

bool Classroom::removeStudent(const string& name){

return classRoster.remove(name);        
}

bool Classroom::containsStudent(const string& name){

return classRoster.contains(name);
}

string Classroom::listAllStudents(){

string studentName = "";

for(int index=0; index < classRoster.size(); index++){
    studentName += classRoster.at(index,studentName);
        if(index!=classRoster.size()-1){
            studentName += ",";
        }
    }

return studentName;
}

#endif 

My test case fails the clasroom tester

ClassroomTester.cpp

TEST_CASE("adding/removing/containment of students preserves uniqueness"," [student]"){
Classroom myClass;
REQUIRE( myClass.listAllStudents() == "" );
// Vector contains student after being added
REQUIRE( myClass.addStudent("Simon") == true );
REQUIRE( myClass.containsStudent("Simon") == true );
REQUIRE( myClass.listAllStudents() == "Simon" ); //This line fails 

My listAllStudent function prints out

"Simon" == "Simon" 

test fail Can anyone see what's wrong?

Aucun commentaire:

Enregistrer un commentaire