blob: 82441695ee9b033cd88bc4e0001261425a1df203 [file] [log] [blame]
// Copyright 2016 The Cobalt Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "starboard/client_porting/eztime/eztime.h"
#include "starboard/client_porting/eztime/test_constants.h"
#include "starboard/common/time.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace starboard {
namespace client_porting {
namespace eztime {
namespace {
TEST(EzTimeTFromSbTime, IsTransitive) {
int64_t sb_time = EzTimeTToSbTime(kTestTimePositive);
EzTimeT ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimePositive, ez_time);
sb_time = EzTimeTToSbTime(kTestTimeWindowsNegative);
ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimeWindowsNegative, ez_time);
sb_time = EzTimeTToSbTime(kTestTimePosixZero) - 1;
ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimePosixZero - 1, ez_time);
sb_time = EzTimeTToSbTime(kTestTimeWindowsZero) - 1;
ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimeWindowsZero - 1, ez_time);
sb_time = EzTimeTToSbTime(kTestTimePosixZero) + 1;
ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimePosixZero, ez_time);
sb_time = EzTimeTToSbTime(kTestTimeWindowsZero) + 1;
ez_time = EzTimeTFromSbTime(sb_time);
EXPECT_EQ(kTestTimeWindowsZero, ez_time);
}
TEST(EzTimeValueFromSbTime, IsTransitive) {
EzTimeValue time_value = EzTimeTToEzTimeValue(kTestTimePositive);
int64_t sb_time = EzTimeValueToSbTime(&time_value);
EzTimeValue time_value2 = EzTimeValueFromSbTime(sb_time);
EXPECT_EQ(time_value.tv_sec, time_value2.tv_sec);
EXPECT_EQ(time_value.tv_usec, time_value2.tv_usec);
time_value = EzTimeTToEzTimeValue(kTestTimeWindowsNegative);
sb_time = EzTimeValueToSbTime(&time_value);
time_value2 = EzTimeValueFromSbTime(sb_time);
EXPECT_EQ(time_value.tv_sec, time_value2.tv_sec);
EXPECT_EQ(time_value.tv_usec, time_value2.tv_usec);
}
TEST(EzTimeTGetNowTest, IsKindOfSane) {
EzTimeT ez_time = EzTimeTGetNow(NULL);
int64_t sb_time = PosixTimeToWindowsTime(CurrentPosixTime());
// They should be within a second of each other.
EXPECT_GT(1'000'000LL, sb_time - EzTimeTToSbTime(ez_time));
EXPECT_GT(1'000'000LL, EzTimeTToSbTime(ez_time) - sb_time);
// Now should be after the time I wrote this test.
EXPECT_LE(kTestTimeWritten, ez_time);
// And it should be before 5 years after I wrote this test, at least for a
// while.
EXPECT_GE(kTestTimePastWritten, ez_time);
EzTimeT ez_time2 = 0;
EzTimeT ez_time3 = EzTimeTGetNow(&ez_time2);
EXPECT_EQ(ez_time2, ez_time3);
}
TEST(EzTimeValueGetNowTest, IsKindOfSane) {
EzTimeValue time_value = {0};
EXPECT_EQ(0, EzTimeValueGetNow(&time_value, NULL));
int64_t sb_time = PosixTimeToWindowsTime(CurrentPosixTime());
// They should be within a second of each other.
EXPECT_GT(1'000'000LL, sb_time - EzTimeValueToSbTime(&time_value));
EXPECT_GT(1'000'000LL, EzTimeValueToSbTime(&time_value) - sb_time);
// Now should be after the time I wrote this test.
EzTimeT ez_time = EzTimeTFromSbTime(EzTimeValueToSbTime(&time_value));
EXPECT_LE(kTestTimeWritten, ez_time);
// And it should be before 5 years after I wrote this test, at least for a
// while.
EXPECT_GE(kTestTimePastWritten, ez_time);
}
TEST(EzTimeTExplodeTest, Positive) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(EzTimeTExplode(&kTestTimePositive, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(2015 - 1900, exploded.tm_year);
EXPECT_EQ(9 - 1, exploded.tm_mon);
EXPECT_EQ(4, exploded.tm_wday);
EXPECT_EQ(24, exploded.tm_mday);
EXPECT_EQ(266, exploded.tm_yday);
EXPECT_EQ(19, exploded.tm_hour);
EXPECT_EQ(2, exploded.tm_min);
EXPECT_EQ(8, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, PositiveLocal) {
EzTimeExploded exploded = {0};
EXPECT_EQ(&exploded, EzTimeTExplodeLocal(&kTestTimePositive, &exploded));
EXPECT_EQ(2015 - 1900, exploded.tm_year);
EXPECT_EQ(9 - 1, exploded.tm_mon);
EXPECT_EQ(4, exploded.tm_wday);
// We don't know what the local time zone is, but it should be within a day of
// the UTC date.
EXPECT_LE(23, exploded.tm_mday);
EXPECT_GE(25, exploded.tm_mday);
EXPECT_EQ(266, exploded.tm_yday);
EXPECT_EQ(2, exploded.tm_min);
EXPECT_EQ(8, exploded.tm_sec);
}
TEST(EzTimeTExplodeTest, PositivePacific) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(
EzTimeTExplode(&kTestTimePositive, kEzTimeZonePacific, &exploded));
EXPECT_EQ(2015 - 1900, exploded.tm_year);
EXPECT_EQ(9 - 1, exploded.tm_mon);
EXPECT_EQ(4, exploded.tm_wday);
EXPECT_EQ(24, exploded.tm_mday);
EXPECT_EQ(266, exploded.tm_yday);
EXPECT_EQ(12, exploded.tm_hour);
EXPECT_EQ(2, exploded.tm_min);
EXPECT_EQ(8, exploded.tm_sec);
EXPECT_EQ(1, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, PosixZero) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(EzTimeTExplode(&kTestTimePosixZero, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(1970 - 1900, exploded.tm_year);
EXPECT_EQ(1 - 1, exploded.tm_mon);
EXPECT_EQ(4, exploded.tm_wday);
EXPECT_EQ(1, exploded.tm_mday);
EXPECT_EQ(0, exploded.tm_yday);
EXPECT_EQ(0, exploded.tm_hour);
EXPECT_EQ(0, exploded.tm_min);
EXPECT_EQ(0, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, PosixNegative) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(
EzTimeTExplode(&kTestTimePosixNegative, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(1945 - 1900, exploded.tm_year);
EXPECT_EQ(7 - 1, exploded.tm_mon);
EXPECT_EQ(1, exploded.tm_wday);
EXPECT_EQ(16, exploded.tm_mday);
EXPECT_EQ(196, exploded.tm_yday);
EXPECT_EQ(11, exploded.tm_hour);
EXPECT_EQ(29, exploded.tm_min);
EXPECT_EQ(21, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, WindowsZero) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(EzTimeTExplode(&kTestTimeWindowsZero, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(1601 - 1900, exploded.tm_year);
EXPECT_EQ(1 - 1, exploded.tm_mon);
EXPECT_EQ(1, exploded.tm_wday);
EXPECT_EQ(1, exploded.tm_mday);
EXPECT_EQ(0, exploded.tm_yday);
EXPECT_EQ(0, exploded.tm_hour);
EXPECT_EQ(0, exploded.tm_min);
EXPECT_EQ(0, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, WindowsNegative) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(
EzTimeTExplode(&kTestTimeWindowsNegative, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(1583 - 1900, exploded.tm_year);
EXPECT_EQ(1 - 1, exploded.tm_mon);
EXPECT_EQ(6, exploded.tm_wday);
EXPECT_EQ(1, exploded.tm_mday);
EXPECT_EQ(0, exploded.tm_yday);
EXPECT_EQ(0, exploded.tm_hour);
EXPECT_EQ(0, exploded.tm_min);
EXPECT_EQ(0, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTExplodeTest, NextCentury) {
EzTimeExploded exploded = {0};
EXPECT_TRUE(EzTimeTExplode(&kTestTimeNextCentury, kEzTimeZoneUTC, &exploded));
EXPECT_EQ(2101 - 1900, exploded.tm_year);
EXPECT_EQ(1 - 1, exploded.tm_mon);
EXPECT_EQ(6, exploded.tm_wday);
EXPECT_EQ(1, exploded.tm_mday);
EXPECT_EQ(0, exploded.tm_yday);
EXPECT_EQ(0, exploded.tm_hour);
EXPECT_EQ(0, exploded.tm_min);
EXPECT_EQ(0, exploded.tm_sec);
EXPECT_EQ(0, exploded.tm_isdst);
}
TEST(EzTimeTImplodeTest, Positive) {
EzTimeExploded exploded = {0};
exploded.tm_year = 2015 - 1900;
exploded.tm_mon = 9 - 1;
exploded.tm_mday = 24;
exploded.tm_hour = 19;
exploded.tm_min = 2;
exploded.tm_sec = 8;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimePositive, result);
}
TEST(EzTimeTImplodeTest, PosixZero) {
EzTimeExploded exploded = {0};
exploded.tm_year = 1970 - 1900;
exploded.tm_mon = 1 - 1;
exploded.tm_mday = 1;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimePosixZero, result);
}
TEST(EzTimeTImplodeTest, PosixNegative) {
EzTimeExploded exploded = {0};
exploded.tm_year = 1945 - 1900;
exploded.tm_mon = 7 - 1;
exploded.tm_mday = 16;
exploded.tm_hour = 11;
exploded.tm_min = 29;
exploded.tm_sec = 21;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimePosixNegative, result);
}
TEST(EzTimeTImplodeTest, WindowsZero) {
EzTimeExploded exploded = {0};
exploded.tm_year = 1601 - 1900;
exploded.tm_mon = 1 - 1;
exploded.tm_mday = 1;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimeWindowsZero, result);
}
TEST(EzTimeTImplodeTest, WindowsNegative) {
EzTimeExploded exploded = {0};
exploded.tm_year = 1583 - 1900;
exploded.tm_mon = 1 - 1;
exploded.tm_mday = 1;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimeWindowsNegative, result);
}
TEST(EzTimeTImplodeTest, NextCentury) {
EzTimeExploded exploded = {0};
exploded.tm_year = 2101 - 1900;
exploded.tm_mon = 1 - 1;
exploded.tm_mday = 1;
EzTimeT result = EzTimeTImplode(&exploded, kEzTimeZoneUTC);
EXPECT_EQ(kTestTimeNextCentury, result);
}
} // namespace
} // namespace eztime
} // namespace client_porting
} // namespace starboard