Lumiera  0.pre.03
»edit your freedom«
testframe.cpp
Go to the documentation of this file.
1 /*
2  TestFrame - test data frame (stub) for checking Render engine functionality
3 
4  Copyright (C) Lumiera.org
5  2011, 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/error.hpp"
30 
31 #include <boost/random/linear_congruential.hpp>
32 
33 #include <cstring>
34 #include <memory>
35 #include <vector>
36 
37 
38 
39 namespace steam {
40 namespace engine {
41 namespace test {
42 
43  using std::vector;
44  using std::memcpy;
45 
46  typedef boost::rand48 PseudoRandom;
47 
48 
49  namespace error = lumiera::error;
50 
51  namespace { // hidden local support facilities....
52 
61  uint64_t
62  generateDistinction(uint seq, uint family)
63  {
64  // random offset, but fixed per executable run
65  static uint base(10 + rand() % 990);
66 
67  // use the family as stepping
68  return (seq+1) * (base+family);
69  }
70 
71 
72  TestFrame&
73  accessAsTestFrame (void* memoryLocation)
74  {
75  REQUIRE (memoryLocation);
76  return *reinterpret_cast<TestFrame*> (memoryLocation);
77  }
78 
79 
94  template<uint CHA, uint FRA>
96  : vector<vector<TestFrame>>
97  {
98  typedef vector<vector<TestFrame>> VECT;
99 
101  : VECT(CHA)
102  {
103  for (uint i=0; i<CHA; ++i)
104  at(i).reserve(FRA);
105  }
106 
107  TestFrame&
108  getFrame (uint seqNr, uint chanNr=0)
109  {
110  if (chanNr >= this->size())
111  {
112  WARN (test, "Growing table of test frames to %d channels, "
113  "which is > the default (%d)", chanNr, CHA);
114  resize(chanNr+1);
115  }
116  ENSURE (chanNr < this->size());
117  vector<TestFrame>& channel = at(chanNr);
118 
119  if (seqNr >= channel.size())
120  {
121  WARN_IF (seqNr >= FRA, test,
122  "Growing channel #%d of test frames to %d elements, "
123  "which is > the default (%d)", chanNr, seqNr, FRA);
124  for (uint i=channel.size(); i<=seqNr; ++i)
125  channel.push_back (TestFrame (i,chanNr));
126  }
127  ENSURE (seqNr < channel.size());
128 
129  return channel[seqNr];
130  }
131  };
132 
133  const uint INITIAL_CHAN = 20;
134  const uint INITIAL_FRAMES = 100;
135 
137 
138  std::unique_ptr<TestFrames> testFrames;
139 
140 
141  TestFrame&
142  accessTestFrame (uint seqNr, uint chanNr)
143  {
144  if (!testFrames) testFrames.reset (new TestFrames);
145 
146  return testFrames->getFrame(seqNr,chanNr);
147  }
148 
149  } // (End) hidden impl details
150 
151 
152 
153 
154  TestFrame&
155  testData (uint seqNr)
156  {
157  return accessTestFrame (seqNr, 0);
158  }
159 
160  TestFrame&
161  testData (uint chanNr, uint seqNr)
162  {
163  return accessTestFrame (seqNr,chanNr);
164  }
165 
166  void
168  {
169  testFrames.reset(0);
170  }
171 
172 
173 
174 
175  /* ===== TestFrame class ===== */
176 
177  TestFrame::~TestFrame()
178  {
179  stage_ = DISCARDED;
180  }
181 
182 
183  TestFrame::TestFrame(uint seq, uint family)
184  : distinction_(generateDistinction (seq,family))
185  , stage_(CREATED)
186  {
187  ASSERT (0 < distinction_);
188  buildData();
189  }
190 
191 
192  TestFrame::TestFrame (TestFrame const& o)
193  : distinction_(o.distinction_)
194  , stage_(CREATED)
195  {
196  memcpy (data_, o.data_, BUFFSIZ);
197  }
198 
199  TestFrame&
200  TestFrame::operator= (TestFrame const& o)
201  {
202  if (DISCARDED == stage_)
203  throw new error::Logic ("target TestFrame is already dead");
204  if (this != &o)
205  {
206  distinction_ = o.distinction_;
207  stage_ = CREATED;
208  memcpy (data_, o.data_, BUFFSIZ);
209  }
210  return *this;
211  }
212 
213 
214 
220  bool
221  TestFrame::isAlive (void* memLocation)
222  {
223  TestFrame& candidate (accessAsTestFrame (memLocation));
224  return candidate.isSane()
225  && candidate.isAlive();
226  }
227 
228  bool
229  TestFrame::isDead (void* memLocation)
230  {
231  TestFrame& candidate (accessAsTestFrame (memLocation));
232  return candidate.isSane()
233  && candidate.isDead();
234  }
235 
236  bool
237  TestFrame::operator== (void* memLocation) const
238  {
239  TestFrame& candidate (accessAsTestFrame (memLocation));
240  return candidate.isSane()
241  && candidate == *this;
242  }
243 
244  bool
245  TestFrame::contentEquals (TestFrame const& o) const
246  {
247  for (uint i=0; i<BUFFSIZ; ++i)
248  if (data_[i] != o.data_[i])
249  return false;
250  return true;
251  }
252 
253  bool
254  TestFrame::verifyData() const
255  {
256  PseudoRandom gen(distinction_);
257  for (uint i=0; i<BUFFSIZ; ++i)
258  if (data_[i] != char(gen() % CHAR_MAX))
259  return false;
260  return true;
261  }
262 
263  void
264  TestFrame::buildData()
265  {
266  PseudoRandom gen(distinction_);
267  for (uint i=0; i<BUFFSIZ; ++i)
268  data_[i] = char(gen() % CHAR_MAX);
269  }
270 
271 
272  bool
273  TestFrame::isAlive() const
274  {
275  return (CREATED == stage_)
276  || (EMITTED == stage_);
277  }
278 
279  bool
280  TestFrame::isDead() const
281  {
282  return (DISCARDED == stage_);
283  }
284 
285  bool
286  TestFrame::isSane() const
287  {
288  return ( (CREATED == stage_)
289  ||(EMITTED == stage_)
290  ||(DISCARDED == stage_))
291  && verifyData();
292  }
293 
294 
295 
296 }}} // namespace steam::engine::test
static bool isAlive(void *memLocation)
Helper to verify that a given memory location holds an active TestFrame instance (created, not yet destroyed)
Definition: testframe.cpp:221
Mock data frame for simulated rendering.
Definition: testframe.hpp:60
Definition: run.hpp:49
allocated buffer, returned from client
TestFrame & testData(uint seqNr)
Helper to access a specific frame of test data at a fixed memory location.
Definition: testframe.cpp:155
Steam-Layer implementation namespace root.
Derived specific exceptions within Lumiera&#39;s exception hierarchy.
Definition: error.hpp:199
void resetTestFrames()
discards all the TestFrame instances and initialises an empty table of test frames ...
Definition: testframe.cpp:167
uint64_t generateDistinction(uint seq, uint family)
Definition: testframe.cpp:62
Lumiera error handling (C++ interface).
Unit test helper to generate fake test data frames.
static bool isDead(void *memLocation)
Helper to verify a given memory location holds an already destroyed TestFrame instance.
Definition: testframe.cpp:229