test_registers.cpp
Go to the documentation of this file.
1 #include "um6/registers.h"
2 #include <gtest/gtest.h>
3 
4 #include <arpa/inet.h>
5 
6 
7 TEST(ByteOrder, compare_with_htons)
8 {
9  // Arbitrary, just try a selection of values.
10  for(uint16_t host_num = 0; host_num < 50000; host_num += 71)
11  {
12  uint16_t net_num = htons(host_num);
13  uint16_t memcpy_num = 0;
14  um6::memcpy_network(&memcpy_num, &host_num, sizeof(host_num));
15  EXPECT_EQ(memcpy_num, net_num);
16  }
17 }
18 
19 TEST(ByteOrder, compare_with_htonl)
20 {
21  for(uint32_t host_num = 0; host_num < 4000000000; host_num += 1299827)
22  {
23  uint32_t net_num = htonl(host_num);
24  uint32_t memcpy_num = 0;
25  um6::memcpy_network(&memcpy_num, &host_num, sizeof(host_num));
26  EXPECT_EQ(memcpy_num, net_num);
27  }
28 }
29 
30 TEST(Accessor, basic_int)
31 {
33  r.write_raw(5, "\x01\x02\x03\x04\x05\x06");
34 
35  um6::Accessor<uint16_t> u16(&r, 5, 3);
36  EXPECT_EQ(0x0102, u16.get(0));
37  EXPECT_EQ(0x0304, u16.get(1));
38  EXPECT_EQ(0x0506, u16.get(2));
39 
40  um6::Accessor<uint16_t> u16n(&r, 6, 1);
41  EXPECT_EQ(0x0506, u16n.get(0));
42 
43  um6::Accessor<uint32_t> u32(&r, 5, 3);
44  EXPECT_EQ(0x01020304, u32.get(0));
45 }
46 
47 TEST(Accessor, basic_float)
48 {
50  r.write_raw(10, "\x01\x02\x03\x04\x05\x06\x07\x08");
51 
52  um6::Accessor<float> f(&r, 10, 2);
53  union
54  {
55  float val;
56  uint32_t bytes;
57  };
58  bytes = 0x01020304;
59  EXPECT_FLOAT_EQ(val, f.get(0));
60  bytes = 0x05060708;
61  EXPECT_FLOAT_EQ(val, f.get(1));
62 }
63 
64 TEST(Accessor, scaled_int)
65 {
67  r.write_raw(11, "\x01\x02\x03\x04");
68 
69  const float scale(0.001);
70  um6::Accessor<int16_t> i16(&r, 11, 2, scale);
71  EXPECT_FLOAT_EQ(scale * 0x0102, i16.get_scaled(0));
72  EXPECT_FLOAT_EQ(scale * 0x0304, i16.get_scaled(1));
73 }
74 
75 TEST(Accessor, set_float)
76 {
78  r.mag_ref.set(0, 0.123);
79  r.mag_ref.set_scaled(1, 0.987);
80  r.mag_ref.set_scaled(2, 0.555);
81 
82  float check;
83  um6::memcpy_network(&check, (float*)r.mag_ref.raw(), 4);
84  EXPECT_FLOAT_EQ(0.123, check);
85  um6::memcpy_network(&check, (float*)r.mag_ref.raw() + 1, 4);
86  EXPECT_FLOAT_EQ(0.987, check);
87  um6::memcpy_network(&check, (float*)r.mag_ref.raw() + 2, 4);
88  EXPECT_FLOAT_EQ(0.555, check);
89 }
90 
91 int main(int argc, char **argv)
92 {
93  testing::InitGoogleTest(&argc, argv);
94  return RUN_ALL_TESTS();
95 }
ROSCPP_DECL bool check()
f
double get_scaled(uint16_t field) const
Definition: registers.h:138
const Accessor< float > mag_ref
Definition: registers.h:195
void write_raw(uint8_t register_index, std::string data)
Definition: registers.h:202
Provides the Registers class, which initializes with a suite of accessors suitable for reading and wr...
void * raw() const
Definition: registers.cpp:39
int main(int argc, char **argv)
void memcpy_network(void *dest, void *src, size_t count)
Definition: registers.h:65
void set_scaled(uint16_t field, double value) const
Definition: registers.h:149
TEST(ByteOrder, compare_with_htons)
RegT get(uint8_t field) const
Definition: registers.h:130
void set(uint8_t field, RegT value) const
Definition: registers.h:143


um6
Author(s): Mike Purvis
autogenerated on Thu Sep 26 2019 03:18:02