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


um6
Author(s): Mike Purvis
autogenerated on Thu Jun 6 2019 19:02:20