From c69b5325fee755c6d07b121695fa854d48acb8fc Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 08:09:20 +0200 Subject: [PATCH 01/20] disable ci except macos Signed-off-by: Martijn Govers --- .github/workflows/clang-tidy.yml | 1 + .github/workflows/main.yml | 6 ++++++ .github/workflows/sonar.yml | 3 ++- 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index ce3506df2..0901bca24 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -44,6 +44,7 @@ concurrency: jobs: clang-tidy: + if: false runs-on: ubuntu-latest strategy: matrix: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8eb508230..b574ca0d9 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -39,6 +39,7 @@ concurrency: jobs: acquire-python-version-build-sdist: + if: false name: Build sdist and set version runs-on: ubuntu-latest steps: @@ -69,6 +70,7 @@ jobs: path: ./wheelhouse/*.tar.gz build-cpp-test-linux: + if: false runs-on: ubuntu-latest strategy: matrix: @@ -103,6 +105,7 @@ jobs: run: ./build.sh -p ${{ env.PRESET }} -e -i -t build-cpp-test-windows: + if: false runs-on: windows-latest strategy: matrix: @@ -180,6 +183,7 @@ jobs: run: ./build.sh -p ${{ env.PRESET }} -e -i -t build-and-test-python: + if: false strategy: matrix: platform: [ linux-x64, linux-aarch64, macos, windows ] @@ -233,6 +237,7 @@ jobs: path: ./wheelhouse/*.whl build-and-test-conda: + if: false name: Build and test in Conda runs-on: ${{ matrix.os }}-latest @@ -265,6 +270,7 @@ jobs: run: pytest publish-wheels: + if: false needs: [build-cpp-test-linux, build-cpp-test-windows, build-cpp-test-macos, build-and-test-python, build-and-test-conda] # always run publish job but fail at the first step if previous jobs have been failed if: always() diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 146fd86df..1db025c43 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -20,7 +20,8 @@ concurrency: jobs: sonar-cloud: - if: (github.event_name == 'push') || (!startsWith(github.head_ref, 'release')) + if: false + # if: (github.event_name == 'push') || (!startsWith(github.head_ref, 'release')) name: SonarCloud runs-on: ubuntu-latest env: From c84fe4e4f74813d97f240387b4a1cee5a82fd2ec Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 08:14:30 +0200 Subject: [PATCH 02/20] fix disabling CI Signed-off-by: Martijn Govers --- .github/workflows/check-code-quality.yml | 1 + .github/workflows/main.yml | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/check-code-quality.yml b/.github/workflows/check-code-quality.yml index af2918650..1e92b281b 100644 --- a/.github/workflows/check-code-quality.yml +++ b/.github/workflows/check-code-quality.yml @@ -28,6 +28,7 @@ concurrency: jobs: check-code-quality: + if: false runs-on: ubuntu-latest diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b574ca0d9..cd49f0d81 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -270,7 +270,6 @@ jobs: run: pytest publish-wheels: - if: false needs: [build-cpp-test-linux, build-cpp-test-windows, build-cpp-test-macos, build-and-test-python, build-and-test-conda] # always run publish job but fail at the first step if previous jobs have been failed if: always() From c45c65f02eb231df6526aec528d4fe61015e9e21 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 10:33:59 +0200 Subject: [PATCH 03/20] add basic API test Signed-off-by: Martijn Govers --- build.sh | 1 + tests/native_api_tests/CMakeLists.txt | 1 + tests/native_api_tests/test_api_buffer.cpp | 103 +++++++++++++++++++++ 3 files changed, 105 insertions(+) create mode 100644 tests/native_api_tests/test_api_buffer.cpp diff --git a/build.sh b/build.sh index 723cf0005..7e51b1fdc 100755 --- a/build.sh +++ b/build.sh @@ -47,6 +47,7 @@ rm -rf ${BUILD_DIR}/ cmake --preset ${PRESET} # build +cmake --build --preset ${PRESET} --verbose -j1 --target power_grid_model_c_api_tests cmake --build --preset ${PRESET} --verbose -j1 # test diff --git a/tests/native_api_tests/CMakeLists.txt b/tests/native_api_tests/CMakeLists.txt index b6e3b97ee..0d5c2512c 100644 --- a/tests/native_api_tests/CMakeLists.txt +++ b/tests/native_api_tests/CMakeLists.txt @@ -5,6 +5,7 @@ set(PROJECT_SOURCES "test_entry_point.cpp" "test_api_meta_data.cpp" + "test_api_buffer.cpp" "test_api_model.cpp" "test_api_serialization.cpp" ) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp new file mode 100644 index 000000000..7524a7b7c --- /dev/null +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -0,0 +1,103 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#include "power_grid_model_cpp.hpp" + +#include + +#include + +#include +#include +#include +#include +#include +#include +#include + +namespace power_grid_model_cpp { +namespace { +constexpr double nan = std::numeric_limits::quiet_NaN(); +constexpr int8_t na_IntS = std::numeric_limits::min(); +constexpr ID na_IntID = std::numeric_limits::min(); + +template T> constexpr T nan_value() { return nan; } +template > T> constexpr T nan_value() { return {nan, nan, nan}; } +template T> constexpr T nan_value() { return na_IntID; } +template T> constexpr T nan_value() { return na_IntS; } + +template U> constexpr T as_type(U const& value) { return static_cast(value); } +template constexpr T as_type(T const& value) { return value; } +template > T> constexpr T as_type(double const& value) { + return {value, value, value}; +} + +inline bool is_nan(double value) { return std::isnan(value); } +inline bool is_nan(std::array value) { return is_nan(value[0]) && is_nan(value[1]) && is_nan(value[2]); } +inline bool is_nan(ID x) { return x == std::numeric_limits::min(); } +inline bool is_nan(Idx x) { return x == std::numeric_limits::min(); } +inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } + +void check_buffer(MetaComponent const* component, MetaAttribute const* attribute) { + auto check_attribute = [component, attribute]() { + for (Idx size = 0; size < 4; ++size) { + Buffer buffer{component, size}; + T ref_value{}; + std::vector ref_buffer(size); + std::vector source_buffer(size); + buffer.set_nan(); + + buffer.get_value(attribute, &ref_value, 0); + CHECK(is_nan(ref_value)); + + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + CHECK(is_nan(ref_buffer[idx])); + } + + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); + } + buffer.set_value(attribute, source_buffer.data(), sizeof(T)); + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + CHECK(ref_buffer[idx] == as_type(idx)); + } + } + }; + switch (MetaData::attribute_ctype(attribute)) { + case PGM_int32: + return check_attribute.template operator()(); + case PGM_int8: + return check_attribute.template operator()(); + case PGM_double: + return check_attribute.template operator()(); + case PGM_double3: + return check_attribute.template operator()>(); + default: + FAIL("Invalid ctype"); + } +} +} // namespace + +TEST_CASE("API Buffer") { + for (Idx dataset_idx = 0; dataset_idx < MetaData::n_datasets(); ++dataset_idx) { + CAPTURE(dataset_idx); + MetaDataset const* dataset = MetaData::get_dataset_by_idx(dataset_idx); + CAPTURE(MetaData::dataset_name(dataset)); + for (Idx component_idx = 0; component_idx < MetaData::n_components(dataset); ++component_idx) { + CAPTURE(component_idx); + MetaComponent const* component = MetaData::get_component_by_idx(dataset, component_idx); + CAPTURE(MetaData::component_name(component)); + + for (Idx attribute_idx = 0; attribute_idx < MetaData::n_attributes(component); ++attribute_idx) { + CAPTURE(attribute_idx); + MetaAttribute const* attribute = MetaData::get_attribute_by_idx(component, attribute_idx); + CAPTURE(MetaData::attribute_name(attribute)); + } + } + } +} + +} // namespace power_grid_model_cpp From 3ac8846af0e109cd736459d3f75c110bf00731c4 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 10:35:24 +0200 Subject: [PATCH 04/20] fix typo Signed-off-by: Martijn Govers --- build.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 7e51b1fdc..94fcc45fe 100755 --- a/build.sh +++ b/build.sh @@ -47,7 +47,7 @@ rm -rf ${BUILD_DIR}/ cmake --preset ${PRESET} # build -cmake --build --preset ${PRESET} --verbose -j1 --target power_grid_model_c_api_tests +cmake --build --preset ${PRESET} --verbose -j1 --target power_grid_model_api_tests cmake --build --preset ${PRESET} --verbose -j1 # test From a6260e012ac034ebdc71f9b29b11e987e105ecb0 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 10:40:19 +0200 Subject: [PATCH 05/20] fix Signed-off-by: Martijn Govers --- build.sh | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/build.sh b/build.sh index 94fcc45fe..865c69ff9 100755 --- a/build.sh +++ b/build.sh @@ -71,8 +71,9 @@ if [[ "${COVERAGE}" ]]; then -d ${BUILD_DIR}/tests/cpp_unit_tests/CMakeFiles/power_grid_model_unit_tests.dir \ -d ${BUILD_DIR}/tests/cpp_integration_tests/CMakeFiles/power_grid_model_integration_tests.dir \ -d ${BUILD_DIR}/tests/cpp_validation_tests/CMakeFiles/power_grid_model_validation_tests.dir \ - -d ${BUILD_DIR}/tests/c_api_tests/CMakeFiles/power_grid_model_c_api_tests.dir \ + -d ${BUILD_DIR}/tests/native_api_tests/CMakeFiles/power_grid_model_api_tests.dir \ -d ${BUILD_DIR}/power_grid_model_c/power_grid_model_c/CMakeFiles/power_grid_model_c.dir \ + -d ${BUILD_DIR}/power_grid_model_c_example/CMakeFiles/power_grid_model_c_example.dir \ -b . \ --no-external \ --output-file cpp_coverage.info \ From 6ef6d05d3452e2b02a9912e09960e921c0903bac Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 10:49:57 +0200 Subject: [PATCH 06/20] check buffer Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 7524a7b7c..5247cd3c2 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -36,20 +36,24 @@ template > T> constexpr T as_type(double cons inline bool is_nan(double value) { return std::isnan(value); } inline bool is_nan(std::array value) { return is_nan(value[0]) && is_nan(value[1]) && is_nan(value[2]); } inline bool is_nan(ID x) { return x == std::numeric_limits::min(); } -inline bool is_nan(Idx x) { return x == std::numeric_limits::min(); } inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } void check_buffer(MetaComponent const* component, MetaAttribute const* attribute) { auto check_attribute = [component, attribute]() { for (Idx size = 0; size < 4; ++size) { - Buffer buffer{component, size}; T ref_value{}; std::vector ref_buffer(size); std::vector source_buffer(size); + + Buffer buffer{component, size}; buffer.set_nan(); buffer.get_value(attribute, &ref_value, 0); - CHECK(is_nan(ref_value)); + if (size > 0) { + CHECK(is_nan(ref_value)); + } else { + CHECK(ref_value == T{}); + } buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); for (Idx idx = 0; idx < size; ++idx) { @@ -95,6 +99,8 @@ TEST_CASE("API Buffer") { CAPTURE(attribute_idx); MetaAttribute const* attribute = MetaData::get_attribute_by_idx(component, attribute_idx); CAPTURE(MetaData::attribute_name(attribute)); + + check_buffer(component, attribute); } } } From ffc9de38c2bb356ba37d25b55fac3bafcea1e910 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 11:09:42 +0200 Subject: [PATCH 07/20] more alignment tests Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 72 ++++++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 5247cd3c2..429c5255f 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -41,32 +41,80 @@ inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } void check_buffer(MetaComponent const* component, MetaAttribute const* attribute) { auto check_attribute = [component, attribute]() { for (Idx size = 0; size < 4; ++size) { - T ref_value{}; - std::vector ref_buffer(size); std::vector source_buffer(size); + std::vector ref_buffer(size); Buffer buffer{component, size}; - buffer.set_nan(); - - buffer.get_value(attribute, &ref_value, 0); - if (size > 0) { - CHECK(is_nan(ref_value)); - } else { - CHECK(ref_value == T{}); - } + // array get value + buffer.set_nan(); buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); for (Idx idx = 0; idx < size; ++idx) { - CHECK(is_nan(ref_buffer[idx])); + REQUIRE(is_nan(ref_buffer[idx])); } + // array set value + buffer.set_nan(); for (Idx idx = 0; idx < size; ++idx) { source_buffer[idx] = as_type(idx); } buffer.set_value(attribute, source_buffer.data(), sizeof(T)); buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); for (Idx idx = 0; idx < size; ++idx) { - CHECK(ref_buffer[idx] == as_type(idx)); + REQUIRE(ref_buffer[idx] == as_type(idx)); + } + // array set value with fixed size + for (Idx sub_size = 0; sub_size < size; ++sub_size) { + for (Idx offset = 0; offset < size - sub_size; ++offset) { + std::ranges::fill(ref_buffer, T{}); + // get value + buffer.set_nan(); + buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(is_nan(ref_buffer[idx])); + } else { + REQUIRE(ref_buffer[idx] == T{}); + } + } + + // set value + buffer.set_nan(); + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); + } + buffer.set_value(attribute, source_buffer.data(), offset, sub_size, sizeof(T)); + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(ref_buffer[idx] == as_type(idx)); + } else { + REQUIRE(is_nan(ref_buffer[idx])); + } + } + } + } + + // single access + T source_value{}; + T ref_value{}; + buffer.set_nan(); + for (Idx idx = 0; idx < size; ++idx) { + // single get value + buffer.get_value(attribute, &ref_value, idx, 0); + if (size > 0) { + REQUIRE(is_nan(ref_value)); + } else { + REQUIRE(ref_value == T{}); + } + // single set value + buffer.set_value(attribute, &source_value, idx, 0); + buffer.get_value(attribute, &ref_value, idx, 0); + if (size > 0) { + REQUIRE(ref_value == source_value); + } else { + REQUIRE(ref_value == T{}); + } } } }; From 7a64b6d046b55a9935644afb6f6f3f0131446d09 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 11:12:16 +0200 Subject: [PATCH 08/20] more tests Signed-off-by: Martijn Govers --- build.sh | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.sh b/build.sh index 865c69ff9..ad1f79d6a 100755 --- a/build.sh +++ b/build.sh @@ -48,6 +48,9 @@ cmake --preset ${PRESET} # build cmake --build --preset ${PRESET} --verbose -j1 --target power_grid_model_api_tests +ctest --preset ${PRESET} -R "API Buffer" --output-on-failure + + cmake --build --preset ${PRESET} --verbose -j1 # test From 0ef6e009bcbd9f63950824d3e38be070c23e523c Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 11:28:47 +0200 Subject: [PATCH 09/20] fix coverage Signed-off-by: Martijn Govers --- build.sh | 1 - 1 file changed, 1 deletion(-) diff --git a/build.sh b/build.sh index ad1f79d6a..68ae69f26 100755 --- a/build.sh +++ b/build.sh @@ -76,7 +76,6 @@ if [[ "${COVERAGE}" ]]; then -d ${BUILD_DIR}/tests/cpp_validation_tests/CMakeFiles/power_grid_model_validation_tests.dir \ -d ${BUILD_DIR}/tests/native_api_tests/CMakeFiles/power_grid_model_api_tests.dir \ -d ${BUILD_DIR}/power_grid_model_c/power_grid_model_c/CMakeFiles/power_grid_model_c.dir \ - -d ${BUILD_DIR}/power_grid_model_c_example/CMakeFiles/power_grid_model_c_example.dir \ -b . \ --no-external \ --output-file cpp_coverage.info \ From 73a8ec12e288f24d77ed2be4c809a3376a1e0baa Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 11:32:59 +0200 Subject: [PATCH 10/20] checkout pull request head instead of merge commit Signed-off-by: Martijn Govers --- .github/workflows/main.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index cd49f0d81..b9d6cc14b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -169,6 +169,8 @@ jobs: steps: - uses: actions/checkout@v4 + with: + ref: ${{ github.event.pull_request.head.sha }} - name: Set up XCode uses: maxim-lobanov/setup-xcode@v1 From 309f3016ebd1603aef653950f7b3810771f6451c Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 12:24:29 +0200 Subject: [PATCH 11/20] restore CI checks Signed-off-by: Martijn Govers --- build.sh | 4 -- .../power_grid_model_cpp/meta_data.hpp | 4 +- .../include/power_grid_model_cpp/utils.hpp | 16 +++++ .../cpp_validation_tests/test_validation.cpp | 2 +- tests/native_api_tests/CMakeLists.txt | 1 + tests/native_api_tests/test_api_buffer.cpp | 68 +++++++++---------- tests/native_api_tests/test_api_utils.cpp | 28 ++++++++ 7 files changed, 81 insertions(+), 42 deletions(-) create mode 100644 tests/native_api_tests/test_api_utils.cpp diff --git a/build.sh b/build.sh index 68ae69f26..d87355f57 100755 --- a/build.sh +++ b/build.sh @@ -47,10 +47,6 @@ rm -rf ${BUILD_DIR}/ cmake --preset ${PRESET} # build -cmake --build --preset ${PRESET} --verbose -j1 --target power_grid_model_api_tests -ctest --preset ${PRESET} -R "API Buffer" --output-on-failure - - cmake --build --preset ${PRESET} --verbose -j1 # test diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/meta_data.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/meta_data.hpp index 26df51a83..c68ef800f 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/meta_data.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/meta_data.hpp @@ -69,7 +69,9 @@ class MetaData { return std::string{PGM_meta_attribute_name(nullptr, attribute)}; } - static Idx attribute_ctype(MetaAttribute const* attribute) { return PGM_meta_attribute_ctype(nullptr, attribute); } + static PGM_CType attribute_ctype(MetaAttribute const* attribute) { + return static_cast(PGM_meta_attribute_ctype(nullptr, attribute)); + } static size_t attribute_offset(MetaAttribute const* attribute) { return PGM_meta_attribute_offset(nullptr, attribute); diff --git a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp index 78e981474..1f943f688 100644 --- a/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp +++ b/power_grid_model_c/power_grid_model_cpp/include/power_grid_model_cpp/utils.hpp @@ -9,6 +9,7 @@ #include "basics.hpp" #include "handle.hpp" +#include "meta_data.hpp" #include #include @@ -26,6 +27,15 @@ inline bool is_nan(std::array, 3> const& array) { return is_nan(array[0]) || is_nan(array[1]) || is_nan(array[2]); } +constexpr double nan = std::numeric_limits::quiet_NaN(); +constexpr int8_t na_IntS = std::numeric_limits::min(); +constexpr ID na_IntID = std::numeric_limits::min(); + +template T> constexpr T nan_value() { return nan; } +template > T> constexpr T nan_value() { return {nan, nan, nan}; } +template T> constexpr T nan_value() { return na_IntID; } +template T> constexpr T nan_value() { return na_IntS; } + class UnsupportedPGM_CType : public PowerGridError { public: UnsupportedPGM_CType() @@ -51,6 +61,12 @@ decltype(auto) pgm_type_func_selector(enum PGM_CType type, Functor&& f, Args&&.. } } +template +decltype(auto) pgm_type_func_selector(MetaAttribute const* attribute, Functor&& f, Args&&... args) { + return pgm_type_func_selector(MetaData::attribute_ctype(attribute), std::forward(f), + std::forward(args)...); +} + } // namespace power_grid_model_cpp #endif // POWER_GRID_MODEL_CPP_UTILS_HPP diff --git a/tests/cpp_validation_tests/test_validation.cpp b/tests/cpp_validation_tests/test_validation.cpp index 71035fec9..adab39f93 100644 --- a/tests/cpp_validation_tests/test_validation.cpp +++ b/tests/cpp_validation_tests/test_validation.cpp @@ -323,7 +323,7 @@ void assert_result(OwningDataset const& owning_result, OwningDataset const& owni attribute_name, dynamic_atol, rtol); }; - pgm_type_func_selector(static_cast(attribute_type), callable_wrapper); + pgm_type_func_selector(attribute_type, callable_wrapper); } } } diff --git a/tests/native_api_tests/CMakeLists.txt b/tests/native_api_tests/CMakeLists.txt index 0d5c2512c..24a6547c6 100644 --- a/tests/native_api_tests/CMakeLists.txt +++ b/tests/native_api_tests/CMakeLists.txt @@ -8,6 +8,7 @@ set(PROJECT_SOURCES "test_api_buffer.cpp" "test_api_model.cpp" "test_api_serialization.cpp" + "test_api_utils.cpp" ) add_executable(power_grid_model_api_tests ${PROJECT_SOURCES}) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 429c5255f..7d0e4c929 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -2,7 +2,9 @@ // // SPDX-License-Identifier: MPL-2.0 -#include "power_grid_model_cpp.hpp" +#include +#include +#include #include @@ -18,28 +20,14 @@ namespace power_grid_model_cpp { namespace { -constexpr double nan = std::numeric_limits::quiet_NaN(); -constexpr int8_t na_IntS = std::numeric_limits::min(); -constexpr ID na_IntID = std::numeric_limits::min(); - -template T> constexpr T nan_value() { return nan; } -template > T> constexpr T nan_value() { return {nan, nan, nan}; } -template T> constexpr T nan_value() { return na_IntID; } -template T> constexpr T nan_value() { return na_IntS; } - template U> constexpr T as_type(U const& value) { return static_cast(value); } template constexpr T as_type(T const& value) { return value; } template > T> constexpr T as_type(double const& value) { return {value, value, value}; } -inline bool is_nan(double value) { return std::isnan(value); } -inline bool is_nan(std::array value) { return is_nan(value[0]) && is_nan(value[1]) && is_nan(value[2]); } -inline bool is_nan(ID x) { return x == std::numeric_limits::min(); } -inline bool is_nan(int8_t x) { return x == std::numeric_limits::min(); } - -void check_buffer(MetaComponent const* component, MetaAttribute const* attribute) { - auto check_attribute = [component, attribute]() { +void check_array_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { + pgm_type_func_selector(attribute, [component, attribute]() { for (Idx size = 0; size < 4; ++size) { std::vector source_buffer(size); std::vector ref_buffer(size); @@ -63,10 +51,20 @@ void check_buffer(MetaComponent const* component, MetaAttribute const* attribute for (Idx idx = 0; idx < size; ++idx) { REQUIRE(ref_buffer[idx] == as_type(idx)); } - // array set value with fixed size + } + }); +} + +void check_sub_array_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { + pgm_type_func_selector(attribute, [component, attribute]() { + for (Idx size = 0; size < 4; ++size) { for (Idx sub_size = 0; sub_size < size; ++sub_size) { for (Idx offset = 0; offset < size - sub_size; ++offset) { - std::ranges::fill(ref_buffer, T{}); + std::vector source_buffer(size); + std::vector ref_buffer(size); + + Buffer buffer{component, size}; + // get value buffer.set_nan(); buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); @@ -94,20 +92,28 @@ void check_buffer(MetaComponent const* component, MetaAttribute const* attribute } } } + } + }); +} + +void check_single_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { + pgm_type_func_selector(attribute, [component, attribute]() { + for (Idx size = 0; size < 4; ++size) { + Buffer buffer{component, size}; - // single access T source_value{}; T ref_value{}; + buffer.set_nan(); for (Idx idx = 0; idx < size; ++idx) { - // single get value + // get value buffer.get_value(attribute, &ref_value, idx, 0); if (size > 0) { REQUIRE(is_nan(ref_value)); } else { REQUIRE(ref_value == T{}); } - // single set value + // set value buffer.set_value(attribute, &source_value, idx, 0); buffer.get_value(attribute, &ref_value, idx, 0); if (size > 0) { @@ -117,19 +123,7 @@ void check_buffer(MetaComponent const* component, MetaAttribute const* attribute } } } - }; - switch (MetaData::attribute_ctype(attribute)) { - case PGM_int32: - return check_attribute.template operator()(); - case PGM_int8: - return check_attribute.template operator()(); - case PGM_double: - return check_attribute.template operator()(); - case PGM_double3: - return check_attribute.template operator()>(); - default: - FAIL("Invalid ctype"); - } + }); } } // namespace @@ -148,7 +142,9 @@ TEST_CASE("API Buffer") { MetaAttribute const* attribute = MetaData::get_attribute_by_idx(component, attribute_idx); CAPTURE(MetaData::attribute_name(attribute)); - check_buffer(component, attribute); + check_array_buffer_access(component, attribute); + check_sub_array_buffer_access(component, attribute); + check_single_buffer_access(component, attribute); } } } diff --git a/tests/native_api_tests/test_api_utils.cpp b/tests/native_api_tests/test_api_utils.cpp new file mode 100644 index 000000000..2818b22b9 --- /dev/null +++ b/tests/native_api_tests/test_api_utils.cpp @@ -0,0 +1,28 @@ +// SPDX-FileCopyrightText: Contributors to the Power Grid Model project +// +// SPDX-License-Identifier: MPL-2.0 + +#include + +#include + +namespace power_grid_model_cpp { +TEST_CASE("API Utils") { + SUBCASE("NaN values and checks") { + CHECK_FALSE(is_nan(ID{})); + CHECK_FALSE(is_nan(IntS{})); + CHECK_FALSE(is_nan(std::complex{})); + CHECK_FALSE(is_nan(std::array{})); + CHECK_FALSE(is_nan(std::array, 3>{})); + + CHECK(is_nan(nan)); + CHECK(is_nan(na_IntS)); + CHECK(is_nan(na_IntID)); + + CHECK(is_nan(nan_value())); + CHECK(is_nan(nan_value>())); + CHECK(is_nan(nan_value())); + CHECK(is_nan(nan_value())); + } +} +} // namespace power_grid_model_cpp From 8fe9d4d8dc64d1aa88c83c87d8a9a893693b3dc8 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 12:27:08 +0200 Subject: [PATCH 12/20] restore CI checks Signed-off-by: Martijn Govers --- .github/workflows/check-code-quality.yml | 1 - .github/workflows/clang-tidy.yml | 1 - .github/workflows/main.yml | 7 ------- .github/workflows/sonar.yml | 3 +-- power_grid_model_c/power_grid_model_c/src/buffer.cpp | 9 ++------- 5 files changed, 3 insertions(+), 18 deletions(-) diff --git a/.github/workflows/check-code-quality.yml b/.github/workflows/check-code-quality.yml index 1e92b281b..af2918650 100644 --- a/.github/workflows/check-code-quality.yml +++ b/.github/workflows/check-code-quality.yml @@ -28,7 +28,6 @@ concurrency: jobs: check-code-quality: - if: false runs-on: ubuntu-latest diff --git a/.github/workflows/clang-tidy.yml b/.github/workflows/clang-tidy.yml index 0901bca24..ce3506df2 100644 --- a/.github/workflows/clang-tidy.yml +++ b/.github/workflows/clang-tidy.yml @@ -44,7 +44,6 @@ concurrency: jobs: clang-tidy: - if: false runs-on: ubuntu-latest strategy: matrix: diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index b9d6cc14b..8eb508230 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -39,7 +39,6 @@ concurrency: jobs: acquire-python-version-build-sdist: - if: false name: Build sdist and set version runs-on: ubuntu-latest steps: @@ -70,7 +69,6 @@ jobs: path: ./wheelhouse/*.tar.gz build-cpp-test-linux: - if: false runs-on: ubuntu-latest strategy: matrix: @@ -105,7 +103,6 @@ jobs: run: ./build.sh -p ${{ env.PRESET }} -e -i -t build-cpp-test-windows: - if: false runs-on: windows-latest strategy: matrix: @@ -169,8 +166,6 @@ jobs: steps: - uses: actions/checkout@v4 - with: - ref: ${{ github.event.pull_request.head.sha }} - name: Set up XCode uses: maxim-lobanov/setup-xcode@v1 @@ -185,7 +180,6 @@ jobs: run: ./build.sh -p ${{ env.PRESET }} -e -i -t build-and-test-python: - if: false strategy: matrix: platform: [ linux-x64, linux-aarch64, macos, windows ] @@ -239,7 +233,6 @@ jobs: path: ./wheelhouse/*.whl build-and-test-conda: - if: false name: Build and test in Conda runs-on: ${{ matrix.os }}-latest diff --git a/.github/workflows/sonar.yml b/.github/workflows/sonar.yml index 1db025c43..146fd86df 100644 --- a/.github/workflows/sonar.yml +++ b/.github/workflows/sonar.yml @@ -20,8 +20,7 @@ concurrency: jobs: sonar-cloud: - if: false - # if: (github.event_name == 'push') || (!startsWith(github.head_ref, 'release')) + if: (github.event_name == 'push') || (!startsWith(github.head_ref, 'release')) name: SonarCloud runs-on: ubuntu-latest env: diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index d4d268674..aeb6374bc 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -20,15 +20,10 @@ using meta_data::RawDataPtr; // buffer control RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* component, PGM_Idx size) { - // alignment should be maximum of alignment of the component and alignment of void* - size_t const alignment = std::max(component->alignment, sizeof(void*)); - // total bytes should be multiple of alignment - size_t const requested_bytes = component->size * size; - size_t const rounded_bytes = ((requested_bytes + alignment - 1) / alignment) * alignment; #ifdef _WIN32 - return _aligned_malloc(rounded_bytes, alignment); + return _aligned_malloc(component->size * size, component->alignment); #else - return std::aligned_alloc(alignment, rounded_bytes); + return std::aligned_alloc(component->alignment, component->size * size); #endif } void PGM_destroy_buffer(RawDataPtr ptr) { From 47c2322f9cb8d8f1e8aff1cbe4295fae1166abfc Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 13:42:10 +0200 Subject: [PATCH 13/20] Revert "restore CI checks" This reverts commit 8fe9d4d8dc64d1aa88c83c87d8a9a893693b3dc8. Signed-off-by: Martijn Govers --- power_grid_model_c/power_grid_model_c/src/buffer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index aeb6374bc..d4d268674 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -20,10 +20,15 @@ using meta_data::RawDataPtr; // buffer control RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* component, PGM_Idx size) { + // alignment should be maximum of alignment of the component and alignment of void* + size_t const alignment = std::max(component->alignment, sizeof(void*)); + // total bytes should be multiple of alignment + size_t const requested_bytes = component->size * size; + size_t const rounded_bytes = ((requested_bytes + alignment - 1) / alignment) * alignment; #ifdef _WIN32 - return _aligned_malloc(component->size * size, component->alignment); + return _aligned_malloc(rounded_bytes, alignment); #else - return std::aligned_alloc(component->alignment, component->size * size); + return std::aligned_alloc(alignment, rounded_bytes); #endif } void PGM_destroy_buffer(RawDataPtr ptr) { From e8f9dd1bb1b93e4834308cc26766f6c0395b5f31 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 13:46:30 +0200 Subject: [PATCH 14/20] make ci happy Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 7d0e4c929..865de2109 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -20,8 +20,13 @@ namespace power_grid_model_cpp { namespace { -template U> constexpr T as_type(U const& value) { return static_cast(value); } -template constexpr T as_type(T const& value) { return value; } +template U> constexpr T as_type(U const& value) { + if constexpr (std::same_as) { + return value; + } else { + return static_cast(value); + } +} template > T> constexpr T as_type(double const& value) { return {value, value, value}; } From df44d2e864e12ac7f0238844971eb2eca76a9dd5 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 14:32:59 +0200 Subject: [PATCH 15/20] fix ci Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 865de2109..d116e7337 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -27,8 +27,9 @@ template U> constexpr T as_type(U const& val return static_cast(value); } } -template > T> constexpr T as_type(double const& value) { - return {value, value, value}; +template > T, std::convertible_to U> constexpr T as_type(U const& value) { + auto const d_value = as_type(value); + return {d_value, d_value, d_value}; } void check_array_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { From 36072de81a4ed194e742f02d03467b469ee47c97 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 15:04:22 +0200 Subject: [PATCH 16/20] set error on buffer allocation failure Signed-off-by: Martijn Govers --- .../power_grid_model_c/src/buffer.cpp | 19 ++++++++++++++++--- .../power_grid_model_c/src/handle.hpp | 6 ++++-- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index d4d268674..4fb5c1361 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -4,6 +4,7 @@ #define PGM_DLL_EXPORTS #include "forward_declarations.hpp" +#include "handle.hpp" #include "power_grid_model_c/buffer.h" @@ -16,10 +17,8 @@ using namespace power_grid_model; using meta_data::RawDataConstPtr; using meta_data::RawDataPtr; -} // namespace -// buffer control -RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* component, PGM_Idx size) { +RawDataPtr create_buffer(PGM_MetaComponent const* component, PGM_Idx size) { // alignment should be maximum of alignment of the component and alignment of void* size_t const alignment = std::max(component->alignment, sizeof(void*)); // total bytes should be multiple of alignment @@ -31,6 +30,20 @@ RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* return std::aligned_alloc(alignment, rounded_bytes); #endif } +} // namespace + +// buffer control +RawDataPtr PGM_create_buffer(PGM_Handle* handle, PGM_MetaComponent const* component, PGM_Idx size) { + return call_with_catch( + handle, + [component, size] { + if (RawDataPtr result = create_buffer(component, size); result != nullptr || size == 0) { + return result; + } + throw std::bad_alloc{}; + }, + PGM_regular_error); +} void PGM_destroy_buffer(RawDataPtr ptr) { #ifdef _WIN32 _aligned_free(ptr); diff --git a/power_grid_model_c/power_grid_model_c/src/handle.hpp b/power_grid_model_c/power_grid_model_c/src/handle.hpp index f630f9128..298ad763b 100644 --- a/power_grid_model_c/power_grid_model_c/src/handle.hpp +++ b/power_grid_model_c/power_grid_model_c/src/handle.hpp @@ -38,8 +38,10 @@ auto call_with_catch(PGM_Handle* handle, Functor func, PGM_Idx error_code, std:: try { return func(); } catch (Exception const& e) { - handle->err_code = error_code; - handle->err_msg = std::string(e.what()) + std::string(extra_msg); + if (handle) { + handle->err_code = error_code; + handle->err_msg = std::string(e.what()) + std::string(extra_msg); + } return static_cast(empty); } } From a7544517a85b32afcc8cd738887ad98391789ccc Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 15:15:24 +0200 Subject: [PATCH 17/20] roll-back error on bad alloc Signed-off-by: Martijn Govers --- .../power_grid_model_c/src/buffer.cpp | 19 +++---------------- .../power_grid_model_c/src/handle.hpp | 2 +- 2 files changed, 4 insertions(+), 17 deletions(-) diff --git a/power_grid_model_c/power_grid_model_c/src/buffer.cpp b/power_grid_model_c/power_grid_model_c/src/buffer.cpp index 4fb5c1361..d4d268674 100644 --- a/power_grid_model_c/power_grid_model_c/src/buffer.cpp +++ b/power_grid_model_c/power_grid_model_c/src/buffer.cpp @@ -4,7 +4,6 @@ #define PGM_DLL_EXPORTS #include "forward_declarations.hpp" -#include "handle.hpp" #include "power_grid_model_c/buffer.h" @@ -17,8 +16,10 @@ using namespace power_grid_model; using meta_data::RawDataConstPtr; using meta_data::RawDataPtr; +} // namespace -RawDataPtr create_buffer(PGM_MetaComponent const* component, PGM_Idx size) { +// buffer control +RawDataPtr PGM_create_buffer(PGM_Handle* /* handle */, PGM_MetaComponent const* component, PGM_Idx size) { // alignment should be maximum of alignment of the component and alignment of void* size_t const alignment = std::max(component->alignment, sizeof(void*)); // total bytes should be multiple of alignment @@ -30,20 +31,6 @@ RawDataPtr create_buffer(PGM_MetaComponent const* component, PGM_Idx size) { return std::aligned_alloc(alignment, rounded_bytes); #endif } -} // namespace - -// buffer control -RawDataPtr PGM_create_buffer(PGM_Handle* handle, PGM_MetaComponent const* component, PGM_Idx size) { - return call_with_catch( - handle, - [component, size] { - if (RawDataPtr result = create_buffer(component, size); result != nullptr || size == 0) { - return result; - } - throw std::bad_alloc{}; - }, - PGM_regular_error); -} void PGM_destroy_buffer(RawDataPtr ptr) { #ifdef _WIN32 _aligned_free(ptr); diff --git a/power_grid_model_c/power_grid_model_c/src/handle.hpp b/power_grid_model_c/power_grid_model_c/src/handle.hpp index 298ad763b..dc2d3b4ce 100644 --- a/power_grid_model_c/power_grid_model_c/src/handle.hpp +++ b/power_grid_model_c/power_grid_model_c/src/handle.hpp @@ -40,7 +40,7 @@ auto call_with_catch(PGM_Handle* handle, Functor func, PGM_Idx error_code, std:: } catch (Exception const& e) { if (handle) { handle->err_code = error_code; - handle->err_msg = std::string(e.what()) + std::string(extra_msg); + handle->err_msg = std::string{e.what()} + std::string{extra_msg}; } return static_cast(empty); } From 6a3665b4ebec9cc6693bc90cd0b982386f0f1792 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 15:27:29 +0200 Subject: [PATCH 18/20] sonar cloud Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 266 +++++++++++++-------- 1 file changed, 169 insertions(+), 97 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index d116e7337..9d1e63792 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -32,69 +32,56 @@ template > T, std::convertible_to U> return {d_value, d_value, d_value}; } -void check_array_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { - pgm_type_func_selector(attribute, [component, attribute]() { - for (Idx size = 0; size < 4; ++size) { - std::vector source_buffer(size); - std::vector ref_buffer(size); - - Buffer buffer{component, size}; - - // array get value - buffer.set_nan(); - buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - REQUIRE(is_nan(ref_buffer[idx])); - } +void check_array_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + std::vector source_buffer(size); + std::vector ref_buffer(size); - // array set value - buffer.set_nan(); - for (Idx idx = 0; idx < size; ++idx) { - source_buffer[idx] = as_type(idx); - } - buffer.set_value(attribute, source_buffer.data(), sizeof(T)); - buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - REQUIRE(ref_buffer[idx] == as_type(idx)); - } + Buffer buffer{component, size}; + buffer.set_nan(); + + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + REQUIRE(is_nan(ref_buffer[idx])); } }); } -void check_sub_array_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { - pgm_type_func_selector(attribute, [component, attribute]() { - for (Idx size = 0; size < 4; ++size) { - for (Idx sub_size = 0; sub_size < size; ++sub_size) { - for (Idx offset = 0; offset < size - sub_size; ++offset) { - std::vector source_buffer(size); - std::vector ref_buffer(size); - - Buffer buffer{component, size}; - - // get value - buffer.set_nan(); - buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - if (idx >= offset && idx < offset + sub_size) { - REQUIRE(is_nan(ref_buffer[idx])); - } else { - REQUIRE(ref_buffer[idx] == T{}); - } - } +void check_array_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + std::vector source_buffer(size); + std::vector ref_buffer(size); - // set value - buffer.set_nan(); - for (Idx idx = 0; idx < size; ++idx) { - source_buffer[idx] = as_type(idx); - } - buffer.set_value(attribute, source_buffer.data(), offset, sub_size, sizeof(T)); - buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - if (idx >= offset && idx < offset + sub_size) { - REQUIRE(ref_buffer[idx] == as_type(idx)); - } else { - REQUIRE(is_nan(ref_buffer[idx])); - } + Buffer buffer{component, size}; + buffer.set_nan(); + + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); + } + buffer.set_value(attribute, source_buffer.data(), sizeof(T)); + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + REQUIRE(ref_buffer[idx] == as_type(idx)); + } + }); +} + +void check_sub_array_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + for (Idx sub_size = 0; sub_size < size; ++sub_size) { + for (Idx offset = 0; offset < size - sub_size; ++offset) { + std::vector source_buffer(size); + std::vector ref_buffer(size); + + Buffer buffer{component, size}; + buffer.set_nan(); + + buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(is_nan(ref_buffer[idx])); + } else { + REQUIRE(ref_buffer[idx] == T{}); } } } @@ -102,57 +89,142 @@ void check_sub_array_buffer_access(MetaComponent const* component, MetaAttribute }); } -void check_single_buffer_access(MetaComponent const* component, MetaAttribute const* attribute) { - pgm_type_func_selector(attribute, [component, attribute]() { - for (Idx size = 0; size < 4; ++size) { - Buffer buffer{component, size}; - - T source_value{}; - T ref_value{}; - - buffer.set_nan(); - for (Idx idx = 0; idx < size; ++idx) { - // get value - buffer.get_value(attribute, &ref_value, idx, 0); - if (size > 0) { - REQUIRE(is_nan(ref_value)); - } else { - REQUIRE(ref_value == T{}); +void check_sub_array_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + for (Idx sub_size = 0; sub_size < size; ++sub_size) { + for (Idx offset = 0; offset < size - sub_size; ++offset) { + std::vector source_buffer(size); + std::vector ref_buffer(size); + + Buffer buffer{component, size}; + buffer.set_nan(); + + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); } - // set value - buffer.set_value(attribute, &source_value, idx, 0); - buffer.get_value(attribute, &ref_value, idx, 0); - if (size > 0) { - REQUIRE(ref_value == source_value); - } else { - REQUIRE(ref_value == T{}); + buffer.set_value(attribute, source_buffer.data(), offset, sub_size, sizeof(T)); + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(ref_buffer[idx] == as_type(idx)); + } else { + REQUIRE(is_nan(ref_buffer[idx])); + } } } } }); } + +void check_single_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + T source_value{}; + T ref_value{}; + + Buffer buffer{component, size}; + buffer.set_nan(); + + for (Idx idx = 0; idx < size; ++idx) { + buffer.get_value(attribute, &ref_value, idx, 0); + if (size > 0) { + REQUIRE(is_nan(ref_value)); + } else { + REQUIRE(ref_value == T{}); + } + } + }); +} + +void check_single_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { + pgm_type_func_selector(attribute, [=]() { + T source_value{}; + T ref_value{}; + + Buffer buffer{component, size}; + buffer.set_nan(); + + for (Idx idx = 0; idx < size; ++idx) { + buffer.set_value(attribute, &source_value, idx, 0); + buffer.get_value(attribute, &ref_value, idx, 0); + if (size > 0) { + REQUIRE(ref_value == source_value); + } else { + REQUIRE(ref_value == T{}); + } + } + }); +} } // namespace TEST_CASE("API Buffer") { - for (Idx dataset_idx = 0; dataset_idx < MetaData::n_datasets(); ++dataset_idx) { - CAPTURE(dataset_idx); - MetaDataset const* dataset = MetaData::get_dataset_by_idx(dataset_idx); - CAPTURE(MetaData::dataset_name(dataset)); - for (Idx component_idx = 0; component_idx < MetaData::n_components(dataset); ++component_idx) { - CAPTURE(component_idx); - MetaComponent const* component = MetaData::get_component_by_idx(dataset, component_idx); - CAPTURE(MetaData::component_name(component)); - - for (Idx attribute_idx = 0; attribute_idx < MetaData::n_attributes(component); ++attribute_idx) { - CAPTURE(attribute_idx); - MetaAttribute const* attribute = MetaData::get_attribute_by_idx(component, attribute_idx); - CAPTURE(MetaData::attribute_name(attribute)); - - check_array_buffer_access(component, attribute); - check_sub_array_buffer_access(component, attribute); - check_single_buffer_access(component, attribute); + auto const loop_datasets_components_attributes = [](Func func) { + for (Idx dataset_idx = 0; dataset_idx < MetaData::n_datasets(); ++dataset_idx) { + CAPTURE(dataset_idx); + MetaDataset const* dataset = MetaData::get_dataset_by_idx(dataset_idx); + CAPTURE(MetaData::dataset_name(dataset)); + for (Idx component_idx = 0; component_idx < MetaData::n_components(dataset); ++component_idx) { + CAPTURE(component_idx); + MetaComponent const* component = MetaData::get_component_by_idx(dataset, component_idx); + CAPTURE(MetaData::component_name(component)); + + for (Idx attribute_idx = 0; attribute_idx < MetaData::n_attributes(component); ++attribute_idx) { + CAPTURE(attribute_idx); + MetaAttribute const* attribute = MetaData::get_attribute_by_idx(component, attribute_idx); + CAPTURE(MetaData::attribute_name(attribute)); + + func(component, attribute); + } } } + }; + + SUBCASE("Array buffer access") { + SUBCASE("get value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_array_get_value(component, attribute, size); + } + }); + } + SUBCASE("set value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_array_set_value(component, attribute, size); + } + }); + } + } + SUBCASE("Sub-array buffer access") { + SUBCASE("get value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_sub_array_get_value(component, attribute, size); + } + }); + } + SUBCASE("set value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_sub_array_set_value(component, attribute, size); + } + }); + } + } + SUBCASE("Single buffer access") { + SUBCASE("get value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_single_get_value(component, attribute, size); + } + }); + } + SUBCASE("set value") { + loop_datasets_components_attributes([](MetaComponent const* component, MetaAttribute const* attribute) { + for (Idx size = 0; size < 4; ++size) { + check_single_set_value(component, attribute, size); + } + }); + } } } From 41381b88b85d66e39fc1d406073e0f2cbcad8aca Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 15:34:57 +0200 Subject: [PATCH 19/20] remove unused variables Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index 9d1e63792..dae863352 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -34,7 +34,6 @@ template > T, std::convertible_to U> void check_array_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { pgm_type_func_selector(attribute, [=]() { - std::vector source_buffer(size); std::vector ref_buffer(size); Buffer buffer{component, size}; @@ -70,7 +69,6 @@ void check_sub_array_get_value(MetaComponent const* component, MetaAttribute con pgm_type_func_selector(attribute, [=]() { for (Idx sub_size = 0; sub_size < size; ++sub_size) { for (Idx offset = 0; offset < size - sub_size; ++offset) { - std::vector source_buffer(size); std::vector ref_buffer(size); Buffer buffer{component, size}; @@ -118,7 +116,6 @@ void check_sub_array_set_value(MetaComponent const* component, MetaAttribute con void check_single_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { pgm_type_func_selector(attribute, [=]() { - T source_value{}; T ref_value{}; Buffer buffer{component, size}; From e3f9a86b29ed9942ea48ee0cf6ba551b45f49372 Mon Sep 17 00:00:00 2001 From: Martijn Govers Date: Tue, 8 Oct 2024 16:07:25 +0200 Subject: [PATCH 20/20] sonar cloud is really strict today Signed-off-by: Martijn Govers --- tests/native_api_tests/test_api_buffer.cpp | 97 ++++++++++++---------- 1 file changed, 51 insertions(+), 46 deletions(-) diff --git a/tests/native_api_tests/test_api_buffer.cpp b/tests/native_api_tests/test_api_buffer.cpp index dae863352..54528fa6e 100644 --- a/tests/native_api_tests/test_api_buffer.cpp +++ b/tests/native_api_tests/test_api_buffer.cpp @@ -33,7 +33,7 @@ template > T, std::convertible_to U> } void check_array_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { + pgm_type_func_selector(attribute, [component, attribute, size]() { std::vector ref_buffer(size); Buffer buffer{component, size}; @@ -47,16 +47,16 @@ void check_array_get_value(MetaComponent const* component, MetaAttribute const* } void check_array_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { + pgm_type_func_selector(attribute, [component, attribute, size]() { std::vector source_buffer(size); std::vector ref_buffer(size); + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); + } Buffer buffer{component, size}; buffer.set_nan(); - for (Idx idx = 0; idx < size; ++idx) { - source_buffer[idx] = as_type(idx); - } buffer.set_value(attribute, source_buffer.data(), sizeof(T)); buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); for (Idx idx = 0; idx < size; ++idx) { @@ -66,56 +66,61 @@ void check_array_set_value(MetaComponent const* component, MetaAttribute const* } void check_sub_array_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { - for (Idx sub_size = 0; sub_size < size; ++sub_size) { - for (Idx offset = 0; offset < size - sub_size; ++offset) { - std::vector ref_buffer(size); - - Buffer buffer{component, size}; - buffer.set_nan(); - - buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - if (idx >= offset && idx < offset + sub_size) { - REQUIRE(is_nan(ref_buffer[idx])); - } else { - REQUIRE(ref_buffer[idx] == T{}); - } - } + auto const check_sub_offset_size = [component, attribute, size](Idx offset, Idx sub_size) { + std::vector ref_buffer(size); + + Buffer buffer{component, size}; + buffer.set_nan(); + + buffer.get_value(attribute, ref_buffer.data(), offset, sub_size, sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(is_nan(ref_buffer[idx])); + } else { + REQUIRE(ref_buffer[idx] == T{}); } } - }); + }; + for (Idx sub_size = 0; sub_size < size; ++sub_size) { + CAPTURE(sub_size); + for (Idx offset = 0; offset < size - sub_size; ++offset) { + CAPTURE(offset); + pgm_type_func_selector(attribute, check_sub_offset_size, offset, sub_size); + } + } } void check_sub_array_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { - for (Idx sub_size = 0; sub_size < size; ++sub_size) { - for (Idx offset = 0; offset < size - sub_size; ++offset) { - std::vector source_buffer(size); - std::vector ref_buffer(size); - - Buffer buffer{component, size}; - buffer.set_nan(); + auto const check_sub_offset_size = [component, attribute, size](Idx offset, Idx sub_size) { + std::vector ref_buffer(size); + std::vector source_buffer(size); + for (Idx idx = 0; idx < size; ++idx) { + source_buffer[idx] = as_type(idx); + } - for (Idx idx = 0; idx < size; ++idx) { - source_buffer[idx] = as_type(idx); - } - buffer.set_value(attribute, source_buffer.data(), offset, sub_size, sizeof(T)); - buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); - for (Idx idx = 0; idx < size; ++idx) { - if (idx >= offset && idx < offset + sub_size) { - REQUIRE(ref_buffer[idx] == as_type(idx)); - } else { - REQUIRE(is_nan(ref_buffer[idx])); - } - } + Buffer buffer{component, size}; + buffer.set_nan(); + buffer.set_value(attribute, source_buffer.data(), offset, sub_size, sizeof(T)); + buffer.get_value(attribute, ref_buffer.data(), sizeof(T)); + for (Idx idx = 0; idx < size; ++idx) { + if (idx >= offset && idx < offset + sub_size) { + REQUIRE(ref_buffer[idx] == as_type(idx)); + } else { + REQUIRE(is_nan(ref_buffer[idx])); } } - }); + }; + for (Idx sub_size = 0; sub_size < size; ++sub_size) { + CAPTURE(sub_size); + for (Idx offset = 0; offset < size - sub_size; ++offset) { + CAPTURE(offset); + pgm_type_func_selector(attribute, check_sub_offset_size, offset, sub_size); + } + } } void check_single_get_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { + pgm_type_func_selector(attribute, [component, attribute, size]() { T ref_value{}; Buffer buffer{component, size}; @@ -133,8 +138,8 @@ void check_single_get_value(MetaComponent const* component, MetaAttribute const* } void check_single_set_value(MetaComponent const* component, MetaAttribute const* attribute, Idx size) { - pgm_type_func_selector(attribute, [=]() { - T source_value{}; + pgm_type_func_selector(attribute, [component, attribute, size]() { + T const source_value{1}; T ref_value{}; Buffer buffer{component, size};