Lumiera  0.pre.03
»edityourfreedom«
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
dummy-job.cpp
Go to the documentation of this file.
1 /*
2  DummyJob - diagnostic job for unit tests
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 
29 
30 #include "lib/test/test-helper.hpp"
31 #include "lib/time/timevalue.hpp"
32 #include "backend/real-clock.hpp"
33 #include "lib/null-value.hpp"
34 #include "lib/hash-value.h"
35 #include "lib/util.hpp"
36 
37 #include <cstdlib>
38 #include <unordered_map>
39 #include <boost/functional/hash.hpp>
40 
41 
42 namespace backend{
43 namespace engine {
44 
45  namespace { // DummyJob implementation details...
46 
47  using lib::HashVal;
48  using lib::NullValue;
49  using lib::time::TimeVar;
50  using std::unordered_map;
52 
53 
54  const int MAX_PARAM_A(1000);
55  const int MAX_PARAM_B(10);
56 
57 
65  class DummyClosure
66  : public JobClosure
67  {
68  void
69  invokeJobOperation (JobParameter parameter)
70  {
71  invocationLog_[hash_value (parameter)] = Invocation(parameter);
72  }
73 
74  void
75  signalFailure (JobParameter,JobFailureReason)
76  {
77  NOTREACHED ("Job failure is not subject of this test");
78  }
79 
80  JobKind
81  getJobKind() const
82  {
83  return META_JOB;
84  }
85 
86  bool
87  verify (Time nominalJobTime, InvocationInstanceID invoKey) const
88  {
89  return Time::NEVER < nominalJobTime
90  && 0 <= invoKey.metaInfo.a
91  && invoKey.metaInfo.a < MAX_PARAM_A
92  && -MAX_PARAM_B <= invoKey.metaInfo.b
93  && invoKey.metaInfo.b < MAX_PARAM_B;
94  }
95 
96  size_t
97  hashOfInstance(InvocationInstanceID invoKey) const
98  {
99  return boost::hash_value (invoKey.metaInfo.a);
100  }
101 
102 
103  /* === Logging/Reporting of job invocation === */
104 
105  struct Invocation
106  {
107  TimeVar nominal;
108  TimeVar real;
109  int a,b;
110 
111  Invocation(JobParameter param)
112  : nominal(TimeValue(param.nominalTime))
113  , real(RealClock::now())
114  , a(param.invoKey.metaInfo.a)
115  , b(param.invoKey.metaInfo.b)
116  { }
117 
118  Invocation()
119  : nominal(Time::NEVER)
120  , real(Time::ANYTIME)
121  , a(MAX_PARAM_A), b(0)
122  { }
123  };
124 
126  unordered_map<HashVal,Invocation> invocationLog_;
127 
128 
129  public:
130  Invocation const&
131  queryInvocation (JobParameter param) const
132  {
133  return access_or_default (invocationLog_, hash_value(param)
134  ,NullValue<Invocation>::get());
135  }
136  };
137 
138 
139 
141  DummyClosure dummyClosure;
142 
143  }// (End)Implementation details
144 
145 
146 
147 
148 
149 
150 
151 
152  Job
154  {
155  InvocationInstanceID invoKey;
156  invoKey.metaInfo.a = rand() % MAX_PARAM_A;
157  invoKey.metaInfo.b = rand() % (2*MAX_PARAM_B) - MAX_PARAM_B;
158 
159  Time nominalTime = lib::test::randTime();
160 
161  return Job(dummyClosure, invoKey, nominalTime);
162  }
163 
164 
165  Job
166  DummyJob::build (Time nominalTime, int additionalKey)
167  {
168  InvocationInstanceID invoKey;
169  invoKey.metaInfo.a = additionalKey;
170  invoKey.metaInfo.b = rand() % (2*MAX_PARAM_B) - MAX_PARAM_B;
171 
172  return Job(dummyClosure, invoKey, nominalTime);
173  }
174 
175 
176  bool
178  {
179  REQUIRE (job.usesClosure (dummyClosure));
180 
181  return Time::NEVER != dummyClosure.queryInvocation(job.parameter).nominal;
182  }
183 
184 
185  Time
187  {
188  REQUIRE (job.usesClosure (dummyClosure));
189 
190  return dummyClosure.queryInvocation(job.parameter).real;
191  }
192 
193 
194 }} // namespace backend::engine
a mutable time value, behaving like a plain number, allowing copy and re-accessing ...
Definition: timevalue.hpp:142
JobFailureReason
Definition: job.h:88
bool usesClosure(JobClosure const &) const
Definition: job.cpp:116
NoUsableHashDefinition hash_value(...)
declared for metaprogramming only, never defined
Singleton holder for NIL or default value objects.
Definition: null-value.hpp:72
MAP::mapped_type const & access_or_default(MAP &map, typename MAP::key_type const &key, typename MAP::mapped_type const &refDefault)
expose a reference to a map entry, with fall-back to some default object
Definition: util.hpp:258
static bool was_invoked(Job const &job)
Definition: dummy-job.cpp:177
Individual frame rendering task, forwarding to a closure.
Definition: job.h:248
Singleton-style holder for NIL or default values.
Lumiera's internal time value datatype.
Definition: timevalue.hpp:226
JobKind
Definition: job.h:69
lumiera_jobParameter const & JobParameter
Definition: job.h:198
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
Definition: dummy-job.cpp:153
static Time invocationTime(Job const &job)
Definition: dummy-job.cpp:186
opaque ID attached to each individual job invocation.
Definition: job.h:113
Unit test helper to generate dummy render jobs.
Hash value types and utilities.
size_t HashVal
a STL compatible hash value
Definition: hash-value.h:56
static const Time NEVER
border condition marker value. NEVER >= any time value
Definition: timevalue.hpp:241
HashVal hash_value(Job const &job)
hash value based on all relevant job data.
Definition: job.cpp:131
lib::time::Time randTime()
create a random but not insane Time value
render process self organisation
Definition: job.h:73
a family of time value like entities and their relationships.
Front-end for simplified access to the current wall clock time.
struct InvocationInstanceID::@0 metaInfo