blob: e500322f0bff0a18e263f75a74692a899f1b9910 [file] [log] [blame]
Matt Spinlerc180a182018-01-18 14:27:13 -06001/**
2 * Copyright © 2017 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 */
Matt Spinlerc180a182018-01-18 14:27:13 -060016#include "../record_manager.hpp"
Matt Spinlerf0f02b92018-10-25 16:12:43 -050017#include "names_values.hpp"
18
19#include <iostream>
20
21#include <gtest/gtest.h>
Matt Spinlerc180a182018-01-18 14:27:13 -060022
23using namespace witherspoon::power::history;
24
25/**
26 * Test the linearToInteger function with different
27 * combinations of positive and negative mantissas and
28 * exponents.
29 *
30 * Value = mantissa * 2**exponent
31 */
32TEST(LinearFormatTest, TestConversions)
33{
Matt Spinlerf0f02b92018-10-25 16:12:43 -050034 // Mantissa > 0, exponent = 0
Matt Spinlerc180a182018-01-18 14:27:13 -060035 EXPECT_EQ(0, RecordManager::linearToInteger(0));
36 EXPECT_EQ(1, RecordManager::linearToInteger(1));
37 EXPECT_EQ(38, RecordManager::linearToInteger(0x26));
38 EXPECT_EQ(1023, RecordManager::linearToInteger(0x3FF));
39
Matt Spinlerf0f02b92018-10-25 16:12:43 -050040 // Mantissa < 0, exponent = 0
Matt Spinlerc180a182018-01-18 14:27:13 -060041 EXPECT_EQ(-1, RecordManager::linearToInteger(0x7FF));
42 EXPECT_EQ(-20, RecordManager::linearToInteger(0x7EC));
43 EXPECT_EQ(-769, RecordManager::linearToInteger(0x4FF));
44 EXPECT_EQ(-989, RecordManager::linearToInteger(0x423));
45 EXPECT_EQ(-1024, RecordManager::linearToInteger(0x400));
46
Matt Spinlerf0f02b92018-10-25 16:12:43 -050047 // Mantissa >= 0, exponent > 0
48 // M = 1, E = 2
Matt Spinlerc180a182018-01-18 14:27:13 -060049 EXPECT_EQ(4, RecordManager::linearToInteger(0x1001));
50
Matt Spinlerf0f02b92018-10-25 16:12:43 -050051 // M = 1000, E = 10
Matt Spinlerc180a182018-01-18 14:27:13 -060052 EXPECT_EQ(1024000, RecordManager::linearToInteger(0x53E8));
53
Matt Spinlerf0f02b92018-10-25 16:12:43 -050054 // M = 10, E = 15
Matt Spinlerc180a182018-01-18 14:27:13 -060055 EXPECT_EQ(327680, RecordManager::linearToInteger(0x780A));
56
Matt Spinlerf0f02b92018-10-25 16:12:43 -050057 // Mantissa >= 0, exponent < 0
58 // M = 0, E = -1
Matt Spinlerc180a182018-01-18 14:27:13 -060059 EXPECT_EQ(0, RecordManager::linearToInteger(0xF800));
60
Matt Spinlerf0f02b92018-10-25 16:12:43 -050061 // M = 100, E = -2
Matt Spinlerc180a182018-01-18 14:27:13 -060062 EXPECT_EQ(25, RecordManager::linearToInteger(0xF064));
63
Matt Spinlerf0f02b92018-10-25 16:12:43 -050064 // Mantissa < 0, exponent < 0
65 // M = -100, E = -1
Matt Spinlerc180a182018-01-18 14:27:13 -060066 EXPECT_EQ(-50, RecordManager::linearToInteger(0xFF9C));
67
Matt Spinlerf0f02b92018-10-25 16:12:43 -050068 // M = -1024, E = -7
Matt Spinlerc180a182018-01-18 14:27:13 -060069 EXPECT_EQ(-8, RecordManager::linearToInteger(0xCC00));
70}
71
72/**
73 * @brief Helper function to create a record buffer
74 *
75 * @param sequenceID - the ID to use
76 * @param avgPower - the average power, in linear format
77 * @param maxPower - the max power, in linear format
78 *
79 * @return vector<uint8_t> the record buffer
80 */
Matt Spinlerf0f02b92018-10-25 16:12:43 -050081std::vector<uint8_t> makeRawRecord(uint8_t sequenceID, uint16_t avgPower,
82 uint16_t maxPower)
Matt Spinlerc180a182018-01-18 14:27:13 -060083{
84 std::vector<uint8_t> record;
85
86 record.push_back(sequenceID);
87 record.push_back(avgPower & 0xFF);
88 record.push_back(avgPower >> 8);
89 record.push_back(maxPower & 0xFF);
90 record.push_back(maxPower >> 8);
91
92 return record;
93}
94
Matt Spinlerc180a182018-01-18 14:27:13 -060095/**
96 * Test record queue management
97 */
98TEST(ManagerTest, TestRecordAdds)
99{
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500100 // Hold 5 max records. IDs roll over at 8.
Matt Spinlerc180a182018-01-18 14:27:13 -0600101 RecordManager mgr{5, 8};
102
103 EXPECT_EQ(0, mgr.getNumRecords());
104
105 mgr.add(makeRawRecord(0, 0, 0));
106 EXPECT_EQ(1, mgr.getNumRecords());
107
108 mgr.add(makeRawRecord(1, 0, 0));
109 EXPECT_EQ(2, mgr.getNumRecords());
110
111 mgr.add(makeRawRecord(2, 0, 0));
112 EXPECT_EQ(3, mgr.getNumRecords());
113
114 mgr.add(makeRawRecord(3, 0, 0));
115 EXPECT_EQ(4, mgr.getNumRecords());
116
117 mgr.add(makeRawRecord(4, 0, 0));
118 EXPECT_EQ(5, mgr.getNumRecords());
119
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500120 // start pruning
Matt Spinlerc180a182018-01-18 14:27:13 -0600121 mgr.add(makeRawRecord(5, 0, 0));
122 EXPECT_EQ(5, mgr.getNumRecords());
123
124 mgr.add(makeRawRecord(6, 0, 0));
125 EXPECT_EQ(5, mgr.getNumRecords());
126
127 mgr.add(makeRawRecord(7, 0, 0));
128 EXPECT_EQ(5, mgr.getNumRecords());
129
130 mgr.add(makeRawRecord(8, 0, 0));
131 EXPECT_EQ(5, mgr.getNumRecords());
132
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500133 // rollover
Matt Spinlerc180a182018-01-18 14:27:13 -0600134 mgr.add(makeRawRecord(0, 0, 0));
135 EXPECT_EQ(5, mgr.getNumRecords());
136
137 mgr.add(makeRawRecord(1, 0, 0));
138 EXPECT_EQ(5, mgr.getNumRecords());
139
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500140 // nonsequential ID, clear previous
Matt Spinlerc180a182018-01-18 14:27:13 -0600141 mgr.add(makeRawRecord(4, 0, 10));
142 EXPECT_EQ(1, mgr.getNumRecords());
143
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500144 // back to normal
Matt Spinlerc180a182018-01-18 14:27:13 -0600145 mgr.add(makeRawRecord(5, 1, 11));
146 EXPECT_EQ(2, mgr.getNumRecords());
147
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500148 // One more good record
Matt Spinlerc180a182018-01-18 14:27:13 -0600149 mgr.add(makeRawRecord(6, 2, 12));
150 EXPECT_EQ(3, mgr.getNumRecords());
151
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500152 // Add a garbage length record. No size change
Matt Spinlerc180a182018-01-18 14:27:13 -0600153 mgr.add(std::vector<uint8_t>(6, 0));
154 EXPECT_EQ(3, mgr.getNumRecords());
155
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500156 // Test the DBus Records
Matt Spinlerc180a182018-01-18 14:27:13 -0600157 auto avgRecords = mgr.getAverageRecords();
158 EXPECT_EQ(3, avgRecords.size());
159
160 auto maxRecords = mgr.getMaximumRecords();
161 EXPECT_EQ(3, maxRecords.size());
162
163 auto avg = 2;
164 for (const auto& r : avgRecords)
165 {
166 EXPECT_EQ(avg, std::get<1>(r));
167 avg--;
168 }
169
170 auto max = 12;
171 for (const auto& r : maxRecords)
172 {
173 EXPECT_EQ(max, std::get<1>(r));
174 max--;
175 }
176
Matt Spinlerf0f02b92018-10-25 16:12:43 -0500177 // Add a zero length record. Records cleared.
Matt Spinlerc180a182018-01-18 14:27:13 -0600178 mgr.add(std::vector<uint8_t>{});
179 EXPECT_EQ(0, mgr.getNumRecords());
180}