#include <iostream>
#include <fstream>
#include "student.h"
#include "teacher.h"
#include "classroom.h"
#include "database.h"
#include "leakdetection.h"
using namespace std;
bool TestStudent();
bool TestTeacher();
bool TestClassroom();
bool TestDataLoad();
bool TestDatabase();
#ifdef MEMORY_LEAK_DETECTION
#define DEBUG_NEW new(__FILE__, __LINE__)
#define new DEBUG_NEW
#endif
int test()
{
cout << "====================================== \n";
cout << "Testing Student \n";
cout << "====================================== \n";
if(!TestStudent())
{
cout << endl << endl;
cout << "Student test failed! Fix those bugs! \n";
return 0;
}
cout << endl << endl;
cout << "====================================== \n";
cout << "Testing Teacher \n";
cout << "====================================== \n";
if(!TestTeacher())
{
cout << endl << endl;
cout << "Teacher test failed! Fix those bugs! \n";
return 0;
}
cout << endl << endl;
cout << "====================================== \n";
cout << "Testing Classroom \n";
cout << "====================================== \n";
if(!TestClassroom())
{
cout << endl << endl;
cout << "Classroom test failed! Fix those bugs! \n";
return 0;
}
cout << "====================================== \n";
cout << "Testing All Data \n";
cout << "====================================== \n";
if(!TestDataLoad())
{
cout << endl << endl;
cout << "All data test failed! Fix those bugs! \n";
return 0;
}
cout << endl << endl;
cout << "====================================== \n";
cout << "Testing Database \n";
cout << "====================================== \n";
if(!TestDatabase())
{
cout << endl << endl;
cout << "Database test failed! Fix those bugs! \n";
return 0;
}
cout << endl << endl;
#ifdef MEMORY_LEAK_DETECTION
cout << "Unfreed : \n";
DumpUnfreed();
#endif
return 0;
}
bool TestDatabase()
{
CDatabase classData;
ifstream inputFile("clf.txt");
if(inputFile.fail())
{
cout << "Failed to open clf.txt! \n";
return false;
}
CClassroom classFileRead;
while(!inputFile.eof())
{
inputFile >> classFileRead;
classData.AddClassroom(&classFileRead);
if(classData.GetClassroom(0) == NULL)
{
cout << "Failed to get class! \n";
return false;
}
inputFile.peek();
}
if(classData.GetNumClasses() != 10)
{
cout << "GetNumClasses returned " << classData.GetNumClasses() << " but that's wrong! \n";
cout << "Error adding the class to the database" << endl;
return false;
}
inputFile.close();
inputFile.open("slf.txt");
if(inputFile.fail())
{
cout << "Failed to open slf.txt! \n";
return false;
}
CStudent tempStudent;
int nNumStudents = 0;
while(!inputFile.eof())
{
inputFile >> tempStudent;
classData.AddStudent(&tempStudent);
nNumStudents++;
}
if(classData.GetNumStudents() != nNumStudents)
{
cout << "Error adding students to database! \n";
return false;
}
for( int i = 0; i < nNumStudents; i++)
{
if(classData.GetStudent(i) == NULL)
{
cout << "Failed to properly retrieve student from database! \n";
return false;
}
}
inputFile.close();
inputFile.open("tlf.txt");
if(inputFile.fail())
{
cout << "Failed to open tlf.txt! \n";
return false;
}
CTeacher tempTeacher;
int nNumTeachers = 0;
while(!inputFile.eof())
{
inputFile >> tempTeacher;
classData.AddTeacher(&tempTeacher);
nNumTeachers++;
}
if(classData.GetNumTeachers() != nNumTeachers)
{
cout << "GetNumTeachers returned " << classData.GetNumTeachers() << " but nNumTeachers = " << nNumTeachers << endl;
cout << "Error adding teacher to database! \n";
return false;
}
for( int i = 0; i < nNumTeachers; i++)
{
if(classData.GetTeacher(i) == NULL)
{
cout << "Failed to properly retrieve teacher from database! \n";
return false;
}
}
cout << "Returning from database test, good job! \n";
return true;
}
bool TestStudent()
{
cout << "Inside test student \n";
cout << "Testing default constructor: \n";
// test the default constructor
CStudent defaultStudent;
if(defaultStudent.GetGPA() != 0)
{
cout << "Failed to initialize GPA! \n";
return false;
}
// test the copy constructor
cout << "Testing copy constructor: \n";
CStudent copyStudent(defaultStudent);
if(defaultStudent.GetGPA() != copyStudent.GetGPA())
{
cout << "Failed to copy GPA! \n";
return false;
}
// test the conversion constructor
cout << "Testing conversion constructor: \n";
double GPA = 4;
CStudent conversionConstructor(GPA);
if(GPA != conversionConstructor.GetGPA())
{
cout << "Failed to convert GPA! \n";
return false;
}
// open our student file
ifstream inputFile("slf.txt");
if(inputFile.fail())
{
cout << "Failed to open slf.txt! \n";
return false;
}
// test our stream insertion/extraction stuff
CStudent testFileRead[10];
ofstream outputFile("output.txt");
int index = 0;
cout << "Testing stream insertion/extraction operator \n";
while(!inputFile.eof())
{
cout << "Extracting... \n";
inputFile >> testFileRead[0];
cout << "Inserting... \n";
outputFile << testFileRead[0];
inputFile.peek();
index++;
}
inputFile.close();
outputFile.close();
testFileRead[0].SetGPA(0);
testFileRead[1].SetGPA(2);
cout << "Attempting to set the name of the student. \n";
testFileRead[0].SetName("James Clay");
testFileRead[1].SetName("James Clay");
testFileRead[0].SetID(10);
testFileRead[1].SetID(10);
if(testFileRead[0] == testFileRead[1])
{
cout << "Student failed GPA comparison test! \n";
return false;
}
testFileRead[0].SetGPA(0);
testFileRead[1].SetGPA(0);
testFileRead[0].SetName("James1 Clay");
testFileRead[1].SetName("James Clay");
testFileRead[0].SetID(10);
testFileRead[1].SetID(10);
if(testFileRead[0] == testFileRead[1])
{
cout << "Student failed NAME comparison test! \n";
return false;
}
testFileRead[0].SetGPA(0);
testFileRead[1].SetGPA(0);
testFileRead[0].SetName("James Clay");
testFileRead[1].SetName("James Clay");
testFileRead[0].SetID(1);
testFileRead[1].SetID(10);
if(testFileRead[0] == testFileRead[1])
{
cout << "Student failed ID comparison test! \n";
return false;
}
testFileRead[0].SetGPA(2);
testFileRead[1].SetGPA(2);
testFileRead[0].SetName("James Clay");
testFileRead[1].SetName("James Clay");
testFileRead[0].SetID(10);
testFileRead[1].SetID(10);
if(testFileRead[0] != testFileRead[1])
{
cout << "Student records marked as mismatched when they're not. Failed comparison test! \n";
return false;
}
cout << "Testing assignment operator. " << endl;
testFileRead[0].SetGPA(1);
testFileRead[0].SetName("Ja");
testFileRead[0].SetID(0);
testFileRead[0] = testFileRead[1];
if(testFileRead[0] != testFileRead[1])
{
cout << "Assignment operator test failed! \n";
return false;
}
cout << "Returning from test student \n";
return true;
}
bool TestTeacher()
{
cout << "Inside test teacher \n";
cout << "Testing default constructor: \n";
CTeacher defaultTeacher;
if(defaultTeacher.GetSalary() != 0)
{
cout << "Failed to properly initialize salary! \n";
return false;
}
cout << "Testing copy constructor: \n";
CTeacher copyTeacher(defaultTeacher);
if(copyTeacher.GetSalary() != defaultTeacher.GetSalary())
{
cout << "Failed to copy salary! \n";
return false;
}
cout << "Testing conversion constructor: \n";
double salary = 40000;
CTeacher conversionTeacher(salary);
if(salary != conversionTeacher.GetSalary())
{
cout << "Failed to convert salary! \n";
return false;
}
ifstream inputFile("tlf.txt");
if(inputFile.fail())
{
cout << "Failed to open tlf.txt! \n";
return false;
}
CTeacher testFileRead[10];
ofstream outputFile("output.txt");
int index = 0;
while(!inputFile.eof())
{
inputFile >> testFileRead[0];
outputFile << testFileRead[0];
inputFile.peek();
index++;
}
inputFile.close();
outputFile.close();
testFileRead[0].SetID(0);
testFileRead[1].SetID(1);
if(testFileRead[0] == testFileRead[1])
{
cout << "Teacher failed comparison test! \n";
return false;
}
cout << "Returning from test teacher \n";
return true;
}
bool TestClassroom()
{
cout << "Inside test classroom \n";
cout << "Testing default constructor: \n";
CClassroom defaultClassroom;
if(defaultClassroom.GetClassSize() != 0)
{
cout << "Failed to initialize class size! \n";
return false;
}
cout << "Testing copy constructor: \n";
CClassroom copyClassroom(defaultClassroom);
if(defaultClassroom.GetClassSize() != copyClassroom.GetClassSize())
{
cout << "Failed to copy class size! \n";
return false;
}
cout << "Testing conversion constructor: \n";
int classSize = 30;
CClassroom conversionClassroom(classSize);
if(conversionClassroom.GetClassSize() != classSize)
{
cout << "Failed to convert class size! \n";
return false;
}
ifstream inputFile("clf.txt");
if(inputFile.fail())
{
cout << "Failed to open clf.txt! \n";
return false;
}
CClassroom testFileRead;
ofstream outputFile("output.txt");
cout << "Reading in from clf.txt and outputting to output.txt" << endl;
while(!inputFile.eof())
{
inputFile >> testFileRead;
if(inputFile.eof())
{
break;
}
outputFile << testFileRead;
inputFile.peek();
}
inputFile.close();
outputFile.close();
inputFile.open("clf.txt");
ifstream newInputFile("output.txt");
cout << "Doing byte for byte comparison between clf.txt and output.txt" << endl;
while(!inputFile.eof() && !newInputFile.eof())
{
char original = inputFile.get();
char newChar = newInputFile.get();
if(original != newChar && (!isspace(newChar)) && (!isspace(original)))
{
cout << "The classroom stream extraction operator/insertion operator failed! \n";
return false;
}
}
inputFile.close();
newInputFile.close();
cout << "Returning from test classroom \n";
return true;
}
bool TestDataLoad()
{
ifstream inputFile("clf.txt");
if(inputFile.fail())
{
cout << "Failed to open clf.txt! \n";
return false;
}
cout << "Reading classes ... \n";
CClassroom classFileRead;
while(!inputFile.eof())
{
inputFile >> classFileRead;
inputFile.peek();
}
inputFile.close();
inputFile.open("slf.txt");
if(inputFile.fail())
{
cout << "Failed to open slf.txt! \n";
return false;
}
CStudent studentFileRead[30];
int index = 0;
while(!inputFile.eof())
{
inputFile >> studentFileRead[index];
index++;
inputFile.peek();
}
int i = 0;
if(i == classFileRead.GetClassSize())
{
cout << "Had difficulty either reading the student ID or the enrolled student's ID." << endl;
return false;
}
cout << "Returning from all data test, good job! \n";
return true;
}