/*
- Copyright (C) 2014 Carl Hetherington <cth@carlh.net>
+ Copyright (C) 2014-2015 Carl Hetherington <cth@carlh.net>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
*/
+#include "sub_time.h"
+#include "exceptions.h"
#include <boost/test/unit_test.hpp>
-#include "metric_time.h"
-#include "frame_time.h"
-#include "time_pair.h"
+#include <iostream>
/* Check time construction */
BOOST_AUTO_TEST_CASE (time_construction_test)
{
{
- sub::MetricTime t (3, 5, 7, 40);
+ sub::Time t = sub::Time::from_hms (3, 5, 7, 40);
BOOST_CHECK_EQUAL (t.hours(), 3);
BOOST_CHECK_EQUAL (t.minutes(), 5);
BOOST_CHECK_EQUAL (t.seconds(), 7);
}
{
- sub::MetricTime t (591353, 1, 2, 3);
+ sub::Time t = sub::Time::from_hms (591353, 1, 2, 3);
BOOST_CHECK_EQUAL (t.hours(), 591353);
BOOST_CHECK_EQUAL (t.minutes(), 1);
BOOST_CHECK_EQUAL (t.seconds(), 2);
BOOST_CHECK_EQUAL (t.milliseconds(), 3);
}
-
- {
- sub::FrameTime t (3 * 60 * 60 * 24 + 31 * 60 * 24 + 4 * 24 + 11, 24);
- BOOST_CHECK_EQUAL (t.hours(), 3);
- BOOST_CHECK_EQUAL (t.minutes(), 31);
- BOOST_CHECK_EQUAL (t.seconds(), 4);
- BOOST_CHECK_EQUAL (t.frames(), 11);
- }
}
/* Check time conversions */
BOOST_AUTO_TEST_CASE (time_conversion_test)
{
- sub::TimePair p;
-
+ sub::Time p;
+
/* 40ms = 1 frame at 25fps */
- p.set_metric (sub::MetricTime (3, 5, 7, 40));
- BOOST_CHECK_EQUAL (p.frame (25), sub::FrameTime (3, 5, 7, 1));
- p.set_frame (sub::FrameTime (3, 5, 7, 1));
- BOOST_CHECK_EQUAL (p.metric (25), sub::MetricTime (3, 5, 7, 40));
+ p = sub::Time::from_hms (3, 5, 7, 40);
+ BOOST_CHECK_EQUAL (p.frames_at (sub::Rational (25, 1)), 1);
+ p = sub::Time::from_hmsf (3, 5, 7, 1, sub::Rational (25, 1));
+ BOOST_CHECK_EQUAL (p.milliseconds (), 40);
/* 120ms = 3 frames at 25fps */
- p.set_metric (sub::MetricTime (3, 5, 7, 120));
- BOOST_CHECK_EQUAL (p.frame (25), sub::FrameTime (3, 5, 7, 3));
- p.set_frame (sub::FrameTime (3, 5, 7, 3));
- BOOST_CHECK_EQUAL (p.metric (25), sub::MetricTime (3, 5, 7, 120));
+ p = sub::Time::from_hms (3, 5, 7, 120);
+ BOOST_CHECK_EQUAL (p.frames_at (sub::Rational (25, 1)), 3);
+ p = sub::Time::from_hmsf (3, 5, 7, 3, sub::Rational (25, 1));
+ BOOST_CHECK_EQUAL (p.milliseconds (), 120);
}
-/* Check time maths */
-BOOST_AUTO_TEST_CASE (time_maths_test)
+/* Check some operators */
+BOOST_AUTO_TEST_CASE (time_operator_test)
{
- {
- sub::FrameTime a (1, 59, 59, 23);
- sub::FrameTime b (2, 31, 19, 2);
- a.add (b, 24);
- BOOST_CHECK_EQUAL (a, sub::FrameTime (4, 31, 19, 1));
- }
+ BOOST_CHECK_EQUAL (sub::Time::from_hms (0, 0, 5, 198 * 4), sub::Time::from_hms (0, 0, 5, 198 * 4));
+ BOOST_CHECK (sub::Time::from_hms (0, 0, 55, 332) != sub::Time::from_hms (0, 0, 58, 332));
+}
- {
- sub::MetricTime a (1, 59, 59, 999);
- sub::MetricTime b (2, 31, 19, 2);
- a.add (b);
- BOOST_CHECK_EQUAL (a, sub::MetricTime (4, 31, 19, 1));
- }
+/* Check some other bits of Time */
+BOOST_AUTO_TEST_CASE (time_other_test)
+{
+ BOOST_CHECK_THROW (sub::Time::from_hmsf (2, 1, 58, 4).all_as_seconds(), sub::UnknownFrameRateError);
+ BOOST_CHECK_CLOSE (sub::Time::from_hmsf (2, 1, 58, 4, sub::Rational (24, 1)).all_as_seconds(), 7318.1667, 0.001);
+}
+
+/* Check an addition case that gave an odd result */
+BOOST_AUTO_TEST_CASE (time_add_test)
+{
+ sub::Time t = sub::Time::from_hmsf (0, 4, 8, 208, sub::Rational(1000, 1));
+ t.add (sub::Time::from_frames(54641, sub::Rational(24, 1)));
+ BOOST_CHECK_EQUAL (t, sub::Time::from_hmsf (0, 42, 4, 916, sub::Rational(1000, 1)));
+}
+
+BOOST_AUTO_TEST_CASE (time_scale_test1)
+{
+ sub::Time t = sub::Time::from_hmsf (0, 0, 1, 0, sub::Rational(1000, 1));
+ t.scale (0.96);
+ BOOST_CHECK_EQUAL (t.seconds(), 0);
+ BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 960);
+}
+
+BOOST_AUTO_TEST_CASE (time_scale_test2)
+{
+ sub::Time t = sub::Time::from_hmsf (0, 0, 2, 0, sub::Rational(1000, 1));
+ t.scale (0.96);
+ BOOST_CHECK_EQUAL (t.seconds(), 1);
+ BOOST_CHECK_EQUAL (t.frames_at(sub::Rational(1000, 1)), 920);
}