From 2631f1f442877afe6c4fa7ac050caca20ffa93f5 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 16 Oct 2024 11:31:44 +0200 Subject: [PATCH 01/29] add experimental feature flag, add test files, adjust cmake for tests, add unit tests Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 33 +++++---- .../vnf_pgm_converter.h | 3 +- .../src/vnf_pgm_converter.cpp | 5 +- tests/c_api_tests/CMakeLists.txt | 3 + .../c_api_tests/test_c_api_vnf_converter.cpp | 30 ++++++++ .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 73 ++++++++++++++++++- 6 files changed, 130 insertions(+), 17 deletions(-) create mode 100644 tests/c_api_tests/test_c_api_vnf_converter.cpp diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index ddb2272..77fe681 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -6,6 +6,8 @@ #ifndef POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP #define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP +#include + #include #include #include @@ -19,7 +21,7 @@ inline power_grid_model::ConstDataset create_const_dataset_from_container(power_grid_model::Container const& /*container*/, power_grid_model::meta_data::MetaData const& meta_data) { // for now leave it empty - std::string_view const dataset_name = "empty_dataset"; + std::string_view const dataset_name = "input"; power_grid_model::ConstDataset const_dataset{false, 1, dataset_name, meta_data}; return const_dataset; } @@ -32,24 +34,26 @@ inline std::string serialize_data(power_grid_model::ConstDataset const& const_da class PgmVnfConverter { public: - PgmVnfConverter(char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr); + PgmVnfConverter(char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, + power_grid_model_io_native::Idx experimental_feature_flag = 0); // Public member functions void parse_vnf_file(); void convert_input(); std::string const& get_serialized_data() const; - void set_file_buffer(char* file_buffer); + void set_file_buffer(const char* file_buffer); void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data); - char* get_file_buffer(); + const char* get_file_buffer(); power_grid_model::WritableDataset* get_deserialized_data(); private: // Private attributes - char* f_file_buffer; + const char* f_file_buffer; power_grid_model::WritableDataset* deserialized_data; // this type because it is generated by a deserializer type structure std::string serialized_data; + power_grid_model_io_native::Idx experimental_feature_flag; // Private member functions static std::vector convert_node_input(); @@ -62,12 +66,15 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(char* buffer, power_grid_model::WritableDataset* data) - : f_file_buffer(buffer), deserialized_data(data) { - using namespace std::string_literals; - using power_grid_model::ExperimentalFeature; - throw ExperimentalFeature{"PGM_VNF_converter", ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", - .value = std::to_string(1)}}; +inline PgmVnfConverter::PgmVnfConverter(char* buffer, power_grid_model::WritableDataset* data, + power_grid_model_io_native::Idx experimental_feature_flag) + : f_file_buffer(buffer), deserialized_data(data), experimental_feature_flag(experimental_feature_flag) { + if (experimental_feature_flag == 0) { + using namespace std::string_literals; + using power_grid_model::ExperimentalFeature; + throw ExperimentalFeature{"PGM_VNF_converter", ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", + .value = std::to_string(1)}}; + } } inline void PgmVnfConverter::parse_vnf_file() { @@ -104,13 +111,13 @@ inline void PgmVnfConverter::convert_input() { this->serialized_data = serialized_pgm_data; } -inline void PgmVnfConverter::set_file_buffer(char* file_buffer) { this->f_file_buffer = file_buffer; } +inline void PgmVnfConverter::set_file_buffer(const char* file_buffer) { this->f_file_buffer = file_buffer; } inline void PgmVnfConverter::set_deserialized_data(power_grid_model::WritableDataset* data) { this->deserialized_data = data; } -inline char* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } +inline const char* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; } diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h index f540552..a91af0d 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h @@ -19,7 +19,8 @@ extern "C" { * @return The pointer to a PGM_IO_VnfConverter instance. The instance must be freed by * PGM_IO_delete_vnf_converter. */ -PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer); +PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer, + PGM_IO_Idx experimental_features); /** * @brief Retrieve the transformed input data from .vnf format to PGM format diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index c7573ee..1ce182b 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -19,8 +19,9 @@ 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) { - auto* converter = new PGM_IO_VnfConverter(file_buffer); +PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(const 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; } diff --git a/tests/c_api_tests/CMakeLists.txt b/tests/c_api_tests/CMakeLists.txt index beb5468..80e418f 100644 --- a/tests/c_api_tests/CMakeLists.txt +++ b/tests/c_api_tests/CMakeLists.txt @@ -5,12 +5,15 @@ set(PROJECT_SOURCES "test_c_api.cpp" "test_entry_point.cpp" + "test_c_api_vnf_converter.cpp" ) add_executable(power_grid_model_io_native_c_api_tests ${PROJECT_SOURCES}) target_link_libraries(power_grid_model_io_native_c_api_tests PRIVATE + power_grid_model + power_grid_model_io_native power_grid_model_io_native_c doctest::doctest ) diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp new file mode 100644 index 0000000..168eec8 --- /dev/null +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -0,0 +1,30 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +// #include +#include + +#include + +#include "C:\headers\true_pgmionative\power-grid-model-io-native\power_grid_model_io_native_c\power_grid_model_io_native_c\src\vnf_pgm_converter.cpp" + +#include + +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); + } + + SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") { + PGM_IO_Idx experimental_feature_flag = 1; + } +}; + +} // namespace power_grid_model_io_native \ No newline at end of file diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index 21f12ce..c7b2fbd 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -4,10 +4,81 @@ #include +#include #include namespace power_grid_model_io_native { -TEST_CASE("Test converter constructor") { CHECK_THROWS_AS(PgmVnfConverter(), power_grid_model::ExperimentalFeature); }; +TEST_CASE("Test converter constructor") { + SUBCASE("Without experimental features enabled") { + CHECK_THROWS_AS(PgmVnfConverter(nullptr, nullptr, 0), power_grid_model::ExperimentalFeature); + } + + SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter(nullptr, nullptr, 1)); } +} + +TEST_CASE("Test if the parse_vnf_file function can be called") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + CHECK_NOTHROW(converter.parse_vnf_file()); +} + +TEST_CASE("Test the convert_input function") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + SUBCASE("Test if the convert_input function can be called") { CHECK_NOTHROW(converter.convert_input()); } + SUBCASE("Test the return of convert_input") { + converter.convert_input(); + auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + auto json_result = converter.get_serialized_data(); + CHECK(json_string == json_result); + } +} + +TEST_CASE("Test if the create_const_dataset_from_container function can be called") { + power_grid_model::Container container; + constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; + CHECK_NOTHROW(create_const_dataset_from_container(container, meta_data)); +} + +TEST_CASE("Test the serialize_data function") { + power_grid_model::Container container; + constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; + power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data); + CHECK_NOTHROW(serialize_data(const_dataset)); + auto result = serialize_data(const_dataset); + auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + CHECK(result == json_string); +} + +TEST_CASE("Test the setter and getter of file_buffer") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + const char* value = "123"; + converter.set_file_buffer(value); + auto file_buff = converter.get_file_buffer(); + CHECK(file_buff == value); + CHECK(strcmp(file_buff, value) == 0); +} + +TEST_CASE("Test the setter and getter of deserialized data") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + power_grid_model::WritableDataset* dataset = nullptr; + converter.set_deserialized_data(dataset); + auto const_data = converter.get_deserialized_data(); + CHECK(dataset == const_data); +} + +TEST_CASE("Test parse_vnf_file_wrapper") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + PgmVnfConverter* converterPtr = &converter; + CHECK_NOTHROW(parse_vnf_file_wrapper(converterPtr)); +} + +TEST_CASE("Test convert_input_wrapper") { + auto converter = PgmVnfConverter(nullptr, nullptr, 1); + PgmVnfConverter* converterPtr = &converter; + auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + CHECK_NOTHROW(convert_input_wrapper(converterPtr)); + auto result = convert_input_wrapper(converterPtr); + CHECK(json_string == result); +} } // namespace power_grid_model_io_native From 65e491c0dfb1b5b9d2b7de4e09bb3928a8f78ea7 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Thu, 17 Oct 2024 13:22:36 +0200 Subject: [PATCH 02/29] adjust c api test file Signed-off-by: Laurynas Jagutis --- tests/c_api_tests/test_c_api_vnf_converter.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index 168eec8..c2e3817 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -2,13 +2,11 @@ // // SPDX-License-Identifier: MPL-2.0 -// #include #include +#include #include -#include "C:\headers\true_pgmionative\power-grid-model-io-native\power_grid_model_io_native_c\power_grid_model_io_native_c\src\vnf_pgm_converter.cpp" - #include namespace power_grid_model_io_native { From 943e7d01f61212aa10f17f21196eacb7f8da9391 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 10:29:15 +0200 Subject: [PATCH 03/29] add call with catch in c api, add tests Signed-off-by: Laurynas Jagutis --- .../src/vnf_pgm_converter.cpp | 18 ++++++---- .../c_api_tests/test_c_api_vnf_converter.cpp | 36 +++++++++++++++---- .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 1 + 3 files changed, 43 insertions(+), 12 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index c31e7c7..2ffb52b 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -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; } diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index c2e3817..4a16644 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 +#include #include #include @@ -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 \ No newline at end of file +} // namespace power_grid_model_io_native diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index c7b2fbd..c0b5fe5 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -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); } From 7b6ce9cb601f3f7ad273e62a9afdf3415e800a81 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 10:50:07 +0200 Subject: [PATCH 04/29] fix tests, fix some warnings Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 3 +-- tests/cpp_unit_tests/test_pgm_vnf_converter.cpp | 1 - 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 77fe681..69f3907 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -53,7 +53,6 @@ class PgmVnfConverter { power_grid_model::WritableDataset* deserialized_data; // this type because it is generated by a deserializer type structure std::string serialized_data; - power_grid_model_io_native::Idx experimental_feature_flag; // Private member functions static std::vector convert_node_input(); @@ -68,7 +67,7 @@ class PgmVnfConverter { inline PgmVnfConverter::PgmVnfConverter(char* buffer, power_grid_model::WritableDataset* data, power_grid_model_io_native::Idx experimental_feature_flag) - : f_file_buffer(buffer), deserialized_data(data), experimental_feature_flag(experimental_feature_flag) { + : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { using namespace std::string_literals; using power_grid_model::ExperimentalFeature; diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index c0b5fe5..c7b2fbd 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -11,7 +11,6 @@ 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); } From 0eda749f8cb96e550ddc1314d2d67f7b25a979f1 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 11:01:48 +0200 Subject: [PATCH 05/29] fix clang tidy warnings Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 69f3907..83356a1 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -65,7 +65,7 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(char* buffer, power_grid_model::WritableDataset* data, +inline PgmVnfConverter::PgmVnfConverter(char* const buffer, power_grid_model::WritableDataset* data, power_grid_model_io_native::Idx experimental_feature_flag) : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { From b489cdcc17a79cc77452c69f6dad42474def773f Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 11:04:06 +0200 Subject: [PATCH 06/29] add an include Signed-off-by: Laurynas Jagutis --- tests/c_api_tests/test_c_api_vnf_converter.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index 4a16644..3770ef0 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -8,6 +8,7 @@ #include +#include #include namespace power_grid_model_io_native { From b821a749e78d377386ed547f319f70a2740b9b44 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 11:10:40 +0200 Subject: [PATCH 07/29] const Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 83356a1..18467eb 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -65,7 +65,7 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(char* const buffer, power_grid_model::WritableDataset* data, +inline PgmVnfConverter::PgmVnfConverter(const char* buffer, power_grid_model::WritableDataset* data, power_grid_model_io_native::Idx experimental_feature_flag) : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { From 2f50ad93cea200faa76feddb27124508deabe8fd Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 11:13:36 +0200 Subject: [PATCH 08/29] const Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 18467eb..49a4225 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -34,7 +34,7 @@ inline std::string serialize_data(power_grid_model::ConstDataset const& const_da class PgmVnfConverter { public: - PgmVnfConverter(char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, + PgmVnfConverter(const char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, power_grid_model_io_native::Idx experimental_feature_flag = 0); // Public member functions From ba2fa7dfc0dea3c53f0b76cf774b0254600a3d40 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 15:39:06 +0200 Subject: [PATCH 09/29] use using, move const, remove unused namespace, remove 1 and add a complete explanation Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 29 ++++++++++--------- .../c_api_tests/test_c_api_vnf_converter.cpp | 14 ++++----- 2 files changed, 22 insertions(+), 21 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 49a4225..edd9d34 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -32,24 +32,26 @@ inline std::string serialize_data(power_grid_model::ConstDataset const& const_da return serialized_pgm_data; } +using namespace power_grid_model_io_native; + class PgmVnfConverter { public: - PgmVnfConverter(const char* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, - power_grid_model_io_native::Idx experimental_feature_flag = 0); + PgmVnfConverter(char const* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, + Idx experimental_feature_flag = 0); // Public member functions void parse_vnf_file(); void convert_input(); std::string const& get_serialized_data() const; - void set_file_buffer(const char* file_buffer); + void set_file_buffer(char const* file_buffer); void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data); - const char* get_file_buffer(); + char const* get_file_buffer(); power_grid_model::WritableDataset* get_deserialized_data(); private: // Private attributes - const char* f_file_buffer; + char const* f_file_buffer; power_grid_model::WritableDataset* deserialized_data; // this type because it is generated by a deserializer type structure std::string serialized_data; @@ -65,14 +67,13 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(const char* buffer, power_grid_model::WritableDataset* data, - power_grid_model_io_native::Idx experimental_feature_flag) +inline PgmVnfConverter::PgmVnfConverter(char const* buffer, power_grid_model::WritableDataset* data, + Idx experimental_feature_flag) : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { - using namespace std::string_literals; using power_grid_model::ExperimentalFeature; throw ExperimentalFeature{"PGM_VNF_converter", ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", - .value = std::to_string(1)}}; + .value = "PgmVnfConverter is still in an experimental phase, if you'd like to use it, enable experimental features."}}; } } @@ -93,11 +94,11 @@ inline void PgmVnfConverter::convert_input() { power_grid_model::Container container; - for (const auto& node : nodes) { + for (auto const& node : nodes) { container.emplace(node.id(), node); } - constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; + constexpr auto const& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data); std::string const serialized_pgm_data = serialize_data(const_dataset); @@ -110,13 +111,13 @@ inline void PgmVnfConverter::convert_input() { this->serialized_data = serialized_pgm_data; } -inline void PgmVnfConverter::set_file_buffer(const char* file_buffer) { this->f_file_buffer = file_buffer; } +inline void PgmVnfConverter::set_file_buffer(char const* file_buffer) { this->f_file_buffer = file_buffer; } inline void PgmVnfConverter::set_deserialized_data(power_grid_model::WritableDataset* data) { this->deserialized_data = data; } -inline const char* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } +inline char const* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; } @@ -127,7 +128,7 @@ inline std::vector PgmVnfConverter::convert_node_input() std::vector nodes; nodes.reserve(node_inputs.size()); - for (const auto& node_input : node_inputs) { + for (auto const& node_input : node_inputs) { nodes.emplace_back(node_input); } diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index 3770ef0..b0e3a2c 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -17,20 +17,20 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") { PGM_IO_Idx experimental_feature_flag = 0; SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") { - 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_Handle* handle = PGM_IO_create_handle(); + auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); + CHECK(PGM_IO_error_code(handle) == PGM_IO_regular_error); PGM_IO_destroy_vnf_converter(converter); - PGM_IO_destroy_handle(handle0); + PGM_IO_destroy_handle(handle); } SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") { - PGM_IO_Handle* handle1 = PGM_IO_create_handle(); + PGM_IO_Handle* handle = PGM_IO_create_handle(); experimental_feature_flag = 1; - auto converter = PGM_IO_create_vnf_converter(handle1, nullptr, experimental_feature_flag); + auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); CHECK(converter != nullptr); PGM_IO_destroy_vnf_converter(converter); - PGM_IO_destroy_handle(handle1); + PGM_IO_destroy_handle(handle); } } From b94586eb7fdfa37e7174a26c068e65c6831735c6 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 15:46:38 +0200 Subject: [PATCH 10/29] change handle names in tests, reformat Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 9 ++++++--- tests/c_api_tests/test_c_api_vnf_converter.cpp | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index edd9d34..76cf2f6 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -72,14 +72,17 @@ inline PgmVnfConverter::PgmVnfConverter(char const* buffer, power_grid_model::Wr : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { using power_grid_model::ExperimentalFeature; - throw ExperimentalFeature{"PGM_VNF_converter", ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", - .value = "PgmVnfConverter is still in an experimental phase, if you'd like to use it, enable experimental features."}}; + throw ExperimentalFeature{ + "PGM_VNF_converter", + ExperimentalFeature::TypeValuePair{.name = "PGM_VNF_conversion", + .value = "PgmVnfConverter is still in an experimental phase, if you'd " + "like to use it, enable experimental features."}}; } } inline void PgmVnfConverter::parse_vnf_file() { // the function should use a deserializer type structure - // will be implemented later + // will be implemented later. } inline void PgmVnfConverter::convert_input() { diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index b0e3a2c..8337ef1 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -12,7 +12,7 @@ #include namespace power_grid_model_io_native { - +// test TEST_CASE("Test PGM_IO_create_vnf_converter") { PGM_IO_Idx experimental_feature_flag = 0; From c641bbaea872e878d1ee79dd3b1c3353dba5c966 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 16:24:41 +0200 Subject: [PATCH 11/29] change char const to string_view, adjust tests Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 14 +++++------ .../vnf_pgm_converter.h | 2 +- .../src/vnf_pgm_converter.cpp | 2 +- .../c_api_tests/test_c_api_vnf_converter.cpp | 10 ++++---- .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 25 +++++++++---------- 5 files changed, 26 insertions(+), 27 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 76cf2f6..a8ee780 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -36,7 +36,7 @@ using namespace power_grid_model_io_native; class PgmVnfConverter { public: - PgmVnfConverter(char const* buffer = nullptr, power_grid_model::WritableDataset* data = nullptr, + PgmVnfConverter(std::string_view buffer, power_grid_model::WritableDataset* data, Idx experimental_feature_flag = 0); // Public member functions @@ -44,14 +44,14 @@ class PgmVnfConverter { void convert_input(); std::string const& get_serialized_data() const; - void set_file_buffer(char const* file_buffer); + void set_file_buffer(std::string_view file_buffer); void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data); - char const* get_file_buffer(); + std::string_view get_file_buffer(); power_grid_model::WritableDataset* get_deserialized_data(); private: // Private attributes - char const* f_file_buffer; + std::string_view f_file_buffer; power_grid_model::WritableDataset* deserialized_data; // this type because it is generated by a deserializer type structure std::string serialized_data; @@ -67,7 +67,7 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(char const* buffer, power_grid_model::WritableDataset* data, +inline PgmVnfConverter::PgmVnfConverter(std::string_view buffer, power_grid_model::WritableDataset* data, Idx experimental_feature_flag) : f_file_buffer(buffer), deserialized_data(data) { if (experimental_feature_flag == 0) { @@ -114,13 +114,13 @@ inline void PgmVnfConverter::convert_input() { this->serialized_data = serialized_pgm_data; } -inline void PgmVnfConverter::set_file_buffer(char const* file_buffer) { this->f_file_buffer = file_buffer; } +inline void PgmVnfConverter::set_file_buffer(std::string_view file_buffer) { this->f_file_buffer = file_buffer; } inline void PgmVnfConverter::set_deserialized_data(power_grid_model::WritableDataset* data) { this->deserialized_data = data; } -inline char const* PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } +inline std::string_view PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; } diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h index 7858acc..eebe146 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h @@ -19,7 +19,7 @@ extern "C" { * @return The pointer to a PGM_IO_VnfConverter instance. The instance must be freed by * PGM_IO_destroy_vnf_converter. */ -PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer, +PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer, PGM_IO_Idx experimental_features); /** diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 2ffb52b..5d3e506 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -19,7 +19,7 @@ struct PGM_IO_VnfConverter : public PgmVnfConverter { }; // TODO(Laurynas-Jagutis) add call_with_catch for these functions. -PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char* file_buffer, +PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer, PGM_IO_Idx experimental_features) { return call_with_catch( handle, diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index 8337ef1..0fd1982 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -18,7 +18,7 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") { SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") { PGM_IO_Handle* handle = PGM_IO_create_handle(); - auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); + auto converter = PGM_IO_create_vnf_converter(handle, "", experimental_feature_flag); CHECK(PGM_IO_error_code(handle) == PGM_IO_regular_error); PGM_IO_destroy_vnf_converter(converter); PGM_IO_destroy_handle(handle); @@ -27,7 +27,7 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") { SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") { PGM_IO_Handle* handle = PGM_IO_create_handle(); experimental_feature_flag = 1; - auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); + auto converter = PGM_IO_create_vnf_converter(handle, "", experimental_feature_flag); CHECK(converter != nullptr); PGM_IO_destroy_vnf_converter(converter); PGM_IO_destroy_handle(handle); @@ -39,12 +39,12 @@ TEST_CASE("Test PGM_IO_get_vnf_input_data") { PGM_IO_Idx experimental_feature_flag = 0; experimental_feature_flag = 1; - auto converter = PGM_IO_create_vnf_converter(handle, nullptr, experimental_feature_flag); + auto converter = PGM_IO_create_vnf_converter(handle, "", 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); + std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + CHECK(json_string == json_result); PGM_IO_destroy_vnf_converter(converter); PGM_IO_destroy_handle(handle); diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index c7b2fbd..e963c8b 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -11,23 +11,23 @@ namespace power_grid_model_io_native { TEST_CASE("Test converter constructor") { SUBCASE("Without experimental features enabled") { - CHECK_THROWS_AS(PgmVnfConverter(nullptr, nullptr, 0), power_grid_model::ExperimentalFeature); + CHECK_THROWS_AS(PgmVnfConverter("", nullptr, 0), power_grid_model::ExperimentalFeature); } - SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter(nullptr, nullptr, 1)); } + SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter("", nullptr, 1)); } } TEST_CASE("Test if the parse_vnf_file function can be called") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); + auto converter = PgmVnfConverter("", nullptr, 1); CHECK_NOTHROW(converter.parse_vnf_file()); } TEST_CASE("Test the convert_input function") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); + auto converter = PgmVnfConverter("", nullptr, 1); SUBCASE("Test if the convert_input function can be called") { CHECK_NOTHROW(converter.convert_input()); } SUBCASE("Test the return of convert_input") { converter.convert_input(); - auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; auto json_result = converter.get_serialized_data(); CHECK(json_string == json_result); } @@ -45,21 +45,20 @@ TEST_CASE("Test the serialize_data function") { power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data); CHECK_NOTHROW(serialize_data(const_dataset)); auto result = serialize_data(const_dataset); - auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; CHECK(result == json_string); } TEST_CASE("Test the setter and getter of file_buffer") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); - const char* value = "123"; + auto converter = PgmVnfConverter("", nullptr, 1); + std::string_view value = "123"; converter.set_file_buffer(value); auto file_buff = converter.get_file_buffer(); CHECK(file_buff == value); - CHECK(strcmp(file_buff, value) == 0); } TEST_CASE("Test the setter and getter of deserialized data") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); + auto converter = PgmVnfConverter("", nullptr, 1); power_grid_model::WritableDataset* dataset = nullptr; converter.set_deserialized_data(dataset); auto const_data = converter.get_deserialized_data(); @@ -67,15 +66,15 @@ TEST_CASE("Test the setter and getter of deserialized data") { } TEST_CASE("Test parse_vnf_file_wrapper") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); + auto converter = PgmVnfConverter("", nullptr, 1); PgmVnfConverter* converterPtr = &converter; CHECK_NOTHROW(parse_vnf_file_wrapper(converterPtr)); } TEST_CASE("Test convert_input_wrapper") { - auto converter = PgmVnfConverter(nullptr, nullptr, 1); + auto converter = PgmVnfConverter("", nullptr, 1); PgmVnfConverter* converterPtr = &converter; - auto json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; CHECK_NOTHROW(convert_input_wrapper(converterPtr)); auto result = convert_input_wrapper(converterPtr); CHECK(json_string == result); From 81207d45a1d792c73d988254b29132ea3993d0a6 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 23 Oct 2024 16:43:15 +0200 Subject: [PATCH 12/29] add namespaces Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 6 +++++- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 2 ++ 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index a8ee780..0aecf88 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -17,6 +17,8 @@ #include +namespace power_grid_model_io_native { + inline power_grid_model::ConstDataset create_const_dataset_from_container(power_grid_model::Container const& /*container*/, power_grid_model::meta_data::MetaData const& meta_data) { @@ -32,7 +34,7 @@ inline std::string serialize_data(power_grid_model::ConstDataset const& const_da return serialized_pgm_data; } -using namespace power_grid_model_io_native; +// using namespace power_grid_model_io_native; class PgmVnfConverter { public: @@ -173,4 +175,6 @@ inline std::string const& convert_input_wrapper(PgmVnfConverter* obj) { return obj->get_serialized_data(); } +} // namespace power_grid_model_io_native + #endif // POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 5d3e506..0de60a1 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -12,6 +12,8 @@ #include +using namespace power_grid_model_io_native; + using power_grid_model::ConstDataset; struct PGM_IO_VnfConverter : public PgmVnfConverter { From d5f0b7df2342cbc9f600f824c7e664fca7e65b36 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 11:19:44 +0100 Subject: [PATCH 13/29] add enum, remove writable dataset from c api, adjust tests accordingly Signed-off-by: Laurynas Jagutis --- .../common/enum.hpp | 16 ++++++++++ .../vnf_converter/vnf_pgm_converter.hpp | 15 +++++---- .../vnf_pgm_converter.h | 2 +- .../src/vnf_pgm_converter.cpp | 10 +++--- .../c_api_tests/test_c_api_vnf_converter.cpp | 13 +++++--- .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 31 ++++++++++--------- 6 files changed, 54 insertions(+), 33 deletions(-) create mode 100644 power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/common/enum.hpp diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/common/enum.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/common/enum.hpp new file mode 100644 index 0000000..9ce2a84 --- /dev/null +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/common/enum.hpp @@ -0,0 +1,16 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#pragma once + +#include "common.hpp" + +namespace power_grid_model_io_native { + +enum class ExperimentalFeatures : IntS { + experimental_features_disabled = 0, + experimental_features_enabled = 1, +}; + +} // namespace power_grid_model_io_native diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 0aecf88..ecfda78 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -7,6 +7,7 @@ #define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP #include +#include #include #include @@ -34,12 +35,11 @@ inline std::string serialize_data(power_grid_model::ConstDataset const& const_da return serialized_pgm_data; } -// using namespace power_grid_model_io_native; - class PgmVnfConverter { public: - PgmVnfConverter(std::string_view buffer, power_grid_model::WritableDataset* data, - Idx experimental_feature_flag = 0); + using enum ExperimentalFeatures; + PgmVnfConverter(std::string_view buffer, + ExperimentalFeatures experimental_feature_flag = experimental_features_disabled); // Public member functions void parse_vnf_file(); @@ -69,10 +69,9 @@ class PgmVnfConverter { void convert_links_input(); }; -inline PgmVnfConverter::PgmVnfConverter(std::string_view buffer, power_grid_model::WritableDataset* data, - Idx experimental_feature_flag) - : f_file_buffer(buffer), deserialized_data(data) { - if (experimental_feature_flag == 0) { +inline PgmVnfConverter::PgmVnfConverter(std::string_view buffer, ExperimentalFeatures experimental_feature_flag) + : f_file_buffer(buffer) { + if (experimental_feature_flag == experimental_features_disabled) { using power_grid_model::ExperimentalFeature; throw ExperimentalFeature{ "PGM_VNF_converter", diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h index eebe146..b11e85e 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/include/power_grid_model_io_native_c/vnf_pgm_converter.h @@ -20,7 +20,7 @@ extern "C" { * PGM_IO_destroy_vnf_converter. */ PGM_IO_API PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer, - PGM_IO_Idx experimental_features); + PGM_IO_ExperimentalFeatures experimental_features); /** * @brief Retrieve the transformed input data from .vnf format to PGM format diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 0de60a1..98b43ea 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -4,6 +4,7 @@ #define PGM_IO_DLL_EXPORTS +#include #include #include "handle.hpp" @@ -14,19 +15,18 @@ using namespace power_grid_model_io_native; -using power_grid_model::ConstDataset; - struct PGM_IO_VnfConverter : public PgmVnfConverter { using PgmVnfConverter::PgmVnfConverter; }; -// TODO(Laurynas-Jagutis) add call_with_catch for these functions. PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char const* file_buffer, - PGM_IO_Idx experimental_features) { + PGM_IO_ExperimentalFeatures experimental_features) { return call_with_catch( handle, [file_buffer, experimental_features] { - auto* converter = new PGM_IO_VnfConverter(file_buffer, nullptr, experimental_features); + ExperimentalFeatures experimental_feature = + static_cast(static_cast(experimental_features)); + auto* converter = new PGM_IO_VnfConverter(file_buffer, experimental_feature); parse_vnf_file_wrapper(converter); return converter; }, diff --git a/tests/c_api_tests/test_c_api_vnf_converter.cpp b/tests/c_api_tests/test_c_api_vnf_converter.cpp index 0fd1982..00ac5a4 100644 --- a/tests/c_api_tests/test_c_api_vnf_converter.cpp +++ b/tests/c_api_tests/test_c_api_vnf_converter.cpp @@ -2,6 +2,8 @@ // // SPDX-License-Identifier: MPL-2.0 +# + #include #include #include @@ -12,9 +14,11 @@ #include namespace power_grid_model_io_native { -// test + +using enum PGM_IO_ExperimentalFeatures; + TEST_CASE("Test PGM_IO_create_vnf_converter") { - PGM_IO_Idx experimental_feature_flag = 0; + PGM_IO_ExperimentalFeatures experimental_feature_flag = PGM_IO_experimental_features_disabled; SUBCASE("Test PGM_IO_create_vnf_converter without experimental feature flag") { PGM_IO_Handle* handle = PGM_IO_create_handle(); @@ -26,7 +30,7 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") { SUBCASE("Test PGM_IO_create_vnf_converter with experimental feature flag") { PGM_IO_Handle* handle = PGM_IO_create_handle(); - experimental_feature_flag = 1; + experimental_feature_flag = PGM_IO_experimental_features_enabled; auto converter = PGM_IO_create_vnf_converter(handle, "", experimental_feature_flag); CHECK(converter != nullptr); PGM_IO_destroy_vnf_converter(converter); @@ -36,9 +40,8 @@ TEST_CASE("Test PGM_IO_create_vnf_converter") { TEST_CASE("Test PGM_IO_get_vnf_input_data") { PGM_IO_Handle* handle = PGM_IO_create_handle(); - PGM_IO_Idx experimental_feature_flag = 0; + PGM_IO_ExperimentalFeatures experimental_feature_flag = PGM_IO_experimental_features_enabled; - experimental_feature_flag = 1; auto converter = PGM_IO_create_vnf_converter(handle, "", experimental_feature_flag); CHECK(converter != nullptr); diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index e963c8b..284a947 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -2,6 +2,7 @@ // // SPDX-License-Identifier: MPL-2.0 +#include #include #include @@ -9,21 +10,23 @@ namespace power_grid_model_io_native { +using enum ExperimentalFeatures; + TEST_CASE("Test converter constructor") { SUBCASE("Without experimental features enabled") { - CHECK_THROWS_AS(PgmVnfConverter("", nullptr, 0), power_grid_model::ExperimentalFeature); + CHECK_THROWS_AS(PgmVnfConverter("", experimental_features_disabled), power_grid_model::ExperimentalFeature); } - SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter("", nullptr, 1)); } + SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter("", experimental_features_enabled)); } } TEST_CASE("Test if the parse_vnf_file function can be called") { - auto converter = PgmVnfConverter("", nullptr, 1); + auto converter = PgmVnfConverter("", experimental_features_enabled); CHECK_NOTHROW(converter.parse_vnf_file()); } TEST_CASE("Test the convert_input function") { - auto converter = PgmVnfConverter("", nullptr, 1); + auto converter = PgmVnfConverter("", experimental_features_enabled); SUBCASE("Test if the convert_input function can be called") { CHECK_NOTHROW(converter.convert_input()); } SUBCASE("Test the return of convert_input") { converter.convert_input(); @@ -50,29 +53,29 @@ TEST_CASE("Test the serialize_data function") { } TEST_CASE("Test the setter and getter of file_buffer") { - auto converter = PgmVnfConverter("", nullptr, 1); + auto converter = PgmVnfConverter("", experimental_features_enabled); std::string_view value = "123"; converter.set_file_buffer(value); auto file_buff = converter.get_file_buffer(); CHECK(file_buff == value); } -TEST_CASE("Test the setter and getter of deserialized data") { - auto converter = PgmVnfConverter("", nullptr, 1); - power_grid_model::WritableDataset* dataset = nullptr; - converter.set_deserialized_data(dataset); - auto const_data = converter.get_deserialized_data(); - CHECK(dataset == const_data); -} +// TEST_CASE("Test the setter and getter of deserialized data") { +// auto converter = PgmVnfConverter("", experimental_features_enabled); +// power_grid_model::WritableDataset* dataset = nullptr; +// converter.set_deserialized_data(dataset); +// auto const_data = converter.get_deserialized_data(); +// CHECK(dataset == const_data); +// } TEST_CASE("Test parse_vnf_file_wrapper") { - auto converter = PgmVnfConverter("", nullptr, 1); + auto converter = PgmVnfConverter("", experimental_features_enabled); PgmVnfConverter* converterPtr = &converter; CHECK_NOTHROW(parse_vnf_file_wrapper(converterPtr)); } TEST_CASE("Test convert_input_wrapper") { - auto converter = PgmVnfConverter("", nullptr, 1); + auto converter = PgmVnfConverter("", experimental_features_enabled); PgmVnfConverter* converterPtr = &converter; std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; CHECK_NOTHROW(convert_input_wrapper(converterPtr)); From 6ebd5dcdc48707e5cf135de70614aa1f64711677 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 11:22:52 +0100 Subject: [PATCH 14/29] make json_string global in tests Signed-off-by: Laurynas Jagutis --- tests/cpp_unit_tests/test_pgm_vnf_converter.cpp | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index 284a947..a8e47fc 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -12,6 +12,8 @@ namespace power_grid_model_io_native { using enum ExperimentalFeatures; +std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; + TEST_CASE("Test converter constructor") { SUBCASE("Without experimental features enabled") { CHECK_THROWS_AS(PgmVnfConverter("", experimental_features_disabled), power_grid_model::ExperimentalFeature); @@ -30,7 +32,6 @@ TEST_CASE("Test the convert_input function") { SUBCASE("Test if the convert_input function can be called") { CHECK_NOTHROW(converter.convert_input()); } SUBCASE("Test the return of convert_input") { converter.convert_input(); - std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; auto json_result = converter.get_serialized_data(); CHECK(json_string == json_result); } @@ -48,7 +49,6 @@ TEST_CASE("Test the serialize_data function") { power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data); CHECK_NOTHROW(serialize_data(const_dataset)); auto result = serialize_data(const_dataset); - std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; CHECK(result == json_string); } @@ -77,7 +77,6 @@ TEST_CASE("Test parse_vnf_file_wrapper") { TEST_CASE("Test convert_input_wrapper") { auto converter = PgmVnfConverter("", experimental_features_enabled); PgmVnfConverter* converterPtr = &converter; - std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; CHECK_NOTHROW(convert_input_wrapper(converterPtr)); auto result = convert_input_wrapper(converterPtr); CHECK(json_string == result); From e6476f88e1d990d0747af5dc2515c7fad1674a88 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 11:25:49 +0100 Subject: [PATCH 15/29] change deserialized data into deserialized_data Signed-off-by: Laurynas Jagutis --- tests/cpp_unit_tests/test_pgm_vnf_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index a8e47fc..44b3bca 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -60,7 +60,7 @@ TEST_CASE("Test the setter and getter of file_buffer") { CHECK(file_buff == value); } -// TEST_CASE("Test the setter and getter of deserialized data") { +// TEST_CASE("Test the setter and getter of deserialized_data") { // auto converter = PgmVnfConverter("", experimental_features_enabled); // power_grid_model::WritableDataset* dataset = nullptr; // converter.set_deserialized_data(dataset); From ccbf57471f598dae56381b57c4b6d5c9217b0fc5 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 11:36:27 +0100 Subject: [PATCH 16/29] change getter and setter names for deserialized data Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index ecfda78..c237daa 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -47,9 +47,9 @@ class PgmVnfConverter { std::string const& get_serialized_data() const; void set_file_buffer(std::string_view file_buffer); - void set_deserialized_data(power_grid_model::WritableDataset* deserialized_data); + void set_deserialized_dataset(power_grid_model::WritableDataset* deserialized_data); std::string_view get_file_buffer(); - power_grid_model::WritableDataset* get_deserialized_data(); + power_grid_model::WritableDataset* get_deserialized_dataset(); private: // Private attributes @@ -117,13 +117,15 @@ inline void PgmVnfConverter::convert_input() { inline void PgmVnfConverter::set_file_buffer(std::string_view file_buffer) { this->f_file_buffer = file_buffer; } -inline void PgmVnfConverter::set_deserialized_data(power_grid_model::WritableDataset* data) { +inline void PgmVnfConverter::set_deserialized_dataset(power_grid_model::WritableDataset* data) { this->deserialized_data = data; } inline std::string_view PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } -inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_data() { return this->deserialized_data; } +inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_dataset() { + return this->deserialized_data; +} inline std::string const& PgmVnfConverter::get_serialized_data() const { return this->serialized_data; } From 3e0ef03c0d3d93621ef2e53fdbcc700e6269e5be Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 11:43:31 +0100 Subject: [PATCH 17/29] initialize uninitialized member value Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index c237daa..e014a7e 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -70,7 +70,7 @@ class PgmVnfConverter { }; inline PgmVnfConverter::PgmVnfConverter(std::string_view buffer, ExperimentalFeatures experimental_feature_flag) - : f_file_buffer(buffer) { + : f_file_buffer(buffer), deserialized_data(nullptr) { if (experimental_feature_flag == experimental_features_disabled) { using power_grid_model::ExperimentalFeature; throw ExperimentalFeature{ From 9fcababf4061e844d28d60499a59804110dad0de Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 12:00:30 +0100 Subject: [PATCH 18/29] change default init Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index e014a7e..9bc2a2d 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -54,8 +54,8 @@ class PgmVnfConverter { private: // Private attributes std::string_view f_file_buffer; - power_grid_model::WritableDataset* - deserialized_data; // this type because it is generated by a deserializer type structure + power_grid_model::WritableDataset* deserialized_data{ + nullptr}; // this type because it is generated by a deserializer type structure std::string serialized_data; // Private member functions @@ -70,7 +70,7 @@ class PgmVnfConverter { }; inline PgmVnfConverter::PgmVnfConverter(std::string_view buffer, ExperimentalFeatures experimental_feature_flag) - : f_file_buffer(buffer), deserialized_data(nullptr) { + : f_file_buffer(buffer) { if (experimental_feature_flag == experimental_features_disabled) { using power_grid_model::ExperimentalFeature; throw ExperimentalFeature{ From 0acbca98fcc5bcaea450d48edc99b529f3764cb8 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 12:07:57 +0100 Subject: [PATCH 19/29] add auto and const to experimental features Signed-off-by: Laurynas Jagutis --- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 98b43ea..6bd2d59 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -24,7 +24,7 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - ExperimentalFeatures experimental_feature = + auto const experimental_feature = static_cast(static_cast(experimental_features)); auto* converter = new PGM_IO_VnfConverter(file_buffer, experimental_feature); parse_vnf_file_wrapper(converter); From 6ca0706d1dbe902988bed779e350ea831fb8901b Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 12:14:02 +0100 Subject: [PATCH 20/29] add const to get_file_buffer Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 9bc2a2d..a0c131f 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -48,7 +48,7 @@ class PgmVnfConverter { std::string const& get_serialized_data() const; void set_file_buffer(std::string_view file_buffer); void set_deserialized_dataset(power_grid_model::WritableDataset* deserialized_data); - std::string_view get_file_buffer(); + std::string_view const get_file_buffer(); power_grid_model::WritableDataset* get_deserialized_dataset(); private: @@ -121,7 +121,7 @@ inline void PgmVnfConverter::set_deserialized_dataset(power_grid_model::Writable this->deserialized_data = data; } -inline std::string_view PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } +inline std::string_view const PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_dataset() { return this->deserialized_data; From 37cfde4e939c051f5e4364cb8f5f12c144bc7474 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 12:23:57 +0100 Subject: [PATCH 21/29] add const to get_file_buffer1 Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index a0c131f..32b5df4 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -48,7 +48,7 @@ class PgmVnfConverter { std::string const& get_serialized_data() const; void set_file_buffer(std::string_view file_buffer); void set_deserialized_dataset(power_grid_model::WritableDataset* deserialized_data); - std::string_view const get_file_buffer(); + std::string_view get_file_buffer const(); power_grid_model::WritableDataset* get_deserialized_dataset(); private: @@ -121,7 +121,7 @@ inline void PgmVnfConverter::set_deserialized_dataset(power_grid_model::Writable this->deserialized_data = data; } -inline std::string_view const PgmVnfConverter::get_file_buffer() { return this->f_file_buffer; } +inline std::string_view PgmVnfConverter::get_file_buffer() const { return this->f_file_buffer; } inline power_grid_model::WritableDataset* PgmVnfConverter::get_deserialized_dataset() { return this->deserialized_data; From 26a4249d5cf68a5b425caa78a491dafc892abcd1 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 12:36:58 +0100 Subject: [PATCH 22/29] const Signed-off-by: Laurynas Jagutis --- .../vnf_converter/vnf_pgm_converter.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp index 32b5df4..ce51ec2 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native/include/power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp @@ -48,7 +48,7 @@ class PgmVnfConverter { std::string const& get_serialized_data() const; void set_file_buffer(std::string_view file_buffer); void set_deserialized_dataset(power_grid_model::WritableDataset* deserialized_data); - std::string_view get_file_buffer const(); + std::string_view get_file_buffer() const; power_grid_model::WritableDataset* get_deserialized_dataset(); private: From 93392087be826d3642d96e9e077c1719d4f9826f Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Mon, 28 Oct 2024 15:06:59 +0100 Subject: [PATCH 23/29] setup a full test for getter and setter of deserialied_data Signed-off-by: Laurynas Jagutis --- .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index 44b3bca..8c954f5 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -5,6 +5,10 @@ #include #include +#include +#include +#include + #include #include @@ -60,13 +64,17 @@ TEST_CASE("Test the setter and getter of file_buffer") { CHECK(file_buff == value); } -// TEST_CASE("Test the setter and getter of deserialized_data") { -// auto converter = PgmVnfConverter("", experimental_features_enabled); -// power_grid_model::WritableDataset* dataset = nullptr; -// converter.set_deserialized_data(dataset); -// auto const_data = converter.get_deserialized_data(); -// CHECK(dataset == const_data); -// } +TEST_CASE("Test the setter and getter of deserialized_data") { + constexpr auto const& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; + + std::string_view const dataset_name = "input"; + power_grid_model::WritableDataset writable_dataset{false, 1, dataset_name, meta_data}; + + auto converter = PgmVnfConverter("", experimental_features_enabled); + converter.set_deserialized_dataset(&writable_dataset); + auto writable_dataset_after_getter = converter.get_deserialized_dataset(); + CHECK(&writable_dataset == writable_dataset_after_getter); +} TEST_CASE("Test parse_vnf_file_wrapper") { auto converter = PgmVnfConverter("", experimental_features_enabled); From 3d8b19bcd20d2a668878e0c2ba8248916019fd8c Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 09:04:29 +0100 Subject: [PATCH 24/29] shorten test names Signed-off-by: Laurynas Jagutis --- .../cpp_unit_tests/test_pgm_vnf_converter.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp index 8c954f5..245bf50 100644 --- a/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp +++ b/tests/cpp_unit_tests/test_pgm_vnf_converter.cpp @@ -19,35 +19,35 @@ using enum ExperimentalFeatures; std::string_view json_string = R"({"version":"1.0","type":"input","is_batch":false,"attributes":{},"data":{}})"; TEST_CASE("Test converter constructor") { - SUBCASE("Without experimental features enabled") { + SUBCASE("Without experimental features") { CHECK_THROWS_AS(PgmVnfConverter("", experimental_features_disabled), power_grid_model::ExperimentalFeature); } - SUBCASE("With experimental features enabled") { CHECK_NOTHROW(PgmVnfConverter("", experimental_features_enabled)); } + SUBCASE("With experimental features") { CHECK_NOTHROW(PgmVnfConverter("", experimental_features_enabled)); } } -TEST_CASE("Test if the parse_vnf_file function can be called") { +TEST_CASE("Test parse_vnf_file is callable") { auto converter = PgmVnfConverter("", experimental_features_enabled); CHECK_NOTHROW(converter.parse_vnf_file()); } -TEST_CASE("Test the convert_input function") { +TEST_CASE("Test convert_input") { auto converter = PgmVnfConverter("", experimental_features_enabled); - SUBCASE("Test if the convert_input function can be called") { CHECK_NOTHROW(converter.convert_input()); } - SUBCASE("Test the return of convert_input") { + SUBCASE("Test convert_input is callable") { CHECK_NOTHROW(converter.convert_input()); } + SUBCASE("Test convert_input") { converter.convert_input(); auto json_result = converter.get_serialized_data(); CHECK(json_string == json_result); } } -TEST_CASE("Test if the create_const_dataset_from_container function can be called") { +TEST_CASE("Test create_const_dataset_from_container is callable") { power_grid_model::Container container; constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; CHECK_NOTHROW(create_const_dataset_from_container(container, meta_data)); } -TEST_CASE("Test the serialize_data function") { +TEST_CASE("Test serialize_data") { power_grid_model::Container container; constexpr const auto& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; power_grid_model::ConstDataset const const_dataset = create_const_dataset_from_container(container, meta_data); @@ -56,7 +56,7 @@ TEST_CASE("Test the serialize_data function") { CHECK(result == json_string); } -TEST_CASE("Test the setter and getter of file_buffer") { +TEST_CASE("Test setter/getter of file_buffer") { auto converter = PgmVnfConverter("", experimental_features_enabled); std::string_view value = "123"; converter.set_file_buffer(value); @@ -64,7 +64,7 @@ TEST_CASE("Test the setter and getter of file_buffer") { CHECK(file_buff == value); } -TEST_CASE("Test the setter and getter of deserialized_data") { +TEST_CASE("Test setter/getter of deserialized_data") { constexpr auto const& meta_data = power_grid_model::meta_data::meta_data_gen::meta_data; std::string_view const dataset_name = "input"; From a80e95c813f3608937d00e738f3ea7974bf9522a Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 09:09:00 +0100 Subject: [PATCH 25/29] remove unnecessary casting Signed-off-by: Laurynas Jagutis --- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 6bd2d59..f10b2a7 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -24,8 +24,7 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - auto const experimental_feature = - static_cast(static_cast(experimental_features)); + auto const experimental_feature = static_cast(experimental_features); auto* converter = new PGM_IO_VnfConverter(file_buffer, experimental_feature); parse_vnf_file_wrapper(converter); return converter; From cd10d1b83f59f0749eb632d1ca6c9fe687b054bb Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 09:40:57 +0100 Subject: [PATCH 26/29] add switch/case Signed-off-by: Laurynas Jagutis --- .../src/vnf_pgm_converter.cpp | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index f10b2a7..9d0e8bf 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -12,6 +12,7 @@ #include #include +#include using namespace power_grid_model_io_native; @@ -24,7 +25,17 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - auto const experimental_feature = static_cast(experimental_features); + ExperimentalFeatures experimental_feature; + switch (experimental_features) { + case PGM_IO_experimental_features_disabled: + experimental_feature = ExperimentalFeatures::experimental_features_disabled; + break; + case PGM_IO_experimental_features_enabled: + experimental_feature = ExperimentalFeatures::experimental_features_enabled; + break; + default: + throw power_grid_model::MissingCaseForEnumError{"PGM_IO_create_vnf_converter", experimental_features}; + } auto* converter = new PGM_IO_VnfConverter(file_buffer, experimental_feature); parse_vnf_file_wrapper(converter); return converter; From 226aac911c0adcfc8ef0d9edb0d61f2fab2b3175 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 09:49:56 +0100 Subject: [PATCH 27/29] initialize experimental feature fully because of clang tidy Signed-off-by: Laurynas Jagutis --- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index 9d0e8bf..d62c4a2 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -25,7 +25,7 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - ExperimentalFeatures experimental_feature; + auto experimental_feature = ExperimentalFeatures::experimental_features_disabled; switch (experimental_features) { case PGM_IO_experimental_features_disabled: experimental_feature = ExperimentalFeatures::experimental_features_disabled; From d7c94b184e1c6f9f4240dd4359c0425b4a666171 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 10:03:56 +0100 Subject: [PATCH 28/29] use using enum Signed-off-by: Laurynas Jagutis --- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index d62c4a2..c938d15 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -25,13 +25,14 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - auto experimental_feature = ExperimentalFeatures::experimental_features_disabled; + using enum ExperimentalFeatures; + auto experimental_feature = experimental_features_disabled; switch (experimental_features) { case PGM_IO_experimental_features_disabled: - experimental_feature = ExperimentalFeatures::experimental_features_disabled; + experimental_feature = experimental_features_disabled; break; case PGM_IO_experimental_features_enabled: - experimental_feature = ExperimentalFeatures::experimental_features_enabled; + experimental_feature = experimental_features_enabled; break; default: throw power_grid_model::MissingCaseForEnumError{"PGM_IO_create_vnf_converter", experimental_features}; From 1db9a9272684ae69eb2a5979e0a4ee7509c3a3c7 Mon Sep 17 00:00:00 2001 From: Laurynas Jagutis Date: Wed, 30 Oct 2024 10:20:06 +0100 Subject: [PATCH 29/29] change namespace usage Signed-off-by: Laurynas Jagutis --- .../power_grid_model_io_native_c/src/vnf_pgm_converter.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp index c938d15..b8065ed 100644 --- a/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp +++ b/power_grid_model_io_native_c/power_grid_model_io_native_c/src/vnf_pgm_converter.cpp @@ -14,9 +14,9 @@ #include #include -using namespace power_grid_model_io_native; +namespace pgm_io = power_grid_model_io_native; -struct PGM_IO_VnfConverter : public PgmVnfConverter { +struct PGM_IO_VnfConverter : public pgm_io::PgmVnfConverter { using PgmVnfConverter::PgmVnfConverter; }; @@ -25,7 +25,7 @@ PGM_IO_VnfConverter* PGM_IO_create_vnf_converter(PGM_IO_Handle* handle, char con return call_with_catch( handle, [file_buffer, experimental_features] { - using enum ExperimentalFeatures; + using enum pgm_io::ExperimentalFeatures; auto experimental_feature = experimental_features_disabled; switch (experimental_features) { case PGM_IO_experimental_features_disabled: