mirror of
https://github.com/mhx/dwarfs.git
synced 2025-08-04 10:16:34 -04:00
test: add error tests
This commit is contained in:
parent
6267df8bcb
commit
ccfd97bf80
@ -603,6 +603,7 @@ if(WITH_TESTS)
|
|||||||
compat_test
|
compat_test
|
||||||
dwarfs_test
|
dwarfs_test
|
||||||
entry_test
|
entry_test
|
||||||
|
error_test
|
||||||
file_access_test
|
file_access_test
|
||||||
fragment_category_test
|
fragment_category_test
|
||||||
incompressible_categorizer_test
|
incompressible_categorizer_test
|
||||||
@ -1020,6 +1021,7 @@ foreach(tgt dwarfs dwarfs_compression dwarfs_categorizer
|
|||||||
target_link_options(${tgt} PRIVATE -fprofile-instr-generate
|
target_link_options(${tgt} PRIVATE -fprofile-instr-generate
|
||||||
-fcoverage-mapping)
|
-fcoverage-mapping)
|
||||||
endif()
|
endif()
|
||||||
|
target_compile_definitions(${tgt} PRIVATE DWARFS_COVERAGE_ENABLED=1)
|
||||||
endif()
|
endif()
|
||||||
endforeach()
|
endforeach()
|
||||||
|
|
||||||
|
@ -35,6 +35,18 @@
|
|||||||
|
|
||||||
namespace dwarfs {
|
namespace dwarfs {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
[[noreturn]] void do_terminate() {
|
||||||
|
#ifdef DWARFS_COVERAGE_ENABLED
|
||||||
|
std::exit(99);
|
||||||
|
#else
|
||||||
|
std::terminate();
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
system_error::system_error(char const* file, int line) noexcept
|
system_error::system_error(char const* file, int line) noexcept
|
||||||
: system_error(errno, file, line) {}
|
: system_error(errno, file, line) {}
|
||||||
|
|
||||||
@ -68,14 +80,14 @@ void handle_nothrow(char const* expr, char const* file, int line) {
|
|||||||
std::cerr << "Expression `" << expr << "` threw `"
|
std::cerr << "Expression `" << expr << "` threw `"
|
||||||
<< folly::exceptionStr(std::current_exception()) << "` in " << file
|
<< folly::exceptionStr(std::current_exception()) << "` in " << file
|
||||||
<< "(" << line << ")\n";
|
<< "(" << line << ")\n";
|
||||||
::abort();
|
do_terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
void assertion_failed(char const* expr, std::string const& msg,
|
void assertion_failed(char const* expr, std::string const& msg,
|
||||||
char const* file, int line) {
|
char const* file, int line) {
|
||||||
std::cerr << "Assertion `" << expr << "` failed in " << file << "(" << line
|
std::cerr << "Assertion `" << expr << "` failed in " << file << "(" << line
|
||||||
<< "): " << msg << "\n";
|
<< "): " << msg << "\n";
|
||||||
::abort();
|
do_terminate();
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace dwarfs
|
} // namespace dwarfs
|
||||||
|
98
test/error_test.cpp
Normal file
98
test/error_test.cpp
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
/* vim:set ts=2 sw=2 sts=2 et: */
|
||||||
|
/**
|
||||||
|
* \author Marcus Holland-Moritz (github@mhxnet.de)
|
||||||
|
* \copyright Copyright (c) Marcus Holland-Moritz
|
||||||
|
*
|
||||||
|
* This file is part of dwarfs.
|
||||||
|
*
|
||||||
|
* dwarfs is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* dwarfs is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with dwarfs. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <cerrno>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
|
||||||
|
#include <folly/String.h>
|
||||||
|
|
||||||
|
#include "dwarfs/error.h"
|
||||||
|
|
||||||
|
using namespace dwarfs;
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
int test_throw_runtime_error(bool throw_it) {
|
||||||
|
if (throw_it) {
|
||||||
|
DWARFS_THROW(runtime_error, "my test error");
|
||||||
|
}
|
||||||
|
return __LINE__ - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
int test_throw_system_error(bool throw_it) {
|
||||||
|
if (throw_it) {
|
||||||
|
errno = EPERM;
|
||||||
|
DWARFS_THROW(system_error, "my test system error");
|
||||||
|
}
|
||||||
|
return __LINE__ - 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(error_test, runtime_error) {
|
||||||
|
int expected_line = test_throw_runtime_error(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
test_throw_runtime_error(true);
|
||||||
|
FAIL() << "expected runtime_error to be thrown";
|
||||||
|
} catch (const runtime_error& e) {
|
||||||
|
EXPECT_EQ("my test error", std::string(e.what()));
|
||||||
|
EXPECT_EQ("error_test.cpp",
|
||||||
|
std::filesystem::path(e.file()).filename().string());
|
||||||
|
EXPECT_EQ(expected_line, e.line());
|
||||||
|
} catch (...) {
|
||||||
|
FAIL() << "expected runtime_error, got "
|
||||||
|
<< folly::exceptionStr(std::current_exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(error_test, system_error) {
|
||||||
|
int expected_line = test_throw_system_error(false);
|
||||||
|
|
||||||
|
try {
|
||||||
|
test_throw_system_error(true);
|
||||||
|
FAIL() << "expected system_error to be thrown";
|
||||||
|
} catch (const system_error& e) {
|
||||||
|
EXPECT_THAT(std::string(e.what()),
|
||||||
|
::testing::MatchesRegex("my test system error: .*"));
|
||||||
|
EXPECT_EQ("error_test.cpp",
|
||||||
|
std::filesystem::path(e.file()).filename().string());
|
||||||
|
EXPECT_EQ(EPERM, e.get_errno());
|
||||||
|
EXPECT_EQ(expected_line, e.line());
|
||||||
|
} catch (...) {
|
||||||
|
FAIL() << "expected system_error, got "
|
||||||
|
<< folly::exceptionStr(std::current_exception());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(error_test, dwarfs_check) {
|
||||||
|
DWARFS_CHECK(true, "my test error");
|
||||||
|
EXPECT_DEATH(DWARFS_CHECK(false, "my test error"), "my test error");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(error_test, dwarfs_nothrow) {
|
||||||
|
std::vector<int> v{1, 2, 3};
|
||||||
|
EXPECT_EQ(3, DWARFS_NOTHROW(v.at(2)));
|
||||||
|
EXPECT_DEATH(DWARFS_NOTHROW(v.at(3)), "Expression `v.at\\(3\\)` threw .*");
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user