| 9 | #include <random> |
| 10 | |
| 11 | TEST(time, calculate_date_correct) { |
| 12 | ::std::string spec = "a[%a]A[%A]b[%b]B[%B]C[%C]d[%d]e[%e]"; |
| 13 | spec += "F[%F]G[%G]g[%g]h[%h]H[%H]I[%I]j[%j]k[%k]l[%l]m[%m]"; |
| 14 | spec += "M[%M]n[%n]p[%p]P[%P]r[%r]R[%R]s[%s]S[%S]t[%t]T[%T]"; |
| 15 | spec += "u[%u]U[%U]V[%V]w[%w]x[%x]X[%X]y[%y]Y[%Y]z[%z]Z[%Z]%%[%%]"; |
| 16 | |
| 17 | // 500年覆盖了完整的闰年周期,测试日期正确性 |
| 18 | auto now = time(nullptr); |
| 19 | ::std::string text1(5000, '\0'); |
| 20 | ::std::string text2(5000, '\0'); |
| 21 | ::std::mt19937_64 gen {::std::random_device {}()}; |
| 22 | for (size_t i = 0; i < 500; ++i) { |
| 23 | for (size_t j = 0; j < 365; ++j) { |
| 24 | time_t time = now + (i * 365 + j) * 24 * 60 * 60 + gen() % (24 * 60 * 60); |
| 25 | struct ::tm tm1; |
| 26 | ::memset(&tm1, 0, sizeof(tm1)); |
| 27 | localtime_r(&time, &tm1); |
| 28 | struct ::tm tm2; |
| 29 | ::memset(&tm2, 0, sizeof(tm2)); |
| 30 | ::babylon::localtime(&time, &tm2); |
| 31 | ASSERT_EQ(tm1.tm_sec, tm2.tm_sec); |
| 32 | ASSERT_EQ(tm1.tm_min, tm2.tm_min); |
| 33 | ASSERT_EQ(tm1.tm_hour, tm2.tm_hour); |
| 34 | ASSERT_EQ(tm1.tm_mday, tm2.tm_mday); |
| 35 | ASSERT_EQ(tm1.tm_mon, tm2.tm_mon); |
| 36 | ASSERT_EQ(tm1.tm_year, tm2.tm_year); |
| 37 | ASSERT_EQ(tm1.tm_wday, tm2.tm_wday); |
| 38 | ASSERT_EQ(tm1.tm_yday, tm2.tm_yday); |
| 39 | ASSERT_EQ(tm1.tm_isdst, tm2.tm_isdst); |
| 40 | ASSERT_EQ(tm1.tm_gmtoff, tm2.tm_gmtoff); |
| 41 | ASSERT_STREQ(tm1.tm_zone, tm2.tm_zone); |
| 42 | ::strftime(&text1[0], text1.size(), spec.c_str(), &tm1); |
| 43 | ::strftime(&text2[0], text2.size(), spec.c_str(), &tm2); |
| 44 | ASSERT_STREQ(text1.c_str(), text2.c_str()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | TEST(time, calculate_near_sequentially_correct) { |
| 50 | auto now = time(nullptr); |