C++ Notes: Classes: Example: floatVectorTest.cpp - Version 1

This is a main program to test the floatVector class, which has similar capabilities to STL vector<float>. See Example: floatVector - Version 1 for links to the header and implementation files.

  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 
 74 
 75 
 76 
 77 
 78 
 79 
 80 
 81 
 82 
 83 
 84 
 85 
 86 
 87 
// fv1/floatVectorTest.cpp Test program for floatVector class.
//  Fred Swartz 2004-12-01

#include <iostream>
using namespace std;

#include "floatVector.h"

//======================================================= prototypes
void test(bool cond, string message);

//====================================================== global vars
static int testsPassed;
static int testsFailed;

//============================================================= main
int main() {    
    //... Initialize test stats.
    testsPassed = 0;
    testsFailed = 0;

    floatVector v;    // Calls constructor

    test(v.size()==0, "Initial size 0");
    
    //--- Test single element.
    v.push_back(1.0);
    test(v.size() ==1  , "push_back() added one element");
    test(v.at(0)  ==1.0, "at() returned correct value"); 
    test(v.back() ==1.0, "back() returned correct value");
    
    //--- Add enough elements to cause expansion
    int currentSize = v.capacity();
    int addN = 0;  // number of values to be added.
    for (; addN<5*currentSize; addN++) {
        v.push_back(2.0 * addN);
    }
    test(v.size()==addN+1, "size() ok after expansion.");
    test(v.back()==2.0*(addN-1), "last value ok after expansion");
    
    
    //--- Test assignment to at.
    v.at(2) = 66.0;
    test(v.at(2)==66.0, "at() on left-hand-side.");
    
    //--- Test assignment to back.
    v.back() = 99.0;
    test(v.at(v.size()-1)==99.0, "back() on left-hand-side.");    

    floatVector v10;
    for (int i=1; i<=10; i++) {
        v10.push_back(1.0 * i);
    } 
    
    
    //--- Test clear
    v10.clear();
    test(v10.size() == 0, "clear()");
    
                  

    //... Print test result summary.
    cout << endl << "============================= Test Results" << endl;
    cout << "= Passed: " << testsPassed << endl;
    if (testsFailed > 0) {
        cout << "= >>>>>>>>>>>>>>FAILED: " << testsFailed << endl;
    } else {
        cout << "= No failures" << endl;
    }
    cout << "=============================" << endl;
    
    system("PAUSE");    // Keep Dev-C++ window open
    return 0;
}//end main


//================================================================ test
void test(bool cond, string message) {
    if (cond) {
        cout << "   Passed: ";
        testsPassed++;
    } else {
        cout << "FAILED: ";
        testsFailed++;
    }
    cout << message << endl;
}