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