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)
5  2008, Hermann Vosseler <Ichthyostega@web.de>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
20 #include "lib/test/run.hpp"
21 
24 #include "lib/format-cout.hpp"
25 
26 #include <vector>
27 
28 
29 namespace lib {
30 namespace test {
31 
32  using ::Test;
33  using std::vector;
34 
35  namespace { // extending the Dummy for our special purpose....
36 
37  class TransDummy
38  : public Dummy
39  {
40  public:
41  TransDummy()
42  {
43  TRACE (test, "CTOR TransDummy() --> this=%p", this);
44  setVal(0); // we use val_==0 to mark the "empty" state
45  }
46 
47  ~TransDummy()
48  {
49  TRACE (test, "DTOR ~TransDummy() this=%p", this);
50  }
51 
52  /* to make Dummy usable within vector, we need to provide
53  * \em special copy operations, an operator bool() and
54  * a transfer_control friend function to be used by
55  * our special allocator.
56  */
57 
58  TransDummy (const TransDummy& o)
59  : Dummy()
60  {
61  TRACE (test, "COPY-ctor TransDummy( ref=%p ) --> this=%p", &o,this);
62  CHECK (!o, "protocol violation: real copy operations inhibited");
63  }
64 
65  TransDummy&
66  operator= (TransDummy const& ref)
67  {
68  TRACE (test, "COPY target=%p <-- source=%p", this,&ref);
69  CHECK (!(*this));
70  CHECK (!ref, "protocol violation: real copy operations inhibited");
71  return *this;
72  }
73 
74  void
75  setup (int x=0)
76  {
77  setVal (x? x : rani (10000));
78  TRACE (test, "CREATE val=%d ---> this=%p", getVal(),this);
79  }
80 
81 
82  // define implicit conversion to "bool" the naive way...
83  operator bool() const
84  {
85  return 0!=getVal();
86  }
87 
88 
89  friend void transfer_control (TransDummy& from, TransDummy& to);
90 
91  };
92 
93 
94 
95  void
96  transfer_control (TransDummy& from, TransDummy& to)
97  {
98  TRACE (test, "TRANSFER target=%p <-- source=%p", &to,&from);
99  CHECK (!to, "protocol violation: target already manages another object");
100  to.setVal (from.getVal());
101  from.setVal(0);
102  }
103 
105  typedef vector<TransDummy, Allo> TransDummyVector;
106  }
107 
108 
109 
110 
111  /******************************************************************************/
118  class VectorTransfer_test : public Test
119  {
120 
121  virtual void
122  run (Arg)
123  {
124  seedRand();
125 
126  cout << "\n..setup table space for 2 elements\n";
127  TransDummyVector table;
128  table.reserve(2);
129  CHECK (0 == Dummy::checksum());
130 
131  cout << "\n..install one element at index[0]\n";
132  table.push_back(TransDummy());
133  CHECK (0 == Dummy::checksum());
134 
135  table[0].setup(); // switches into "managed" state
136  CHECK (0 < Dummy::checksum());
137  int theSum = Dummy::checksum();
138 
139  cout << "\n..*** resize table to 5 elements\n";
140  table.resize(5);
141  CHECK (theSum==Dummy::checksum());
142 
143  cout << "\n..install another element\n";
144  table[3].setup(375);
145  CHECK (theSum+375==Dummy::checksum());
146 
147  cout << "\n..kill all elements....\n";
148  table.clear();
149  CHECK (0 == Dummy::checksum());
150  }
151 
152  };
153 
154  LAUNCHER (VectorTransfer_test, "unit common");
155 
156 
157 }} // namespace lib::test
Automatically use custom string conversion in C++ stream output.
Definition: run.hpp:40
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Implementation namespace for support and library code.
A mechanism to take ownership without allowing copy.
Addendum to scoped-holder.hpp for transferring the lifecycle management to another instance...
Simplistic test class runner.
unittest helper code: test dummy objects to track instances.
A Dummy object for tests.
auto setup(FUN &&workFun)
Helper: setup a Worker-Pool configuration for the test.