[tests] use constexpr for constants in unit tests (#13145)

This commit refactors various unit tests to use `constexpr` for
defining constants instead of anonymous `enum` types.

Using `constexpr` is the modern and preferred approach in C++, as it
provides explicit types for constants and improves code clarity and
type safety.
This commit is contained in:
Abtin Keshavarzian
2026-05-25 19:39:27 -07:00
committed by GitHub
parent 6847b9acdf
commit 9d95a19e52
15 changed files with 87 additions and 152 deletions
+1 -4
View File
@@ -39,10 +39,7 @@ namespace ot {
static Instance *sInstance; static Instance *sInstance;
enum constexpr uint16_t kMaxChildIp6Addresses = OPENTHREAD_CONFIG_MLE_IP_ADDRS_PER_CHILD;
{
kMaxChildIp6Addresses = OPENTHREAD_CONFIG_MLE_IP_ADDRS_PER_CHILD,
};
void VerifyChildIp6Addresses(const Child &aChild, uint8_t aAddressListLength, const Ip6::Address aAddressList[]) void VerifyChildIp6Addresses(const Child &aChild, uint8_t aAddressListLength, const Ip6::Address aAddressList[])
{ {
+1 -4
View File
@@ -40,10 +40,7 @@ namespace ot {
static Instance *sInstance; static Instance *sInstance;
enum constexpr uint16_t kMaxChildren = OPENTHREAD_CONFIG_MLE_MAX_CHILDREN;
{
kMaxChildren = OPENTHREAD_CONFIG_MLE_MAX_CHILDREN,
};
struct TestChild struct TestChild
{ {
+20 -31
View File
@@ -42,11 +42,8 @@ namespace ot {
void TestDnsName(void) void TestDnsName(void)
{ {
enum static constexpr uint16_t kMaxSize = 300;
{ static constexpr uint16_t kMaxNameLength = Dns::Name::kMaxNameSize - 1;
kMaxSize = 300,
kMaxNameLength = Dns::Name::kMaxNameSize - 1,
};
struct TestName struct TestName
{ {
@@ -635,19 +632,14 @@ void TestDnsName(void)
void TestDnsCompressedName(void) void TestDnsCompressedName(void)
{ {
enum static constexpr uint8_t kHeaderOffset = 10;
{ static constexpr uint8_t kGuardBlockSize = 20;
kHeaderOffset = 10, static constexpr uint16_t kMaxBufferSize = 100;
kGuardBlockSize = 20, static constexpr uint16_t kLabelSize = 64;
kMaxBufferSize = 100, static constexpr uint16_t kNameSize = 256;
kLabelSize = 64, static constexpr uint16_t kName2EncodedSize = 4 + 2; // encoded "FOO" + pointer label (2 bytes)
kNameSize = 256, static constexpr uint16_t kName3EncodedSize = 2; // pointer label (2 bytes)
static constexpr uint16_t kName4EncodedSize = 15 + 2; // encoded "Human.Readable" + pointer label (2 bytes).
kName2EncodedSize = 4 + 2, // encoded "FOO" + pointer label (2 bytes)
kName3EncodedSize = 2, // pointer label (2 bytes)
kName4EncodedSize = 15 + 2, // encoded "Human.Readable" + pointer label (2 bytes).
};
const char kName[] = "F.ISI.ARPA"; const char kName[] = "F.ISI.ARPA";
const char kLabel1[] = "FOO"; const char kLabel1[] = "FOO";
@@ -1155,19 +1147,16 @@ void TestDnsCompressedName(void)
void TestHeaderAndResourceRecords(void) void TestHeaderAndResourceRecords(void)
{ {
enum static constexpr uint8_t kHeaderOffset = 0;
{ static constexpr uint16_t kQuestionCount = 1;
kHeaderOffset = 0, static constexpr uint16_t kAnswerCount = 2;
kQuestionCount = 1, static constexpr uint16_t kAdditionalCount = 6;
kAnswerCount = 2, static constexpr uint32_t kTtl = 7200;
kAdditionalCount = 6, static constexpr uint32_t kTxtTtl = 7300;
kTtl = 7200, static constexpr uint16_t kSrvPort = 1234;
kTxtTtl = 7300, static constexpr uint16_t kSrvPriority = 1;
kSrvPort = 1234, static constexpr uint16_t kSrvWeight = 2;
kSrvPriority = 1, static constexpr uint16_t kMaxSize = 600;
kSrvWeight = 2,
kMaxSize = 600,
};
const char kMessageString[] = "DnsMessage"; const char kMessageString[] = "DnsMessage";
const char kDomainName[] = "example.com."; const char kDomainName[] = "example.com.";
+9 -13
View File
@@ -38,20 +38,16 @@
namespace ot { namespace ot {
namespace Ncp { namespace Ncp {
enum constexpr uint16_t kBufferSize = 1500; // Frame buffer size
{ constexpr uint16_t kMaxFrameLength = 500; // Maximum allowed frame length (used when randomly generating frames)
kBufferSize = 1500, // Frame buffer size constexpr uint32_t kFuzzTestIteration = 50000; // Number of iteration during fuzz test (randomly generating frames)
kMaxFrameLength = 500, // Maximum allowed frame length (used when randomly generating frames) constexpr uint16_t kFrameHeaderSize = 4; // Frame header size
kFuzzTestIteration = 50000, // Number of iteration during fuzz test (randomly generating frames)
kFrameHeaderSize = 4, // Frame header size
kFlagXOn = 0x11, constexpr uint8_t kFlagXOn = 0x11;
kFlagXOff = 0x13, constexpr uint8_t kFlagXOff = 0x13;
kFlagSequence = 0x7e, ///< HDLC Flag value constexpr uint8_t kFlagSequence = 0x7e; // HDLC Flag value
kEscapeSequence = 0x7d, ///< HDLC Escape value constexpr uint8_t kEscapeSequence = 0x7d; // HDLC Escape value
kFlagSpecial = 0xf8, constexpr uint8_t kFlagSpecial = 0xf8;
};
static const uint8_t sOpenThreadText[] = "OpenThread Rocks"; static const uint8_t sOpenThreadText[] = "OpenThread Rocks";
static const uint8_t sHelloText[] = "Hello there!"; static const uint8_t sHelloText[] = "Hello there!";
+2 -5
View File
@@ -52,11 +52,8 @@ struct TestVector
void TestHkdfSha256(void) void TestHkdfSha256(void)
{ {
enum static constexpr uint16_t kMaxOuttKey = 128;
{ static constexpr uint8_t kFillByte = 0x77;
kMaxOuttKey = 128,
kFillByte = 0x77,
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Test Case #1: RFC-5869 Appendix A.1 // Test Case #1: RFC-5869 Appendix A.1
+8 -11
View File
@@ -38,20 +38,17 @@ namespace ot {
static Instance *sInstance; static Instance *sInstance;
enum constexpr int8_t kMaxRssValue = 0;
{ constexpr int8_t kMinRssValue = -128;
kMaxRssValue = 0,
kMinRssValue = -128,
kStringBuffferSize = 80, constexpr uint16_t kStringBuffferSize = 80;
kRssAverageMaxDiff = 16, constexpr uint8_t kRssAverageMaxDiff = 16;
kNumRssAdds = 300, constexpr uint16_t kNumRssAdds = 300;
kRawAverageBitShift = 3, constexpr uint8_t kRawAverageBitShift = 3;
kRawAverageMultiple = (1 << kRawAverageBitShift), constexpr uint8_t kRawAverageMultiple = (1 << kRawAverageBitShift);
kRawAverageBitMask = (1 << kRawAverageBitShift) - 1, constexpr uint8_t kRawAverageBitMask = (1 << kRawAverageBitShift) - 1;
};
#define MIN_RSS(_rss1, _rss2) (((_rss1) < (_rss2)) ? (_rss1) : (_rss2)) #define MIN_RSS(_rss1, _rss2) (((_rss1) < (_rss2)) ? (_rss1) : (_rss2))
#define MAX_RSS(_rss1, _rss2) (((_rss1) < (_rss2)) ? (_rss2) : (_rss1)) #define MAX_RSS(_rss1, _rss2) (((_rss1) < (_rss2)) ? (_rss2) : (_rss1))
+3 -6
View File
@@ -1856,12 +1856,9 @@ void TestLowpanIphc(void)
void TestLowpanMeshHeader(void) void TestLowpanMeshHeader(void)
{ {
enum static constexpr uint16_t kMaxFrameSize = 127;
{ static constexpr uint16_t kSourceAddr = 0x100;
kMaxFrameSize = 127, static constexpr uint16_t kDestAddr = 0x200;
kSourceAddr = 0x100,
kDestAddr = 0x200,
};
const uint8_t kMeshHeader1[] = {0xb1, 0x01, 0x00, 0x02, 0x00}; // src:0x100, dest:0x200, hop:0x1 const uint8_t kMeshHeader1[] = {0xb1, 0x01, 0x00, 0x02, 0x00}; // src:0x100, dest:0x200, hop:0x1
const uint8_t kMeshHeader2[] = {0xbf, 0x20, 0x01, 0x00, 0x02, 0x00}; // src:0x100, dest:0x200, hop:0x20 const uint8_t kMeshHeader2[] = {0xbf, 0x20, 0x01, 0x00, 0x02, 0x00}; // src:0x100, dest:0x200, hop:0x20
+2 -11
View File
@@ -41,18 +41,9 @@
using namespace ot; using namespace ot;
using namespace ot::Ncp; using namespace ot::Ncp;
enum constexpr uint16_t kTestBufferSize = 800;
{
kTestBufferSize = 800
};
enum OT_TOOL_PACKED_BEGIN struct RadioMessage
{
kTestMacScanChannelMask = 0x01
};
OT_TOOL_PACKED_BEGIN
struct RadioMessage
{ {
uint8_t mChannel; uint8_t mChannel;
uint8_t mPsdu[OT_RADIO_FRAME_MAX_SIZE]; uint8_t mPsdu[OT_RADIO_FRAME_MAX_SIZE];
+16 -19
View File
@@ -40,11 +40,8 @@
#include <openthread/platform/ble.h> #include <openthread/platform/ble.h>
#endif #endif
enum constexpr uint16_t kFlashSwapSize = 2048;
{ constexpr uint8_t kFlashSwapNum = 2;
FLASH_SWAP_SIZE = 2048,
FLASH_SWAP_NUM = 2,
};
std::map<uint32_t, std::vector<std::vector<uint8_t>>> settings; std::map<uint32_t, std::vector<std::vector<uint8_t>>> settings;
@@ -53,7 +50,7 @@ ot::Instance *testInitInstance(void)
otInstance *instance = nullptr; otInstance *instance = nullptr;
settings.clear(); settings.clear();
for (uint8_t idx = 0; idx < FLASH_SWAP_NUM; idx++) for (uint8_t idx = 0; idx < kFlashSwapNum; idx++)
{ {
otPlatFlashErase(nullptr, idx); otPlatFlashErase(nullptr, idx);
} }
@@ -388,7 +385,7 @@ OT_TOOL_WEAK void otPlatSettingsWipe(otInstance *) { settings.clear(); }
uint8_t *GetFlash(void) uint8_t *GetFlash(void)
{ {
static uint8_t sFlash[FLASH_SWAP_SIZE * FLASH_SWAP_NUM]; static uint8_t sFlash[kFlashSwapSize * kFlashSwapNum];
static bool sInitialized; static bool sInitialized;
if (!sInitialized) if (!sInitialized)
@@ -402,28 +399,28 @@ uint8_t *GetFlash(void)
OT_TOOL_WEAK void otPlatFlashInit(otInstance *) {} OT_TOOL_WEAK void otPlatFlashInit(otInstance *) {}
OT_TOOL_WEAK uint32_t otPlatFlashGetSwapSize(otInstance *) { return FLASH_SWAP_SIZE; } OT_TOOL_WEAK uint32_t otPlatFlashGetSwapSize(otInstance *) { return kFlashSwapSize; }
OT_TOOL_WEAK void otPlatFlashErase(otInstance *, uint8_t aSwapIndex) OT_TOOL_WEAK void otPlatFlashErase(otInstance *, uint8_t aSwapIndex)
{ {
uint32_t address; uint32_t address;
VerifyOrQuit(aSwapIndex < FLASH_SWAP_NUM, "aSwapIndex invalid"); VerifyOrQuit(aSwapIndex < kFlashSwapNum, "aSwapIndex invalid");
address = aSwapIndex ? FLASH_SWAP_SIZE : 0; address = aSwapIndex ? kFlashSwapSize : 0;
memset(GetFlash() + address, 0xff, FLASH_SWAP_SIZE); memset(GetFlash() + address, 0xff, kFlashSwapSize);
} }
OT_TOOL_WEAK void otPlatFlashRead(otInstance *, uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize) OT_TOOL_WEAK void otPlatFlashRead(otInstance *, uint8_t aSwapIndex, uint32_t aOffset, void *aData, uint32_t aSize)
{ {
uint32_t address; uint32_t address;
VerifyOrQuit(aSwapIndex < FLASH_SWAP_NUM, "aSwapIndex invalid"); VerifyOrQuit(aSwapIndex < kFlashSwapNum, "aSwapIndex invalid");
VerifyOrQuit(aSize <= FLASH_SWAP_SIZE, "aSize invalid"); VerifyOrQuit(aSize <= kFlashSwapSize, "aSize invalid");
VerifyOrQuit(aOffset <= (FLASH_SWAP_SIZE - aSize), "aOffset + aSize invalid"); VerifyOrQuit(aOffset <= (kFlashSwapSize - aSize), "aOffset + aSize invalid");
address = aSwapIndex ? FLASH_SWAP_SIZE : 0; address = aSwapIndex ? kFlashSwapSize : 0;
memcpy(aData, GetFlash() + address + aOffset, aSize); memcpy(aData, GetFlash() + address + aOffset, aSize);
} }
@@ -436,11 +433,11 @@ OT_TOOL_WEAK void otPlatFlashWrite(otInstance *,
{ {
uint32_t address; uint32_t address;
VerifyOrQuit(aSwapIndex < FLASH_SWAP_NUM, "aSwapIndex invalid"); VerifyOrQuit(aSwapIndex < kFlashSwapNum, "aSwapIndex invalid");
VerifyOrQuit(aSize <= FLASH_SWAP_SIZE, "aSize invalid"); VerifyOrQuit(aSize <= kFlashSwapSize, "aSize invalid");
VerifyOrQuit(aOffset <= (FLASH_SWAP_SIZE - aSize), "aOffset + aSize invalid"); VerifyOrQuit(aOffset <= (kFlashSwapSize - aSize), "aOffset + aSize invalid");
address = aSwapIndex ? FLASH_SWAP_SIZE : 0; address = aSwapIndex ? kFlashSwapSize : 0;
for (uint32_t index = 0; index < aSize; index++) for (uint32_t index = 0; index < aSize; index++)
{ {
+17 -24
View File
@@ -43,12 +43,10 @@ namespace Spinel {
// This module implements unit-test for Spinel::Buffer class. // This module implements unit-test for Spinel::Buffer class.
// Test related constants: // Test related constants:
enum
{ constexpr uint16_t kTestBufferSize = 800;
kTestBufferSize = 800, constexpr uint16_t kTestIterationAttemps = 10000;
kTestIterationAttemps = 10000, constexpr uint16_t kTagArraySize = 1000;
kTagArraySize = 1000,
};
// Messages used for building frames... // Messages used for building frames...
static const uint8_t sOpenThreadText[] = "OpenThread Rocks"; static const uint8_t sOpenThreadText[] = "OpenThread Rocks";
@@ -68,15 +66,13 @@ struct CallbackContext
CallbackContext sContext; CallbackContext sContext;
enum constexpr uint8_t kNumPrios = 2; // Number of priority levels.
{
kNumPrios = 2, // Number of priority levels.
kTestFrame1Size = sizeof(sMottoText) + sizeof(sMysteryText) + sizeof(sMottoText) + sizeof(sHelloText), constexpr uint16_t kTestFrame1Size =
kTestFrame2Size = sizeof(sMysteryText) + sizeof(sHelloText) + sizeof(sOpenThreadText), sizeof(sMottoText) + sizeof(sMysteryText) + sizeof(sMottoText) + sizeof(sHelloText);
kTestFrame3Size = sizeof(sMysteryText), constexpr uint16_t kTestFrame2Size = sizeof(sMysteryText) + sizeof(sHelloText) + sizeof(sOpenThreadText);
kTestFrame4Size = sizeof(sOpenThreadText), constexpr uint16_t kTestFrame3Size = sizeof(sMysteryText);
}; constexpr uint16_t kTestFrame4Size = sizeof(sOpenThreadText);
Spinel::Buffer::FrameTag sTagHistoryArray[kNumPrios][kTagArraySize]; Spinel::Buffer::FrameTag sTagHistoryArray[kNumPrios][kTagArraySize];
uint32_t sTagHistoryHead[kNumPrios] = {0}; uint32_t sTagHistoryHead[kNumPrios] = {0};
@@ -870,16 +866,13 @@ void TestBuffer(void)
* Handle the cases where buffer gets full or empty. * Handle the cases where buffer gets full or empty.
*/ */
enum constexpr uint16_t kFuzTestBufferSize = 2000; // Size of the buffer used during fuzz testing
{ constexpr uint32_t kFuzTestIterationAttempts = 500000; // Number of iterations to run
kFuzTestBufferSize = 2000, // Size of the buffer used during fuzz testing constexpr uint16_t kLensArraySize = 500; // Size of "Lengths" array.
kFuzTestIterationAttempts = 500000, // Number of iterations to run constexpr uint16_t kMaxFrameLen = 400; // Maximum frame length
kLensArraySize = 500, // Size of "Lengths" array. constexpr uint8_t kReadProbability = 50; // Probability to randomly choose to read vs write frame
kMaxFrameLen = 400, // Maximum frame length constexpr uint8_t kHighPriorityProbability = 20; // Probability to write a high priority frame
kReadProbability = 50, // Probability (in percent) to randomly choose to read vs write frame constexpr bool kUseTrueRandomNumberGenerator = true; // To use true random number generator or not.
kHighPriorityProbability = 20, // Probability (in percent) to write a high priority frame
kUseTrueRandomNumberGenerator = 1, // To use true random number generator or not.
};
uint8_t sFrameBuffer[kNumPrios][kFuzTestBufferSize]; uint8_t sFrameBuffer[kNumPrios][kFuzTestBufferSize];
uint32_t sFrameBufferTailIndex[kNumPrios] = {0}; uint32_t sFrameBufferTailIndex[kNumPrios] = {0};
+1 -4
View File
@@ -35,10 +35,7 @@
namespace ot { namespace ot {
namespace Spinel { namespace Spinel {
enum constexpr uint16_t kTestBufferSize = 800;
{
kTestBufferSize = 800,
};
void TestDecoder(void) void TestDecoder(void)
{ {
+1 -4
View File
@@ -35,10 +35,7 @@
namespace ot { namespace ot {
namespace Spinel { namespace Spinel {
enum constexpr uint16_t kTestBufferSize = 800;
{
kTestBufferSize = 800,
};
otError ReadFrame(Spinel::Buffer &aNcpBuffer, uint8_t *aFrame, uint16_t &aFrameLen) otError ReadFrame(Spinel::Buffer &aNcpBuffer, uint8_t *aFrame, uint16_t &aFrameLen)
{ {
+1 -4
View File
@@ -36,10 +36,7 @@
namespace ot { namespace ot {
enum constexpr uint16_t kStringSize = 10;
{
kStringSize = 10,
};
template <uint16_t kSize> void PrintString(const char *aName, const String<kSize> aString) template <uint16_t kSize> void PrintString(const char *aName, const String<kSize> aString)
{ {
+4 -8
View File
@@ -37,14 +37,10 @@
namespace ot { namespace ot {
enum constexpr uint16_t kCallCountIndexAlarmStop = 0;
{ constexpr uint16_t kCallCountIndexAlarmStart = 1;
kCallCountIndexAlarmStop = 0, constexpr uint16_t kCallCountIndexTimerHandler = 2;
kCallCountIndexAlarmStart, constexpr uint16_t kCallCountIndexMax = 3;
kCallCountIndexTimerHandler,
kCallCountIndexMax
};
uint32_t sNow; uint32_t sNow;
uint32_t sPlatT0; uint32_t sPlatT0;
+1 -4
View File
@@ -32,10 +32,7 @@
void DumpBuffer(const char *aTextMessage, const uint8_t *aBuffer, uint16_t aBufferLength) void DumpBuffer(const char *aTextMessage, const uint8_t *aBuffer, uint16_t aBufferLength)
{ {
enum static constexpr uint16_t kBytesPerLine = 16; // Number of bytes per line.
{
kBytesPerLine = 16, // Number of bytes per line.
};
char charBuff[kBytesPerLine + 1]; char charBuff[kBytesPerLine + 1];
uint16_t counter; uint16_t counter;