Bump version to 5.0-14
[LibreOffice.git] / sax / qa / cppunit / test_converter.cxx
blob6de224a84151c9b9d88dec1e00cb5b66c53fc5bf
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 .
20 #include <limits>
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/Date.hpp>
32 #include <com/sun/star/util/Duration.hpp>
33 #include <com/sun/star/util/MeasureUnit.hpp>
35 #include "sax/tools/converter.hxx"
36 #include <comphelper/sequence.hxx>
37 #include "sal/log.hxx"
40 using namespace ::com::sun::star;
41 using namespace ::com::sun::star::util;
42 using sax::Converter;
45 namespace {
47 class ConverterTest
48 : public ::CppUnit::TestFixture
50 public:
51 virtual void setUp() SAL_OVERRIDE;
52 virtual void tearDown() SAL_OVERRIDE;
54 void testDuration();
55 void testDateTime();
56 void testTime();
57 void testDouble();
58 void testMeasure();
59 void testBool();
60 void testPercent();
61 void testColor();
62 void testNumber();
63 void testBase64();
65 CPPUNIT_TEST_SUITE(ConverterTest);
66 CPPUNIT_TEST(testDuration);
67 CPPUNIT_TEST(testDateTime);
68 CPPUNIT_TEST(testTime);
69 CPPUNIT_TEST(testDouble);
70 CPPUNIT_TEST(testMeasure);
71 CPPUNIT_TEST(testBool);
72 CPPUNIT_TEST(testPercent);
73 CPPUNIT_TEST(testColor);
74 CPPUNIT_TEST(testNumber);
75 CPPUNIT_TEST(testBase64);
76 CPPUNIT_TEST_SUITE_END();
78 private:
81 void ConverterTest::setUp()
85 void ConverterTest::tearDown()
89 static void doTest(util::Duration const & rid, char const*const pis,
90 char const*const i_pos = 0)
92 char const*const pos((i_pos) ? i_pos : pis);
93 util::Duration od;
94 OUString is(::rtl::OUString::createFromAscii(pis));
95 SAL_INFO("sax.cppunit","about to convert '" << is << "'");
96 bool bSuccess = Converter::convertDuration(od, is);
97 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");
98 CPPUNIT_ASSERT(bSuccess);
99 CPPUNIT_ASSERT_EQUAL(rid.Years, od.Years);
100 CPPUNIT_ASSERT_EQUAL(rid.Months, od.Months);
101 CPPUNIT_ASSERT_EQUAL(rid.Days, od.Days);
102 CPPUNIT_ASSERT_EQUAL(rid.Hours, od.Hours);
103 CPPUNIT_ASSERT_EQUAL(rid.Minutes, od.Minutes);
104 CPPUNIT_ASSERT_EQUAL(rid.Seconds, od.Seconds);
105 CPPUNIT_ASSERT_EQUAL(rid.NanoSeconds, od.NanoSeconds);
106 CPPUNIT_ASSERT_EQUAL(rid.Negative, od.Negative);
107 OUStringBuffer buf;
108 Converter::convertDuration(buf, od);
109 SAL_INFO("sax.cppunit","" << buf.toString());
110 CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
113 static void doTestDurationF(char const*const pis)
115 util::Duration od;
116 bool bSuccess = Converter::convertDuration(od,
117 OUString::createFromAscii(pis));
118 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");
119 CPPUNIT_ASSERT_MESSAGE(pis, !bSuccess);
122 void ConverterTest::testDuration()
124 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
125 doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
126 doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
127 doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
128 doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
129 doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
130 doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
131 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190000000), "PT0.19S", "PT0.190000000S");
132 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90000000), "PT0.09S", "PT0.090000000S" );
133 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9000000), "PT0.009S", "PT0.009000000S" );
134 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.000000009S", "PT0.000000009S" );
135 doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999999999),
136 "PT9.999999999999999999999999999999S", "PT9.999999999S" );
137 doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
138 doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10000000),
139 "-P7Y6M5DT4H3M2.01000S", "-P7Y6M5DT4H3M2.010000000S" );
140 doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
141 doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
142 doTestDurationF("1Y1M"); // invalid: no ^P
143 doTestDurationF("P-1Y1M"); // invalid: - after P
144 doTestDurationF("P1M1Y"); // invalid: Y after M
145 doTestDurationF("PT1Y"); // invalid: Y after T
146 doTestDurationF("P1Y1M1M"); // invalid: M twice, no T
147 doTestDurationF("P1YT1MT1M"); // invalid: T twice
148 doTestDurationF("P1YT"); // invalid: T but no H,M,S
149 doTestDurationF("P99999999999Y"); // cannot parse so many Ys
150 doTestDurationF("PT.1S"); // invalid: no 0 preceding .
151 doTestDurationF("PT5M.134S"); // invalid: no 0 preceding .
152 doTestDurationF("PT1.S"); // invalid: no digit following .
153 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
157 static bool eqDateTime(const util::DateTime& a, const util::DateTime& b) {
158 return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
159 && a.Hours == b.Hours && a.Minutes == b.Minutes
160 && a.Seconds == b.Seconds
161 && a.NanoSeconds == b.NanoSeconds
162 && a.IsUTC == b.IsUTC;
165 static void doTest(util::DateTime const & rdt, char const*const pis,
166 char const*const i_pos = 0)
168 char const*const pos((i_pos) ? i_pos : pis);
169 OUString is(OUString::createFromAscii(pis));
170 util::DateTime odt;
171 SAL_INFO("sax.cppunit","about to convert '" << is << "'");
172 bool bSuccess( Converter::parseDateTime(odt, 0, is) );
173 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: " << (bool)odt.IsUTC);
174 CPPUNIT_ASSERT(bSuccess);
175 CPPUNIT_ASSERT(eqDateTime(rdt, odt));
176 OUStringBuffer buf;
177 Converter::convertDateTime(buf, odt, 0, true);
178 SAL_INFO("sax.cppunit","" << buf.toString());
179 CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos),
180 buf.makeStringAndClear());
183 static void doTestDateTimeF(char const*const pis)
185 util::DateTime odt;
186 bool bSuccess = Converter::parseDateTime(odt, 0,
187 OUString::createFromAscii(pis));
188 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);
189 CPPUNIT_ASSERT(!bSuccess);
192 void ConverterTest::testDateTime()
194 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
195 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, false), "0001-01-01T00:00:00" );
196 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true), "0001-01-01T00:00:00Z" );
197 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1, false),
198 "-0001-01-01T00:00:00");
199 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
200 "-0001-01-01T01:00:00+01:00", "-0001-01-01T00:00:00Z");
201 doTest( util::DateTime(0, 0, 0, 0, 1, 1, -324, false),
202 "-0324-01-01T00:00:00" );
203 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
204 "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00Z" );
205 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
206 "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00Z" );
207 doTest( util::DateTime(0, 0, 0, 12, 2, 1, 1, true),
208 "0001-01-02T00:00:00-12:00", "0001-01-02T12:00:00Z" );
209 doTest( util::DateTime(0, 0, 0, 12, 1, 1, 1, true),
210 "0001-01-02T00:00:00+12:00", "0001-01-01T12:00:00Z" );
211 doTest( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, false),
212 "9999-12-31T23:59:59.99", "9999-12-31T23:59:59.990000000" );
213 doTest( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, true),
214 "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.990000000Z" );
215 doTest( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, false),
216 "9999-12-31T23:59:59.9999999999999999999999999999999999999",
217 "9999-12-31T23:59:59.999999999" );
218 doTest( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, true),
219 "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
220 "9999-12-31T23:59:59.999999999Z" );
221 doTest( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
222 "2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00Z" );
223 doTest( util::DateTime(0, 0, 0, 0, 29, 2, 1600, true), // leap year
224 "1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00Z" );
225 doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333, false)
226 /*(0, 0, 0, 0, 2, 1, 333)*/,
227 "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
228 // While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
229 // in what we accept.
230 doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
231 "1-01-01T00:00:00", "0001-01-01T00:00:00" );
232 doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
233 doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
234 doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
235 doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
236 doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
237 doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
238 doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
239 doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
240 doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
241 doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
242 doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
243 doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
244 doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
245 doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
246 doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
247 doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
248 doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
249 doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
250 doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
251 doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
252 doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
253 doTestDateTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
254 doTestDateTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
255 doTestDateTimeF( "00:00:00" ); // invalid: no date
256 doTestDateTimeF( "T00:00:00" ); // invalid: no date
257 SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
260 static void doTestTime(util::DateTime const & rdt, char const*const pis,
261 char const*const i_pos = 0)
263 char const*const pos((i_pos) ? i_pos : pis);
264 OUString is(OUString::createFromAscii(pis));
265 util::DateTime odt;
266 SAL_INFO("sax.cppunit","about to convert '" << is << "'");
267 bool bSuccess( Converter::parseTimeOrDateTime(odt, 0, is) );
268 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: " << (bool)odt.IsUTC);
269 CPPUNIT_ASSERT(bSuccess);
270 CPPUNIT_ASSERT(eqDateTime(rdt, odt));
271 OUStringBuffer buf;
272 Converter::convertTimeOrDateTime(buf, odt, 0);
273 SAL_INFO("sax.cppunit","" << buf.toString());
274 CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos),
275 buf.makeStringAndClear());
278 static void doTestTimeF(char const*const pis)
280 util::DateTime odt;
281 bool bSuccess = Converter::parseTimeOrDateTime(odt, 0,
282 OUString::createFromAscii(pis));
283 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);
284 CPPUNIT_ASSERT_MESSAGE(pis, !bSuccess);
287 void ConverterTest::testTime() // time or dateTime + horrible backcompat mess
289 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
290 "0001-01-01T00:00:00" );
291 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
292 "0001-01-01T00:00:00" );
293 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
294 "0001-01-01T00:00:00Z" );
295 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, false),
296 "-0001-01-01T00:00:00");
297 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
298 "-0001-01-01T01:00:00+01:00", "-0001-01-01T00:00:00Z");
299 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -324, false),
300 "-0324-01-01T00:00:00" );
301 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
302 "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00Z" );
303 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
304 "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00Z" );
305 doTestTime( util::DateTime(0, 0, 0, 12, 2, 1, 1, true),
306 "0001-01-02T00:00:00-12:00", "0001-01-02T12:00:00Z" );
307 doTestTime( util::DateTime(0, 0, 0, 12, 1, 1, 1, true),
308 "0001-01-02T00:00:00+12:00", "0001-01-01T12:00:00Z" );
309 doTestTime( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, false),
310 "9999-12-31T23:59:59.99", "9999-12-31T23:59:59.990000000" );
311 doTestTime( util::DateTime(990000000, 59, 59, 23, 31, 12, 9999, true),
312 "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.990000000Z" );
313 doTestTime( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, false),
314 "9999-12-31T23:59:59.9999999999999999999999999999999999999",
315 "9999-12-31T23:59:59.999999999" );
316 doTestTime( util::DateTime(999999999, 59, 59, 23, 31, 12, 9999, true),
317 "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
318 "9999-12-31T23:59:59.999999999Z" );
319 doTestTime( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
320 "2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00Z" );
321 doTestTime( util::DateTime(0, 0, 0, 0, 29, 2, 1600, true), // leap year
322 "1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00Z" );
323 doTestTime( util::DateTime(0, 0, 0, 24, 1, 1, 333, false)
324 /*(0, 0, 0, 0, 2, 1, 333)*/,
325 "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
326 // While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
327 // in what we accept.
328 doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
329 "1-01-01T00:00:00", "0001-01-01T00:00:00" );
331 doTestTime( util::DateTime(0, 0, 0, 0, 0, 0, 0, false), "00:00:00" );
332 doTestTime( util::DateTime(0, 0, 0, 24, 0, 0, 0, false), "24:00:00" );
333 doTestTime( util::DateTime(0, 0, 59, 0, 0, 0, 0, false), "00:59:00" );
334 doTestTime( util::DateTime(0, 1, 2, 4, 0, 0, 0, true), "04:02:01Z" );
335 doTestTime( util::DateTime(0, 1, 2, 4, 0, 0, 0, true),
336 "05:02:01+01:00", "04:02:01Z" );
337 doTestTime( util::DateTime(0, 11, 12, 9, 0, 0, 0, true),
338 "05:12:11-04:00", "09:12:11Z" );
339 doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, false),
340 "23:59:59.99", "23:59:59.990000000" );
341 doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, true),
342 "23:59:59.99Z", "23:59:59.990000000Z" );
343 // backwards compatible: recognize invalid 0000-00-00 date (LO 3.5)
344 doTestTime( util::DateTime(0, 1, 0, 0, 0, 0, 0, false),
345 "0000-00-00T00:00:01", "00:00:01" );
346 // backwards compatible: recognize invalid 0-00-00 date (OOo)
347 doTestTime( util::DateTime(0, 0, 1, 0, 0, 0, 0, false),
348 "0-00-00T00:01:00", "00:01:00" );
350 doTestTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
351 doTestTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
352 doTestTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
353 doTestTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
354 doTestTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
355 doTestTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
356 doTestTimeF( "0001-01-01T00:00:00." ); // invalid: .$
357 doTestTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
358 doTestTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
359 doTestTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
360 doTestTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
361 doTestTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
362 doTestTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
363 doTestTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
364 doTestTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
365 doTestTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
366 doTestTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
367 doTestTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
368 doTestTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
369 doTestTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
370 doTestTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
371 doTestTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
372 doTestTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
373 doTestTimeF( "T00:00:00" ); // invalid: T
374 doTestTimeF( "0:00:00" ); // invalid: < 2 H
375 doTestTimeF( "00:0:00" ); // invalid: < 2 M
376 doTestTimeF( "00:00:0" ); // invalid: < 2 S
377 doTestTimeF( "00:00:00." ); // invalid: .$
378 doTestTimeF( "00:00:00+1:00" ); // invalid: < 2 TZ H
379 doTestTimeF( "00:00:00+00:1" ); // invalid: < 2 TZ M
380 doTestTimeF( "25:00:00" ); // invalid: H > 24
381 doTestTimeF( "00:60:00" ); // invalid: M > 59
382 doTestTimeF( "00:00:60" ); // invalid: S > 59
383 doTestTimeF( "24:01:00" ); // invalid: H=24, but M != 0
384 doTestTimeF( "24:00:01" ); // invalid: H=24, but S != 0
385 doTestTimeF( "24:00:00.1" ); // invalid: H=24, but H != 0
386 doTestTimeF( "00:00:00+15:00" ); // invalid: TZ > +14:00
387 doTestTimeF( "00:00:00+14:01" ); // invalid: TZ > +14:00
388 doTestTimeF( "00:00:00-15:00" ); // invalid: TZ < -14:00
389 doTestTimeF( "00:00:00-14:01" ); // invalid: TZ < -14:00
392 void doTestDouble(char const*const pis, double const rd,
393 sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
395 OUString const is(OUString::createFromAscii(pis));
396 double od;
397 bool bSuccess(Converter::convertDouble(od, is, nSourceUnit, nTargetUnit));
398 SAL_INFO("sax.cppunit","" << od);
399 CPPUNIT_ASSERT(bSuccess);
400 CPPUNIT_ASSERT_DOUBLES_EQUAL(rd, od, 0.00000001);
401 OUStringBuffer buf;
402 Converter::convertDouble(buf, od, true, nTargetUnit, nSourceUnit);
403 SAL_INFO("sax.cppunit","" << buf.toString());
404 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
407 void ConverterTest::testDouble()
409 doTestDouble("42", 42.0, MeasureUnit::TWIP, MeasureUnit::TWIP);
410 doTestDouble("42", 42.0, MeasureUnit::POINT, MeasureUnit::POINT);
411 doTestDouble("42", 42.0, MeasureUnit::MM_100TH, MeasureUnit::MM_100TH);
412 doTestDouble("42", 42.0, MeasureUnit::MM_10TH, MeasureUnit::MM_10TH);
413 doTestDouble("42", 42.0, MeasureUnit::MM, MeasureUnit::MM); // identity don't seem to add unit?
414 doTestDouble("42", 42.0, MeasureUnit::CM, MeasureUnit::CM);
415 doTestDouble("42", 42.0, MeasureUnit::INCH, MeasureUnit::INCH);
416 doTestDouble("2pt", 40.0, MeasureUnit::POINT, MeasureUnit::TWIP);
417 doTestDouble("20pc", 1, MeasureUnit::TWIP, MeasureUnit::POINT);
418 doTestDouble("4", 2.26771653543307, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
419 doTestDouble("4", 22.6771653543307, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
420 doTestDouble("4mm", 226.771653543307, MeasureUnit::MM, MeasureUnit::TWIP);
421 doTestDouble("4cm", 2267.71653543307, MeasureUnit::CM, MeasureUnit::TWIP);
422 doTestDouble("4in", 5760.0, MeasureUnit::INCH, MeasureUnit::TWIP);
423 doTestDouble("1440pc", 1.0, MeasureUnit::TWIP, MeasureUnit::INCH);
424 doTestDouble("567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::CM);
425 doTestDouble("56.7pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM);
426 doTestDouble("5.67pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_10TH);
427 doTestDouble("0.567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_100TH);
428 doTestDouble("42pt", 1.4816666666666, MeasureUnit::POINT, MeasureUnit::CM);
429 doTestDouble("42pt", 14.816666666666, MeasureUnit::POINT, MeasureUnit::MM);
430 doTestDouble("42pt", 148.16666666666, MeasureUnit::POINT, MeasureUnit::MM_10TH);
431 doTestDouble("42pt", 1481.6666666666, MeasureUnit::POINT, MeasureUnit::MM_100TH);
432 doTestDouble("72pt", 1.0, MeasureUnit::POINT, MeasureUnit::INCH);
433 doTestDouble("3.5in", 8.89, MeasureUnit::INCH, MeasureUnit::CM);
434 doTestDouble("3.5in", 88.9, MeasureUnit::INCH, MeasureUnit::MM);
435 doTestDouble("3.5in", 889.0, MeasureUnit::INCH, MeasureUnit::MM_10TH);
436 doTestDouble("3.5in", 8890.0, MeasureUnit::INCH, MeasureUnit::MM_100TH);
437 doTestDouble("2in", 144, MeasureUnit::INCH, MeasureUnit::POINT);
438 doTestDouble("5.08cm", 2.0, MeasureUnit::CM, MeasureUnit::INCH);
439 doTestDouble("3.5cm", 3500.0, MeasureUnit::CM, MeasureUnit::MM_100TH);
440 doTestDouble("3.5cm", 350.0, MeasureUnit::CM, MeasureUnit::MM_10TH);
441 doTestDouble("3.5cm", 35.0, MeasureUnit::CM, MeasureUnit::MM);
442 doTestDouble("10cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::POINT);
443 doTestDouble("0.5cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::TWIP);
444 doTestDouble("10mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::POINT);
445 doTestDouble("0.5mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::TWIP);
446 doTestDouble("10", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::POINT);
447 doTestDouble("0.5", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
448 doTestDouble("10", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::POINT);
449 doTestDouble("0.5", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
450 doTestDouble("10mm", 1.0, MeasureUnit::MM, MeasureUnit::CM);
451 doTestDouble("10mm", 100.0, MeasureUnit::MM, MeasureUnit::MM_10TH);
452 doTestDouble("20mm", 2000.0, MeasureUnit::MM, MeasureUnit::MM_100TH);
453 doTestDouble("300", 30.0, MeasureUnit::MM_10TH, MeasureUnit::MM);
454 doTestDouble("400", 4.0, MeasureUnit::MM_100TH, MeasureUnit::MM);
455 doTestDouble("600", 6000.0, MeasureUnit::MM_10TH, MeasureUnit::MM_100TH);
456 doTestDouble("700", 70.0, MeasureUnit::MM_100TH, MeasureUnit::MM_10TH);
459 void doTestStringToMeasure(sal_Int32 rValue, char const*const pis, sal_Int16 nTargetUnit, sal_Int32 nMin, sal_Int32 nMax)
461 OUString const is(OUString::createFromAscii(pis));
462 sal_Int32 nVal;
463 bool bSuccess(Converter::convertMeasure(nVal, is, nTargetUnit, nMin, nMax));
464 SAL_INFO("sax.cppunit","" << nVal);
465 CPPUNIT_ASSERT(bSuccess);
466 CPPUNIT_ASSERT_EQUAL(rValue, nVal);
469 void doTestMeasureToString(char const*const pis, sal_Int32 nMeasure, sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
471 OUString const is(OUString::createFromAscii(pis));
472 OUStringBuffer buf;
473 Converter::convertMeasure(buf, nMeasure, nSourceUnit, nTargetUnit);
474 SAL_INFO("sax.cppunit","" << buf.toString());
475 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
478 void ConverterTest::testMeasure()
480 //check all the measure units
481 doTestStringToMeasure(1000, "10mm", MeasureUnit::MM_100TH, -1, 4321);
482 doTestStringToMeasure(200, "20mm", MeasureUnit::MM_10TH, 12, 4567);
483 doTestStringToMeasure(300, "300", MeasureUnit::MM, 31, 555);
484 doTestStringToMeasure(400, "400", MeasureUnit::CM, 10, 4321);
485 doTestStringToMeasure(120, "120", MeasureUnit::INCH_1000TH, 10, 4321);
486 doTestStringToMeasure(111, "111", MeasureUnit::INCH_100TH, 10, 4321);
487 doTestStringToMeasure(22, "22", MeasureUnit::INCH_10TH, 10, 4321);
488 doTestStringToMeasure(27, "27", MeasureUnit::INCH, 10, 4321);
489 doTestStringToMeasure(52, "52", MeasureUnit::POINT, 10, 4321);
490 doTestStringToMeasure(120, "120", MeasureUnit::TWIP, 10, 4321);
491 doTestStringToMeasure(666, "666", MeasureUnit::M, 10, 4321);
492 doTestStringToMeasure(42, "42", MeasureUnit::KM, 10, 4321);
493 doTestStringToMeasure(30, "30", MeasureUnit::PICA, 10, 4321);
494 doTestStringToMeasure(20, "20", MeasureUnit::FOOT, 10, 4321);
495 doTestStringToMeasure(40, "40", MeasureUnit::MILE, 10, 4321);
496 doTestStringToMeasure(40, "40%", MeasureUnit::PERCENT, 10, 4321);
497 doTestStringToMeasure(800, "800", MeasureUnit::PIXEL, 10, 4321);
498 doTestStringToMeasure(600, "600px", MeasureUnit::PIXEL, 10, 4321);
499 doTestStringToMeasure(777, "777", MeasureUnit::APPFONT, 10, 4321);
500 doTestStringToMeasure(80000, "80000", MeasureUnit::SYSFONT, 10, 432100);
501 //strange values (negative, too large etc.)
502 doTestStringToMeasure(555, "666", MeasureUnit::MM, -1000, 555);
503 doTestStringToMeasure(-1000, "-1001", MeasureUnit::MM, -1000, 555);
504 doTestStringToMeasure(0, "-0", MeasureUnit::MM, -1, 0);
505 doTestStringToMeasure(::std::numeric_limits<sal_Int32>::max(), "1234567890mm", MeasureUnit::MM_10TH, 12, ::std::numeric_limits<sal_Int32>::max());
506 doTestStringToMeasure(-300, "-300", MeasureUnit::MM, -1000, 555);
507 doTestStringToMeasure(::std::numeric_limits<sal_Int32>::min(), "-999999999999999px", MeasureUnit::PIXEL, ::std::numeric_limits<sal_Int32>::min(), 555); //really crazy numbers...
509 doTestMeasureToString("6mm", 600, MeasureUnit::MM_100TH, MeasureUnit::MM);
510 doTestMeasureToString("0.005cm", 000000005, MeasureUnit::MM_100TH, MeasureUnit::CM); // zeros in the front doesn't count
511 doTestMeasureToString("3mm", 30, MeasureUnit::MM_10TH, MeasureUnit::MM);
512 doTestMeasureToString("6.66cm", 666, MeasureUnit::MM_10TH, MeasureUnit::CM);
513 doTestMeasureToString("-157.3pt", -555, MeasureUnit::MM_10TH, MeasureUnit::POINT);
514 doTestMeasureToString("174976.378in", 44444000, MeasureUnit::MM_10TH, MeasureUnit::INCH); //let's check accuracy
515 doTestMeasureToString("40%", 40, MeasureUnit::PERCENT, MeasureUnit::PERCENT);
516 doTestMeasureToString("70.56mm", 4000, MeasureUnit::TWIP, MeasureUnit::MM);
517 doTestMeasureToString("979.928cm", 555550, MeasureUnit::TWIP, MeasureUnit::CM);
518 doTestMeasureToString("111.1pt", 2222, MeasureUnit::TWIP, MeasureUnit::POINT);
519 doTestMeasureToString("385.7986in", 555550, MeasureUnit::TWIP, MeasureUnit::INCH);
522 void doTestStringToBool(bool bBool, char const*const pis)
524 OUString const is(OUString::createFromAscii(pis));
525 bool bTemp;
526 bool bSuccess(Converter::convertBool(bTemp, is));
527 SAL_INFO("sax.cppunit","" << bTemp);
528 CPPUNIT_ASSERT(bSuccess);
529 CPPUNIT_ASSERT_EQUAL(bBool, bTemp);
533 void doTestBoolToString(char const*const pis, bool bValue )
535 OUString const is(OUString::createFromAscii(pis));
536 OUStringBuffer buf;
537 Converter::convertBool(buf, bValue);
538 SAL_INFO("sax.cppunit","" << buf.toString());
539 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
542 void ConverterTest::testBool()
544 doTestStringToBool(true, "true");
545 doTestStringToBool(false, "false");
546 doTestBoolToString("true", true);
547 doTestBoolToString("false", false);
550 void doTestStringToPercent(sal_Int32 nValue, char const*const pis)
552 OUString const is(OUString::createFromAscii(pis));
553 sal_Int32 nTemp;
554 bool bSuccess(Converter::convertPercent(nTemp, is));
555 SAL_INFO("sax.cppunit","" << nTemp);
556 CPPUNIT_ASSERT(bSuccess);
557 CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
560 void doTestPercentToString(char const*const pis, sal_Int32 nValue)
562 OUString const is(OUString::createFromAscii(pis));
563 OUStringBuffer buf;
564 Converter::convertPercent(buf, nValue);
565 SAL_INFO("sax.cppunit","" << buf.toString());
566 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
569 void ConverterTest::testPercent()
571 doTestStringToPercent(40, "40%");
572 doTestStringToPercent(30, "30");
573 doTestStringToPercent(120, "120%");
574 doTestStringToPercent(-40, "-40%");
575 doTestStringToPercent(0, "0%");
576 doTestPercentToString("12%", 12);
577 doTestPercentToString("-123%", -123);
578 doTestPercentToString("0%", 0);
579 doTestPercentToString("1%", 00001);
582 void doTestStringToColor(sal_Int32 nValue, char const*const pis)
584 OUString const is(OUString::createFromAscii(pis));
585 sal_Int32 nTemp;
586 bool bSuccess(Converter::convertColor(nTemp, is));
587 SAL_INFO("sax.cppunit","" << nTemp);
588 CPPUNIT_ASSERT(bSuccess);
589 CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
592 void doTestColorToString(char const*const pis, sal_Int32 nValue)
594 OUString const is(OUString::createFromAscii(pis));
595 OUStringBuffer buf;
596 Converter::convertColor(buf, nValue);
597 SAL_INFO("sax.cppunit","" << buf.toString());
598 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
601 void ConverterTest::testColor()
603 doTestStringToColor(11259375, "#abcdef");
604 doTestStringToColor(160, "#0000a0");
605 doTestStringToColor(40960, "#00a000");
606 doTestStringToColor(0, "#000000");
607 doTestColorToString("#000615", 1557);
608 doTestColorToString("#5bcd15", 123456789);
609 doTestColorToString("#fffac7", -1337);
610 doTestColorToString("#000000", 0);
613 void doTestStringToNumber(sal_Int32 nValue, char const*const pis, sal_Int32 nMin, sal_Int32 nMax)
615 OUString const is(OUString::createFromAscii(pis));
616 sal_Int32 nTemp;
617 bool bSuccess(Converter::convertNumber(nTemp, is, nMin, nMax));
618 SAL_INFO("sax.cppunit","" << nTemp);
619 CPPUNIT_ASSERT(bSuccess);
620 CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
623 void doTestNumberToString(char const*const pis, sal_Int32 nValue)
625 OUString const is(OUString::createFromAscii(pis));
626 OUStringBuffer buf;
627 Converter::convertNumber(buf, nValue);
628 SAL_INFO("sax.cppunit","" << buf.toString());
629 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
632 void ConverterTest::testNumber()
634 doTestStringToNumber(30, "30", 1, 40);
635 doTestStringToNumber(1, "-5", 1, 300);
636 doTestStringToNumber(-30, "7", -100, -30);
637 doTestStringToNumber(0, "-0", 0, 1);
638 doTestStringToNumber(0, "666", -0, 0);
639 doTestNumberToString("333", 333);
640 doTestNumberToString("-1", -1);
641 doTestNumberToString("0", 0000);
642 doTestNumberToString("-1", -0001);
643 doTestNumberToString("0", -0);
646 void doTestEncodeBase64(char const*const pis, const uno::Sequence<sal_Int8>& aPass)
648 OUString const is(OUString::createFromAscii(pis));
649 OUStringBuffer buf;
650 Converter::encodeBase64(buf, aPass);
651 SAL_INFO("sax.cppunit","" << buf.toString());
652 CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
655 void doTestDecodeBase64(const uno::Sequence<sal_Int8>& aPass, char const*const pis)
657 OUString const is(OUString::createFromAscii(pis));
658 uno::Sequence< sal_Int8 > tempSequence;
659 Converter::decodeBase64(tempSequence, is);
660 SAL_INFO("sax.cppunit","" << is);
661 bool b = (tempSequence==aPass);
662 CPPUNIT_ASSERT(b);
665 void ConverterTest::testBase64()
667 std::vector< sal_Int8 > tempSeq(4);
668 for(sal_Int8 i = 0; i<4; ++i)
669 tempSeq.push_back(i);
670 uno::Sequence< sal_Int8 > tempSequence = comphelper::containerToSequence(tempSeq);
671 doTestEncodeBase64("AAAAAAABAgM=", tempSequence);
672 doTestDecodeBase64(tempSequence, "AAAAAAABAgM=");
673 tempSeq[0] = sal_Int8(5);
674 tempSeq[1] = sal_Int8(2);
675 tempSeq[2] = sal_Int8(3);
676 tempSequence = comphelper::containerToSequence(tempSeq);
677 doTestEncodeBase64("BQIDAAABAgM=", tempSequence);
678 doTestDecodeBase64(tempSequence, "BQIDAAABAgM=");
679 tempSeq[0] = sal_Int8(sal_uInt8(200));
680 tempSeq[1] = sal_Int8(31);
681 tempSeq[2] = sal_Int8(77);
682 tempSeq[3] = sal_Int8(111);
683 tempSequence = comphelper::containerToSequence(tempSeq);
684 doTestEncodeBase64("yB9NbwABAgM=", tempSequence);
685 doTestDecodeBase64(tempSequence, "yB9NbwABAgM=");
688 CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest);
692 CPPUNIT_PLUGIN_IMPLEMENT();
694 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */