xref: /aosp_15_r20/external/federated-compute/fcp/base/time_util_test.cc (revision 14675a029014e728ec732f129a32e299b2da0601)
1 /*
2  * Copyright 2022 Google LLC
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include "fcp/base/time_util.h"
17 
18 #include "gmock/gmock.h"
19 #include "gtest/gtest.h"
20 #include "fcp/testing/testing.h"
21 
22 namespace fcp {
23 namespace {
24 
TEST(ConvertAbslToProtoTimestampTest,ConvertSuccessfully)25 TEST(ConvertAbslToProtoTimestampTest, ConvertSuccessfully) {
26   absl::Time time = absl::FromUnixSeconds(1000) + absl::Nanoseconds(3);
27   google::protobuf::Timestamp expected_timestamp;
28   expected_timestamp.set_seconds(1000L);
29   expected_timestamp.set_nanos(3);
30   EXPECT_THAT(TimeUtil::ConvertAbslToProtoTimestamp(time),
31               EqualsProto(expected_timestamp));
32 }
33 
TEST(ConvertProtoToAbslTimeTest,ConvertSuccessfully)34 TEST(ConvertProtoToAbslTimeTest, ConvertSuccessfully) {
35   google::protobuf::Timestamp timestamp;
36   timestamp.set_seconds(1000L);
37   timestamp.set_nanos(3);
38   absl::Time expected_time = absl::FromUnixSeconds(1000) + absl::Nanoseconds(3);
39   EXPECT_EQ(TimeUtil::ConvertProtoToAbslTime(timestamp), expected_time);
40 }
41 
TEST(ConvertAbslToProtoDurationTest,ConvertSuccessfully)42 TEST(ConvertAbslToProtoDurationTest, ConvertSuccessfully) {
43   absl::Duration duration = absl::Seconds(1000) + absl::Nanoseconds(3);
44   google::protobuf::Duration expected_duration;
45   expected_duration.set_seconds(1000L);
46   expected_duration.set_nanos(3);
47   EXPECT_THAT(TimeUtil::ConvertAbslToProtoDuration(duration),
48               EqualsProto(expected_duration));
49 }
50 
TEST(ConvertProtoToAbslDurationTest,ConvertSuccessfully)51 TEST(ConvertProtoToAbslDurationTest, ConvertSuccessfully) {
52   google::protobuf::Duration duration;
53   duration.set_seconds(1000L);
54   duration.set_nanos(3);
55   absl::Duration expected_duration = absl::Seconds(1000) + absl::Nanoseconds(3);
56   EXPECT_EQ(TimeUtil::ConvertProtoToAbslDuration(duration), expected_duration);
57 }
58 
59 }  // anonymous namespace
60 }  // namespace fcp
61