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)
5  2013, 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 
35 #include "lib/test/test-helper.hpp"
36 #include "lib/time/timevalue.hpp"
37 #include "vault/real-clock.hpp"
38 #include "lib/hash-combine.hpp"
39 #include "lib/null-value.hpp"
40 #include "lib/depend.hpp"
41 #include "lib/util.hpp"
42 
43 #include <functional>
44 #include <unordered_map>
45 
46 
47 namespace steam {
48 namespace engine{
49 namespace test {
50 
51  namespace { // MockJob and DummyClosure implementation details...
52 
53  using lib::rani;
54  using lib::HashVal;
55  using lib::NullValue;
56  using lib::time::TimeVar;
57  using std::unordered_map;
58  using util::access_or_default;
59 
60  using vault::gear::JobParameter;
61 
62 
63  const int MAX_PARAM_A(1000);
64  const int MAX_PARAM_B(10);
65 
66 
75  : public JobClosure
76  {
77  void
78  invokeJobOperation (JobParameter parameter) override
79  {
80  invocationLog_[hash_value (parameter)] = Invocation(parameter);
81  }
82 
83  JobKind
84  getJobKind() const override
85  {
86  return META_JOB;
87  }
88 
100  buildInstanceID (HashVal seed) const override
101  {
102  InvocationInstanceID instance;
103  instance.part.a = seed;
104  return instance;
105  }
106 
107  size_t
108  hashOfInstance (InvocationInstanceID invoKey) const override
109  {
110  std::hash<size_t> hashr;
111  HashVal res = hashr (invoKey.frameNumber);
112  lib::hash::combine (res, hashr (invoKey.part.t));
113  return res;
114  }
115 
116 
117  /* === Logging/Reporting of job invocation === */
118 
119  struct Invocation
120  {
121  TimeVar nominal;
122  TimeVar real;
123  int a,b;
124 
125  Invocation (JobParameter param)
126  : nominal{TimeValue(param.nominalTime)}
127  , real{RealClock::now()}
128  , a{param.invoKey.part.a}
129  , b{param.invoKey.part.b}
130  { }
131 
132  Invocation()
133  : nominal{Time::ANYTIME}
134  , real{Time::NEVER}
135  , a{MAX_PARAM_A}, b{0}
136  { }
137  };
138 
140  unordered_map<HashVal,Invocation> invocationLog_;
141 
142 
143  public:
144  Invocation const&
145  queryInvocation (JobParameter param) const
146  {
147  return access_or_default (invocationLog_, hash_value(param)
149  }
150 
151  void
152  clearLog()
153  {
154  invocationLog_.clear();
155  }
156  };
157 
158 
159 
162 
165 
166  }// (End)Implementation details
167 
168 
169 
170 
171 
172 
173 
174 
175  Job
177  {
178  InvocationInstanceID invoKey;
179  invoKey.part.a = rani (MAX_PARAM_A);
180  invoKey.part.b = rani (2*MAX_PARAM_B - MAX_PARAM_B);
181 
182  Time nominalTime = lib::test::randTime();
183 
184  return Job(dummyClosure, invoKey, nominalTime);
185  }
186 
187 
188  Job
189  MockJob::build (Time nominalTime, int additionalKey)
190  {
191  InvocationInstanceID invoKey;
192  invoKey.part.a = additionalKey;
193  invoKey.part.b = rani (2*MAX_PARAM_B - MAX_PARAM_B);
194 
195  return Job(dummyClosure, invoKey, nominalTime);
196  }
197 
198 
199  bool
200  MockJob::was_invoked (Job const& job)
201  {
202  REQUIRE (job.usesClosure (dummyClosure));
203 
204  return Time::NEVER != dummyClosure.queryInvocation(job.parameter).real;
205  }
206 
207 
208  Time
209  MockJob::invocationTime (Job const& job)
210  {
211  REQUIRE (job.usesClosure (dummyClosure));
212 
213  return dummyClosure.queryInvocation(job.parameter).real;
214  }
215 
216 
217  Time
218  MockJob::invocationNominalTime (Job const& job)
219  {
220  REQUIRE (job.usesClosure (dummyClosure));
221 
222  return dummyClosure.queryInvocation(job.parameter).nominal;
223  }
224 
225 
226  int
227  MockJob::invocationAdditionalKey (Job const& job)
228  {
229  REQUIRE (job.usesClosure (dummyClosure));
230 
231  return dummyClosure.queryInvocation(job.parameter).a;
232  }
233 
234 
236  JobClosure&
238  {
239  return dummyClosure;
240  }
241 
248  bool
249  MockJob::isNopJob (Job const& job)
250  {
251  InvocationInstanceID empty;
252  JobClosure& jobFunctor = static_cast<JobClosure&> (*job.jobClosure);
253  return lumiera_invokey_eq (&util::unConst(job).parameter.invoKey, &empty)
254  and util::isSameObject (jobFunctor, nopFunctor());
255  }
256 
257 
258 
259 }}} // namespace steam::engine::test
static const Time ANYTIME
border condition marker value. ANYTIME <= any time value
Definition: timevalue.hpp:313
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:232
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:40
void combine(size_t &combinedHash, size_t additionalHash)
meld the additional hash value into the given base hash value.
int rani(uint bound=_iBOUND())
Definition: random.hpp:135
Primary class template for std::hash.
Singleton holder for NIL or default value objects.
Definition: null-value.hpp:62
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:280
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:299
JobKind
Definition: job.h:62
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:103
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:52
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:314
static bool isNopJob(Job const &)
Interface of the closure for frame rendering jobs.
Definition: job.h:235
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:66
Individual frame rendering task, forwarding to a closure.
Definition: job.h:268
a family of time value like entities and their relationships.
Front-end for simplified access to the current wall clock time.
HashVal hash_value(ProcID const &procID)
generate registry hash value based on the distinct data in ProcID.
Definition: proc-node.cpp:105
basic constant internal time value.
Definition: timevalue.hpp:133