array printer tests complete

This commit is contained in:
Robin Müller 2021-01-15 18:07:32 +01:00
parent 38dd230887
commit a9135696a5
9 changed files with 77 additions and 31 deletions

View File

@ -2,20 +2,21 @@
#include "../serviceinterface/ServiceInterface.h" #include "../serviceinterface/ServiceInterface.h"
#include <bitset> #include <bitset>
void arrayprinter::print(const uint8_t *data, size_t size, OutputType type, void arrayprinter::print(const uint8_t *data, size_t size, OutputType type,
bool printInfo, size_t maxCharPerLine) { bool printInfo, size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
if(printInfo) { if(printInfo) {
sif::info << "Printing data with size " << size << ": "; sif::info << "Printing data with size " << size << ": " << std::endl;
} }
sif::info << "[";
#else #else
#if FSFW_NO_C99_IO == 1 #if FSFW_NO_C99_IO == 1
sif::printInfo("Printing data with size %lu: [", static_cast<unsigned long>(size)); sif::printInfo("Printing data with size %lu: \n", static_cast<unsigned long>(size));
#else #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_NO_C99_IO == 1 */
#endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */ #endif /* FSFW_CPP_OSTREAM_ENABLED == 1 */
if(type == OutputType::HEX) { if(type == OutputType::HEX) {
arrayprinter::printHex(data, size, maxCharPerLine); 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, void arrayprinter::printHex(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #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++) { for(size_t i = 0; i < size; i++) {
sif::info << "0x" << static_cast<int>(data[i]); std::cout << "0x" << static_cast<int>(data[i]);
if(i < size - 1) { if(i < size - 1) {
sif::info << " , "; std::cout << " , ";
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and (i + 1) % maxCharPerLine == 0) {
sif::info << std::endl; std::cout << std::endl;
} }
} }
} }
sif::info << std::dec; std::cout << std::dec;
sif::info << "]" << std::endl; std::cout << "]" << std::endl;
#else #else
// General format: 0x01, 0x02, 0x03 so it is number of chars times 6 // General format: 0x01, 0x02, 0x03 so it is number of chars times 6
// plus line break plus small safety margin. // 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]); currentPos += snprintf(printBuffer + currentPos, 6, "0x%02x", data[i]);
if(i < size - 1) { if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", "); currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and (i + 1) % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n"); currentPos += sprintf(printBuffer + currentPos, "\n");
} }
} }
} }
printf("[%s]\n", printBuffer);
#endif #endif
} }
void arrayprinter::printDec(const uint8_t *data, size_t size, void arrayprinter::printDec(const uint8_t *data, size_t size,
size_t maxCharPerLine) { size_t maxCharPerLine) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #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++) { for(size_t i = 0; i < size; i++) {
sif::info << static_cast<int>(data[i]); std::cout << static_cast<int>(data[i]);
if(i < size - 1){ if(i < size - 1){
sif::info << " , "; std::cout << " , ";
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and (i + 1) % maxCharPerLine == 0) {
sif::info << std::endl; std::cout << std::endl;
} }
} }
} }
sif::info << "]" << std::endl; std::cout << "]" << std::endl;
#else #else
// General format: 32, 243, -12 so it is number of chars times 5 // General format: 32, 243, -12 so it is number of chars times 5
// plus line break plus small safety margin. // 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]); currentPos += snprintf(printBuffer + currentPos, 3, "%d", data[i]);
if(i < size - 1) { if(i < size - 1) {
currentPos += sprintf(printBuffer + currentPos, ", "); currentPos += sprintf(printBuffer + currentPos, ", ");
if(i > 0 and i % maxCharPerLine == 0) { if(i > 0 and (i + 1) % maxCharPerLine == 0) {
currentPos += sprintf(printBuffer + currentPos, "\n"); currentPos += sprintf(printBuffer + currentPos, "\n");
} }
} }
} }
printf("[%s]\n", printBuffer);
#endif #endif
} }
void arrayprinter::printBin(const uint8_t *data, size_t size) { void arrayprinter::printBin(const uint8_t *data, size_t size) {
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "\n" << std::flush;
for(size_t i = 0; i < size; i++) { 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 #else
sif::printInfo("\n");
for(size_t i = 0; i < size; i++) { 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 #endif
} }

View File

@ -10,13 +10,14 @@ enum class OutputType {
BIN BIN
}; };
//! TODO: Write unit tests for this module.
namespace arrayprinter { namespace arrayprinter {
void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX, void print(const uint8_t* data, size_t size, OutputType type = OutputType::HEX,
bool printInfo = true, size_t maxCharPerLine = 12); bool printInfo = true, size_t maxCharPerLine = 10);
void printHex(const uint8_t* data, size_t size, size_t maxCharPerLine = 12); 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 = 12); void printDec(const uint8_t* data, size_t size, size_t maxCharPerLine = 10);
void printBin(const uint8_t* data, size_t size); void printBin(const uint8_t* data, size_t size);
} }

View File

@ -167,7 +167,9 @@ std::string* ServiceInterfaceBuffer::getPreamble(size_t * preambleSize) {
return &preamble; return &preamble;
} }
bool ServiceInterfaceBuffer::crAdditionEnabled() const {
return addCrToPreamble;
}
#ifdef UT699 #ifdef UT699
#include "../osal/rtems/Interrupt.h" #include "../osal/rtems/Interrupt.h"

View File

@ -70,6 +70,8 @@ private:
void putChars(char const* begin, char const* end); void putChars(char const* begin, char const* end);
std::string* getPreamble(size_t * preambleSize = nullptr); std::string* getPreamble(size_t * preambleSize = nullptr);
bool crAdditionEnabled() const;
}; };
#endif #endif

View File

@ -15,5 +15,9 @@ std::string* ServiceInterfaceStream::getPreamble() {
return streambuf.getPreamble(); return streambuf.getPreamble();
} }
bool ServiceInterfaceStream::crAdditionEnabled() const {
return streambuf.crAdditionEnabled();
}
#endif #endif

View File

@ -39,6 +39,8 @@ public:
*/ */
std::string* getPreamble(); std::string* getPreamble();
bool crAdditionEnabled() const;
protected: protected:
ServiceInterfaceBuffer streambuf; ServiceInterfaceBuffer streambuf;
}; };

View File

@ -32,7 +32,7 @@ ReturnValue_t InternalUnitTester::performTests(struct InternalUnitTester::TestCo
#if FSFW_CPP_OSTREAM_ENABLED == 1 #if FSFW_CPP_OSTREAM_ENABLED == 1
sif::info << "Internal unit tests finished." << std::endl; sif::info << "Internal unit tests finished." << std::endl;
#else #else
sif::printInfo("Running internal unit tests..\n"); sif::printInfo("Internal unit tests finished.\n");
#endif #endif
return RETURN_OK; return RETURN_OK;
} }

View File

@ -1,4 +1,29 @@
#include "TestArrayPrinter.h" #include "TestArrayPrinter.h"
void arrayprinter::testArrayPrinter() { void arrayprinter::testArrayPrinter() {
{
const std::array<uint8_t, 5> 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<uint8_t, 16> 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<uint8_t, 32> 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);
}
} }

View File

@ -1,6 +1,9 @@
#ifndef FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_ #ifndef FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_
#define FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_ #define FSFW_UNITTEST_INTERNAL_GLOBALFUNCTIONS_TESTARRAYPRINTER_H_
#include <fsfw/globalfunctions/arrayprinter.h>
#include <array>
namespace arrayprinter { namespace arrayprinter {
void testArrayPrinter(); void testArrayPrinter();