Add operator== for ServerAddress and InlinedVector

pull/19277/head
Juanli Shen 6 years ago
parent bb6a97b0c8
commit 5bd4c0c7d0
  1. 10
      src/core/ext/filters/client_channel/server_address.cc
  2. 4
      src/core/ext/filters/client_channel/server_address.h
  3. 8
      src/core/lib/gprpp/inlined_vector.h
  4. 18
      test/core/gprpp/inlined_vector_test.cc

@ -39,12 +39,10 @@ ServerAddress::ServerAddress(const void* address, size_t address_len,
address_.len = static_cast<socklen_t>(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 {

@ -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_; }

@ -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<T*>(gpr_malloc(sizeof(T) * capacity));

@ -109,6 +109,24 @@ TEST(InlinedVectorTest, ConstIndexOperator) {
const_func(v);
}
TEST(InlinedVectorTest, EqualOperator) {
constexpr int kNumElements = 10;
// Both v1 and v2 are empty.
InlinedVector<int, 5> v1;
InlinedVector<int, 5> 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;

Loading…
Cancel
Save