Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/pcms/utility/assert.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#ifndef PCMS_COUPLING_ASSERT_H
#define PCMS_COUPLING_ASSERT_H
#include <stdexcept>
#include <mpi.h>

// https://stackoverflow.com/questions/16683146/can-macros-be-overloaded-by-number-of-arguments
Expand Down Expand Up @@ -45,6 +46,12 @@

namespace pcms
{

struct pcms_error : std::runtime_error
{
using std::runtime_error::runtime_error;
};

// from scorec/core/pcu_fail.h
void Pcms_Assert_Fail(const char* msg) __attribute__((noreturn));
} // namespace pcms
Expand Down
1 change: 1 addition & 0 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ if(Catch2_FOUND)
list(
APPEND
PCMS_UNIT_TEST_SOURCES
test_error_handling.cpp
test_field_transfer.cpp
test_uniform_grid.cpp
test_omega_h_copy.cpp
Expand Down
31 changes: 31 additions & 0 deletions test/test_error_handling.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#include <catch2/catch_test_macros.hpp>
#include "pcms/utility/assert.h"
#include "pcms/utility/print.h"
#include <iostream>

int raise_error(int code)
{
if (code) {
throw pcms::pcms_error("Test exception - Raising error for testing");
return 1;
}
return 0;
}

TEST_CASE("pcms error handling test with try-catch")
{
bool exception_caught = false;
bool correct_exception_type = false;

try {
raise_error(1);
FAIL("Expected exception was not thrown");
} catch (const pcms::pcms_error& e) {
exception_caught = true;
correct_exception_type = true;
std::cout << "Caught pcms_error: " << e.what() << std::endl;
}

REQUIRE(exception_caught);
REQUIRE(correct_exception_type);
}