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)
5  2011, 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 
19 #include "lib/error.hpp"
20 #include "lib/test/run.hpp"
21 #include "lib/test/test-helper.hpp"
23 #include "lib/util-foreach.hpp"
28 
29 using util::isSameObject;
30 using util::for_each;
31 
32 
33 namespace steam {
34 namespace engine{
35 namespace test {
36 
37  using lib::test::Dummy;
38 
40  using LERR_(LIFECYCLE);
41  using LERR_(LOGIC);
42 
43 
44  namespace { // Test fixture
45 
46  const uint TEST_SIZE = 1024*1024;
47  const uint TEST_ELMS = 20;
48 
49 
50  void
51  do_some_calculations (BuffHandle const& buffer)
52  {
53  UNIMPLEMENTED ("some verifiable test/dummy buffer accessing operations");
54  }
55 
56  }
57 
58 
59  /**************************************************************************/
70  class BufferProviderProtocol_test : public Test
71  {
72  virtual void
73  run (Arg)
74  {
75  verifySimpleUsage();
76  verifyStandardCase();
77  verifyObjectAttachment();
78  verifyObjectAttachmentFailure();
79  }
80 
81 
82  void
83  verifySimpleUsage()
84  {
85  // Create Test fixture.
86  // In real usage, a suitable memory/frame/buffer provider
87  // will be preconfigured, depending on the usage context
89 
90  BuffHandle buff = provider.lockBufferFor<TestFrame>();
91  CHECK (buff.isValid());
92  CHECK (sizeof(TestFrame) <= buff.size());
93  buff.accessAs<TestFrame>() = testData(0);
94 
95  TestFrame& content = buff.accessAs<TestFrame>();
96  CHECK (testData(0) == content);
97 
98  buff.emit();
99  buff.release();
100  CHECK (!buff.isValid());
101  VERIFY_ERROR (LIFECYCLE, buff.accessAs<TestFrame>() );
102 
104  CHECK (checker.buffer_was_used (0));
105  CHECK (checker.buffer_was_closed (0));
106 
107  CHECK (testData(0) == checker.accessMemory (0));
108  }
109 
110 
111  void
112  verifyStandardCase()
113  {
114 #if false
115  // Create Test fixture.
116  // In real usage, a suitable memory/frame/buffer provider
117  // will be preconfigured, depending on the usage context
119 
120  BuffDescr desc1 = provider.getDescriptor<TestFrame>(); // note: implies also sizeof(TestFrame)
121  BuffDescr desc2 = provider.getDescriptorFor(TEST_SIZE);
122  CHECK (desc1.verifyValidity());
123  CHECK (desc2.verifyValidity());
124 
125  uint num1 = provider.announce(TEST_ELMS, desc1);
126  uint num2 = provider.announce(TEST_ELMS, desc2);
127  CHECK (num1 == TEST_ELMS);
128  CHECK (0 < num2 && num2 <=TEST_ELMS);
129 
130  const size_t STORAGE_SIZE = BuffTable::Storage<2*TEST_ELMS>::size;
131  char storage[STORAGE_SIZE];
132  BuffTable& tab =
133  BuffTable::prepare(STORAGE_SIZE, storage)
134  .announce(num1, desc1)
135  .announce(num2, desc2)
136  .build();
137 
138  tab.lockBuffers();
139  for_each (tab.buffers(), do_some_calculations);
140  tab.releaseBuffers();
141 
143  CHECK (checker.all_buffers_released());
144 #endif
145  }
146 
147 
148  void
149  verifyObjectAttachment()
150  {
152  BuffDescr type_A = provider.getDescriptorFor(sizeof(TestFrame));
153  BuffDescr type_B = provider.getDescriptorFor(sizeof(int));
154  BuffDescr type_C = provider.getDescriptor<int>();
155 
156  BuffHandle handle_A = provider.lockBuffer(type_A);
157  BuffHandle handle_B = provider.lockBuffer(type_B);
158  BuffHandle handle_C = provider.lockBuffer(type_C);
159 
160  CHECK (handle_A);
161  CHECK (handle_B);
162  CHECK (handle_C);
163 
164  CHECK (sizeof(TestFrame) == handle_A.size());
165  CHECK (sizeof( int ) == handle_B.size());
166  CHECK (sizeof( int ) == handle_C.size());
167 
168  TestFrame& embeddedFrame = handle_A.create<TestFrame>();
169  CHECK (isSameObject (*handle_A, embeddedFrame));
170  CHECK (embeddedFrame.isAlive());
171  CHECK (embeddedFrame.isSane());
172 
173  VERIFY_ERROR (LOGIC, handle_B.create<TestFrame>()); // too small to hold a TestFrame
174  VERIFY_ERROR (LIFECYCLE, handle_C.create<int>()); // has already an attached TypeHandler (creating an int)
175 
176  handle_A.release();
177  handle_B.release();
178  handle_C.release();
179 
180  CHECK (embeddedFrame.isDead());
181  CHECK (embeddedFrame.isSane());
182  }
183 
184 
185  void
186  verifyObjectAttachmentFailure()
187  {
189  BuffDescr type_D = provider.getDescriptorFor(sizeof(Dummy));
190 
191  Dummy::checksum() = 0;
192  BuffHandle handle_D = provider.lockBuffer(type_D);
193  CHECK (0 == Dummy::checksum()); // nothing created thus far
194 
195  handle_D.create<Dummy>();
196  CHECK (0 < Dummy::checksum());
197 
198  handle_D.release();
199  CHECK (0 == Dummy::checksum());
200 
201  BuffHandle handle_DD = provider.lockBuffer(type_D);
202 
203  CHECK (0 == Dummy::checksum());
204  Dummy::activateCtorFailure();
205 
206  CHECK (handle_DD.isValid());
207  try
208  {
209  handle_DD.create<Dummy>();
210  NOTREACHED ("Dummy ctor should fail");
211  }
212  catch (int val)
213  {
214  CHECK (!handle_DD.isValid());
215 
216  CHECK (0 < Dummy::checksum());
217  CHECK (val == Dummy::checksum());
218  }
219 
220  VERIFY_ERROR (LIFECYCLE, handle_DD.accessAs<Dummy>() );
221  VERIFY_ERROR (LIFECYCLE, handle_DD.create<Dummy>() );
222  }
223  };
224 
225 
227  LAUNCHER (BufferProviderProtocol_test, "unit player");
228 
229 
230 
231 }}} // namespace steam::engine::test
Helper for unit tests: Buffer provider reference implementation.
uint announce(uint count, BuffDescr const &)
BufferProvider API: declare in advance the need for working buffers.
Mock data frame for simulated rendering.
Definition: testframe.hpp:68
Definition: run.hpp:40
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
An opaque descriptor to identify the type and further properties of a data buffer.
Definition: buffhandle.hpp:77
static DiagnosticBufferProvider & access(BufferProvider const &)
access the diagnostic API of the buffer provider
Steam-Layer implementation namespace root.
BuffDescr getDescriptorFor(size_t storageSize=0)
describe the kind of buffer managed by this provider
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.
BuffDescr getDescriptor(ARGS ...args)
define a "buffer type" for automatically creating an instance of the template type embedded into the ...
Simplistic test class runner.
unittest helper code: test dummy objects to track instances.
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.
BuffHandle lockBuffer(BuffDescr const &)
BufferProvider API: retrieve a single buffer for exclusive use.
A collection of frequently used helper functions to support unit testing.
A Dummy object for tests.
Lumiera error handling (C++ interface).
BuffHandle lockBufferFor(ARGS ...args)
convenience shortcut: prepare and claim ("lock") a buffer suitable to hold an object of the given typ...
Handle for a buffer for processing data, abstracting away the actual implementation.
Definition: buffhandle.hpp:111
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.
Tables of buffer handles and corresponding dereferenced buffer pointers.
Definition: bufftable.hpp:57
Unit test helper to generate fake test data frames.
TestFrame & testData(uint seqNr, uint chanNr)
Helper to access a specific frame of test data at a fixed memory location.
Definition: testframe.cpp:186
Perform operations "for each element" of a collection.
bool isSameObject(A const &a, B const &b)
compare plain object identity, based directly on the referee&#39;s memory identities. ...
Definition: util.hpp:421