Testcase 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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
//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 


Classroom.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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
1
2
3
4
5
6
7
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?
Last edited on
I would look over this part of your code again

1
2
3
4
5
6
for(int index=0; index < classRoster.size(); index++){
    studentName += classRoster.at(index,studentName);
    if(index!=classRoster.size()-1){
        studentName += ",";
    }
}
I can't seem to figure what's wrong. I'm guessing it's classRoster.at(index,StudentName). The index is only reading the first and the last names of the vector.

I have pinpointed that the problem is the line
 
studentName += classRoster.at(index,studentName
Last edited on
I have pinpointed that the problem is the line

That is the problem you already said before in your previous thread.
In a previous thread, I had a SIGABRT error. Now I am able to run my test cases.
Maybe there are two different problems in the same line.
If I read this correctly don't UniqueVector<T>::at return a bool value? Which than you are appending it onto studentName.

I would also check out: http://www.cplusplus.com/reference/set/
Last edited on
I figured it out.

for my code
 
studentName += classRoster.at(index,studentName);


i should've declared a new string something like
 
string studentNameData;


and changed my code to
1
2
classRoster.at(index,studentNameData);
studentName += studentNameData


Since my at function stores StudentNameData from the index. Therefore I increase the string by the data store. Thanks for the heads up @Izink!
Topic archived. No new replies allowed.