$search
00001 /*************************************************************************** 00002 tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 enum_type_test.cpp 00003 00004 enum_type_test.cpp - description 00005 ------------------- 00006 begin : Tue September 07 2010 00007 copyright : (C) 2010 The SourceWorks 00008 email : peter@thesourceworks.com 00009 00010 *************************************************************************** 00011 * * 00012 * This program is free software; you can redistribute it and/or modify * 00013 * it under the terms of the GNU General Public License as published by * 00014 * the Free Software Foundation; either version 2 of the License, or * 00015 * (at your option) any later version. * 00016 * * 00017 ***************************************************************************/ 00018 00019 00020 #include "unit.hpp" 00021 #include <rtt-fwd.hpp> 00022 #include <internal/DataSources.hpp> 00023 #include <os/fosi.h> 00024 #include <boost/lambda/lambda.hpp> 00025 00026 #include "datasource_fixture.hpp" 00027 #include "marsh/PropertyBagIntrospector.hpp" 00028 #include "types/EnumTypeInfo.hpp" 00029 #include "marsh/PropertyLoader.hpp" 00030 #include "TaskContext.hpp" 00031 00032 using namespace boost::lambda; 00033 00034 typedef enum 00035 { 00036 A, B, C 00037 } TheEnum; 00038 00039 class EnumTypeTest 00040 { 00041 public: 00042 TheEnum enum1; 00043 TheEnum enum2; 00044 00045 AssignableDataSource<TheEnum>::shared_ptr a; 00046 AssignableDataSource<TheEnum>::shared_ptr b; 00047 TypeInfo* ti; 00048 TypeInfo* ts; 00049 00050 EnumTypeTest() 00051 { 00052 00053 a = new ValueDataSource<TheEnum>( A ); 00054 b = new ValueDataSource<TheEnum>( B ); 00055 00056 Types()->addType( new EnumTypeInfo<TheEnum>("TheEnum") ); 00057 } 00058 ~EnumTypeTest() 00059 { 00060 } 00061 }; 00062 00063 00064 // Registers the fixture into the 'registry' 00065 BOOST_FIXTURE_TEST_SUITE( EnumTypeTestSuite, EnumTypeTest ) 00066 00067 // Tests enum to int conversions 00068 BOOST_AUTO_TEST_CASE( testEnumIntConversion ) 00069 { 00070 00071 BOOST_REQUIRE( Types()->type("TheEnum") ); 00072 BOOST_REQUIRE( Types()->type("int") ); 00073 00074 // Test enum to int 00075 BOOST_CHECK( Types()->type("int")->convert( a ) ); 00076 BOOST_CHECK( Types()->type("int")->convert( b ) ); 00077 00078 // Test enum to int to enum 00079 BOOST_CHECK( Types()->type("TheEnum")->convert( Types()->type("int")->convert(a) ) ); 00080 BOOST_CHECK( Types()->type("TheEnum")->convert( Types()->type("int")->convert(b) ) ); 00081 00082 // Test composition of enum from int. 00083 PropertyBag result; 00084 Property<TheEnum> pa("a","", a ); 00085 Property<TheEnum> pb("a","", b ); 00086 PropertyBagIntrospector pbi(result); 00087 pbi.introspect( &pa); 00088 BOOST_REQUIRE_EQUAL( result.size(), 1); 00089 BOOST_CHECK( result.getItem(0)->getTypeInfo() == Types()->type("int") ); 00090 DataSource<int>::shared_ptr dint = DataSource<int>::narrow( result.getItem(0)->getDataSource().get() ); 00091 BOOST_CHECK_EQUAL(dint->get(), (int)A ); 00092 00093 pbi.introspect( &pb); 00094 BOOST_REQUIRE_EQUAL( result.size(), 2); 00095 BOOST_CHECK( result.getItem(1)->getTypeInfo() == Types()->type("int") ); 00096 dint = DataSource<int>::narrow( result.getItem(1)->getDataSource().get() ); 00097 BOOST_REQUIRE( dint ); 00098 BOOST_CHECK_EQUAL(dint->get(), (int)B ); 00099 } 00100 00101 00102 // Tests enum to file and back 00103 BOOST_AUTO_TEST_CASE( testEnumSaveIntProperties ) 00104 { 00105 TaskContext tc("TC"); 00106 PropertyLoader pl(&tc); 00107 enum1 = A; 00108 enum2 = B; 00109 tc.addProperty("enum1", enum1 ); 00110 tc.addProperty("enum2", enum2 ); 00111 00112 // saves A and B 00113 BOOST_CHECK( pl.save("enum_type_test_int_save.cpf", true) ); 00114 00115 enum1 = B; 00116 enum2 = A; 00117 00118 // restores A and B 00119 BOOST_CHECK( pl.load("enum_type_test_int_save.cpf") ); 00120 00121 BOOST_CHECK_EQUAL( enum1, A); 00122 BOOST_CHECK_EQUAL( enum2, B); 00123 } 00124 00125 BOOST_AUTO_TEST_SUITE_END() 00126