Lumiera  0.pre.03
»edit your freedom«
mock-dispatcher.cpp
Go to the documentation of this file.
1 /*
2  MockDispatcher - diagnostic render job and frame dispatcher
3 
4  Copyright (C) Lumiera.org
5  2013, 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 
44 #include "lib/test/test-helper.hpp"
45 #include "lib/time/timevalue.hpp"
46 #include "vault/real-clock.hpp"
47 #include "lib/null-value.hpp"
48 #include "lib/hash-value.h"
49 #include "lib/depend.hpp"
50 #include "lib/util.hpp"
51 
52 #include <cstdlib>
53 #include <functional>
54 #include <unordered_map>
55 
56 
57 namespace steam {
58 namespace engine{
59 namespace test {
60 
61  namespace { // MockJob and DummyClosure implementation details...
62 
63  using lib::HashVal;
64  using lib::NullValue;
65  using lib::time::TimeVar;
66  using std::unordered_map;
67  using util::access_or_default;
68 
69  using vault::gear::JobParameter;
70 
71 
72  const int MAX_PARAM_A(1000);
73  const int MAX_PARAM_B(10);
74 
75 
84  : public JobClosure
85  {
86  void
87  invokeJobOperation (JobParameter parameter) override
88  {
89  invocationLog_[hash_value (parameter)] = Invocation(parameter);
90  }
91 
92  JobKind
93  getJobKind() const override
94  {
95  return META_JOB;
96  }
97 
109  buildInstanceID (HashVal seed) const override
110  {
111  InvocationInstanceID instance;
112  instance.part.a = seed;
113  return instance;
114  }
115 
116  size_t
117  hashOfInstance (InvocationInstanceID invoKey) const override
118  {
119  std::hash<size_t> hashr;
120  HashVal res = hashr (invoKey.frameNumber);
121  lib::hash::combine (res, hashr (invoKey.part.t));
122  return res;
123  }
124 
125 
126  /* === Logging/Reporting of job invocation === */
127 
128  struct Invocation
129  {
130  TimeVar nominal;
131  TimeVar real;
132  int a,b;
133 
134  Invocation (JobParameter param)
135  : nominal{TimeValue(param.nominalTime)}
136  , real{RealClock::now()}
137  , a{param.invoKey.part.a}
138  , b{param.invoKey.part.b}
139  { }
140 
141  Invocation()
142  : nominal{Time::ANYTIME}
143  , real{Time::NEVER}
144  , a{MAX_PARAM_A}, b{0}
145  { }
146  };
147 
149  unordered_map<HashVal,Invocation> invocationLog_;
150 
151 
152  public:
153  Invocation const&
154  queryInvocation (JobParameter param) const
155  {
156  return access_or_default (invocationLog_, hash_value(param)
158  }
159 
160  void
161  clearLog()
162  {
163  invocationLog_.clear();
164  }
165  };
166 
167 
168 
171 
174 
175  }// (End)Implementation details
176 
177 
178 
179 
180 
181 
182 
183 
184  Job
186  {
187  InvocationInstanceID invoKey;
188  invoKey.part.a = rand() % MAX_PARAM_A;
189  invoKey.part.b = rand() % (2*MAX_PARAM_B) - MAX_PARAM_B;
190 
191  Time nominalTime = lib::test::randTime();
192 
193  return Job(dummyClosure, invoKey, nominalTime);
194  }
195 
196 
197  Job
198  MockJob::build (Time nominalTime, int additionalKey)
199  {
200  InvocationInstanceID invoKey;
201  invoKey.part.a = additionalKey;
202  invoKey.part.b = rand() % (2*MAX_PARAM_B) - MAX_PARAM_B;
203 
204  return Job(dummyClosure, invoKey, nominalTime);
205  }
206 
207 
208  bool
209  MockJob::was_invoked (Job const& job)
210  {
211  REQUIRE (job.usesClosure (dummyClosure));
212 
213  return Time::NEVER != dummyClosure.queryInvocation(job.parameter).real;
214  }
215 
216 
217  Time
218  MockJob::invocationTime (Job const& job)
219  {
220  REQUIRE (job.usesClosure (dummyClosure));
221 
222  return dummyClosure.queryInvocation(job.parameter).real;
223  }
224 
225 
226  Time
227  MockJob::invocationNominalTime (Job const& job)
228  {
229  REQUIRE (job.usesClosure (dummyClosure));
230 
231  return dummyClosure.queryInvocation(job.parameter).nominal;
232  }
233 
234 
235  int
236  MockJob::invocationAdditionalKey (Job const& job)
237  {
238  REQUIRE (job.usesClosure (dummyClosure));
239 
240  return dummyClosure.queryInvocation(job.parameter).a;
241  }
242 
243 
245  JobClosure&
247  {
248  return dummyClosure;
249  }
250 
257  bool
258  MockJob::isNopJob (Job const& job)
259  {
260  InvocationInstanceID empty;
261  JobClosure& jobFunctor = static_cast<JobClosure&> (*job.jobClosure);
262  return lumiera_invokey_eq (&util::unConst(job).parameter.invoKey, &empty)
263  and util::isSameObject (jobFunctor, nopFunctor());
264  }
265 
266 
267 
268 }}} // namespace steam::engine::test
static const Time ANYTIME
border condition marker value. ANYTIME <= any time value
Definition: timevalue.hpp:322
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:241
Mock data structures to support implementation testing of render job planning and frame dispatch...
static JobClosure & getFunctor()
Generic implementation of a JobFunctor to perform no calculations.
Definition: run.hpp:49
void combine(size_t &combinedHash, size_t additionalHash)
meld the additional hash value into the given base hash value.
Definition: hash-value.h:71
Primary class template for std::hash.
Singleton holder for NIL or default value objects.
Definition: null-value.hpp:71
unordered_map< HashVal, Invocation > invocationLog_
recording MockJob invocations
Singleton-style holder for NIL or default values.
Steam-Layer implementation namespace root.
Access point to singletons and other kinds of dependencies designated by type.
Definition: depend.hpp:289
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:308
JobKind
Definition: job.h:71
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
A collection of frequently used helper functions to support unit testing.
static Job build()
uses random job definition values
DummyClosure dummyClosure
actual instance of the test dummy job functor
Singleton services and Dependency Injection.
lib::Depend< vault::gear::NopJobFunctor > nopFunctor
access to the fallback-implementation for empty segments
opaque ID attached to each individual job invocation.
Definition: job.h:112
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
const int MAX_PARAM_A(1000)
random test values 0...1000
static const Time NEVER
border condition marker value. NEVER >= any time value
Definition: timevalue.hpp:323
static bool isNopJob(Job const &)
Interface of the closure for frame rendering jobs.
Definition: job.h:244
const int MAX_PARAM_B(10)
random test values -10...+10
InvocationInstanceID buildInstanceID(HashVal seed) const override
Generate a specifically marked invocationKey for use in unit-tests.
render process self organisation
Definition: job.h:75
Individual frame rendering task, forwarding to a closure.
Definition: job.h:277
a family of time value like entities and their relationships.
Front-end for simplified access to the current wall clock time.
basic constant internal time value.
Definition: timevalue.hpp:142