Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 1 | /** |
| 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 McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 19 | #include "id_map.hpp" |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 20 | #include "test_utils.hpp" |
| 21 | |
| 22 | #include <memory> |
| 23 | #include <stdexcept> |
| 24 | #include <utility> |
| 25 | #include <vector> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | using namespace phosphor::power::regulators; |
| 30 | using namespace phosphor::power::regulators::test_utils; |
| 31 | |
| 32 | TEST(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 McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 45 | devices.emplace_back(createDevice("vdd_reg1")); |
| 46 | devices.emplace_back(createDevice("vdd_reg2")); |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 47 | |
| 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 McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 70 | TEST(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 McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 98 | TEST(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 McCarney | db0b833 | 2020-04-06 14:13:04 -0500 | [diff] [blame] | 110 | devices.emplace_back(createDevice("vdd_reg1")); |
| 111 | devices.emplace_back(createDevice("vdd_reg2")); |
Shawn McCarney | 8a3afd7 | 2020-03-12 14:28:44 -0500 | [diff] [blame] | 112 | |
| 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 | |
| 121 | TEST(ChassisTests, GetNumber) |
| 122 | { |
| 123 | Chassis chassis{3}; |
| 124 | EXPECT_EQ(chassis.getNumber(), 3); |
| 125 | } |