PointerTest.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002  * Copyright (C) 2014 by Ralf Kaestner                                        *
00003  * ralf.kaestner@gmail.com                                                    *
00004  *                                                                            *
00005  * This program is free software; you can redistribute it and/or modify       *
00006  * it under the terms of the Lesser GNU General Public License as published by*
00007  * the Free Software Foundation; either version 3 of the License, or          *
00008  * (at your option) any later version.                                        *
00009  *                                                                            *
00010  * This program is distributed in the hope that it will be useful,            *
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of             *
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the               *
00013  * Lesser GNU General Public License for more details.                        *
00014  *                                                                            *
00015  * You should have received a copy of the Lesser GNU General Public License   *
00016  * along with this program. If not, see <http://www.gnu.org/licenses/>.       *
00017  ******************************************************************************/
00018 
00019 #include <gtest/gtest.h>
00020 
00021 #include <variant_msgs/Test.h>
00022 
00023 #include <variant_topic_tools/ArrayMemberPointer.h>
00024 #include <variant_topic_tools/BuiltinPointer.h>
00025 #include <variant_topic_tools/MessageMemberPointer.h>
00026 
00027 using namespace variant_topic_tools;
00028 
00029 TEST(Pointer, Builtin) {
00030   BuiltinPointer<int> p1(new int());
00031   BuiltinPointer<int> p2;
00032   
00033   EXPECT_TRUE(p1);
00034   EXPECT_NO_THROW(*p1);
00035   
00036   EXPECT_FALSE(p2);
00037   EXPECT_ANY_THROW(*p2);
00038 }
00039 
00040 TEST(Pointer, Array) {
00041   boost::array<int, 3>* a1 = new boost::array<int, 3>();
00042   (*a1)[0] = 0;
00043   (*a1)[1] = 1;
00044   (*a1)[2] = 2;  
00045   ArrayMemberPointer<int[3]> p1(a1, 1);
00046   
00047   std::vector<int>* a2 = new std::vector<int>(3);
00048   (*a2)[0] = 0;
00049   (*a2)[1] = 1;
00050   (*a2)[2] = 2;  
00051   ArrayMemberPointer<int[]> p2(a2, 1);
00052   ArrayMemberPointer<int[]> p3(p2.getArray(), 2);
00053   
00054   EXPECT_EQ(a1, p1.getArray().get());
00055   EXPECT_EQ(1, p1.getIndex());
00056   EXPECT_EQ(1, *p1);
00057   
00058   EXPECT_EQ(a2, p2.getArray().get());
00059   EXPECT_EQ(1, p2.getIndex());
00060   EXPECT_EQ(1, *p2);
00061   
00062   EXPECT_EQ(a2, p3.getArray().get());
00063   EXPECT_EQ(2, p3.getIndex());
00064   EXPECT_EQ(2, *p3);
00065 }
00066 
00067 TEST(Pointer, Message) {
00068   variant_msgs::Test* m1 = new variant_msgs::Test();
00069   m1->builtin_int = 42;
00070   m1->builtin_string = "Test";
00071   
00072   MessageMemberPointer<variant_msgs::Test, int32_t> p1(m1,
00073     offsetof(variant_msgs::Test, builtin_int));
00074   MessageMemberPointer<variant_msgs::Test, std::string> p2(
00075     p1.getMessage(), offsetof(variant_msgs::Test, builtin_string));
00076   
00077   EXPECT_EQ(m1, p1.getMessage().get());
00078   EXPECT_EQ(offsetof(variant_msgs::Test, builtin_int), p1.getOffset());
00079   EXPECT_EQ(m1->builtin_int, *p1);
00080   
00081   EXPECT_EQ(m1, p2.getMessage().get());
00082   EXPECT_EQ(offsetof(variant_msgs::Test, builtin_string), p2.getOffset());
00083   EXPECT_EQ(m1->builtin_string, *p2);
00084 }
00085 
00086 TEST(Pointer, ArrayOfMessages) {
00087   variant_msgs::Test* m1 = new variant_msgs::Test();
00088   m1->string_array[1].data = "Test";
00089   
00090   MessageMemberPointer<variant_msgs::Test, std_msgs::String[3]>
00091     p1(m1, offsetof(variant_msgs::Test, string_array));
00092   ArrayMemberPointer<std_msgs::String[3]> p2(p1, 1);
00093   MessageMemberPointer<std_msgs::String, std::string>
00094     p3(p2, offsetof(std_msgs::String, data));
00095   
00096   EXPECT_EQ(m1, p1.getMessage().get());
00097   EXPECT_EQ(offsetof(variant_msgs::Test, string_array), p1.getOffset());
00098   
00099   EXPECT_EQ(&m1->string_array, p2.getArray().get());
00100   EXPECT_EQ(1, p2.getIndex());
00101   
00102   EXPECT_EQ(&m1->string_array[1], p3.getMessage().get());
00103   EXPECT_EQ(offsetof(std_msgs::String, data), p3.getOffset());
00104   
00105   EXPECT_NO_THROW(p1.reset());
00106   EXPECT_ANY_THROW(*p1);
00107   EXPECT_NO_THROW(*p2);
00108   EXPECT_NO_THROW(*p3);
00109   EXPECT_EQ("Test", *p3);
00110   EXPECT_NO_THROW(p2.reset());
00111   EXPECT_ANY_THROW(*p2);
00112   EXPECT_NO_THROW(*p3);
00113   EXPECT_EQ("Test", *p3);
00114   EXPECT_NO_THROW(p3.reset());  
00115 }


variant_topic_test
Author(s): Ralf Kaestner
autogenerated on Fri Aug 5 2016 06:06:49