1 /**
2 * Copyright © 2019 IBM Corporation
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 "extensions/openpower-pels/bcd_time.hpp"
17
18 #include <gtest/gtest.h>
19
20 using namespace openpower::pels;
21
TEST(BCDTimeTest,ToBCDTest)22 TEST(BCDTimeTest, ToBCDTest)
23 {
24 EXPECT_EQ(toBCD(0), 0x00);
25 EXPECT_EQ(toBCD(1), 0x01);
26 EXPECT_EQ(toBCD(10), 0x10);
27 EXPECT_EQ(toBCD(99), 0x99);
28 EXPECT_EQ(toBCD(37), 0x37);
29 EXPECT_EQ(toBCD(60), 0x60);
30 EXPECT_EQ(toBCD(12345678), 0x12345678);
31 EXPECT_EQ(toBCD(0xF), 0x15);
32 }
33
TEST(BCDTimeTest,FlattenUnflattenTest)34 TEST(BCDTimeTest, FlattenUnflattenTest)
35 {
36 std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8};
37 Stream stream{data};
38 BCDTime bcd;
39
40 // Unflatten
41 stream >> bcd;
42
43 EXPECT_EQ(bcd.yearMSB, 1);
44 EXPECT_EQ(bcd.yearLSB, 2);
45 EXPECT_EQ(bcd.month, 3);
46 EXPECT_EQ(bcd.day, 4);
47 EXPECT_EQ(bcd.hour, 5);
48 EXPECT_EQ(bcd.minutes, 6);
49 EXPECT_EQ(bcd.seconds, 7);
50 EXPECT_EQ(bcd.hundredths, 8);
51
52 // Flatten
53 uint8_t val = 0x20;
54 bcd.yearMSB = val++;
55 bcd.yearLSB = val++;
56 bcd.month = val++;
57 bcd.day = val++;
58 bcd.hour = val++;
59 bcd.minutes = val++;
60 bcd.seconds = val++;
61 bcd.hundredths = val++;
62
63 stream.offset(0);
64 stream << bcd;
65
66 for (size_t i = 0; i < 8; i++)
67 {
68 EXPECT_EQ(data[i], 0x20 + i);
69 }
70 }
71
TEST(BCDTimeTest,ConvertTest)72 TEST(BCDTimeTest, ConvertTest)
73 {
74 // Convert a time_point into BCDTime
75 tm time_tm;
76 time_tm.tm_year = 125;
77 time_tm.tm_mon = 11;
78 time_tm.tm_mday = 31;
79 time_tm.tm_hour = 15;
80 time_tm.tm_min = 23;
81 time_tm.tm_sec = 42;
82 time_tm.tm_isdst = 0;
83
84 auto timepoint = std::chrono::system_clock::from_time_t(timegm(&time_tm));
85 auto timeInBCD = getBCDTime(timepoint);
86
87 EXPECT_EQ(timeInBCD.yearMSB, 0x20);
88 EXPECT_EQ(timeInBCD.yearLSB, 0x25);
89 EXPECT_EQ(timeInBCD.month, 0x12);
90 EXPECT_EQ(timeInBCD.day, 0x31);
91 EXPECT_EQ(timeInBCD.hour, 0x15);
92 EXPECT_EQ(timeInBCD.minutes, 0x23);
93 EXPECT_EQ(timeInBCD.seconds, 0x42);
94 EXPECT_EQ(timeInBCD.hundredths, 0x00);
95 }
96
TEST(BCDTimeTest,ConvertFromMSTest)97 TEST(BCDTimeTest, ConvertFromMSTest)
98 {
99 auto now = std::chrono::system_clock::now();
100 uint64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(
101 now.time_since_epoch())
102 .count();
103
104 ASSERT_EQ(getBCDTime(now), getBCDTime(ms));
105 }
106
TEST(BCDTimeTest,GetMillisecondsSinceEpochTest)107 TEST(BCDTimeTest, GetMillisecondsSinceEpochTest)
108 {
109 // Convert current time to a BCDTime to use
110 auto now = std::chrono::system_clock::now();
111 uint64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(
112 now.time_since_epoch())
113 .count();
114 auto bcdTime = getBCDTime(ms);
115
116 // BCDTime only tracks down to hundredths of a second (10ms),
117 // so some precision will be lost converting back to milliseconds.
118 // e.g. 12345 -> 12340
119 ms = ms - (ms % 10);
120
121 EXPECT_EQ(ms, getMillisecondsSinceEpoch(bcdTime));
122 }
123