TimeUtils.Tests.cpp
1 #include "pch.h" 2 #include "TestHelpers.h" 3 #include <timeutil.h> 4 5 using namespace Microsoft::VisualStudio::CppUnitTestFramework; 6 7 namespace UnitTestsCommonUtils 8 { 9 TEST_CLASS(TimeUtilsTests) 10 { 11 public: 12 // to_string tests 13 TEST_METHOD(ToString_ZeroTime_ReturnsZero) 14 { 15 time_t t = 0; 16 auto result = timeutil::to_string(t); 17 Assert::AreEqual(std::wstring(L"0"), result); 18 } 19 20 TEST_METHOD(ToString_PositiveTime_ReturnsString) 21 { 22 time_t t = 1234567890; 23 auto result = timeutil::to_string(t); 24 Assert::AreEqual(std::wstring(L"1234567890"), result); 25 } 26 27 TEST_METHOD(ToString_LargeTime_ReturnsString) 28 { 29 time_t t = 1700000000; 30 auto result = timeutil::to_string(t); 31 Assert::AreEqual(std::wstring(L"1700000000"), result); 32 } 33 34 // from_string tests 35 TEST_METHOD(FromString_ZeroString_ReturnsZero) 36 { 37 auto result = timeutil::from_string(L"0"); 38 Assert::IsTrue(result.has_value()); 39 Assert::AreEqual(static_cast<time_t>(0), result.value()); 40 } 41 42 TEST_METHOD(FromString_ValidNumber_ReturnsTime) 43 { 44 auto result = timeutil::from_string(L"1234567890"); 45 Assert::IsTrue(result.has_value()); 46 Assert::AreEqual(static_cast<time_t>(1234567890), result.value()); 47 } 48 49 TEST_METHOD(FromString_InvalidString_ReturnsNullopt) 50 { 51 auto result = timeutil::from_string(L"invalid"); 52 Assert::IsFalse(result.has_value()); 53 } 54 55 TEST_METHOD(FromString_EmptyString_ReturnsNullopt) 56 { 57 auto result = timeutil::from_string(L""); 58 Assert::IsFalse(result.has_value()); 59 } 60 61 TEST_METHOD(FromString_MixedAlphaNumeric_ReturnsNullopt) 62 { 63 auto result = timeutil::from_string(L"123abc"); 64 Assert::IsFalse(result.has_value()); 65 } 66 67 TEST_METHOD(FromString_NegativeNumber_ReturnsNullopt) 68 { 69 auto result = timeutil::from_string(L"-1"); 70 Assert::IsFalse(result.has_value()); 71 } 72 73 // Roundtrip test 74 TEST_METHOD(ToStringFromString_Roundtrip_Works) 75 { 76 time_t original = 1609459200; // 2021-01-01 00:00:00 UTC 77 auto str = timeutil::to_string(original); 78 auto result = timeutil::from_string(str); 79 Assert::IsTrue(result.has_value()); 80 Assert::AreEqual(original, result.value()); 81 } 82 83 // now tests 84 TEST_METHOD(Now_ReturnsReasonableTime) 85 { 86 auto result = timeutil::now(); 87 // Should be after 2020 and before 2100 88 Assert::IsTrue(result > 1577836800); // 2020-01-01 89 Assert::IsTrue(result < 4102444800); // 2100-01-01 90 } 91 92 TEST_METHOD(Now_TwoCallsAreCloseInTime) 93 { 94 auto first = timeutil::now(); 95 std::this_thread::sleep_for(std::chrono::milliseconds(100)); 96 auto second = timeutil::now(); 97 // Difference should be less than 2 seconds 98 Assert::IsTrue(second >= first); 99 Assert::IsTrue(second - first < 2); 100 } 101 102 // diff::in_seconds tests 103 TEST_METHOD(DiffInSeconds_SameTime_ReturnsZero) 104 { 105 time_t t = 1000000; 106 auto result = timeutil::diff::in_seconds(t, t); 107 Assert::AreEqual(static_cast<int64_t>(0), result); 108 } 109 110 TEST_METHOD(DiffInSeconds_OneDifference_ReturnsOne) 111 { 112 time_t to = 1000001; 113 time_t from = 1000000; 114 auto result = timeutil::diff::in_seconds(to, from); 115 Assert::AreEqual(static_cast<int64_t>(1), result); 116 } 117 118 TEST_METHOD(DiffInSeconds_60Seconds_Returns60) 119 { 120 time_t to = 1000060; 121 time_t from = 1000000; 122 auto result = timeutil::diff::in_seconds(to, from); 123 Assert::AreEqual(static_cast<int64_t>(60), result); 124 } 125 126 TEST_METHOD(DiffInSeconds_NegativeDiff_ReturnsNegative) 127 { 128 time_t to = 1000000; 129 time_t from = 1000060; 130 auto result = timeutil::diff::in_seconds(to, from); 131 Assert::AreEqual(static_cast<int64_t>(-60), result); 132 } 133 134 // diff::in_minutes tests 135 TEST_METHOD(DiffInMinutes_SameTime_ReturnsZero) 136 { 137 time_t t = 1000000; 138 auto result = timeutil::diff::in_minutes(t, t); 139 Assert::AreEqual(static_cast<int64_t>(0), result); 140 } 141 142 TEST_METHOD(DiffInMinutes_OneMinute_ReturnsOne) 143 { 144 time_t to = 1000060; 145 time_t from = 1000000; 146 auto result = timeutil::diff::in_minutes(to, from); 147 Assert::AreEqual(static_cast<int64_t>(1), result); 148 } 149 150 TEST_METHOD(DiffInMinutes_60Minutes_Returns60) 151 { 152 time_t to = 1003600; 153 time_t from = 1000000; 154 auto result = timeutil::diff::in_minutes(to, from); 155 Assert::AreEqual(static_cast<int64_t>(60), result); 156 } 157 158 TEST_METHOD(DiffInMinutes_LessThanMinute_ReturnsZero) 159 { 160 time_t to = 1000059; 161 time_t from = 1000000; 162 auto result = timeutil::diff::in_minutes(to, from); 163 Assert::AreEqual(static_cast<int64_t>(0), result); 164 } 165 166 // diff::in_hours tests 167 TEST_METHOD(DiffInHours_SameTime_ReturnsZero) 168 { 169 time_t t = 1000000; 170 auto result = timeutil::diff::in_hours(t, t); 171 Assert::AreEqual(static_cast<int64_t>(0), result); 172 } 173 174 TEST_METHOD(DiffInHours_OneHour_ReturnsOne) 175 { 176 time_t to = 1003600; 177 time_t from = 1000000; 178 auto result = timeutil::diff::in_hours(to, from); 179 Assert::AreEqual(static_cast<int64_t>(1), result); 180 } 181 182 TEST_METHOD(DiffInHours_24Hours_Returns24) 183 { 184 time_t to = 1086400; 185 time_t from = 1000000; 186 auto result = timeutil::diff::in_hours(to, from); 187 Assert::AreEqual(static_cast<int64_t>(24), result); 188 } 189 190 TEST_METHOD(DiffInHours_LessThanHour_ReturnsZero) 191 { 192 time_t to = 1003599; 193 time_t from = 1000000; 194 auto result = timeutil::diff::in_hours(to, from); 195 Assert::AreEqual(static_cast<int64_t>(0), result); 196 } 197 198 // diff::in_days tests 199 TEST_METHOD(DiffInDays_SameTime_ReturnsZero) 200 { 201 time_t t = 1000000; 202 auto result = timeutil::diff::in_days(t, t); 203 Assert::AreEqual(static_cast<int64_t>(0), result); 204 } 205 206 TEST_METHOD(DiffInDays_OneDay_ReturnsOne) 207 { 208 time_t to = 1086400; 209 time_t from = 1000000; 210 auto result = timeutil::diff::in_days(to, from); 211 Assert::AreEqual(static_cast<int64_t>(1), result); 212 } 213 214 TEST_METHOD(DiffInDays_7Days_Returns7) 215 { 216 time_t to = 1604800; 217 time_t from = 1000000; 218 auto result = timeutil::diff::in_days(to, from); 219 Assert::AreEqual(static_cast<int64_t>(7), result); 220 } 221 222 TEST_METHOD(DiffInDays_LessThanDay_ReturnsZero) 223 { 224 time_t to = 1086399; 225 time_t from = 1000000; 226 auto result = timeutil::diff::in_days(to, from); 227 Assert::AreEqual(static_cast<int64_t>(0), result); 228 } 229 230 // format_as_local tests 231 TEST_METHOD(FormatAsLocal_YearFormat_ReturnsYear) 232 { 233 time_t t = 1609459200; // 2021-01-01 00:00:00 UTC 234 auto result = timeutil::format_as_local("%Y", t); 235 // Result depends on local timezone, but year should be 2020 or 2021 236 Assert::IsTrue(result == "2020" || result == "2021"); 237 } 238 239 TEST_METHOD(FormatAsLocal_DateFormat_ReturnsDate) 240 { 241 time_t t = 0; // 1970-01-01 00:00:00 UTC 242 auto result = timeutil::format_as_local("%Y-%m-%d", t); 243 // Result should be a date around 1970-01-01 depending on timezone 244 Assert::IsTrue(result.length() == 10); // YYYY-MM-DD format 245 Assert::IsTrue(result.substr(0, 4) == "1969" || result.substr(0, 4) == "1970"); 246 } 247 }; 248 }