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
|
/*
*
* Copyright (C) 2010-2022, OFFIS e.V.
* All rights reserved. See COPYRIGHT file for details.
*
* This software and supporting documentation were developed by
*
* OFFIS e.V.
* R&D Division Health
* Escherweg 2
* D-26121 Oldenburg, Germany
*
*
* Module: ofstd
*
* Author: Uli Schlachter
*
* Purpose: test program for class OFVector
*
*/
#include "dcmtk/config/osconfig.h"
#define OFTEST_OFSTD_ONLY
#include "dcmtk/ofstd/oftest.h"
#include "dcmtk/ofstd/ofvector.h"
struct Recursive : OFVector<Recursive>
{
int i;
};
OFTEST(ofstd_OFVector)
{
OFVector<int> m;
OFVector<int>::iterator it;
int i;
// Just for the fun of it, test if an empty vector is empty
// (and if one can have vectors in other vectors).
OFCHECK(OFVector<OFVector<char> >().empty());
// Test whether iterators stay valid if enough space was reserved
m.reserve(6);
it = m.begin();
OFCHECK(it == m.begin());
OFCHECK(it == m.end());
// the first push_back() invalidates the iterator,
// therefore, get a new one
m.push_back(1);
it = m.begin();
OFCHECK(it == m.begin());
OFCHECK(it != m.end());
// Fill with some entries
for (i = 2; i <= 6; ++i)
m.push_back(i);
OFCHECK(it == m.begin());
OFCHECK(it != m.end());
// verify that the entries where really added
OFCHECK_EQUAL(m.size(), 6);
for (i = 1; i <= 6; ++i)
OFCHECK_EQUAL(*(it++), i);
OFCHECK(it == m.end());
// check erase()
for (i = 1; i <= 3; ++i)
{
OFCHECK_EQUAL(m.at(0), i);
OFCHECK_EQUAL(m[0], i);
m.erase(m.begin());
}
OFCHECK_EQUAL(m.size(), 3);
// test the copy-constructor
OFVector<int> n(m);
OFCHECK_EQUAL(n.size(), 3);
// does pop_back() work?
n.pop_back();
OFCHECK_EQUAL(n.size(), 2);
OFCHECK_EQUAL(m.size(), 3);
// test operator= and the range-constructor
n = OFVector<int>(m.begin(), m.end());
OFCHECK_EQUAL(n.size(), 3);
OFCHECK_EQUAL(n[1], 5);
n[1] = 42;
OFCHECK_EQUAL(n[1], 42);
n.pop_back();
// does swap do what it is supposed to do?
it = m.begin();
m.swap(n);
OFCHECK(it == n.begin());
OFCHECK_EQUAL(n.size(), 3);
OFCHECK_EQUAL(m.size(), 2);
OFCHECK_EQUAL(n[1], 5);
OFCHECK_EQUAL(m[1], 42);
// does clear() really clear the vector?
n.clear();
OFCHECK(n.empty());
// this should produce a vector which elements 0 to 49 with value "5",
// then the values "4" and "42" once and then another 50 times "6".
n = OFVector<int>(50, 5);
n.resize(100, 6);
OFCHECK_EQUAL(n.size(), 100);
n.insert(n.begin() + 50, m.begin(), m.end());
OFCHECK_EQUAL(n.size(), 102);
it = n.begin();
for (i = 0; it != n.end(); ++it, ++i)
{
if (i < 50)
OFCHECK_EQUAL(*it, 5);
else if(i == 50)
OFCHECK_EQUAL(*it, 4);
else if(i == 51)
OFCHECK_EQUAL(*it, 42);
else
OFCHECK_EQUAL(*it, 6);
}
// test if recursive vector structures can be constructed without causing
// a stack overflow
OFCHECK(OFVector<Recursive>().size() == 0);
}
|