Skip to content

Commit

Permalink
Merge pull request #22 from contour-terminal/improvement/runtime_divi…
Browse files Browse the repository at this point in the history
…sion_check_in_debug

Add division check for debug mode
  • Loading branch information
Yaraslaut authored Sep 27, 2024
2 parents 82b3621 + 52e0033 commit c0ff849
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 6 deletions.
10 changes: 5 additions & 5 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,17 @@ on:
jobs:
check_clang_format:
name: "Check C++ style"
runs-on: ubuntu-22.04
runs-on: ubuntu-24.04
steps:
- uses: actions/checkout@v3
- name: Install clang
run: |
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
sudo ./llvm.sh 17
sudo apt-get install clang-format-17
sudo ./llvm.sh 18
sudo apt-get install clang-format-18
- name: "Clang-format"
run: clang-format-17 --Werror --dry-run test-boxed-cpp.cpp include/boxed-cpp/boxed.hpp
run: clang-format-18 --Werror --dry-run test-boxed-cpp.cpp include/boxed-cpp/boxed.hpp

editorconfig:
name: "Check editorconfig"
Expand All @@ -37,7 +37,7 @@ jobs:
matrix:
os: [ubuntu-24.04, ubuntu-22.04]
cxx: [20]
build_type: ["RelWithDebInfo"]
build_type: ["RelWithDebInfo", "Debug"]
llvm_version:
[
"17",
Expand Down
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ target_include_directories(boxed-cpp INTERFACE
$<BUILD_INTERFACE:${${PROJECT_NAME}_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
)
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
target_compile_definitions(boxed-cpp INTERFACE BOXED_DEBUG)
endif()

# Generate the version, config and target files
include(CMakePackageConfigHelpers)
Expand Down
40 changes: 39 additions & 1 deletion include/boxed-cpp/boxed.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
#include <compare>
#include <iostream>
#include <limits>
#include <stdexcept>
#include <type_traits>
#if defined(BOXED_DEBUG)
#include <cassert>
#endif

namespace boxed
{
Expand Down Expand Up @@ -126,7 +130,41 @@ template <typename T, typename U> constexpr boxed<T, U>& operator/=(boxed<T, U>&
template <typename T, typename U> constexpr boxed<T, U>& operator%=(boxed<T, U>& a, boxed<T, U> const& b) noexcept { a.value %= b.value; return a; }

template <typename T, typename U> std::ostream& operator<<(std::ostream& os, boxed<T, U> const& v) { return os << v.value; }
// clang-format on

// clang-format on
#if defined(BOXED_DEBUG)

template <std::integral T, typename U>
constexpr boxed<T, U> operator/(boxed<T, U> const& a, boxed<T, U> const& b)
{
auto div = std::div(b.value, a.value);
if (div.rem != 0)
throw std::invalid_argument("Division is not exact");
return boxed<T, U> { div.quot };
}

template <std::integral T, typename U>
constexpr boxed<T, U> operator/(boxed<T, U> const& a, T b)
{
return a / boxed<T, U> { b };
}

template <std::integral T, typename U>
constexpr boxed<T, U> operator/(T b, boxed<T, U> const& a)
{
return boxed<T, U> { b } / a;
}

template <std::integral T, typename U>
constexpr boxed<T, U>& operator/=(boxed<T, U>& a, boxed<T, U> const& b)
{
auto res = a / b;
a = res;
return a;
}

#endif

} // namespace detail

namespace helper
Expand Down
27 changes: 27 additions & 0 deletions test-boxed-cpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <cmath>
#include <functional>
#include <limits>
#include <stdexcept>
#include <type_traits>

#include <boxed-cpp/boxed.hpp>
Expand Down Expand Up @@ -216,6 +217,32 @@ TEST_CASE("advanced")
REQUIRE(x_coord(phi, theta, rho) == x_coord(phi, theta, rho));
}

TEST_CASE("devision of integral_types")
{
using Int = boxed::boxed<int>;
int int_a { 10 };
Int Int_a { 10 };
int int_b { 3 };
Int Int_b { 3 };
#if defined(BOXED_DEBUG)
REQUIRE_THROWS_AS(int_a / Int_b, std::invalid_argument);
REQUIRE_THROWS_AS(Int_a / int_b, std::invalid_argument);
REQUIRE_THROWS_AS(Int_a / Int_b, std::invalid_argument);
REQUIRE_THROWS_AS(
[&]() {
Int_a /= Int_b;
}(),
std::invalid_argument);

#else
REQUIRE(unbox(int_a / Int_b) == 3);
REQUIRE(unbox(Int_a / int_b) == 3);
REQUIRE(unbox(Int_a / Int_b) == 3);
Int_a /= Int_b;
REQUIRE(unbox(Int_a) == 3);
#endif
}

#ifdef __has_include
#if __has_include(<version>)
#include <version>
Expand Down

0 comments on commit c0ff849

Please sign in to comment.