2019-11-25 15:20:49 +00:00
|
|
|
// 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.
|
|
|
|
|
2020-04-17 14:36:10 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2019-11-25 15:20:49 +00:00
|
|
|
#include "src/utils/utils.h"
|
2020-04-17 14:36:10 +00:00
|
|
|
#include "testing/gmock-support.h"
|
2019-11-25 15:20:49 +00:00
|
|
|
#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;
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_EQ(4u, foo2.size()); // Includes trailing '\0'.
|
2019-11-25 15:20:49 +00:00
|
|
|
foo2.Truncate(foo2.size() - 1);
|
|
|
|
// This is a requirement for the test.
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_NE(foo1.begin(), foo2.begin());
|
|
|
|
EXPECT_EQ(foo1, foo2);
|
2019-11-25 15:20:49 +00:00
|
|
|
|
|
|
|
// 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);
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_NE(vec1_char.begin(), vec2_char.begin());
|
|
|
|
// Note: We directly call operator== and operator!= here (without EXPECT_EQ or
|
|
|
|
// EXPECT_NE) to have full control over the arguments.
|
|
|
|
EXPECT_TRUE(vec1_char == vec1_const_char);
|
|
|
|
EXPECT_TRUE(vec1_char == vec2_char);
|
|
|
|
EXPECT_TRUE(vec1_const_char == vec2_char);
|
|
|
|
EXPECT_TRUE(vec1_const_char != vec3_char);
|
|
|
|
EXPECT_TRUE(vec3_char != vec2_char);
|
|
|
|
EXPECT_TRUE(vec3_char != vec1_const_char);
|
2019-11-25 15:20:49 +00:00
|
|
|
}
|
|
|
|
|
2020-01-20 15:48:23 +00:00
|
|
|
TEST(OwnedVectorConstruction, Equals) {
|
|
|
|
auto int_vec = OwnedVector<int>::New(4);
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_EQ(4u, int_vec.size());
|
2020-01-20 15:48:23 +00:00
|
|
|
auto find_non_zero = [](int i) { return i != 0; };
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_EQ(int_vec.end(),
|
|
|
|
std::find_if(int_vec.begin(), int_vec.end(), find_non_zero));
|
2020-01-20 15:48:23 +00:00
|
|
|
|
|
|
|
constexpr int kInit[] = {4, 11, 3};
|
|
|
|
auto init_vec1 = OwnedVector<int>::Of(kInit);
|
|
|
|
// Note: {const int} should also work: We initialize the owned vector, but
|
|
|
|
// afterwards it's non-modifyable.
|
|
|
|
auto init_vec2 = OwnedVector<const int>::Of(ArrayVector(kInit));
|
2020-04-17 12:39:59 +00:00
|
|
|
EXPECT_EQ(init_vec1.as_vector(), ArrayVector(kInit));
|
|
|
|
EXPECT_EQ(init_vec1.as_vector(), init_vec2.as_vector());
|
2020-01-20 15:48:23 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 14:36:10 +00:00
|
|
|
// Test that the constexpr factory methods work.
|
|
|
|
TEST(VectorTest, ConstexprFactories) {
|
|
|
|
static constexpr int kInit1[] = {4, 11, 3};
|
|
|
|
static constexpr auto kVec1 = ArrayVector(kInit1);
|
|
|
|
STATIC_ASSERT(kVec1.size() == 3);
|
|
|
|
EXPECT_THAT(kVec1, testing::ElementsAreArray(kInit1));
|
|
|
|
|
|
|
|
static constexpr auto kVec2 = VectorOf(kInit1, 2);
|
|
|
|
STATIC_ASSERT(kVec2.size() == 2);
|
|
|
|
EXPECT_THAT(kVec2, testing::ElementsAre(4, 11));
|
|
|
|
|
|
|
|
static constexpr const char kInit3[] = "foobar";
|
[base] Fix {StaticCharVector} and add {StaticOneByteVector}
{StaticCharVector}, according to its name, should return a
{Vector<const char>}. For getting a {Vector<const uint8_t>}, the method
should be called {StaticOneByteVector}, analog to the
{OneByteVector} methods that already exist.
Also, {StaticCharVector} is constexpr, but {StaticOneByteVector} cannot
be, since it contains a {reinterpret_cast}. The same holds for
{Vector::cast} in general.
This CL
- changes the return type of {StaticCharVector} to be
{Vector<const char>},
- introduces a new {StaticOneByteVector} which returns
{Vector<const uint8_t>},
- fixes constexpr annotations at various methods returning {Vector}s,
- refactors users of {StaticCharVector} to either use
{StaticOneByteVector} instead, or work on {char} if that makes more
sense.
R=leszeks@chromium.org
Bug: v8:10426
Change-Id: I71e336097e41ad30f982aa6344ca3d67b3a01fe3
Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/2154196
Commit-Queue: Clemens Backes <clemensb@chromium.org>
Reviewed-by: Leszek Swirski <leszeks@chromium.org>
Cr-Commit-Position: refs/heads/master@{#67213}
2020-04-17 13:44:55 +00:00
|
|
|
static constexpr auto kVec3 = StaticCharVector(kInit3);
|
2020-04-17 14:36:10 +00:00
|
|
|
STATIC_ASSERT(kVec3.size() == 6);
|
|
|
|
EXPECT_THAT(kVec3, testing::ElementsAreArray(kInit3, kInit3 + 6));
|
|
|
|
}
|
|
|
|
|
2019-11-25 15:20:49 +00:00
|
|
|
} // namespace internal
|
|
|
|
} // namespace v8
|