diff --git a/tests/native_api_tests/test_api_meta_data.cpp b/tests/native_api_tests/test_api_meta_data.cpp index a50cf60e9..c264c2cc8 100644 --- a/tests/native_api_tests/test_api_meta_data.cpp +++ b/tests/native_api_tests/test_api_meta_data.cpp @@ -65,12 +65,8 @@ TEST_CASE("API Meta Data") { SUBCASE("Check error handling for unknown name") { using namespace std::string_literals; - try { - MetaData::get_attribute_by_name("No_dataset", "no_name", "no attribute"); - } catch (PowerGridRegularError const& e) { - std::string const err_msg{e.what()}; - CHECK(err_msg.find("You supplied wrong name and/or index!"s) != std::string::npos); - } + CHECK_THROWS_WITH_AS(MetaData::get_attribute_by_name("No_dataset", "no_name", "no attribute"), + doctest::Contains("You supplied wrong name and/or index!"), PowerGridRegularError); } } } diff --git a/tests/native_api_tests/test_api_model.cpp b/tests/native_api_tests/test_api_model.cpp index df46a05e2..08e8a9975 100644 --- a/tests/native_api_tests/test_api_model.cpp +++ b/tests/native_api_tests/test_api_model.cpp @@ -37,7 +37,19 @@ void check_exception(PowerGridError const& e, PGM_ErrorCode const& reference_err std::string_view reference_err_msg) { CHECK(e.error_code() == reference_error); std::string const err_msg{e.what()}; - CHECK(err_msg.find(reference_err_msg) != std::string::npos); + doctest::String const ref_err_msg{reference_err_msg.data()}; + REQUIRE(err_msg.c_str() == doctest::Contains(ref_err_msg)); +} + +template +void check_throws_with(Func&& func, PGM_ErrorCode const& reference_error, std::string_view reference_err_msg, + Args&&... args) { + try { + std::forward(func)(std::forward(args)...); + FAIL("Expected error not thrown."); + } catch (PowerGridRegularError const& e) { + check_exception(e, reference_error, reference_err_msg); + } } } // namespace @@ -281,36 +293,36 @@ TEST_CASE("API Model") { SUBCASE("Input error handling") { SUBCASE("Construction error") { load_id = 0; - try { - Model const wrong_model{50.0, input_dataset}; - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "Conflicting id detected:"s); - } + load_buffer.set_value(PGM_def_input_sym_load_id, &load_id, -1); + source_update_id = 1; + source_update_buffer.set_value(PGM_def_update_source_id, &source_update_id, 0, -1); + auto const wrong_model_lambda = [&input_dataset]() { Model const wrong_model{50.0, input_dataset}; }; + check_throws_with(wrong_model_lambda, PGM_regular_error, "Conflicting id detected:"s); } SUBCASE("Update error") { - source_update_id = 5; - try { - model.update(single_update_dataset); - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "The id cannot be found:"s); - } + load_id = 2; + load_buffer.set_value(PGM_def_input_sym_load_id, &load_id, -1); + source_update_id = 99; + source_update_buffer.set_value(PGM_def_update_source_id, &source_update_id, 0, -1); + auto const bad_update_lambda = [&model, &single_update_dataset]() { model.update(single_update_dataset); }; + check_throws_with(bad_update_lambda, PGM_regular_error, "The id cannot be found:"s); } SUBCASE("Invalid calculation type error") { - try { + auto const bad_calc_type_lambda = [&options, &model, &single_output_dataset]() { options.set_calculation_type(-128); - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "CalculationType is not implemented for"s); - } + model.calculate(options, single_output_dataset); + }; + check_throws_with(bad_calc_type_lambda, PGM_regular_error, "CalculationType is not implemented for"s); } SUBCASE("Invalid tap changing strategy error") { - try { + auto const bad_tap_strat_lambda = [&options, &model, &single_output_dataset]() { options.set_tap_changing_strategy(-128); - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "get_optimizer_type is not implemented for"s); - } + model.calculate(options, single_output_dataset); + }; + check_throws_with(bad_tap_strat_lambda, PGM_regular_error, "get_optimizer_type is not implemented for"s); } SUBCASE("Tap changing strategy") { @@ -326,28 +338,26 @@ TEST_CASE("API Model") { options.set_err_tol(1e-100); options.set_symmetric(0); options.set_threading(1); - try { - model.calculate(options, single_output_dataset); - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "Iteration failed to converge after"s); - } + auto const calc_error_lambda = [&model, &single_output_dataset](auto const& opt) { + model.calculate(opt, single_output_dataset); + }; + check_throws_with(calc_error_lambda, PGM_regular_error, "Iteration failed to converge after"s, options); // wrong method options.set_calculation_type(PGM_state_estimation); options.set_calculation_method(PGM_iterative_current); - try { - model.calculate(options, single_output_dataset); - } catch (PowerGridRegularError const& e) { - check_exception(e, PGM_regular_error, "The calculation method is invalid for this calculation!"s); - } + check_throws_with(calc_error_lambda, PGM_regular_error, + "The calculation method is invalid for this calculation!"s, options); } SUBCASE("Batch calculation error") { // wrong id - load_updates_id[1] = 5; + load_updates_id[1] = 999; + load_updates_buffer.set_value(PGM_def_update_sym_load_id, load_updates_id.data(), 1, -1); // failed in batch 1 try { model.calculate(options, batch_output_dataset, batch_update_dataset); + FAIL("Expected batch calculation error not thrown."); } catch (PowerGridBatchError const& e) { CHECK(e.error_code() == PGM_batch_error); auto const& failed_scenarios = e.failed_scenarios(); @@ -372,16 +382,6 @@ TEST_CASE("API Model") { CHECK(batch_node_result_u[1] == doctest::Approx(0.0)); CHECK(batch_node_result_u_pu[1] == doctest::Approx(0.0)); CHECK(batch_node_result_u_angle[1] == doctest::Approx(0.0)); - CHECK(batch_node_result_id[2] == 0); - CHECK(batch_node_result_energized[2] == 1); - CHECK(batch_node_result_u[2] == doctest::Approx(70.0)); - CHECK(batch_node_result_u_pu[2] == doctest::Approx(0.7)); - CHECK(batch_node_result_u_angle[2] == doctest::Approx(0.0)); - CHECK(batch_node_result_id[3] == 4); - CHECK(batch_node_result_energized[3] == 0); - CHECK(batch_node_result_u[3] == doctest::Approx(0.0)); - CHECK(batch_node_result_u_pu[3] == doctest::Approx(0.0)); - CHECK(batch_node_result_u_angle[3] == doctest::Approx(0.0)); } } } diff --git a/tests/native_api_tests/test_api_serialization.cpp b/tests/native_api_tests/test_api_serialization.cpp index 40405ee73..015a864ec 100644 --- a/tests/native_api_tests/test_api_serialization.cpp +++ b/tests/native_api_tests/test_api_serialization.cpp @@ -95,6 +95,7 @@ TEST_CASE("API Serialization and Deserialization") { SUBCASE("Invalid serialization format") { try { Serializer const unknown_serializer{dataset, -1}; + FAIL("Expected serialization error not thrown."); } catch (PowerGridSerializationError const& e) { CHECK(e.error_code() == PGM_serialization_error); }