Commit d1e80b34 authored by Kenton Varda's avatar Kenton Varda

Allow comparing arrays with different element types.

parent 18e3c9f1
......@@ -471,6 +471,27 @@ TEST(Common, ArrayAsBytes) {
}
}
KJ_TEST("ArrayPtr operator ==") {
KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const int>({123, 456}));
KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 456})));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123, 321}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const int>({123}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) == ArrayPtr<const short>({123, 456}));
KJ_EXPECT(!(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 456})));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123, 321}));
KJ_EXPECT(ArrayPtr<const int>({123, 456}) != ArrayPtr<const short>({123}));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) ==
ArrayPtr<const char* const>({"foo", "bar"})));
KJ_EXPECT(!(ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo", "bar"})));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo", "baz"})));
KJ_EXPECT((ArrayPtr<const StringPtr>({"foo", "bar"}) !=
ArrayPtr<const char* const>({"foo"})));
}
KJ_TEST("kj::range()") {
uint expected = 5;
for (uint i: range(5, 10)) {
......
......@@ -1331,6 +1331,17 @@ public:
}
inline bool operator!=(const ArrayPtr& other) const { return !(*this == other); }
template <typename U>
inline bool operator==(const ArrayPtr<U>& other) const {
if (size_ != other.size()) return false;
for (size_t i = 0; i < size_; i++) {
if (ptr[i] != other[i]) return false;
}
return true;
}
template <typename U>
inline bool operator!=(const ArrayPtr<U>& other) const { return !(*this == other); }
private:
T* ptr;
size_t size_;
......
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