Lumiera  0.pre.03
»edit your freedom«
time-parsing-test.cpp
Go to the documentation of this file.
1 /*
2  TimeParsing(Test) - handling textual time(code) specifications
3 
4  Copyright (C) Lumiera.org
5  2011, 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/quantiser.hpp"
32 #include "lib/time/timecode.hpp"
33 #include "lib/symbol.hpp"
34 #include "lib/util.hpp"
35 
36 using lib::Symbol;
37 
38 
39 namespace lib {
40 namespace time{
41 namespace test{
42 
44  using LERR_(INVALID_TIMECODE);
45 
46 
47  namespace { // Helper for writing test cases
48 
49  Symbol DEFAULT_GRID = "pal0";
50  Symbol OFFSET_GRID = "pal10";
51 
56  template<class FMT>
57  class Parsing
58  {
59  string const& timeSpec_;
60  PQuant grid_;
61 
62  public:
63  Parsing (string const& toParse, Symbol gridID =DEFAULT_GRID)
64  : timeSpec_(toParse)
65  , grid_(Quantiser::retrieve(gridID))
66  { }
67 
68 
69  void
70  should_yield (TimeValue const& expected)
71  {
72  TimeValue parsed = FMT::parse (timeSpec_, *grid_);
73  CHECK (parsed == expected, "parsing '%s' resulted in %s instead of %s"
74  , cStr(timeSpec_)
75  , cStr(Time(parsed))
76  , cStr(Time(expected)));
77  }
78 
79  void
80  should_yield (FSecs const& expectedSecs)
81  {
82  should_yield (Time (expectedSecs));
83  }
84 
85  void
86  should_fail ()
87  {
88  VERIFY_ERROR (INVALID_TIMECODE, FMT::parse (timeSpec_, *grid_));
89  }
90 
91  };
92 
93  }//(End)Test case helper
94 
95 
96 
97 
98  /****************************************************/
102  class TimeParsing_test : public Test
103  {
104  virtual void
105  run (Arg)
106  {
107  defineTestTimeGrids();
108 
109  parseFrames();
110  parseFractionalSeconds();
112 // parseHms();
113 // parseSmpte();
114 // parseDropFrame();
115  }
116 
117 
118  void
119  defineTestTimeGrids()
120  {
121  TimeGrid::build(DEFAULT_GRID, FrameRate::PAL);
122  TimeGrid::build(OFFSET_GRID, FrameRate::PAL, Time(0,10));
123  }
124 
125 
126  void
127  parseFrames ()
128  {
129  Parsing<format::Frames> ("0#") .should_yield (0);
130  Parsing<format::Frames> ("1#") .should_yield (FSecs(1,25) );
131  Parsing<format::Frames> ("-1#") .should_yield (FSecs(-1,25) );
132  Parsing<format::Frames> ("-0#") .should_yield (0);
133  Parsing<format::Frames> ("25#") .should_yield (1 );
134  Parsing<format::Frames> ("26#") .should_yield (Time(40,1) );
135  Parsing<format::Frames> ("25#", OFFSET_GRID).should_yield (1+10 );
136  Parsing<format::Frames> ("-1#", OFFSET_GRID).should_yield (10 - FSecs(1,25));
137 
138  Parsing<format::Frames> ("23") .should_fail();
139  Parsing<format::Frames> ("23 #") .should_fail();
140  Parsing<format::Frames> ("23.#") .should_fail();
141  Parsing<format::Frames> ("23x#") .should_fail();
142 
143  Parsing<format::Frames> ("xxx25#xxx") .should_yield (1);
144  Parsing<format::Frames> ("12 25#") .should_yield (1);
145  Parsing<format::Frames> ("12 25# 33#") .should_yield (1); // note pitfall: the first valid number is used
146  Parsing<format::Frames> ("12 25# \n 33#") .should_yield (1);
147  Parsing<format::Frames> ("12\n 25# \n 33#") .should_yield (1);
148  Parsing<format::Frames> ("12.25#") .should_fail(); // rejected because of leading dot (ambiguity)
149  }
150 
151 
152  void
153  parseFractionalSeconds ()
154  {
155  Parsing<format::Seconds> ("0sec") .should_yield (0);
156  Parsing<format::Seconds> ("1sec") .should_yield (1);
157  Parsing<format::Seconds> ("10sec") .should_yield (10);
158  Parsing<format::Seconds> ("100sec") .should_yield (100);
159  Parsing<format::Seconds> ("-10sec") .should_yield (-10);
160  Parsing<format::Seconds> ("-0sec") .should_yield (0);
161 
162  Parsing<format::Seconds> ("1/2sec") .should_yield (Time(500,0) );
163  Parsing<format::Seconds> ("1/25sec") .should_yield (Time( 40,0) );
164  Parsing<format::Seconds> ("1/250sec") .should_yield (Time( 4,0) ); // no quantisation involved in parsing
165  Parsing<format::Seconds> ("1/250sec", OFFSET_GRID).should_yield (Time(4,10)); // ...but the origin of the grid is used
166 
167  Parsing<format::Seconds> ("10/2sec") .should_yield (5);
168  Parsing<format::Seconds> ("1000/200sec") .should_yield (5);
169  Parsing<format::Seconds> ("-10/2sec") .should_yield (-5);
170  Parsing<format::Seconds> ("10/-2sec") .should_fail(); // only leading sign allowed (ambiguity)
171 
172  Parsing<format::Seconds> ("1+1/2sec") .should_yield (Time(500,1) );
173  Parsing<format::Seconds> ("1-1/2sec") .should_yield (Time(500,0) );
174  Parsing<format::Seconds> ("-1-1/2sec") .should_yield (-Time(500,1) );
175  Parsing<format::Seconds> ("-1+1/2sec") .should_yield (-Time(500,0) );
176  Parsing<format::Seconds> ("-1+1/-2sec") .should_fail();
177 
178  Parsing<format::Seconds> ("-12+24690/12345sec", OFFSET_GRID).should_yield(0); // origin=+10sec -12sec + 2/1sec == 0
179 
180  Parsing<format::Seconds> ("1") .should_fail();
181  Parsing<format::Seconds> ("1 sec") .should_fail();
182  Parsing<format::Seconds> ("--1sec") .should_fail();
183  Parsing<format::Seconds> ("/-1sec") .should_fail();
184  Parsing<format::Seconds> ("1.2sec") .should_fail();
185  Parsing<format::Seconds> ("1/.2sec") .should_fail();
186  Parsing<format::Seconds> ("1 + 2 / 4 sec") .should_fail();
187  Parsing<format::Seconds> ("1 + 2 / 4sec") .should_yield(4); // note pitfall: leading garbage not considered
188  Parsing<format::Seconds> ("xxx4secxxxx") .should_yield(4);
189  Parsing<format::Seconds> ("x1# 8/2sec 2sec").should_yield(4); // note pitfall: first valid number used
190  }
191 
192 
193  void
194  parseHms ()
195  {
196  UNIMPLEMENTED ("verify reading hour-minutes-seconds-millis time specs");
197  }
198 
199 
200  void
201  parseSmpte ()
202  {
203  }
204 
205 
206  void
207  parseDropFrame ()
208  {
209  UNIMPLEMENTED ("verify especially SMPTE-drop-frame timecode");
210  }
211  };
212 
213 
215  LAUNCHER (TimeParsing_test, "unit common");
216 
217 
218 
219 }}} // namespace lib::time::test
Interface: a grid and scale definition for time quantisation.
Definition: time-grid.hpp:86
static PQuant retrieve(Symbol gridID)
Access an existing grid definition or quantiser, known by the given symbolic ID.
CStr cStr(std::string const &rendered)
convenience shortcut: forced conversion to c-String via string.
Definition: symbol.hpp:68
Definition: run.hpp:49
#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
Token or Atom with distinct identity.
Definition: symbol.hpp:126
Timecode handling library This header defines the foundation interface TCode to represent a grid alig...
Marker types to indicate a literal string and a Symbol.
Expression builder for writing time value parsing tests.
Simple test class runner.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
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.
Library functions to support the formation of grid-aligned time values.
To establish a reference scale for quantised time values.
basic constant internal time value.
Definition: timevalue.hpp:142
static const FrameRate PAL
predefined constant for PAL framerate
Definition: timevalue.hpp:680