units
Use physical dimensions at compile-time or run-time.
Functions
rational-test.cpp File Reference

Test-cases for vnix::rat::rational. More...

#include "../vnix/rat.hpp"
#include "catch.hpp"
#include <sstream>
Include dependency graph for rational-test.cpp:

Go to the source code of this file.

Functions

 TEST_CASE ("Constructor from num and den works as expected.","[rational]")
 
 TEST_CASE ("Conversion-constructor works as expected.","[rational]")
 
 TEST_CASE ("Addition and subtraction work.","[rational]")
 
 TEST_CASE ("Reciprocal works as expected.","[rational]")
 
 TEST_CASE ("Multiplication and division work.","[rational]")
 
 TEST_CASE ("Encoding and decoding work as expected.","[rational]")
 
 TEST_CASE ("Comparison operators work as expected.","[rational]")
 
 TEST_CASE ("Unary operators work as expected.","[rational]")
 
 TEST_CASE ("Stream-output works as expected.","[rational]")
 

Detailed Description

Test-cases for vnix::rat::rational.

License: BSD three-clause; see LICENSE.

Definition in file rational-test.cpp.

Function Documentation

TEST_CASE ( "Constructor from num and den works as expected."  ,
""  [rational] 
)

Definition at line 13 of file rational-test.cpp.

13  {
14  rat8_t constexpr r1;
15  REQUIRE(!r1.to_bool());
16  REQUIRE(r1.to_int() == 0);
17  REQUIRE(r1.to_float() == 0);
18  REQUIRE(r1.to_double() == 0);
19 
20  rat8_t constexpr r2(3);
21  REQUIRE(r2.to_bool());
22  REQUIRE(r2.to_int() == 3);
23  REQUIRE(r2.to_float() == 3);
24  REQUIRE(r2.to_double() == 3);
25 
26  rat8_t constexpr r3(3, 2);
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);
31 
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);
37 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Conversion-constructor works as expected."  ,
""  [rational] 
)

Definition at line 40 of file rational-test.cpp.

40  {
41  rat8_t constexpr r1(3, 2);
42  rat16_t constexpr r2(r1);
43  REQUIRE(r2.to_double() == 1.5);
44  REQUIRE(r1 == r2);
45 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Addition and subtraction work."  ,
""  [rational] 
)

Definition at line 48 of file rational-test.cpp.

48  {
49  rat8_t r1(3, 2);
50  rat16_t r1a(1);
51  r1 += r1a;
52  REQUIRE(r1 == rat8_t(5, 2));
53  rat32_t r1s(1);
54  r1 -= r1s;
55  REQUIRE(r1 == rat8_t(3, 2));
56  rat16_t constexpr r2(-3, 4);
57  rat16_t constexpr r3(1, 6);
58  REQUIRE(r2 + r3 == rat16_t(-7, 12));
59  REQUIRE(r2 - r3 == rat16_t(-11, 12));
60 }
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
Definition: rat.hpp:21
rational< 9, 7 > rat16_t
Rational with nine bits for numerator and seven for denominator.
Definition: rat.hpp:24
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Reciprocal works as expected."  ,
""  [rational] 
)

Definition at line 63 of file rational-test.cpp.

63  {
64  rat8_t r1(-3, 2);
65  rat8_t r2(2, 3);
66  REQUIRE(r1.reciprocal() == -r2);
67  REQUIRE_NOTHROW(rat8_t(8).reciprocal());
68  REQUIRE_THROWS(rat8_t(9).reciprocal());
69 }
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
Definition: rat.hpp:21
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Multiplication and division work."  ,
""  [rational] 
)

Definition at line 72 of file rational-test.cpp.

72  {
73  rat8_t r1(-3, 2);
74  rat8_t r2(-1, 4);
75  REQUIRE(r1 * r2 == rat8_t(3, 8));
76  REQUIRE(r1 / r2 == rat8_t(6));
77  rat8_t r1m(1, 2);
78  r1 *= r1m;
79  REQUIRE(r1 == rat8_t(-3, 4));
80  rat8_t r1d(2, 3);
81  r1 /= r1d;
82  REQUIRE(r1 == rat8_t(-9, 8));
83 }
rational< 5, 3 > rat8_t
Rational with five bits for numerator and three for denominator.
Definition: rat.hpp:21
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Encoding and decoding work as expected."  ,
""  [rational] 
)

Definition at line 86 of file rational-test.cpp.

86  {
87  rat8_t r1(-3, 4);
88  uint8_t code = 0xE8 | 0x03;
89  REQUIRE(rat8_t::encode(r1) == code);
90  REQUIRE(rat8_t::decode(code) == r1);
91 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Comparison operators work as expected."  ,
""  [rational] 
)

Definition at line 94 of file rational-test.cpp.

94  {
95  rat8_t r1(1, 2);
96  rat8_t r2(-3, 8);
97  REQUIRE(r1 == r1);
98  REQUIRE(r1 != r2);
99  REQUIRE(r1 >= r1);
100  REQUIRE(r1 >= r2);
101  REQUIRE(r1 > r2);
102  REQUIRE(r2 <= r1);
103  REQUIRE(r2 < r1);
104 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Unary operators work as expected."  ,
""  [rational] 
)

Definition at line 107 of file rational-test.cpp.

107  {
108  rat8_t r1(4, 6);
109  rat8_t r2(-2, 3);
110  REQUIRE(r1 == +r1);
111  REQUIRE(r1 == -r2);
112 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function:

TEST_CASE ( "Stream-output works as expected."  ,
""  [rational] 
)

Definition at line 115 of file rational-test.cpp.

115  {
116  rat8_t r1 = 4;
117  rat8_t r2(-6, 8);
118  std::ostringstream oss1, oss2;
119  oss1 << r1;
120  oss2 << r2;
121  REQUIRE(oss1.str() == "4");
122  REQUIRE(oss2.str() == "-3/4");
123 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27

Here is the call graph for this function: