Skip to content

Commit

Permalink
replace use of assert macro
Browse files Browse the repository at this point in the history
Change-Id: I97edf879e46911507e93c35060fdf8d01afe29c4
  • Loading branch information
oliverlee committed Sep 29, 2024
1 parent 1a2227e commit bcd41e8
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 12 deletions.
3 changes: 0 additions & 3 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@ Checks: >
# Bazel does this for determinism,
-clang-diagnostic-builtin-macro-redefined,
# suppress due to assert,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
# short names are fine for short lifetimes,
-readability-identifier-length,
Expand Down
6 changes: 3 additions & 3 deletions rigid_geometric_algebra/blade_type_from.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
#include "rigid_geometric_algebra/algebra_type.hpp"
#include "rigid_geometric_algebra/blade.hpp"
#include "rigid_geometric_algebra/blade_ordering.hpp"
#include "rigid_geometric_algebra/detail/contract.hpp"

#include <array>
#include <cassert>
#include <cstddef>
#include <utility>

Expand All @@ -30,12 +30,12 @@ struct blade_type_from

for (auto index = 0UZ; index != mask.size(); ++index) {
if (mask.test(index)) {
assert(it != dims.end());
detail::invariant(it != dims.end());
*it++ = index;
}
}
assert(it == dims.end());

detail::postcondition(it == dims.end());
return dims;
}();

Expand Down
5 changes: 3 additions & 2 deletions rigid_geometric_algebra/detail/array_subset.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#pragma once

#include "rigid_geometric_algebra/detail/contract.hpp"

#include <array>
#include <cassert>
#include <cstddef>
#include <ranges>

Expand All @@ -26,7 +27,7 @@ class array_subset
constexpr array_subset(const base_type& base, std::size_t size)
: base_{base}, size_{size}
{
assert(size_ <= Capacity);
detail::precondition(size_ <= Capacity);
}

constexpr auto begin() const noexcept { return base_.begin(); }
Expand Down
40 changes: 40 additions & 0 deletions rigid_geometric_algebra/detail/contract.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ struct precondition_contract
explicit precondition_contract() = default;
};

struct invariant_contract
{
static constexpr auto name = "INVARIANT";
explicit invariant_contract() = default;
};

struct postcondition_contract
{
static constexpr auto name = "POSTCONDITION";
Expand Down Expand Up @@ -137,6 +143,40 @@ constexpr auto precondition(

/// @}

/// check an invariant, optionally printing a message on failure
/// @param cond boolean-convertible value
/// @param violation_handler handler to invoke on contract violation.
/// @param sl source location
///
/// Check invariant `cond`. If `false`, invoke `handler(sl)`.
///
/// @{

template <class Handler>
requires std::is_invocable_v<
const Handler&,
invariant_contract,
const std::source_location&>
constexpr auto invariant(
const std::convertible_to<bool> auto& cond,
const Handler& violation_handler,
const std::source_location& sl = std::source_location::current()) -> void
{
if (cond) {
return;

Check warning on line 166 in rigid_geometric_algebra/detail/contract.hpp

View check run for this annotation

Codecov / codecov/patch

rigid_geometric_algebra/detail/contract.hpp#L165-L166

Added lines #L165 - L166 were not covered by tests
}

violation_handler(invariant_contract{}, sl);

Check warning on line 169 in rigid_geometric_algebra/detail/contract.hpp

View check run for this annotation

Codecov / codecov/patch

rigid_geometric_algebra/detail/contract.hpp#L169

Added line #L169 was not covered by tests
}

constexpr auto invariant(
const std::convertible_to<bool> auto& cond,
std::format_string<> message = "", // NOLINT(misc-include-cleaner)
const std::source_location& sl = std::source_location::current()) -> void
{
return invariant(cond, contract_violation_handler{message}, sl);

Check warning on line 177 in rigid_geometric_algebra/detail/contract.hpp

View check run for this annotation

Codecov / codecov/patch

rigid_geometric_algebra/detail/contract.hpp#L177

Added line #L177 was not covered by tests
}

/// check a postcondition, optionally printing a message on failure
/// @param cond boolean-convertible value
/// @param violation_handler handler to invoke on contract violation.
Expand Down
1 change: 0 additions & 1 deletion rigid_geometric_algebra/detail/disjoint_subset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

#include <algorithm>
#include <array>
#include <cassert>
#include <cstddef>
#include <functional>
#include <ranges>
Expand Down
1 change: 0 additions & 1 deletion rigid_geometric_algebra/detail/structural_bitset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include "rigid_geometric_algebra/detail/contract.hpp"

#include <bit>
#include <cassert>
#include <cstddef>
#include <cstdint>
#include <type_traits>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <algorithm>
#include <array>
#include <cassert>
#include <concepts>
#include <cstddef>
#include <functional>
Expand Down
2 changes: 2 additions & 0 deletions test/complement_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ auto main() -> int
using ::SymEngine::Expression;

const auto abs = []<class T>(T&& arg) -> Expression {
// string literal decays to pointer
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-array-to-pointer-decay)
return {::SymEngine::abs(Expression{std::forward<T>(arg)})};
};

Expand Down
1 change: 0 additions & 1 deletion test/detail/define_prioritized_overload_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "rigid_geometric_algebra/detail/priority_list.hpp"
#include "skytest/skytest.hpp"

#include <cassert>
#include <cstddef>
#include <cstdlib>
#include <type_traits>
Expand Down

0 comments on commit bcd41e8

Please sign in to comment.