1 #include <cmath> 2 #include <sensorutils.hpp> 3 4 #include "gtest/gtest.h" 5 6 TEST(sensorutils, TranslateToIPMI) 7 { 8 /*bool getSensorAttributes(double maxValue, double minValue, int16_t 9 &mValue, int8_t &rExp, int16_t &bValue, int8_t &bExp, bool &bSigned); */ 10 // normal unsigned sensor 11 double maxValue = 0xFF; 12 double minValue = 0x0; 13 int16_t mValue; 14 int8_t rExp; 15 int16_t bValue; 16 int8_t bExp; 17 bool bSigned; 18 bool result; 19 20 uint8_t scaledVal; 21 22 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 23 bExp, bSigned); 24 EXPECT_EQ(result, true); 25 if (result) 26 { 27 EXPECT_EQ(bSigned, false); 28 EXPECT_EQ(mValue, 1); 29 EXPECT_EQ(rExp, 0); 30 EXPECT_EQ(bValue, 0); 31 EXPECT_EQ(bExp, 0); 32 } 33 double expected = 0x50; 34 scaledVal = ipmi::scaleIPMIValueFromDouble(0x50, mValue, rExp, bValue, bExp, 35 bSigned); 36 EXPECT_NEAR(scaledVal, expected, expected * 0.01); 37 38 // normal signed sensor 39 maxValue = 127; 40 minValue = -128; 41 42 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 43 bExp, bSigned); 44 EXPECT_EQ(result, true); 45 46 if (result) 47 { 48 EXPECT_EQ(bSigned, true); 49 EXPECT_EQ(mValue, 1); 50 EXPECT_EQ(rExp, 0); 51 EXPECT_EQ(bValue, 0); 52 EXPECT_EQ(bExp, 0); 53 } 54 55 // fan example 56 maxValue = 16000; 57 minValue = 0; 58 59 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 60 bExp, bSigned); 61 EXPECT_EQ(result, true); 62 if (result) 63 { 64 EXPECT_EQ(bSigned, false); 65 EXPECT_EQ(mValue, floor((16000.0 / 0xFF) + 0.5)); 66 EXPECT_EQ(rExp, 0); 67 EXPECT_EQ(bValue, 0); 68 EXPECT_EQ(bExp, 0); 69 } 70 71 // voltage sensor example 72 maxValue = 20; 73 minValue = 0; 74 75 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 76 bExp, bSigned); 77 EXPECT_EQ(result, true); 78 if (result) 79 { 80 EXPECT_EQ(bSigned, false); 81 EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); 82 EXPECT_EQ(rExp, -3); 83 EXPECT_EQ(bValue, 0); 84 EXPECT_EQ(bExp, 0); 85 } 86 scaledVal = ipmi::scaleIPMIValueFromDouble(12.2, mValue, rExp, bValue, bExp, 87 bSigned); 88 89 expected = 12.2 / (mValue * std::pow(10, rExp)); 90 EXPECT_NEAR(scaledVal, expected, expected * 0.01); 91 92 // shifted fan example 93 maxValue = 16000; 94 minValue = 8000; 95 96 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 97 bExp, bSigned); 98 EXPECT_EQ(result, true); 99 100 if (result) 101 { 102 EXPECT_EQ(bSigned, false); 103 EXPECT_EQ(mValue, floor(8000.0 / 0xFF)); 104 EXPECT_EQ(rExp, 0); 105 EXPECT_EQ(bValue, 80); 106 EXPECT_EQ(bExp, 2); 107 } 108 109 // signed voltage sensor example 110 maxValue = 10; 111 minValue = -10; 112 113 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 114 bExp, bSigned); 115 EXPECT_EQ(result, true); 116 if (result) 117 { 118 EXPECT_EQ(bSigned, true); 119 EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); 120 EXPECT_EQ(rExp, -3); 121 EXPECT_EQ(bValue, 0); 122 EXPECT_EQ(bExp, 0); 123 } 124 125 scaledVal = 126 ipmi::scaleIPMIValueFromDouble(5, mValue, rExp, bValue, bExp, bSigned); 127 128 expected = 5 / (mValue * std::pow(10, rExp)); 129 EXPECT_NEAR(scaledVal, expected, expected * 0.01); 130 131 // reading = max example 132 maxValue = 277; 133 minValue = 0; 134 135 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 136 bExp, bSigned); 137 EXPECT_EQ(result, true); 138 if (result) 139 { 140 EXPECT_EQ(bSigned, false); 141 } 142 143 scaledVal = ipmi::scaleIPMIValueFromDouble(maxValue, mValue, rExp, bValue, 144 bExp, bSigned); 145 146 expected = 0xFF; 147 EXPECT_NEAR(scaledVal, expected, expected * 0.01); 148 149 // 0, 0 failure 150 maxValue = 0; 151 minValue = 0; 152 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 153 bExp, bSigned); 154 EXPECT_EQ(result, false); 155 156 // too close failure 157 maxValue = 12; 158 minValue = 10; 159 result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, 160 bExp, bSigned); 161 EXPECT_EQ(result, false); 162 } 163