2021-07-11 18:16:13 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2022-01-13 00:33:43 +03:00
|
|
|
#include <LibTest/TestCase.h>
|
2021-07-11 18:16:13 +03:00
|
|
|
#include <LibTest/TestSuite.h>
|
|
|
|
|
|
|
|
#include <AK/FixedArray.h>
|
2022-01-13 00:33:43 +03:00
|
|
|
#include <AK/NoAllocationGuard.h>
|
2021-07-11 18:16:13 +03:00
|
|
|
|
|
|
|
TEST_CASE(construct)
|
|
|
|
{
|
2022-01-13 00:33:43 +03:00
|
|
|
EXPECT_EQ(FixedArray<int>().size(), 0u);
|
|
|
|
EXPECT_EQ(FixedArray<int>::must_create_but_fixme_should_propagate_errors(1985).size(), 1985u);
|
2021-07-11 18:16:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(ints)
|
|
|
|
{
|
2022-01-08 22:06:03 +03:00
|
|
|
FixedArray<int> ints = FixedArray<int>::must_create_but_fixme_should_propagate_errors(3);
|
2021-07-11 18:16:13 +03:00
|
|
|
ints[0] = 0;
|
|
|
|
ints[1] = 1;
|
|
|
|
ints[2] = 2;
|
|
|
|
EXPECT_EQ(ints[0], 0);
|
|
|
|
EXPECT_EQ(ints[1], 1);
|
|
|
|
EXPECT_EQ(ints[2], 2);
|
|
|
|
}
|
2022-01-13 00:33:43 +03:00
|
|
|
|
|
|
|
TEST_CASE(swap)
|
|
|
|
{
|
|
|
|
FixedArray<int> first = FixedArray<int>::must_create_but_fixme_should_propagate_errors(4);
|
|
|
|
FixedArray<int> second = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
|
|
|
|
first[3] = 1;
|
|
|
|
second[3] = 2;
|
|
|
|
first.swap(second);
|
|
|
|
EXPECT_EQ(first.size(), 5u);
|
|
|
|
EXPECT_EQ(second.size(), 4u);
|
|
|
|
EXPECT_EQ(first[3], 2);
|
|
|
|
EXPECT_EQ(second[3], 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(move)
|
|
|
|
{
|
|
|
|
FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
|
|
|
|
FixedArray<int> moved_to_array(move(moved_from_array));
|
|
|
|
EXPECT_EQ(moved_to_array.size(), 6u);
|
|
|
|
EXPECT_EQ(moved_from_array.size(), 0u);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_CASE(no_allocation)
|
|
|
|
{
|
|
|
|
FixedArray<int> array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(5);
|
2022-09-10 00:53:53 +03:00
|
|
|
EXPECT_NO_CRASH("Assignments", [&] {
|
2022-01-13 00:33:43 +03:00
|
|
|
NoAllocationGuard guard;
|
|
|
|
array[0] = 0;
|
|
|
|
array[1] = 1;
|
|
|
|
array[2] = 2;
|
|
|
|
array[4] = array[1];
|
|
|
|
array[3] = array[0] + array[2];
|
|
|
|
return Test::Crash::Failure::DidNotCrash;
|
|
|
|
});
|
|
|
|
|
|
|
|
EXPECT_NO_CRASH("Move", [&] {
|
|
|
|
FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
|
2022-09-11 13:44:30 +03:00
|
|
|
// We need an Optional here to ensure that the NoAllocationGuard is
|
|
|
|
// destroyed before the moved_to_array, because that would call free
|
|
|
|
Optional<FixedArray<int>> moved_to_array;
|
|
|
|
|
|
|
|
{
|
|
|
|
NoAllocationGuard guard;
|
|
|
|
moved_to_array.emplace(move(moved_from_array));
|
|
|
|
}
|
|
|
|
|
2022-01-13 00:33:43 +03:00
|
|
|
return Test::Crash::Failure::DidNotCrash;
|
|
|
|
});
|
|
|
|
|
|
|
|
EXPECT_NO_CRASH("Swap", [&] {
|
|
|
|
FixedArray<int> target_for_swapping;
|
|
|
|
{
|
|
|
|
NoAllocationGuard guard;
|
|
|
|
array.swap(target_for_swapping);
|
|
|
|
}
|
|
|
|
return Test::Crash::Failure::DidNotCrash;
|
|
|
|
});
|
|
|
|
}
|