Skip to content

Commit

Permalink
add call with catch in c api, add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Laurynas Jagutis <[email protected]>
  • Loading branch information
Laurynas-Jagutis committed Oct 23, 2024
1 parent 5b4598a commit 943e7d0
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,21 @@ struct PGM_IO_VnfConverter : public PgmVnfConverter {
};

// TODO(Laurynas-Jagutis) add call_with_catch for these functions.
PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(const PGM_IO_Handle* /*handle*/, char* file_buffer,
PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer,
PGM_IO_Idx experimental_features) {
auto* converter = new PGM_IO_VnfConverter(file_buffer, nullptr, experimental_features);
parse_vnf_file_wrapper(converter);
return converter;
return call_with_catch(
handle,
[file_buffer, experimental_features] {
auto* converter = new PGM_IO_VnfConverter(file_buffer, nullptr, experimental_features);
parse_vnf_file_wrapper(converter);
return converter;
},
PGM_IO_regular_error);
}

char const* PGM_IO_get_vnf_input_data(const PGM_IO_Handle* /*handle*/, PGM_IO_VnfConverter* converter_ptr) {
return convert_input_wrapper(converter_ptr).c_str();
char const* PGM_IO_get_vnf_input_data(PGM_IO_Handle* handle, PGM_IO_VnfConverter* converter_ptr) {
return call_with_catch(
handle, [converter_ptr] { return convert_input_wrapper(converter_ptr).c_str(); }, PGM_IO_regular_error);
}

void PGM_IO_destroy_vnf_converter(PGM_IO_VnfConverter* converter_ptr) { delete converter_ptr; }
36 changes: 30 additions & 6 deletions tests/c_api_tests/test_c_api_vnf_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
//
// SPDX-License-Identifier: MPL-2.0

#include <power_grid_model_io_native_c/basics.h>
#include <power_grid_model_io_native_c/handle.h>
#include <power_grid_model_io_native_c/vnf_pgm_converter.h>

Expand All @@ -12,17 +13,40 @@
namespace power_grid_model_io_native {

TEST_CASE("Test PGM_IO_create_vnf_converter") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_Idx experimental_feature_flag = 0;

SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") {
CHECK_THROWS_AS(PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag),
power_grid_model::ExperimentalFeature);
PGM_IO_Handle* handle0 = PGM_IO_create_handle();
auto converter = PGM_IO_create_vnf_converter(handle0, nullptr, experimental_feature_flag);
CHECK(PGM_IO_error_code(handle0) == PGM_IO_regular_error);
PGM_IO_destroy_vnf_converter(converter);
PGM_IO_destroy_handle(handle0);
}

SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") {
PGM_IO_Idx experimental_feature_flag = 1;
PGM_IO_Handle* handle1 = PGM_IO_create_handle();
experimental_feature_flag = 1;
auto converter = PGM_IO_create_vnf_converter(handle1, nullptr, experimental_feature_flag);
CHECK(converter != nullptr);
PGM_IO_destroy_vnf_converter(converter);
PGM_IO_destroy_handle(handle1);
}
};
}

TEST_CASE("Test PGM_IO_get_vnf_input_data") {
PGM_IO_Handle* handle = PGM_IO_create_handle();
PGM_IO_Idx experimental_feature_flag = 0;

experimental_feature_flag = 1;
auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag);
CHECK(converter != nullptr);

auto json_result = PGM_IO_get_vnf_input_data(handle, converter);
auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})";
CHECK(strcmp(json_string, json_result) == 0);

PGM_IO_destroy_vnf_converter(converter);
PGM_IO_destroy_handle(handle);
}

} // namespace power_grid_model_io_native
} // namespace power_grid_model_io_native
1 change: 1 addition & 0 deletions tests/cpp_unit_tests/test_pgm_vnf_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ namespace power_grid_model_io_native {

TEST_CASE("Test converter constructor") {
SUBCASE("Without experimental features enabled") {
PgmVnfConverter(nullptr, nullptr, 0);
CHECK_THROWS_AS(PgmVnfConverter(nullptr, nullptr, 0), power_grid_model::ExperimentalFeature);
}

Expand Down

0 comments on commit 943e7d0

Please sign in to comment.