Flutter Engine
The Flutter Engine
time_unittest.cc
Go to the documentation of this file.
1// Copyright 2013 The Flutter Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include <thread>
6
7#include "flutter/fml/time/chrono_timestamp_provider.h"
8#include "flutter/fml/time/time_delta.h"
9#include "gtest/gtest.h"
10
11namespace fml {
12namespace {
13
14TEST(Time, Now) {
16 for (int i = 0; i < 3; ++i) {
17 auto now = ChronoTicksSinceEpoch();
18 EXPECT_GE(now, start);
19 std::this_thread::yield();
20 }
21}
22
23TEST(Time, IntConversions) {
24 // Integer conversions should all truncate, not round.
25 TimeDelta delta = TimeDelta::FromNanoseconds(102304506708ll);
26 EXPECT_EQ(102304506708ll, delta.ToNanoseconds());
27 EXPECT_EQ(102304506ll, delta.ToMicroseconds());
28 EXPECT_EQ(102304ll, delta.ToMilliseconds());
29 EXPECT_EQ(102ll, delta.ToSeconds());
30}
31
32TEST(Time, FloatConversions) {
33 // Float conversions should remain close to the original value.
34 TimeDelta delta = TimeDelta::FromNanoseconds(102304506708ll);
35 EXPECT_FLOAT_EQ(102304506708.0, delta.ToNanosecondsF());
36 EXPECT_FLOAT_EQ(102304506.708, delta.ToMicrosecondsF());
37 EXPECT_FLOAT_EQ(102304.506708, delta.ToMillisecondsF());
38 EXPECT_FLOAT_EQ(102.304506708, delta.ToSecondsF());
39}
40
41TEST(Time, TimespecConversions) {
42 struct timespec ts;
43 ts.tv_sec = 5;
44 ts.tv_nsec = 7;
45 TimeDelta from_timespec = TimeDelta::FromTimespec(ts);
46 EXPECT_EQ(5, from_timespec.ToSeconds());
47 EXPECT_EQ(5 * 1000000000ll + 7, from_timespec.ToNanoseconds());
48 struct timespec to_timespec = from_timespec.ToTimespec();
49 EXPECT_EQ(ts.tv_sec, to_timespec.tv_sec);
50 EXPECT_EQ(ts.tv_nsec, to_timespec.tv_nsec);
51}
52
53} // namespace
54} // namespace fml
static constexpr TimeDelta FromTimespec(struct timespec ts)
Definition: time_delta.h:106
static constexpr TimeDelta FromNanoseconds(int64_t nanos)
Definition: time_delta.h:40
Definition: ascii_trie.cc:9
fml::TimePoint ChronoTicksSinceEpoch()
TEST(MallocMapping, EmptyContructor)