Renderer, ...: use PixelRect::GetCenter()
[xcsoar.git] / test / src / TestTimeFormatter.cpp
blob00cb4a69f96554125d55cd6e910caf7a2b8a04e3
1 /* Copyright_License {
3 XCSoar Glide Computer - http://www.xcsoar.org/
4 Copyright (C) 2000-2013 The XCSoar Project
5 A detailed list of copyright holders can be found in the file "AUTHORS".
7 This program is free software; you can redistribute it and/or
8 modify it under the terms of the GNU General Public License
9 as published by the Free Software Foundation; either version 2
10 of the License, or (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 #include "Formatter/TimeFormatter.hpp"
24 #include "Util/Macros.hpp"
25 #include "Util/StringUtil.hpp"
26 #include "TestUtil.hpp"
28 static void
29 TestFormat()
31 TCHAR buffer[256];
33 FormatTime(buffer, fixed(0));
34 ok1(StringIsEqual(buffer, _T("00:00:00")));
36 FormatTime(buffer, fixed(1));
37 ok1(StringIsEqual(buffer, _T("00:00:01")));
39 FormatTime(buffer, fixed(59));
40 ok1(StringIsEqual(buffer, _T("00:00:59")));
42 FormatTime(buffer, fixed(60));
43 ok1(StringIsEqual(buffer, _T("00:01:00")));
45 FormatTime(buffer, fixed(60 * 5));
46 ok1(StringIsEqual(buffer, _T("00:05:00")));
48 FormatTime(buffer, fixed(60 * 59));
49 ok1(StringIsEqual(buffer, _T("00:59:00")));
51 FormatTime(buffer, fixed(60 * 60));
52 ok1(StringIsEqual(buffer, _T("01:00:00")));
54 FormatTime(buffer, fixed(60 * 60 * 3 + 60 * 25));
55 ok1(StringIsEqual(buffer, _T("03:25:00")));
57 FormatTime(buffer, fixed(60 * 60 * 19 + 60 * 47 + 43));
58 ok1(StringIsEqual(buffer, _T("19:47:43")));
60 FormatTime(buffer, fixed(-(60 * 59)));
61 ok1(StringIsEqual(buffer, _T("-00:59:00")));
63 FormatTime(buffer, fixed(-(60 * 60 * 19 + 60 * 47 + 43)));
64 ok1(StringIsEqual(buffer, _T("-19:47:43")));
67 static void
68 TestFormatLong()
70 TCHAR buffer[256];
72 FormatTimeLong(buffer, fixed(0));
73 ok1(StringIsEqual(buffer, _T("00:00:00.000")));
75 FormatTimeLong(buffer, fixed(1.123));
76 ok1(StringIsEqual(buffer, _T("00:00:01.123")));
78 FormatTimeLong(buffer, fixed(59));
79 ok1(StringIsEqual(buffer, _T("00:00:59.000")));
81 FormatTimeLong(buffer, fixed(60.001));
82 ok1(StringIsEqual(buffer, _T("00:01:00.001")));
84 FormatTimeLong(buffer, fixed(60 * 5));
85 ok1(StringIsEqual(buffer, _T("00:05:00.000")));
87 FormatTimeLong(buffer, fixed(60 * 59));
88 ok1(StringIsEqual(buffer, _T("00:59:00.000")));
90 FormatTimeLong(buffer, fixed(60 * 60));
91 ok1(StringIsEqual(buffer, _T("01:00:00.000")));
93 FormatTimeLong(buffer, fixed(60 * 60 * 3 + 60 * 25));
94 ok1(StringIsEqual(buffer, _T("03:25:00.000")));
96 FormatTimeLong(buffer, fixed(60 * 60 * 19 + 60 * 47 + 43.765));
97 ok1(StringIsEqual(buffer, _T("19:47:43.765")));
99 FormatTimeLong(buffer, fixed(-(60 * 59)));
100 ok1(StringIsEqual(buffer, _T("-00:59:00.000")));
102 FormatTimeLong(buffer, fixed(-(60 * 60 * 19 + 60 * 47 + 43.765)));
103 ok1(StringIsEqual(buffer, _T("-19:47:43.765")));
106 static void
107 TestHHMM()
109 TCHAR buffer[256];
111 FormatSignedTimeHHMM(buffer, 0);
112 ok1(StringIsEqual(buffer, _T("00:00")));
114 FormatSignedTimeHHMM(buffer, 1);
115 ok1(StringIsEqual(buffer, _T("00:00")));
117 FormatSignedTimeHHMM(buffer, 59);
118 ok1(StringIsEqual(buffer, _T("00:00")));
120 FormatSignedTimeHHMM(buffer, 60);
121 ok1(StringIsEqual(buffer, _T("00:01")));
123 FormatSignedTimeHHMM(buffer, 60 * 5);
124 ok1(StringIsEqual(buffer, _T("00:05")));
126 FormatSignedTimeHHMM(buffer, 60 * 59);
127 ok1(StringIsEqual(buffer, _T("00:59")));
129 FormatSignedTimeHHMM(buffer, 60 * 60);
130 ok1(StringIsEqual(buffer, _T("01:00")));
132 FormatSignedTimeHHMM(buffer, 60 * 60 * 3 + 60 * 25);
133 ok1(StringIsEqual(buffer, _T("03:25")));
135 FormatSignedTimeHHMM(buffer, 60 * 60 * 19 + 60 * 47);
136 ok1(StringIsEqual(buffer, _T("19:47")));
138 FormatSignedTimeHHMM(buffer, -(60 * 59));
139 ok1(StringIsEqual(buffer, _T("-00:59")));
141 FormatSignedTimeHHMM(buffer, -(60 * 60 * 19 + 60 * 47));
142 ok1(StringIsEqual(buffer, _T("-19:47")));
145 static void
146 TestTwoLines()
148 TCHAR buffer[256], buffer2[256];
150 FormatTimeTwoLines(buffer, buffer2, 0);
151 ok1(StringIsEqual(buffer, _T("00'00")));
152 ok1(StringIsEqual(buffer2, _T("")));
154 FormatTimeTwoLines(buffer, buffer2, 1);
155 ok1(StringIsEqual(buffer, _T("00'01")));
156 ok1(StringIsEqual(buffer2, _T("")));
158 FormatTimeTwoLines(buffer, buffer2, 59);
159 ok1(StringIsEqual(buffer, _T("00'59")));
160 ok1(StringIsEqual(buffer2, _T("")));
162 FormatTimeTwoLines(buffer, buffer2, 60);
163 ok1(StringIsEqual(buffer, _T("01'00")));
164 ok1(StringIsEqual(buffer2, _T("")));
166 FormatTimeTwoLines(buffer, buffer2, 60 * 5);
167 ok1(StringIsEqual(buffer, _T("05'00")));
168 ok1(StringIsEqual(buffer2, _T("")));
170 FormatTimeTwoLines(buffer, buffer2, 60 * 59);
171 ok1(StringIsEqual(buffer, _T("59'00")));
172 ok1(StringIsEqual(buffer2, _T("")));
174 FormatTimeTwoLines(buffer, buffer2, 60 * 60);
175 ok1(StringIsEqual(buffer, _T("01:00")));
176 ok1(StringIsEqual(buffer2, _T("00")));
178 FormatTimeTwoLines(buffer, buffer2, 60 * 60 * 3 + 60 * 25 + 13);
179 ok1(StringIsEqual(buffer, _T("03:25")));
180 ok1(StringIsEqual(buffer2, _T("13")));
182 FormatTimeTwoLines(buffer, buffer2, 60 * 60 * 19 + 60 * 47 + 28);
183 ok1(StringIsEqual(buffer, _T("19:47")));
184 ok1(StringIsEqual(buffer2, _T("28")));
186 FormatTimeTwoLines(buffer, buffer2, -(60 * 59));
187 ok1(StringIsEqual(buffer, _T("59'00")));
188 ok1(StringIsEqual(buffer2, _T("")));
190 FormatTimeTwoLines(buffer, buffer2, -(60 * 60 * 19 + 60 * 47 + 28));
191 ok1(StringIsEqual(buffer, _T("19:47")));
192 ok1(StringIsEqual(buffer2, _T("28")));
195 static void
196 TestSmart(int time, const TCHAR *expected_output1,
197 const TCHAR *expected_output2, const TCHAR *expected_output3,
198 const TCHAR *expected_output4, const TCHAR *separator = _T(" "))
200 TCHAR buffer[256];
202 FormatTimespanSmart(buffer, time, 1, separator);
203 ok1(StringIsEqual(buffer, expected_output1));
205 FormatTimespanSmart(buffer, time, 2, separator);
206 ok1(StringIsEqual(buffer, expected_output2));
208 FormatTimespanSmart(buffer, time, 3, separator);
209 ok1(StringIsEqual(buffer, expected_output3));
211 FormatTimespanSmart(buffer, time, 4, separator);
212 ok1(StringIsEqual(buffer, expected_output4));
215 static void
216 TestSmart()
218 TestSmart(0, _T("0 sec"), _T("0 sec"), _T("0 sec"), _T("0 sec"));
219 TestSmart(1, _T("1 sec"), _T("1 sec"), _T("1 sec"), _T("1 sec"));
220 TestSmart(59, _T("59 sec"), _T("59 sec"), _T("59 sec"), _T("59 sec"));
221 TestSmart(60, _T("1 min"), _T("1 min"), _T("1 min"), _T("1 min"));
223 TestSmart(60 + 59, _T("1 min"), _T("1 min 59 sec"), _T("1 min 59 sec"),
224 _T("1 min 59 sec"));
226 TestSmart(60 * 5 + 34, _T("5 min"), _T("5 min 34 sec"), _T("5 min 34 sec"),
227 _T("5 min 34 sec"));
229 TestSmart(60 * 59, _T("59 min"), _T("59 min"), _T("59 min"), _T("59 min"));
230 TestSmart(60 * 60, _T("1 h"), _T("1 h"), _T("1 h"), _T("1 h"));
232 TestSmart(60 * 60 * 3 + 60 * 25, _T("3 h"), _T("3 h 25 min"),
233 _T("3 h 25 min"), _T("3 h 25 min"));
235 TestSmart(60 * 60 * 19 + 60 * 47, _T("19 h"), _T("19 h 47 min"),
236 _T("19 h 47 min"), _T("19 h 47 min"));
238 TestSmart(60 * 60 * 19 + 47, _T("19 h"), _T("19 h"),
239 _T("19 h 0 min 47 sec"), _T("19 h 0 min 47 sec"));
241 TestSmart(60 * 60 * 19 + 60 * 47 + 5, _T("19 h"), _T("19 h 47 min"),
242 _T("19 h 47 min 5 sec"), _T("19 h 47 min 5 sec"));
244 TestSmart(60 * 60 * 24 * 3 + 60 * 60 * 19 + 60 * 47 + 5, _T("3 days"),
245 _T("3 days 19 h"), _T("3 days 19 h 47 min"),
246 _T("3 days 19 h 47 min 5 sec"));
248 TestSmart(-(60 * 60 * 24 * 3 + 60 * 60 * 19 + 60 * 47 + 5), _T("-3 days"),
249 _T("-3 days 19 h"), _T("-3 days 19 h 47 min"),
250 _T("-3 days 19 h 47 min 5 sec"));
254 main(int argc, char **argv)
256 plan_tests(111);
258 TestFormat();
259 TestFormatLong();
260 TestHHMM();
261 TestTwoLines();
262 TestSmart();
264 return exit_status();