Pseudo-Random Testing
by Guy W. Lecky-Thompson

Listing One
class CEmployee
{
  private:
    char * szName;
    long int lID;

  public:
    // Constructor
    CEmployee ( char * szName, long int lID );
    // Destructor
    ~CEmployee ( );
    // Inline Data Access Methods
    char * GetName() { return this->szName; }
    long int GetID() { return this->lID; }
};


Listing Two
#include <stdio.h>  // Standard ANSI C I/O
#include <string.h> // Useful for string comparisons
#include "CEmployee.h" // The object class to be tested int main ( void )
{
  // Instantiate the test data
  char test_name [] = "This is a test name. 123. ABC.";
  long int test_id = 12345;
  // Create an instance of the CEmployee object, using the test data
  CEmployee * OEmployee = new CEmployee ( test_name, test_id );
  // Check that the data has been correctly stored
  if ( strcmp( test_name, OEmployee->GetName() ) != 0)
    printf("Name test failed!\n Expected [%s] but found [%s]",
      test_name, OEmployee->GetName() );
  if ( test_id != OEmployee->GetID() )
    printf("ID test failed!\n Expected [%ld] but found [%lld]",
      test_id, OEmployee->GetID() );
  // Clean up...
  delete OEmployee;
  return 0;
}


Listing Three
CEmployee * OEmployee = new CEmployee( "", 1 );       // Illogical, Empty name
CEmployee * OEmployee = new CEmployee( 1, "" );       // Incorrect parameters
CEmployee * OEmployee = new CEmployee( "Test", -1 );  // Illogical ID
CEmployee * OEmployee = new CEmployee( "Test", 3.5 ); // ID wrong type


Listing Four
    ...
    char test_name[255]; // This array is larger than it needs to be
    // Generate the test_name data, containing every printable ANSI character
    int pos = 0;
    for (int j = 0; j < 255; j++)
    {
      if ( isprint(j) )
      {
       test_name[pos] = j;
        pos++;
      }
    }
    test_name[pos] = '\0'; // Just in case
    CEmployee * OEmployee = new CEmployee ( test_name, 1 ); // For example
    ...


Listing Five
void CreateRandomString ( long int nLength, char * szText )
{
  long int j;
  j = 0;
  while ( j < nLength )
  {
    int c = 0;
    while ( !isprint ( c ) )
    {
      c = rand() % 255;
    }
    szText[j] = c;
    j++;
  }
}


Listing Six
char * szText;
long int lSize;

lSize = rand() % MAX_LONG_INT;

szText = (char *) malloc ( (lSize + 1) * sizeof(char) );

CreateRandomString ( lSize, szText );
szText[lSize] = '\0';
Listing Seven
#include <stdio.h>
#include <stdlib.h>
#include "RandomString.h" // For the CreateRandomString function
void main ( void )
{
  char szFileName[] = "CEmployeeTest.cpp";
  FILE * hOut = fopen( szFileName, "w" ); // Open file for writing
  // Create the preamble for the test harness
  fprintf( hOut, "#include <stdio.h>\n\n #include \"CEmployee.h\"\n\n" );
  fprintf( hOut, "void main ( void )\n{\n" );
  // Set up the test_data
  char * test_data;
  long int lSize;
  lSize =  rand() % (MAX_LONG_INT / 2);
  lSize += rand() % (MAX_LONG_INT / 2);
  test_data = (char *) malloc ( (lSize + 1) * sizeof ( char ) );
  CreateRandomString ( lSize, test_data );
  // Print out the dataset
  fprintf( hOut, "char test_data[] = \"%s\"\n", test_data );
  // Print out the test process
  fprintf( hOut, "CEmployee * OEmployee = new CEmployee ( test_data, 1 )\n" );
  fprintf( hOut, "if ( strcmp ( OEmployee->GetName(), test_data ) != 0)\n" );
  fprintf( hOut, "\tprintf(\"String test failed!\\n\")\n");
  fprintf( hOut, "}\n" );
  fclose ( hOut );
}





