Commit aff30817 authored by Clemens Backes's avatar Clemens Backes Committed by Commit Bot

[utils] Add unit tests for Vector

Also, fix the implementation of {operator==} and add {operator!=}.
{operator==} could not be instantiated on a {Vector<T>} where T is not
const, as it would access the fields of another instantiation of Vector
({T} vs {const T}).

R=jkummerow@chromium.org

Bug: v8:9810
Change-Id: I65c2d3071a781f6fe7a624b727d2770b43b7f7a9
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/1932363
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: 's avatarJakob Kummerow <jkummerow@chromium.org>
Cr-Commit-Position: refs/heads/master@{#65155}
parent ead247c1
......@@ -118,14 +118,11 @@ class Vector {
}
bool operator==(const Vector<const T> other) const {
if (length_ != other.length_) return false;
if (start_ == other.start_) return true;
for (size_t i = 0; i < length_; ++i) {
if (start_[i] != other.start_[i]) {
return false;
}
}
return true;
return std::equal(begin(), end(), other.begin(), other.end());
}
bool operator!=(const Vector<const T> other) const {
return !operator==(other);
}
private:
......
......@@ -215,6 +215,7 @@ v8_source_set("unittests_sources") {
"utils/detachable-vector-unittest.cc",
"utils/locked-queue-unittest.cc",
"utils/utils-unittest.cc",
"utils/vector-unittest.cc",
"wasm/control-transfer-unittest.cc",
"wasm/decoder-unittest.cc",
"wasm/function-body-decoder-unittest.cc",
......
// Copyright 2019 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/utils/utils.h"
#include "testing/gtest-support.h"
namespace v8 {
namespace internal {
TEST(VectorTest, Factories) {
auto vec = CStrVector("foo");
EXPECT_EQ(3u, vec.size());
EXPECT_EQ(0, memcmp(vec.begin(), "foo", 3));
vec = ArrayVector("foo");
EXPECT_EQ(4u, vec.size());
EXPECT_EQ(0, memcmp(vec.begin(), "foo\0", 4));
vec = CStrVector("foo\0\0");
EXPECT_EQ(3u, vec.size());
EXPECT_EQ(0, memcmp(vec.begin(), "foo", 3));
vec = CStrVector("");
EXPECT_EQ(0u, vec.size());
vec = CStrVector("\0");
EXPECT_EQ(0u, vec.size());
}
// Test operator== and operator!= on different Vector types.
TEST(VectorTest, Equals) {
auto foo1 = CStrVector("foo");
auto foo2 = ArrayVector("ffoo") + 1;
CHECK_EQ(4, foo2.size()); // Includes trailing '\0'.
foo2.Truncate(foo2.size() - 1);
// This is a requirement for the test.
CHECK_NE(foo1.begin(), foo2.begin());
CHECK_EQ(foo1, foo2);
// Compare Vector<char> against Vector<const char>.
char arr1[] = {'a', 'b', 'c'};
char arr2[] = {'a', 'b', 'c'};
char arr3[] = {'a', 'b', 'd'};
Vector<char> vec1_char = ArrayVector(arr1);
Vector<const char> vec1_const_char = vec1_char;
Vector<char> vec2_char = ArrayVector(arr2);
Vector<char> vec3_char = ArrayVector(arr3);
CHECK_NE(vec1_char.begin(), vec2_char.begin());
// Note: We directly call operator== and operator!= here (without CHECK_EQ or
// CHECK_NE) to have full control over the arguments.
CHECK(vec1_char == vec1_const_char);
CHECK(vec1_char == vec2_char);
CHECK(vec1_const_char == vec2_char);
CHECK(vec1_const_char != vec3_char);
CHECK(vec3_char != vec2_char);
CHECK(vec3_char != vec1_const_char);
}
} // namespace internal
} // namespace v8
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