View Single Post
Old 02-06-2004, 08:57 AM   #1 (permalink)
Karby
Psycho
 
Location: somewhere
help...test driver with operators

we were given an assgnment to write test drivers for our two previous assgnments: class'Can' and class'Array', where we have to test each function in the program and show all the results and error messages going to a file, and the messages "Command number x completed" and "Testing completed" outputted to the screen. The 'Can' part was pretty easy because it just had a different functions with different names and we only had to test one operator: the input extraction, plus the copy constructor was easy to figure out:
PHP Code:
// Test driver

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "Can.h"
using namespace std;

int main()
{

    
int numCommands;


    
ifstream ins;
    
ofstream outs;

    
string inName;
    
string outName;

    
string label;
    
string command;

    
can cannis;
    
    
cout << "Enter the name of input file; press return.\n";
    
cin >> inName;
    
ins.open(inName.c_str());

    
cout << "Enter the name of output file; press return.\n";
    
cin >> outName;
    
outs.open(outName.c_str());

    
cout << "Enter the name of the test run; press return.\n";
    
cin >> label;
    
outs << label << endl;

    
    
numCommands 0;
    
ins >> command;
    
    while (
command != "Quit")
    {

        if (
command == "operator")
        {
            
float wtradht;
            
numCommands++;
            
ins >> wt >> rad >> ht;
            if ( 
wt && rad && ht 0)
            {
                
outs << "\nOperator is good" << endl;
            
                
outs <<  wt << rad << ht << endl;
            }
            else
            { 
                
outs << "File contains one or more values of invalid data \n";
            }
            
cout << "Command number " << numCommands << " completed.\n";
        }

        else if (
command == "can")
        {
            
ins >> cannis;
            
cannis.getWeight();
            
cannis.getHeight();
            
cannis.getRadius();
                
            
                
outs <<    cannis.getWeight() << endl;
                
            
outs <<    cannis.getHeight() << endl;
                        
outs <<    cannis.getRadius() << endl;
        }

        else if (
command == "gramsProduct")
        {
            
int productVol;
            
numCommands ++;
            
            
ins >> productVol;
            
cannis.gramsProduct(productVol);
            if (
productVol )

                
outs << "Invalid data in file\n";
            else
            
                
outs << productVol << endl;
                         
            
cout << "Command number " << numCommands << " completed.\n";

        }
            
        else if (
command == "getWeight")
        {
            
numCommands ++;
            
outs <<    cannis.getWeight() << endl;
            
            
            
cout << "Command number " << numCommands << " completed.\n";
        }
                
        else if (
command == "getRadius")
        {
            
        
outs <<    cannis.getRadius() << endl;
        
            
numCommands ++;
            
cout << "Command number " << numCommands << " completed.\n";
        }    
        
        else if (
command == "getHeight")
        {
            
        
outs <<    cannis.getHeight() << endl;
            
numCommands ++;
            
cout << "Command number " << numCommands << " completed.\n";
        }    
        
        else if (
command == "volume")
        {
        
        
outs <<    cannis.volume() << endl;
            
numCommands ++;
            
cout << "Command number " << numCommands << " completed.\n";
        }    


        
ins >> command;
    }

    
outs << "Testing completed.\n";
    
ins.close();
    
outs.close();



    return 
0;

PHP Code:
 // input file:
operator
6 9 3 

can
6 9 3

gramsProduct
-4
getWeight

getRadius

getHeight

volume

operator
6 9 
-3

Quit 
but the 'Array' program has a lot of different operators, and even though they do different things, they basically all have the same names. not only that, i can't test the Array copy constructor the way i did in Can because of all the operators...or at least...i thin k that's the problem:
PHP Code:
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

#include <cassert>
#include <cstdlib>
#include "array.h"
//
//  Default constructor
//
Array::Array()
{
    
size 0;
}

//
//  Copy constructor
//
Array::Array( const Array& )
{
    for (
int j 0;  a.size;  ++j)
        
data[j] = a.data[j];
    
size a.size;
}

//
//  Assignment operator
//
Array& Array:: operator = (const Array& a)
{
    for (
int j 0;  a.size;  ++j)
        
data[j] = a.data[j];
    
size a.size;
    return *
this;
}

//
//  Equality operator
//
bool Array:: operator == (const Array& a) const
{
    
bool equal size == a.size;
    
int j 0;

    while (
size  &&  equal)
        if (
data[j] != a.data[j])
            
equal false;
        else
            ++
j;
    return 
equal;
}

//
//  Subscript operator with range checking
//
Element& Array:: operator [] (int index
{
    
assertindex >= 0  &&  index size );
    return 
data[index];
}

// Multiplication operator

Element Array:: operator * (const Array& a)
{
    
Element product 0;
    
assert(size == a.size);

    for (
int i 0sizei++)
        
product += data[i] * a.data[i];

    return 
product;
}

//
//  Revised Input extraction operator
//
istreamoperator >> (istreamin, Array& a)
{
      
a.size MaxArraySize;

    
int i 0;
    
in >> a.data[i];
    
i++;
    while ( 
a.size && !in.fail() && !in.eof())
    {
        
in >> a.data[i];
         ++
i;
    }
    if(!
in.fail() || !in.eof())
    {
        
in >> a.data[i];
        if(!
in.fail())
        {
            
in.setstate(ios::failbit);
            
a.size 0;
        }
    }
    else if(
in.eof())
    {
        
in.clear();
        
a.size 1;
    }
    else
        
a.size 0;
            
    return 
in;
}

//
//  Output insertion operator
//
ostreamoperator << (ostreamout, const Array& a)
{
    for (
int i 0a.size; ++i)
        
out << setw) << a.data[i];
    
out << endl;
    return 
out;

i can't do:
if (command == " operator")
because they all say operator.

and i can't say:
"operator
operator
operator
etc."
in the input file...

and i have no idea how to test the copy constructor with all those operators...i need help
i hope i'm saying all of this right

i think i gave all the info that i needed to...but if you think i left anything out...then i can show everything else
__________________
~my karma ran over my dogma.~
Karby is offline  
 

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