Lumiera  0.pre.03
»edit your freedom«
test-time.c
Go to the documentation of this file.
1 /*
2  TEST-TIME - test the time conversion lib
3 
4  Copyright (C) Lumiera.org
5  2010 Stefan Kangas <skangas@skangas.se>
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 
31 #include "lib/test/test.h"
32 #include "lib/time.h"
33 
34 #include <ctype.h>
35 #include <nobug.h>
36 
37 static int
38 calculate_framecount (gavl_time_t t, uint fps)
39 {
40  return lumiera_quantise_frames_fps (t,0,fps);
41 }
42 
43 
44 TESTS_BEGIN
45 
46 const int FRAMES = 15;
47 const int MILLIS = 700;
48 const int SECONDS = 20;
49 const int MINUTES = 55;
50 const int HOURS = 3;
51 const int FPS = 24;
52 
53 /*
54  * 1. Basic functionality
55  */
56 
57 TEST (basic)
58 {
59  // Zero
60  gavl_time_t t = lumiera_build_time (0,0,0,0);
61 
62  CHECK ((gavl_time_t) t == 0);
63  CHECK (lumiera_time_millis (t) == 0);
64  CHECK (lumiera_time_seconds (t) == 0);
65  CHECK (lumiera_time_minutes (t) == 0);
66  CHECK (lumiera_time_hours (t) == 0);
67  CHECK (lumiera_time_frames (t, FPS) == 0);
68  CHECK (lumiera_time_frames (t, FPS+5) == 0);
69  CHECK (calculate_framecount (t,FPS) == 0);
70  CHECK (calculate_framecount (t, FPS+5) == 0);
71 
72  ECHO ("%s", lumiera_tmpbuf_print_time (t));
73 
74  // Non-zero
75  t = lumiera_build_time (MILLIS, SECONDS, MINUTES, HOURS);
76 
77  CHECK (lumiera_time_millis (t) == MILLIS);
78  CHECK (lumiera_time_seconds (t) == SECONDS);
79  CHECK (lumiera_time_minutes (t) == MINUTES);
80  CHECK (lumiera_time_hours (t) == HOURS);
81  CHECK (lumiera_time_frames (t, FPS) == FPS * MILLIS / 1000);
82  CHECK (lumiera_time_frames (t, FPS+5) == (FPS+5) * MILLIS / 1000);
83  CHECK (calculate_framecount (t, FPS) == 338896);
84  CHECK (calculate_framecount (t, FPS+5) == 409500);
85 
86  ECHO ("%s", lumiera_tmpbuf_print_time (t));
87 }
88 
89 /*
90  * 2. Frame rate dependent calculations.
91  */
92 
93 TEST (fps)
94 {
95  gavl_time_t t = lumiera_build_time_fps (FPS, FRAMES, SECONDS, MINUTES, HOURS);
96 
97  CHECK (lumiera_time_millis (t) == FRAMES * 1000 / FPS);
98  CHECK (lumiera_time_seconds (t) == SECONDS);
99  CHECK (lumiera_time_minutes (t) == MINUTES);
100  CHECK (lumiera_time_hours (t) == HOURS);
101  CHECK (lumiera_time_frames (t, FPS) == FRAMES);
102  CHECK (lumiera_time_frames (t, FPS+5) == FRAMES * (FPS+5)/FPS);
103  CHECK (calculate_framecount (t, FPS) == 338895);
104  CHECK (calculate_framecount (t, FPS+5) == 409498);
105 }
106 
107 /*
108  * 3. NTSC drop-frame calculations.
109  */
110 
111 TEST (ntsc_drop_frame)
112 {
113  // Make sure frame 0 begins at 0
114  gavl_time_t t = lumiera_build_time_ntsc_drop (0, 0, 0, 0);
115 
116  CHECK ((gavl_time_t) t == 0);
117  CHECK (lumiera_time_millis (t) == 0);
118  CHECK (lumiera_time_seconds (t) == 0);
119  CHECK (lumiera_time_minutes (t) == 0);
120  CHECK (lumiera_time_hours (t) == 0);
121  CHECK (lumiera_time_frames (t, FPS) == 0);
122  CHECK (lumiera_time_frames (t, FPS+5) == 0);
123  CHECK (calculate_framecount (t, FPS) == 0);
124  CHECK (calculate_framecount (t, FPS+5) == 0);
125 
126 
127 
128  t = lumiera_build_time_ntsc_drop (FRAMES, SECONDS, MINUTES, HOURS);
129 
130  // Calculate manually what result to expect....
131  int frames = FRAMES + 30*SECONDS + 30*60*MINUTES + 30*60*60*HOURS; // sum up using nominal 30fps
132  int minutes_to_drop_frames = (MINUTES - MINUTES/10) + (HOURS * 54); // but every minute, with the exception of every 10 minutes...
133  frames -= 2*minutes_to_drop_frames; // ...drop 2 frames
134  int64_t expectedMillis = 1000LL * frames * 1001/30000; // now convert frames to time, using the real framerate
135 
136  expectedMillis %= 1000; // look at the remainder..
137  CHECK (lumiera_time_millis (t) == expectedMillis);
138 
139  CHECK (lumiera_time_seconds (t) == SECONDS); // while all other components should come out equal as set
140  CHECK (lumiera_time_minutes (t) == MINUTES);
141  CHECK (lumiera_time_hours (t) == HOURS);
142 
143  // Reverse calculate frames for NTSC drop
144 //CHECK (lumiera_quantise_frames (t, 0, dropFrameDuration) == frames); // the total nominal frames
145  CHECK (lumiera_time_ntsc_drop_frames (t) == FRAMES); // maximum one frame off due to rounding
146 
147  // Cover the whole value range;
148  // Manually construct a drop-frame timecode
149  // Make sure our library function returns the same times.
150  int min;
151  int sec;
152  int frame;
153  int hrs;
154  for (hrs = 0; hrs <= 24; hrs += 6)
155  for (min = 0; min <= 59; min += 1)
156  for (sec = 0; sec <= 59; sec += 10)
157  for (frame = 0; frame <= 29; frame++)
158  {
159  // Skip dropped frames
160  if (min % 10 && sec == 0 && frame < 2)
161  continue;
162 
163  t = lumiera_build_time_ntsc_drop(frame, sec, min, hrs);
164  /*
165  ECHO ("%02d:%02d:%02d;%02d"
166  , lumiera_time_ntsc_drop_hours (t)
167  , lumiera_time_ntsc_drop_minutes (t)
168  , lumiera_time_ntsc_drop_seconds (t)
169  , lumiera_time_ntsc_drop_frames (t)
170  );
171  */
172  CHECK (lumiera_time_ntsc_drop_frames (t) == frame);
173  CHECK (lumiera_time_ntsc_drop_seconds (t) == sec);
174  CHECK (lumiera_time_ntsc_drop_minutes (t) == min);
175  CHECK (lumiera_time_ntsc_drop_hours (t) == hrs % 24);
176  }
177 
178  // Make sure we do not get non-existent frames
179  int i;
180  for (i = 0; i < 59; i++)
181  {
182  int frame = (i % 10 == 0) ? 0 : 2;
183  t = lumiera_build_time_ntsc_drop (frame, 0, i, 0);
184  CHECK (lumiera_time_ntsc_drop_frames (t) == frame);
185  }
186 }
187 
188 TESTS_END
int lumiera_time_ntsc_drop_frames(gavl_time_t time)
Extract the frame part of given time, using NTSC drop-frame timecode.
Definition: time.cpp:611
int lumiera_time_hours(gavl_time_t time)
Extract the hour part of given time.
Definition: time.cpp:544
gavl_time_t lumiera_build_time(long millis, uint secs, uint mins, uint hours)
Build a time value by summing up the given components.
Definition: time.cpp:522
Common functions for handling of time values.
Helpers and support macros for defining test executables in C.
int lumiera_time_ntsc_drop_seconds(gavl_time_t time)
Extract the second part of given time, using NTSC drop-frame timecode.
Definition: time.cpp:617
int lumiera_time_frames(gavl_time_t time, uint fps)
Extract the remaining frame part of given time.
Definition: time.cpp:568
gavl_time_t lumiera_build_time_ntsc_drop(uint frames, uint secs, uint mins, uint hours)
Builds a time value by summing up the given components.
Definition: time.cpp:635
int lumiera_time_millis(gavl_time_t time)
Extract the milliseconds part of given time.
Definition: time.cpp:562
int lumiera_time_ntsc_drop_hours(gavl_time_t time)
Extract the hour part of given time, using NTSC drop-frame timecode.
Definition: time.cpp:629
int lumiera_time_seconds(gavl_time_t time)
Extract the seconds part of given time.
Definition: time.cpp:556
int lumiera_time_minutes(gavl_time_t time)
Extract the minute part of given time.
Definition: time.cpp:550
int lumiera_time_ntsc_drop_minutes(gavl_time_t time)
Extract the minute part of given time, using NTSC drop-frame timecode.
Definition: time.cpp:623
gavl_time_t lumiera_build_time_fps(uint fps, uint frames, uint secs, uint mins, uint hours)
Builds a time value by summing up the given components.
Definition: time.cpp:533
char * lumiera_tmpbuf_print_time(gavl_time_t time)
Definition: time.cpp:395