Lumiera  0.pre.03
»edit your freedom«
vector-transfer-test.cpp
Go to the documentation of this file.
1 /*
2  VectorTransfer(Test) - intercept object copying when a STL vector grows
3 
4  Copyright (C) Lumiera.org
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License as
9  published by the Free Software Foundation; either version 2 of
10  the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 
21 * *****************************************************/
22 
29 #include "lib/test/run.hpp"
30 
32 #include "lib/test/testdummy.hpp"
33 
34 #include <iostream>
35 #include <vector>
36 
37 
38 namespace lib {
39 namespace test {
40 
41  using ::Test;
42  using std::vector;
43  using std::cout;
44 
45  namespace { // extending the Dummy for our special purpose....
46 
47  class TransDummy
48  : public Dummy
49  {
50  public:
51  TransDummy()
52  {
53  TRACE (test, "CTOR TransDummy() --> this=%p", this);
54  setVal(0); // we use val_==0 to mark the "empty" state
55  }
56 
57  ~TransDummy()
58  {
59  TRACE (test, "DTOR ~TransDummy() this=%p", this);
60  }
61 
62  /* to make Dummy usable within vector, we need to provide
63  * \em special copy operations, an operator bool() and
64  * a transfer_control friend function to be used by
65  * our special allocator.
66  */
67 
68  TransDummy (const TransDummy& o)
69  : Dummy()
70  {
71  TRACE (test, "COPY-ctor TransDummy( ref=%p ) --> this=%p", &o,this);
72  CHECK (!o, "protocol violation: real copy operations inhibited");
73  }
74 
75  TransDummy&
76  operator= (TransDummy const& ref)
77  {
78  TRACE (test, "COPY target=%p <-- source=%p", this,&ref);
79  CHECK (!(*this));
80  CHECK (!ref, "protocol violation: real copy operations inhibited");
81  return *this;
82  }
83 
84  void
85  setup (int x=0)
86  {
87  setVal (x? x : (rand() % 10000));
88  TRACE (test, "CREATE val=%d ---> this=%p", getVal(),this);
89  }
90 
91 
92  // define implicit conversion to "bool" the naive way...
93  operator bool() const
94  {
95  return 0!=getVal();
96  }
97 
98 
99  friend void transfer_control (TransDummy& from, TransDummy& to);
100 
101  };
102 
103 
104 
105  void
106  transfer_control (TransDummy& from, TransDummy& to)
107  {
108  TRACE (test, "TRANSFER target=%p <-- source=%p", &to,&from);
109  CHECK (!to, "protocol violation: target already manages another object");
110  to.setVal (from.getVal());
111  from.setVal(0);
112  }
113 
115  typedef vector<TransDummy, Allo> TransDummyVector;
116  }
117 
118 
119 
120 
121  /******************************************************************************/
128  class VectorTransfer_test : public Test
129  {
130 
131  virtual void
132  run (Arg)
133  {
134  cout << "\n..setup table space for 2 elements\n";
135  TransDummyVector table;
136  table.reserve(2);
137  CHECK (0 == Dummy::checksum());
138 
139  cout << "\n..install one element at index[0]\n";
140  table.push_back(TransDummy());
141  CHECK (0 == Dummy::checksum());
142 
143  table[0].setup(); // switches into "managed" state
144  CHECK (0 < Dummy::checksum());
145  int theSum = Dummy::checksum();
146 
147  cout << "\n..*** resize table to 5 elements\n";
148  table.resize(5);
149  CHECK (theSum==Dummy::checksum());
150 
151  cout << "\n..install another element\n";
152  table[3].setup(375);
153  CHECK (theSum+375==Dummy::checksum());
154 
155  cout << "\n..kill all elements....\n";
156  table.clear();
157  CHECK (0 == Dummy::checksum());
158  }
159 
160  };
161 
162  LAUNCHER (VectorTransfer_test, "unit common");
163 
164 
165 }} // namespace lib::test
Definition: run.hpp:49
Implementation namespace for support and library code.
unittest helper code: test dummy objects to track instances.
A mechanism to take ownership without allowing copy.
Addendum to scoped-holder.hpp for transferring the lifecycle management to another instance...
Simple test class runner.
A Dummy object for tests.
Definition: testdummy.hpp:50
auto setup(FUN &&workFun)
Helper: setup a Worker-Pool configuration for the test.