Lumiera  0.pre.03
»edit your freedom«
time-quantisation-test.cpp
Go to the documentation of this file.
1 /*
2  TimeQuantisation(Test) - handling of virtually grid aligned time values
3 
4  Copyright (C) Lumiera.org
5  2010, 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/test/run.hpp"
29 #include "lib/test/test-helper.hpp"
31 #include "lib/time/timequant.hpp"
32 #include "lib/format-cout.hpp"
33 #include "lib/util.hpp"
34 
35 #include <boost/lexical_cast.hpp>
36 #include <cstdlib>
37 
38 using boost::lexical_cast;
39 using util::isnil;
40 using util::contains;
41 using std::rand;
42 
43 
44 namespace lib {
45 namespace time{
46 namespace test{
47 
49 
50 
51  /****************************************************/
60  class TimeQuantisation_test : public Test
61  {
62  int
63  random_or_get (Arg arg)
64  {
65  if (isnil(arg))
66  return 1 + (rand() % 10000);
67  else
68  return lexical_cast<int> (arg[1]);
69  }
70 
71 
72 
73  virtual void
74  run (Arg arg)
75  {
76  Time ref (0,random_or_get(arg),0,0);
77  CHECK (TimeValue(0) < ref);
78 
79  checkSimpleUsage (ref);
80  check_theFullStory (ref);
81  checkMultipleGrids (ref);
82  checkGridBinding (ref);
83  }
84 
85 
86  void
87  checkSimpleUsage (TimeValue org)
88  {
89  TimeGrid::build("my_simple_grid", 25); // "someone" has defined a time grid
90 
91  QuTime qVal (org, "my_simple_grid"); // create time quantised to this grid
92 
93  FrameNr count(qVal); // materialise this quantised time into..
94  int n = count; // frame count, accessible as plain number
95 
96  CHECK (Time(FSecs(n-1, 25)) <= org); // verify quantisation: the original time
97  CHECK (org < Time(FSecs(n+1, 25))); // is properly bracketed by (n-1, n+1)
98  }
99 
100 
101  void
102  check_theFullStory (TimeValue org)
103  {
104  PQuant fixQ (new FixedFrameQuantiser(25));
105  QuTime qVal (org, fixQ);
106 
107  CHECK ( qVal.supports<format::Frames>());
108  CHECK ( qVal.supports<format::Smpte>());
109 
110  SmpteTC smpteTCode = qVal.formatAs<format::Smpte>();
111  showTimeCode (smpteTCode);
112 
113  HmsTC pureTimeCode = qVal.formatAs<format::Hms>();
114  showTimeCode (pureTimeCode);
115 
116  FrameNr frameTCode = qVal.formatAs<format::Frames>();
117  showTimeCode (frameTCode);
118 
119  Secs seconds = qVal.formatAs<format::Seconds>();
120  showTimeCode (seconds);
121  }
122 
123  template<class TC>
124  void
125  showTimeCode (TC timecodeValue)
126  {
127  cout << timecodeValue.describe()
128  << " time = "<< timecodeValue.getTime()
129  << " code = "<< timecodeValue
130  << endl;
131  }
132 
133 
134  void
135  checkMultipleGrids (TimeValue org)
136  {
137  TimeGrid::build("my_alternate_grid", FrameRate::NTSC);
138 
139  QuTime palVal (org, "my_simple_grid");
140  QuTime ntscVal (org, "my_alternate_grid");
141 
142  CHECK (org == palVal);
143  CHECK (org == ntscVal);
144 
145  FrameNr palNr (palVal);
146  FrameNr ntscNr(ntscVal);
147  CHECK (palNr <= ntscNr);
148  }
149 
150 
151  void
152  checkGridBinding (TimeValue org)
153  {
154  // refer to a grid not yet defined
155  VERIFY_ERROR (UNKNOWN_GRID, QuTime weird(org, "special_funny_grid"));
156 
157  TimeGrid::build("special_funny_grid", 1); // provide the grid's definition (1 frame per second)
158 
159  QuTime funny (org, "special_funny_grid"); // now OK, grid is known
160  int cnt = funny.formatAs<format::Frames>();
161  // and now performing quantisation is OK
162  SmpteTC smpte (funny); // also converting into SMPTE (which implies frame quantisation)
163  CHECK (0 == smpte.frames); // we have 1fps, thus the frame part is always zero!
164  CHECK (cnt % 60 == smpte.secs); // and the seconds part will be in sync with the frame count
165  }
166  };
167 
168 
170  LAUNCHER (TimeQuantisation_test, "unit common");
171 
172 
173 
174 }}} // namespace lib::time::test
Interface: a grid and scale definition for time quantisation.
Definition: time-grid.hpp:86
Frame count as timecode format.
Definition: formats.hpp:84
Automatically use custom string conversion in C++ stream output.
Classical Timecode value reminiscent to SMPTE format.
Definition: timecode.hpp:150
The informal hours-minutes-seconds-millisecond timecode.
Definition: formats.hpp:117
Definition: run.hpp:49
Widely used standard media timecode format.
Definition: formats.hpp:99
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify that a statement indeed raises an exception.
static PGrid build(FrameRate frames_per_second)
Definition: time-grid.cpp:167
Implementation namespace for support and library code.
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:308
format::Traits< FMT >::TimeCode formatAs() const
create new time code instance, then castInto
Definition: timequant.hpp:147
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Simple timecode specification as fractional seconds.
Definition: formats.hpp:135
boost::rational< int64_t > FSecs
rational representation of fractional seconds
Definition: timevalue.hpp:229
A collection of frequently used helper functions to support unit testing.
Support library to represent grid-aligned time specifications This is part of Lumiera&#39;s time and time...
To establish a reference scale for quantised time values.
bool supports() const
does our implicit time grid support building that timecode format?
Definition: timequant.hpp:139
A frame counting timecode value.
Definition: timecode.hpp:105
basic constant internal time value.
Definition: timevalue.hpp:142
Simple stand-alone Quantiser implementation based on a constant sized gird.
Definition: quantiser.hpp:144
bool contains(SEQ const &cont, typename SEQ::const_reference val)
shortcut for brute-force containment test in any sequential container
Definition: util.hpp:255
grid aligned time specification, referring to a specific scale.
Definition: timequant.hpp:99