test_registers.cpp
Go to the documentation of this file.
00001 #include "um7/registers.h"
00002 #include <gtest/gtest.h>
00003 
00004 #include <arpa/inet.h>
00005 
00006 
00007 TEST(ByteOrder, compare_with_htons)
00008 {
00009   // Arbitrary, just try a selection of values.
00010   for(uint16_t host_num = 0; host_num < 50000; host_num += 71) { 
00011     uint16_t net_num = htons(host_num);
00012     uint16_t memcpy_num = 0;
00013     um7::memcpy_network(&memcpy_num, &host_num, sizeof(host_num));
00014     EXPECT_EQ(memcpy_num, net_num);
00015   }
00016 }
00017 
00018 TEST(ByteOrder, compare_with_htonl)
00019 {
00020   for(uint32_t host_num = 0; host_num < 4000000000; host_num += 1299827) { 
00021     uint32_t net_num = htonl(host_num);
00022     uint32_t memcpy_num = 0;
00023     um7::memcpy_network(&memcpy_num, &host_num, sizeof(host_num));
00024     EXPECT_EQ(memcpy_num, net_num);
00025   }
00026 }
00027 
00028 TEST(Accessor, basic_int)
00029 {
00030   um7::Registers r;
00031   r.write_raw(5, "\x01\x02\x03\x04\x05\x06");
00032   
00033   um7::Accessor<uint16_t> u16(&r, 5, 3);
00034   EXPECT_EQ(0x0102, u16.get(0));
00035   EXPECT_EQ(0x0304, u16.get(1));
00036   EXPECT_EQ(0x0506, u16.get(2));
00037 
00038   um7::Accessor<uint16_t> u16n(&r, 6, 1);
00039   EXPECT_EQ(0x0506, u16n.get(0));
00040 
00041   um7::Accessor<uint32_t> u32(&r, 5, 3);
00042   EXPECT_EQ(0x01020304, u32.get(0));
00043 }
00044 
00045 TEST(Accessor, basic_float)
00046 {
00047   um7::Registers r;
00048   r.write_raw(10, "\x01\x02\x03\x04\x05\x06\x07\x08");
00049 
00050   um7::Accessor<float> f(&r, 10, 2);
00051   union {
00052     float val;
00053     uint32_t bytes;
00054   };
00055   bytes = 0x01020304;
00056   EXPECT_FLOAT_EQ(val, f.get(0));
00057   bytes = 0x05060708;
00058   EXPECT_FLOAT_EQ(val, f.get(1));
00059 }
00060 
00061 TEST(Accessor, scaled_int)
00062 {
00063   um7::Registers r;
00064   r.write_raw(11, "\x01\x02\x03\x04");
00065 
00066   const float scale(0.001);
00067   um7::Accessor<int16_t> i16(&r, 11, 2, scale);
00068   EXPECT_FLOAT_EQ(scale * 0x0102, i16.get_scaled(0));
00069   EXPECT_FLOAT_EQ(scale * 0x0304, i16.get_scaled(1));
00070 }
00071 
00072 TEST(Accessor, set_float)
00073 {
00074   um7::Registers r;
00075   r.mag_bias.set(0, 0.123);
00076   r.mag_bias.set_scaled(1, 0.987);
00077   r.mag_bias.set_scaled(2, 0.555);
00078 
00079   float check;
00080   um7::memcpy_network(&check, (float*)r.mag_bias.raw(), 4);
00081   EXPECT_FLOAT_EQ(0.123, check);
00082   um7::memcpy_network(&check, (float*)r.mag_bias.raw() + 1, 4);
00083   EXPECT_FLOAT_EQ(0.987, check);
00084   um7::memcpy_network(&check, (float*)r.mag_bias.raw() + 2, 4);
00085   EXPECT_FLOAT_EQ(0.555, check);
00086 }
00087 
00088 int main(int argc, char **argv)
00089 {
00090 testing::InitGoogleTest(&argc, argv);
00091 return RUN_ALL_TESTS();
00092 }
00093 


um7
Author(s): Mike Purvis , Alex Brown
autogenerated on Thu Jun 6 2019 19:34:53