mirror of
https://github.com/espressif/openthread.git
synced 2026-08-12 13:47:47 +00:00
[heap-data] add Matches() to compare Heap::Data with a given buffer (#9740)
Also updates the unit test to validate the new method.
This commit is contained in:
@@ -78,6 +78,25 @@ void Data::SetFrom(Data &&aData)
|
||||
TakeFrom(aData);
|
||||
}
|
||||
|
||||
bool Data::Matches(const uint8_t *aBuffer, uint16_t aLength) const
|
||||
{
|
||||
bool matches = false;
|
||||
|
||||
VerifyOrExit(aLength == mData.GetLength());
|
||||
|
||||
if (IsNull())
|
||||
{
|
||||
matches = (aLength == 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
matches = mData.MatchesBytesIn(aBuffer);
|
||||
}
|
||||
|
||||
exit:
|
||||
return matches;
|
||||
}
|
||||
|
||||
void Data::Free(void)
|
||||
{
|
||||
Heap::Free(mData.GetBytes());
|
||||
|
||||
@@ -169,6 +169,18 @@ public:
|
||||
*/
|
||||
void CopyBytesTo(uint8_t *aBuffer) const { return mData.CopyBytesTo(aBuffer); }
|
||||
|
||||
/**
|
||||
* Compares the `Data` content with the bytes from a given buffer.
|
||||
*
|
||||
* @param[in] aBuffer A pointer to a buffer to compare with the data.
|
||||
* @param[in] aLength The length of @p aBuffer.
|
||||
*
|
||||
* @retval TRUE The `Data` content matches the bytes in @p aBuffer.
|
||||
* @retval FALSE The `Data` content does not match the byes in @p aBuffer.
|
||||
*
|
||||
*/
|
||||
bool Matches(const uint8_t *aBuffer, uint16_t aLength) const;
|
||||
|
||||
/**
|
||||
* Frees any buffer allocated by the `Heap::Data`.
|
||||
*
|
||||
|
||||
@@ -171,11 +171,15 @@ void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength
|
||||
|
||||
PrintData(aData);
|
||||
|
||||
VerifyOrQuit(aData.Matches(aBytes, aLength));
|
||||
VerifyOrQuit(!aData.Matches(aBytes, aLength + 1));
|
||||
|
||||
if (aLength == 0)
|
||||
{
|
||||
VerifyOrQuit(aData.IsNull());
|
||||
VerifyOrQuit(aData.GetBytes() == nullptr);
|
||||
VerifyOrQuit(aData.GetLength() == 0);
|
||||
VerifyOrQuit(aData.Matches(nullptr, 0));
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -186,6 +190,10 @@ void VerifyData(const Heap::Data &aData, const uint8_t *aBytes, uint16_t aLength
|
||||
|
||||
aData.CopyBytesTo(buffer);
|
||||
VerifyOrQuit(memcmp(buffer, aBytes, aLength) == 0, "CopyBytesTo() failed");
|
||||
|
||||
VerifyOrQuit(aData.Matches(buffer, aLength));
|
||||
buffer[aLength - 1]++;
|
||||
VerifyOrQuit(!aData.Matches(buffer, aLength));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,6 +231,10 @@ void TestHeapData(void)
|
||||
printf("After constructor\n");
|
||||
VerifyData(data, nullptr, 0);
|
||||
|
||||
VerifyOrQuit(data.Matches(nullptr, 0));
|
||||
VerifyOrQuit(data.Matches(kData1, 0));
|
||||
VerifyOrQuit(!data.Matches(kData1, 1));
|
||||
|
||||
printf("------------------------------------------------------------------------------------\n");
|
||||
printf("SetFrom(aBuffer, aLength)\n");
|
||||
|
||||
|
||||
Reference in New Issue
Block a user