date_test.cpp 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. ///////////////////////////////////////////////////////////////////////////////////////
  2. /// \file date_test.cpp
  3. /// \brief Unit tests for the Date class
  4. ///
  5. /// \author Joe Siltberg
  6. /// $Date: 2013-11-22 10:56:18 +0100 (Fri, 22 Nov 2013) $
  7. ///
  8. ///////////////////////////////////////////////////////////////////////////////////////
  9. #include "config.h"
  10. #include "catch.hpp"
  11. #include "guess.h"
  12. namespace {
  13. /// Help function that simply calls Date::next n times
  14. void take_n_steps(Date& d, int n) {
  15. for (int i = 0; i < n; i++) {
  16. d.next();
  17. }
  18. }
  19. }
  20. TEST_CASE("date/construction", "Some basic tests of constructing dates") {
  21. Date d;
  22. d.init(1);
  23. REQUIRE(d.day == 0);
  24. REQUIRE(d.dayofmonth == 0);
  25. REQUIRE(d.month == 0);
  26. REQUIRE(d.year == 0);
  27. REQUIRE(d.islastyear);
  28. REQUIRE(!d.islastmonth);
  29. REQUIRE(!d.islastday);
  30. REQUIRE(!d.ismidday);
  31. }
  32. TEST_CASE("date/stepping", "Tests Date::next()") {
  33. Date d;
  34. d.init(1);
  35. // Given that we take these number of steps...
  36. int steps[] = { 1, 30, 28, 306 };
  37. // ...we expect to end up at these combinations of year, month and day
  38. int expectations[][3] = { {0, 0, 1}, { 0, 1, 0 }, {0, 2, 0}, {1, 0, 0} };
  39. for (int i = 0; i < sizeof(steps)/sizeof(steps[0]); i++) {
  40. take_n_steps(d, steps[i]);
  41. REQUIRE(d.year == expectations[i][0]);
  42. REQUIRE(d.month == expectations[i][1]);
  43. REQUIRE(d.dayofmonth == expectations[i][2]);
  44. }
  45. }
  46. TEST_CASE("date/leap", "Tests isleap") {
  47. REQUIRE(!Date::is_leap(1900));
  48. REQUIRE(!Date::is_leap(1975));
  49. REQUIRE(Date::is_leap(1904));
  50. REQUIRE(Date::is_leap(2000));
  51. }
  52. TEST_CASE("date/months", "Tests prevmonth and nextmonth") {
  53. Date d;
  54. d.init(1);
  55. // We start in January
  56. REQUIRE(d.nextmonth() == 1);
  57. REQUIRE(d.prevmonth() == 11);
  58. // Go to February
  59. take_n_steps(d, 31);
  60. REQUIRE(d.nextmonth() == 2);
  61. REQUIRE(d.prevmonth() == 0);
  62. // Go to December
  63. take_n_steps(d, 330);
  64. REQUIRE(d.nextmonth() == 0);
  65. REQUIRE(d.prevmonth() == 10);
  66. // Go to January the following year
  67. take_n_steps(d, 10);
  68. REQUIRE(d.nextmonth() == 1);
  69. REQUIRE(d.prevmonth() == 11);
  70. }
  71. TEST_CASE("date/calendar_year", "Tests the calendar year concept") {
  72. Date d;
  73. d.init(1);
  74. // If calendar year isn't set, the simulation year and calendar year will be the same
  75. REQUIRE(d.get_calendar_year() == 0);
  76. d.set_first_calendar_year(1900);
  77. REQUIRE(d.get_calendar_year() == 1900);
  78. // Go to next year
  79. take_n_steps(d, 400);
  80. REQUIRE(d.year == 1);
  81. REQUIRE(d.get_calendar_year() == 1901);
  82. }