# C++ Notes: Test Program for xvector

Here is a test problem for the template class xvector as specified in problem 6 of Example: floatVector. It tests both xvector<float> and xvector<string>.

      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
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    // xvectorTest.cpp Test program for xvector class.
    //  Fred Swartz December 2004

    #include <iostream>
    #include <sstream>
    #include <cassert>
    #include <stdexcept>
    using namespace std;

    #include "xvector.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;

        //......................... test xvector<float> .............
        xvector<float> v;   // Calls the constructor.

        test(v.size()==0, "Initial size 0");
        test(v.empty()  , "empty() on empty xvector<float>");

        //--- 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;
        }

        xvector<float> v10;
        for (int i=1; i<=10; i++) {
            v10.push_back(1.0 * i);
        }

        //--- Test copy constructor
        xvector<float> v10copy = xvector<float>(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 ==
        test(v10==v10, "== 1");

        //--- Test assignment (assumes == works)
        xvector<float> 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 == again
        v10copy[3]++;  // change one element
        test(!(v10==v10copy), "== 2");

        //--- Test <<
        xvector<float> 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;
        xvector<float> v0;
        testout2 << v0;
        test(testout2.str() == "{}", string("<< \"") + testout2.str() + "\"");

        //--- Test clear
        v10.clear();
        test(v10.size() == 0, "clear()");

        //......................... test xvector<string> .............
        string s1 = "Hello";
        string s2 = "Guten Tag";
        string s3 = "Goodbye";
        string s4 = "Auf Widersehen";

        xvector<string> sv;   // Calls the constructor.

        test(sv.size()==0, "Initial size 0");
        test(sv.empty()  , "empty() on empty xvector<float>");

        //--- Test single element.
        sv.push_back(s1);
        test(!sv.empty()    , "empty() on non-empty floadVector");
        test(sv.size() ==1  , "push_back() added one element");
        test(sv.at(0)  ==s1, "at() returned correct value");
        test(sv[0]     ==s1, "[] returned correct value");
        test(sv.front()==s1, "front() returned correct value");
        test(sv.back() ==s1, "back() returned correct value");

        //--- Add enough elements to cause expansion
        currentSize = sv.capacity();
        cout << "capacity = " << currentSize << endl;
        for (addN=0; addN<5*currentSize; addN++) {
            cout << "adding " << addN <<endl;
            sv.push_back(s1);
        }
        test(sv.size()==addN+1, "size() ok after expansion.");
        test(sv.back()==s1, "last value ok after expansion");

        //--- Test assignment to at.
        sv.at(2) = s2;
        test(sv.at(2)==s2, "at() on left-hand-side.");

        //--- Test assignment to [].
        sv[3] = s2;
        test(sv.at(3)==s2, "[] on left-hand-side.");

        //--- Test assignment to front.
        sv.front() = s3;
        test(sv.at(0)==s3, "front() on left-hand-side.");

        //--- Test assignment to back.
        sv.back() = s4;
        test(sv.at(sv.size()-1)==s4, "back() on left-hand-side.");

        //--- Test at range check
        try {
            string causeException = sv.at(sv.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 {
            string causeException = sv.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 {
            string causeException = sv[sv.size()];
            test(false, "[] beyond size range check");
        } catch (out_of_range e) {
            test(true, "[] beyond size range check");
            //cerr << e.what() << endl;
        }
        try {
            string causeException = sv[-1];
            test(false, "[] below 0 range check");
        } catch (out_of_range e) {
            test(true, "[] below 0 range check");
            //cerr << e.what() << endl;
        }

        xvector<string> sv10;
        for (int i=1; i<=10; i++) {
            sv10.push_back(s3);
        }

        //--- Test copy constructor
        xvector<string> sv10copy = xvector<string>(sv10);
        test(sv10copy.size()==sv10.size(), "copy constructor: size equal");
        test(&sv10copy[0] != &sv10[0],     "copy constructor: different data areas");
        test(sv10copy.capacity() >= sv10copy.size(), "copy constructor: capacity >= size");
        elementsEqual = true;
        for (int i=0; i<sv10.size(); i++) {
            if (sv10copy[i] != sv10[i]) {
                elementsEqual = false;
                break;
            }
        }
        test(elementsEqual, "copy constructor: all elements equal");

        //--- Test ==
        test(sv10==sv10, "== 1");

        //--- Test assignment (assumes == works)
        xvector<string> sv10copy2;
        sv10copy2 = sv10;
        test(&sv10copy2[0] != &sv10[0], "assignment: different data areas");
        test(sv10copy2 == sv10,         "assignment: data elements and size equal");
        test(sv10.capacity() >= sv10.size(), "assignment: capacity ok");

        //--- Test == again
        sv10copy[3] = s2;  // change one element
        test(!(sv10==sv10copy), "== 2");

        //--- Test <<
        xvector<string> sv123;
        sv123.push_back(s1);
        sv123.push_back(s2);
        sv123.push_back(s3);
        ostringstream stestout;
        stestout << sv123;
        test(stestout.str() == "{Hello, Guten Tag, Goodbye}", string("<< user=\"") + testout.str() + "\"");

        ostringstream stestout2;
        xvector<string> sv0;
        stestout2 << sv0;
        test(stestout2.str() == "{}", string("<< \"") + testout2.str() + "\"");

        //--- Test clear
        sv10.clear();
        test(sv10.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;
    }