units
Use physical dimensions at compile-time or run-time.
statdim-base-test.cpp
Go to the documentation of this file.
1 /// @file test/statdim-base-test.cpp
2 /// @brief Test-cases for vnix::units::statdim_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 statdim_base from dim.", "[statdim-base]") {
15  dim constexpr d(-1, 1, 0, 0, 0);
16  dim e(1, 0, 0, 0, 0);
17  REQUIRE_NOTHROW(statdim_base<d.encode()>(d));
18  REQUIRE_THROWS(statdim_base<d.encode()>(e));
19 }
20 
21 
22 TEST_CASE("Sum & diff require same dimension for statdim.", "[statdim-base]") {
23  dim constexpr d1(-1, 1, 1, 0, 0);
24  dim constexpr d2(+1, 0, 0, 0, 0);
25 
26  statdim_base<d1.encode()> sdb;
27  dyndim_base ddb1(d1), ddb2(d2);
28 
29  REQUIRE_NOTHROW(sdb.sum(sdb));
30  REQUIRE_NOTHROW(sdb.sum(ddb1));
31  REQUIRE_NOTHROW(sdb.diff(sdb));
32  REQUIRE_NOTHROW(sdb.diff(ddb1));
33 
34  REQUIRE(sdb.sum(sdb).d() == d1);
35  REQUIRE(sdb.sum(ddb1).d() == d1);
36  REQUIRE(sdb.diff(sdb).d() == d1);
37  REQUIRE(sdb.diff(ddb1).d() == d1);
38 
39  REQUIRE_THROWS(sdb.sum(ddb2));
40  REQUIRE_THROWS(sdb.diff(ddb2));
41 }
42 
43 
44 TEST_CASE("Prod & quot change dimension for statdim.", "[statdim-base]") {
45  dim constexpr d1(-1, +1, +1, 0, 0);
46  dim constexpr d2(+1, +0, +0, 0, 0);
47  dim constexpr d3(+0, +1, +1, 0, 0);
48  dim constexpr d4(-2, +1, +1, 0, 0);
49  dim constexpr d5(+1, -1, -1, 0, 0);
50 
51  statdim_base<d1.encode()> sdb1;
52  statdim_base<d2.encode()> sdb2;
53  dyndim_base ddb2(d2);
54 
55  REQUIRE(sdb1.prod(sdb2).d() == d3);
56  REQUIRE(sdb1.quot(sdb2).d() == d4);
57  REQUIRE(sdb1.prod(ddb2).d() == d3);
58  REQUIRE(sdb1.quot(ddb2).d() == d4);
59  REQUIRE(sdb1.recip().d() == d5);
60 }
61 
62 
63 TEST_CASE("Pow and sqrt change dimension for statdim.", "[statdim-base]") {
64  dim constexpr d1(-1, +1, +1, 0, 0);
65  dim constexpr d2(-2, +2, +2, 0, 0);
66 
67  statdim_base<d1.encode()> sdb1;
68 
69  REQUIRE(sdb1.pow<2>().d() == d2);
70  REQUIRE(sdb1.pow(2).d() == d2);
71 
72  dim constexpr d3(dim::rat(-1, 2), dim::rat(1, 2), dim::rat(1, 2), 0, 0);
73  REQUIRE(sdb1.sqrt().d() == d3);
74 }
static constexpr dim d()
Exponent for each unit in dimensioned quantity.
constexpr word encode() const
Encode data for this dim into a word.
Definition: dim.hpp:104
static constexpr dyndim_base prod(dyndim_base const &db)
Dimension for product of dimensioned values.
static constexpr auto diff(statdim_base)
Dimension for difference of dimensioned values.
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
static constexpr dyndim_base quot(dyndim_base const &db)
Dimension for quotient of dimensioned values.
static constexpr dyndim_base sum(dyndim_base const &db)
Dimension for sum of dimensioned values.
constexpr dyndim_base(dim dd)
Initialize from exponents representing dimension.
Definition: dyndim-base.hpp:29
static constexpr recip_basedim recip()
Dimension for reciprocal of dimensioned value.
constexpr rational(stype n=0, stype d=1)
Initialize from numerator and denominator.
Definition: rational.hpp:47
static constexpr dyndim_base pow(dim::rat p)
Dimension for rational power of dimensioned value.
static constexpr auto sum(statdim_base)
Dimension for sum of dimensioned values.
static constexpr dyndim_base diff(dyndim_base const &db)
Dimension for difference of dimensioned values.
constexpr bool operator==(basic_dim const &d) const
Definition: dim.hpp:227
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
constexpr statdim_base(dim dd)
Check for compatibility on contruction from dim.
static constexpr auto sqrt()
Dimension for square-root of dimensioned value.
Classes and functions supporting a model of physically dimensioned quantities.