mirror of
https://github.com/espressif/openthread.git
synced 2026-10-05 15:27:40 +00:00
[ip6] update Ip6::Prefix comparison (#7813)
This commit updates `Ip6::Prefix` comparison (overload of `<` operator). If the two prefixes have same length N, then the bytes are compared directly (as two big-endian N-bit numbers). If the two prefix have different lengths, the shorter prefix is padded by `0` bit up to the longer prefix length N before the bytes are compared (as big-endian N-bit numbers). If all bytes are equal, the prefix with shorter length is considered smaller. This commit also updates `test_ip_address` unit test to validate the new comparison behavior.
This commit is contained in:
@@ -274,6 +274,17 @@ void TestIp6AddressSetPrefix(void)
|
||||
}
|
||||
}
|
||||
|
||||
ot::Ip6::Prefix PrefixFrom(const char *aAddressString, uint8_t aPrefixLength)
|
||||
{
|
||||
ot::Ip6::Prefix prefix;
|
||||
ot::Ip6::Address address;
|
||||
|
||||
SuccessOrQuit(address.FromString(aAddressString));
|
||||
prefix.Set(address.GetBytes(), aPrefixLength);
|
||||
|
||||
return prefix;
|
||||
}
|
||||
|
||||
void TestIp6Prefix(void)
|
||||
{
|
||||
const uint8_t kPrefixes[][OT_IP6_ADDRESS_SIZE] = {
|
||||
@@ -351,6 +362,38 @@ void TestIp6Prefix(void)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
struct TestCase
|
||||
{
|
||||
ot::Ip6::Prefix mPrefixA;
|
||||
ot::Ip6::Prefix mPrefixB;
|
||||
};
|
||||
|
||||
TestCase kTestCases[] = {
|
||||
{PrefixFrom("fd00::", 16), PrefixFrom("fd01::", 16)},
|
||||
{PrefixFrom("fc00::", 16), PrefixFrom("fd00::", 16)},
|
||||
{PrefixFrom("fd00::", 15), PrefixFrom("fd00::", 16)},
|
||||
{PrefixFrom("fd00::", 16), PrefixFrom("fd00:0::", 32)},
|
||||
{PrefixFrom("2001:0:0:0::", 64), PrefixFrom("fd00::", 8)},
|
||||
{PrefixFrom("2001:dba::", 32), PrefixFrom("fd12:3456:1234:abcd::", 64)},
|
||||
{PrefixFrom("910b:1000:0::", 48), PrefixFrom("910b:2000::", 32)},
|
||||
{PrefixFrom("::", 0), PrefixFrom("fd00::", 8)},
|
||||
{PrefixFrom("::", 0), PrefixFrom("::", 16)},
|
||||
{PrefixFrom("fd00:2:2::", 33), PrefixFrom("fd00:2:2::", 35)},
|
||||
{PrefixFrom("1:2:3:ffff::", 62), PrefixFrom("1:2:3:ffff::", 63)},
|
||||
};
|
||||
|
||||
printf("\nCompare Prefixes:\n");
|
||||
|
||||
for (const TestCase &testCase : kTestCases)
|
||||
{
|
||||
printf(" %26s < %s\n", testCase.mPrefixA.ToString().AsCString(),
|
||||
testCase.mPrefixB.ToString().AsCString());
|
||||
VerifyOrQuit(testCase.mPrefixA < testCase.mPrefixB);
|
||||
VerifyOrQuit(!(testCase.mPrefixB < testCase.mPrefixA));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void TestIp4Ip6Translation(void)
|
||||
|
||||
Reference in New Issue
Block a user