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)
5  2010 Stefan Kangas <skangas@skangas.se>
6 
7   **Lumiera** is free software; you can redistribute it and/or modify it
8   under the terms of the GNU General Public License as published by the
9   Free Software Foundation; either version 2 of the License, or (at your
10   option) any later version. See the file COPYING for further details.
11 
12 * *****************************************************************/
13 
22 #include "lib/test/test.h"
23 #include "lib/time.h"
24 
25 #include <ctype.h>
26 #include <nobug.h>
27 
28 static int
29 calculate_framecount (gavl_time_t t, uint fps)
30 {
31  return lumiera_quantise_frames_fps (t,0,fps);
32 }
33 
34 
35 TESTS_BEGIN
36 
37 const int FRAMES = 15;
38 const int MILLIS = 700;
39 const int SECONDS = 20;
40 const int MINUTES = 55;
41 const int HOURS = 3;
42 const int FPS = 24;
43 
44 /*
45  * 1. Basic functionality
46  */
47 
48 TEST (basic)
49 {
50  // Zero
51  gavl_time_t t = lumiera_build_time (0,0,0,0);
52 
53  CHECK ((gavl_time_t) t == 0);
54  CHECK (lumiera_time_millis (t) == 0);
55  CHECK (lumiera_time_seconds (t) == 0);
56  CHECK (lumiera_time_minutes (t) == 0);
57  CHECK (lumiera_time_hours (t) == 0);
58  CHECK (lumiera_time_frames (t, FPS) == 0);
59  CHECK (lumiera_time_frames (t, FPS+5) == 0);
60  CHECK (calculate_framecount (t,FPS) == 0);
61  CHECK (calculate_framecount (t, FPS+5) == 0);
62 
63  ECHO ("%s", lumiera_tmpbuf_print_time (t));
64 
65  // Non-zero
66  t = lumiera_build_time (MILLIS, SECONDS, MINUTES, HOURS);
67 
68  CHECK (lumiera_time_millis (t) == MILLIS);
69  CHECK (lumiera_time_seconds (t) == SECONDS);
70  CHECK (lumiera_time_minutes (t) == MINUTES);
71  CHECK (lumiera_time_hours (t) == HOURS);
72  CHECK (lumiera_time_frames (t, FPS) == FPS * MILLIS / 1000);
73  CHECK (lumiera_time_frames (t, FPS+5) == (FPS+5) * MILLIS / 1000);
74  CHECK (calculate_framecount (t, FPS) == 338896);
75  CHECK (calculate_framecount (t, FPS+5) == 409500);
76 
77  ECHO ("%s", lumiera_tmpbuf_print_time (t));
78 }
79 
80 /*
81  * 2. Frame rate dependent calculations.
82  */
83 
84 TEST (fps)
85 {
86  gavl_time_t t = lumiera_build_time_fps (FPS, FRAMES, SECONDS, MINUTES, HOURS);
87 
88  CHECK (lumiera_time_millis (t) == FRAMES * 1000 / FPS);
89  CHECK (lumiera_time_seconds (t) == SECONDS);
90  CHECK (lumiera_time_minutes (t) == MINUTES);
91  CHECK (lumiera_time_hours (t) == HOURS);
92  CHECK (lumiera_time_frames (t, FPS) == FRAMES);
93  CHECK (lumiera_time_frames (t, FPS+5) == FRAMES * (FPS+5)/FPS);
94  CHECK (calculate_framecount (t, FPS) == 338895);
95  CHECK (calculate_framecount (t, FPS+5) == 409498);
96 }
97 
98 /*
99  * 3. NTSC drop-frame calculations.
100  */
101 
102 TEST (ntsc_drop_frame)
103 {
104  // Make sure frame 0 begins at 0
105  gavl_time_t t = lumiera_build_time_ntsc_drop (0, 0, 0, 0);
106 
107  CHECK ((gavl_time_t) t == 0);
108  CHECK (lumiera_time_millis (t) == 0);
109  CHECK (lumiera_time_seconds (t) == 0);
110  CHECK (lumiera_time_minutes (t) == 0);
111  CHECK (lumiera_time_hours (t) == 0);
112  CHECK (lumiera_time_frames (t, FPS) == 0);
113  CHECK (lumiera_time_frames (t, FPS+5) == 0);
114  CHECK (calculate_framecount (t, FPS) == 0);
115  CHECK (calculate_framecount (t, FPS+5) == 0);
116 
117 
118 
119  t = lumiera_build_time_ntsc_drop (FRAMES, SECONDS, MINUTES, HOURS);
120 
121  // Calculate manually what result to expect....
122  int frames = FRAMES + 30*SECONDS + 30*60*MINUTES + 30*60*60*HOURS; // sum up using nominal 30fps
123  int minutes_to_drop_frames = (MINUTES - MINUTES/10) + (HOURS * 54); // but every minute, with the exception of every 10 minutes...
124  frames -= 2*minutes_to_drop_frames; // ...drop 2 frames
125  int64_t expectedMillis = 1000LL * frames * 1001/30000; // now convert frames to time, using the real framerate
126 
127  expectedMillis %= 1000; // look at the remainder..
128  CHECK (lumiera_time_millis (t) == expectedMillis);
129 
130  CHECK (lumiera_time_seconds (t) == SECONDS); // while all other components should come out equal as set
131  CHECK (lumiera_time_minutes (t) == MINUTES);
132  CHECK (lumiera_time_hours (t) == HOURS);
133 
134  // Reverse calculate frames for NTSC drop
135 //CHECK (lumiera_quantise_frames (t, 0, dropFrameDuration) == frames); // the total nominal frames
136  CHECK (lumiera_time_ntsc_drop_frames (t) == FRAMES); // maximum one frame off due to rounding
137 
138  // Cover the whole value range;
139  // Manually construct a drop-frame timecode
140  // Make sure our library function returns the same times.
141  int min;
142  int sec;
143  int frame;
144  int hrs;
145  for (hrs = 0; hrs <= 24; hrs += 6)
146  for (min = 0; min <= 59; min += 1)
147  for (sec = 0; sec <= 59; sec += 10)
148  for (frame = 0; frame <= 29; frame++)
149  {
150  // Skip dropped frames
151  if (min % 10 && sec == 0 && frame < 2)
152  continue;
153 
154  t = lumiera_build_time_ntsc_drop(frame, sec, min, hrs);
155  /*
156  ECHO ("%02d:%02d:%02d;%02d"
157  , lumiera_time_ntsc_drop_hours (t)
158  , lumiera_time_ntsc_drop_minutes (t)
159  , lumiera_time_ntsc_drop_seconds (t)
160  , lumiera_time_ntsc_drop_frames (t)
161  );
162  */
163  CHECK (lumiera_time_ntsc_drop_frames (t) == frame);
164  CHECK (lumiera_time_ntsc_drop_seconds (t) == sec);
165  CHECK (lumiera_time_ntsc_drop_minutes (t) == min);
166  CHECK (lumiera_time_ntsc_drop_hours (t) == hrs % 24);
167  }
168 
169  // Make sure we do not get non-existent frames
170  int i;
171  for (i = 0; i < 59; i++)
172  {
173  int frame = (i % 10 == 0) ? 0 : 2;
174  t = lumiera_build_time_ntsc_drop (frame, 0, i, 0);
175  CHECK (lumiera_time_ntsc_drop_frames (t) == frame);
176  }
177 }
178 
179 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:602
int lumiera_time_hours(gavl_time_t time)
Extract the hour part of given time.
Definition: time.cpp:535
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:513
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:608
int lumiera_time_frames(gavl_time_t time, uint fps)
Extract the remaining frame part of given time.
Definition: time.cpp:559
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:626
int lumiera_time_millis(gavl_time_t time)
Extract the milliseconds part of given time.
Definition: time.cpp:553
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:620
int lumiera_time_seconds(gavl_time_t time)
Extract the seconds part of given time.
Definition: time.cpp:547
int lumiera_time_minutes(gavl_time_t time)
Extract the minute part of given time.
Definition: time.cpp:541
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:614
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:524
char * lumiera_tmpbuf_print_time(gavl_time_t time)
Definition: time.cpp:386