test_header.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2009, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 /* Author: Josh Faust */
00031 
00032 /*
00033  * Test Header serialization/deserialization
00034  */
00035 
00036 #include <gtest/gtest.h>
00037 #include "ros/header.h"
00038 
00039 static char g_header_data1[] = "A=B";
00040 static char g_header_data2[] = "AAAAAAAAAAAAA=BBBBBBB\nBBBBBBB";
00041 
00042 using namespace ros;
00043 
00044 TEST(Header, parse)
00045 {
00046   Header header;
00047 
00048   uint32_t total_len = 4 + sizeof(g_header_data1) + 4 + sizeof(g_header_data2);
00049   boost::shared_array<uint8_t> buffer(new uint8_t[total_len]);
00050 
00051   char* ptr = (char*)buffer.get();
00052   *(uint32_t*)ptr = sizeof(g_header_data1);
00053   ptr += 4;
00054   memcpy(ptr, g_header_data1, sizeof(g_header_data1));
00055   ptr += sizeof(g_header_data1);
00056 
00057   *(uint32_t*)ptr = sizeof(g_header_data2);
00058   ptr += 4;
00059   memcpy(ptr, g_header_data2, sizeof(g_header_data2));
00060   ptr += sizeof(g_header_data2);
00061 
00062   std::string error_msg;
00063   ASSERT_TRUE(header.parse(buffer, total_len, error_msg));
00064 
00065   std::string val;
00066   ASSERT_TRUE(header.getValue("A", val));
00067   ASSERT_STREQ(val.c_str(), "B");
00068 
00069   ASSERT_TRUE(header.getValue("AAAAAAAAAAAAA", val));
00070   ASSERT_STREQ(val.c_str(), "BBBBBBB\nBBBBBBB");
00071 }
00072 
00073 TEST(Header, write)
00074 {
00075   M_string map;
00076   map["haha"] = "hoho";
00077   map["fasdf"] = "aaaaaaaaaaaaaaa";
00078   map["02490\n254"] = "idsjowiejf\nioajfoiwje";
00079 
00080   boost::shared_array<uint8_t> buffer;
00081   uint32_t len;
00082   Header::write(map, buffer, len);
00083 
00084   ASSERT_EQ(len, 4*3 + 3 + strlen("haha") + strlen("hoho") + strlen("fasdf") + strlen("aaaaaaaaaaaaaaa") + strlen("02490\n254") + strlen("idsjowiejf\nioajfoiwje"));
00085 
00086   Header header;
00087   std::string error_msg;
00088   ASSERT_TRUE(header.parse(buffer, len, error_msg));
00089 
00090   std::string val;
00091   ASSERT_TRUE(header.getValue("haha", val));
00092   ASSERT_STREQ(val.c_str(), "hoho");
00093 
00094   ASSERT_TRUE(header.getValue("fasdf", val));
00095   ASSERT_STREQ(val.c_str(), "aaaaaaaaaaaaaaa");
00096 
00097   ASSERT_TRUE(header.getValue("02490\n254", val));
00098   ASSERT_STREQ(val.c_str(), "idsjowiejf\nioajfoiwje");
00099 }
00100 
00101 int
00102 main(int argc, char** argv)
00103 {
00104   testing::InitGoogleTest(&argc, argv);
00105   return RUN_ALL_TESTS();
00106 }
00107 
00108 


test_roscpp
Author(s): Morgan Quigley, Josh Faust, Brian Gerkey, Troy Straszheim
autogenerated on Fri Aug 28 2015 12:33:59