test_header.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2009, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 /* Author: Josh Faust */
31 
32 /*
33  * Test Header serialization/deserialization
34  */
35 
36 #include <gtest/gtest.h>
37 #include "ros/header.h"
38 
39 static char g_header_data1[] = "A=B";
40 static char g_header_data2[] = "AAAAAAAAAAAAA=BBBBBBB\nBBBBBBB";
41 
42 using namespace ros;
43 
44 TEST(Header, parse)
45 {
46  Header header;
47 
48  uint32_t total_len = 4 + sizeof(g_header_data1) + 4 + sizeof(g_header_data2);
49  boost::shared_array<uint8_t> buffer(new uint8_t[total_len]);
50 
51  char* ptr = (char*)buffer.get();
52  *(uint32_t*)ptr = sizeof(g_header_data1);
53  ptr += 4;
54  memcpy(ptr, g_header_data1, sizeof(g_header_data1));
55  ptr += sizeof(g_header_data1);
56 
57  *(uint32_t*)ptr = sizeof(g_header_data2);
58  ptr += 4;
59  memcpy(ptr, g_header_data2, sizeof(g_header_data2));
60  ptr += sizeof(g_header_data2);
61 
62  std::string error_msg;
63  ASSERT_TRUE(header.parse(buffer, total_len, error_msg));
64 
65  std::string val;
66  ASSERT_TRUE(header.getValue("A", val));
67  ASSERT_STREQ(val.c_str(), "B");
68 
69  ASSERT_TRUE(header.getValue("AAAAAAAAAAAAA", val));
70  ASSERT_STREQ(val.c_str(), "BBBBBBB\nBBBBBBB");
71 }
72 
73 TEST(Header, write)
74 {
75  M_string map;
76  map["haha"] = "hoho";
77  map["fasdf"] = "aaaaaaaaaaaaaaa";
78  map["02490\n254"] = "idsjowiejf\nioajfoiwje";
79 
81  uint32_t len;
82  Header::write(map, buffer, len);
83 
84  ASSERT_EQ(len, 4*3 + 3 + strlen("haha") + strlen("hoho") + strlen("fasdf") + strlen("aaaaaaaaaaaaaaa") + strlen("02490\n254") + strlen("idsjowiejf\nioajfoiwje"));
85 
86  Header header;
87  std::string error_msg;
88  ASSERT_TRUE(header.parse(buffer, len, error_msg));
89 
90  std::string val;
91  ASSERT_TRUE(header.getValue("haha", val));
92  ASSERT_STREQ(val.c_str(), "hoho");
93 
94  ASSERT_TRUE(header.getValue("fasdf", val));
95  ASSERT_STREQ(val.c_str(), "aaaaaaaaaaaaaaa");
96 
97  ASSERT_TRUE(header.getValue("02490\n254", val));
98  ASSERT_STREQ(val.c_str(), "idsjowiejf\nioajfoiwje");
99 }
100 
101 int
102 main(int argc, char** argv)
103 {
104  testing::InitGoogleTest(&argc, argv);
105  return RUN_ALL_TESTS();
106 }
107 
108 
std_msgs::Header * header(M &m)
TEST(Header, parse)
Definition: test_header.cpp:44
std::map< std::string, std::string > M_string
bool parse(const boost::shared_array< uint8_t > &buffer, uint32_t size, std::string &error_msg)
static void write(const M_string &key_vals, boost::shared_array< uint8_t > &buffer, uint32_t &size)
static char g_header_data1[]
Definition: test_header.cpp:39
static char g_header_data2[]
Definition: test_header.cpp:40
int main(int argc, char **argv)
bool getValue(const std::string &key, std::string &value) const


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:46