mirror of
https://github.com/espressif/openthread.git
synced 2026-09-17 06:30:10 +00:00
[string] adding ot::String<size> class (#2764)
This commit adds a new template class `ot::String<size>` which implements a fixed-length character string. This class is then used as return value of `ToString()` methods from different classes. This simplifies the `ToString()` implementation and its use. This commit also adds a unit test for `String`.
This commit is contained in:
committed by
Jonathan Hui
parent
90b160ccfa
commit
4b918f85f0
@@ -98,6 +98,7 @@ check_PROGRAMS = \
|
||||
test-pskc \
|
||||
test-spinel-decoder \
|
||||
test-spinel-encoder \
|
||||
test-string \
|
||||
test-strlcat \
|
||||
test-strlcpy \
|
||||
test-strnlen \
|
||||
@@ -184,6 +185,9 @@ test_priority_queue_SOURCES = test_platform.cpp test_priority_queue.cpp
|
||||
test_pskc_LDADD = $(COMMON_LDADD)
|
||||
test_pskc_SOURCES = test_platform.cpp test_pskc.cpp
|
||||
|
||||
test_string_LDADD = $(COMMON_LDADD)
|
||||
test_string_SOURCES = test_platform.cpp test_string.cpp
|
||||
|
||||
test_strlcat_LDADD = $(COMMON_LDADD)
|
||||
test_strlcat_SOURCES = test_strlcat.c
|
||||
|
||||
@@ -231,6 +235,7 @@ PRETTY_FILES = \
|
||||
$(test_pskc_SOURCES) \
|
||||
$(test_spinel_decoder_SOURCES) \
|
||||
$(test_spinel_encoder_SOURCES) \
|
||||
$(test_string) \
|
||||
$(test_strlcat_SOURCES) \
|
||||
$(test_strlcpy_SOURCES) \
|
||||
$(test_strnlen_SOURCES) \
|
||||
|
||||
@@ -83,14 +83,7 @@ void VerifyRawRssValue(int8_t aAverage, uint16_t aRawValue)
|
||||
// This function prints the values in the passed in link info instance. It is invoked as the final step in test-case.
|
||||
void PrintOutcome(LinkQualityInfo &aLinkInfo)
|
||||
{
|
||||
char stringBuf[LinkQualityInfo::kInfoStringSize];
|
||||
|
||||
VerifyOrQuit(aLinkInfo.ToInfoString(stringBuf, sizeof(stringBuf)) != NULL, "ToInfoString() returned NULL");
|
||||
|
||||
printf("%s", stringBuf);
|
||||
|
||||
// This test-case succeeded.
|
||||
printf(" -> PASS\n");
|
||||
printf("%s -> PASS \n", aLinkInfo.ToInfoString().AsCString());
|
||||
}
|
||||
|
||||
void TestLinkQualityData(RssTestData aRssData)
|
||||
@@ -139,16 +132,10 @@ void VerifyRawRssValue(RssAverager &aRssAverager)
|
||||
}
|
||||
}
|
||||
|
||||
// This function prints the values in the passed in link info instance. It is invoked as the final step in test-case.
|
||||
// This function prints the values in the passed link info instance. It is invoked as the final step in test-case.
|
||||
void PrintOutcome(RssAverager &aRssAverager)
|
||||
{
|
||||
char stringBuf[RssAverager::kStringSize];
|
||||
|
||||
VerifyOrQuit(aRssAverager.ToString(stringBuf, sizeof(stringBuf)) != NULL, "ToString() returned NULL");
|
||||
printf("%s", stringBuf);
|
||||
|
||||
// This test-case succeeded.
|
||||
printf(" -> PASS\n");
|
||||
printf("%s -> PASS\n", aRssAverager.ToString().AsCString());
|
||||
}
|
||||
|
||||
int8_t GetRandomRss(void)
|
||||
|
||||
@@ -136,16 +136,14 @@ void TestMacChannelMask(void)
|
||||
Mac::ChannelMask mask1;
|
||||
Mac::ChannelMask mask2(OT_RADIO_SUPPORTED_CHANNELS);
|
||||
|
||||
char stringBuffer[Mac::ChannelMask::kInfoStringSize];
|
||||
|
||||
printf("Testing Mac::ChannelMask\n");
|
||||
|
||||
VerifyOrQuit(mask1.IsEmpty(), "ChannelMask.IsEmpty failed\n");
|
||||
printf("empty = %s\n", mask1.ToString(stringBuffer, sizeof(stringBuffer)));
|
||||
printf("empty = %s\n", mask1.ToString().AsCString());
|
||||
|
||||
VerifyOrQuit(!mask2.IsEmpty(), "ChannelMask.IsEmpty failed\n");
|
||||
VerifyOrQuit(mask2.GetMask() == OT_RADIO_SUPPORTED_CHANNELS, "ChannelMask.GetMask() failed\n");
|
||||
printf("all_channels = %s\n", mask2.ToString(stringBuffer, sizeof(stringBuffer)));
|
||||
printf("all_channels = %s\n", mask2.ToString().AsCString());
|
||||
|
||||
mask1.SetMask(OT_RADIO_SUPPORTED_CHANNELS);
|
||||
VerifyOrQuit(!mask1.IsEmpty(), "ChannelMask.IsEmpty failed\n");
|
||||
@@ -169,7 +167,7 @@ void TestMacChannelMask(void)
|
||||
mask1.AddChannel(channels1[index]);
|
||||
}
|
||||
|
||||
printf("channels1 = %s\n", mask1.ToString(stringBuffer, sizeof(stringBuffer)));
|
||||
printf("channels1 = %s\n", mask1.ToString().AsCString());
|
||||
|
||||
VerifyOrQuit(!mask1.IsEmpty(), "ChannelMask.IsEmpty failed\n");
|
||||
VerifyChannelMaskContent(mask1, channels1, sizeof(channels1));
|
||||
@@ -181,7 +179,7 @@ void TestMacChannelMask(void)
|
||||
mask2.AddChannel(channels2[index]);
|
||||
}
|
||||
|
||||
printf("channels2 = %s\n", mask2.ToString(stringBuffer, sizeof(stringBuffer)));
|
||||
printf("channels2 = %s\n", mask2.ToString().AsCString());
|
||||
|
||||
VerifyOrQuit(!mask2.IsEmpty(), "ChannelMask.IsEmpty failed\n");
|
||||
VerifyChannelMaskContent(mask2, channels2, sizeof(channels2));
|
||||
@@ -193,7 +191,7 @@ void TestMacChannelMask(void)
|
||||
mask2.AddChannel(channles4[0]);
|
||||
VerifyChannelMaskContent(mask2, channles4, sizeof(channles4));
|
||||
|
||||
printf("channels4 = %s\n", mask2.ToString(stringBuffer, sizeof(stringBuffer)));
|
||||
printf("channels4 = %s\n", mask2.ToString().AsCString());
|
||||
|
||||
mask1.Clear();
|
||||
mask2.Clear();
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
/*
|
||||
* Copyright (c) 2018, The OpenThread Authors.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 3. Neither the name of the copyright holder nor the
|
||||
* names of its contributors may be used to endorse or promote products
|
||||
* derived from this software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
|
||||
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||
* POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#include "test_platform.h"
|
||||
|
||||
#include <openthread/config.h>
|
||||
#include <openthread/openthread.h>
|
||||
|
||||
#include "test_util.h"
|
||||
#include "common/code_utils.hpp"
|
||||
#include "common/string.hpp"
|
||||
|
||||
namespace ot {
|
||||
|
||||
enum
|
||||
{
|
||||
kStringSize = 10,
|
||||
};
|
||||
|
||||
template <uint16_t kSize>
|
||||
void PrintString(const char *aName, const String<kSize> aString)
|
||||
{
|
||||
printf("\t%s = [%d] \"%s\"\n", aName, aString.GetLength(), aString.AsCString());
|
||||
}
|
||||
|
||||
void TestString(void)
|
||||
{
|
||||
otError error;
|
||||
String<kStringSize> str1;
|
||||
String<kStringSize> str2("abc");
|
||||
String<kStringSize> str3("%d", 12);
|
||||
|
||||
printf("\nTest 1: String constructor\n");
|
||||
|
||||
VerifyOrQuit(str1.GetSize() == kStringSize, "GetSize() failed");
|
||||
|
||||
VerifyOrQuit(str1.GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(str2.GetLength() == 3, "GetLength() failed");
|
||||
VerifyOrQuit(str3.GetLength() == 2, "GetLength() failed");
|
||||
|
||||
VerifyOrQuit(strcmp(str1.AsCString(), "") == 0, "String content is incorrect");
|
||||
VerifyOrQuit(strcmp(str2.AsCString(), "abc") == 0, "String content is incorrect");
|
||||
VerifyOrQuit(strcmp(str3.AsCString(), "12") == 0, "String content is incorrect");
|
||||
|
||||
PrintString("str1", str1);
|
||||
PrintString("str2", str2);
|
||||
PrintString("str3", str3);
|
||||
|
||||
printf(" -- PASS\n");
|
||||
|
||||
printf("\nTest 2: String::Set() and String::Clear() method\n");
|
||||
|
||||
error = str1.Set("Hello");
|
||||
SuccessOrQuit(error, "String::Set() failed unexpectedly");
|
||||
VerifyOrQuit(str1.GetLength() == 5, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str1.AsCString(), "Hello") == 0, "String content is incorrect");
|
||||
PrintString("str1", str1);
|
||||
|
||||
str1.Clear();
|
||||
VerifyOrQuit(str1.GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str1.AsCString(), "") == 0, "String content is incorrect");
|
||||
|
||||
str1.Set("%d", 12);
|
||||
VerifyOrQuit(str1.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str1.AsCString(), "12") == 0, "String content is incorrect");
|
||||
PrintString("str1", str1);
|
||||
|
||||
error = str1.Set("abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz");
|
||||
VerifyOrQuit(error == OT_ERROR_NO_BUFS, "String::Set() did not handle overflow buffer correctly");
|
||||
PrintString("str1", str1);
|
||||
|
||||
printf("\nTest 3: String::Append() method\n");
|
||||
|
||||
str2.Clear();
|
||||
VerifyOrQuit(str2.GetLength() == 0, "GetLength() failed for empty string");
|
||||
VerifyOrQuit(strcmp(str2.AsCString(), "") == 0, "String content is incorrect");
|
||||
|
||||
error = str2.Append("Hi");
|
||||
SuccessOrQuit(error, "String::Append() failed unexpectedly");
|
||||
VerifyOrQuit(str2.GetLength() == 2, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str2.AsCString(), "Hi") == 0, "String content is incorrect");
|
||||
PrintString("str2", str2);
|
||||
|
||||
error = str2.Append("%s%d", "!", 12);
|
||||
SuccessOrQuit(error, "String::Append() failed unexpectedly");
|
||||
VerifyOrQuit(str2.GetLength() == 5, "GetLength() failed");
|
||||
VerifyOrQuit(strcmp(str2.AsCString(), "Hi!12") == 0, "String content is incorrect");
|
||||
PrintString("str2", str2);
|
||||
|
||||
error = str2.Append("abcdefghijklmnopqratuvwxyzabcdefghijklmnopqratuvwxyz");
|
||||
VerifyOrQuit(error == OT_ERROR_NO_BUFS, "String::Append() did not handle overflow buffer correctly");
|
||||
PrintString("str2", str2);
|
||||
|
||||
printf(" -- PASS\n");
|
||||
}
|
||||
|
||||
} // namespace ot
|
||||
|
||||
#ifdef ENABLE_TEST_MAIN
|
||||
int main(void)
|
||||
{
|
||||
ot::TestString();
|
||||
printf("\nAll tests passed.\n");
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user