DateTest.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * Author: Patrick-Christopher Mattulat
  3. * Company: Lynar Studios
  4. * E-Mail: webmaster@lynarstudios.com
  5. * Created: 2020-08-14
  6. * Changed: 2021-04-23
  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, operatorAdd)
  26. {
  27. ls_std::Date date{};
  28. time_t timestamp = date.getTime();
  29. date = date + 1;
  30. ASSERT_EQ(timestamp + 86400, date.getTime());
  31. }
  32. TEST_F(DateTest, operatorSub)
  33. {
  34. ls_std::Date date{};
  35. time_t timestamp = date.getTime();
  36. date = date - 1;
  37. ASSERT_EQ(timestamp - 86400, date.getTime());
  38. }
  39. // additional functionality
  40. TEST_F(DateTest, after)
  41. {
  42. ls_std::Date date{};
  43. ls_std::Date date2{};
  44. date2.setTime(1000000);
  45. ASSERT_TRUE(date.after(date2));
  46. }
  47. TEST_F(DateTest, before)
  48. {
  49. ls_std::Date date{};
  50. date.setTime(1000000);
  51. ls_std::Date date2{};
  52. ASSERT_TRUE(date.before(date2));
  53. }
  54. TEST_F(DateTest, getDay)
  55. {
  56. ls_std::Date date{};
  57. date.setTime(1597433693);
  58. ASSERT_EQ(14, date.getDay());
  59. }
  60. TEST_F(DateTest, getHour)
  61. {
  62. ls_std::Date date{};
  63. date.setTime(1597433693);
  64. ASSERT_EQ(21, date.getHour());
  65. }
  66. TEST_F(DateTest, getMinute)
  67. {
  68. ls_std::Date date{};
  69. date.setTime(1597433693);
  70. ASSERT_EQ(34, date.getMinute());
  71. }
  72. TEST_F(DateTest, getMonth)
  73. {
  74. ls_std::Date date{};
  75. date.setTime(1597433693);
  76. ASSERT_EQ(8, date.getMonth());
  77. }
  78. TEST_F(DateTest, getSecond)
  79. {
  80. ls_std::Date date{};
  81. date.setTime(1597433693);
  82. ASSERT_EQ(53, date.getSecond());
  83. }
  84. TEST_F(DateTest, getTime)
  85. {
  86. ls_std::Date date{};
  87. ASSERT_TRUE(date.getTime() > 0);
  88. }
  89. TEST_F(DateTest, getYear)
  90. {
  91. ls_std::Date date{};
  92. date.setTime(1597433693);
  93. ASSERT_EQ(2020, date.getYear());
  94. }
  95. TEST_F(DateTest, setTime)
  96. {
  97. ls_std::Date date{};
  98. ASSERT_TRUE(date.getTime() > 1000000);
  99. date.setTime(1000);
  100. ASSERT_EQ(1000, date.getTime());
  101. }
  102. TEST_F(DateTest, toString)
  103. {
  104. ls_std::Date date{};
  105. std::string regexSearchString = R"((\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}))";
  106. std::regex _regex{regexSearchString};
  107. std::string dateString = date.toString();
  108. ASSERT_TRUE(std::regex_match(dateString, _regex));
  109. }
  110. }