Tilted Forum Project Discussion Community

Tilted Forum Project Discussion Community (https://thetfp.com/tfp/)
-   Tilted Technology (https://thetfp.com/tfp/tilted-technology/)
-   -   help...test driver with operators (https://thetfp.com/tfp/tilted-technology/44602-help-test-driver-operators.html)

Karby 02-06-2004 08:57 AM

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 wt, rad, ht;
            
numCommands++;
            
ins >> wt >> rad >> ht;
            if ( 
wt > 0 && rad > 0 && 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 < 0 )

                
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& a )
{
    for (
int j = 0;  j < a.size;  ++j)
        
data[j] = a.data[j];
    
size = a.size;
}

//
//  Assignment operator
//
Array& Array:: operator = (const Array& a)
{
    for (
int j = 0;  j < 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 (
j < size  &&  equal)
        if (
data[j] != a.data[j])
            
equal = false;
        else
            ++
j;
    return 
equal;
}

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

// Multiplication operator

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

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

    return 
product;
}

//
//  Revised Input extraction operator
//
istream& operator >> (istream& in, Array& a)
{
      
a.size = MaxArraySize;

    
int i = 0;
    
in >> a.data[i];
    
i++;
    while ( 
i < 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 = i - 1;
    }
    else
        
a.size = 0;
            
    return 
in;
}

//
//  Output insertion operator
//
ostream& operator << (ostream& out, const Array& a)
{
    for (
int i = 0; i < a.size; ++i)
        
out << setw( 4 ) << 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

cowlick 02-09-2004 06:49 AM

Why not make the command names things like:
"Copy_Constructor"
"Default_Constructor"
"Assignment_Op"

and then map from a table of strings to the method to test.

Karby 02-10-2004 11:41 AM

i figured it out already. but thanx anyway:)


All times are GMT -8. The time now is 07:57 PM.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2026, vBulletin Solutions, Inc.
Search Engine Optimization by vBSEO 3.6.0 PL2
© 2002-2012 Tilted Forum Project


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 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73