1 | #include <string> |
---|
2 | #include <string.h> |
---|
3 | #include "base/user_info.h" |
---|
4 | #include "test_util.h" |
---|
5 | #include "UnitTest++.h" |
---|
6 | |
---|
7 | using namespace std; |
---|
8 | using namespace bdm; |
---|
9 | |
---|
10 | class Passenger : public root { |
---|
11 | public: |
---|
12 | |
---|
13 | Passenger() { |
---|
14 | } |
---|
15 | }; |
---|
16 | |
---|
17 | class Human : public Passenger { |
---|
18 | public: |
---|
19 | string name; |
---|
20 | |
---|
21 | Human() { |
---|
22 | name = "none"; |
---|
23 | } |
---|
24 | |
---|
25 | virtual void from_setting ( const Setting &set ) { |
---|
26 | UI::get ( name, set, "name" ); |
---|
27 | } |
---|
28 | |
---|
29 | virtual void to_setting ( Setting &set ) const { |
---|
30 | Setting &name_setting = set.add ( "name", Setting::TypeString ); |
---|
31 | name_setting = name; |
---|
32 | } |
---|
33 | |
---|
34 | string to_string() { |
---|
35 | return name; |
---|
36 | } |
---|
37 | }; |
---|
38 | |
---|
39 | UIREGISTER ( Human ); |
---|
40 | |
---|
41 | class Robot : public Passenger { |
---|
42 | public: |
---|
43 | int number; |
---|
44 | Array<string> software; |
---|
45 | |
---|
46 | Robot() : software() { |
---|
47 | number = -1; |
---|
48 | } |
---|
49 | |
---|
50 | virtual void from_setting ( const Setting &set ) { |
---|
51 | UI::get ( number, set, "number" ); |
---|
52 | UI::get ( software, set, "software" ); |
---|
53 | } |
---|
54 | |
---|
55 | virtual void to_setting ( Setting &set ) const { |
---|
56 | Setting &number_setting = set.add ( "number", Setting::TypeInt ); |
---|
57 | number_setting = number; |
---|
58 | |
---|
59 | UI::save ( software, set, "software" ); |
---|
60 | } |
---|
61 | |
---|
62 | string to_string() { |
---|
63 | stringstream stream; |
---|
64 | stream << number; |
---|
65 | for ( int i = 0; i < software.length(); i++ ) |
---|
66 | stream << "_" + software ( i ); |
---|
67 | return stream.str(); |
---|
68 | } |
---|
69 | }; |
---|
70 | |
---|
71 | UIREGISTER ( Robot ); |
---|
72 | |
---|
73 | class Transport : public root { |
---|
74 | public: |
---|
75 | int year; |
---|
76 | string manufacturer; |
---|
77 | |
---|
78 | Transport() { |
---|
79 | year = 1900; |
---|
80 | manufacturer = "unknown"; |
---|
81 | } |
---|
82 | |
---|
83 | Transport ( int year, string manufacturer ) |
---|
84 | : year ( year ), manufacturer ( manufacturer ) { |
---|
85 | } |
---|
86 | |
---|
87 | virtual void from_setting ( const Setting &set ) { |
---|
88 | UI::get ( year, set, "year" ); |
---|
89 | UI::get ( manufacturer, set, "manufacturer" ); |
---|
90 | } |
---|
91 | |
---|
92 | virtual void to_setting ( Setting &set ) const { |
---|
93 | Setting &year_setting = set.add ( "year", Setting::TypeInt ); |
---|
94 | year_setting = year; |
---|
95 | |
---|
96 | Setting &manufacturer_setting = set.add ( "manufacturer", Setting::TypeString ); |
---|
97 | manufacturer_setting = manufacturer; |
---|
98 | } |
---|
99 | }; |
---|
100 | |
---|
101 | class Car : public Transport { |
---|
102 | public: |
---|
103 | int kilometers; |
---|
104 | Array<shared_ptr<Passenger> > passengers; |
---|
105 | |
---|
106 | Car() : Transport() { |
---|
107 | kilometers = 0; |
---|
108 | } |
---|
109 | |
---|
110 | |
---|
111 | Car ( int year, string manufacturer, int kilometers ) |
---|
112 | : Transport ( year, manufacturer ), kilometers ( kilometers ) { |
---|
113 | } |
---|
114 | |
---|
115 | virtual void from_setting ( const Setting &set ) { |
---|
116 | Transport::from_setting ( set ); |
---|
117 | |
---|
118 | UI::get ( kilometers, set, "kilometers" ); |
---|
119 | UI::get ( passengers, set, "passengers" ); |
---|
120 | } |
---|
121 | |
---|
122 | virtual void to_setting ( Setting &set ) const { |
---|
123 | Transport::to_setting ( set ); |
---|
124 | |
---|
125 | Setting &kilometers_setting = set.add ( "kilometers", Setting::TypeInt ); |
---|
126 | kilometers_setting = kilometers; |
---|
127 | |
---|
128 | UI::save ( passengers, set, "passengers" ); |
---|
129 | } |
---|
130 | |
---|
131 | string to_string() { |
---|
132 | stringstream stream; |
---|
133 | stream << "A car made in " << year << " by " << manufacturer << ", having " << kilometers << " kilometers on the clock."; |
---|
134 | if ( passengers.length() ) { |
---|
135 | stream << "The names of passengers are as follows: "; |
---|
136 | for ( int i = 0; i < passengers.length(); i++ ) |
---|
137 | stream << passengers ( i )->to_string() << " "; |
---|
138 | } |
---|
139 | return stream.str(); |
---|
140 | } |
---|
141 | }; |
---|
142 | |
---|
143 | UIREGISTER ( Car ); |
---|
144 | |
---|
145 | class Bike : public Transport { |
---|
146 | public: |
---|
147 | bool electricLights; |
---|
148 | mat matr; |
---|
149 | |
---|
150 | Bike() : Transport(), matr ( "2,2;3,4" ) { |
---|
151 | electricLights = false; |
---|
152 | } |
---|
153 | |
---|
154 | Bike ( int year, string manufacturer, bool electricLights ) |
---|
155 | : Transport ( year, manufacturer ), electricLights ( electricLights ) { |
---|
156 | } |
---|
157 | |
---|
158 | ~Bike() { |
---|
159 | } |
---|
160 | |
---|
161 | void from_setting ( const Setting &set ) { |
---|
162 | Transport::from_setting ( set ); |
---|
163 | |
---|
164 | int elights; |
---|
165 | if ( UI::get ( elights, set, "electricLights", UI::optional ) ) { |
---|
166 | electricLights = !!elights; |
---|
167 | } |
---|
168 | |
---|
169 | UI::get ( matr, set, "matr" ); |
---|
170 | } |
---|
171 | |
---|
172 | void to_setting ( Setting &set ) const { |
---|
173 | Transport::to_setting ( set ); |
---|
174 | |
---|
175 | Setting &electricLights_setting = set.add ( "electricLights", Setting::TypeBoolean ); |
---|
176 | electricLights_setting = electricLights; |
---|
177 | |
---|
178 | UI::save ( matr, set, "matr" ); |
---|
179 | } |
---|
180 | |
---|
181 | string to_string() { |
---|
182 | stringstream stream; |
---|
183 | stream << "a bike made in " << year << " by " << manufacturer; |
---|
184 | if ( electricLights ) stream << " with electric lights included"; |
---|
185 | return stream.str(); |
---|
186 | } |
---|
187 | }; |
---|
188 | |
---|
189 | UIREGISTER ( Bike ); |
---|
190 | |
---|
191 | TEST ( test_load ) { |
---|
192 | UIFile in ( "test_user_info_input.cfg" ); |
---|
193 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
194 | 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() ); |
---|
195 | |
---|
196 | shared_ptr<Transport> jardovo ( UI::build<Transport> ( in, "jardovo", UI::compulsory ) ); |
---|
197 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), jardovo->to_string() ); |
---|
198 | |
---|
199 | shared_ptr<Transport> ondrejovo ( UI::build<Transport> ( in, "ondrejovo", UI::compulsory ) ); |
---|
200 | CHECK_EQUAL ( string ( "a bike made in 1996 by author with electric lights included" ), ondrejovo->to_string() ); |
---|
201 | |
---|
202 | shared_ptr<Transport> elisky ( UI::build<Transport> ( in, "elisky", UI::compulsory ) ); |
---|
203 | CHECK_EQUAL ( string ( "A car made in 1992 by liaz, having 1555000 kilometers on the clock." ), elisky->to_string() ); |
---|
204 | |
---|
205 | shared_ptr<Transport> kati ( UI::build<Transport> ( in, "kati", UI::compulsory ) ); |
---|
206 | CHECK_EQUAL ( string ( "A car made in 1980 by vecernicek, having 250000 kilometers on the clock." ), kati->to_string() ); |
---|
207 | } |
---|
208 | |
---|
209 | TEST ( test_save ) { |
---|
210 | UIFile in ( "test_user_info_input.cfg" ); |
---|
211 | shared_ptr<Transport> pepikovo ( UI::build<Transport> ( in, "pepikovo", UI::compulsory ) ); |
---|
212 | |
---|
213 | Car audi ( 1968, "zyl", 200 ); |
---|
214 | Car liaz ( 1989, "skoda", 1000 ); |
---|
215 | Bike author ( 2001, "noname", false ); |
---|
216 | UIFile out; |
---|
217 | |
---|
218 | UI::save ( &audi, out, "marty" ); |
---|
219 | UI::save ( &liaz, out, "bohousovo" ); |
---|
220 | UI::save ( &author, out, "karlovo" ); |
---|
221 | UI::save ( pepikovo, out, "pepikovo" ); |
---|
222 | out.save ( "testUI_out.cfg" ); |
---|
223 | |
---|
224 | string expected ( load_test_file ( "testUI_out.matrix" ) ); |
---|
225 | string actual ( load_test_file ( "testUI_out.cfg" ) ); |
---|
226 | CHECK_EQUAL ( expected, actual ); |
---|
227 | } |
---|