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