Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <limits>
21 :
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>
27 :
28 : #include <rtl/ustrbuf.hxx>
29 :
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>
34 :
35 : #include "sax/tools/converter.hxx"
36 : #include <comphelper/sequence.hxx>
37 : #include "sal/log.hxx"
38 :
39 :
40 : using namespace ::com::sun::star;
41 : using namespace ::com::sun::star::util;
42 : using sax::Converter;
43 :
44 :
45 : namespace {
46 :
47 30 : class ConverterTest
48 : : public ::CppUnit::TestFixture
49 : {
50 : public:
51 : virtual void setUp() SAL_OVERRIDE;
52 : virtual void tearDown() SAL_OVERRIDE;
53 :
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();
64 :
65 2 : CPPUNIT_TEST_SUITE(ConverterTest);
66 1 : CPPUNIT_TEST(testDuration);
67 1 : CPPUNIT_TEST(testDateTime);
68 1 : CPPUNIT_TEST(testTime);
69 1 : CPPUNIT_TEST(testDouble);
70 1 : CPPUNIT_TEST(testMeasure);
71 1 : CPPUNIT_TEST(testBool);
72 1 : CPPUNIT_TEST(testPercent);
73 1 : CPPUNIT_TEST(testColor);
74 1 : CPPUNIT_TEST(testNumber);
75 1 : CPPUNIT_TEST(testBase64);
76 5 : CPPUNIT_TEST_SUITE_END();
77 :
78 : private:
79 : };
80 :
81 10 : void ConverterTest::setUp()
82 : {
83 10 : }
84 :
85 10 : void ConverterTest::tearDown()
86 : {
87 10 : }
88 :
89 15 : static void doTest(util::Duration const & rid, char const*const pis,
90 : char const*const i_pos = 0)
91 : {
92 15 : char const*const pos((i_pos) ? i_pos : pis);
93 15 : util::Duration od;
94 15 : OUString is(::rtl::OUString::createFromAscii(pis));
95 : SAL_INFO("sax.cppunit","about to convert '" << is << "'");
96 15 : 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 15 : CPPUNIT_ASSERT(bSuccess);
99 15 : CPPUNIT_ASSERT_EQUAL(rid.Years, od.Years);
100 15 : CPPUNIT_ASSERT_EQUAL(rid.Months, od.Months);
101 15 : CPPUNIT_ASSERT_EQUAL(rid.Days, od.Days);
102 15 : CPPUNIT_ASSERT_EQUAL(rid.Hours, od.Hours);
103 15 : CPPUNIT_ASSERT_EQUAL(rid.Minutes, od.Minutes);
104 15 : CPPUNIT_ASSERT_EQUAL(rid.Seconds, od.Seconds);
105 15 : CPPUNIT_ASSERT_EQUAL(rid.NanoSeconds, od.NanoSeconds);
106 15 : CPPUNIT_ASSERT_EQUAL(rid.Negative, od.Negative);
107 30 : OUStringBuffer buf;
108 15 : Converter::convertDuration(buf, od);
109 : SAL_INFO("sax.cppunit","" << buf.toString());
110 30 : CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
111 15 : }
112 :
113 11 : static void doTestDurationF(char const*const pis)
114 : {
115 11 : util::Duration od;
116 : bool bSuccess = Converter::convertDuration(od,
117 11 : 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 11 : CPPUNIT_ASSERT_MESSAGE(pis, !bSuccess);
120 11 : }
121 :
122 1 : void ConverterTest::testDuration()
123 : {
124 : SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
125 1 : doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
126 1 : doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
127 1 : doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
128 1 : doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
129 1 : doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
130 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
131 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190000000), "PT0.19S", "PT0.190000000S");
132 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90000000), "PT0.09S", "PT0.090000000S" );
133 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9000000), "PT0.009S", "PT0.009000000S" );
134 1 : 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 1 : "PT9.999999999999999999999999999999S", "PT9.999999999S" );
137 1 : 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 1 : "-P7Y6M5DT4H3M2.01000S", "-P7Y6M5DT4H3M2.010000000S" );
140 1 : doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
141 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
142 1 : doTestDurationF("1Y1M"); // invalid: no ^P
143 1 : doTestDurationF("P-1Y1M"); // invalid: - after P
144 1 : doTestDurationF("P1M1Y"); // invalid: Y after M
145 1 : doTestDurationF("PT1Y"); // invalid: Y after T
146 1 : doTestDurationF("P1Y1M1M"); // invalid: M twice, no T
147 1 : doTestDurationF("P1YT1MT1M"); // invalid: T twice
148 1 : doTestDurationF("P1YT"); // invalid: T but no H,M,S
149 1 : doTestDurationF("P99999999999Y"); // cannot parse so many Ys
150 1 : doTestDurationF("PT.1S"); // invalid: no 0 preceding .
151 1 : doTestDurationF("PT5M.134S"); // invalid: no 0 preceding .
152 1 : doTestDurationF("PT1.S"); // invalid: no digit following .
153 : SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
154 1 : }
155 :
156 :
157 45 : static bool eqDateTime(const util::DateTime& a, const util::DateTime& b) {
158 90 : return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
159 45 : && a.Hours == b.Hours && a.Minutes == b.Minutes
160 45 : && a.Seconds == b.Seconds
161 45 : && a.NanoSeconds == b.NanoSeconds
162 90 : && a.IsUTC == b.IsUTC;
163 : }
164 :
165 17 : static void doTest(util::DateTime const & rdt, char const*const pis,
166 : char const*const i_pos = 0)
167 : {
168 17 : char const*const pos((i_pos) ? i_pos : pis);
169 17 : OUString is(OUString::createFromAscii(pis));
170 17 : util::DateTime odt;
171 : SAL_INFO("sax.cppunit","about to convert '" << is << "'");
172 17 : 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 17 : CPPUNIT_ASSERT(bSuccess);
175 17 : CPPUNIT_ASSERT(eqDateTime(rdt, odt));
176 34 : OUStringBuffer buf;
177 17 : Converter::convertDateTime(buf, odt, 0, true);
178 : SAL_INFO("sax.cppunit","" << buf.toString());
179 34 : CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos),
180 34 : buf.makeStringAndClear());
181 17 : }
182 :
183 25 : static void doTestDateTimeF(char const*const pis)
184 : {
185 25 : util::DateTime odt;
186 : bool bSuccess = Converter::parseDateTime(odt, 0,
187 25 : 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 25 : CPPUNIT_ASSERT(!bSuccess);
190 25 : }
191 :
192 1 : void ConverterTest::testDateTime()
193 : {
194 : SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST BEGIN");
195 1 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, false), "0001-01-01T00:00:00" );
196 1 : 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 1 : "-0001-01-01T00:00:00");
199 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
200 1 : "-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 1 : "-0324-01-01T00:00:00" );
203 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
204 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "9999-12-31T23:59:59.999999999Z" );
221 : doTest( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
222 1 : "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 1 : "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 1 : "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 : "1-01-01T00:00:00", "0001-01-01T00:00:00" );
232 1 : doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
233 1 : doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
234 1 : doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
235 1 : doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
236 1 : doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
237 1 : doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
238 1 : doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
239 1 : doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
240 1 : doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
241 1 : doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
242 1 : doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
243 1 : doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
244 1 : doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
245 1 : doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
246 1 : doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
247 1 : doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
248 1 : doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
249 1 : doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
250 1 : doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
251 1 : doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
252 1 : doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
253 1 : doTestDateTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
254 1 : doTestDateTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
255 1 : doTestDateTimeF( "00:00:00" ); // invalid: no date
256 1 : doTestDateTimeF( "T00:00:00" ); // invalid: no date
257 : SAL_INFO("sax.cppunit","\nSAX CONVERTER TEST END");
258 1 : }
259 :
260 28 : static void doTestTime(util::DateTime const & rdt, char const*const pis,
261 : char const*const i_pos = 0)
262 : {
263 28 : char const*const pos((i_pos) ? i_pos : pis);
264 28 : OUString is(OUString::createFromAscii(pis));
265 28 : util::DateTime odt;
266 : SAL_INFO("sax.cppunit","about to convert '" << is << "'");
267 28 : 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 28 : CPPUNIT_ASSERT(bSuccess);
270 28 : CPPUNIT_ASSERT(eqDateTime(rdt, odt));
271 56 : OUStringBuffer buf;
272 28 : Converter::convertTimeOrDateTime(buf, odt, 0);
273 : SAL_INFO("sax.cppunit","" << buf.toString());
274 56 : CPPUNIT_ASSERT_EQUAL(OUString::createFromAscii(pos),
275 56 : buf.makeStringAndClear());
276 28 : }
277 :
278 40 : static void doTestTimeF(char const*const pis)
279 : {
280 40 : util::DateTime odt;
281 : bool bSuccess = Converter::parseTimeOrDateTime(odt, 0,
282 40 : 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 40 : CPPUNIT_ASSERT_MESSAGE(pis, !bSuccess);
285 40 : }
286 :
287 1 : void ConverterTest::testTime() // time or dateTime + horrible backcompat mess
288 : {
289 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
290 1 : "0001-01-01T00:00:00" );
291 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, false),
292 1 : "0001-01-01T00:00:00" );
293 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
294 1 : "0001-01-01T00:00:00Z" );
295 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, false),
296 1 : "-0001-01-01T00:00:00");
297 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, -1, true),
298 1 : "-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 1 : "-0324-01-01T00:00:00" );
301 : doTestTime( util::DateTime(0, 0, 0, 0, 1, 1, 1, true),
302 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "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 1 : "9999-12-31T23:59:59.999999999Z" );
319 : doTestTime( util::DateTime(0, 0, 0, 0, 29, 2, 2000, true), // leap year
320 1 : "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 1 : "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 1 : "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 : "1-01-01T00:00:00", "0001-01-01T00:00:00" );
330 :
331 1 : doTestTime( util::DateTime(0, 0, 0, 0, 0, 0, 0, false), "00:00:00" );
332 1 : doTestTime( util::DateTime(0, 0, 0, 24, 0, 0, 0, false), "24:00:00" );
333 1 : doTestTime( util::DateTime(0, 0, 59, 0, 0, 0, 0, false), "00:59:00" );
334 1 : 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 1 : "05:02:01+01:00", "04:02:01Z" );
337 : doTestTime( util::DateTime(0, 11, 12, 9, 0, 0, 0, true),
338 1 : "05:12:11-04:00", "09:12:11Z" );
339 : doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, false),
340 1 : "23:59:59.99", "23:59:59.990000000" );
341 : doTestTime( util::DateTime(990000000, 59, 59, 23, 0, 0, 0, true),
342 1 : "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 1 : "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 1 : "0-00-00T00:01:00", "00:01:00" );
349 :
350 1 : doTestTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
351 1 : doTestTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
352 1 : doTestTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
353 1 : doTestTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
354 1 : doTestTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
355 1 : doTestTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
356 1 : doTestTimeF( "0001-01-01T00:00:00." ); // invalid: .$
357 1 : doTestTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
358 1 : doTestTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
359 1 : doTestTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
360 1 : doTestTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
361 1 : doTestTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
362 1 : doTestTimeF( "0001-01-01T00:60:00" ); // invalid: M > 59
363 1 : doTestTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
364 1 : doTestTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
365 1 : doTestTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
366 1 : doTestTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
367 1 : doTestTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
368 1 : doTestTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
369 1 : doTestTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
370 1 : doTestTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
371 1 : doTestTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
372 1 : doTestTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
373 1 : doTestTimeF( "T00:00:00" ); // invalid: T
374 1 : doTestTimeF( "0:00:00" ); // invalid: < 2 H
375 1 : doTestTimeF( "00:0:00" ); // invalid: < 2 M
376 1 : doTestTimeF( "00:00:0" ); // invalid: < 2 S
377 1 : doTestTimeF( "00:00:00." ); // invalid: .$
378 1 : doTestTimeF( "00:00:00+1:00" ); // invalid: < 2 TZ H
379 1 : doTestTimeF( "00:00:00+00:1" ); // invalid: < 2 TZ M
380 1 : doTestTimeF( "25:00:00" ); // invalid: H > 24
381 1 : doTestTimeF( "00:60:00" ); // invalid: M > 59
382 1 : doTestTimeF( "00:00:60" ); // invalid: S > 59
383 1 : doTestTimeF( "24:01:00" ); // invalid: H=24, but M != 0
384 1 : doTestTimeF( "24:00:01" ); // invalid: H=24, but S != 0
385 1 : doTestTimeF( "24:00:00.1" ); // invalid: H=24, but H != 0
386 1 : doTestTimeF( "00:00:00+15:00" ); // invalid: TZ > +14:00
387 1 : doTestTimeF( "00:00:00+14:01" ); // invalid: TZ > +14:00
388 1 : doTestTimeF( "00:00:00-15:00" ); // invalid: TZ < -14:00
389 1 : doTestTimeF( "00:00:00-14:01" ); // invalid: TZ < -14:00
390 1 : }
391 :
392 48 : void doTestDouble(char const*const pis, double const rd,
393 : sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
394 : {
395 48 : OUString const is(OUString::createFromAscii(pis));
396 : double od;
397 48 : bool bSuccess(Converter::convertDouble(od, is, nSourceUnit, nTargetUnit));
398 : SAL_INFO("sax.cppunit","" << od);
399 48 : CPPUNIT_ASSERT(bSuccess);
400 48 : CPPUNIT_ASSERT_DOUBLES_EQUAL(rd, od, 0.00000001);
401 96 : OUStringBuffer buf;
402 48 : Converter::convertDouble(buf, od, true, nTargetUnit, nSourceUnit);
403 : SAL_INFO("sax.cppunit","" << buf.toString());
404 96 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
405 48 : }
406 :
407 1 : void ConverterTest::testDouble()
408 : {
409 1 : doTestDouble("42", 42.0, MeasureUnit::TWIP, MeasureUnit::TWIP);
410 1 : doTestDouble("42", 42.0, MeasureUnit::POINT, MeasureUnit::POINT);
411 1 : doTestDouble("42", 42.0, MeasureUnit::MM_100TH, MeasureUnit::MM_100TH);
412 1 : doTestDouble("42", 42.0, MeasureUnit::MM_10TH, MeasureUnit::MM_10TH);
413 1 : doTestDouble("42", 42.0, MeasureUnit::MM, MeasureUnit::MM); // identity don't seem to add unit?
414 1 : doTestDouble("42", 42.0, MeasureUnit::CM, MeasureUnit::CM);
415 1 : doTestDouble("42", 42.0, MeasureUnit::INCH, MeasureUnit::INCH);
416 1 : doTestDouble("2pt", 40.0, MeasureUnit::POINT, MeasureUnit::TWIP);
417 1 : doTestDouble("20pc", 1, MeasureUnit::TWIP, MeasureUnit::POINT);
418 1 : doTestDouble("4", 2.26771653543307, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
419 1 : doTestDouble("4", 22.6771653543307, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
420 1 : doTestDouble("4mm", 226.771653543307, MeasureUnit::MM, MeasureUnit::TWIP);
421 1 : doTestDouble("4cm", 2267.71653543307, MeasureUnit::CM, MeasureUnit::TWIP);
422 1 : doTestDouble("4in", 5760.0, MeasureUnit::INCH, MeasureUnit::TWIP);
423 1 : doTestDouble("1440pc", 1.0, MeasureUnit::TWIP, MeasureUnit::INCH);
424 1 : doTestDouble("567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::CM);
425 1 : doTestDouble("56.7pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM);
426 1 : doTestDouble("5.67pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_10TH);
427 1 : doTestDouble("0.567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_100TH);
428 1 : doTestDouble("42pt", 1.4816666666666, MeasureUnit::POINT, MeasureUnit::CM);
429 1 : doTestDouble("42pt", 14.816666666666, MeasureUnit::POINT, MeasureUnit::MM);
430 1 : doTestDouble("42pt", 148.16666666666, MeasureUnit::POINT, MeasureUnit::MM_10TH);
431 1 : doTestDouble("42pt", 1481.6666666666, MeasureUnit::POINT, MeasureUnit::MM_100TH);
432 1 : doTestDouble("72pt", 1.0, MeasureUnit::POINT, MeasureUnit::INCH);
433 1 : doTestDouble("3.5in", 8.89, MeasureUnit::INCH, MeasureUnit::CM);
434 1 : doTestDouble("3.5in", 88.9, MeasureUnit::INCH, MeasureUnit::MM);
435 1 : doTestDouble("3.5in", 889.0, MeasureUnit::INCH, MeasureUnit::MM_10TH);
436 1 : doTestDouble("3.5in", 8890.0, MeasureUnit::INCH, MeasureUnit::MM_100TH);
437 1 : doTestDouble("2in", 144, MeasureUnit::INCH, MeasureUnit::POINT);
438 1 : doTestDouble("5.08cm", 2.0, MeasureUnit::CM, MeasureUnit::INCH);
439 1 : doTestDouble("3.5cm", 3500.0, MeasureUnit::CM, MeasureUnit::MM_100TH);
440 1 : doTestDouble("3.5cm", 350.0, MeasureUnit::CM, MeasureUnit::MM_10TH);
441 1 : doTestDouble("3.5cm", 35.0, MeasureUnit::CM, MeasureUnit::MM);
442 1 : doTestDouble("10cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::POINT);
443 1 : doTestDouble("0.5cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::TWIP);
444 1 : doTestDouble("10mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::POINT);
445 1 : doTestDouble("0.5mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::TWIP);
446 1 : doTestDouble("10", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::POINT);
447 1 : doTestDouble("0.5", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
448 1 : doTestDouble("10", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::POINT);
449 1 : doTestDouble("0.5", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
450 1 : doTestDouble("10mm", 1.0, MeasureUnit::MM, MeasureUnit::CM);
451 1 : doTestDouble("10mm", 100.0, MeasureUnit::MM, MeasureUnit::MM_10TH);
452 1 : doTestDouble("20mm", 2000.0, MeasureUnit::MM, MeasureUnit::MM_100TH);
453 1 : doTestDouble("300", 30.0, MeasureUnit::MM_10TH, MeasureUnit::MM);
454 1 : doTestDouble("400", 4.0, MeasureUnit::MM_100TH, MeasureUnit::MM);
455 1 : doTestDouble("600", 6000.0, MeasureUnit::MM_10TH, MeasureUnit::MM_100TH);
456 1 : doTestDouble("700", 70.0, MeasureUnit::MM_100TH, MeasureUnit::MM_10TH);
457 1 : }
458 :
459 26 : void doTestStringToMeasure(sal_Int32 rValue, char const*const pis, sal_Int16 nTargetUnit, sal_Int32 nMin, sal_Int32 nMax)
460 : {
461 26 : OUString const is(OUString::createFromAscii(pis));
462 : sal_Int32 nVal;
463 26 : bool bSuccess(Converter::convertMeasure(nVal, is, nTargetUnit, nMin, nMax));
464 : SAL_INFO("sax.cppunit","" << nVal);
465 26 : CPPUNIT_ASSERT(bSuccess);
466 26 : CPPUNIT_ASSERT_EQUAL(rValue, nVal);
467 26 : }
468 :
469 11 : void doTestMeasureToString(char const*const pis, sal_Int32 nMeasure, sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
470 : {
471 11 : OUString const is(OUString::createFromAscii(pis));
472 22 : OUStringBuffer buf;
473 11 : Converter::convertMeasure(buf, nMeasure, nSourceUnit, nTargetUnit);
474 : SAL_INFO("sax.cppunit","" << buf.toString());
475 22 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
476 11 : }
477 :
478 1 : void ConverterTest::testMeasure()
479 : {
480 : //check all the measure units
481 1 : doTestStringToMeasure(1000, "10mm", MeasureUnit::MM_100TH, -1, 4321);
482 1 : doTestStringToMeasure(200, "20mm", MeasureUnit::MM_10TH, 12, 4567);
483 1 : doTestStringToMeasure(300, "300", MeasureUnit::MM, 31, 555);
484 1 : doTestStringToMeasure(400, "400", MeasureUnit::CM, 10, 4321);
485 1 : doTestStringToMeasure(120, "120", MeasureUnit::INCH_1000TH, 10, 4321);
486 1 : doTestStringToMeasure(111, "111", MeasureUnit::INCH_100TH, 10, 4321);
487 1 : doTestStringToMeasure(22, "22", MeasureUnit::INCH_10TH, 10, 4321);
488 1 : doTestStringToMeasure(27, "27", MeasureUnit::INCH, 10, 4321);
489 1 : doTestStringToMeasure(52, "52", MeasureUnit::POINT, 10, 4321);
490 1 : doTestStringToMeasure(120, "120", MeasureUnit::TWIP, 10, 4321);
491 1 : doTestStringToMeasure(666, "666", MeasureUnit::M, 10, 4321);
492 1 : doTestStringToMeasure(42, "42", MeasureUnit::KM, 10, 4321);
493 1 : doTestStringToMeasure(30, "30", MeasureUnit::PICA, 10, 4321);
494 1 : doTestStringToMeasure(20, "20", MeasureUnit::FOOT, 10, 4321);
495 1 : doTestStringToMeasure(40, "40", MeasureUnit::MILE, 10, 4321);
496 1 : doTestStringToMeasure(40, "40%", MeasureUnit::PERCENT, 10, 4321);
497 1 : doTestStringToMeasure(800, "800", MeasureUnit::PIXEL, 10, 4321);
498 1 : doTestStringToMeasure(600, "600px", MeasureUnit::PIXEL, 10, 4321);
499 1 : doTestStringToMeasure(777, "777", MeasureUnit::APPFONT, 10, 4321);
500 1 : doTestStringToMeasure(80000, "80000", MeasureUnit::SYSFONT, 10, 432100);
501 : //strange values (negative, too large etc.)
502 1 : doTestStringToMeasure(555, "666", MeasureUnit::MM, -1000, 555);
503 1 : doTestStringToMeasure(-1000, "-1001", MeasureUnit::MM, -1000, 555);
504 1 : doTestStringToMeasure(0, "-0", MeasureUnit::MM, -1, 0);
505 1 : doTestStringToMeasure(::std::numeric_limits<sal_Int32>::max(), "1234567890mm", MeasureUnit::MM_10TH, 12, ::std::numeric_limits<sal_Int32>::max());
506 1 : doTestStringToMeasure(-300, "-300", MeasureUnit::MM, -1000, 555);
507 1 : doTestStringToMeasure(::std::numeric_limits<sal_Int32>::min(), "-999999999999999px", MeasureUnit::PIXEL, ::std::numeric_limits<sal_Int32>::min(), 555); //really crazy numbers...
508 :
509 1 : doTestMeasureToString("6mm", 600, MeasureUnit::MM_100TH, MeasureUnit::MM);
510 1 : doTestMeasureToString("0.005cm", 000000005, MeasureUnit::MM_100TH, MeasureUnit::CM); // zeros in the front doesn't count
511 1 : doTestMeasureToString("3mm", 30, MeasureUnit::MM_10TH, MeasureUnit::MM);
512 1 : doTestMeasureToString("6.66cm", 666, MeasureUnit::MM_10TH, MeasureUnit::CM);
513 1 : doTestMeasureToString("-157.3pt", -555, MeasureUnit::MM_10TH, MeasureUnit::POINT);
514 1 : doTestMeasureToString("174976.378in", 44444000, MeasureUnit::MM_10TH, MeasureUnit::INCH); //let's check accuracy
515 1 : doTestMeasureToString("40%", 40, MeasureUnit::PERCENT, MeasureUnit::PERCENT);
516 1 : doTestMeasureToString("70.56mm", 4000, MeasureUnit::TWIP, MeasureUnit::MM);
517 1 : doTestMeasureToString("979.928cm", 555550, MeasureUnit::TWIP, MeasureUnit::CM);
518 1 : doTestMeasureToString("111.1pt", 2222, MeasureUnit::TWIP, MeasureUnit::POINT);
519 1 : doTestMeasureToString("385.7986in", 555550, MeasureUnit::TWIP, MeasureUnit::INCH);
520 1 : }
521 :
522 2 : void doTestStringToBool(bool bBool, char const*const pis)
523 : {
524 2 : OUString const is(OUString::createFromAscii(pis));
525 : bool bTemp;
526 2 : bool bSuccess(Converter::convertBool(bTemp, is));
527 : SAL_INFO("sax.cppunit","" << bTemp);
528 2 : CPPUNIT_ASSERT(bSuccess);
529 2 : CPPUNIT_ASSERT_EQUAL(bBool, bTemp);
530 :
531 2 : }
532 :
533 2 : void doTestBoolToString(char const*const pis, bool bValue )
534 : {
535 2 : OUString const is(OUString::createFromAscii(pis));
536 4 : OUStringBuffer buf;
537 2 : Converter::convertBool(buf, bValue);
538 : SAL_INFO("sax.cppunit","" << buf.toString());
539 4 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
540 2 : }
541 :
542 1 : void ConverterTest::testBool()
543 : {
544 1 : doTestStringToBool(true, "true");
545 1 : doTestStringToBool(false, "false");
546 1 : doTestBoolToString("true", true);
547 1 : doTestBoolToString("false", false);
548 1 : }
549 :
550 5 : void doTestStringToPercent(sal_Int32 nValue, char const*const pis)
551 : {
552 5 : OUString const is(OUString::createFromAscii(pis));
553 : sal_Int32 nTemp;
554 5 : bool bSuccess(Converter::convertPercent(nTemp, is));
555 : SAL_INFO("sax.cppunit","" << nTemp);
556 5 : CPPUNIT_ASSERT(bSuccess);
557 5 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
558 5 : }
559 :
560 4 : void doTestPercentToString(char const*const pis, sal_Int32 nValue)
561 : {
562 4 : OUString const is(OUString::createFromAscii(pis));
563 8 : OUStringBuffer buf;
564 4 : Converter::convertPercent(buf, nValue);
565 : SAL_INFO("sax.cppunit","" << buf.toString());
566 8 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
567 4 : }
568 :
569 1 : void ConverterTest::testPercent()
570 : {
571 1 : doTestStringToPercent(40, "40%");
572 1 : doTestStringToPercent(30, "30");
573 1 : doTestStringToPercent(120, "120%");
574 1 : doTestStringToPercent(-40, "-40%");
575 1 : doTestStringToPercent(0, "0%");
576 1 : doTestPercentToString("12%", 12);
577 1 : doTestPercentToString("-123%", -123);
578 1 : doTestPercentToString("0%", 0);
579 1 : doTestPercentToString("1%", 00001);
580 1 : }
581 :
582 4 : void doTestStringToColor(sal_Int32 nValue, char const*const pis)
583 : {
584 4 : OUString const is(OUString::createFromAscii(pis));
585 : sal_Int32 nTemp;
586 4 : bool bSuccess(Converter::convertColor(nTemp, is));
587 : SAL_INFO("sax.cppunit","" << nTemp);
588 4 : CPPUNIT_ASSERT(bSuccess);
589 4 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
590 4 : }
591 :
592 4 : void doTestColorToString(char const*const pis, sal_Int32 nValue)
593 : {
594 4 : OUString const is(OUString::createFromAscii(pis));
595 8 : OUStringBuffer buf;
596 4 : Converter::convertColor(buf, nValue);
597 : SAL_INFO("sax.cppunit","" << buf.toString());
598 8 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
599 4 : }
600 :
601 1 : void ConverterTest::testColor()
602 : {
603 1 : doTestStringToColor(11259375, "#abcdef");
604 1 : doTestStringToColor(160, "#0000a0");
605 1 : doTestStringToColor(40960, "#00a000");
606 1 : doTestStringToColor(0, "#000000");
607 1 : doTestColorToString("#000615", 1557);
608 1 : doTestColorToString("#5bcd15", 123456789);
609 1 : doTestColorToString("#fffac7", -1337);
610 1 : doTestColorToString("#000000", 0);
611 1 : }
612 :
613 5 : void doTestStringToNumber(sal_Int32 nValue, char const*const pis, sal_Int32 nMin, sal_Int32 nMax)
614 : {
615 5 : OUString const is(OUString::createFromAscii(pis));
616 : sal_Int32 nTemp;
617 5 : bool bSuccess(Converter::convertNumber(nTemp, is, nMin, nMax));
618 : SAL_INFO("sax.cppunit","" << nTemp);
619 5 : CPPUNIT_ASSERT(bSuccess);
620 5 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
621 5 : }
622 :
623 5 : void doTestNumberToString(char const*const pis, sal_Int32 nValue)
624 : {
625 5 : OUString const is(OUString::createFromAscii(pis));
626 10 : OUStringBuffer buf;
627 5 : Converter::convertNumber(buf, nValue);
628 : SAL_INFO("sax.cppunit","" << buf.toString());
629 10 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
630 5 : }
631 :
632 1 : void ConverterTest::testNumber()
633 : {
634 1 : doTestStringToNumber(30, "30", 1, 40);
635 1 : doTestStringToNumber(1, "-5", 1, 300);
636 1 : doTestStringToNumber(-30, "7", -100, -30);
637 1 : doTestStringToNumber(0, "-0", 0, 1);
638 1 : doTestStringToNumber(0, "666", -0, 0);
639 1 : doTestNumberToString("333", 333);
640 1 : doTestNumberToString("-1", -1);
641 1 : doTestNumberToString("0", 0000);
642 1 : doTestNumberToString("-1", -0001);
643 1 : doTestNumberToString("0", -0);
644 1 : }
645 :
646 3 : void doTestEncodeBase64(char const*const pis, const uno::Sequence<sal_Int8>& aPass)
647 : {
648 3 : OUString const is(OUString::createFromAscii(pis));
649 6 : OUStringBuffer buf;
650 3 : Converter::encodeBase64(buf, aPass);
651 : SAL_INFO("sax.cppunit","" << buf.toString());
652 6 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
653 3 : }
654 :
655 3 : void doTestDecodeBase64(const uno::Sequence<sal_Int8>& aPass, char const*const pis)
656 : {
657 3 : OUString const is(OUString::createFromAscii(pis));
658 6 : uno::Sequence< sal_Int8 > tempSequence;
659 3 : Converter::decodeBase64(tempSequence, is);
660 : SAL_INFO("sax.cppunit","" << is);
661 3 : bool b = (tempSequence==aPass);
662 6 : CPPUNIT_ASSERT(b);
663 3 : }
664 :
665 1 : void ConverterTest::testBase64()
666 : {
667 1 : std::vector< sal_Int8 > tempSeq(4);
668 5 : for(sal_Int8 i = 0; i<4; ++i)
669 4 : tempSeq.push_back(i);
670 2 : uno::Sequence< sal_Int8 > tempSequence = comphelper::containerToSequence(tempSeq);
671 1 : doTestEncodeBase64("AAAAAAABAgM=", tempSequence);
672 1 : doTestDecodeBase64(tempSequence, "AAAAAAABAgM=");
673 1 : tempSeq[0] = sal_Int8(5);
674 1 : tempSeq[1] = sal_Int8(2);
675 1 : tempSeq[2] = sal_Int8(3);
676 1 : tempSequence = comphelper::containerToSequence(tempSeq);
677 1 : doTestEncodeBase64("BQIDAAABAgM=", tempSequence);
678 1 : doTestDecodeBase64(tempSequence, "BQIDAAABAgM=");
679 1 : tempSeq[0] = sal_Int8(sal_uInt8(200));
680 1 : tempSeq[1] = sal_Int8(31);
681 1 : tempSeq[2] = sal_Int8(77);
682 1 : tempSeq[3] = sal_Int8(111);
683 1 : tempSequence = comphelper::containerToSequence(tempSeq);
684 1 : doTestEncodeBase64("yB9NbwABAgM=", tempSequence);
685 2 : doTestDecodeBase64(tempSequence, "yB9NbwABAgM=");
686 1 : }
687 :
688 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest);
689 :
690 : }
691 :
692 4 : CPPUNIT_PLUGIN_IMPLEMENT();
693 :
694 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|