From a9135696a58b8ec783ffa46770c4869541d21846 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 15 Jan 2021 18:07:32 +0100 Subject: [PATCH] array printer tests complete --- globalfunctions/arrayprinter.cpp | 57 +++++++++++-------- globalfunctions/arrayprinter.h | 9 +-- serviceinterface/ServiceInterfaceBuffer.cpp | 4 +- serviceinterface/ServiceInterfaceBuffer.h | 2 + serviceinterface/ServiceInterfaceStream.cpp | 4 ++ serviceinterface/ServiceInterfaceStream.h | 2 + unittest/internal/InternalUnitTester.cpp | 2 +- .../globalfunctions/TestArrayPrinter.cpp | 25 ++++++++ .../globalfunctions/TestArrayPrinter.h | 3 + 9 files changed, 77 insertions(+), 31 deletions(-) diff --git a/globalfunctions/arrayprinter.cpp b/globalfunctions/arrayprinter.cpp index 6dc959d7..7dc056b0 100644 --- a/globalfunctions/arrayprinter.cpp +++ b/globalfunctions/arrayprinter.cpp @@ -2,20 +2,21 @@ #include "../serviceinterface/ServiceInterface.h" #include + void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, bool printInfo, size_t maxCharPerLine) { #if FSFW_CPP_OSTREAM_ENABLED == 1 if(printInfo) { - sif::info << "Printing data with size " << size << ": "; + sif::info << "Printing data with size " << size << ": " << std::endl; } - sif::info << "["; #else #if FSFW_NO_C99_IO == 1 - sif::printInfo("Printing data with size %lu: [", static_cast(size)); + sif::printInfo("Printing data with size %lu: \n", static_cast(size)); #else - sif::printInfo("Printing data with size %zu: [", size); + sif::printInfo("Printing data with size %zu: \n", size); #endif /* FSFW_NO_C99_IO == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ + if(type == OutputType::HEX) { arrayprinter::printHex(data, size, maxCharPerLine); } @@ -30,19 +31,23 @@ void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, void arrayprinter::printHex(const uint8_t *data, size_t size, size_t maxCharPerLine) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::info << std::hex; + if(sif::info.crAdditionEnabled()) { + std::cout << "\r" << std::endl; + } + + std::cout << "[" << std::hex; for(size_t i = 0; i < size; i++) { - sif::info << "0x" << static_cast(data[i]); + std::cout << "0x" << static_cast(data[i]); if(i < size - 1) { - sif::info << " , "; - if(i > 0 and i % maxCharPerLine == 0) { - sif::info << std::endl; + std::cout << " , "; + if(i > 0 and (i + 1) % maxCharPerLine == 0) { + std::cout << std::endl; } } } - sif::info << std::dec; - sif::info << "]" << std::endl; + std::cout << std::dec; + std::cout << "]" << std::endl; #else // General format: 0x01, 0x02, 0x03 so it is number of chars times 6 // plus line break plus small safety margin. @@ -57,28 +62,33 @@ void arrayprinter::printHex(const uint8_t *data, size_t size, currentPos += snprintf(printBuffer + currentPos, 6, "0x%02x", data[i]); if(i < size - 1) { currentPos += sprintf(printBuffer + currentPos, ", "); - if(i > 0 and i % maxCharPerLine == 0) { + if(i > 0 and (i + 1) % maxCharPerLine == 0) { currentPos += sprintf(printBuffer + currentPos, "\n"); } } } + printf("[%s]\n", printBuffer); #endif } void arrayprinter::printDec(const uint8_t *data, size_t size, size_t maxCharPerLine) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::info << std::dec; + if(sif::info.crAdditionEnabled()) { + std::cout << "\r" << std::endl; + } + + std::cout << "[" << std::dec; for(size_t i = 0; i < size; i++) { - sif::info << static_cast(data[i]); + std::cout << static_cast(data[i]); if(i < size - 1){ - sif::info << " , "; - if(i > 0 and i % maxCharPerLine == 0) { - sif::info << std::endl; + std::cout << " , "; + if(i > 0 and (i + 1) % maxCharPerLine == 0) { + std::cout << std::endl; } } } - sif::info << "]" << std::endl; + std::cout << "]" << std::endl; #else // General format: 32, 243, -12 so it is number of chars times 5 // plus line break plus small safety margin. @@ -93,26 +103,23 @@ void arrayprinter::printDec(const uint8_t *data, size_t size, currentPos += snprintf(printBuffer + currentPos, 3, "%d", data[i]); if(i < size - 1) { currentPos += sprintf(printBuffer + currentPos, ", "); - if(i > 0 and i % maxCharPerLine == 0) { + if(i > 0 and (i + 1) % maxCharPerLine == 0) { currentPos += sprintf(printBuffer + currentPos, "\n"); } } } + printf("[%s]\n", printBuffer); #endif } void arrayprinter::printBin(const uint8_t *data, size_t size) { #if FSFW_CPP_OSTREAM_ENABLED == 1 - sif::info << "\n" << std::flush; for(size_t i = 0; i < size; i++) { - sif::info << "Byte " << i + 1 << ": 0b" << std::bitset<8>(data[i]) << "," << std::endl; + sif::info << "Byte " << i + 1 << ": 0b" << std::bitset<8>(data[i]) << std::endl; } - sif::info << "]" << std::endl; #else - sif::printInfo("\n"); for(size_t i = 0; i < size; i++) { - sif::printInfo("Byte %d: 0b" BYTE_TO_BINARY_PATTERN ",\n", BYTE_TO_BINARY(data[i])); + sif::printInfo("Byte %d: 0b" BYTE_TO_BINARY_PATTERN "\n", i + 1, BYTE_TO_BINARY(data[i])); } - sif::printInfo("]\n"); #endif } diff --git a/globalfunctions/arrayprinter.h b/globalfunctions/arrayprinter.h index 6dfa6ecd..e6b3ea6b 100644 --- a/globalfunctions/arrayprinter.h +++ b/globalfunctions/arrayprinter.h @@ -10,13 +10,14 @@ enum class OutputType { BIN }; -//! TODO: Write unit tests for this module. namespace arrayprinter { + + void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, - bool printInfo = true, size_t maxCharPerLine = 12); -void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); -void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); + bool printInfo = true, size_t maxCharPerLine = 10); +void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 10); +void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 10); void printBin(const uint8_t* data, size_t size); } diff --git a/serviceinterface/ServiceInterfaceBuffer.cpp b/serviceinterface/ServiceInterfaceBuffer.cpp index 1a02d6e9..3f624564 100644 --- a/serviceinterface/ServiceInterfaceBuffer.cpp +++ b/serviceinterface/ServiceInterfaceBuffer.cpp @@ -167,7 +167,9 @@ std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) { return &preamble; } - +bool ServiceInterfaceBuffer::crAdditionEnabled() const { + return addCrToPreamble; +} #ifdef UT699 #include "../osal/rtems/Interrupt.h" diff --git a/serviceinterface/ServiceInterfaceBuffer.h b/serviceinterface/ServiceInterfaceBuffer.h index b1a50848..9d3ce069 100644 --- a/serviceinterface/ServiceInterfaceBuffer.h +++ b/serviceinterface/ServiceInterfaceBuffer.h @@ -70,6 +70,8 @@ private: void putChars(char const* begin, char const* end); std::string* getPreamble(size_t * preambleSize = nullptr); + + bool crAdditionEnabled() const; }; #endif diff --git a/serviceinterface/ServiceInterfaceStream.cpp b/serviceinterface/ServiceInterfaceStream.cpp index 59526536..ad14cd04 100644 --- a/serviceinterface/ServiceInterfaceStream.cpp +++ b/serviceinterface/ServiceInterfaceStream.cpp @@ -15,5 +15,9 @@ std::string* ServiceInterfaceStream::getPreamble() { return streambuf.getPreamble(); } +bool ServiceInterfaceStream::crAdditionEnabled() const { + return streambuf.crAdditionEnabled(); +} + #endif diff --git a/serviceinterface/ServiceInterfaceStream.h b/serviceinterface/ServiceInterfaceStream.h index f3cb2cd0..e3c0ea7a 100644 --- a/serviceinterface/ServiceInterfaceStream.h +++ b/serviceinterface/ServiceInterfaceStream.h @@ -39,6 +39,8 @@ public: */ std::string* getPreamble(); + bool crAdditionEnabled() const; + protected: ServiceInterfaceBuffer streambuf; }; diff --git a/unittest/internal/InternalUnitTester.cpp b/unittest/internal/InternalUnitTester.cpp index f89d093a..a9394ad3 100644 --- a/unittest/internal/InternalUnitTester.cpp +++ b/unittest/internal/InternalUnitTester.cpp @@ -32,7 +32,7 @@ ReturnValue_t InternalUnitTester::performTests(struct InternalUnitTester::TestCo #if FSFW_CPP_OSTREAM_ENABLED == 1 sif::info << "Internal unit tests finished." << std::endl; #else - sif::printInfo("Running internal unit tests..\n"); + sif::printInfo("Internal unit tests finished.\n"); #endif return RETURN_OK; } diff --git a/unittest/internal/globalfunctions/TestArrayPrinter.cpp b/unittest/internal/globalfunctions/TestArrayPrinter.cpp index c26faa85..de016d19 100644 --- a/unittest/internal/globalfunctions/TestArrayPrinter.cpp +++ b/unittest/internal/globalfunctions/TestArrayPrinter.cpp @@ -1,4 +1,29 @@ #include "TestArrayPrinter.h" void arrayprinter::testArrayPrinter() { + { + const std::array testDataSmall = {0x01, 0x02, 0x03, 0x04, 0x05}; + arrayprinter::print(testDataSmall.data(), testDataSmall.size()); + arrayprinter::print(testDataSmall.data(), testDataSmall.size(), OutputType::DEC); + arrayprinter::print(testDataSmall.data(), testDataSmall.size(), OutputType::BIN); + } + + { + std::array testDataMed; + for(size_t idx = 0; idx < testDataMed.size(); idx ++) { + testDataMed[idx] = testDataMed.size() - idx; + } + arrayprinter::print(testDataMed.data(), testDataMed.size()); + arrayprinter::print(testDataMed.data(), testDataMed.size(), OutputType::DEC, 8); + } + + { + std::array testDataLarge; + for(size_t idx = 0; idx < testDataLarge.size(); idx ++) { + testDataLarge[idx] = idx; + } + arrayprinter::print(testDataLarge.data(), testDataLarge.size()); + arrayprinter::print(testDataLarge.data(), testDataLarge.size(), OutputType::DEC); + } + } diff --git a/unittest/internal/globalfunctions/TestArrayPrinter.h b/unittest/internal/globalfunctions/TestArrayPrinter.h index 714c3aed..50b82ca0 100644 --- a/unittest/internal/globalfunctions/TestArrayPrinter.h +++ b/unittest/internal/globalfunctions/TestArrayPrinter.h @@ -1,6 +1,9 @@ #ifndef FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_ #define FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_ +#include +#include + namespace arrayprinter { void testArrayPrinter();