Lumiera  0.pre.03
»edit your freedom«
testframe-test.cpp
Go to the documentation of this file.
1 /*
2  TestFrame(Test) - verify proper operation of dummy data frames
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 
28 #include "lib/test/run.hpp"
30 #include "lib/util.hpp"
31 
32 #include <cstdlib>
33 #include <memory>
34 
35 using test::Test;
36 using std::rand;
37 using util::isSameObject;
38 using std::unique_ptr;
39 
40 
41 namespace steam {
42 namespace engine{
43 namespace test {
44 
45  namespace { // used internally
46 
47  const uint CHAN_COUNT = 30; // independent families of test frames to generate
48  const uint NUM_FRAMES = 1000; // number of test frames in each of these families
49 
50 
51  void
52  corruptMemory(void* base, uint offset, uint count)
53  {
54  char* accessor = reinterpret_cast<char*> (base);
55  while (count--)
56  accessor[offset+count] = rand() % CHAR_MAX;
57  }
58  } // (End) internal defs
59 
60 
61 
62  /***************************************************************/
76  class TestFrame_test : public Test
77  {
78 
79  virtual void
80  run (Arg)
81  {
82  verifyBasicProperties();
83  verifyFrameLifecycle();
85  useFrameTable();
86  }
87 
88 
89  void
90  verifyBasicProperties()
91  {
92  CHECK (1024 < sizeof(TestFrame));
93 
94  TestFrame frameA;
95  TestFrame frameB;
96  TestFrame frameC(5);
97 
98  CHECK (frameA == frameB);
99  CHECK (frameA != frameC);
100  CHECK (frameB != frameC);
101 
102  CHECK (frameA.isAlive());
103  CHECK (frameB.isAlive());
104  CHECK (frameC.isAlive());
105 
106  CHECK (frameA.isSane());
107  CHECK (frameB.isSane());
108  CHECK (frameC.isSane());
109 
110  void * frameMem = &frameB;
111 
112  CHECK (frameA == frameMem);
113  corruptMemory(frameMem,20,5);
114  CHECK (!frameB.isSane());
115 
116  frameB = frameC;
117 
118  CHECK (frameB.isSane());
119  CHECK (frameA != frameB);
120  CHECK (frameA != frameC);
121  CHECK (frameB == frameC);
122  }
123 
124 
125  void
126  verifyFrameLifecycle()
127  {
128  CHECK (!TestFrame::isDead (this));
129  CHECK (!TestFrame::isAlive (this));
130 
131  static char buffer[sizeof(TestFrame)];
132  TestFrame* frame = new(&buffer) TestFrame(23);
133 
134  CHECK ( TestFrame::isAlive (frame));
135  CHECK (!frame->isDead());
136  CHECK (frame->isAlive());
137  CHECK (frame->isSane());
138 
139  frame->~TestFrame();
140  CHECK ( TestFrame::isDead (frame));
141  CHECK (!TestFrame::isAlive (frame));
142  }
143 
144 
149  void
151  {
152  unique_ptr<TestFrame> thisFrames[CHAN_COUNT];
153  unique_ptr<TestFrame> prevFrames[CHAN_COUNT];
154 
155  for (uint i=0; i<CHAN_COUNT; ++i)
156  thisFrames[i].reset (new TestFrame(0, i));
157 
158  for (uint nr=1; nr<NUM_FRAMES; ++nr)
159  for (uint i=0; i<CHAN_COUNT; ++i)
160  {
161  thisFrames[i].swap (prevFrames[i]);
162  thisFrames[i].reset (new TestFrame(nr, i));
163  CHECK (thisFrames[i]->isSane());
164  CHECK (prevFrames[i]->isSane());
165  CHECK (prevFrames[i]->isAlive());
166 
167  CHECK (*thisFrames[i] != *prevFrames[i]); // differs from predecessor within the same channel
168 
169  for (uint j=0; j<i; ++j)
170  {
171  ENSURE (j!=i);
172  CHECK (*thisFrames[i] != *thisFrames[j]); // differs from frames in other channels at this point
173  CHECK (*thisFrames[i] != *prevFrames[j]); // differs cross wise from predecessors in other channels
174  } } }
175 
176 
179  void
181  {
182  TestFrame& frX = testData(3,50);
183  TestFrame& frY = testData(3,25);
184  TestFrame& frZ = testData(3,50);
185 
186  CHECK (frX.isSane());
187  CHECK (frY.isSane());
188  CHECK (frZ.isSane());
189 
190  CHECK (frX != frY);
191  CHECK (frX == frZ);
192  CHECK (frY != frZ);
193 
194  CHECK (isSameObject (frX, frZ));
195 
196  corruptMemory(&frZ,40,20);
197  CHECK (!frX.isSane());
198  CHECK (!testData(3,50).isSane());
199  CHECK ( testData(3,51).isSane());
200  CHECK ( testData(3,49).isSane());
201 
202  resetTestFrames();
203 
204  CHECK ( testData(3,50).isSane());
205  }
206  };
207 
208 
210  LAUNCHER (TestFrame_test, "unit engine");
211 
212 
213 
214 }}} // 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
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.
Abstract Base Class for all testcases.
Definition: run.hpp:62
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
void resetTestFrames()
discards all the TestFrame instances and initialises an empty table of test frames ...
Definition: testframe.cpp:167
Unit test helper to generate fake test data frames.
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:347