mirror of
https://github.com/espressif/openthread.git
synced 2026-08-16 07:37:47 +00:00
[unit-test] add a common function to dump buffer content (#4367)
This commit updates `test_utils.hpp/cpp` to include a common helper function `DumpBuffer` to print the content of a buffer (as hex and char string) to screen. It also removes unused helper functions and use of STL header files and types.
This commit is contained in:
committed by
Jonathan Hui
parent
91e8903699
commit
eaf2e7b9f7
+28
-37
@@ -26,55 +26,46 @@
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "test_util.h"
|
||||
#include "test_util.hpp"
|
||||
|
||||
#include <iostream>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <ctype.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
void otTestHexToVector(std::string &aHex, std::vector<uint8_t> &aOutBytes)
|
||||
void DumpBuffer(const char *aTextMessage, uint8_t *aBuffer, uint16_t aBufferLength)
|
||||
{
|
||||
std::istringstream ss(aHex);
|
||||
std::string word;
|
||||
|
||||
while (ss >> word)
|
||||
enum
|
||||
{
|
||||
uint8_t n = static_cast<uint8_t>(strtol(word.data(), NULL, 16));
|
||||
aOutBytes.push_back(n);
|
||||
}
|
||||
}
|
||||
kBytesPerLine = 16, // Number of bytes per line.
|
||||
};
|
||||
|
||||
void otTestPrintHex(uint8_t *aBuffer, int aLength)
|
||||
{
|
||||
int i;
|
||||
char charBuff[kBytesPerLine + 1];
|
||||
uint16_t counter;
|
||||
uint8_t byte;
|
||||
|
||||
for (i = 0; i < aLength; i++)
|
||||
printf("\n%s - len = %u\n ", aTextMessage ? aTextMessage : "Buffer", aBufferLength);
|
||||
|
||||
counter = 0;
|
||||
|
||||
while (aBufferLength--)
|
||||
{
|
||||
printf("%02x ", aBuffer[i]);
|
||||
byte = *aBuffer++;
|
||||
printf("%02X ", byte);
|
||||
charBuff[counter] = isprint(byte) ? static_cast<char>(byte) : '.';
|
||||
counter++;
|
||||
|
||||
if (i % 16 == 7)
|
||||
if (counter == kBytesPerLine)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
if (i % 16 == 15 && aLength != i + 1)
|
||||
{
|
||||
printf("\n");
|
||||
charBuff[counter] = 0;
|
||||
printf(" %s\n ", charBuff);
|
||||
counter = 0;
|
||||
}
|
||||
}
|
||||
|
||||
printf("\n");
|
||||
}
|
||||
charBuff[counter] = 0;
|
||||
|
||||
void otTestPrintHex(std::string &aString)
|
||||
{
|
||||
otTestPrintHex((uint8_t *)aString.data(), static_cast<int>(aString.size()));
|
||||
}
|
||||
while (counter++ < kBytesPerLine)
|
||||
{
|
||||
printf(" ");
|
||||
}
|
||||
|
||||
void otTestPrintHex(std::vector<uint8_t> &aBytes)
|
||||
{
|
||||
otTestPrintHex((uint8_t *)&aBytes[0], static_cast<int>(aBytes.size()));
|
||||
printf(" %s\n", charBuff);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user