Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/full clang tidy after indptrs #403

Merged
merged 11 commits into from
Oct 17, 2023
1 change: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-avoid-const-or-ref-data-members,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-init-variables,
-cppcoreguidelines-macro-usage,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ class Deserializer {
// movable
Deserializer(Deserializer&&) = default;
Deserializer& operator=(Deserializer&&) = default;

// destructor
~Deserializer() = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "power_grid_model.hpp"

#include <boost/iterator/iterator_facade.hpp>
#include <boost/range.hpp>

#include <functional>
#include <memory>
Expand Down Expand Up @@ -309,28 +310,17 @@ class Container<RetrievableTypes<GettableTypes...>, StorageableTypes...> {
Idx idx_;
};

// define proxy
template <class Gettable> class Proxy {
private:
static constexpr bool is_const = std::is_const_v<Gettable>;
using base_type = std::remove_cv_t<Gettable>;
using container_type = std::conditional_t<is_const, Container const, Container>;

public:
explicit Proxy(container_type& container)
: begin_{&container, 0}, end_{&container, container.template size<base_type>()} {}
Iterator<Gettable> begin() { return begin_; }
Iterator<Gettable> end() { return end_; }

private:
Iterator<Gettable> const begin_;
Iterator<Gettable> const end_;
};

public:
template <class Gettable> Proxy<Gettable> iter() { return Proxy<Gettable>{*this}; }
template <class Gettable> Proxy<Gettable const> iter() const { return Proxy<Gettable const>{*this}; }
template <class Gettable> Proxy<Gettable const> citer() const { return iter<Gettable>(); }
template <class Gettable> auto iter() {
return boost::make_iterator_range(Iterator<Gettable>{this, 0},
Iterator<Gettable>{this, this->template size<std::remove_cv_t<Gettable>>()});
}
template <class Gettable> auto iter() const {
return boost::make_iterator_range(
Iterator<Gettable const>{this, 0},
Iterator<Gettable const>{this, this->template size<std::remove_cv_t<Gettable>>()});
}
template <class Gettable> auto citer() const { return iter<Gettable>(); }
};

// type traits to instantiate container
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,6 @@ struct from_dense_t {};
constexpr auto from_sparse = from_sparse_t{};
constexpr auto from_dense = from_dense_t{};

class DenseGroupedIdxVector;

