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 | |
| 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); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 65 | EXPECT_EQ(mValue, floor(16000.0 / 0xFF)); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 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); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 81 | EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 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); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 103 | EXPECT_EQ(mValue, floor(8000.0 / 0xFF)); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 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); |
James Feist | 39417c7 | 2019-01-03 09:14:24 -0800 | [diff] [blame] | 119 | EXPECT_EQ(mValue, floor(((20.0 / 0xFF) / std::pow(10, rExp)))); |
Jason M. Bills | 3f7c5e4 | 2018-10-03 14:00:41 -0700 | [diff] [blame] | 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 | // 0, 0 failure |
| 132 | maxValue = 0; |
| 133 | minValue = 0; |
| 134 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 135 | bExp, bSigned); |
| 136 | EXPECT_EQ(result, false); |
| 137 | |
| 138 | // too close failure |
| 139 | maxValue = 12; |
| 140 | minValue = 10; |
| 141 | result = ipmi::getSensorAttributes(maxValue, minValue, mValue, rExp, bValue, |
| 142 | bExp, bSigned); |
| 143 | EXPECT_EQ(result, false); |
| 144 | } |