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