mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-08 05:27:14 +09:00

A challenge for getting LibTest working on Windows has always been CrashTest. It implements death tests similar to Google Test where a child process is cloned to invoke the expression that should abort/terminate the program. Then the exit code of the child is used by the parent test process to verify if the application correctly aborted/terminated due to invoking the expression. The problem was that finding an equivalent way to port Crash::run() to Windows was not looking very likely as publicly exposed Win32/ Native APIs have no equivalent to fork(); however, Windows actually does have native support for process cloning via undocumented NT APIs that clever people reverse engineered and published, see `NtCreateUserProcess()`. All that being said, this `EXPECT_DEATH()` implementation avoids needing to use a child process in general, allowing us to remove CrashTest in favour of a single cross-platform solution for death tests.
90 lines
2.6 KiB
C++
90 lines
2.6 KiB
C++
/*
|
|
* Copyright (c) 2018-2021, Andreas Kling <andreas@ladybird.org>
|
|
*
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
*/
|
|
|
|
#include <LibTest/TestCase.h>
|
|
#include <LibTest/TestSuite.h>
|
|
|
|
#include <AK/FixedArray.h>
|
|
#include <AK/NoAllocationGuard.h>
|
|
|
|
TEST_CASE(construct)
|
|
{
|
|
EXPECT_EQ(FixedArray<int>().size(), 0u);
|
|
EXPECT_EQ(FixedArray<int>::must_create_but_fixme_should_propagate_errors(1985).size(), 1985u);
|
|
}
|
|
|
|
TEST_CASE(ints)
|
|
{
|
|
FixedArray<int> ints = FixedArray<int>::must_create_but_fixme_should_propagate_errors(3);
|
|
ints[0] = 0;
|
|
ints[1] = 1;
|
|
ints[2] = 2;
|
|
EXPECT_EQ(ints[0], 0);
|
|
EXPECT_EQ(ints[1], 1);
|
|
EXPECT_EQ(ints[2], 2);
|
|
}
|
|
|
|
TEST_CASE(conforms_to_iterator_procotol)
|
|
{
|
|
static_assert(std::random_access_iterator<FixedArray<int>::Iterator>);
|
|
static_assert(std::random_access_iterator<FixedArray<int>::ConstIterator>);
|
|
static_assert(std::random_access_iterator<FixedArray<int const>::Iterator>);
|
|
static_assert(std::random_access_iterator<FixedArray<int const>::ConstIterator>);
|
|
}
|
|
|
|
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);
|
|
EXPECT_NO_DEATH("Assignments", [&]() {
|
|
NoAllocationGuard guard;
|
|
array[0] = 0;
|
|
array[1] = 1;
|
|
array[2] = 2;
|
|
array[4] = array[1];
|
|
array[3] = array[0] + array[2];
|
|
}());
|
|
|
|
EXPECT_NO_DEATH("Move", [&]() {
|
|
FixedArray<int> moved_from_array = FixedArray<int>::must_create_but_fixme_should_propagate_errors(6);
|
|
// 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));
|
|
}
|
|
}());
|
|
|
|
EXPECT_NO_DEATH("Swap", [&]() {
|
|
FixedArray<int> target_for_swapping;
|
|
{
|
|
NoAllocationGuard guard;
|
|
array.swap(target_for_swapping);
|
|
}
|
|
}());
|
|
}
|