2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2021-04-25 08:53:23 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2019-08-02 10:23:03 +03:00
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
#include <AK/DeprecatedString.h>
|
2019-08-14 12:02:49 +03:00
|
|
|
#include <AK/NonnullOwnPtrVector.h>
|
2019-07-21 12:35:09 +03:00
|
|
|
#include <AK/OwnPtr.h>
|
2023-01-02 17:15:14 +03:00
|
|
|
#include <AK/ReverseIterator.h>
|
2019-06-27 15:50:22 +03:00
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2019-07-16 11:08:39 +03:00
|
|
|
TEST_CASE(construct)
|
2019-06-27 15:50:22 +03:00
|
|
|
{
|
|
|
|
EXPECT(Vector<int>().is_empty());
|
|
|
|
EXPECT(Vector<int>().size() == 0);
|
2019-07-16 11:08:39 +03:00
|
|
|
}
|
2019-06-27 15:50:22 +03:00
|
|
|
|
2019-07-16 11:08:39 +03:00
|
|
|
TEST_CASE(ints)
|
|
|
|
{
|
2019-06-27 15:50:22 +03:00
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 3u);
|
2019-06-27 15:50:22 +03:00
|
|
|
EXPECT_EQ(ints.take_last(), 3);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 2u);
|
2019-06-27 15:50:22 +03:00
|
|
|
EXPECT_EQ(ints.take_last(), 2);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 1u);
|
2019-06-27 15:50:22 +03:00
|
|
|
EXPECT_EQ(ints.take_last(), 1);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 0u);
|
2019-06-27 15:50:22 +03:00
|
|
|
|
|
|
|
ints.clear();
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 0u);
|
2019-07-16 11:08:39 +03:00
|
|
|
}
|
2019-06-27 15:50:22 +03:00
|
|
|
|
2019-07-16 11:08:39 +03:00
|
|
|
TEST_CASE(strings)
|
|
|
|
{
|
2022-12-04 21:02:33 +03:00
|
|
|
Vector<DeprecatedString> strings;
|
2019-06-27 15:50:22 +03:00
|
|
|
strings.append("ABC");
|
|
|
|
strings.append("DEF");
|
|
|
|
|
|
|
|
int loop_counter = 0;
|
2022-12-04 21:02:33 +03:00
|
|
|
for (DeprecatedString const& string : strings) {
|
2019-06-27 15:50:22 +03:00
|
|
|
EXPECT(!string.is_null());
|
|
|
|
EXPECT(!string.is_empty());
|
|
|
|
++loop_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop_counter = 0;
|
2022-12-04 21:02:33 +03:00
|
|
|
for (auto& string : (const_cast<Vector<DeprecatedString> const&>(strings))) {
|
2019-06-27 15:50:22 +03:00
|
|
|
EXPECT(!string.is_null());
|
|
|
|
EXPECT(!string.is_empty());
|
|
|
|
++loop_counter;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(loop_counter, 2);
|
2019-07-16 11:08:39 +03:00
|
|
|
}
|
2019-06-27 15:50:22 +03:00
|
|
|
|
2019-07-16 11:08:39 +03:00
|
|
|
TEST_CASE(strings_insert_ordered)
|
|
|
|
{
|
2022-12-04 21:02:33 +03:00
|
|
|
Vector<DeprecatedString> strings;
|
2019-07-16 11:08:39 +03:00
|
|
|
strings.append("abc");
|
|
|
|
strings.append("def");
|
|
|
|
strings.append("ghi");
|
2019-07-04 15:20:48 +03:00
|
|
|
|
2022-07-11 20:32:29 +03:00
|
|
|
strings.insert_before_matching("f-g"sv, [](auto& entry) {
|
|
|
|
return "f-g"sv < entry;
|
2019-07-16 11:08:39 +03:00
|
|
|
});
|
2019-07-04 15:20:48 +03:00
|
|
|
|
2019-07-16 11:08:39 +03:00
|
|
|
EXPECT_EQ(strings[0], "abc");
|
|
|
|
EXPECT_EQ(strings[1], "def");
|
|
|
|
EXPECT_EQ(strings[2], "f-g");
|
|
|
|
EXPECT_EQ(strings[3], "ghi");
|
2019-06-27 15:50:22 +03:00
|
|
|
}
|
2019-07-16 11:08:39 +03:00
|
|
|
|
2019-07-20 17:10:52 +03:00
|
|
|
TEST_CASE(prepend_vector)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
|
|
|
|
Vector<int> more_ints;
|
|
|
|
more_ints.append(4);
|
|
|
|
more_ints.append(5);
|
|
|
|
more_ints.append(6);
|
|
|
|
|
|
|
|
ints.prepend(move(more_ints));
|
|
|
|
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 6u);
|
|
|
|
EXPECT_EQ(more_ints.size(), 0u);
|
2019-07-20 17:10:52 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(ints[0], 4);
|
|
|
|
EXPECT_EQ(ints[1], 5);
|
|
|
|
EXPECT_EQ(ints[2], 6);
|
|
|
|
EXPECT_EQ(ints[3], 1);
|
|
|
|
EXPECT_EQ(ints[4], 2);
|
|
|
|
EXPECT_EQ(ints[5], 3);
|
|
|
|
|
|
|
|
ints.prepend(move(more_ints));
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 6u);
|
|
|
|
EXPECT_EQ(more_ints.size(), 0u);
|
2019-07-20 17:10:52 +03:00
|
|
|
|
|
|
|
more_ints.prepend(move(ints));
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(more_ints.size(), 6u);
|
|
|
|
EXPECT_EQ(ints.size(), 0u);
|
2019-07-20 17:10:52 +03:00
|
|
|
}
|
|
|
|
|
2019-07-21 12:35:09 +03:00
|
|
|
TEST_CASE(prepend_vector_object)
|
|
|
|
{
|
|
|
|
struct SubObject {
|
2019-08-01 17:23:21 +03:00
|
|
|
SubObject(int v)
|
|
|
|
: value(v)
|
|
|
|
{
|
|
|
|
}
|
2019-07-21 12:35:09 +03:00
|
|
|
int value { 0 };
|
|
|
|
};
|
|
|
|
struct Object {
|
2019-08-01 17:23:21 +03:00
|
|
|
Object(NonnullOwnPtr<SubObject>&& a_subobject)
|
|
|
|
: subobject(move(a_subobject))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
OwnPtr<SubObject> subobject;
|
2019-07-21 12:35:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Vector<Object> objects;
|
2019-08-01 17:23:21 +03:00
|
|
|
objects.empend(make<SubObject>(1));
|
|
|
|
objects.empend(make<SubObject>(2));
|
|
|
|
objects.empend(make<SubObject>(3));
|
2019-07-21 12:35:09 +03:00
|
|
|
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(objects.size(), 3u);
|
2019-07-21 12:35:09 +03:00
|
|
|
|
|
|
|
Vector<Object> more_objects;
|
2019-08-01 17:23:21 +03:00
|
|
|
more_objects.empend(make<SubObject>(4));
|
|
|
|
more_objects.empend(make<SubObject>(5));
|
|
|
|
more_objects.empend(make<SubObject>(6));
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(more_objects.size(), 3u);
|
2019-07-21 12:35:09 +03:00
|
|
|
|
|
|
|
objects.prepend(move(more_objects));
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(more_objects.size(), 0u);
|
|
|
|
EXPECT_EQ(objects.size(), 6u);
|
2019-07-21 12:35:09 +03:00
|
|
|
|
2019-08-01 17:23:21 +03:00
|
|
|
EXPECT_EQ(objects[0].subobject->value, 4);
|
|
|
|
EXPECT_EQ(objects[1].subobject->value, 5);
|
|
|
|
EXPECT_EQ(objects[2].subobject->value, 6);
|
|
|
|
EXPECT_EQ(objects[3].subobject->value, 1);
|
|
|
|
EXPECT_EQ(objects[4].subobject->value, 2);
|
|
|
|
EXPECT_EQ(objects[5].subobject->value, 3);
|
2019-07-21 12:35:09 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:05:10 +03:00
|
|
|
TEST_CASE(vector_compare)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
Vector<int> same_ints;
|
|
|
|
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
|
|
|
ints.append(i);
|
|
|
|
same_ints.append(i);
|
|
|
|
}
|
|
|
|
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 1000u);
|
2019-08-07 16:05:10 +03:00
|
|
|
EXPECT_EQ(ints, same_ints);
|
|
|
|
|
2022-12-04 21:02:33 +03:00
|
|
|
Vector<DeprecatedString> strings;
|
|
|
|
Vector<DeprecatedString> same_strings;
|
2019-08-07 16:05:10 +03:00
|
|
|
|
|
|
|
for (int i = 0; i < 1000; ++i) {
|
2022-12-04 21:02:33 +03:00
|
|
|
strings.append(DeprecatedString::number(i));
|
|
|
|
same_strings.append(DeprecatedString::number(i));
|
2019-08-07 16:05:10 +03:00
|
|
|
}
|
|
|
|
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(strings.size(), 1000u);
|
2019-08-07 16:05:10 +03:00
|
|
|
EXPECT_EQ(strings, same_strings);
|
|
|
|
}
|
|
|
|
|
2019-08-07 21:42:56 +03:00
|
|
|
TEST_CASE(grow_past_inline_capacity)
|
|
|
|
{
|
|
|
|
auto make_vector = [] {
|
2022-12-04 21:02:33 +03:00
|
|
|
Vector<DeprecatedString, 16> strings;
|
2019-08-07 21:42:56 +03:00
|
|
|
for (int i = 0; i < 32; ++i) {
|
2022-12-04 21:02:33 +03:00
|
|
|
strings.append(DeprecatedString::number(i));
|
2019-08-07 21:42:56 +03:00
|
|
|
}
|
|
|
|
return strings;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto strings = make_vector();
|
|
|
|
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(strings.size(), 32u);
|
2019-08-07 21:42:56 +03:00
|
|
|
EXPECT_EQ(strings[31], "31");
|
|
|
|
|
|
|
|
strings.clear();
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(strings.size(), 0u);
|
|
|
|
EXPECT_EQ(strings.capacity(), 16u);
|
2019-08-07 21:42:56 +03:00
|
|
|
|
|
|
|
strings = make_vector();
|
|
|
|
|
|
|
|
strings.clear_with_capacity();
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(strings.size(), 0u);
|
|
|
|
EXPECT(strings.capacity() >= 32u);
|
2019-08-07 21:42:56 +03:00
|
|
|
}
|
|
|
|
|
2019-08-07 16:25:34 +03:00
|
|
|
BENCHMARK_CASE(vector_append_trivial)
|
|
|
|
{
|
|
|
|
// This should be super fast thanks to Vector using memmove.
|
|
|
|
Vector<int> ints;
|
|
|
|
for (int i = 0; i < 1000000; ++i) {
|
|
|
|
ints.append(i);
|
|
|
|
}
|
|
|
|
for (int i = 0; i < 100; ++i) {
|
|
|
|
Vector<int> tmp;
|
2021-06-12 14:24:45 +03:00
|
|
|
tmp.extend(ints);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(tmp.size(), 1000000u);
|
2019-08-07 16:25:34 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-12 11:35:05 +03:00
|
|
|
BENCHMARK_CASE(vector_remove_trivial)
|
|
|
|
{
|
|
|
|
// This should be super fast thanks to Vector using memmove.
|
|
|
|
Vector<int> ints;
|
|
|
|
for (int i = 0; i < 10000; ++i) {
|
|
|
|
ints.append(i);
|
|
|
|
}
|
|
|
|
while (!ints.is_empty()) {
|
|
|
|
ints.remove(0);
|
|
|
|
}
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 0u);
|
2019-08-12 11:35:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(vector_remove)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
ints.append(4);
|
|
|
|
ints.append(5);
|
|
|
|
|
|
|
|
ints.remove(1);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 4u);
|
2019-08-12 11:35:05 +03:00
|
|
|
EXPECT_EQ(ints[0], 1);
|
|
|
|
EXPECT_EQ(ints[1], 3);
|
|
|
|
EXPECT_EQ(ints[2], 4);
|
|
|
|
EXPECT_EQ(ints[3], 5);
|
|
|
|
|
|
|
|
ints.remove(0);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 3u);
|
2019-08-12 11:35:05 +03:00
|
|
|
EXPECT_EQ(ints[0], 3);
|
|
|
|
EXPECT_EQ(ints[1], 4);
|
|
|
|
EXPECT_EQ(ints[2], 5);
|
|
|
|
|
|
|
|
ints.take_last();
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 2u);
|
2019-08-12 11:35:05 +03:00
|
|
|
EXPECT_EQ(ints[0], 3);
|
|
|
|
EXPECT_EQ(ints[1], 4);
|
|
|
|
|
|
|
|
ints.take_first();
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 1u);
|
2019-08-12 11:35:05 +03:00
|
|
|
EXPECT_EQ(ints[0], 4);
|
|
|
|
}
|
|
|
|
|
2022-01-05 19:00:17 +03:00
|
|
|
TEST_CASE(remove_all_matching)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
ints.append(4);
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.size(), 4u);
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.remove_all_matching([&](int value) { return value > 2; }), true);
|
|
|
|
EXPECT_EQ(ints.remove_all_matching([&](int) { return false; }), false);
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.size(), 2u);
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.remove_all_matching([&](int) { return true; }), true);
|
|
|
|
|
|
|
|
EXPECT(ints.is_empty());
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.remove_all_matching([&](int) { return true; }), false);
|
|
|
|
}
|
|
|
|
|
2019-08-14 12:02:49 +03:00
|
|
|
TEST_CASE(nonnullownptrvector)
|
|
|
|
{
|
|
|
|
struct Object {
|
2022-12-04 21:02:33 +03:00
|
|
|
DeprecatedString string;
|
2019-08-14 12:02:49 +03:00
|
|
|
};
|
|
|
|
NonnullOwnPtrVector<Object> objects;
|
|
|
|
|
|
|
|
objects.append(make<Object>());
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(objects.size(), 1u);
|
2019-08-14 12:02:49 +03:00
|
|
|
|
|
|
|
OwnPtr<Object> o = make<Object>();
|
|
|
|
objects.append(o.release_nonnull());
|
|
|
|
EXPECT(o == nullptr);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(objects.size(), 2u);
|
2019-08-14 12:02:49 +03:00
|
|
|
}
|
|
|
|
|
2020-01-19 14:15:43 +03:00
|
|
|
TEST_CASE(insert_trivial)
|
|
|
|
{
|
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(0);
|
|
|
|
ints.append(10);
|
|
|
|
ints.append(20);
|
|
|
|
ints.append(30);
|
|
|
|
ints.append(40);
|
|
|
|
ints.insert(2, 15);
|
2020-03-06 13:21:23 +03:00
|
|
|
EXPECT_EQ(ints.size(), 6u);
|
2020-01-19 14:15:43 +03:00
|
|
|
EXPECT_EQ(ints[0], 0);
|
|
|
|
EXPECT_EQ(ints[1], 10);
|
|
|
|
EXPECT_EQ(ints[2], 15);
|
|
|
|
EXPECT_EQ(ints[3], 20);
|
|
|
|
EXPECT_EQ(ints[4], 30);
|
|
|
|
EXPECT_EQ(ints[5], 40);
|
|
|
|
}
|
|
|
|
|
2020-09-07 14:17:12 +03:00
|
|
|
TEST_CASE(resize_initializes)
|
|
|
|
{
|
|
|
|
struct A {
|
|
|
|
A() { initialized = true; }
|
|
|
|
bool initialized { false };
|
|
|
|
};
|
|
|
|
|
|
|
|
Vector<A> ints;
|
|
|
|
ints.resize(32);
|
|
|
|
|
|
|
|
for (size_t idx = 0; idx < 32; ++idx)
|
|
|
|
EXPECT(ints[idx].initialized);
|
|
|
|
}
|
|
|
|
|
2020-11-15 19:45:53 +03:00
|
|
|
TEST_CASE(should_compare_vectors_of_same_type)
|
|
|
|
{
|
|
|
|
Vector<int> a {};
|
|
|
|
Vector<int> b {};
|
|
|
|
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
a.append(1);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
|
|
|
|
b.append(1);
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
a.append(42);
|
|
|
|
b.append(17);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_compare_vectors_of_different_inline_capacity)
|
|
|
|
{
|
|
|
|
Vector<int, 1> a {};
|
|
|
|
Vector<int, 64> b {};
|
|
|
|
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
a.append(1);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
|
|
|
|
b.append(1);
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
a.append(42);
|
|
|
|
b.append(17);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_compare_vectors_of_different_sizes)
|
|
|
|
{
|
|
|
|
Vector<int, 0> a {};
|
|
|
|
Vector<int, 0> b {};
|
|
|
|
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
// A is longer
|
|
|
|
a.append(1);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
|
|
|
|
b.append(1);
|
|
|
|
EXPECT(a == b);
|
|
|
|
EXPECT(!(a != b));
|
|
|
|
|
|
|
|
// B is longer
|
|
|
|
b.append(42);
|
|
|
|
EXPECT(!(a == b));
|
|
|
|
EXPECT(a != b);
|
|
|
|
}
|
|
|
|
|
2020-12-23 21:35:20 +03:00
|
|
|
TEST_CASE(should_find_value)
|
|
|
|
{
|
2021-02-25 23:10:47 +03:00
|
|
|
Vector<int> v { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
|
2020-12-23 21:35:20 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const expected = v.begin() + 4;
|
2020-12-23 21:35:20 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(expected, v.find(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_find_predicate)
|
|
|
|
{
|
2021-02-25 23:10:47 +03:00
|
|
|
Vector<int> v { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
|
2020-12-23 21:35:20 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const expected = v.begin() + 4;
|
2020-12-23 21:35:20 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
EXPECT_EQ(expected, v.find_if([](auto const v) { return v == 0; }));
|
2020-12-23 21:35:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_find_index)
|
|
|
|
{
|
2021-02-25 23:10:47 +03:00
|
|
|
Vector<int> v { 1, 2, 3, 4, 0, 6, 7, 8, 0, 0 };
|
2020-12-23 21:35:20 +03:00
|
|
|
|
|
|
|
EXPECT_EQ(4u, v.find_first_index(0).value());
|
|
|
|
EXPECT(!v.find_first_index(42).has_value());
|
|
|
|
}
|
2021-05-10 23:49:54 +03:00
|
|
|
|
|
|
|
TEST_CASE(should_contain_start)
|
|
|
|
{
|
|
|
|
// Tests whether value is found if at the start of the range.
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5 };
|
|
|
|
|
|
|
|
EXPECT(v.contains_in_range(1, 0, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_contain_end)
|
|
|
|
{
|
|
|
|
// Tests whether value is found if at the end of the range.
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5 };
|
|
|
|
|
|
|
|
EXPECT(v.contains_in_range(5, 0, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_contain_range)
|
|
|
|
{
|
|
|
|
// Tests whether value is found within a range.
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5 };
|
|
|
|
|
|
|
|
EXPECT(v.contains_in_range(3, 0, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_not_contain_not_present)
|
|
|
|
{
|
|
|
|
// Tests whether a value that is not present is not found, as expected.
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5 };
|
|
|
|
|
|
|
|
EXPECT(!v.contains_in_range(6, 0, 4));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(should_not_contain_present_not_in_range)
|
|
|
|
{
|
|
|
|
// Tests whether a value that is present, but not in range, is not found.
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5 };
|
|
|
|
|
|
|
|
EXPECT(!v.contains_in_range(2, 2, 4));
|
|
|
|
}
|
2021-06-08 16:24:06 +03:00
|
|
|
|
|
|
|
TEST_CASE(can_store_references)
|
|
|
|
{
|
|
|
|
int my_integer = 42;
|
|
|
|
Vector<int&> references;
|
|
|
|
references.append(my_integer);
|
|
|
|
references.prepend(my_integer);
|
|
|
|
EXPECT_EQ(&references.first(), &references.last());
|
|
|
|
|
|
|
|
{
|
|
|
|
Vector<int&> other_references;
|
2021-06-12 14:24:45 +03:00
|
|
|
other_references.extend(references);
|
2021-06-08 16:24:06 +03:00
|
|
|
EXPECT_EQ(&other_references.first(), &my_integer);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
Vector<int&> other_references;
|
|
|
|
other_references = references;
|
|
|
|
EXPECT_EQ(&other_references.first(), &my_integer);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto it = references.find(my_integer);
|
|
|
|
EXPECT(!it.is_end());
|
|
|
|
EXPECT_EQ(*it, my_integer);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
int other_integer = 42;
|
|
|
|
auto index = references.find_first_index(other_integer);
|
|
|
|
EXPECT(index.has_value());
|
|
|
|
EXPECT_EQ(index.value_or(99999u), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
auto integer = 42;
|
|
|
|
EXPECT(references.contains_slow(integer));
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
references.remove(0);
|
|
|
|
references.ensure_capacity(10);
|
|
|
|
EXPECT_EQ(&references.take_first(), &my_integer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(reference_deletion_should_not_affect_object)
|
|
|
|
{
|
|
|
|
size_t times_deleted = 0;
|
|
|
|
struct DeleteCounter {
|
|
|
|
explicit DeleteCounter(size_t& deleted)
|
|
|
|
: deleted(deleted)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~DeleteCounter()
|
|
|
|
{
|
|
|
|
++deleted;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t& deleted;
|
|
|
|
};
|
|
|
|
|
|
|
|
{
|
|
|
|
DeleteCounter counter { times_deleted };
|
|
|
|
Vector<DeleteCounter&> references;
|
|
|
|
for (size_t i = 0; i < 16; ++i)
|
|
|
|
references.append(counter);
|
|
|
|
}
|
|
|
|
EXPECT_EQ(times_deleted, 1u);
|
|
|
|
}
|
2022-02-23 17:59:00 +03:00
|
|
|
|
|
|
|
TEST_CASE(rbegin)
|
|
|
|
{
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
|
|
|
|
|
|
|
|
auto const expected = v.begin() + 4;
|
|
|
|
auto const expected_in_reverse = v.rbegin() + 4;
|
|
|
|
EXPECT_EQ(*expected, *expected_in_reverse);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(rend)
|
|
|
|
{
|
|
|
|
Vector<int> v { 1, 2, 3, 4, 5, 6, 7, 8, 0 };
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
auto const expected = v.end() - 5;
|
|
|
|
auto const expected_in_reverse = v.rend() - 5;
|
2022-02-23 17:59:00 +03:00
|
|
|
EXPECT_EQ(*expected, *expected_in_reverse);
|
|
|
|
}
|
|
|
|
|
2022-02-25 13:41:13 +03:00
|
|
|
TEST_CASE(reverse_iterator_for_loop)
|
2022-02-23 17:59:00 +03:00
|
|
|
{
|
|
|
|
Vector<int> v { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
int index = 9;
|
|
|
|
for (auto rev = v.rbegin(); rev != v.rend(); ++rev)
|
|
|
|
EXPECT_EQ(*rev, index--);
|
|
|
|
}
|
2022-02-25 13:41:13 +03:00
|
|
|
|
|
|
|
TEST_CASE(reverse_range_for_loop)
|
|
|
|
{
|
|
|
|
Vector<int> v { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
|
|
|
|
int index = 9;
|
|
|
|
for (auto item : AK::ReverseWrapper::in_reverse(v))
|
|
|
|
EXPECT_EQ(item, index--);
|
2022-03-05 16:05:56 +03:00
|
|
|
|
|
|
|
index = 9;
|
|
|
|
for (auto item : v.in_reverse())
|
|
|
|
EXPECT_EQ(item, index--);
|
2022-02-25 13:41:13 +03:00
|
|
|
}
|