enum_type_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 enum_type_test.cpp
3 
4  enum_type_test.cpp - description
5  -------------------
6  begin : Tue September 07 2010
7  copyright : (C) 2010 The SourceWorks
8  email : peter@thesourceworks.com
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 
20 #include "unit.hpp"
21 #include <rtt-fwd.hpp>
22 #include <internal/DataSources.hpp>
23 #include <os/fosi.h>
24 
25 #include "datasource_fixture.hpp"
27 #include "types/EnumTypeInfo.hpp"
28 #include "marsh/PropertyLoader.hpp"
29 #include "TaskContext.hpp"
30 
31 typedef enum
32 {
33  A, B, C
34 } TheEnum;
35 
36 class EnumTypeTest
37 {
38 public:
39  TheEnum enum1;
40  TheEnum enum2;
41 
44  TypeInfo* ti;
45  TypeInfo* ts;
46 
48  {
49 
50  a = new ValueDataSource<TheEnum>( A );
51  b = new ValueDataSource<TheEnum>( B );
52 
53  Types()->addType( new EnumTypeInfo<TheEnum>("TheEnum") );
54  }
56  {
57  }
58 };
59 
60 
61 // Registers the fixture into the 'registry'
62 BOOST_FIXTURE_TEST_SUITE( EnumTypeTestSuite, EnumTypeTest )
63 
64 // Tests enum to int conversions
65 BOOST_AUTO_TEST_CASE( testEnumIntConversion )
66 {
67 
68  BOOST_REQUIRE( Types()->type("TheEnum") );
69  BOOST_REQUIRE( Types()->type("int") );
70 
71  // Test enum to int
72  BOOST_CHECK( Types()->type("int")->convert( a ) );
73  BOOST_CHECK( Types()->type("int")->convert( b ) );
74 
75  // Test enum to int to enum
76  BOOST_CHECK( Types()->type("TheEnum")->convert( Types()->type("int")->convert(a) ) );
77  BOOST_CHECK( Types()->type("TheEnum")->convert( Types()->type("int")->convert(b) ) );
78 
79  // Test composition of enum from int.
80  PropertyBag result;
81  Property<TheEnum> pa("a","", a );
82  Property<TheEnum> pb("a","", b );
83  PropertyBagIntrospector pbi(result);
84  pbi.introspect( &pa);
85  BOOST_REQUIRE_EQUAL( result.size(), 1);
86  BOOST_CHECK( result.getItem(0)->getTypeInfo() == Types()->type("int") );
88  BOOST_CHECK_EQUAL(dint->get(), (int)A );
89 
90  pbi.introspect( &pb);
91  BOOST_REQUIRE_EQUAL( result.size(), 2);
92  BOOST_CHECK( result.getItem(1)->getTypeInfo() == Types()->type("int") );
93  dint = DataSource<int>::narrow( result.getItem(1)->getDataSource().get() );
94  BOOST_REQUIRE( dint );
95  BOOST_CHECK_EQUAL(dint->get(), (int)B );
96 }
97 
98 
99 // Tests enum to file and back
100 BOOST_AUTO_TEST_CASE( testEnumSaveIntProperties )
101 {
102  TaskContext tc("TC");
103  PropertyLoader pl(&tc);
104  enum1 = A;
105  enum2 = B;
106  tc.addProperty("enum1", enum1 );
107  tc.addProperty("enum2", enum2 );
108 
109  // saves A and B
110  BOOST_CHECK( pl.save("enum_type_test_int_save.cpf", true) );
111 
112  enum1 = B;
113  enum2 = A;
114 
115  // restores A and B
116  BOOST_CHECK( pl.load("enum_type_test_int_save.cpf") );
117 
118  BOOST_CHECK_EQUAL( enum1, A);
119  BOOST_CHECK_EQUAL( enum2, B);
120 }
121 
123 
Property< T > & addProperty(const std::string &name, T &attr)
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
TypeInfo * ti
Contains &#39;B&#39;.
TheEnum
#define BOOST_AUTO_TEST_SUITE_END()
This class fills up a given bag with the results of an introspection.
bool load(const std::string &filename) const
BOOST_AUTO_TEST_CASE(testEnumIntConversion)
A container for holding references to properties.
Definition: PropertyBag.hpp:96
bool save(const std::string &filename, bool all=true) const
A property represents a named value of any type with a description.
Definition: Property.hpp:76
TypeInfoRepository::shared_ptr Types()
Definition: Types.cpp:48
EnumTypeTest()
Type info for &#39;string&#39;.
boost::intrusive_ptr< AssignableDataSource< T > > shared_ptr
Definition: DataSource.hpp:198
base::PropertyBase * getItem(int i) const
AssignableDataSource< TheEnum >::shared_ptr a
AssignableDataSource< TheEnum >::shared_ptr b
Contains &#39;A&#39;.
size_t size() const
TypeInfo * ts
Type info for enum.
virtual DataSourceBase::shared_ptr getDataSource() const =0
virtual const types::TypeInfo * getTypeInfo() const =0


rtt
Author(s): RTT Developers
autogenerated on Tue Jun 25 2019 19:33:24