enum_string_type_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 enum_string_type_test.cpp
3 
4  enum_string_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 struct EnumStringTypeInfo: public EnumTypeInfo<TheEnum>
37 {
39  EnumTypeInfo<TheEnum> ("TheEnum")
40  {
41  to_string[A] = "A";
42  to_string[B] = "B";
43  to_string[C] = "C";
44  }
45 };
46 
48 {
49 public:
52 
57 
59  {
60 
61  a = new ValueDataSource<TheEnum>( A );
62  b = new ValueDataSource<TheEnum>( B );
63 
64  Types()->addType( new EnumStringTypeInfo() );
65  }
67  {
68  }
69 };
70 
71 
72 // Registers the fixture into the 'registry'
73 BOOST_FIXTURE_TEST_SUITE( EnumTypeTestSuite, EnumTypeTest )
74 // Tests enum to string conversions
75 BOOST_AUTO_TEST_CASE( testEnumStringConversion )
76 {
77 
78  ti = Types()->type("TheEnum");
79  ts = Types()->type("string");
80 
81  BOOST_REQUIRE( ti );
82  BOOST_REQUIRE( ts );
83 
84  // Test enum conversion
85  BOOST_REQUIRE( ti->decomposeType( a ) );
86  BOOST_CHECK_EQUAL( ti->decomposeType( a )->getTypeInfo(), ts );
87 
88  // converts a to "A" and then back to enum and stores it in B.
89  BOOST_CHECK( ti->composeType(ti->decomposeType(a), b) );
90 
91  BOOST_CHECK_EQUAL( a->get(), A);
92  BOOST_CHECK_EQUAL( b->get(), A);
93 
94  b->set(B);
95 
96  // Test composition of enum from string.
97  PropertyBag result;
98  Property<TheEnum> pa("a","", a );
99  Property<TheEnum> pb("a","", b );
100  PropertyBagIntrospector pbi(result);
101  pbi.introspect( &pa);
102  BOOST_REQUIRE_EQUAL( result.size(), 1);
103  BOOST_CHECK( result.getItem(0)->getTypeInfo() == ts );
105  BOOST_CHECK_EQUAL(dstring->get(), "A" );
106 
107  pbi.introspect( &pb);
108  BOOST_REQUIRE_EQUAL( result.size(), 2);
109  BOOST_CHECK( result.getItem(1)->getTypeInfo() == ts );
110  dstring = DataSource<string>::narrow( result.getItem(1)->getDataSource().get() );
111 
112  BOOST_CHECK_EQUAL(dstring->get(), "B" );
113 }
114 
115 // Tests enum to file and back
116 BOOST_AUTO_TEST_CASE( testEnumSaveStringProperties )
117 {
118  TaskContext tc("TC");
119  PropertyLoader pl(&tc);
120  enum1 = A;
121  enum2 = B;
122  tc.addProperty("enum1", enum1 );
123  tc.addProperty("enum2", enum2 );
124 
125  // saves A and B
126  BOOST_CHECK( pl.save("enum_type_test_string_save.cpf", true) );
127 
128  enum1 = B;
129  enum2 = A;
130 
131  // restores A and B
132  BOOST_CHECK( pl.load("enum_type_test_string_save.cpf") );
133 
134  BOOST_CHECK_EQUAL( enum1, A);
135  BOOST_CHECK_EQUAL( enum2, B);
136 }
137 
139 
Property< T > & addProperty(const std::string &name, T &attr)
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
TypeInfo * ti
Contains &#39;B&#39;.
BOOST_AUTO_TEST_CASE(testEnumStringConversion)
#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
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< DataSource< T > > shared_ptr
Definition: DataSource.hpp:115
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