Skip to content

Commit

Permalink
Added operator scalar / resistances = conductance
Browse files Browse the repository at this point in the history
  • Loading branch information
bernedom committed Aug 3, 2019
1 parent eec0f55 commit 14e6844
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 1.1.1

* Unified usage of template arugments in unit_t
* electric conductance can be built from 1/Ohm (including all ratios)

## 1.1.0

Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ All units that can be built from other units are also decayable to the respectiv
| Electric charge | Q | C | I \* T | aC to EC | `*_coulomb_t` |
| Electric potential | U | V | P / I, E/Q | aV to EV | `*_volt_t` |
| Electric resistance | O* | Ohm (Ω) | U / I | aOhm to EOhm | `*_ohm_t` |
| Electric conductance | G | S | I / U | aS to ES | `*_siemens_t` |
| Electric conductance | G | S | I / U, 1/R | aS to ES | `*_siemens_t` |
| Electric capacity | C | F | Q / U | aF to EF | `*_farad_t` |
| Force | F | N | M \* a | aN to EN | `*_newton_t` |
| Pressure | p | pa | F / L^2 | apa to Epa | `*_pascal_t` |
Expand Down
12 changes: 11 additions & 1 deletion include/SI/electric_conductance.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "detail/unit.h"
#include "electric_current.h"
#include "electric_potential.h"
#include "electric_resistance.h"

namespace SI {

Expand Down Expand Up @@ -50,9 +51,18 @@ template <typename _Type>
using exa_siemens_t = electric_conductance_t<_Type, std::exa>;

namespace detail {
/// @todo add building from 1/Ohm

BUILD_UNIT_FROM_DIVISON(electric_conductance_t, electric_current_t,
electric_potential_t)

/// Builds conductance from 1/resistance
template <typename _Type, class _Ratio = std::ratio<1>>
constexpr auto
operator/(const _Type scalar,
const electric_resistance_t<_Type, _Ratio> &resistance) {
return electric_conductance_t<_Type, std::ratio<_Ratio::den, _Ratio::num>>{
scalar / resistance.raw_value()};
}
} // namespace detail

inline namespace literals {
Expand Down
45 changes: 44 additions & 1 deletion test/src/derived_unit_tests/electric_conductance_tests.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <catch.hpp>

#include <SI/electric_conductance.h>
#include <SI/electric_resistance.h>

using namespace SI::literals;
TEST_CASE("GIVEN a value WHEN constructed with literal _aS THEN result is a "
Expand Down Expand Up @@ -198,7 +199,7 @@ TEMPLATE_TEST_CASE("GIVEN a electric_conductance value WHEN multiplied by an "
TEMPLATE_TEST_CASE("GIVEN a electric_current value WHEN divided by a "
"electric_conductance value THEN "
"result is an electric_potential value",
"[electric_current][operator/]", int64_t, long double) {
"[electric_conductance][operator/]", int64_t, long double) {
constexpr SI::electric_current_t<TestType, std::ratio<1>> electric_current{1};
constexpr SI::electric_conductance_t<TestType, std::ratio<1>>
electric_conductance{1};
Expand All @@ -209,3 +210,45 @@ TEMPLATE_TEST_CASE("GIVEN a electric_current value WHEN divided by a "
std::is_same<decltype(result), const SI::electric_potential_t<
TestType, std::ratio<1>>>::value);
}

TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a resistance value THEN "
"result is an electric conductance value",
"[electric_conductance][operator/]", int64_t, long double) {

constexpr SI::electric_resistance_t<TestType, std::ratio<1>> resistance{5};
constexpr TestType scalar{10};

constexpr auto result = scalar / resistance;
constexpr SI::siemens_t<TestType> expected{2};
STATIC_REQUIRE(
std::is_same<decltype(result), const SI::siemens_t<TestType>>::value);
STATIC_REQUIRE(result == expected);
}

namespace {
/// helper struct to faciltate templated testcase
template <typename _ratio_lhs, typename _ratio_rhs> struct ratio_tuple {
using ratio_lhs = _ratio_lhs;
using ratio_rhs = _ratio_rhs;
};

} // namespace

TEMPLATE_TEST_CASE("GIVEN a scalar WHEN divided by a resistance value THEN "
"result is electric_conductance AND ratio is kept",
"[electric_conductance][operator/]",
(ratio_tuple<std::milli, std::kilo>),
(ratio_tuple<std::kilo, std::milli>),
(ratio_tuple<std::ratio<1>, std::ratio<1>>)) {

constexpr auto resistance =
SI::electric_resistance_t<int64_t, typename TestType::ratio_lhs>{5};
constexpr int64_t scalar{10};

constexpr auto result = scalar / resistance;
constexpr SI::electric_conductance_t<int64_t, typename TestType::ratio_rhs>
expected{2};
STATIC_REQUIRE(std::ratio_equal<typename decltype(result)::ratio,
typename TestType::ratio_rhs>::value);
STATIC_REQUIRE(result == expected);
}

0 comments on commit 14e6844

Please sign in to comment.