DateTest.cpp 4.2 KB

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