test_parser.cpp
Go to the documentation of this file.
1 // Bring in my package's API, which is what I'm testing
2 #include <boost/property_tree/ptree.hpp>
4 
5 // Bring in gtest
6 #include <gtest/gtest.h>
7 
8 
9 template<typename T> canopen::HoldAny parse_int(boost::property_tree::iptree &pt, const std::string &key);
10 
11 template<typename T> canopen::HoldAny prepare_test(const std::string &str){
12  boost::property_tree::iptree pt;
13  pt.put("test", str);
14  return parse_int<T>(pt, "test");
15 }
16 
17 template<typename T> class TestHexTypes : public ::testing::Test{
18 public:
19  static void test_hex(const T& val, const std::string &str){
20  EXPECT_EQ(val, prepare_test<T>(str).template get<T>());
21  }
22  static void test_hex_node(const T& val, const std::string &str, const uint8_t offset){
23  EXPECT_EQ(val, canopen::NodeIdOffset<T>::apply(prepare_test<T>(str),offset));
24  }
25 };
26 
27 typedef ::testing::Types<uint8_t, uint16_t, uint32_t, uint64_t> PosTypes;
28 
30 
32  TestFixture::test_hex(0,"0");
33  TestFixture::test_hex(0,"0x0");
34 
35  for(uint8_t i = 0; i <=127; ++i){
36  TestFixture::test_hex_node(0,"0",i);
37  TestFixture::test_hex_node(0,"0x0",i);
38  TestFixture::test_hex_node(0+i,"$NODEID+0",i);
39  TestFixture::test_hex_node(0+i,"$NODEID+0x0",i);
40  }
41 }
42 TEST(TestHex, checkCamelCase){
43  TestHexTypes<uint16_t>::test_hex(0xABCD, "0xABCD");
44  TestHexTypes<uint16_t>::test_hex(0xABCD, "0xabcd");
45  TestHexTypes<uint16_t>::test_hex(0xABCD, "0xAbCd");
46  TestHexTypes<uint16_t>::test_hex(0xABCD, "0xabCD");
47 }
48 
49 TEST(TestHex, checkNodeCamelCase){
50  for(uint8_t i = 0; i <=127; ++i){
56  }
57 }
58 
59 TEST(TestHex, checkSpaces){
60  TestHexTypes<uint16_t>::test_hex(0xABCD, " 0xABCD ");
61  TestHexTypes<uint16_t>::test_hex(0xABCD, "0xABCD ");
62  TestHexTypes<uint16_t>::test_hex(0xABCD, " 0xABCD");
63 }
64 
65 TEST(TestHex, checkNodeSpaces){
66  for(uint8_t i = 0; i <=127; ++i){
70  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID + 1",i);
71  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID+ 1",i);
72  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID+1",i);
73  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID +1",i);
74  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID +0x1 ",i);
75  TestHexTypes<uint16_t>::test_hex_node(i+1,"$NODEID + 0x1",i);
76  }
77 }
78 TEST(TestHex, checkCommonObjects){
79  for(uint8_t i = 0; i <=127; ++i){
80  TestHexTypes<uint32_t>::test_hex_node( 0x80+i,"$NODEID+0x80",i); // EMCY
81 
82  TestHexTypes<uint32_t>::test_hex_node(0x180+i,"$NODEID+0x180",i); //TPDO1
83  TestHexTypes<uint32_t>::test_hex_node(0x200+i,"$NODEID+0x200",i); //RPDO1
84 
85  TestHexTypes<uint32_t>::test_hex_node(0x280+i,"$NODEID+0x280",i); //TPDO2
86  TestHexTypes<uint32_t>::test_hex_node(0x300+i,"$NODEID+0x300",i); //RPDO2
87 
88  TestHexTypes<uint32_t>::test_hex_node(0x380+i,"$NODEID+0x380",i); //TPDO3
89  TestHexTypes<uint32_t>::test_hex_node(0x400+i,"$NODEID+0x400",i); //RPDO3
90 
91  TestHexTypes<uint32_t>::test_hex_node(0x480+i,"$NODEID+0x480",i); //TPDO4
92  TestHexTypes<uint32_t>::test_hex_node(0x500+i,"$NODEID+0x500",i); //RPDO4
93 
94  TestHexTypes<uint32_t>::test_hex_node(0x580+i,"$NODEID+0x580",i); // TSDO
95  TestHexTypes<uint32_t>::test_hex_node(0x600+i,"$NODEID+0x600",i); // RSDO
96 
97  TestHexTypes<uint32_t>::test_hex_node(0x700+i,"$NODEID+0x700",i); //NMT
98  }
99 }
100 
101 void set_access( canopen::ObjectDict::Entry &entry, std::string access);
102 
103 void testAccess(bool c, bool r, bool w, const char* variants[]){
105  for(const char ** v = variants; *v; ++v){
106  SCOPED_TRACE(*v);
107  entry.constant = !c;
108  entry.readable = !r;
109  entry.writable = !w;
110 
111  set_access(entry, *v);
112 
113  ASSERT_EQ(c, entry.constant);
114  ASSERT_EQ(r, entry.readable);
115  ASSERT_EQ(w, entry.writable);
116  }
117 }
118 
119 TEST(TestAccessString, TestRO)
120 {
121  const char* variants[] = {"ro", "Ro", "rO", "RO", 0};
122  testAccess(false, true, false, variants);
123 }
124 
125 TEST(TestAccessString, TestWO)
126 {
127  const char* variants[] = {"wo", "Wo", "wO", "WO", 0};
128  testAccess(false, false, true, variants);
129 }
130 
131 TEST(TestAccessString, TestRW)
132 {
133  const char* variants[] = {
134  "rw" , "Rw" , "rW" , "Rw",
135  "rwr", "Rwr", "rWr", "Rwr",
136  "rwR", "RwR", "rWR", "RwR",
137  "rww", "Rww", "rWw", "Rww",
138  "rwW", "RwW", "rWW", "RwW",
139  0};
140  testAccess(false, true, true, variants);
141 }
142 
143 TEST(TestAccessString, TestConst)
144 {
145  const char* variants[] = {
146  "const" , "Const" , "CONST", 0};
147  testAccess(true, true, false, variants);
148 }
149 
150 
151 // Run all the tests that were declared with TEST()
152 int main(int argc, char **argv){
153  testing::InitGoogleTest(&argc, argv);
154  return RUN_ALL_TESTS();
155 }
int main(int argc, char **argv)
canopen::HoldAny parse_int(boost::property_tree::iptree &pt, const std::string &key)
Definition: objdict.cpp:139
::testing::Types< uint8_t, uint16_t, uint32_t, uint64_t > PosTypes
Definition: test_parser.cpp:27
TYPED_TEST_CASE(TestHexTypes, PosTypes)
static void test_hex(const T &val, const std::string &str)
Definition: test_parser.cpp:19
void testAccess(bool c, bool r, bool w, const char *variants[])
TEST(TestHex, checkCamelCase)
Definition: test_parser.cpp:42
static void test_hex_node(const T &val, const std::string &str, const uint8_t offset)
Definition: test_parser.cpp:22
void set_access(canopen::ObjectDict::Entry &entry, std::string access)
Definition: objdict.cpp:84
canopen::HoldAny prepare_test(const std::string &str)
Definition: test_parser.cpp:11
TYPED_TEST(TestHexTypes, checkZero)
Definition: test_parser.cpp:31


canopen_master
Author(s): Mathias Lüdtke
autogenerated on Fri May 14 2021 02:59:41