Uuid::operator==(): self-comparison optimization, and more tests (#5939)

Co-authored-by: Anton Kolesnyk <antkmsft@users.noreply.github.com>
This commit is contained in:
Anton Kolesnyk 2024-08-27 14:22:21 -07:00 committed by GitHub
parent 5c082753ac
commit 91608bb5dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 64 additions and 4 deletions

View File

@ -79,12 +79,15 @@ namespace Azure { namespace Core {
*/
constexpr bool operator==(Uuid const& other) const
{
// std::array::operator==() is not a constexpr until C++20
for (size_t i = 0; i < m_uuid.size(); ++i)
if (this != &other)
{
if (m_uuid[i] != other.m_uuid[i])
// std::array::operator==() is not a constexpr until C++20
for (size_t i = 0; i < m_uuid.size(); ++i)
{
return false;
if (m_uuid[i] != other.m_uuid[i])
{
return false;
}
}
}

View File

@ -205,3 +205,60 @@ TEST(Uuid, parse)
// Just a string of text
ASSERT_THROW(Uuid::Parse("The quick brown fox jumps over the lazy dog."), std::invalid_argument);
}
TEST(Uuid, equality)
{
{
Uuid const a;
EXPECT_TRUE(a == a);
EXPECT_FALSE(a != a);
}
{
Uuid const a = Uuid::Parse("00000000-0000-0000-0000-000000000000");
EXPECT_TRUE(a == a);
EXPECT_FALSE(a != a);
}
{
Uuid const a = Uuid::Parse("00112233-4455-6677-8899-aabbccddeeff");
EXPECT_TRUE(a == a);
EXPECT_FALSE(a != a);
}
{
Uuid const a;
Uuid const b;
EXPECT_TRUE(a == b);
EXPECT_TRUE(b == a);
EXPECT_FALSE(a != b);
EXPECT_FALSE(b != a);
}
{
Uuid const a;
Uuid const b = Uuid::Parse("00000000-0000-0000-0000-000000000000");
EXPECT_TRUE(a == b);
EXPECT_TRUE(b == a);
EXPECT_FALSE(a != b);
EXPECT_FALSE(b != a);
}
{
Uuid const a;
Uuid const b = Uuid::Parse("00112233-4455-6677-8899-aabbccddeeff");
EXPECT_FALSE(a == b);
EXPECT_FALSE(b == a);
EXPECT_TRUE(a != b);
EXPECT_TRUE(b != a);
}
{
Uuid const a = Uuid::Parse("ffeeddcc-bbaa-9988-7766-554433221100");
Uuid const b = Uuid::Parse("00112233-4455-6677-8899-aabbccddeeff");
EXPECT_FALSE(a == b);
EXPECT_FALSE(b == a);
EXPECT_TRUE(a != b);
EXPECT_TRUE(b != a);
}
}