time.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* time.cpp - time tests
  2. * Copyright (C) 2017 caryoscelus
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include <catch.hpp>
  18. #include <core/time/period.h>
  19. using namespace rainynite::core;
  20. TEST_CASE("Time sanity", "[time]") {
  21. Time a(3, 8, 4);
  22. Time b(a);
  23. CHECK(b == a);
  24. CHECK(a+b == Time(7, 8, 0));
  25. CHECK(Time(0, 12, 24) == Time(2, 12, 0));
  26. }
  27. TEST_CASE("Time FPS conversion", "[time]") {
  28. auto t = Time(10.0);
  29. t.set_fps(12);
  30. t.change_fps(24);
  31. REQUIRE(t.get_seconds() == 5.0);
  32. }
  33. TEST_CASE("Time arithmetics", "[time]") {
  34. CHECK(Time(1)+Time(3) == Time(4));
  35. CHECK(Time()+Time(1) == Time(1));
  36. CHECK(Time(4)-Time(5) < Time());
  37. CHECK(Time(4)*5 == Time(20));
  38. CHECK(++Time(4) == Time(5));
  39. CHECK(--Time(0, 20, 5) == Time(0, 20, 4));
  40. }
  41. TEST_CASE("Time FPS mismatch", "[time]") {
  42. CHECK(Time(1, 12, 10) > Time(1, 24, 5));
  43. auto t = Time(1, 12, 10) + Time(1, 24, 5);
  44. CHECK(t == Time(3, 24, 1));
  45. }
  46. void test_period(TimePeriod const& period, unsigned amount) {
  47. unsigned i = 0;
  48. for (auto t : period) {
  49. (void) t;
  50. ++i;
  51. }
  52. REQUIRE(i == amount);
  53. }
  54. TEST_CASE("Time period iterator", "[time]") {
  55. test_period(TimePeriod(Time(0), Time(4)), 4);
  56. test_period(TimePeriod(Time(0), Time(4.5)), 5);
  57. test_period(TimePeriod(Time(0, 8), Time(5, 8)), 40);
  58. }