1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
22 #include <sal/types.h>
23 #include <cppunit/TestAssert.h>
24 #include <cppunit/TestFixture.h>
25 #include <cppunit/extensions/HelperMacros.h>
26 #include <cppunit/plugin/TestPlugIn.h>
28 #include <rtl/ustrbuf.hxx>
30 #include <com/sun/star/util/DateTime.hpp>
31 #include <com/sun/star/util/Duration.hpp>
32 #include <com/sun/star/util/MeasureUnit.hpp>
34 #include <sax/tools/converter.hxx>
35 #include <sal/log.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::util
;
46 : public ::CppUnit::TestFixture
59 void testConvertMeasureUnit();
61 CPPUNIT_TEST_SUITE(ConverterTest
);
62 CPPUNIT_TEST(testDuration
);
63 CPPUNIT_TEST(testDateTime
);
64 CPPUNIT_TEST(testTime
);
65 CPPUNIT_TEST(testDouble
);
66 CPPUNIT_TEST(testMeasure
);
67 CPPUNIT_TEST(testBool
);
68 CPPUNIT_TEST(testPercent
);
69 CPPUNIT_TEST(testColor
);
70 CPPUNIT_TEST(testNumber
);
71 CPPUNIT_TEST(testConvertMeasureUnit
);
72 CPPUNIT_TEST_SUITE_END();
77 void doTest(util::Duration
const & rid
, char const*const pis
,
78 char const*const i_pos
= nullptr)
80 char const*const pos(i_pos
? i_pos
: pis
);
82 OUString
is(OUString::createFromAscii(pis
));
83 SAL_INFO("sax.cppunit","about to convert '" << is
<< "'");
84 bool bSuccess
= Converter::convertDuration(od
, is
);
85 SAL_INFO("sax.cppunit","" << (od
.Negative
? "-" : "+") << " " << od
.Years
<< "Y " << od
.Months
<< "M " << od
.Days
<< "D " << od
.Hours
<< "H " << od
.Minutes
<< "M " << od
.Seconds
<< "S " << od
.NanoSeconds
<< "n");
86 CPPUNIT_ASSERT(bSuccess
);
87 CPPUNIT_ASSERT_EQUAL(rid
.Years
, od
.Years
);
88 CPPUNIT_ASSERT_EQUAL(rid
.Months
, od
.Months
);
89 CPPUNIT_ASSERT_EQUAL(rid
.Days
, od
.Days
);
90 CPPUNIT_ASSERT_EQUAL(rid
.Hours
, od
.Hours
);
91 CPPUNIT_ASSERT_EQUAL(rid
.Minutes
, od
.Minutes
);
92 CPPUNIT_ASSERT_EQUAL(rid
.Seconds
, od
.Seconds
);
93 CPPUNIT_ASSERT_EQUAL(rid
.NanoSeconds
, od
.NanoSeconds
);
94 CPPUNIT_ASSERT_EQUAL(rid
.Negative
, od
.Negative
);
95 OUStringBuffer
buf(64);
96 Converter::convertDuration(buf
, od
);
97 SAL_INFO("sax.cppunit","" << buf
.toString());
98 CPPUNIT_ASSERT(buf
.makeStringAndClear().equalsAscii(pos
));
101 void doTestDurationF(char const*const pis
)
104 bool bSuccess
= Converter::convertDuration(od
,
105 OUString::createFromAscii(pis
));
106 SAL_INFO("sax.cppunit","" << (od
.Negative
? "-" : "+") << " " << od
.Years
<< "Y " << od
.Months
<< "M " << od
.Days
<< "D " << od
.Hours
<< "H " << od
.Minutes
<< "M " << od
.Seconds
<< "S " << od
.NanoSeconds
<< "n");
107 CPPUNIT_ASSERT_MESSAGE(pis
, !bSuccess
);
110 void ConverterTest::testDuration()
112 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
113 doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
114 doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
115 doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
116 doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
117 doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
118 doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
119 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190000000), "PT0.19S", "PT0.190000000S");
120 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90000000), "PT0.09S", "PT0.090000000S" );
121 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9000000), "PT0.009S", "PT0.009000000S" );
122 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.000000009S", "PT0.000000009S" );
123 doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999999999),
124 "PT9.999999999999999999999999999999S", "PT9.999999999S" );
125 doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
126 doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10000000),
127 "-P7Y6M5DT4H3M2.01000S", "-P7Y6M5DT4H3M2.010000000S" );
128 doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
129 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
130 doTestDurationF("1Y1M"); // invalid: no ^P
131 doTestDurationF("P-1Y1M"); // invalid: - after P
132 doTestDurationF("P1M1Y"); // invalid: Y after M
133 doTestDurationF("PT1Y"); // invalid: Y after T
134 doTestDurationF("P1Y1M1M"); // invalid: M twice, no T
135 doTestDurationF("P1YT1MT1M"); // invalid: T twice
136 doTestDurationF("P1YT"); // invalid: T but no H,M,S
137 doTestDurationF("P99999999999Y"); // cannot parse so many Ys
138 doTestDurationF("PT.1S"); // invalid: no 0 preceding .
139 doTestDurationF("PT5M.134S"); // invalid: no 0 preceding .
140 doTestDurationF("PT1.S"); // invalid: no digit following .
141 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
145 bool eqDateTime(const util::DateTime
& a
, const util::DateTime
& b
) {
146 return a
.Year
== b
.Year
&& a
.Month
== b
.Month
&& a
.Day
== b
.Day
147 && a
.Hours
== b
.Hours
&& a
.Minutes
== b
.Minutes
148 && a
.Seconds
== b
.Seconds
149 && a
.NanoSeconds
== b
.NanoSeconds
150 && a
.IsUTC
== b
.IsUTC
;
153 void doTest(util::DateTime
const & rdt
, char const*const pis
,
154 char const*const i_pos
= nullptr)
156 char const*const pos(i_pos
? i_pos
: pis
);
157 OUString
is(OUString::createFromAscii(pis
));
159 SAL_INFO("sax.cppunit","about to convert '" << is
<< "'");
160 bool bSuccess( Converter::parseDateTime(odt
, is
) );
161 SAL_INFO("sax.cppunit","Y:" << odt
.Year
<< " M:" << odt
.Month
<< " D:" << odt
.Day
<< " H:" << odt
.Hours
<< " M:" << odt
.Minutes
<< " S:" << odt
.Seconds
<< " nS:" << odt
.NanoSeconds
<< " UTC: " << static_cast<bool>(odt
.IsUTC
));
162 CPPUNIT_ASSERT(bSuccess
);
163 CPPUNIT_ASSERT(eqDateTime(rdt
, odt
));
164 OUStringBuffer
buf(32);
165 Converter::convertDateTime(buf
, odt
, nullptr, true);
166 SAL_INFO("sax.cppunit","" << buf
.toString());
167 CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos
),
168 buf
.makeStringAndClear());
171 void doTestDateTimeF(char const*const pis
)
174 bool bSuccess
= Converter::parseDateTime(odt
, OUString::createFromAscii(pis
));
175 SAL_INFO("sax.cppunit","Y:" << odt
.Year
<< " M:" << odt
.Month
<< " D:" << odt
.Day
<< " H:" << odt
.Hours
<< "H M:" << odt
.Minutes
<< " S:" << odt
.Seconds
<< " nS:" << odt
.NanoSeconds
);
176 CPPUNIT_ASSERT(!bSuccess
);
179 void ConverterTest::testDateTime()
181 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
182 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, false), "0001-01-01T00:00:00" );
183 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true), "0001-01-01T00:00:00Z" );
184 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1, false),
185 "-0001-01-01T00:00:00");
186 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
187 "-0001-01-01T01:00:00+01:00", "-0001-01-01T00:00:00Z");
188 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -324, false),
189 "-0324-01-01T00:00:00" );
190 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
191 "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00Z" );
192 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
193 "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00Z" );
194 doTest( util::DateTime(0, 0, 0, 12, 2, 1, 1, true),
195 "0001-01-02T00:00:00-12:00", "0001-01-02T12:00:00Z" );
196 doTest( util::DateTime(0, 0, 0, 12, 1, 1, 1, true),
197 "0001-01-02T00:00:00+12:00", "0001-01-01T12:00:00Z" );
198 doTest( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, false),
199 "9999-12-31T23:59:59.99", "9999-12-31T23:59:59.990000000" );
200 doTest( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, true),
201 "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.990000000Z" );
202 doTest( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, false),
203 "9999-12-31T23:59:59.9999999999999999999999999999999999999",
204 "9999-12-31T23:59:59.999999999" );
205 doTest( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, true),
206 "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
207 "9999-12-31T23:59:59.999999999Z" );
208 doTest( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
209 "2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00Z" );
210 doTest( util::DateTime(0, 0, 0, 0, 29, 2, 1600, true), // leap year
211 "1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00Z" );
212 doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333, false)
213 /*(0, 0, 0, 0, 2, 1, 333)*/,
214 "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
215 // While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
216 // in what we accept.
217 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
218 "1-01-01T00:00:00", "0001-01-01T00:00:00" );
219 doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
220 doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
221 doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
222 doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
223 doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
224 doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
225 doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
226 doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
227 doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
228 doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
229 doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
230 doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
231 doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
232 doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
233 doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
234 doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
235 doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
236 doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
237 doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
238 doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
239 doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
240 doTestDateTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
241 doTestDateTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
242 doTestDateTimeF( "00:00:00" ); // invalid: no date
243 doTestDateTimeF( "T00:00:00" ); // invalid: no date
244 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
247 void doTestTime(util::DateTime
const & rdt
, char const*const pis
,
248 char const*const i_pos
= nullptr)
250 char const*const pos(i_pos
? i_pos
: pis
);
251 OUString
is(OUString::createFromAscii(pis
));
253 SAL_INFO("sax.cppunit","about to convert '" << is
<< "'");
254 bool bSuccess( Converter::parseTimeOrDateTime(odt
, is
) );
255 SAL_INFO("sax.cppunit","Y:" << odt
.Year
<< " M:" << odt
.Month
<< " D:" << odt
.Day
<< " H:" << odt
.Hours
<< " M:" << odt
.Minutes
<< " S:" << odt
.Seconds
<< " nS:" << odt
.NanoSeconds
<< " UTC: " << static_cast<bool>(odt
.IsUTC
));
256 CPPUNIT_ASSERT(bSuccess
);
257 CPPUNIT_ASSERT(eqDateTime(rdt
, odt
));
258 OUStringBuffer
buf(32);
259 Converter::convertTimeOrDateTime(buf
, odt
);
260 SAL_INFO("sax.cppunit","" << buf
.toString());
261 CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos
),
262 buf
.makeStringAndClear());
265 void doTestTimeF(char const*const pis
)
268 bool bSuccess
= Converter::parseTimeOrDateTime(odt
, OUString::createFromAscii(pis
));
269 SAL_INFO("sax.cppunit","Y:" << odt
.Year
<< " M:" << odt
.Month
<< " D:" << odt
.Day
<< " H:" << odt
.Hours
<< "H M:" << odt
.Minutes
<< " S:" << odt
.Seconds
<< " nS:" << odt
.NanoSeconds
);
270 CPPUNIT_ASSERT_MESSAGE(pis
, !bSuccess
);
273 void ConverterTest::testTime() // time or dateTime + horrible backcompat mess
275 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
276 "0001-01-01T00:00:00" );
277 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
278 "0001-01-01T00:00:00" );
279 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
280 "0001-01-01T00:00:00Z" );
281 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, false),
282 "-0001-01-01T00:00:00");
283 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
284 "-0001-01-01T01:00:00+01:00", "-0001-01-01T00:00:00Z");
285 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -324, false),
286 "-0324-01-01T00:00:00" );
287 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
288 "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00Z" );
289 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
290 "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00Z" );
291 doTestTime( util::DateTime(0, 0, 0, 12, 2, 1, 1, true),
292 "0001-01-02T00:00:00-12:00", "0001-01-02T12:00:00Z" );
293 doTestTime( util::DateTime(0, 0, 0, 12, 1, 1, 1, true),
294 "0001-01-02T00:00:00+12:00", "0001-01-01T12:00:00Z" );
295 doTestTime( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, false),
296 "9999-12-31T23:59:59.99", "9999-12-31T23:59:59.990000000" );
297 doTestTime( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, true),
298 "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.990000000Z" );
299 doTestTime( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, false),
300 "9999-12-31T23:59:59.9999999999999999999999999999999999999",
301 "9999-12-31T23:59:59.999999999" );
302 doTestTime( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, true),
303 "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
304 "9999-12-31T23:59:59.999999999Z" );
305 doTestTime( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
306 "2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00Z" );
307 doTestTime( util::DateTime(0, 0, 0, 0, 29, 2, 1600, true), // leap year
308 "1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00Z" );
309 doTestTime( util::DateTime(0, 0, 0, 24, 1, 1, 333, false)
310 /*(0, 0, 0, 0, 2, 1, 333)*/,
311 "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
312 // While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
313 // in what we accept.
314 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
315 "1-01-01T00:00:00", "0001-01-01T00:00:00" );
317 doTestTime( util::DateTime(0, 0, 0, 0, 0, 0, 0, false), "00:00:00" );
318 doTestTime( util::DateTime(0, 0, 0, 24, 0, 0, 0, false), "24:00:00" );
319 doTestTime( util::DateTime(0, 0, 59, 0, 0, 0, 0, false), "00:59:00" );
320 doTestTime( util::DateTime(0, 1, 2, 4, 0, 0, 0, true), "04:02:01Z" );
321 doTestTime( util::DateTime(0, 1, 2, 4, 0, 0, 0, true),
322 "05:02:01+01:00", "04:02:01Z" );
323 doTestTime( util::DateTime(0, 11, 12, 9, 0, 0, 0, true),
324 "05:12:11-04:00", "09:12:11Z" );
325 doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, false),
326 "23:59:59.99", "23:59:59.990000000" );
327 doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, true),
328 "23:59:59.99Z", "23:59:59.990000000Z" );
329 // backwards compatible: recognize invalid 0000-00-00 date (LO 3.5)
330 doTestTime( util::DateTime(0, 1, 0, 0, 0, 0, 0, false),
331 "0000-00-00T00:00:01", "00:00:01" );
332 // backwards compatible: recognize invalid 0-00-00 date (OOo)
333 doTestTime( util::DateTime(0, 0, 1, 0, 0, 0, 0, false),
334 "0-00-00T00:01:00", "00:01:00" );
336 doTestTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
337 doTestTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
338 doTestTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
339 doTestTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
340 doTestTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
341 doTestTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
342 doTestTimeF( "0001-01-01T00:00:00." ); // invalid: .$
343 doTestTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
344 doTestTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
345 doTestTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
346 doTestTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
347 doTestTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
348 doTestTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
349 doTestTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
350 doTestTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
351 doTestTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
352 doTestTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
353 doTestTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
354 doTestTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
355 doTestTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
356 doTestTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
357 doTestTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
358 doTestTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
359 doTestTimeF( "T00:00:00" ); // invalid: T
360 doTestTimeF( "0:00:00" ); // invalid: < 2 H
361 doTestTimeF( "00:0:00" ); // invalid: < 2 M
362 doTestTimeF( "00:00:0" ); // invalid: < 2 S
363 doTestTimeF( "00:00:00." ); // invalid: .$
364 doTestTimeF( "00:00:00+1:00" ); // invalid: < 2 TZ H
365 doTestTimeF( "00:00:00+00:1" ); // invalid: < 2 TZ M
366 doTestTimeF( "25:00:00" ); // invalid: H > 24
367 doTestTimeF( "00:60:00" ); // invalid: M > 59
368 doTestTimeF( "00:00:60" ); // invalid: S > 59
369 doTestTimeF( "24:01:00" ); // invalid: H=24, but M != 0
370 doTestTimeF( "24:00:01" ); // invalid: H=24, but S != 0
371 doTestTimeF( "24:00:00.1" ); // invalid: H=24, but H != 0
372 doTestTimeF( "00:00:00+15:00" ); // invalid: TZ > +14:00
373 doTestTimeF( "00:00:00+14:01" ); // invalid: TZ > +14:00
374 doTestTimeF( "00:00:00-15:00" ); // invalid: TZ < -14:00
375 doTestTimeF( "00:00:00-14:01" ); // invalid: TZ < -14:00
378 void doTestDouble(char const*const pis
, double const rd
,
379 sal_Int16
const nSourceUnit
, sal_Int16
const nTargetUnit
)
381 OUString
const is(OUString::createFromAscii(pis
));
383 bool bSuccess(Converter::convertDouble(od
, is
, nSourceUnit
, nTargetUnit
));
384 SAL_INFO("sax.cppunit","" << od
);
385 CPPUNIT_ASSERT(bSuccess
);
386 CPPUNIT_ASSERT_DOUBLES_EQUAL(rd
, od
, 0.00000001);
388 Converter::convertDouble(buf
, od
, true, nTargetUnit
, nSourceUnit
);
389 SAL_INFO("sax.cppunit","" << buf
.toString());
390 CPPUNIT_ASSERT_EQUAL(is
, buf
.makeStringAndClear());
393 void ConverterTest::testDouble()
395 doTestDouble("42", 42.0, MeasureUnit::TWIP
, MeasureUnit::TWIP
);
396 doTestDouble("42", 42.0, MeasureUnit::POINT
, MeasureUnit::POINT
);
397 doTestDouble("42", 42.0, MeasureUnit::MM_100TH
, MeasureUnit::MM_100TH
);
398 doTestDouble("42", 42.0, MeasureUnit::MM_10TH
, MeasureUnit::MM_10TH
);
399 doTestDouble("42", 42.0, MeasureUnit::MM
, MeasureUnit::MM
); // identity don't seem to add unit?
400 doTestDouble("42", 42.0, MeasureUnit::CM
, MeasureUnit::CM
);
401 doTestDouble("42", 42.0, MeasureUnit::INCH
, MeasureUnit::INCH
);
402 doTestDouble("2pt", 40.0, MeasureUnit::POINT
, MeasureUnit::TWIP
);
403 doTestDouble("20pc", 1, MeasureUnit::TWIP
, MeasureUnit::POINT
);
404 doTestDouble("4", 2.26771653543307, MeasureUnit::MM_100TH
, MeasureUnit::TWIP
);
405 doTestDouble("4", 22.6771653543307, MeasureUnit::MM_10TH
, MeasureUnit::TWIP
);
406 doTestDouble("4mm", 226.771653543307, MeasureUnit::MM
, MeasureUnit::TWIP
);
407 doTestDouble("4cm", 2267.71653543307, MeasureUnit::CM
, MeasureUnit::TWIP
);
408 doTestDouble("4in", 5760.0, MeasureUnit::INCH
, MeasureUnit::TWIP
);
409 doTestDouble("1440pc", 1.0, MeasureUnit::TWIP
, MeasureUnit::INCH
);
410 doTestDouble("567pc", 1.000125, MeasureUnit::TWIP
, MeasureUnit::CM
);
411 doTestDouble("56.7pc", 1.000125, MeasureUnit::TWIP
, MeasureUnit::MM
);
412 doTestDouble("5.67pc", 1.000125, MeasureUnit::TWIP
, MeasureUnit::MM_10TH
);
413 doTestDouble("0.567pc", 1.000125, MeasureUnit::TWIP
, MeasureUnit::MM_100TH
);
414 doTestDouble("42pt", 1.4816666666666, MeasureUnit::POINT
, MeasureUnit::CM
);
415 doTestDouble("42pt", 14.816666666666, MeasureUnit::POINT
, MeasureUnit::MM
);
416 doTestDouble("42pt", 148.16666666666, MeasureUnit::POINT
, MeasureUnit::MM_10TH
);
417 doTestDouble("42pt", 1481.6666666666, MeasureUnit::POINT
, MeasureUnit::MM_100TH
);
418 doTestDouble("72pt", 1.0, MeasureUnit::POINT
, MeasureUnit::INCH
);
419 doTestDouble("3.5in", 8.89, MeasureUnit::INCH
, MeasureUnit::CM
);
420 doTestDouble("3.5in", 88.9, MeasureUnit::INCH
, MeasureUnit::MM
);
421 doTestDouble("3.5in", 889.0, MeasureUnit::INCH
, MeasureUnit::MM_10TH
);
422 doTestDouble("3.5in", 8890.0, MeasureUnit::INCH
, MeasureUnit::MM_100TH
);
423 doTestDouble("2in", 144, MeasureUnit::INCH
, MeasureUnit::POINT
);
424 doTestDouble("5.08cm", 2.0, MeasureUnit::CM
, MeasureUnit::INCH
);
425 doTestDouble("3.5cm", 3500.0, MeasureUnit::CM
, MeasureUnit::MM_100TH
);
426 doTestDouble("3.5cm", 350.0, MeasureUnit::CM
, MeasureUnit::MM_10TH
);
427 doTestDouble("3.5cm", 35.0, MeasureUnit::CM
, MeasureUnit::MM
);
428 doTestDouble("10cm", 283.464566929134, MeasureUnit::CM
, MeasureUnit::POINT
);
429 doTestDouble("0.5cm", 283.464566929134, MeasureUnit::CM
, MeasureUnit::TWIP
);
430 doTestDouble("10mm", 28.3464566929134, MeasureUnit::MM
, MeasureUnit::POINT
);
431 doTestDouble("0.5mm", 28.3464566929134, MeasureUnit::MM
, MeasureUnit::TWIP
);
432 doTestDouble("10", 2.83464566929134, MeasureUnit::MM_10TH
, MeasureUnit::POINT
);
433 doTestDouble("0.5", 2.83464566929134, MeasureUnit::MM_10TH
, MeasureUnit::TWIP
);
434 doTestDouble("10", 0.283464566929134, MeasureUnit::MM_100TH
, MeasureUnit::POINT
);
435 doTestDouble("0.5", 0.283464566929134, MeasureUnit::MM_100TH
, MeasureUnit::TWIP
);
436 doTestDouble("10mm", 1.0, MeasureUnit::MM
, MeasureUnit::CM
);
437 doTestDouble("10mm", 100.0, MeasureUnit::MM
, MeasureUnit::MM_10TH
);
438 doTestDouble("20mm", 2000.0, MeasureUnit::MM
, MeasureUnit::MM_100TH
);
439 doTestDouble("300", 30.0, MeasureUnit::MM_10TH
, MeasureUnit::MM
);
440 doTestDouble("400", 4.0, MeasureUnit::MM_100TH
, MeasureUnit::MM
);
441 doTestDouble("600", 6000.0, MeasureUnit::MM_10TH
, MeasureUnit::MM_100TH
);
442 doTestDouble("700", 70.0, MeasureUnit::MM_100TH
, MeasureUnit::MM_10TH
);
445 void doTestStringToMeasure(sal_Int32 rValue
, char const*const pis
, sal_Int16 nTargetUnit
, sal_Int32 nMin
, sal_Int32 nMax
)
447 OUString
const is(OUString::createFromAscii(pis
));
449 bool bSuccess(Converter::convertMeasure(nVal
, is
, nTargetUnit
, nMin
, nMax
));
450 SAL_INFO("sax.cppunit","" << nVal
);
451 CPPUNIT_ASSERT(bSuccess
);
452 CPPUNIT_ASSERT_EQUAL(rValue
, nVal
);
455 void doTestMeasureToString(char const*const pis
, sal_Int32 nMeasure
, sal_Int16
const nSourceUnit
, sal_Int16
const nTargetUnit
)
457 OUString
const is(OUString::createFromAscii(pis
));
459 Converter::convertMeasure(buf
, nMeasure
, nSourceUnit
, nTargetUnit
);
460 SAL_INFO("sax.cppunit","" << buf
.toString());
461 CPPUNIT_ASSERT_EQUAL(is
, buf
.makeStringAndClear());
464 void ConverterTest::testMeasure()
466 //check all the measure units
467 doTestStringToMeasure(1000, "10mm", MeasureUnit::MM_100TH
, -1, 4321);
468 doTestStringToMeasure(200, "20mm", MeasureUnit::MM_10TH
, 12, 4567);
469 doTestStringToMeasure(300, "300", MeasureUnit::MM
, 31, 555);
470 doTestStringToMeasure(400, "400", MeasureUnit::CM
, 10, 4321);
471 doTestStringToMeasure(120, "120", MeasureUnit::INCH_1000TH
, 10, 4321);
472 doTestStringToMeasure(111, "111", MeasureUnit::INCH_100TH
, 10, 4321);
473 doTestStringToMeasure(22, "22", MeasureUnit::INCH_10TH
, 10, 4321);
474 doTestStringToMeasure(27, "27", MeasureUnit::INCH
, 10, 4321);
475 doTestStringToMeasure(52, "52", MeasureUnit::POINT
, 10, 4321);
476 doTestStringToMeasure(120, "120", MeasureUnit::TWIP
, 10, 4321);
477 doTestStringToMeasure(666, "666", MeasureUnit::M
, 10, 4321);
478 doTestStringToMeasure(42, "42", MeasureUnit::KM
, 10, 4321);
479 doTestStringToMeasure(30, "30", MeasureUnit::PICA
, 10, 4321);
480 doTestStringToMeasure(20, "20", MeasureUnit::FOOT
, 10, 4321);
481 doTestStringToMeasure(40, "40", MeasureUnit::MILE
, 10, 4321);
482 doTestStringToMeasure(40, "40%", MeasureUnit::PERCENT
, 10, 4321);
483 doTestStringToMeasure(800, "800", MeasureUnit::PIXEL
, 10, 4321);
484 doTestStringToMeasure(600, "600px", MeasureUnit::PIXEL
, 10, 4321);
485 doTestStringToMeasure(777, "777", MeasureUnit::APPFONT
, 10, 4321);
486 doTestStringToMeasure(80000, "80000", MeasureUnit::SYSFONT
, 10, 432100);
487 //strange values (negative, too large etc.)
488 doTestStringToMeasure(555, "666", MeasureUnit::MM
, -1000, 555);
489 doTestStringToMeasure(-1000, "-1001", MeasureUnit::MM
, -1000, 555);
490 doTestStringToMeasure(0, "-0", MeasureUnit::MM
, -1, 0);
491 doTestStringToMeasure(::std::numeric_limits
<sal_Int32
>::max(), "1234567890mm", MeasureUnit::MM_10TH
, 12, ::std::numeric_limits
<sal_Int32
>::max());
492 doTestStringToMeasure(-300, "-300", MeasureUnit::MM
, -1000, 555);
493 doTestStringToMeasure(::std::numeric_limits
<sal_Int32
>::min(), "-999999999999999px", MeasureUnit::PIXEL
, ::std::numeric_limits
<sal_Int32
>::min(), 555); //really crazy numbers...
495 doTestMeasureToString("6mm", 600, MeasureUnit::MM_100TH
, MeasureUnit::MM
);
496 doTestMeasureToString("0.005cm", 000000005, MeasureUnit::MM_100TH
, MeasureUnit::CM
); // zeros in the front doesn't count
497 doTestMeasureToString("3mm", 30, MeasureUnit::MM_10TH
, MeasureUnit::MM
);
498 doTestMeasureToString("6.66cm", 666, MeasureUnit::MM_10TH
, MeasureUnit::CM
);
499 doTestMeasureToString("-157.3pt", -555, MeasureUnit::MM_10TH
, MeasureUnit::POINT
);
500 doTestMeasureToString("174976.378in", 44444000, MeasureUnit::MM_10TH
, MeasureUnit::INCH
); //let's check accuracy
501 doTestMeasureToString("40%", 40, MeasureUnit::PERCENT
, MeasureUnit::PERCENT
);
502 doTestMeasureToString("70.56mm", 4000, MeasureUnit::TWIP
, MeasureUnit::MM
);
503 doTestMeasureToString("979.928cm", 555550, MeasureUnit::TWIP
, MeasureUnit::CM
);
504 doTestMeasureToString("111.1pt", 2222, MeasureUnit::TWIP
, MeasureUnit::POINT
);
505 doTestMeasureToString("385.7986in", 555550, MeasureUnit::TWIP
, MeasureUnit::INCH
);
506 doTestMeasureToString("-2147483.648cm", std::numeric_limits
<sal_Int32
>::min(), MeasureUnit::MM_100TH
, MeasureUnit::CM
);
509 void doTestStringToBool(bool bBool
, char const*const pis
)
511 OUString
const is(OUString::createFromAscii(pis
));
513 bool bSuccess(Converter::convertBool(bTemp
, is
));
514 SAL_INFO("sax.cppunit","" << bTemp
);
515 CPPUNIT_ASSERT(bSuccess
);
516 CPPUNIT_ASSERT_EQUAL(bBool
, bTemp
);
520 void doTestBoolToString(char const*const pis
, bool bValue
)
522 OUString
const is(OUString::createFromAscii(pis
));
524 Converter::convertBool(buf
, bValue
);
525 SAL_INFO("sax.cppunit","" << buf
.toString());
526 CPPUNIT_ASSERT_EQUAL(is
, buf
.makeStringAndClear());
529 void ConverterTest::testBool()
531 doTestStringToBool(true, "true");
532 doTestStringToBool(false, "false");
533 doTestBoolToString("true", true);
534 doTestBoolToString("false", false);
537 void doTestStringToPercent(sal_Int32 nValue
, char const*const pis
)
539 OUString
const is(OUString::createFromAscii(pis
));
541 bool bSuccess(Converter::convertPercent(nTemp
, is
));
542 SAL_INFO("sax.cppunit","" << nTemp
);
543 CPPUNIT_ASSERT(bSuccess
);
544 CPPUNIT_ASSERT_EQUAL(nValue
, nTemp
);
547 void doTestPercentToString(char const*const pis
, sal_Int32 nValue
)
549 OUString
const is(OUString::createFromAscii(pis
));
551 Converter::convertPercent(buf
, nValue
);
552 SAL_INFO("sax.cppunit","" << buf
.toString());
553 CPPUNIT_ASSERT_EQUAL(is
, buf
.makeStringAndClear());
556 void ConverterTest::testPercent()
558 doTestStringToPercent(40, "40%");
559 doTestStringToPercent(30, "30");
560 doTestStringToPercent(120, "120%");
561 doTestStringToPercent(-40, "-40%");
562 doTestStringToPercent(0, "0%");
563 doTestPercentToString("12%", 12);
564 doTestPercentToString("-123%", -123);
565 doTestPercentToString("0%", 0);
566 doTestPercentToString("1%", 00001);
569 void doTestStringToColor(sal_Int32 nValue
, char const*const pis
)
571 OUString
const is(OUString::createFromAscii(pis
));
573 bool bSuccess(Converter::convertColor(nTemp
, is
));
574 SAL_INFO("sax.cppunit","" << nTemp
);
575 CPPUNIT_ASSERT(bSuccess
);
576 CPPUNIT_ASSERT_EQUAL(nValue
, nTemp
);
579 void doTestColorToString(char const*const pis
, sal_Int32 nValue
)
581 OUString
const is(OUString::createFromAscii(pis
));
583 Converter::convertColor(buf
, nValue
);
584 SAL_INFO("sax.cppunit","" << buf
.toString());
585 CPPUNIT_ASSERT_EQUAL(is
, buf
.makeStringAndClear());
588 void ConverterTest::testColor()
590 doTestStringToColor(11259375, "#abcdef");
591 doTestStringToColor(160, "#0000a0");
592 doTestStringToColor(40960, "#00a000");
593 doTestStringToColor(0, "#000000");
594 doTestColorToString("#000615", 1557);
595 doTestColorToString("#5bcd15", 123456789);
596 doTestColorToString("#fffac7", -1337);
597 doTestColorToString("#000000", 0);
600 void doTestStringToNumber(sal_Int32 nValue
, char const*const pis
, sal_Int32 nMin
, sal_Int32 nMax
)
602 OUString
const is(OUString::createFromAscii(pis
));
604 bool bSuccess(Converter::convertNumber(nTemp
, is
, nMin
, nMax
));
605 SAL_INFO("sax.cppunit","" << nTemp
);
606 CPPUNIT_ASSERT(bSuccess
);
607 CPPUNIT_ASSERT_EQUAL(nValue
, nTemp
);
610 void ConverterTest::testNumber()
612 doTestStringToNumber(30, "30", 1, 40);
613 doTestStringToNumber(1, "-5", 1, 300);
614 doTestStringToNumber(-30, "7", -100, -30);
615 doTestStringToNumber(0, "-0", 0, 1);
616 doTestStringToNumber(0, "666", -0, 0);
619 void ConverterTest::testConvertMeasureUnit()
621 auto fnFromStr
= [](std::string_view aStr
, double dExpValue
, std::optional
<sal_Int16
> nExpUnit
,
625 std::optional
<sal_Int16
> nUnit
;
627 bool bResult
= Converter::convertMeasureUnit(dValue
, nUnit
, aStr
);
629 CPPUNIT_ASSERT_EQUAL(bExpResult
, bResult
);
630 CPPUNIT_ASSERT_DOUBLES_EQUAL(dExpValue
, dValue
, 0.00001);
631 CPPUNIT_ASSERT_EQUAL(nExpUnit
.has_value(), nUnit
.has_value());
633 if (nExpUnit
.has_value())
635 CPPUNIT_ASSERT_EQUAL(nExpUnit
.value(), nUnit
.value());
639 auto fnToStr
= [](double dValue
, std::optional
<sal_Int16
> nValueUnit
) -> OUString
641 OUStringBuffer stBuf
;
642 Converter::convertMeasureUnit(stBuf
, dValue
, nValueUnit
);
643 return stBuf
.makeStringAndClear();
646 // Characteristic cases without unit parsing
647 fnFromStr("5000", 5000.0, std::nullopt
, true);
648 fnFromStr("-123", -123.0, std::nullopt
, true);
649 CPPUNIT_ASSERT_EQUAL(u
"5000"_ustr
, fnToStr(5000.0, std::nullopt
));
650 CPPUNIT_ASSERT_EQUAL(u
"-123"_ustr
, fnToStr(-123.0, std::nullopt
));
652 // Characteristic case with invalid unit
653 fnFromStr("5000xy", 0.0, std::nullopt
, false);
655 // Characteristic cases for unit printing
656 CPPUNIT_ASSERT_EQUAL(u
"5000em"_ustr
, fnToStr(5000.0, MeasureUnit::FONT_EM
));
657 CPPUNIT_ASSERT_EQUAL(u
"-123%"_ustr
, fnToStr(-123.0, MeasureUnit::PERCENT
));
659 // Branch coverage for unit parsing
660 fnFromStr("5000%", 5000.0, MeasureUnit::PERCENT
, true);
661 fnFromStr("5000cm", 5000.0, MeasureUnit::CM
, true);
662 fnFromStr("5000em", 5000.0, MeasureUnit::FONT_EM
, true);
663 fnFromStr("5000ic", 5000.0, MeasureUnit::FONT_CJK_ADVANCE
, true);
664 fnFromStr("5000in", 5000.0, MeasureUnit::INCH
, true);
665 fnFromStr("5000mm", 5000.0, MeasureUnit::MM
, true);
666 fnFromStr("5000pt", 5000.0, MeasureUnit::POINT
, true);
667 fnFromStr("5000pc", 5000.0, MeasureUnit::PICA
, true);
668 fnFromStr("5000px", 5000.0, MeasureUnit::PIXEL
, true);
670 // All units should be case-insensitive
671 fnFromStr("5000cm", 5000.0, MeasureUnit::CM
, true);
672 fnFromStr("5000Cm", 5000.0, MeasureUnit::CM
, true);
673 fnFromStr("5000cM", 5000.0, MeasureUnit::CM
, true);
674 fnFromStr("5000CM", 5000.0, MeasureUnit::CM
, true);
675 fnFromStr("5000px", 5000.0, MeasureUnit::PIXEL
, true);
676 fnFromStr("5000Px", 5000.0, MeasureUnit::PIXEL
, true);
677 fnFromStr("5000pX", 5000.0, MeasureUnit::PIXEL
, true);
678 fnFromStr("5000PX", 5000.0, MeasureUnit::PIXEL
, true);
680 // Characteristic cases for whitespace between numbers and units
681 fnFromStr("5000 cm", 5000.0, MeasureUnit::CM
, true);
682 fnFromStr("5000\t\tcm", 5000.0, MeasureUnit::CM
, true);
684 // tdf#36709: Measure conversion was refactored to isolate parsing and unit conversion.
685 // Some of the unit parsing code looks suspicious. The current behavior is correct, but could
686 // be prone to well-meaning breakage (e.g. refactoring, argument reordering). The following
687 // cases exercise relevant edge cases.
689 // The tail after percent is always ignored
690 fnFromStr("5000 %%", 5000.0, MeasureUnit::PERCENT
, true);
691 fnFromStr("5000 % ", 5000.0, MeasureUnit::PERCENT
, true);
692 fnFromStr("5000 %cmcmcmcm cm", 5000.0, MeasureUnit::PERCENT
, true);
694 // The tail after other units, however, is not ignored
695 fnFromStr("5000 cmc", 0.0, std::nullopt
, false);
696 fnFromStr("5000 ccm", 0.0, std::nullopt
, false);
698 // Whitespace is allowed after units, but not inside units
699 fnFromStr("5000 c m", 0.0, std::nullopt
, false);
700 fnFromStr("5000 cm ", 5000.0, MeasureUnit::CM
, true);
703 CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest
);
707 CPPUNIT_PLUGIN_IMPLEMENT();
709 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */