Skip to content

Commit

Permalink
Adding TIC::DatasetView::labelEquals() and TIC::DatasetView::dataToUi…
Browse files Browse the repository at this point in the history
…nt32() with associated UTs
  • Loading branch information
lains committed May 15, 2024
1 parent ed4b4f5 commit 88438ea
Show file tree
Hide file tree
Showing 4 changed files with 192 additions and 2 deletions.
17 changes: 17 additions & 0 deletions include/TIC/DatasetView.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,23 @@ class DatasetView {
*/
static uint32_t uint32FromValueBuffer(const uint8_t* buf, unsigned int cnt);

/**
* @brief Check if the current data label matches the one provided as argument
*
* @param cString The C-style string to compare to
*
* @return true if the data is valid and matches the provided string
*/
bool labelEquals(const char* cString) const;

/**
* @brief Converts the current data value to an 32-bit unsigned int
*
* @return The decoded unsigned int value, or -1 in case of errors (value does not contain a positive number or dataset is invalid)
*/
uint32_t dataToUint32() const;


private:
static uint8_t computeCRC(const uint8_t* bytes, unsigned int count);

Expand Down
2 changes: 1 addition & 1 deletion include/TIC/Unframer.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class Unframer {
/**
* @brief Construct a new TIC::Unframer object
*
* @param onNewFrameBytes A FOnNewFrameBytesFunc function to invoke for each byte received in the current TIC frame received
* @param onNewFrameBytes A FOnNewFrameBytesFunc function to invoke for each byte received in the current TIC frame
* @param onFrameComplete A FOnFrameCompleteFunc function to invoke after a full TiC frame has been received
* @param parserFuncContext A user-defined pointer that will be passed as last argument when invoking onFrameComplete()
*
Expand Down
19 changes: 19 additions & 0 deletions src/TIC/DatasetView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,7 @@ horodate() {
this->dataSz = 0;

if (computedCrc != crcByte) {
// printf("Invalid CRC character, got '%c', expected '%c'\n", crcByte, computedCrc);
this->decodedType = TIC::DatasetView::DatasetType::WrongCRC;
this->labelSz = 0;
this->dataSz = 0;
Expand Down Expand Up @@ -337,6 +338,9 @@ uint8_t TIC::DatasetView::computeCRC(const uint8_t* bytes, unsigned int count) {

uint32_t TIC::DatasetView::uint32FromValueBuffer(const uint8_t* buf, unsigned int cnt) {
uint32_t result = 0;
if (cnt == 0)
return -1; /* No digit at all */

for (unsigned int idx = 0; idx < cnt; idx++) {
uint8_t digit = buf[idx];
if (digit < '0' || digit > '9') { /* Invalid decimal value */
Expand All @@ -353,3 +357,18 @@ uint32_t TIC::DatasetView::uint32FromValueBuffer(const uint8_t* buf, unsigned in
}
return result;
}

bool TIC::DatasetView::labelEquals(const char* cString) const {
if (cString == nullptr)
return false;
if (!this->isValid())
return false;
size_t cStringLen = strlen(cString);
return (this->labelSz == cStringLen && memcmp(this->labelBuffer, cString, cStringLen) == 0);
}

uint32_t TIC::DatasetView::dataToUint32() const {
if (!this->isValid() || this->dataBuffer == nullptr)
return -1;
return TIC::DatasetView::uint32FromValueBuffer(this->dataBuffer, this->dataSz);
}
156 changes: 155 additions & 1 deletion test/src/TicDatasetView_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,37 @@ TEST(TicDatasetView_tests, TicDatasetView_standard_with_horodate_and_value) {
}
}

TEST(TicDatasetView_tests, TicDatasetView_historical_with_value) {
const char dataset[] = "PAPP 00750 -";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (!dv.isValid()) {
FAILF("Dataset shoud be valid");
}

if (dv.labelBuffer == nullptr) {
FAILF("NULL labelBuffer");
}
std::vector<uint8_t> labelBufferVec(dv.labelBuffer, dv.labelBuffer + dv.labelSz);
if (labelBufferVec != std::vector<uint8_t>({'P', 'A', 'P', 'P'})) {
FAILF("Wrong dataset label: %s", vectorToHexString(labelBufferVec).c_str());
}

if (dv.dataBuffer == nullptr) {
FAILF("NULL dataBuffer");
}
std::vector<uint8_t> dataBufferVec(dv.dataBuffer, dv.dataBuffer + dv.dataSz);
if (dataBufferVec != std::vector<uint8_t>({'0', '0', '7', '5', '0'})) {
FAILF("Wrong dataset data: %s", vectorToHexString(dataBufferVec).c_str());
}

if (dv.horodate.isValid) {
FAILF("Expected no horodate");
}
}

/**
* @brief Send the content of a file to a TIC::Unframer, cutting it into chunks
*
Expand Down Expand Up @@ -653,6 +684,120 @@ TEST(TicDatasetView_tests, TicDatasetView_uint32FromValueBuffer) {
}
}

TEST(TicDatasetView_tests, TicDatasetView_labelEquals) {
const char dataset[] = "PAPP 00750 -";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.labelBuffer == nullptr) {
FAILF("NULL labelBuffer");
}

if (!dv.labelEquals("PAPP")) {
FAILF("Expected label match");
}
if (dv.labelEquals("PAP")) {
FAILF("Expected label mismatch");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnValidValue) {
const char dataset[] = "URMS1\t230\t?";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataBuffer == nullptr) {
FAILF("NULL dataBuffer");
}

if (dv.dataToUint32() != 230) {
FAILF("Failed to convert label to uint32");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnValidValueWith0Prefix) {
const char dataset[] = "PAPP 00750 -";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataBuffer == nullptr) {
FAILF("NULL dataBuffer");
}
if (dv.dataToUint32() != 750) {
FAILF("Failed to convert label to uint32");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnValidValue0) {
const char dataset[] = "EASD02\t000000000\t!";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataBuffer == nullptr) {
FAILF("NULL dataBuffer");
}
if (dv.dataToUint32() != 0) {
FAILF("Failed to convert label to uint32");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnNegativeValue) {
const char dataset[] = "IINST2 -002 8";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataBuffer == nullptr) {
FAILF("NULL dataBuffer");
}
if (!dv.isValid()) {
FAILF("Expecting a valid data");
}
if (dv.dataToUint32() != (uint32_t)-1) {
FAILF("Should not convert invalid data to anything else than (uint32_t)-1");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnInvalidCRC) {
const char dataset[] = "PAPP 00750 A";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataToUint32() != (uint32_t)-1) {
FAILF("Should not convert invalid data to anything else than (uint32_t)-1");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnNonDigitValue) {
const char dataset[] = "PAPP 0a75b P";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (!dv.isValid()) {
FAILF("Expecting a valid data");
}
if (dv.dataToUint32() != (uint32_t)-1) {
FAILF("Should not convert invalid data to anything else than (uint32_t)-1");
}
}

TEST(TicDatasetView_tests, TicDatasetView_dataToUint32OnEmptyValue) {
const char dataset[] = "PAPP 1";
const uint8_t* datasetBuf = reinterpret_cast<const unsigned char*>(dataset);

TIC::DatasetView dv((const uint8_t*)datasetBuf, static_cast<unsigned int>(strlen(dataset)));

if (dv.dataToUint32() != (uint32_t)-1) {
FAILF("Should not convert invalid data to anything else than (uint32_t)-1");
}
}

TEST(TicHorodate_tests, TicHorodate_sample_date1) {
char sampleHorodateAsCString[] = "H081225223518";
TIC::Horodate horodate = TIC::Horodate::fromLabelBytes(reinterpret_cast<uint8_t*>(sampleHorodateAsCString), strlen(sampleHorodateAsCString));
Expand Down Expand Up @@ -925,9 +1070,18 @@ void runTicDatasetViewAllUnitTests() {
TicDatasetView_too_short();
TicDatasetView_valid_with_horodate_without_value();
TicDatasetView_standard_with_horodate_and_value();
TicDatasetView_uint32FromValueBuffer();
TicDatasetView_historical_with_value();
Chunked_sample_unframe_dsextract_decode_historical_TIC();
Chunked_sample_unframe_dsextract_decode_standard_TIC();
TicDatasetView_uint32FromValueBuffer();
TicDatasetView_labelEquals();
TicDatasetView_dataToUint32OnValidValue();
TicDatasetView_dataToUint32OnValidValueWith0Prefix();
TicDatasetView_dataToUint32OnValidValue0();
TicDatasetView_dataToUint32OnNegativeValue();
TicDatasetView_dataToUint32OnInvalidCRC();
TicDatasetView_dataToUint32OnNonDigitValue();
TicDatasetView_dataToUint32OnEmptyValue();
TicHorodate_sample_date1();
TicHorodate_sample_date2();
TicHorodate_sample_season_NA();
Expand Down

0 comments on commit 88438ea

Please sign in to comment.