Commit 2ea7563b authored by Daniel Hosseinian's avatar Daniel Hosseinian Committed by Commit Bot

Overload base::GUID comparison operators

operator<() needs to be overloaded so base::GUID can be used in set
containers.

Per the style guide, overload all other comparison operators.

Bug: 1026195
Change-Id: I9c36f0aee13b34c3bbdfdc3581a84a9c23485e21
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2533649
Commit-Queue: Daniel Hosseinian <dhoss@chromium.org>
Reviewed-by: default avatarGabriel Charette <gab@chromium.org>
Cr-Commit-Position: refs/heads/master@{#827413}
parent 2e9359de
...@@ -153,6 +153,22 @@ bool GUID::operator!=(const GUID& other) const { ...@@ -153,6 +153,22 @@ bool GUID::operator!=(const GUID& other) const {
return !(*this == other); return !(*this == other);
} }
bool GUID::operator<(const GUID& other) const {
return AsLowercaseString() < other.AsLowercaseString();
}
bool GUID::operator<=(const GUID& other) const {
return *this < other || *this == other;
}
bool GUID::operator>(const GUID& other) const {
return !(*this <= other);
}
bool GUID::operator>=(const GUID& other) const {
return !(*this < other);
}
std::ostream& operator<<(std::ostream& out, const GUID& guid) { std::ostream& operator<<(std::ostream& out, const GUID& guid) {
return out << guid.AsLowercaseString(); return out << guid.AsLowercaseString();
} }
......
...@@ -72,6 +72,10 @@ class BASE_EXPORT GUID { ...@@ -72,6 +72,10 @@ class BASE_EXPORT GUID {
// Invalid GUIDs are equal. // Invalid GUIDs are equal.
bool operator==(const GUID& other) const; bool operator==(const GUID& other) const;
bool operator!=(const GUID& other) const; bool operator!=(const GUID& other) const;
bool operator<(const GUID& other) const;
bool operator<=(const GUID& other) const;
bool operator>(const GUID& other) const;
bool operator>=(const GUID& other) const;
private: private:
// TODO(crbug.com/1026195): Consider using a different internal type. // TODO(crbug.com/1026195): Consider using a different internal type.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
#include <stdint.h> #include <stdint.h>
#include <limits> #include <limits>
#include <set>
#include <unordered_set> #include <unordered_set>
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
...@@ -164,4 +165,67 @@ TEST(GUIDTest, UnorderedSet) { ...@@ -164,4 +165,67 @@ TEST(GUIDTest, UnorderedSet) {
EXPECT_EQ(2u, guid_set.size()); EXPECT_EQ(2u, guid_set.size());
} }
TEST(GUIDTest, Set) {
std::set<GUID> guid_set;
static constexpr char kGUID1[] = "01234567-89ab-cdef-0123-456789abcdef";
const GUID guid1 = GUID::ParseLowercase(kGUID1);
ASSERT_TRUE(guid1.is_valid());
guid_set.insert(guid1);
static constexpr char kGUID2[] = "deadbeef-dead-beef-dead-beefdeadbeef";
const GUID guid2 = GUID::ParseLowercase(kGUID2);
ASSERT_TRUE(guid2.is_valid());
guid_set.insert(guid2);
// Test that the order of the GUIDs was preserved.
auto it = guid_set.begin();
EXPECT_EQ(guid1, *it);
++it;
EXPECT_EQ(guid2, *it);
++it;
EXPECT_EQ(guid_set.end(), it);
}
TEST(GUIDTest, Compare) {
static constexpr char kGUID[] = "21abd97f-73e8-4b88-9389-a9fee6abda5e";
static constexpr char kGUIDLess[] = "1e0dcaca-9e7c-4f4b-bcc6-e4c02b0c99df";
static constexpr char kGUIDGreater[] = "6eeb1bc8-186b-433c-9d6a-a827bc96b2d4";
const GUID guid = GUID::ParseLowercase(kGUID);
const GUID guid_eq = GUID::ParseLowercase(kGUID);
const GUID guid_lt = GUID::ParseLowercase(kGUIDLess);
const GUID guid_gt = GUID::ParseLowercase(kGUIDGreater);
const GUID guid_invalid = GUID();
EXPECT_TRUE(guid_eq == guid);
EXPECT_FALSE(guid_eq != guid);
EXPECT_FALSE(guid_eq < guid);
EXPECT_TRUE(guid_eq <= guid);
EXPECT_FALSE(guid_eq > guid);
EXPECT_TRUE(guid_eq >= guid);
EXPECT_FALSE(guid_lt == guid);
EXPECT_TRUE(guid_lt != guid);
EXPECT_TRUE(guid_lt < guid);
EXPECT_TRUE(guid_lt <= guid);
EXPECT_FALSE(guid_lt > guid);
EXPECT_FALSE(guid_lt >= guid);
EXPECT_FALSE(guid_gt == guid);
EXPECT_TRUE(guid_gt != guid);
EXPECT_FALSE(guid_gt < guid);
EXPECT_FALSE(guid_gt <= guid);
EXPECT_TRUE(guid_gt > guid);
EXPECT_TRUE(guid_gt >= guid);
// Invalid GUIDs are the "least".
EXPECT_FALSE(guid_invalid == guid);
EXPECT_TRUE(guid_invalid != guid);
EXPECT_TRUE(guid_invalid < guid);
EXPECT_TRUE(guid_invalid <= guid);
EXPECT_FALSE(guid_invalid > guid);
EXPECT_FALSE(guid_invalid >= guid);
}
} // namespace base } // namespace base
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment