blob: 0f66d24ccdb7a282ea5b81218b69e6cce356ebe7 [file] [log] [blame]
Shawn McCarney8a3afd72020-03-12 14:28:44 -05001/**
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 "chassis.hpp"
17#include "device.hpp"
18#include "i2c_interface.hpp"
Shawn McCarneydb0b8332020-04-06 14:13:04 -050019#include "id_map.hpp"
Shawn McCarney8a3afd72020-03-12 14:28:44 -050020#include "test_utils.hpp"
21
22#include <memory>
23#include <stdexcept>
24#include <utility>
25#include <vector>
26
27#include <gtest/gtest.h>
28
29using namespace phosphor::power::regulators;
30using namespace phosphor::power::regulators::test_utils;
31
32TEST(ChassisTests, Constructor)
33{
34 // Test where works: Only required parameters are specified
35 {
36 Chassis chassis{2};
37 EXPECT_EQ(chassis.getNumber(), 2);
38 EXPECT_EQ(chassis.getDevices().size(), 0);
39 }
40
41 // Test where works: All parameters are specified
42 {
43 // Create vector of Device objects
44 std::vector<std::unique_ptr<Device>> devices{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -050045 devices.emplace_back(createDevice("vdd_reg1"));
46 devices.emplace_back(createDevice("vdd_reg2"));
Shawn McCarney8a3afd72020-03-12 14:28:44 -050047
48 // Create Chassis
49 Chassis chassis{1, std::move(devices)};
50 EXPECT_EQ(chassis.getNumber(), 1);
51 EXPECT_EQ(chassis.getDevices().size(), 2);
52 }
53
54 // Test where fails: Invalid chassis number < 1
55 try
56 {
57 Chassis chassis{0};
58 ADD_FAILURE() << "Should not have reached this line.";
59 }
60 catch (const std::invalid_argument& e)
61 {
62 EXPECT_STREQ(e.what(), "Invalid chassis number: 0");
63 }
64 catch (...)
65 {
66 ADD_FAILURE() << "Should not have caught exception.";
67 }
68}
69
Shawn McCarneydb0b8332020-04-06 14:13:04 -050070TEST(ChassisTests, AddToIDMap)
71{
72 // Create vector of Device objects
73 std::vector<std::unique_ptr<Device>> devices{};
74 devices.emplace_back(createDevice("reg1", {"rail1"}));
75 devices.emplace_back(createDevice("reg2", {"rail2a", "rail2b"}));
76 devices.emplace_back(createDevice("reg3"));
77
78 // Create Chassis
79 Chassis chassis{1, std::move(devices)};
80
81 // Add Device and Rail objects within the Chassis to an IDMap
82 IDMap idMap{};
83 chassis.addToIDMap(idMap);
84
85 // Verify all Devices are in the IDMap
86 EXPECT_NO_THROW(idMap.getDevice("reg1"));
87 EXPECT_NO_THROW(idMap.getDevice("reg2"));
88 EXPECT_NO_THROW(idMap.getDevice("reg3"));
89 EXPECT_THROW(idMap.getDevice("reg4"), std::invalid_argument);
90
91 // Verify all Rails are in the IDMap
92 EXPECT_NO_THROW(idMap.getRail("rail1"));
93 EXPECT_NO_THROW(idMap.getRail("rail2a"));
94 EXPECT_NO_THROW(idMap.getRail("rail2b"));
95 EXPECT_THROW(idMap.getRail("rail3"), std::invalid_argument);
96}
97
Shawn McCarney8a3afd72020-03-12 14:28:44 -050098TEST(ChassisTests, GetDevices)
99{
100 // Test where no devices were specified in constructor
101 {
102 Chassis chassis{2};
103 EXPECT_EQ(chassis.getDevices().size(), 0);
104 }
105
106 // Test where devices were specified in constructor
107 {
108 // Create vector of Device objects
109 std::vector<std::unique_ptr<Device>> devices{};
Shawn McCarneydb0b8332020-04-06 14:13:04 -0500110 devices.emplace_back(createDevice("vdd_reg1"));
111 devices.emplace_back(createDevice("vdd_reg2"));
Shawn McCarney8a3afd72020-03-12 14:28:44 -0500112
113 // Create Chassis
114 Chassis chassis{1, std::move(devices)};
115 EXPECT_EQ(chassis.getDevices().size(), 2);
116 EXPECT_EQ(chassis.getDevices()[0]->getID(), "vdd_reg1");
117 EXPECT_EQ(chassis.getDevices()[1]->getID(), "vdd_reg2");
118 }
119}
120
121TEST(ChassisTests, GetNumber)
122{
123 Chassis chassis{3};
124 EXPECT_EQ(chassis.getNumber(), 3);
125}