/* Copyright (C) 2014-2015 Carl Hetherington 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 the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */ #include "sub_time.h" #include "exceptions.h" #include /* Check time construction */ BOOST_AUTO_TEST_CASE (time_construction_test) { { sub::Time t = sub::Time::from_hmsm (3, 5, 7, 40); BOOST_CHECK_EQUAL (t.hours, 3); BOOST_CHECK_EQUAL (t.minutes, 5); BOOST_CHECK_EQUAL (t.seconds, 7); BOOST_CHECK_EQUAL (t._milliseconds.get(), 40); } { sub::Time t = sub::Time::from_hmsm (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.get(), 3); } { sub::Time t = sub::Time::from_frames (91231513, sub::TWENTY_FOUR); BOOST_CHECK_EQUAL (t.hours, 1055); BOOST_CHECK_EQUAL (t.minutes, 55); BOOST_CHECK_EQUAL (t.seconds, 13); BOOST_CHECK_EQUAL (t._frames.get(), 1); } { sub::Time t = sub::Time::from_milliseconds (91231513); BOOST_CHECK_EQUAL (t.hours, 25); BOOST_CHECK_EQUAL (t.minutes, 20); BOOST_CHECK_EQUAL (t.seconds, 31); BOOST_CHECK_EQUAL (t._milliseconds.get(), 513); } } /* Check time conversions */ BOOST_AUTO_TEST_CASE (time_conversion_test) { /* 40ms = 1 frame at 25fps */ sub::Time p = sub::Time::from_hmsm (3, 5, 7, 40); BOOST_CHECK_EQUAL (p.frames (sub::TWENTY_FIVE), 1); p = sub::Time::from_hmsf (3, 5, 7, 1, sub::TWENTY_FIVE); BOOST_CHECK_EQUAL (p.as_milliseconds(), sub::Time::from_hmsm (3, 5, 7, 40).as_milliseconds ()); /* 120ms = 3 frames at 25fps */ p = sub::Time::from_hmsm (3, 5, 7, 120); BOOST_CHECK_EQUAL (p.frames (sub::TWENTY_FIVE), 3); p = sub::Time::from_hmsf (3, 5, 7, 3, sub::TWENTY_FIVE); BOOST_CHECK_EQUAL (p.as_milliseconds (), sub::Time::from_hmsm (3, 5, 7, 120).as_milliseconds ()); } BOOST_AUTO_TEST_CASE (time_addition_test) { { sub::Time a = sub::Time::from_hmsf (4, 54, 23, 5); sub::Time b = sub::Time::from_hmsf (4, 54, 23, 5); BOOST_CHECK_THROW (a + b, sub::UnknownFrameRateException); } { sub::Time a = sub::Time::from_hmsf (4, 54, 23, 5, sub::THIRTY); sub::Time b = sub::Time::from_hmsf (4, 54, 23, 5, sub::TWENTY_FOUR); BOOST_CHECK_THROW (a + b, sub::MismatchedFrameRateException); } { sub::Time a = sub::Time::from_hmsf (4, 54, 23, 5, sub::TWENTY_FOUR); sub::Time b = sub::Time::from_hmsf (1, 9, 55, 23, sub::TWENTY_FOUR); BOOST_CHECK_EQUAL (a + b, sub::Time::from_hmsf (6, 4, 19, 4, sub::TWENTY_FOUR)); } { sub::Time a = sub::Time::from_hmsm (4, 54, 23, 512); sub::Time b = sub::Time::from_hmsm (1, 9, 55, 998); BOOST_CHECK_EQUAL (a + b, sub::Time::from_hmsm (6, 4, 19, 510)); } } BOOST_AUTO_TEST_CASE (time_subtraction_test) { { sub::Time a = sub::Time::from_hmsf (4, 54, 23, 5); sub::Time b = sub::Time::from_hmsf (4, 54, 23, 5); BOOST_CHECK_THROW (a - b, sub::UnknownFrameRateException); } { sub::Time a = sub::Time::from_hmsf (4, 54, 23, 5, sub::THIRTY); sub::Time b = sub::Time::from_hmsf (4, 54, 23, 5, sub::TWENTY_FOUR); BOOST_CHECK_THROW (a - b, sub::MismatchedFrameRateException); } { sub::Time a = sub::Time::from_hmsf (2, 1, 1, 1, sub::TWENTY_FOUR); sub::Time b = sub::Time::from_hmsf (1, 59, 59, 23, sub::TWENTY_FOUR); BOOST_CHECK_EQUAL (a - b, sub::Time::from_hmsf (0, 1, 1, 2, sub::TWENTY_FOUR)); } { sub::Time a = sub::Time::from_hmsm (2, 1, 1, 1); sub::Time b = sub::Time::from_hmsm (1, 59, 59, 999); BOOST_CHECK_EQUAL (a - b, sub::Time::from_hmsm (0, 1, 1, 2)); } }