Skip to content

Commit

Permalink
Add tests for shorthand mult/div
Browse files Browse the repository at this point in the history
  • Loading branch information
chiphogg committed Aug 17, 2024
1 parent 43145a9 commit 0d44faf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
25 changes: 13 additions & 12 deletions au/code/au/quantity.hh
Original file line number Diff line number Diff line change
Expand Up @@ -339,16 +339,21 @@ class Quantity {
return *this;
}

// Short-hand multiplication assignment.
template <typename T>
constexpr Quantity &operator*=(T s) {
void perform_shorthand_checks() {
static_assert(
std::is_arithmetic<T>::value,
"This overload is only for scalar multiplication-assignment with arithmetic types");
IsValidRep<T>::value,
"This overload is only for scalar mult/div-assignment with raw numeric types");

static_assert(
std::is_floating_point<Rep>::value || std::is_integral<T>::value,
"We don't support compound multiplication of integral types by floating point");
static_assert((!std::is_integral<detail::RealPart<Rep>>::value) ||
std::is_integral<detail::RealPart<T>>::value,
"We don't support compound mult/div of integral types by floating point");
}

// Short-hand multiplication assignment.
template <typename T>
constexpr Quantity &operator*=(T s) {
perform_shorthand_checks<T>();

value_ *= s;
return *this;
Expand All @@ -357,11 +362,7 @@ class Quantity {
// Short-hand division assignment.
template <typename T>
constexpr Quantity &operator/=(T s) {
static_assert(std::is_arithmetic<T>::value,
"This overload is only for scalar division-assignment with arithmetic types");

static_assert(std::is_floating_point<Rep>::value || std::is_integral<T>::value,
"We don't support compound division of integral types by floating point");
perform_shorthand_checks<T>();

value_ /= s;
return *this;
Expand Down
12 changes: 12 additions & 0 deletions au/code/au/quantity_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,18 @@ TEST(Quantity, SupportsExplicitRepConversionToComplexRep) {
EXPECT_THAT(b, SameTypeAndValue(miles(std::complex<int>{2, 0})));
}

TEST(Quantity, ShorthandMultiplicationAssignmentWorksForComplexRepAndScalar) {
auto test = meters(std::complex<float>{1.5f, 0.5f});
test *= std::complex<int>{2, 1};
EXPECT_THAT(test, SameTypeAndValue(meters(std::complex<float>{2.5f, 2.5f})));
}

TEST(Quantity, ShorthandDivisionAssignmentWorksForComplexRepAndScalar) {
auto test = meters(std::complex<float>{25.0f, 12.5f});
test /= std::complex<int>{3, 4};
EXPECT_THAT(test, SameTypeAndValue(meters(std::complex<float>{5.0f, -2.5f})));
}

TEST(Quantity, CanDivideArbitraryQuantities) {
constexpr auto d = feet(6.);
constexpr auto t = hours(3.);
Expand Down

0 comments on commit 0d44faf

Please sign in to comment.