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

Test-cases for vnix::units::dim. More...

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

Go to the source code of this file.

Functions

 TEST_CASE ("dim can be initialized from array.","[dim]")
 
 TEST_CASE ("Encoding and decoding works as expected.","[dim]")
 
 TEST_CASE ("Addition and subtraction work as expected.","[dim]")
 
 TEST_CASE ("Multiplication and division work as expected.","[dim]")
 

Detailed Description

Test-cases for vnix::units::dim.

License: BSD three-clause; see LICENSE.

Definition in file dim-test.cpp.

Function Documentation

TEST_CASE ( "dim can be initialized from array."  ,
""  [dim] 
)

Definition at line 14 of file dim-test.cpp.

14  {
15  std::array<dim::rat, dim::NUM_BASES> a;
16  for (unsigned i = 0; i < dim::NUM_BASES; ++i) { a[i] = i; }
17  dim d(a);
18  for (auto i : dim::off::array) { REQUIRE(d[i] == dim::rat(i)); }
19 }
static constexpr off const array[num_offs]
Array of enumerated offsets for iterating.
Model of a fixed-precision rational number.
Definition: rational.hpp:27
Number of bases for dimension.
Definition: dim.hpp:30

Here is the call graph for this function:

TEST_CASE ( "Encoding and decoding works as expected."  ,
""  [dim] 
)

Definition at line 22 of file dim-test.cpp.

22  {
23  std::array<dim::rat, dim::NUM_BASES> a;
24  uint64_t t = 0;
25  for (unsigned i = 0; i < dim::NUM_BASES; ++i) {
26  a[i] = i;
27  using rat = dim::rat;
28  enum { BITS = rat::BITS };
29  t |= ((rat::encode(i) & bit_range<uint64_t>(0, BITS)) << i * BITS);
30  }
31  dim d(a);
32  dim::word e = d.encode();
33  REQUIRE(e == t);
34  REQUIRE(dim(e) == d);
35 }
rational< 4, 2 > rat
Type of rational for dimensioned values.
Definition: dim.hpp:26
typename int_types< NUM_BITS >::US word
Type of unsigned word in which dimensions are encoded.
Definition: dim.hpp:35
basic_dim< dim_base_off > dim
Define dim as specification of basic_dim by way of YAML- and ERB-generated type dim_base_off.
Definition: dim.hpp:260
Number of bases for dimension.
Definition: dim.hpp:30

Here is the call graph for this function:

TEST_CASE ( "Addition and subtraction work as expected."  ,
""  [dim] 
)

Definition at line 38 of file dim-test.cpp.

38  {
39  std::array<dim::rat, dim::NUM_BASES> a, b, c, d;
40  for (unsigned i = 0; i < dim::NUM_BASES; ++i) {
41  a[i] = i;
42  std::cout << "a[i]=" << a[i] << std::endl;
43  b[i] = dim::rat(-1, 2);
44  std::cout << "b[i]=" << b[i] << std::endl;
45  c[i] = a[i] + b[i];
46  std::cout << "c[i]=" << c[i] << std::endl;
47  d[i] = a[i] / 2 - b[i];
48  std::cout << "d[i]=" << d[i] << std::endl;
49  }
50  dim x(a), y(b), z1(c), z2(d);
51  REQUIRE(x + y == z1);
52  REQUIRE(x / 2 - y == z2);
53 }
rational< 4, 2 > rat
Type of rational for dimensioned values.
Definition: dim.hpp:26
Number of bases for dimension.
Definition: dim.hpp:30

Here is the call graph for this function:

TEST_CASE ( "Multiplication and division work as expected."  ,
""  [dim] 
)

Definition at line 56 of file dim-test.cpp.

56  {
57  std::array<dim::rat, dim::NUM_BASES> a, c, d;
58  dim::rat const b(-1, 2);
59  for (unsigned i = 0; i < dim::NUM_BASES; ++i) {
60  a[i] = i;
61  c[i] = a[i] * b;
62  d[i] = a[i] / b;
63  }
64  dim x(a), z1(c), z2(d);
65  REQUIRE(x * b == z1);
66  REQUIRE(x / b == z2);
67 }
Model of a fixed-precision rational number.
Definition: rational.hpp:27
Number of bases for dimension.
Definition: dim.hpp:30

Here is the call graph for this function: