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 using util::cStr;
38 
39 
40 namespace lib {
41 namespace time{
42 namespace test{
43 
45  using format::LERR_(INVALID_TIMECODE);
46 
47 
48  namespace { // Helper for writing test cases
49 
50  Symbol DEFAULT_GRID = "pal0";
51  Symbol OFFSET_GRID = "pal10";
52 
57  template<class FMT>
58  class Parsing
59  {
60  string const& timeSpec_;
61  PQuant grid_;
62 
63  public:
64  Parsing (string const& toParse, Symbol gridID =DEFAULT_GRID)
65  : timeSpec_(toParse)
66  , grid_(Quantiser::retrieve(gridID))
67  { }
68 
69 
70  void
71  should_yield (TimeValue const& expected)
72  {
73  TimeValue parsed = FMT::parse (timeSpec_, *grid_);
74  CHECK (parsed == expected, "parsing '%s' resulted in %s instead of %s"
75  , cStr(timeSpec_)
76  , cStr(Time(parsed))
77  , cStr(Time(expected)));
78  }
79 
80  void
81  should_yield (FSecs const& expectedSecs)
82  {
83  should_yield (Time (expectedSecs));
84  }
85 
86  void
87  should_fail ()
88  {
89  VERIFY_ERROR (INVALID_TIMECODE, FMT::parse (timeSpec_, *grid_));
90  }
91 
92  };
93 
94  }//(End)Test case helper
95 
96 
97 
98 
99  /****************************************************/
103  class TimeParsing_test : public Test
104  {
105  virtual void
106  run (Arg)
107  {
108  defineTestTimeGrids();
109 
110  parseFrames();
111  parseFractionalSeconds();
113 // parseHms();
114 // parseSmpte();
115 // parseDropFrame();
116  }
117 
118 
119  void
120  defineTestTimeGrids()
121  {
122  TimeGrid::build(DEFAULT_GRID, FrameRate::PAL);
123  TimeGrid::build(OFFSET_GRID, FrameRate::PAL, Time(0,10));
124  }
125 
126 
127  void
128  parseFrames ()
129  {
130  Parsing<format::Frames> ("0#") .should_yield (0);
131  Parsing<format::Frames> ("1#") .should_yield (FSecs(1,25) );
132  Parsing<format::Frames> ("-1#") .should_yield (FSecs(-1,25) );
133  Parsing<format::Frames> ("-0#") .should_yield (0);
134  Parsing<format::Frames> ("25#") .should_yield (1 );
135  Parsing<format::Frames> ("26#") .should_yield (Time(40,1) );
136  Parsing<format::Frames> ("25#", OFFSET_GRID).should_yield (1+10 );
137  Parsing<format::Frames> ("-1#", OFFSET_GRID).should_yield (10 - FSecs(1,25));
138 
139  Parsing<format::Frames> ("23") .should_fail();
140  Parsing<format::Frames> ("23 #") .should_fail();
141  Parsing<format::Frames> ("23.#") .should_fail();
142  Parsing<format::Frames> ("23x#") .should_fail();
143 
144  Parsing<format::Frames> ("xxx25#xxx") .should_yield (1);
145  Parsing<format::Frames> ("12 25#") .should_yield (1);
146  Parsing<format::Frames> ("12 25# 33#") .should_yield (1); // note pitfall: the first valid number is used
147  Parsing<format::Frames> ("12 25# \n 33#") .should_yield (1);
148  Parsing<format::Frames> ("12\n 25# \n 33#") .should_yield (1);
149  Parsing<format::Frames> ("12.25#") .should_fail(); // rejected because of leading dot (ambiguity)
150  }
151 
152 
153  void
154  parseFractionalSeconds ()
155  {
156  Parsing<format::Seconds> ("0sec") .should_yield (0);
157  Parsing<format::Seconds> ("1sec") .should_yield (1);
158  Parsing<format::Seconds> ("10sec") .should_yield (10);
159  Parsing<format::Seconds> ("100sec") .should_yield (100);
160  Parsing<format::Seconds> ("-10sec") .should_yield (-10);
161  Parsing<format::Seconds> ("-0sec") .should_yield (0);
162 
163  Parsing<format::Seconds> ("1/2sec") .should_yield (Time(500,0) );
164  Parsing<format::Seconds> ("1/25sec") .should_yield (Time( 40,0) );
165  Parsing<format::Seconds> ("1/250sec") .should_yield (Time( 4,0) ); // no quantisation involved in parsing
166  Parsing<format::Seconds> ("1/250sec", OFFSET_GRID).should_yield (Time(4,10)); // ...but the origin of the grid is used
167 
168  Parsing<format::Seconds> ("10/2sec") .should_yield (5);
169  Parsing<format::Seconds> ("1000/200sec") .should_yield (5);
170  Parsing<format::Seconds> ("-10/2sec") .should_yield (-5);
171  Parsing<format::Seconds> ("10/-2sec") .should_fail(); // only leading sign allowed (ambiguity)
172 
173  Parsing<format::Seconds> ("1+1/2sec") .should_yield (Time(500,1) );
174  Parsing<format::Seconds> ("1-1/2sec") .should_yield (Time(500,0) );
175  Parsing<format::Seconds> ("-1-1/2sec") .should_yield (-Time(500,1) );
176  Parsing<format::Seconds> ("-1+1/2sec") .should_yield (-Time(500,0) );
177  Parsing<format::Seconds> ("-1+1/-2sec") .should_fail();
178 
179  Parsing<format::Seconds> ("-12+24690/12345sec", OFFSET_GRID).should_yield(0); // origin=+10sec -12sec + 2/1sec == 0
180 
181  Parsing<format::Seconds> ("1") .should_fail();
182  Parsing<format::Seconds> ("1 sec") .should_fail();
183  Parsing<format::Seconds> ("--1sec") .should_fail();
184  Parsing<format::Seconds> ("/-1sec") .should_fail();
185  Parsing<format::Seconds> ("1.2sec") .should_fail();
186  Parsing<format::Seconds> ("1/.2sec") .should_fail();
187  Parsing<format::Seconds> ("1 + 2 / 4 sec") .should_fail();
188  Parsing<format::Seconds> ("1 + 2 / 4sec") .should_yield(4); // note pitfall: leading garbage not considered
189  Parsing<format::Seconds> ("xxx4secxxxx") .should_yield(4);
190  Parsing<format::Seconds> ("x1# 8/2sec 2sec").should_yield(4); // note pitfall: first valid number used
191  }
192 
193 
194  void
195  parseHms ()
196  {
197  UNIMPLEMENTED ("verify reading hour-minutes-seconds-millis time specs");
198  }
199 
200 
201  void
202  parseSmpte ()
203  {
204  }
205 
206 
207  void
208  parseDropFrame ()
209  {
210  UNIMPLEMENTED ("verify especially SMPTE-drop-frame timecode");
211  }
212  };
213 
214 
216  LAUNCHER (TimeParsing_test, "unit common");
217 
218 
219 
220 }}} // 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.
Definition: run.hpp:49
#define VERIFY_ERROR(ERROR_ID, ERRONEOUS_STATEMENT)
Macro to verify a statement indeed raises an exception.
const char * cStr(string const &org)
convenience shortcut: conversion to c-String via string.
Definition: util.hpp:423
Implementation namespace for support and library code.
Lumiera&#39;s internal time value datatype.
Definition: timevalue.hpp:305
Token or Atom with distinct identity.
Definition: symbol.hpp:116
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:226
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:673