Test-cases for vnix::rat::rational.
More...
#include "../vnix/rat.hpp"
#include "catch.hpp"
#include <sstream>
Go to the source code of this file.
Test-cases for vnix::rat::rational.
- Copyright
- 2019 Thomas E. Vaughan; all rights reserved.
- License: BSD three-clause; see LICENSE.
Definition in file rational-test.cpp.
TEST_CASE |
( |
"Constructor from num and den works as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 13 of file rational-test.cpp.
15 REQUIRE(!r1.to_bool());
16 REQUIRE(r1.to_int() == 0);
17 REQUIRE(r1.to_float() == 0);
18 REQUIRE(r1.to_double() == 0);
21 REQUIRE(r2.to_bool());
22 REQUIRE(r2.to_int() == 3);
23 REQUIRE(r2.to_float() == 3);
24 REQUIRE(r2.to_double() == 3);
27 REQUIRE(r3.to_bool());
28 REQUIRE_THROWS(r3.to_int());
29 REQUIRE(r3.to_float() == 1.5);
30 REQUIRE(r3.to_double() == 1.5);
32 rat8_t constexpr r4(4, -4);
33 REQUIRE(r4.to_bool());
34 REQUIRE(r4.to_int() == -1);
35 REQUIRE(r4.to_float() == -1);
36 REQUIRE(r4.to_double() == -1);
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Conversion-constructor works as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 40 of file rational-test.cpp.
43 REQUIRE(r2.to_double() == 1.5);
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Addition and subtraction work." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 48 of file rational-test.cpp.
52 REQUIRE(r1 ==
rat8_t(5, 2));
55 REQUIRE(r1 ==
rat8_t(3, 2));
58 REQUIRE(r2 + r3 ==
rat16_t(-7, 12));
59 REQUIRE(r2 - r3 ==
rat16_t(-11, 12));
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
rational< 9, 7 > rat16_t
Rational with nine bits for numerator and seven for denominator.
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Reciprocal works as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 63 of file rational-test.cpp.
66 REQUIRE(r1.reciprocal() == -r2);
67 REQUIRE_NOTHROW(
rat8_t(8).reciprocal());
68 REQUIRE_THROWS(
rat8_t(9).reciprocal());
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Multiplication and division work." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 72 of file rational-test.cpp.
75 REQUIRE(r1 * r2 ==
rat8_t(3, 8));
76 REQUIRE(r1 / r2 ==
rat8_t(6));
79 REQUIRE(r1 ==
rat8_t(-3, 4));
82 REQUIRE(r1 ==
rat8_t(-9, 8));
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Encoding and decoding work as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 86 of file rational-test.cpp.
88 uint8_t code = 0xE8 | 0x03;
89 REQUIRE(rat8_t::encode(r1) == code);
90 REQUIRE(rat8_t::decode(code) == r1);
Model of a fixed-precision rational number.
TEST_CASE |
( |
"Comparison operators work as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
TEST_CASE |
( |
"Unary operators work as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
TEST_CASE |
( |
"Stream-output works as expected." |
, |
|
|
"" |
[rational] |
|
) |
| |
Definition at line 115 of file rational-test.cpp.
118 std::ostringstream oss1, oss2;
121 REQUIRE(oss1.str() ==
"4");
122 REQUIRE(oss2.str() ==
"-3/4");
Model of a fixed-precision rational number.