Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 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 | |
James Feist | f6a0783 | 2019-10-29 09:39:14 -0700 | [diff] [blame] | 55 | // check negative values |
| 56 | expected = 236; // 2s compliment -20 |
| 57 | scaledVal = ipmi::scaleIPMIValueFromDouble(-20, mValue, rExp, bValue, bExp, |
| 58 | bSigned); |
| 59 | EXPECT_NEAR(scaledVal, expected, expected * 0.01); |
| 60 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 61 | // fan example |
| 62 | maxValue = 16000; |
| 63 | minValue = 0; |
| 64 | |
| 65 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 66 | bExp, bSigned); |
| 67 | EXPECT_EQ(result, true); |
| 68 | if (result) |
| 69 | { |
| 70 | EXPECT_EQ(bSigned, false); |
James Feist | aecaef7 | 2019-04-26 10:30:32 -0700 | [diff] [blame] | 71 | EXPECT_EQ(mValue, floor((16000.0 / 0xFF) + 0.5)); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 72 | EXPECT_EQ(rExp, 0); |
| 73 | EXPECT_EQ(bValue, 0); |
| 74 | EXPECT_EQ(bExp, 0); |
| 75 | } |
| 76 | |
| 77 | // voltage sensor example |
| 78 | maxValue = 20; |
| 79 | minValue = 0; |
| 80 | |
| 81 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 82 | bExp, bSigned); |
| 83 | EXPECT_EQ(result, true); |
| 84 | if (result) |
| 85 | { |
| 86 | EXPECT_EQ(bSigned, false); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 87 | EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 88 | EXPECT_EQ(rExp, -3); |
| 89 | EXPECT_EQ(bValue, 0); |
| 90 | EXPECT_EQ(bExp, 0); |
| 91 | } |
| 92 | scaledVal = ipmi::scaleIPMIValueFromDouble(12.2, mValue, rExp, bValue, bExp, |
| 93 | bSigned); |
| 94 | |
| 95 | expected = 12.2 / (mValue * std::pow(10, rExp)); |
| 96 | EXPECT_NEAR(scaledVal, expected, expected * 0.01); |
| 97 | |
| 98 | // shifted fan example |
| 99 | maxValue = 16000; |
| 100 | minValue = 8000; |
| 101 | |
| 102 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 103 | bExp, bSigned); |
| 104 | EXPECT_EQ(result, true); |
| 105 | |
| 106 | if (result) |
| 107 | { |
| 108 | EXPECT_EQ(bSigned, false); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 109 | EXPECT_EQ(mValue, floor(8000.0 / 0xFF)); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 110 | EXPECT_EQ(rExp, 0); |
| 111 | EXPECT_EQ(bValue, 80); |
| 112 | EXPECT_EQ(bExp, 2); |
| 113 | } |
| 114 | |
| 115 | // signed voltage sensor example |
| 116 | maxValue = 10; |
| 117 | minValue = -10; |
| 118 | |
| 119 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 120 | bExp, bSigned); |
| 121 | EXPECT_EQ(result, true); |
| 122 | if (result) |
| 123 | { |
| 124 | EXPECT_EQ(bSigned, true); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 125 | EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 126 | EXPECT_EQ(rExp, -3); |
| 127 | EXPECT_EQ(bValue, 0); |
| 128 | EXPECT_EQ(bExp, 0); |
| 129 | } |
| 130 | |
| 131 | scaledVal = |
| 132 | ipmi::scaleIPMIValueFromDouble(5, mValue, rExp, bValue, bExp, bSigned); |
| 133 | |
| 134 | expected = 5 / (mValue * std::pow(10, rExp)); |
| 135 | EXPECT_NEAR(scaledVal, expected, expected * 0.01); |
| 136 | |
James Feist | aecaef7 | 2019-04-26 10:30:32 -0700 | [diff] [blame] | 137 | // reading = max example |
| 138 | maxValue = 277; |
| 139 | minValue = 0; |
| 140 | |
| 141 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 142 | bExp, bSigned); |
| 143 | EXPECT_EQ(result, true); |
| 144 | if (result) |
| 145 | { |
| 146 | EXPECT_EQ(bSigned, false); |
| 147 | } |
| 148 | |
| 149 | scaledVal = ipmi::scaleIPMIValueFromDouble(maxValue, mValue, rExp, bValue, |
| 150 | bExp, bSigned); |
| 151 | |
| 152 | expected = 0xFF; |
| 153 | EXPECT_NEAR(scaledVal, expected, expected * 0.01); |
| 154 | |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 155 | // 0, 0 failure |
| 156 | maxValue = 0; |
| 157 | minValue = 0; |
| 158 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 159 | bExp, bSigned); |
| 160 | EXPECT_EQ(result, false); |
| 161 | |
| 162 | // too close failure |
| 163 | maxValue = 12; |
| 164 | minValue = 10; |
| 165 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 166 | bExp, bSigned); |
| 167 | EXPECT_EQ(result, false); |
| 168 | } |