Skip to content

Commit

Permalink
add enum, remove writable dataset from c api, adjust tests accordingly
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 28, 2024
1 parent 81207d4 commit d5f0b7d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 33 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-FileCopyrightText: Contributors to the Power Grid Model project <[email protected]>
//
// 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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define POWER_GRID_MODEL_IO_NATIVE_C_VNF_PGM_CONVERTER_HPP

#include <power_grid_model_io_native/common/common.hpp>
#include <power_grid_model_io_native/common/enum.hpp>

#include <power_grid_model/auxiliary/dataset.hpp>
#include <power_grid_model/auxiliary/meta_data_gen.hpp>
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#define PGM_IO_DLL_EXPORTS

#include <power_grid_model_io_native/common/enum.hpp>
#include <power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp>

#include "handle.hpp"
Expand All @@ -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<ExperimentalFeatures>(static_cast<IntS>(experimental_features));
auto* converter = new PGM_IO_VnfConverter(file_buffer, experimental_feature);
parse_vnf_file_wrapper(converter);
return converter;
},
Expand Down
13 changes: 8 additions & 5 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,8 @@
//
// 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,9 +14,11 @@
#include <doctest/doctest.h>

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();
Expand All @@ -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);
Expand All @@ -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);

Expand Down
31 changes: 17 additions & 14 deletions tests/cpp_unit_tests/test_pgm_vnf_converter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,31 @@
//
// SPDX-License-Identifier: MPL-2.0

#include <power_grid_model_io_native/common/enum.hpp>
#include <power_grid_model_io_native/vnf_converter/vnf_pgm_converter.hpp>

#include <cstring>
#include <doctest/doctest.h>

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();
Expand All @@ -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));
Expand Down

0 comments on commit d5f0b7d

Please sign in to comment.