From 5bd4c0c7d069611d485bcbcff50c713d751e95c5 Mon Sep 17 00:00:00 2001 From: Juanli Shen Date: Fri, 7 Jun 2019 17:39:08 -0700 Subject: [PATCH] Add operator== for ServerAddress and InlinedVector --- .../filters/client_channel/server_address.cc | 10 ++++------ .../filters/client_channel/server_address.h | 4 +--- src/core/lib/gprpp/inlined_vector.h | 8 ++++++++ test/core/gprpp/inlined_vector_test.cc | 18 ++++++++++++++++++ 4 files changed, 31 insertions(+), 9 deletions(-) diff --git a/src/core/ext/filters/client_channel/server_address.cc b/src/core/ext/filters/client_channel/server_address.cc index c2941afbcfd..31c0edaca17 100644 --- a/src/core/ext/filters/client_channel/server_address.cc +++ b/src/core/ext/filters/client_channel/server_address.cc @@ -39,12 +39,10 @@ ServerAddress::ServerAddress(const void* address, size_t address_len, address_.len = static_cast(address_len); } -int ServerAddress::Cmp(const ServerAddress& other) const { - if (address_.len > other.address_.len) return 1; - if (address_.len < other.address_.len) return -1; - int retval = memcmp(address_.addr, other.address_.addr, address_.len); - if (retval != 0) return retval; - return grpc_channel_args_compare(args_, other.args_); +bool ServerAddress::operator==(const grpc_core::ServerAddress& other) const { + return address_.len == other.address_.len && + memcmp(address_.addr, other.address_.addr, address_.len) == 0 && + grpc_channel_args_compare(args_, other.args_) == 0; } bool ServerAddress::IsBalancer() const { diff --git a/src/core/ext/filters/client_channel/server_address.h b/src/core/ext/filters/client_channel/server_address.h index 040cd2ee317..1b68a59ed85 100644 --- a/src/core/ext/filters/client_channel/server_address.h +++ b/src/core/ext/filters/client_channel/server_address.h @@ -73,9 +73,7 @@ class ServerAddress { return *this; } - bool operator==(const ServerAddress& other) const { return Cmp(other) == 0; } - - int Cmp(const ServerAddress& other) const; + bool operator==(const ServerAddress& other) const; const grpc_resolved_address& address() const { return address_; } const grpc_channel_args* args() const { return args_; } diff --git a/src/core/lib/gprpp/inlined_vector.h b/src/core/lib/gprpp/inlined_vector.h index 66dc751a567..ffc37387ec3 100644 --- a/src/core/lib/gprpp/inlined_vector.h +++ b/src/core/lib/gprpp/inlined_vector.h @@ -97,6 +97,14 @@ class InlinedVector { return data()[offset]; } + bool operator==(const InlinedVector& other) const { + if (size_ != other.size_) return false; + for (size_t i = 0; i < size_; ++i) { + if (data()[i] != other.data()[i]) return false; + } + return true; + } + void reserve(size_t capacity) { if (capacity > capacity_) { T* new_dynamic = static_cast(gpr_malloc(sizeof(T) * capacity)); diff --git a/test/core/gprpp/inlined_vector_test.cc b/test/core/gprpp/inlined_vector_test.cc index f943128f53c..4b7e46761c0 100644 --- a/test/core/gprpp/inlined_vector_test.cc +++ b/test/core/gprpp/inlined_vector_test.cc @@ -109,6 +109,24 @@ TEST(InlinedVectorTest, ConstIndexOperator) { const_func(v); } +TEST(InlinedVectorTest, EqualOperator) { + constexpr int kNumElements = 10; + // Both v1 and v2 are empty. + InlinedVector v1; + InlinedVector v2; + EXPECT_TRUE(v1 == v2); + // Both v1 and v2 contains the same data. + FillVector(&v1, kNumElements); + FillVector(&v2, kNumElements); + EXPECT_TRUE(v1 == v2); + // The sizes of v1 and v2 are different. + v1.push_back(0); + EXPECT_FALSE(v1 == v2); + // The contents of v1 and v2 are different although their sizes are the same. + v2.push_back(1); + EXPECT_FALSE(v1 == v2); +} + // the following constants and typedefs are used for copy/move // construction/assignment const size_t kInlinedLength = 8;