OmniSciDB  c07336695a
DateTimeUtilsTest.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2018 OmniSci, Inc.
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 
17 #include "../../Shared/DateConverters.h"
18 #include "Tests/TestHelpers.h"
19 
20 #include <gtest/gtest.h>
21 
22 namespace {
23 
24 template <class T>
25 void compare_epoch(const std::vector<T>& expected, const std::vector<T>& actual) {
26  ASSERT_EQ(expected.size(), actual.size());
27  for (size_t i = 0; i < actual.size(); i++) {
28  ASSERT_EQ(expected[i], actual[i]);
29  }
30 }
31 
32 } // namespace
33 
35  const std::vector<int64_t> epoch{-31496400, -31536000, 31579200, 31536000};
36  const std::vector<int64_t> actual_days{-365, -365, 365, 365};
37  const std::vector<int64_t> expected_seconds{-31536000, -31536000, 31536000, 31536000};
38 };
39 
40 TEST(DATE, EpochSecondsToDaysTest) {
41  const auto sample = SampleDateEpochs();
42  std::vector<int64_t> computed;
43  for (const auto ep : sample.epoch) {
44  computed.push_back(DateConverters::get_epoch_days_from_seconds(ep));
45  }
46  compare_epoch(computed, sample.actual_days);
47 }
48 
49 TEST(DATE, EpochDaysToSecondsTest) {
50  const auto sample = SampleDateEpochs();
51  std::vector<int64_t> computed;
52  for (const auto ep : sample.actual_days) {
53  computed.push_back(DateConverters::get_epoch_seconds_from_days(ep));
54  }
55  compare_epoch(computed, sample.expected_seconds);
56 }
57 
58 int main(int argc, char** argv) {
60  testing::InitGoogleTest(&argc, argv);
61 
62  int err{0};
63  try {
64  err = RUN_ALL_TESTS();
65  } catch (const std::exception& e) {
66  LOG(ERROR) << e.what();
67  }
68  return err;
69 }
const std::vector< int64_t > expected_seconds
const std::vector< int64_t > epoch
#define LOG(tag)
Definition: Logger.h:182
void compare_epoch(const std::vector< T > &expected, const std::vector< T > &actual)
int64_t get_epoch_seconds_from_days(const int64_t days)
int main(int argc, char **argv)
const std::vector< int64_t > actual_days
void init_logger_stderr_only(int argc, char const *const *argv)
Definition: TestHelpers.h:194
TEST(DATE, EpochSecondsToDaysTest)
int64_t get_epoch_days_from_seconds(const int64_t seconds)