This program tests the enhancements posed in the first five floatVector problems. SeeExample: floatVector - Version 1
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
// fv3/floatVectorTest.cpp Test program for floatVector class. // Fred Swartz 2004-12-01 #include <iostream> #include <sstream> #include <cassert> #include <stdexcept> using namespace std; #include "floatVector.h" //=================================================== prototypes void test(bool cond, string message); void summarizeTestResults(); //=================================================== global vars static int testsPassed; static int testsFailed; //============================================================= main int main() { //... Initialize test stats. testsPassed = 0; testsFailed = 0; floatVector v; // Calls the constructor. test(v.size()==0, "Initial size 0"); test(v.empty() , "empty() on empty floatVector"); //--- Test single element. v.push_back(1.0); test(!v.empty() , "empty() on non-empty floadVector"); test(v.size() ==1 , "push_back() added one element"); test(v.at(0) ==1.0, "at() returned correct value"); test(v[0] ==1.0, "[] returned correct value"); test(v.front()==1.0, "front() 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 []. v[3] = 77.0; test(v.at(3)==77.0, "[] on left-hand-side."); //--- Test assignment to front. v.front() = 88.0; test(v.at(0)==88.0, "front() 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."); //--- Test at range check try { float causeException = v.at(v.size()); test(false, "at() beyond size range check"); } catch (out_of_range e) { test(true, "at() beyond size range check"); //cerr << e.what() << endl; } try { float causeException = v.at(-1); test(false, "at() below 0 range check"); } catch (out_of_range e) { test(true, "at() below 0 range check"); //cerr << e.what() << endl; } //--- Test [] range check try { float causeException = v[v.size()]; test(false, "[] beyond size range check"); } catch (out_of_range e) { test(true, "[] beyond size range check"); //cerr << e.what() << endl; } try { float causeException = v[-1]; test(false, "[] below 0 range check"); } catch (out_of_range e) { test(true, "[] below 0 range check"); //cerr << e.what() << endl; } floatVector v10; for (int i=1; i<=10; i++) { v10.push_back(1.0 * i); } //--- Test copy constructor floatVector v10copy = floatVector(v10); test(v10copy.size()==v10.size(), "copy constructor: size equal"); test(&v10copy[0] != &v10[0], "copy constructor: different data areas"); test(v10copy.capacity() >= v10copy.size(), "copy constructor: capacity >= size"); bool elementsEqual = true; for (int i=0; i<v10.size(); i++) { if (v10copy[i] != v10[i]) { elementsEqual = false; break; } } test(elementsEqual, "copy constructor: all elements equal"); //--- Test equals test(v10==v10, "=="); //--- Test assignment (assumes == works) floatVector v10copy2; v10copy2 = v10; test(&v10copy2[0] != &v10[0], "assignment: different data areas"); test(v10copy2 == v10, "assignment: data elements and size equal"); test(v10.capacity() >= v10.size(), "assignment: capacity ok"); //--- Test << floatVector v123; v123.push_back(1.0); v123.push_back(2.0); v123.push_back(3.0); ostringstream testout; testout << v123; test(testout.str() == "{1, 2, 3}", string("<< user=\"") + testout.str() + "\""); ostringstream testout2; floatVector v0; testout2 << v0; test(testout2.str() == "{}", string("<< \"") + testout2.str() + "\""); //--- Test clear v10.clear(); test(v10.size() == 0, "clear()"); summarizeTestResults(); //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; } //================================================ summarizeTestResults void summarizeTestResults() { //... 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; } |