DateTest.cpp 4.2 KB

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