DateTest.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2023-03-25
  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. public:
  21. DateTest() = default;
  22. ~DateTest() override = default;
  23. };
  24. // arithmetic operators
  25. TEST_F(DateTest, operator_add)
  26. {
  27. 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. 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. 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. 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. 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. 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. 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. 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. Date date{}; // today
  89. 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. Date date{}; // is today
  96. Date referenceDate{};
  97. referenceDate = referenceDate + 1; // is tomorrow
  98. ASSERT_FALSE(date.after(referenceDate));
  99. }
  100. TEST_F(DateTest, before)
  101. {
  102. Date date{};
  103. date = date - 1; // yesterday
  104. Date referenceDate{}; // today
  105. ASSERT_TRUE(date.before(referenceDate));
  106. }
  107. TEST_F(DateTest, before_is_after_reference_date)
  108. {
  109. Date date{}; // today
  110. Date referenceDate{};
  111. referenceDate = referenceDate - 1; // yesterday
  112. ASSERT_FALSE(date.before(referenceDate));
  113. }
  114. TEST_F(DateTest, getDay)
  115. {
  116. Date date{};
  117. date.setTime(1597433693);
  118. ASSERT_EQ(14, date.getDay());
  119. }
  120. TEST_F(DateTest, getHour)
  121. {
  122. Date date{};
  123. date.setTime(1597433693);
  124. ASSERT_EQ(21, date.getHour());
  125. }
  126. TEST_F(DateTest, getMinute)
  127. {
  128. Date date{};
  129. date.setTime(1597433693);
  130. ASSERT_EQ(34, date.getMinute());
  131. }
  132. TEST_F(DateTest, getMonth)
  133. {
  134. Date date{};
  135. date.setTime(1597433693);
  136. ASSERT_EQ(8, date.getMonth());
  137. }
  138. TEST_F(DateTest, getSecond)
  139. {
  140. Date date{};
  141. date.setTime(1597433693);
  142. ASSERT_EQ(53, date.getSecond());
  143. }
  144. TEST_F(DateTest, getTime)
  145. {
  146. Date date{};
  147. ASSERT_TRUE(date.getTime() > 0);
  148. }
  149. TEST_F(DateTest, getYear)
  150. {
  151. Date date{};
  152. date.setTime(1597433693);
  153. ASSERT_EQ(2020, date.getYear());
  154. }
  155. TEST_F(DateTest, setTime)
  156. {
  157. 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. Date date{};
  165. string regexSearchString = R"((\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))";
  166. regex _regex{regexSearchString};
  167. string dateString = date.toString();
  168. ASSERT_TRUE(regex_match(dateString, _regex));
  169. }
  170. }