1 #include "extensions/openpower-pels/bcd_time.hpp"
2 
3 #include <gtest/gtest.h>
4 
5 using namespace openpower::pels;
6 
7 TEST(BCDTimeTest, ToBCDTest)
8 {
9     EXPECT_EQ(toBCD(0), 0x00);
10     EXPECT_EQ(toBCD(1), 0x01);
11     EXPECT_EQ(toBCD(10), 0x10);
12     EXPECT_EQ(toBCD(99), 0x99);
13     EXPECT_EQ(toBCD(37), 0x37);
14     EXPECT_EQ(toBCD(60), 0x60);
15     EXPECT_EQ(toBCD(12345678), 0x12345678);
16     EXPECT_EQ(toBCD(0xF), 0x15);
17 }
18 
19 TEST(BCDTimeTest, FlattenUnflattenTest)
20 {
21     std::vector<uint8_t> data{1, 2, 3, 4, 5, 6, 7, 8};
22     Stream stream{data};
23     BCDTime bcd;
24 
25     // Unflatten
26     stream >> bcd;
27 
28     EXPECT_EQ(bcd.yearMSB, 1);
29     EXPECT_EQ(bcd.yearLSB, 2);
30     EXPECT_EQ(bcd.month, 3);
31     EXPECT_EQ(bcd.day, 4);
32     EXPECT_EQ(bcd.hour, 5);
33     EXPECT_EQ(bcd.minutes, 6);
34     EXPECT_EQ(bcd.seconds, 7);
35     EXPECT_EQ(bcd.hundredths, 8);
36 
37     // Flatten
38     uint8_t val = 0x20;
39     bcd.yearMSB = val++;
40     bcd.yearLSB = val++;
41     bcd.month = val++;
42     bcd.day = val++;
43     bcd.hour = val++;
44     bcd.minutes = val++;
45     bcd.seconds = val++;
46     bcd.hundredths = val++;
47 
48     stream.offset(0);
49     stream << bcd;
50 
51     for (size_t i = 0; i < 8; i++)
52     {
53         EXPECT_EQ(data[i], 0x20 + i);
54     }
55 }
56 
57 TEST(BCDTimeTest, ConvertTest)
58 {
59     // Convert a time_point into BCDTime
60     tm time_tm;
61     time_tm.tm_year = 125;
62     time_tm.tm_mon = 11;
63     time_tm.tm_mday = 31;
64     time_tm.tm_hour = 15;
65     time_tm.tm_min = 23;
66     time_tm.tm_sec = 42;
67     time_tm.tm_isdst = 0;
68 
69     auto timepoint = std::chrono::system_clock::from_time_t(mktime(&time_tm));
70     auto timeInBCD = getBCDTime(timepoint);
71 
72     EXPECT_EQ(timeInBCD.yearMSB, 0x20);
73     EXPECT_EQ(timeInBCD.yearLSB, 0x25);
74     EXPECT_EQ(timeInBCD.month, 0x12);
75     EXPECT_EQ(timeInBCD.day, 0x31);
76     EXPECT_EQ(timeInBCD.hour, 0x15);
77     EXPECT_EQ(timeInBCD.minutes, 0x23);
78     EXPECT_EQ(timeInBCD.seconds, 0x42);
79     EXPECT_EQ(timeInBCD.hundredths, 0x00);
80 }
81 
82 TEST(BCDTimeTest, ConvertFromMSTest)
83 {
84     auto now = std::chrono::system_clock::now();
85     uint64_t ms = std::chrono::duration_cast<std::chrono::milliseconds>(
86                       now.time_since_epoch())
87                       .count();
88 
89     ASSERT_EQ(getBCDTime(now), getBCDTime(ms));
90 }
91