MCPcopy Create free account
hub / github.com/bytedance/bolt / testTmToString

Function testTmToString

bolt/type/tests/TimestampTest.cpp:491–535  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

489}
490
491void testTmToString(
492 const std::string& format,
493 const TimestampToStringOptions::Mode mode) {
494 uint64_t seed = 42;
495 std::default_random_engine gen(seed);
496
497 std::uniform_int_distribution<time_t> dist(
498 std::numeric_limits<time_t>::min(), std::numeric_limits<time_t>::max());
499 std::uniform_int_distribution<int> nanosDist(0, Timestamp::kMaxNanos);
500
501 std::tm actual{};
502 std::tm expected{};
503
504 TimestampToStringOptions options;
505 options.mode = mode;
506
507 const std::vector<TimestampToStringOptions::Precision> precisions = {
508 TimestampToStringOptions::Precision::kMilliseconds,
509 TimestampToStringOptions::Precision::kNanoseconds};
510
511 for (auto precision : precisions) {
512 options.precision = precision;
513 for (int i = 0; i < 10'000; ++i) {
514 auto epoch = dist(gen);
515 auto nanos = nanosDist(gen);
516 SCOPED_TRACE(fmt::format(
517 "epoch={}, nanos={}, mode={}, precision={}",
518 epoch,
519 nanos,
520 mode,
521 precision));
522 if (gmtime_r(&epoch, &expected)) {
523 ASSERT_TRUE(Timestamp::epochToUtc(epoch, actual));
524 checkTm(actual, expected);
525
526 auto actualString = Timestamp::tmToString(actual, nanos, options);
527 auto expectedString = tmToString(expected, nanos, format, options);
528 ASSERT_EQ(expectedString, actualString);
529
530 } else {
531 ASSERT_FALSE(Timestamp::epochToUtc(epoch, actual));
532 }
533 }
534 }
535}
536
537TEST(TimestampTest, tmToStringDateOnly) {
538 // %F - equivalent to "%Y-%m-%d" (the ISO 8601 date format)

Callers 1

TESTFunction · 0.85

Calls 4

checkTmFunction · 0.85
tmToStringFunction · 0.85
minFunction · 0.50
maxFunction · 0.50

Tested by

no test coverage detected