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


um7
Author(s): Mike Purvis , Alex Brown
autogenerated on Tue Feb 11 2020 03:26:50