test_parser.cpp
Go to the documentation of this file.
00001 // Bring in my package's API, which is what I'm testing
00002 #include <boost/property_tree/ptree.hpp>
00003 #include <canopen_master/objdict.h>
00004 
00005 // Bring in gtest
00006 #include <gtest/gtest.h>
00007 
00008 
00009 template<typename T> canopen::HoldAny parse_int(boost::property_tree::iptree &pt, const std::string &key);
00010 
00011 template<typename T> canopen::HoldAny prepare_test(const std::string &str){
00012     boost::property_tree::iptree pt;
00013     pt.put("test", str);
00014     return parse_int<T>(pt, "test");
00015 }
00016 
00017 template<typename T> class TestHexTypes :  public ::testing::Test{
00018 public:
00019     static void test_hex(const T& val, const std::string &str){
00020         EXPECT_EQ(val, prepare_test<T>(str).template get<T>());
00021     }
00022     static void test_hex_node(const T& val, const std::string &str, const uint8_t offset){
00023         EXPECT_EQ(val, canopen::NodeIdOffset<T>::apply(prepare_test<T>(str),offset));
00024     }
00025 };
00026 
00027 typedef ::testing::Types<uint8_t, uint16_t, uint32_t, uint64_t> PosTypes;
00028 
00029 TYPED_TEST_CASE(TestHexTypes, PosTypes);
00030 
00031 TYPED_TEST(TestHexTypes, checkZero){
00032     TestFixture::test_hex(0,"0");
00033     TestFixture::test_hex(0,"0x0");
00034 
00035     for(uint8_t i = 0; i <=127; ++i){
00036         TestFixture::test_hex_node(0,"0",i);
00037         TestFixture::test_hex_node(0,"0x0",i);
00038         TestFixture::test_hex_node(0+i,"$NODEID+0",i);
00039         TestFixture::test_hex_node(0+i,"$NODEID+0x0",i);
00040     }
00041 }
00042 TEST(TestHex, checkCamelCase){
00043     TestHexTypes<uint16_t>::test_hex(0xABCD, "0xABCD");
00044     TestHexTypes<uint16_t>::test_hex(0xABCD, "0xabcd");
00045     TestHexTypes<uint16_t>::test_hex(0xABCD, "0xAbCd");
00046     TestHexTypes<uint16_t>::test_hex(0xABCD, "0xabCD");
00047 }
00048 
00049 TEST(TestHex, checkNodeCamelCase){
00050     for(uint8_t i = 0; i <=127; ++i){
00051         TestHexTypes<uint16_t>::test_hex_node(i," $NODEID",i);
00052         TestHexTypes<uint16_t>::test_hex_node(i," $NODeID",i);
00053         TestHexTypes<uint16_t>::test_hex_node(i," $NodeId",i);
00054         TestHexTypes<uint16_t>::test_hex_node(i," $NodeID",i);
00055         TestHexTypes<uint16_t>::test_hex_node(i," $nodeID",i);
00056     }
00057 }
00058 
00059 TEST(TestHex, checkSpaces){
00060     TestHexTypes<uint16_t>::test_hex(0xABCD, " 0xABCD ");
00061     TestHexTypes<uint16_t>::test_hex(0xABCD, "0xABCD ");
00062     TestHexTypes<uint16_t>::test_hex(0xABCD, " 0xABCD");
00063 }
00064 
00065 TEST(TestHex, checkNodeSpaces){
00066     for(uint8_t i = 0; i <=127; ++i){
00067         TestHexTypes<uint16_t>::test_hex_node(i," $NODEID ",i);
00068         TestHexTypes<uint16_t>::test_hex_node(i," $NODEID",i);
00069         TestHexTypes<uint16_t>::test_hex_node(i,"$NODEID ",i);
00070         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID + 1",i);
00071         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID+ 1",i);
00072         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID+1",i);
00073         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID +1",i);
00074         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID +0x1 ",i);
00075         TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID + 0x1",i);
00076     }
00077 }
00078 TEST(TestHex, checkCommonObjects){
00079     for(uint8_t i = 0; i <=127; ++i){
00080         TestHexTypes<uint32_t>::test_hex_node( 0x80+i,"$NODEID+0x80",i); // EMCY
00081 
00082         TestHexTypes<uint32_t>::test_hex_node(0x180+i,"$NODEID+0x180",i); //TPDO1
00083         TestHexTypes<uint32_t>::test_hex_node(0x200+i,"$NODEID+0x200",i); //RPDO1
00084 
00085         TestHexTypes<uint32_t>::test_hex_node(0x280+i,"$NODEID+0x280",i); //TPDO2
00086         TestHexTypes<uint32_t>::test_hex_node(0x300+i,"$NODEID+0x300",i); //RPDO2
00087 
00088         TestHexTypes<uint32_t>::test_hex_node(0x380+i,"$NODEID+0x380",i); //TPDO3
00089         TestHexTypes<uint32_t>::test_hex_node(0x400+i,"$NODEID+0x400",i); //RPDO3
00090 
00091         TestHexTypes<uint32_t>::test_hex_node(0x480+i,"$NODEID+0x480",i); //TPDO4
00092         TestHexTypes<uint32_t>::test_hex_node(0x500+i,"$NODEID+0x500",i); //RPDO4
00093 
00094         TestHexTypes<uint32_t>::test_hex_node(0x580+i,"$NODEID+0x580",i); // TSDO
00095         TestHexTypes<uint32_t>::test_hex_node(0x600+i,"$NODEID+0x600",i); // RSDO
00096 
00097         TestHexTypes<uint32_t>::test_hex_node(0x700+i,"$NODEID+0x700",i); //NMT
00098     }
00099 }
00100 
00101 void set_access( canopen::ObjectDict::Entry &entry, std::string access);
00102 
00103 void testAccess(bool c, bool r, bool w, const char* variants[]){
00104   canopen::ObjectDict::Entry entry;
00105   for(const char ** v = variants; *v; ++v){
00106       SCOPED_TRACE(*v);
00107       entry.constant = !c;
00108       entry.readable = !r;
00109       entry.writable = !w;
00110 
00111       set_access(entry, *v);
00112 
00113       ASSERT_EQ(c, entry.constant);
00114       ASSERT_EQ(r, entry.readable);
00115       ASSERT_EQ(w, entry.writable);
00116   }
00117 }
00118 
00119 TEST(TestAccessString, TestRO)
00120 {
00121   const char* variants[] = {"ro", "Ro", "rO", "RO", 0};
00122   testAccess(false, true, false, variants);
00123 }
00124 
00125 TEST(TestAccessString, TestWO)
00126 {
00127   const char* variants[] = {"wo", "Wo", "wO", "WO", 0};
00128   testAccess(false, false, true, variants);
00129 }
00130 
00131 TEST(TestAccessString, TestRW)
00132 {
00133   const char* variants[] = {
00134       "rw" , "Rw" , "rW" , "Rw",
00135       "rwr", "Rwr", "rWr", "Rwr",
00136       "rwR", "RwR", "rWR", "RwR",
00137       "rww", "Rww", "rWw", "Rww",
00138       "rwW", "RwW", "rWW", "RwW",
00139       0};
00140   testAccess(false, true, true, variants);
00141 }
00142 
00143 TEST(TestAccessString, TestConst)
00144 {
00145   const char* variants[] = {
00146       "const" , "Const" , "CONST", 0};
00147   testAccess(true, true, false, variants);
00148 }
00149 
00150 
00151 // Run all the tests that were declared with TEST()
00152 int main(int argc, char **argv){
00153     testing::InitGoogleTest(&argc, argv);
00154     return RUN_ALL_TESTS();
00155 }


canopen_master
Author(s): Mathias Lüdtke
autogenerated on Sun Sep 3 2017 03:10:42