DateTest.cpp 4.2 KB

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