class SparseGroupedIdxVector {
private:
class GroupIterator : public boost::iterator_facade<GroupIterator, Idx, boost::random_access_traversal_tag,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ template <bool sym> class IterativeLinearSESolver {

// loop to iterate
Idx num_iter = 0;
do {
while (max_dev > err_tol || num_iter == 0) {
if (num_iter++ == max_iter) {
throw IterationDiverge{max_iter, max_dev, err_tol};
}
Expand All @@ -604,7 +604,7 @@ template <bool sym> class IterativeLinearSESolver {
sparse_solver_.solve_with_prefactorized_matrix(data_gain_, perm_, x_rhs_, x_rhs_);
sub_timer = Timer(calculation_info, 2226, "Iterate unknown");
max_dev = iterate_unknown(output.u, measured_values.has_angle_measurement());
} while (max_dev > err_tol);
};

// calculate math result
sub_timer = Timer(calculation_info, 2227, "Calculate Math Result");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ template <bool sym, typename DerivedSolver> class IterativePFSolver {
// start calculation
// iteration
Idx num_iter = 0;
do {
while (max_dev > err_tol || num_iter == 0) {
if (num_iter++ == max_iter) {
throw IterationDiverge{max_iter, max_dev, err_tol};
}
Expand All @@ -86,7 +86,7 @@ template <bool sym, typename DerivedSolver> class IterativePFSolver {
Timer const sub_timer{calculation_info, 2224, "Iterate unknown"};
max_dev = derived_solver.iterate_unknown(output.u);
}
} while (max_dev > err_tol);
}

// calculate math result
{
Expand Down
10 changes: 5 additions & 5 deletions tests/c_api_tests/test_c_api_model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ TEST_CASE("C API Model") {
PGM_Options* opt = unique_options.get();

// input data
ConstDatasetPtr const unique_input_dataset{PGM_create_dataset_const(hl, "input", false, 1)};
ConstDatasetPtr const unique_input_dataset{PGM_create_dataset_const(hl, "input", 0, 1)};
petersalemink95 marked this conversation as resolved.
Show resolved Hide resolved
PGM_ConstDataset* input_dataset = unique_input_dataset.get();
NodeInput node_input{{0}, 100.0};
SourceInput source_input{{{1}, 0, 1}, 1.0, 0.0, 1000.0, 0.0, 1.0};
Expand All @@ -70,10 +70,10 @@ TEST_CASE("C API Model") {
std::array<NodeOutput<true>, 2> sym_node_outputs{};
NodeOutput<true>& node_result_0 = sym_node_outputs[0];
NodeOutput<true>& node_result_1 = sym_node_outputs[1];
MutableDatasetPtr const unique_single_output_dataset{PGM_create_dataset_mutable(hl, "sym_output", false, 1)};
MutableDatasetPtr const unique_single_output_dataset{PGM_create_dataset_mutable(hl, "sym_output", 0, 1)};
PGM_MutableDataset* single_output_dataset = unique_single_output_dataset.get();
PGM_dataset_mutable_add_buffer(hl, single_output_dataset, "node", 1, 1, nullptr, sym_node_outputs.data());
MutableDatasetPtr const unique_batch_output_dataset{PGM_create_dataset_mutable(hl, "sym_output", true, 2)};
MutableDatasetPtr const unique_batch_output_dataset{PGM_create_dataset_mutable(hl, "sym_output", 1, 2)};
PGM_MutableDataset* batch_output_dataset = unique_batch_output_dataset.get();
PGM_dataset_mutable_add_buffer(hl, batch_output_dataset, "node", 1, 2, nullptr, sym_node_outputs.data());

Expand All @@ -90,11 +90,11 @@ TEST_CASE("C API Model") {
load_updates[1].id = 2;
load_updates[1].q_specified = 300.0;
// dataset
ConstDatasetPtr const unique_single_update_dataset{PGM_create_dataset_const(hl, "update", false, 1)};
ConstDatasetPtr const unique_single_update_dataset{PGM_create_dataset_const(hl, "update", 0, 1)};
PGM_ConstDataset* single_update_dataset = unique_single_update_dataset.get();
PGM_dataset_const_add_buffer(hl, single_update_dataset, "source", 1, 1, nullptr, &source_update);
PGM_dataset_const_add_buffer(hl, single_update_dataset, "sym_load", 1, 1, nullptr, load_updates.data());
ConstDatasetPtr const unique_batch_update_dataset{PGM_create_dataset_const(hl, "update", true, 2)};
ConstDatasetPtr const unique_batch_update_dataset{PGM_create_dataset_const(hl, "update", 1, 2)};
PGM_ConstDataset* batch_update_dataset = unique_batch_update_dataset.get();
PGM_dataset_const_add_buffer(hl, batch_update_dataset, "source", -1, 1, source_update_indptr.data(),
&source_update);
Expand Down
22 changes: 11 additions & 11 deletions tests/c_api_tests/test_serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ TEST_CASE("Serialization") {
std::vector<Idx> const total_elements = {1, 2};

SUBCASE("Serializer") {
ConstDatasetPtr unique_dataset{PGM_create_dataset_const(hl, "input", is_batch, batch_size)};
ConstDatasetPtr const unique_dataset{PGM_create_dataset_const(hl, "input", is_batch, batch_size)};
CHECK(PGM_error_code(hl) == PGM_no_error);
auto const dataset = unique_dataset.get();
auto* const dataset = unique_dataset.get();
PGM_dataset_const_add_buffer(hl, dataset, "node", elements_per_scenario[0], total_elements[0], nullptr,
node.data());
PGM_dataset_const_add_buffer(hl, dataset, "source", elements_per_scenario[1], total_elements[1], nullptr,
source.data());
CHECK(PGM_error_code(hl) == PGM_no_error);

SUBCASE("json") {
SerializerPtr json_serializer{
SerializerPtr const json_serializer{
PGM_create_serializer(hl, dataset, static_cast<PGM_Idx>(SerializationFormat::json))};
auto const serializer = json_serializer.get();
auto* const serializer = json_serializer.get();
CHECK(PGM_error_code(hl) == PGM_no_error);

// to string
Expand All @@ -66,9 +66,9 @@ TEST_CASE("Serialization") {
}

SUBCASE("msgpack") {
SerializerPtr msgpack_serializer{
SerializerPtr const msgpack_serializer{
PGM_create_serializer(hl, dataset, static_cast<PGM_Idx>(SerializationFormat::msgpack))};
auto const serializer = msgpack_serializer.get();
auto* const serializer = msgpack_serializer.get();

// round trip
char const* msgpack_data{};
Expand All @@ -87,10 +87,10 @@ TEST_CASE("Serialization") {
std::vector<char> msgpack_data;
nlohmann::json::to_msgpack(json_document, msgpack_data);

DeserializerPtr unique_deserializer_json(PGM_create_deserializer_from_null_terminated_string(
DeserializerPtr const unique_deserializer_json(PGM_create_deserializer_from_null_terminated_string(
hl, json_data, static_cast<PGM_Idx>(SerializationFormat::json)));
CHECK(PGM_error_code(hl) == PGM_no_error);
DeserializerPtr unique_deserializer_msgpack(
DeserializerPtr const unique_deserializer_msgpack(
PGM_create_deserializer_from_binary_buffer(hl, msgpack_data.data(), static_cast<Idx>(msgpack_data.size()),
static_cast<PGM_Idx>(SerializationFormat::msgpack)));
CHECK(PGM_error_code(hl) == PGM_no_error);
Expand All @@ -100,16 +100,16 @@ TEST_CASE("Serialization") {
// reset data
node[0] = {};
// get dataset
auto const dataset = PGM_deserializer_get_dataset(hl, deserializer);
auto const info = PGM_dataset_writable_get_info(hl, dataset);
auto* const dataset = PGM_deserializer_get_dataset(hl, deserializer);
auto const* const info = PGM_dataset_writable_get_info(hl, dataset);
// check meta data
CHECK(PGM_dataset_info_name(hl, info) == "input"s);
CHECK(PGM_dataset_info_is_batch(hl, info) == is_batch);
CHECK(PGM_dataset_info_batch_size(hl, info) == batch_size);
CHECK(PGM_dataset_info_n_components(hl, info) == n_components);
CHECK(PGM_dataset_info_component_name(hl, info, 0) == "node"s);
CHECK(PGM_dataset_info_component_name(hl, info, 1) == "source"s);
for (Idx idx : {0, 1}) {
for (Idx const idx : {0, 1}) {
CHECK(PGM_dataset_info_elements_per_scenario(hl, info, idx) == elements_per_scenario[idx]);
CHECK(PGM_dataset_info_total_elements(hl, info, idx) == total_elements[idx]);
}
Expand Down
8 changes: 7 additions & 1 deletion tests/cpp_unit_tests/test_deserializer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
//
// SPDX-License-Identifier: MPL-2.0

// Issue in msgpack, reported in https://github.com/msgpack/msgpack-c/issues/1098
// May be a Clang Analyzer bug
#ifndef __clang_analyzer__ // TODO(mgovers): re-enable this when issue in msgpack is fixed

#include <power_grid_model/auxiliary/input.hpp>
#include <power_grid_model/auxiliary/serialization/deserializer.hpp>
#include <power_grid_model/auxiliary/update.hpp>
Expand Down Expand Up @@ -454,4 +458,6 @@ TEST_CASE("Deserializer with error") {
}
}

} // namespace power_grid_model::meta_data
} // namespace power_grid_model::meta_data

#endif // __clang_analyzer__ // issue in msgpack