float_spec.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
3  */
4 
5 #include <gtest/gtest.h>
6 #include <limits>
9 
10 
11 TEST(FloatSpec, Sizes)
12 {
17 }
18 
19 TEST(FloatSpec, Limits)
20 {
21  using uavcan::FloatSpec;
24 
25  typedef FloatSpec<16, CastModeSaturate> F16S;
26  typedef FloatSpec<32, CastModeTruncate> F32T;
27  typedef FloatSpec<64, CastModeSaturate> F64S;
28 
29  ASSERT_FALSE(F16S::IsExactRepresentation);
30  ASSERT_FLOAT_EQ(65504.0F, F16S::max());
31  ASSERT_FLOAT_EQ(9.77e-04F, F16S::epsilon());
32 
33  ASSERT_TRUE(F32T::IsExactRepresentation);
34  ASSERT_FLOAT_EQ(std::numeric_limits<float>::max(), F32T::max());
35  ASSERT_FLOAT_EQ(std::numeric_limits<float>::epsilon(), F32T::epsilon());
36 
37  ASSERT_TRUE(F64S::IsExactRepresentation);
38  ASSERT_DOUBLE_EQ(std::numeric_limits<double>::max(), F64S::max());
39  ASSERT_DOUBLE_EQ(std::numeric_limits<double>::epsilon(), F64S::epsilon());
40 }
41 
42 TEST(FloatSpec, Basic)
43 {
44  using uavcan::FloatSpec;
47  using uavcan::StorageType;
48 
49  typedef FloatSpec<16, CastModeSaturate> F16S;
50  typedef FloatSpec<16, CastModeTruncate> F16T;
51  typedef FloatSpec<32, CastModeSaturate> F32S;
52  typedef FloatSpec<32, CastModeTruncate> F32T;
53  typedef FloatSpec<64, CastModeSaturate> F64S;
54  typedef FloatSpec<64, CastModeTruncate> F64T;
55 
56  static const long double Values[] =
57  {
58  0.0,
59  1.0,
60  M_PI,
61  123,
62  -123,
63  99999,
64  -999999,
67  std::numeric_limits<double>::infinity(),
68  -std::numeric_limits<double>::infinity(),
69  nanl("")
70  };
71  static const int NumValues = int(sizeof(Values) / sizeof(Values[0]));
72 
73  static const long double ValuesF16S[] =
74  {
75  0.0,
76  1.0,
77  3.140625,
78  123,
79  -123,
80  F16S::max(),
81  -F16S::max(),
82  F16S::max(),
83  -F16S::max(),
84  std::numeric_limits<F16S::StorageType>::infinity(),
85  -std::numeric_limits<F16S::StorageType>::infinity(),
86  nanl("")
87  };
88  static const long double ValuesF16T[] =
89  {
90  0.0,
91  1.0,
92  3.140625,
93  123,
94  -123,
95  std::numeric_limits<F16S::StorageType>::infinity(),
96  -std::numeric_limits<F16S::StorageType>::infinity(),
97  std::numeric_limits<F16S::StorageType>::infinity(),
98  -std::numeric_limits<F16S::StorageType>::infinity(),
99  std::numeric_limits<F16S::StorageType>::infinity(),
100  -std::numeric_limits<F16S::StorageType>::infinity(),
101  nanl("")
102  };
103 
104  /*
105  * Writing
106  */
107  uavcan::StaticTransferBuffer<NumValues * (2 + 4 + 8) * 2> buf;
108  uavcan::BitStream bs_wr(buf);
109  uavcan::ScalarCodec sc_wr(bs_wr);
110 
111  for (int i = 0; i < NumValues; i++)
112  {
113  ASSERT_EQ(1, F16S::encode(float(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
114  ASSERT_EQ(1, F16T::encode(float(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
115  ASSERT_EQ(1, F32S::encode(float(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
116  ASSERT_EQ(1, F32T::encode(float(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
117  ASSERT_EQ(1, F64S::encode(double(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
118  ASSERT_EQ(1, F64T::encode(double(Values[i]), sc_wr, uavcan::TailArrayOptDisabled));
119  }
120 
121  ASSERT_EQ(0, F16S::encode(0, sc_wr, uavcan::TailArrayOptDisabled)); // Out of buffer space now
122 
123  /*
124  * Reading
125  */
126  uavcan::BitStream bs_rd(buf);
127  uavcan::ScalarCodec sc_rd(bs_rd);
128 
129 #define CHECK(FloatType, expected_value) \
130  do { \
131  StorageType<FloatType>::Type var = StorageType<FloatType>::Type(); \
132  ASSERT_EQ(1, FloatType::decode(var, sc_rd, uavcan::TailArrayOptDisabled)); \
133  if (!std::isnan(expected_value)) { \
134  ASSERT_DOUBLE_EQ(expected_value, var); } \
135  else { \
136  ASSERT_EQ(!!std::isnan(expected_value), !!std::isnan(var)); } \
137  } while (0)
138 
139  for (int i = 0; i < NumValues; i++)
140  {
141  CHECK(F16S, float(ValuesF16S[i]));
142  CHECK(F16T, float(ValuesF16T[i]));
143  CHECK(F32S, float(Values[i]));
144  CHECK(F32T, float(Values[i]));
145  CHECK(F64S, double(Values[i]));
146  CHECK(F64T, double(Values[i]));
147  }
148 
149 #undef CHECK
150 }
151 
152 TEST(FloatSpec, Float16Representation)
153 {
154  using uavcan::FloatSpec;
157 
158  typedef FloatSpec<16, CastModeSaturate> F16S;
159  typedef FloatSpec<16, CastModeTruncate> F16T;
160 
162  uavcan::BitStream bs_wr(buf);
163  uavcan::ScalarCodec sc_wr(bs_wr);
164 
165  ASSERT_EQ(1, F16S::encode(0.0, sc_wr, uavcan::TailArrayOptDisabled));
166  ASSERT_EQ(1, F16S::encode(1.0, sc_wr, uavcan::TailArrayOptDisabled));
167  ASSERT_EQ(1, F16S::encode(-2.0, sc_wr, uavcan::TailArrayOptDisabled));
168  ASSERT_EQ(1, F16T::encode(999999, sc_wr, uavcan::TailArrayOptDisabled)); // +inf
169  ASSERT_EQ(1, F16S::encode(-999999, sc_wr, uavcan::TailArrayOptDisabled)); // -max
170  ASSERT_EQ(1, F16S::encode(float(nan("")), sc_wr, uavcan::TailArrayOptDisabled)); // nan
171 
172  ASSERT_EQ(0, F16S::encode(0, sc_wr, uavcan::TailArrayOptDisabled)); // Out of buffer space now
173 
174  // Keep in mind that this is LITTLE ENDIAN representation
175  static const std::string Reference =
176  "00000000 00000000 " // 0.0
177  "00000000 00111100 " // 1.0
178  "00000000 11000000 " // -2.0
179  "00000000 01111100 " // +inf
180  "11111111 11111011 " // -max
181  "11111111 01111111"; // nan
182 
183  ASSERT_EQ(Reference, bs_wr.toString());
184 }
types.hpp
check
ROSCPP_DECL bool check()
uavcan::BitStream
Definition: bit_stream.hpp:31
uavcan::FloatSpec
Definition: float_spec.hpp:130
uavcan::NativeFloatSelector::Type
Select<(sizeof(float) *8 >=BitLen), float, typename Select<(sizeof(double) *8 >=BitLen), double, typename Select<(sizeof(long double) *8 >=BitLen), long double, ErrorNoSuchFloat >::Result >::Result >::Result Type
Definition: float_spec.hpp:29
uavcan::StaticAssert
struct UAVCAN_EXPORT StaticAssert
Definition: templates.hpp:29
uavcan::max
const UAVCAN_EXPORT T & max(const T &a, const T &b)
Definition: templates.hpp:291
uavcan::CastModeTruncate
@ CastModeTruncate
Definition: type_util.hpp:17
uavcan::StaticTransferBuffer
Definition: transfer_buffer.hpp:50
int
int
Definition: libstubs.cpp:120
CHECK
#define CHECK(FloatType, expected_value)
TEST
TEST(FloatSpec, Sizes)
Definition: float_spec.cpp:11
uavcan::TailArrayOptDisabled
@ TailArrayOptDisabled
Definition: type_util.hpp:22
transfer_buffer.hpp
uavcan::StorageType
Definition: type_util.hpp:51
uavcan::ScalarCodec
Definition: scalar_codec.hpp:20
uavcan::CastModeSaturate
@ CastModeSaturate
Definition: type_util.hpp:17


uavcan_communicator
Author(s):
autogenerated on Fri Dec 13 2024 03:10:02