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

OOFTST 32 - boolean field operations

This sample tests boolean field operations.


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

Our first move is to declare a class containing a couple of boolean fields.

DECLARE_CLASS(dbBoolTest)
        dbUlong			Number;
        dbBool	        IsEven;
        dbBool          IsOdd;  
        dbBoolTest() : dbTable("booltest") ,
                                Number("Number"),
                                IsEven("IsEven"),
                                IsOdd("IsOdd")
        {
                Number.index(kIndexNoDups);
                IsEven.index(kIndexed);
        };
          
        void addTestData();
        virtual void saveRecord();
};  

We have a different way to save the record as the two boolean values depend on the value of the other field. Adding data will only actually add a value to the numerical field and we then have to determine the boolen values from there, so we work out if the number is odd or even and assign the bvoolean values appropriately. Then we save the record as per usual by explicitly calling the saveRecord function from the dbTable class this class is descended from.

void
dbBoolTest::saveRecord()
{

To determine if the number is even, we use here a commonly used conditional expression incorporating the use of the  mod operation. The next operation just determines the opposite of what this expression find.

Testing: assignment to boolean by conditional expression
Testing: assignment to boolean by NOT of another boolean

	IsEven = ((Number%2) == 0);
	IsOdd = !IsEven;
	dbTable::saveRecord();
}

This procedure merely adds some test data, iterating to a certain number and adding each iteration as the record.

void
dbBoolTest::addTestData()
{
	const unsigned long numRecords = 200;
	cout << "Adding " << numRecords << " test records\n";
	for (unsigned long i=0; i<numRecords; i++) {
		newRecord();
		Number = i;
		saveRecord();
	}
}
  
int 
main()
{
	cout << "OOFILE Validation Suite - Test 32\n"
		 << "Store bools in database"
		 << endl << endl;  

We must declare our global variables at the beginning.

	TEST_CONNECT	theDB;
	dbBoolTest		BoolTest;	  

Now we assign a filename according to which platform we are running under.

	#ifdef TESTING_DBASE
		#ifdef _Macintosh
			const char* kExistsName =  ":ooftst32:booltest.dbf";
			const char* kDatabaseName = ":ooftst32:";
		#else
			const char* kExistsName =   "booltest.dbf"
			const char* kDatabaseName = "";
		#endif	  
	#else
		const char* kDatabaseName = "ooftst32.db";
		const char* kExistsName = kDatabaseName;
	#endif

We open or create a new empty database file.

	if (dbConnect::fileExists(kExistsName)) {
		theDB.openConnection(kDatabaseName);
		cout << "Deleting old test data\n";
		BoolTest.deleteAll();
	}
	else {
		theDB.newConnection(kDatabaseName);
	}  

Then add fresh data.

	BoolTest.addTestData();

Now we search the database on one of the boolean fields. The result of this search will return all records whose number is even. Note that we do not have to test if the field equals anything, we merely use the fact that this field is true to be the selection criteria. Note also that this is an indexed field

Testing: indexed search on boolean field

	cout << "Searching indexed True IsEven\n";
	BoolTest.search(BoolTest.IsEven);
	cout << BoolTest << endl;

Here we do a more traditional search, checking if the boolean field equals the value true. 

Testing: non-indexed search on boolean == true

	cout << "Searching nonindexed True IsOdd\n";
	BoolTest.search(BoolTest.IsOdd == true);
	cout << BoolTest << endl;
	  
	cout << endl << "done" << endl;
	return EXIT_SUCCESS;
}        

 

Feature index

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