#include "MockDtypes.h" #include "MockSerial.h" #include "session/Session.h" #include "session/MockStorage.h" #include template std::vector parseMessagePack(const uint8_t * data, uint32_t &startTime, uint32_t &startIndex) { int offset = 0; // map header const int expectedMapSize = 3; auto mapHeader = reinterpret_cast(&data[offset]); offset += 1; assert( *mapHeader == (0b10000000 | expectedMapSize), "Map Header wrong"); // string255: sessionStartTime auto stringHeader = reinterpret_cast(&data[offset++]); auto stringSize = reinterpret_cast(&data[offset++]); assert(*stringHeader == '\xd9', "String header wrong"); std::string sessionStartTimeStr = std::string((const char*)(&data[offset]), (size_t)(*stringSize)); assert( sessionStartTimeStr == "sessionStartTime", "sessionStartTime string is wrong"); offset += *stringSize; //uint32 auto intCode = reinterpret_cast(&data[offset++]); startTime = ntohl(*reinterpret_cast(&data[offset])); offset += 4; // string255: startIndex stringHeader = reinterpret_cast(&data[offset++]); stringSize = reinterpret_cast(&data[offset++]); assert(*stringHeader == '\xd9', "String header wrong"); std::string startIndexStr = std::string((const char*)(&data[offset]), (size_t)(*stringSize)); assert( startIndexStr == "startIndex", "startIndex string is wrong"); offset += *stringSize; //uint32 intCode = reinterpret_cast(&data[offset++]); startIndex = ntohl(*reinterpret_cast(&data[offset])); offset += 4; // string255: values stringHeader = reinterpret_cast(&data[offset++]); stringSize = reinterpret_cast(&data[offset++]); assert(*stringHeader == '\xd9', "String header wrong"); std::string valueStr = std::string((const char*)(&data[offset]), (size_t)(*stringSize)); assert( valueStr == "values", "values string is wrong"); offset += *stringSize; // vector auto vectorHeader = reinterpret_cast(&data[offset++]); assert( *vectorHeader == '\xc9', "Vector header wrong"); size_t vectorLength = ntohl(*reinterpret_cast(&data[offset])) / sizeof(Measurement_T); offset += 4; offset += 1; // jump over type auto vectorData = reinterpret_cast(&data[offset]); return std::vector(vectorData, vectorData + vectorLength); } void testSessionChunkAdd() { const uint_t size = 16; SessionChunk chunk; for( uint16_t i=0; i < size; ++i) { bool res = chunk.addPoint(i); assert(res, "Adding point failed"); assert( chunk.numMeasurements() == i+1, "Number of measurements reported wrong"); } bool res = chunk.addPoint(0); assert(!res, "Full chunk was not detected"); assert(chunk.numMeasurements() == size, "Point appears to be added"); } void testSessionChunkGetterSetter() { const uint_t size = 16; SessionChunk chunk; const uint32_t time = 244213; const uint32_t startIdx = 131; chunk.init(time, startIdx); assert( chunk.getStartIndex() == startIdx, "Start Index wrong"); assert( chunk.getStartTime() == time, "Start time wrong"); } void testSessionChunkSerialization() { const uint_t size = 16; const uint32_t startTime = 194232; const uint32_t startIndex = 1314; const uint_t fillSize = 12; SessionChunk chunk; chunk.init(startTime, startIndex); for( uint16_t i=0; i < fillSize; ++i) { bool res = chunk.addPoint(i); assert(res, "Adding point failed"); assert( chunk.numMeasurements() == i+1, "Number of measurements reported wrong"); } std::vector data; VectorAdaptor adaptor( &data ); StreamingMsgPackEncoder encoder(&adaptor); chunk.serialize(encoder); uint32_t readStartTime=0; uint32_t readStartIndex=0; auto result = parseMessagePack(&data[0], readStartTime, readStartIndex); assert(startIndex == readStartIndex && startTime == readStartTime, ""); assert(result.size() == fillSize, "Wrong result array size"); for( uint16_t i=0; i < fillSize; ++i) { assert(result[i] == i, "Wrong array contents"); } } void testSession() { const uint32_t SESSION_SIZE = 128; typedef Session MockSession; const uint32_t startTime = 194842; const uint_t fillSize = SESSION_SIZE * 4 + 7; MockSession session; session.init(startTime); for (uint16_t i = 0; i < fillSize; ++i) { session.addPoint(i); } std::vector data; VectorAdaptor adaptor( &data ); StreamingMsgPackEncoder encoder(&adaptor); session.serialize(encoder, 0); uint32_t readStartTime=0; uint32_t readStartIndex=0; auto result = parseMessagePack(&data[0], readStartTime, readStartIndex); assert(readStartIndex == 0 && startTime == readStartTime, ""); assert(result.size() == fillSize, "Wrong result array size"); for( uint16_t i=0; i < fillSize; ++i) { assert(result[i] == i, "Wrong array contents"); } } int main(int argc, char**argv) { testSessionChunkAdd(); testSessionChunkGetterSetter(); testSessionChunkSerialization(); testSession(); return 0; }