units
Use physical dimensions at compile-time or run-time.
encoding-test.cpp
Go to the documentation of this file.
1 /// @file test/encoding-test.cpp
2 /// @brief Test-cases for vnix::rat::encoding.
3 /// @copyright 2019 Thomas E. Vaughan; all rights reserved.
4 /// @license BSD three-clause; see LICENSE.
5 
6 #include "../vnix/rat/encoding.hpp"
7 #include "catch.hpp"
8 
9 using np8_t = vnix::rat::normalized_pair<5, 3>;
10 using np9_t = vnix::rat::normalized_pair<5, 4>;
11 using en8_t = vnix::rat::encoding<5, 3>;
12 using en9_t = vnix::rat::encoding<5, 4>;
13 
14 
15 TEST_CASE("Constants are computed correctly.", "[encoding]") {
16  using en = vnix::rat::encoding<5, 3>;
17  REQUIRE(en::BITS == 8);
18  REQUIRE(en::DNM_MASK == 0x07);
19  REQUIRE(en::NMR_MASK == 0xF8);
20 }
21 
22 
23 TEST_CASE("Limiting values are encoded and decoded.", "[encoding]") {
24  en8_t const e1(np8_t(-16, 1));
25  en8_t const e2(np8_t(+15, 1));
26  en8_t const e3(np8_t(+1, 8));
27  en9_t const e4(np9_t(-16, 1));
28  en9_t const e5(np9_t(+15, 1));
29  en9_t const e6(np9_t(+1, 16));
30 
31  REQUIRE(e1.n() == -16);
32  REQUIRE(e1.d() == 1);
33 
34  REQUIRE(e2.n() == +15);
35  REQUIRE(e2.d() == 1);
36 
37  REQUIRE(e3.n() == +1);
38  REQUIRE(e3.d() == 8);
39 
40  REQUIRE(e4.n() == -16);
41  REQUIRE(e4.d() == 1);
42 
43  REQUIRE(e5.n() == +15);
44  REQUIRE(e5.d() == 1);
45 
46  REQUIRE(e6.n() == +1);
47  REQUIRE(e6.d() == 16);
48 }
Numerator and denominator of a rational number as separate numbers, not encoded into the same word...
constexpr int_types< DNM_BITS >::UF d() const
Normalized denominator.
Definition: encoding.hpp:68
constexpr normalized_pair(S nn, S dd)
Initialize normalized numerator and denominator for encoding of rational number.
constexpr encoding(normalized_pair< NMR_BITS, DNM_BITS > p)
Initialize from normalized numerator and denominator.
Definition: encoding.hpp:52
Thomas E. Vaughan&#39;s public software.
Definition: rational.hpp:13
constexpr int_types< NMR_BITS >::SF n() const
Normalized numerator.
Definition: encoding.hpp:55
Classes and functions supporting a model of a fixed-precision rational number.
Definition: rational.hpp:17
Encoding of numerator and denominator into unsigned word.
Definition: encoding.hpp:19