Lumiera  0.pre.03
»edit your freedom«
buffer-provider-protocol-test.cpp
Go to the documentation of this file.
1 /*
2  BufferProviderProtocol(Test) - demonstration of buffer provider usage cycle
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/error.hpp"
29 #include "lib/test/run.hpp"
30 #include "lib/test/test-helper.hpp"
31 #include "lib/test/testdummy.hpp"
32 #include "lib/util-foreach.hpp"
37 
38 using util::isSameObject;
39 using util::for_each;
40 
41 
42 namespace steam {
43 namespace engine{
44 namespace test {
45 
46  using lib::test::Dummy;
47 
49  using LERR_(LIFECYCLE);
50  using LERR_(LOGIC);
51 
52 
53  namespace { // Test fixture
54 
55  const uint TEST_SIZE = 1024*1024;
56  const uint TEST_ELMS = 20;
57 
58 
59  void
60  do_some_calculations (BuffHandle const& buffer)
61  {
62  UNIMPLEMENTED ("some verifiable test/dummy buffer accessing operations");
63  }
64 
65  }
66 
67 
68  /**************************************************************************/
79  class BufferProviderProtocol_test : public Test
80  {
81  virtual void
82  run (Arg)
83  {
84  verifySimpleUsage();
85  verifyStandardCase();
86  verifyObjectAttachment();
87  verifyObjectAttachmentFailure();
88  }
89 
90 
91  void
92  verifySimpleUsage()
93  {
94  // Create Test fixture.
95  // In real usage, a suitable memory/frame/buffer provider
96  // will be preconfigured, depending on the usage context
98 
99  BuffHandle buff = provider.lockBufferFor<TestFrame>();
100  CHECK (buff.isValid());
101  CHECK (sizeof(TestFrame) <= buff.size());
102  buff.accessAs<TestFrame>() = testData(0);
103 
104  TestFrame& content = buff.accessAs<TestFrame>();
105  CHECK (testData(0) == content);
106 
107  buff.emit();
108  buff.release();
109  CHECK (!buff.isValid());
110  VERIFY_ERROR (LIFECYCLE, buff.accessAs<TestFrame>() );
111 
113  CHECK (checker.buffer_was_used (0));
114  CHECK (checker.buffer_was_closed (0));
115 
116  CHECK (testData(0) == checker.accessMemory (0));
117  }
118 
119 
120  void
121  verifyStandardCase()
122  {
123 #if false
124  // Create Test fixture.
125  // In real usage, a suitable memory/frame/buffer provider
126  // will be preconfigured, depending on the usage context
128 
129  BufferDescriptor desc1 = provider.getDescriptor<TestFrame>(); // note: implies also sizeof(TestFrame)
130  BufferDescriptor desc2 = provider.getDescriptorFor(TEST_SIZE);
131  CHECK (desc1.verifyValidity());
132  CHECK (desc2.verifyValidity());
133 
134  uint num1 = provider.announce(TEST_ELMS, desc1);
135  uint num2 = provider.announce(TEST_ELMS, desc2);
136  CHECK (num1 == TEST_ELMS);
137  CHECK (0 < num2 && num2 <=TEST_ELMS);
138 
139  const size_t STORAGE_SIZE = BuffTable::Storage<2*TEST_ELMS>::size;
140  char storage[STORAGE_SIZE];
141  BuffTable& tab =
142  BuffTable::prepare(STORAGE_SIZE, storage)
143  .announce(num1, desc1)
144  .announce(num2, desc2)
145  .build();
146 
147  tab.lockBuffers();
148  for_each (tab.buffers(), do_some_calculations);
149  tab.releaseBuffers();
150 
152  CHECK (checker.all_buffers_released());
153 #endif
154  }
155 
156 
157  void
158  verifyObjectAttachment()
159  {
161  BufferDescriptor type_A = provider.getDescriptorFor(sizeof(TestFrame));
162  BufferDescriptor type_B = provider.getDescriptorFor(sizeof(int));
163  BufferDescriptor type_C = provider.getDescriptor<int>();
164 
165  BuffHandle handle_A = provider.lockBuffer(type_A);
166  BuffHandle handle_B = provider.lockBuffer(type_B);
167  BuffHandle handle_C = provider.lockBuffer(type_C);
168 
169  CHECK (handle_A);
170  CHECK (handle_B);
171  CHECK (handle_C);
172 
173  CHECK (sizeof(TestFrame) == handle_A.size());
174  CHECK (sizeof( int ) == handle_B.size());
175  CHECK (sizeof( int ) == handle_C.size());
176 
177  TestFrame& embeddedFrame = handle_A.create<TestFrame>();
178  CHECK (isSameObject (*handle_A, embeddedFrame));
179  CHECK (embeddedFrame.isAlive());
180  CHECK (embeddedFrame.isSane());
181 
182  VERIFY_ERROR (LOGIC, handle_B.create<TestFrame>()); // too small to hold a TestFrame
183  VERIFY_ERROR (LIFECYCLE, handle_C.create<int>()); // has already an attached TypeHandler (creating an int)
184 
185  handle_A.release();
186  handle_B.release();
187  handle_C.release();
188 
189  CHECK (embeddedFrame.isDead());
190  CHECK (embeddedFrame.isSane());
191  }
192 
193 
194  void
195  verifyObjectAttachmentFailure()
196  {
198  BufferDescriptor type_D = provider.getDescriptorFor(sizeof(Dummy));
199 
200  Dummy::checksum() = 0;
201  BuffHandle handle_D = provider.lockBuffer(type_D);
202  CHECK (0 == Dummy::checksum()); // nothing created thus far
203 
204  handle_D.create<Dummy>();
205  CHECK (0 < Dummy::checksum());
206 
207  handle_D.release();
208  CHECK (0 == Dummy::checksum());
209 
210  BuffHandle handle_DD = provider.lockBuffer(type_D);
211 
212  CHECK (0 == Dummy::checksum());
213  Dummy::activateCtorFailure();
214 
215  CHECK (handle_DD.isValid());
216  try
217  {
218  handle_DD.create<Dummy>();
219  NOTREACHED ("Dummy ctor should fail");
220  }
221  catch (int val)
222  {
223  CHECK (!handle_DD.isValid());
224 
225  CHECK (0 < Dummy::checksum());
226  CHECK (val == Dummy::checksum());
227  }
228 
229  VERIFY_ERROR (LIFECYCLE, handle_DD.accessAs<Dummy>() );
230  VERIFY_ERROR (LIFECYCLE, handle_DD.create<Dummy>() );
231  }
232  };
233 
234 
236  LAUNCHER (BufferProviderProtocol_test, "unit player");
237 
238 
239 
240 }}} // namespace steam::engine::test
Helper for unit tests: Buffer provider reference implementation.
Mock data frame for simulated rendering.
Definition: testframe.hpp:60
Definition: run.hpp:49
BuffHandle lockBufferFor()
convenience shortcut: prepare and claim ("lock") a buffer suitable to hold an object of the given typ...
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
static DiagnosticBufferProvider & access(BufferProvider const &)
access the diagnostic API of the buffer provider
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.
unittest helper code: test dummy objects to track instances.
BU & accessAs()
convenience shortcut: access the buffer contents casted to a specific type.
helper for organisation of render data buffers Used during the process of _"pulling"_ a render node...
BU & create()
convenience shortcut: place and maintain an object within the buffer.
uint announce(uint count, BufferDescriptor const &)
BufferProvider API: declare in advance the need for working buffers.
Simple test class runner.
BufferDescriptor getDescriptorFor(size_t storageSize=0)
describe the kind of buffer managed by this provider
An opaque descriptor to identify the type and further properties of a data buffer.
Definition: buffhandle.hpp:85
void for_each(CON const &elements, FUN function, P1 &&bind1, ARGS &&...args)
Accept binding for arbitrary function arguments.
Extension to allow placing objects right into the buffers, taking ownership.
A collection of frequently used helper functions to support unit testing.
A Dummy object for tests.
Definition: testdummy.hpp:50
BuffHandle lockBuffer(BufferDescriptor const &)
BufferProvider API: retrieve a single buffer for exclusive use.
Lumiera error handling (C++ interface).
Handle for a buffer for processing data, abstracting away the actual implementation.
Definition: buffhandle.hpp:115
A facility for writing unit-tests targeting the BufferProvider interface.
static BufferProvider & build()
build a new Diagnostic Buffer Provider instance, discard the existing one.
Interface: a facility providing and managing working buffers for media calculations.
Obsolete, to be rewritten /////TICKET #826.
Unit test helper to generate fake test data frames.
Perform operations "for each element" of a collection.
BufferDescriptor getDescriptor()
define a "buffer type" for automatically creating an instance of the template type embedded into the ...
bool isSameObject(A const &a, B const &b)
compare plain object identity, bypassing any custom comparison operators.
Definition: util.hpp:372