-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdatasource_test.cpp
190 lines (152 loc) · 6.01 KB
/
datasource_test.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/***************************************************************************
tag: The SourceWorks Tue Sep 7 00:54:57 CEST 2010 datasource_test.cpp
datasource_test.cpp - description
-------------------
begin : Tue September 07 2010
copyright : (C) 2010 The SourceWorks
email : [email protected]
***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "unit.hpp"
#include <boost/array.hpp>
#include <rtt-fwd.hpp>
#include <internal/DataSources.hpp>
#include <internal/PartDataSource.hpp>
#include <internal/ArrayPartDataSource.hpp>
//#include <internal/OffsetPartDataSource.hpp>
#include <internal/carray.hpp>
#include <boost/serialization/array.hpp>
#include <os/fosi.h>
#include <boost/lambda/lambda.hpp>
#include "datasource_fixture.hpp"
using namespace boost::lambda;
class DataSourceTest
{
public:
AType atype;
AType atype_orig;
BType btype;
BType btype_orig;
DataSourceTest() {
atype.init();
atype_orig.init();
btype.init();
btype_orig.init();
}
~DataSourceTest() { }
};
// Registers the fixture into the 'registry'
BOOST_FIXTURE_TEST_SUITE( DataSourceTestSuite, DataSourceTest )
// Test semantics of ValueDataSource and test fixture.
BOOST_AUTO_TEST_CASE( testValueDataSource )
{
AssignableDataSource<AType>::shared_ptr d = new ValueDataSource<AType>( atype );
// Test fixture:
BOOST_CHECK_EQUAL( atype, atype );
BOOST_CHECK_EQUAL( atype, atype_orig );
// Do not take by reference:
BOOST_CHECK_EQUAL( d->set(), atype );
atype.a = -atype.a;
BOOST_CHECK_EQUAL( d->set(), atype_orig );
}
// Test constant DataSource.
BOOST_AUTO_TEST_CASE( testConstantDataSource )
{
DataSource<AType>::shared_ptr d = new ConstantDataSource<AType>( atype );
// Do not take by reference:
BOOST_CHECK_EQUAL( d->get(), atype );
atype.a = -atype.a;
BOOST_CHECK_EQUAL( d->get(), atype_orig );
}
// Test reference to C++ data
BOOST_AUTO_TEST_CASE( testReferenceDataSource )
{
AssignableDataSource<AType>::shared_ptr d = new ReferenceDataSource<AType>( atype );
// Take by reference:
BOOST_CHECK_EQUAL( d->set(), atype );
atype.a = -atype.a;
BOOST_CHECK_EQUAL( d->set(), atype );
d->set().a = 55;
BOOST_CHECK_EQUAL( d->set(), atype );
}
// Test const reference to C++ data
BOOST_AUTO_TEST_CASE( testConstReferenceDataSource )
{
DataSource<AType>::shared_ptr d = new ConstReferenceDataSource<AType>( atype );
// Take by reference:
BOOST_CHECK_EQUAL( d->get(), atype );
atype.a = -atype.a;
BOOST_CHECK_EQUAL( d->get(), atype );
}
// Test part reference to C++ data
BOOST_AUTO_TEST_CASE( testPartDataSource )
{
DataSource<AType>::shared_ptr abase = new UpdatedReferenceDataSource<AType>( atype );
AssignableDataSource<string>::shared_ptr d = new PartDataSource<string>( atype.c, abase );
// Take string by reference:
BOOST_CHECK_EQUAL( d->set(), atype.c );
atype.c = "world";
BOOST_CHECK_EQUAL( d->set(), atype.c );
// Test both versions of set(). This also checks updated():
d->set( "Long gone!" );
BOOST_CHECK_EQUAL( d->set(), abase->get().c );
BOOST_CHECK_EQUAL( d->set(), atype.c );
d->set() = "And back!";
d->updated();
BOOST_CHECK_EQUAL( d->set(), abase->get().c );
BOOST_CHECK_EQUAL( d->set(), atype.c );
}
//! Test C-style array as DataSource
//! Only applies to BType objects and uses carray for this.
BOOST_AUTO_TEST_CASE( testArrayDataSource )
{
DataSource<BType>::shared_ptr abase = new UpdatedReferenceDataSource<BType>( btype );
AssignableDataSource<carray<char> >::shared_ptr d =
new ValueDataSource<carray<char> >( carray<char>(btype.c, 10) );
// Take string by reference (!):
BOOST_CHECK_EQUAL( d->set().address(), btype.c );
strcpy( btype.c, "world");
BOOST_CHECK_EQUAL( "world", btype.c );
BOOST_CHECK_EQUAL( d->set().address(), btype.c );
// Test both versions of set(). This also checks updated():
*d->set().address() = 'L';
d->updated();
BOOST_CHECK_EQUAL( "Lorld", btype.c );
BOOST_CHECK_EQUAL( "Lorld", d->get().address() );
BOOST_CHECK_EQUAL( d->set().address(), btype.c );
}
//! Test reference to part of C-style array
//! Only applies to BType objects.
BOOST_AUTO_TEST_CASE( testArrayPartDataSource )
{
AssignableDataSource<unsigned int>::shared_ptr index = new ValueDataSource<unsigned int>(0);
DataSource<BType>::shared_ptr abase = new UpdatedReferenceDataSource<BType>( btype );
AssignableDataSource<char>::shared_ptr d = new ArrayPartDataSource<char>( *btype.c, index, abase, sizeof(btype.c) );
// Take string by reference:
BOOST_CHECK_EQUAL( &d->set(), btype.c );
strcpy( btype.c, "world");
BOOST_CHECK_EQUAL( "world", btype.c );
BOOST_CHECK_EQUAL( &d->set(), btype.c );
// Test both versions of set(). This also checks updated():
d->set( 'L' );
BOOST_CHECK_EQUAL( "Lorld", btype.c );
BOOST_CHECK( strcmp( &d->set(), abase->get().c) == 0 );
BOOST_CHECK_EQUAL( &d->set(), btype.c );
d->set() = 'W';
d->updated();
BOOST_CHECK_EQUAL( "World", btype.c );
BOOST_CHECK( strcmp( &d->set(), abase->get().c) == 0 );
BOOST_CHECK_EQUAL( &d->set(), btype.c );
index->set( 3 );
d->set( 'L' );
BOOST_CHECK_EQUAL( "WorLd", btype.c );
BOOST_CHECK( strcmp( &d->set(), &abase->get().c[3]) == 0 );
BOOST_CHECK_EQUAL( &d->set(), &btype.c[3] );
}
BOOST_AUTO_TEST_SUITE_END()