OOFILE | Downloads | Purchasing | Press | Services | Company Information | Soapbox | References | F.A.Q. | HOME

OOFTST 11 - generating test data

This sample shows an easy way to generate test data. It comes from a real project, which is why the CLASS_TABLE looks a bit funny - it was generated by AppMaker


#include "oofile.h"	// the general oofile library

Here, we declare our class.

CLASS_TABLE(CdbPhonePro)
	dbChar		LastName;
	dbChar		FirstName;
	dbChar		Title;
	dbChar		Extn;
	dbChar		Department;
	dbChar		Pager;  
CdbPhonePro() : dbTable("PhonePro") ,
	LastName(50, "LastName"),
	FirstName(30, "FirstName"),
	Title(50, "Title"),
	Extn(24, "Extn"),
	Department(50, "Department"),
	Pager(24, "Pager")
{
LastName.index(kIndexCompress);
FirstName.index(kIndexCompress);
Title.index(kIndexCompress);
Extn.index(kIndexCompress);
Department.index(kIndexCompress);
Pager.index(kIndexCompress);  
};
};

We now declare our database and table.

dbConnect_ctree theDB;
CdbPhonePro	PhonePro;  
int main()
{  

This changes the types of files created by c-tree (given modification to ctclib.c) when you are using a Macintosh.

#ifdef _Macintosh
ctMacCreator = 'PhPr';
ctMacType = 'OOCT';
#endif  
	cout << "OOFILE Validation Suite - Test 11\n"
		 << "Simple test to demonstrate using the inbuilt test data generator" << endl
		 << "as you would to generate random data for a real project" << endl << endl;
		   

First off, we prompt the user for a filename, from which the random chunks will be taken.

	cout << "Enter the name of a text file from which random chunks will be drawn" << endl
	     << "File: " << flush;
	       

We store the response.

	char testFileName[255];
	cin.width(255);
	cin  >> testFileName;
	  

We now test to see if the file exists, giving an error message if it doesn't.

	if (!dbConnect::fileExists(testFileName)) {
		cout << "Sorry, couldn't find the file: " << testFileName << endl;
		return EXIT_SUCCESS;
	}

We now prompt for the number of test records, the user want to us create. 

	cout << endl << "Number of test records: " << flush;
	unsigned long numRecs;
	cin >> numRecs;  

We test to see if the user wants none created and end the program if this is the case.

	if (numRecs==0)
		return EXIT_SUCCESS;
		  

We now create or open and clear the contents of the file in which we will store the new test database.

	if (dbConnect::fileExists("ooftst11.db")) {
		theDB.openConnection("ooftst11.db");
		cout << "Deleting the " << PhonePro.countAll() << " records from the last run..." << endl;
		PhonePro.deleteAll();
	}
	else {
		theDB.newConnection("ooftst11.db");
	}
	  
	cout << endl << "Generating " << numRecs << " test records..." << endl;
	  

We generate the test data by calling the function generateTestData(), passing it the file name and number of records we wish for it to create.

	theDB.generateTestData(testFileName, numRecs);
	  

We ask the user if they want to display the test data.

	cout << endl << "Do you want to display the test data? (Y/N) " << flush;
	char yn = 'N';
	cin >> yn;
	  

If their response was a definite yes, we print the database for them.

	if ((yn=='y') || (yn=='Y'))
		cout << theDB << endl;
	  
	cout << "done" << endl;  
	return EXIT_SUCCESS;
}        

 

Feature index

(c) Copyright A.D. Software 1994-2000 (All Rights Reserved).
Last Updated: 9th September 2001