units
Use physical dimensions at compile-time or run-time.
dyndim-base-test.cpp
Go to the documentation of this file.
1 /// @file test/dyndim-base-test.cpp
2 /// @brief Test-cases for vnix::units::dyndim_base.
3 /// @copyright 2019 Thomas E. Vaughan; all rights reserved.
4 /// @license BSD three-clause; see LICENSE.
5 
6 #include "../vnix/units/statdim-base.hpp"
7 #include "catch.hpp"
8 
9 using dim = vnix::units::dim;
10 template <uint64_t D> using statdim_base = vnix::units::statdim_base<D>;
11 using dyndim_base = vnix::units::dyndim_base;
12 
13 
14 TEST_CASE("Check dimension on dyndim_base from dim.", "[dyndim-base]") {
15  dim constexpr d(-1, 1, 0, 0, 0);
16  dim e(1, 0, 0, 0, 0);
17  REQUIRE(dyndim_base(d).d() == d);
18  REQUIRE(dyndim_base(e).d() == e);
19 }
20 
21 
22 TEST_CASE("Sum & diff require same dimension for dyndim.", "[dyndim-base]") {
23  dim constexpr d1(-1, 1, 1, 0, 0);
24  dim constexpr d2(+1, 0, 0, 0, 0);
25 
26  dyndim_base ddb(d1);
27  statdim_base<d1.encode()> sdb1;
28  statdim_base<d2.encode()> sdb2;
29 
30  REQUIRE_NOTHROW(ddb.sum(ddb));
31  REQUIRE_NOTHROW(ddb.sum(sdb1));
32  REQUIRE_NOTHROW(ddb.diff(ddb));
33  REQUIRE_NOTHROW(ddb.diff(sdb1));
34 
35  REQUIRE(ddb.sum(ddb).d() == d1);
36  REQUIRE(ddb.sum(sdb1).d() == d1);
37  REQUIRE(ddb.diff(ddb).d() == d1);
38  REQUIRE(ddb.diff(sdb1).d() == d1);
39 
40  REQUIRE_THROWS(ddb.sum(sdb2));
41  REQUIRE_THROWS(ddb.diff(sdb2));
42 }
43 
44 
45 TEST_CASE("Prod & quot change dimension for dyndim.", "[dyndim-base]") {
46  dim constexpr d1(-1, +1, +1, 0, 0);
47  dim constexpr d2(+1, +0, +0, 0, 0);
48  dim constexpr d3(+0, +1, +1, 0, 0);
49  dim constexpr d4(-2, +1, +1, 0, 0);
50  dim constexpr d5(+1, -1, -1, 0, 0);
51 
52  dyndim_base ddb1(d1), ddb2(d2);
53  statdim_base<d2.encode()> sdb2;
54 
55  REQUIRE(ddb1.prod(ddb2).d() == d3);
56  REQUIRE(ddb1.quot(ddb2).d() == d4);
57  REQUIRE(ddb1.prod(sdb2).d() == d3);
58  REQUIRE(ddb1.quot(sdb2).d() == d4);
59  REQUIRE(ddb1.recip().d() == d5);
60 }
61 
62 
63 TEST_CASE("Pow and sqrt change dimension for dyndim.", "[dyndim-base]") {
64  using rat = dim::rat;
65 
66  dim constexpr d1(-1, +1, +1, 0, 0);
67  dim constexpr d2(-2, +2, +2, 0, 0);
68  dim constexpr d3(rat(-1, 2), rat(1, 2), rat(1, 2), 0, 0);
69 
70  dyndim_base ddb(d1);
71 
72  REQUIRE(ddb.pow<2>().d() == d2);
73  REQUIRE(ddb.pow(2).d() == d2);
74  REQUIRE(ddb.sqrt().d() == d3);
75 }
constexpr word encode() const
Encode data for this dim into a word.
Definition: dim.hpp:104
constexpr recip_basedim recip() const
Dimension for reciprocal of dimensioned value.
Definition: dyndim-base.hpp:86
constexpr dyndim_base diff(B const &b) const
Dimension for difference of dimensioned values.
Definition: dyndim-base.hpp:60
Base-type for a dimensioned value whose dimension is specified, perhaps dynamically at run-time...
Definition: dyndim-base.hpp:24
constexpr dim d() const
Exponent for each unit in dimensioned quantity.
Definition: dyndim-base.hpp:33
constexpr dyndim_base(dim dd)
Initialize from exponents representing dimension.
Definition: dyndim-base.hpp:29
constexpr rational(stype n=0, stype d=1)
Initialize from numerator and denominator.
Definition: rational.hpp:47
constexpr dyndim_base quot(B const &b) const
Dimension for quotient of dimensioned values.
Definition: dyndim-base.hpp:77
constexpr dyndim_base pow(dim::rat p) const
Dimension for rational power of dimensioned value.
Definition: dyndim-base.hpp:99
constexpr dyndim_base sum(B const &b) const
Dimension for sum of dimensioned values.
Definition: dyndim-base.hpp:51
constexpr dyndim_base prod(B const &b) const
Dimension for product of dimensioned values.
Definition: dyndim-base.hpp:69
constexpr bool operator==(basic_dim const &d) const
Definition: dim.hpp:227
constexpr dyndim_base sqrt() const
Dimension for square-root of dimensioned value.
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
Classes and functions supporting a model of physically dimensioned quantities.
constexpr dyndim_base pow() const
Dimension for rational power of dimensioned value.
Definition: dyndim-base.hpp:92