2019-07-16 08:08:39 +00:00
|
|
|
#include <AK/TestSuite.h>
|
2019-06-27 12:50:22 +00:00
|
|
|
#include <AK/AKString.h>
|
|
|
|
#include <AK/Vector.h>
|
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(construct)
|
2019-06-27 12:50:22 +00:00
|
|
|
{
|
|
|
|
EXPECT(Vector<int>().is_empty());
|
|
|
|
EXPECT(Vector<int>().size() == 0);
|
2019-07-16 08:08:39 +00:00
|
|
|
}
|
2019-06-27 12:50:22 +00:00
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(ints)
|
|
|
|
{
|
2019-06-27 12:50:22 +00:00
|
|
|
Vector<int> ints;
|
|
|
|
ints.append(1);
|
|
|
|
ints.append(2);
|
|
|
|
ints.append(3);
|
|
|
|
EXPECT_EQ(ints.size(), 3);
|
|
|
|
EXPECT_EQ(ints.take_last(), 3);
|
|
|
|
EXPECT_EQ(ints.size(), 2);
|
|
|
|
EXPECT_EQ(ints.take_last(), 2);
|
|
|
|
EXPECT_EQ(ints.size(), 1);
|
|
|
|
EXPECT_EQ(ints.take_last(), 1);
|
|
|
|
EXPECT_EQ(ints.size(), 0);
|
|
|
|
|
|
|
|
ints.clear();
|
|
|
|
EXPECT_EQ(ints.size(), 0);
|
2019-07-16 08:08:39 +00:00
|
|
|
}
|
2019-06-27 12:50:22 +00:00
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(strings)
|
|
|
|
{
|
2019-06-27 12:50:22 +00:00
|
|
|
Vector<String> strings;
|
|
|
|
strings.append("ABC");
|
|
|
|
strings.append("DEF");
|
|
|
|
|
|
|
|
int loop_counter = 0;
|
|
|
|
for (const String& string : strings) {
|
|
|
|
EXPECT(!string.is_null());
|
|
|
|
EXPECT(!string.is_empty());
|
|
|
|
++loop_counter;
|
|
|
|
}
|
|
|
|
|
|
|
|
loop_counter = 0;
|
|
|
|
for (auto& string : (const_cast<const Vector<String>&>(strings))) {
|
|
|
|
EXPECT(!string.is_null());
|
|
|
|
EXPECT(!string.is_empty());
|
|
|
|
++loop_counter;
|
|
|
|
}
|
|
|
|
EXPECT_EQ(loop_counter, 2);
|
2019-07-16 08:08:39 +00:00
|
|
|
}
|
2019-06-27 12:50:22 +00:00
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_CASE(strings_insert_ordered)
|
|
|
|
{
|
|
|
|
Vector<String> strings;
|
|
|
|
strings.append("abc");
|
|
|
|
strings.append("def");
|
|
|
|
strings.append("ghi");
|
2019-07-04 12:20:48 +00:00
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
strings.insert_before_matching("f-g", [](auto& entry) {
|
|
|
|
return "f-g" < entry;
|
|
|
|
});
|
2019-07-04 12:20:48 +00:00
|
|
|
|
2019-07-16 08:08:39 +00: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 12:50:22 +00:00
|
|
|
}
|
2019-07-16 08:08:39 +00:00
|
|
|
|
2019-07-20 14:10:52 +00: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));
|
|
|
|
|
|
|
|
EXPECT_EQ(ints.size(), 6);
|
|
|
|
EXPECT_EQ(more_ints.size(), 0);
|
|
|
|
|
|
|
|
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));
|
|
|
|
EXPECT_EQ(ints.size(), 6);
|
|
|
|
EXPECT_EQ(more_ints.size(), 0);
|
|
|
|
|
|
|
|
more_ints.prepend(move(ints));
|
|
|
|
EXPECT_EQ(more_ints.size(), 6);
|
|
|
|
EXPECT_EQ(ints.size(), 0);
|
|
|
|
}
|
|
|
|
|
2019-07-16 08:08:39 +00:00
|
|
|
TEST_MAIN(Vector)
|