blob: 4af095c4997eee03bb96025a352eae279c1945e3 [file] [log] [blame]
Bob Kingd6820bb2020-04-28 15:37:02 +08001/**
2 * Copyright © 2020 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 "action_environment.hpp"
17#include "i2c_action.hpp"
18#include "i2c_interface.hpp"
19#include "pmbus_read_sensor_action.hpp"
20#include "pmbus_utils.hpp"
21
22#include <cstdint>
23#include <optional>
24#include <stdexcept>
25#include <string>
26
27#include <gtest/gtest.h>
28
29using namespace phosphor::power::regulators;
30
31TEST(PMBusReadSensorActionTests, Constructor)
32{
33 // Test where works: exponent value is specified
34 try
35 {
36 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
37 uint8_t command = 0x8C;
38 pmbus_utils::SensorDataFormat format{
39 pmbus_utils::SensorDataFormat::linear_16};
40 std::optional<int8_t> exponent{-8};
41 PMBusReadSensorAction action{type, command, format, exponent};
42 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
43 EXPECT_EQ(action.getCommand(), 0x8C);
44 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
45 EXPECT_EQ(action.getExponent().has_value(), true);
46 EXPECT_EQ(action.getExponent().value(), -8);
47 }
48 catch (...)
49 {
50 ADD_FAILURE() << "Should not have caught exception.";
51 }
52
53 // Test where works: exponent value is not specified
54 try
55 {
56 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
57 uint8_t command = 0x8C;
58 pmbus_utils::SensorDataFormat format{
59 pmbus_utils::SensorDataFormat::linear_11};
60 std::optional<int8_t> exponent{};
61 PMBusReadSensorAction action{type, command, format, exponent};
62 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::iout);
63 EXPECT_EQ(action.getCommand(), 0x8C);
64 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_11);
65 EXPECT_EQ(action.getExponent().has_value(), false);
66 }
67 catch (...)
68 {
69 ADD_FAILURE() << "Should not have caught exception.";
70 }
71}
72
73TEST(PMBusReadSensorActionTests, Execute)
74{
75 // TODO: Not implemented yet
76}
77
78TEST(PMBusReadSensorActionTests, GetCommand)
79{
80 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
81 uint8_t command = 0x8C;
82 pmbus_utils::SensorDataFormat format{
83 pmbus_utils::SensorDataFormat::linear_16};
84 std::optional<int8_t> exponent{-8};
85 PMBusReadSensorAction action{type, command, format, exponent};
86 EXPECT_EQ(action.getCommand(), 0x8C);
87}
88
89TEST(PMBusReadSensorActionTests, GetExponent)
90{
91 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
92 uint8_t command = 0x8C;
93 pmbus_utils::SensorDataFormat format{
94 pmbus_utils::SensorDataFormat::linear_16};
95
96 // Exponent value is specified
97 {
98 std::optional<int8_t> exponent{-9};
99 PMBusReadSensorAction action{type, command, format, exponent};
100 EXPECT_EQ(action.getExponent().has_value(), true);
101 EXPECT_EQ(action.getExponent().value(), -9);
102 }
103
104 // Exponent value is not specified
105 {
106 std::optional<int8_t> exponent{};
107 PMBusReadSensorAction action{type, command, format, exponent};
108 EXPECT_EQ(action.getExponent().has_value(), false);
109 }
110}
111
112TEST(PMBusReadSensorActionTests, GetFormat)
113{
114 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::iout};
115 uint8_t command = 0x8C;
116 pmbus_utils::SensorDataFormat format{
117 pmbus_utils::SensorDataFormat::linear_16};
118 std::optional<int8_t> exponent{-8};
119 PMBusReadSensorAction action{type, command, format, exponent};
120 EXPECT_EQ(action.getFormat(), pmbus_utils::SensorDataFormat::linear_16);
121}
122
123TEST(PMBusReadSensorActionTests, GetType)
124{
125 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::pout};
126 uint8_t command = 0x8C;
127 pmbus_utils::SensorDataFormat format{
128 pmbus_utils::SensorDataFormat::linear_16};
129 std::optional<int8_t> exponent{-8};
130 PMBusReadSensorAction action{type, command, format, exponent};
131 EXPECT_EQ(action.getType(), pmbus_utils::SensorValueType::pout);
132}
133
134TEST(PMBusReadSensorActionTests, ToString)
135{
136 // Test where exponent value is specified
137 {
138 pmbus_utils::SensorValueType type{
139 pmbus_utils::SensorValueType::temperature_peak};
140 uint8_t command = 0x8C;
141 pmbus_utils::SensorDataFormat format{
142 pmbus_utils::SensorDataFormat::linear_16};
143 std::optional<int8_t> exponent{-8};
144 PMBusReadSensorAction action{type, command, format, exponent};
145 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: "
146 "temperature_peak, command: 0x8C, format: "
147 "linear_16, exponent: -8 }");
148 }
149
150 // Test where exponent value is not specified
151 {
152 pmbus_utils::SensorValueType type{pmbus_utils::SensorValueType::vout};
153 uint8_t command = 0x8C;
154 pmbus_utils::SensorDataFormat format{
155 pmbus_utils::SensorDataFormat::linear_11};
156 std::optional<int8_t> exponent{};
157 PMBusReadSensorAction action{type, command, format, exponent};
158 EXPECT_EQ(action.toString(), "pmbus_read_sensor: { type: vout, "
159 "command: 0x8C, format: linear_11 }");
160 }
161}