root/library/tests/testsuite/user_info_test.cpp @ 1225

Revision 1064, 7.8 kB (checked in by mido, 14 years ago)

astyle applied all over the library

  • Property svn:eol-style set to native
Line 
1#include <string>
2#include <vector>
3#include <string.h>
4#include "base/user_info.h"
5#include "../test_util.h"
6#include "UnitTest++.h"
7
8using namespace std;
9using namespace bdm;
10
11class Passenger : public root {
12public:
13
14    Passenger() {
15    }
16};
17
18class Human : public Passenger {
19public:
20    string name;
21
22    Human() {
23        name = "none";
24    }
25
26    virtual void from_setting ( const Setting &set ) {
27        UI::get ( name, set, "name" );
28    }
29
30    virtual void to_setting ( Setting &set ) const {
31        Setting &name_setting = set.add ( "name", Setting::TypeString );
32        name_setting = name;
33    }
34
35    string to_string()  const {
36        return name;
37    }
38};
39
40UIREGISTER ( Human );
41
42class Robot : public Passenger {
43public:
44    int number;
45    Array<string> software;
46
47    Robot() : software() {
48        number = -1;
49    }
50
51    virtual void from_setting ( const Setting &set ) {
52        UI::get ( number, set, "number" );
53        UI::get ( software, set, "software" );
54    }
55
56    virtual void to_setting ( Setting &set ) const {
57        Setting &number_setting = set.add ( "number", Setting::TypeInt );
58        number_setting = number;
59
60        UI::save ( software, set, "software" );
61    }
62
63    string to_string()  const {
64        stringstream stream;
65        stream << number;
66        for ( int i = 0; i < software.length(); i++ )
67            stream << "_" + software ( i );
68        return stream.str();
69    }
70};
71
72UIREGISTER ( Robot );
73
74class Transport : public root {
75public:
76    int year;
77    string manufacturer;
78
79    Transport() : year ( 1900 ), manufacturer ( "unknown" ) {
80    }
81
82    Transport ( int year, string manufacturer ) :
83        year ( year ), manufacturer ( manufacturer ) {
84    }
85
86    virtual void from_setting ( const Setting &set ) {
87        UI::get ( year, set, "year" );
88        UI::get ( manufacturer, set, "manufacturer" );
89    }
90
91    virtual void to_setting ( Setting &set ) const {
92        Setting &year_setting = set.add ( "year", Setting::TypeInt );
93        year_setting = year;
94
95        Setting &manufacturer_setting = set.add ( "manufacturer", Setting::TypeString );
96        manufacturer_setting = manufacturer;
97    }
98
99    virtual string to_string() const
100    {
101        return "the method to_string() is not implemented";
102    }
103};
104
105class Car : public Transport {
106public:
107    int kilometers;
108    Array<shared_ptr<Passenger> > passengers;
109
110    Car() : Transport() {
111        kilometers = 0;
112    }
113
114
115    Car ( int year, string manufacturer, int kilometers )
116        : Transport ( year, manufacturer ), kilometers ( kilometers ) {
117    }
118
119    virtual void from_setting ( const Setting &set ) {
120        Transport::from_setting ( set );
121
122        UI::get ( kilometers, set, "kilometers" );
123        UI::get ( passengers, set, "passengers" );
124    }
125
126    virtual void to_setting ( Setting &set ) const {
127        Transport::to_setting ( set );
128
129        Setting &kilometers_setting = set.add ( "kilometers", Setting::TypeInt );
130        kilometers_setting = kilometers;
131
132        UI::save ( passengers, set, "passengers" );
133    }
134
135    string to_string()  const {
136        stringstream stream;
137        stream << "A car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock.";
138        if ( passengers.length() ) {
139            stream << "The names of passengers are as follows: ";
140            for ( int i = 0; i < passengers.length(); i++ )
141                stream << passengers ( i )->to_string() << " ";
142        }
143        return stream.str();
144    }
145};
146
147UIREGISTER ( Car );
148
149class Bike : public Transport {
150public:
151    bool electricLights;
152    mat matr;
153
154    Bike() : Transport(), matr ( "2,2;3,4" ) {
155        electricLights = false;
156    }
157
158    Bike ( int year, string manufacturer, bool electricLights )
159        : Transport ( year, manufacturer ), electricLights ( electricLights ) {
160    }
161
162    ~Bike() {
163    }
164
165    void from_setting ( const Setting &set ) {
166        Transport::from_setting ( set );
167
168        int elights;
169        if ( UI::get ( elights, set, "electricLights", UI::optional ) ) {
170            electricLights = !!elights;
171        }
172
173        UI::get ( matr, set, "matr" );
174    }
175
176    void to_setting ( Setting &set ) const {
177        Transport::to_setting ( set );
178
179        Setting &electricLights_setting = set.add ( "electricLights", Setting::TypeBoolean );
180        electricLights_setting = electricLights;
181
182        UI::save ( matr, set, "matr" );
183    }
184
185    string to_string()  const {
186        stringstream stream;
187        stream << "a bike made in " << year << " by " << manufacturer;
188        if ( electricLights ) stream << " with electric lights included";
189        return stream.str();
190    }
191};
192
193UIREGISTER ( Bike );
194
195class Teleport : public Transport {
196public:
197    void to_setting ( Setting &set ) const {
198        set.add ( "this throws", Setting::TypeNone );
199    }
200};
201
202UIREGISTER ( Teleport );
203
204TEST ( load_test ) {
205    UIFile in ( "user_info_valid.cfg" );
206    shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) );
207    CHECK_EQUAL ( string ( "A car made in 1998 by audi, having 25000 kilometers on the clock.The names of passengers are as follows: Karlos Novak -1_CygWin_Matlab_Aimsun Karlosik Novacek " ), pepikovo->to_string() );
208
209    Transport pepikovo_static;
210    UI::get( pepikovo_static, in, "pepikovo", UI::compulsory );
211    CHECK_EQUAL ( string ( "the method to_string() is not implemented" ), pepikovo_static.to_string() );
212
213    shared_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) );
214    CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), jardovo->to_string() );
215
216    shared_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) );
217    CHECK_EQUAL ( string ( "a bike made in 1996 by author with electric lights included" ), ondrejovo->to_string() );
218
219    shared_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) );
220    CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), elisky->to_string() );
221
222    shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) );
223    CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() );
224}
225
226TEST ( load_error_test ) {
227    UIFile in ( "user_info_valid.cfg" );
228
229    try {
230        shared_ptr<Robot> failing ( UI::build<Robot> ( in, "elisky", UI::compulsory ) );
231        CHECK ( false );
232    } catch ( UIClassException &exc ) {
233        CHECK ( exc.what() );
234    }
235
236    try {
237        shared_ptr<Robot> failing ( UI::build<Robot> ( in, "kati", UI::compulsory ) );
238        CHECK ( false );
239    } catch ( UIException &exc ) {
240        CHECK ( exc.what() );
241    }
242
243    UIFile erroneous ( "user_info_erroneous.cfg" );
244    try {
245        shared_ptr<Transport> jardovo ( UI::build<Transport> ( erroneous, "jardovo", UI::compulsory ) );
246        CHECK ( false );
247    } catch ( UISettingException &exc ) {
248        CHECK ( exc.what() );
249    }
250}
251
252TEST ( save_test ) {
253    UIFile in ( "user_info_valid.cfg" );
254    shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) );
255
256    Car audi ( 1968, "zyl", 200 );
257    Car liaz ( 1989, "skoda", 1000 );
258    Bike author ( 2001, "noname", false );
259    UIFile out;
260
261    UI::save ( &audi, out, "marty" );
262    UI::save ( &liaz, out, "bohousovo" );
263    UI::save ( &author, out, "karlovo" );
264    UI::save ( pepikovo, out, "pepikovo" );
265    out.save ( "user_info_output.cfg" );
266
267    string expected ( load_test_file ( "user_info_matrix.cfg" ) );
268    string actual ( load_test_file ( "user_info_output.cfg" ) );
269    CHECK_EQUAL ( expected, actual );
270}
271
272
273TEST ( save_error_test ) {
274    UIFile out;
275
276    Teleport teleport;
277    try {
278        UI::save ( &teleport, out, "unsaveable" );
279        CHECK ( false );
280    } catch ( UIException &exc ) {
281        CHECK ( exc.what() );
282    }
283}
Note: See TracBrowser for help on using the browser.