DateTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2020-08-21
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <regex>
  11. #include "../../../source/time/Date.hpp"
  12. namespace {
  13. class DateTest : public ::testing::Test {
  14. protected:
  15. DateTest() = default;
  16. ~DateTest() override = default;
  17. void SetUp() override {}
  18. void TearDown() override {}
  19. };
  20. // arithmetic operators
  21. TEST_F(DateTest, operatorAdd)
  22. {
  23. ls_std::Date date {};
  24. time_t timestamp = date.getTime();
  25. date = date + 1;
  26. ASSERT_EQ(timestamp + 86400, date.getTime());
  27. }
  28. TEST_F(DateTest, operatorSub)
  29. {
  30. ls_std::Date date {};
  31. time_t timestamp = date.getTime();
  32. date = date - 1;
  33. ASSERT_EQ(timestamp - 86400, date.getTime());
  34. }
  35. // additional functionality
  36. TEST_F(DateTest, after)
  37. {
  38. ls_std::Date date {};
  39. ls_std::Date date2 {};
  40. date2.setTime(1000000);
  41. ASSERT_TRUE(date.after(date2));
  42. }
  43. TEST_F(DateTest, before)
  44. {
  45. ls_std::Date date {};
  46. date.setTime(1000000);
  47. ls_std::Date date2 {};
  48. ASSERT_TRUE(date.before(date2));
  49. }
  50. TEST_F(DateTest, getDay)
  51. {
  52. ls_std::Date date {};
  53. date.setTime(1597433693);
  54. ASSERT_EQ(14, date.getDay());
  55. }
  56. TEST_F(DateTest, getHour)
  57. {
  58. ls_std::Date date {};
  59. date.setTime(1597433693);
  60. ASSERT_EQ(21, date.getHour());
  61. }
  62. TEST_F(DateTest, getMinute)
  63. {
  64. ls_std::Date date {};
  65. date.setTime(1597433693);
  66. ASSERT_EQ(34, date.getMinute());
  67. }
  68. TEST_F(DateTest, getMonth)
  69. {
  70. ls_std::Date date {};
  71. date.setTime(1597433693);
  72. ASSERT_EQ(8, date.getMonth());
  73. }
  74. TEST_F(DateTest, getSecond)
  75. {
  76. ls_std::Date date {};
  77. date.setTime(1597433693);
  78. ASSERT_EQ(53, date.getSecond());
  79. }
  80. TEST_F(DateTest, getTime)
  81. {
  82. ls_std::Date date {};
  83. ASSERT_TRUE(date.getTime() > 0);
  84. }
  85. TEST_F(DateTest, getYear)
  86. {
  87. ls_std::Date date {};
  88. date.setTime(1597433693);
  89. ASSERT_EQ(2020, date.getYear());
  90. }
  91. TEST_F(DateTest, setTime)
  92. {
  93. ls_std::Date date {};
  94. ASSERT_TRUE(date.getTime() > 1000000);
  95. date.setTime(1000);
  96. ASSERT_EQ(1000, date.getTime());
  97. }
  98. TEST_F(DateTest, toString)
  99. {
  100. ls_std::Date date {};
  101. std::string regexSearchString = R"((\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))";
  102. std::regex _regex {regexSearchString};
  103. std::string dateString = date.toString();
  104. ASSERT_TRUE(std::regex_match(dateString, _regex));
  105. }
  106. }