DateTest.cpp 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2022-05-07
  7. *
  8. * */
  9. #include <gtest/gtest.h>
  10. #include <regex>
  11. #include <ls_std/ls_std.hpp>
  12. namespace
  13. {
  14. class DateTest : public ::testing::Test
  15. {
  16. protected:
  17. DateTest() = default;
  18. ~DateTest() override = default;
  19. void SetUp() override
  20. {}
  21. void TearDown() override
  22. {}
  23. };
  24. // arithmetic operators
  25. TEST_F(DateTest, operator_add)
  26. {
  27. ls::std::time::Date date{};
  28. time_t timestamp = date.getTime();
  29. date = date + 1;
  30. ASSERT_EQ(timestamp + 86400, date.getTime());
  31. }
  32. TEST_F(DateTest, operator_add_with_negative_value)
  33. {
  34. ls::std::time::Date date{};
  35. time_t timestamp = date.getTime();
  36. date = date + (-1);
  37. ASSERT_EQ(timestamp - 86400, date.getTime());
  38. }
  39. TEST_F(DateTest, operator_subtraction)
  40. {
  41. ls::std::time::Date date{};
  42. time_t timestamp = date.getTime();
  43. date = date - 1;
  44. ASSERT_EQ(timestamp - 86400, date.getTime());
  45. }
  46. TEST_F(DateTest, operator_subtraction_with_negative_value)
  47. {
  48. ls::std::time::Date date{};
  49. time_t timestamp = date.getTime();
  50. date = date - (-1);
  51. ASSERT_EQ(timestamp + 86400, date.getTime());
  52. }
  53. TEST_F(DateTest, operator_plus_equals)
  54. {
  55. ls::std::time::Date date{};
  56. time_t timestamp = date.getTime();
  57. date += 2;
  58. time_t expectedTimestamp = timestamp + 86400 * 2;
  59. ASSERT_EQ(expectedTimestamp, date.getTime());
  60. }
  61. TEST_F(DateTest, operator_plus_equals_with_negative_value)
  62. {
  63. ls::std::time::Date date{};
  64. time_t timestamp = date.getTime();
  65. date += -2;
  66. time_t expectedTimestamp = timestamp - 86400 * 2;
  67. ASSERT_EQ(expectedTimestamp, date.getTime());
  68. }
  69. TEST_F(DateTest, operator_minus_equals)
  70. {
  71. ls::std::time::Date date{};
  72. time_t timestamp = date.getTime();
  73. date -= 2;
  74. time_t expectedTimestamp = timestamp - 86400 * 2;
  75. ASSERT_EQ(expectedTimestamp, date.getTime());
  76. }
  77. TEST_F(DateTest, operator_minus_equals_with_negative_value)
  78. {
  79. ls::std::time::Date date{};
  80. time_t timestamp = date.getTime();
  81. date -= -2;
  82. time_t expectedTimestamp = timestamp + 86400 * 2;
  83. ASSERT_EQ(expectedTimestamp, date.getTime());
  84. }
  85. // additional functionality
  86. TEST_F(DateTest, after)
  87. {
  88. ls::std::time::Date date{}; // today
  89. ls::std::time::Date referenceDate{};
  90. referenceDate = referenceDate - 1; // yesterday
  91. ASSERT_TRUE(date.after(referenceDate));
  92. }
  93. TEST_F(DateTest, after_is_before_refernce_date)
  94. {
  95. ls::std::time::Date date{}; // is today
  96. ls::std::time::Date referenceDate{};
  97. referenceDate = referenceDate + 1; // is tomorrow
  98. ASSERT_FALSE(date.after(referenceDate));
  99. }
  100. TEST_F(DateTest, before)
  101. {
  102. ls::std::time::Date date{};
  103. date = date - 1; // yesterday
  104. ls::std::time::Date referenceDate{}; // today
  105. ASSERT_TRUE(date.before(referenceDate));
  106. }
  107. TEST_F(DateTest, before_is_after_reference_date)
  108. {
  109. ls::std::time::Date date{}; // today
  110. ls::std::time::Date referenceDate{};
  111. referenceDate = referenceDate - 1; // yesterday
  112. ASSERT_FALSE(date.before(referenceDate));
  113. }
  114. TEST_F(DateTest, getDay)
  115. {
  116. ls::std::time::Date date{};
  117. date.setTime(1597433693);
  118. ASSERT_EQ(14, date.getDay());
  119. }
  120. TEST_F(DateTest, getHour)
  121. {
  122. ls::std::time::Date date{};
  123. date.setTime(1597433693);
  124. ASSERT_EQ(21, date.getHour());
  125. }
  126. TEST_F(DateTest, getMinute)
  127. {
  128. ls::std::time::Date date{};
  129. date.setTime(1597433693);
  130. ASSERT_EQ(34, date.getMinute());
  131. }
  132. TEST_F(DateTest, getMonth)
  133. {
  134. ls::std::time::Date date{};
  135. date.setTime(1597433693);
  136. ASSERT_EQ(8, date.getMonth());
  137. }
  138. TEST_F(DateTest, getSecond)
  139. {
  140. ls::std::time::Date date{};
  141. date.setTime(1597433693);
  142. ASSERT_EQ(53, date.getSecond());
  143. }
  144. TEST_F(DateTest, getTime)
  145. {
  146. ls::std::time::Date date{};
  147. ASSERT_TRUE(date.getTime() > 0);
  148. }
  149. TEST_F(DateTest, getYear)
  150. {
  151. ls::std::time::Date date{};
  152. date.setTime(1597433693);
  153. ASSERT_EQ(2020, date.getYear());
  154. }
  155. TEST_F(DateTest, setTime)
  156. {
  157. ls::std::time::Date date{};
  158. ASSERT_TRUE(date.getTime() > 1000000);
  159. date.setTime(1000);
  160. ASSERT_EQ(1000, date.getTime());
  161. }
  162. TEST_F(DateTest, toString)
  163. {
  164. ls::std::time::Date date{};
  165. std::string regexSearchString = R"((\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))";
  166. std::regex _regex{regexSearchString};
  167. std::string dateString = date.toString();
  168. ASSERT_TRUE(std::regex_match(dateString, _regex));
  169. }
  170. }