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 :
38 :
39 : using namespace ::com::sun::star;
40 : using namespace ::com::sun::star::util;
41 : using sax::Converter;
42 :
43 :
44 : namespace {
45 :
46 27 : class ConverterTest
47 : : public ::CppUnit::TestFixture
48 : {
49 : public:
50 : virtual void setUp();
51 : virtual void tearDown();
52 :
53 : void testDuration();
54 : void testDateTime();
55 : void testDouble();
56 : void testMeasure();
57 : void testBool();
58 : void testPercent();
59 : void testColor();
60 : void testNumber();
61 : void testBase64();
62 :
63 2 : CPPUNIT_TEST_SUITE(ConverterTest);
64 1 : CPPUNIT_TEST(testDuration);
65 1 : CPPUNIT_TEST(testDateTime);
66 1 : CPPUNIT_TEST(testDouble);
67 1 : CPPUNIT_TEST(testMeasure);
68 1 : CPPUNIT_TEST(testBool);
69 1 : CPPUNIT_TEST(testPercent);
70 1 : CPPUNIT_TEST(testColor);
71 1 : CPPUNIT_TEST(testNumber);
72 1 : CPPUNIT_TEST(testBase64);
73 2 : CPPUNIT_TEST_SUITE_END();
74 :
75 : private:
76 : };
77 :
78 9 : void ConverterTest::setUp()
79 : {
80 9 : }
81 :
82 9 : void ConverterTest::tearDown()
83 : {
84 9 : }
85 :
86 14 : static bool eqDuration(util::Duration a, util::Duration b) {
87 : return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days
88 : && a.Hours == b.Hours && a.Minutes == b.Minutes
89 : && a.Seconds == b.Seconds
90 : && a.MilliSeconds == b.MilliSeconds
91 14 : && a.Negative == b.Negative;
92 : }
93 :
94 14 : static void doTest(util::Duration const & rid, char const*const pis,
95 : char const*const i_pos = 0)
96 : {
97 14 : char const*const pos((i_pos) ? i_pos : pis);
98 14 : util::Duration od;
99 14 : ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
100 14 : bool bSuccess = Converter::convertDuration(od, is);
101 : OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dm",
102 : od.Negative, od.Years, od.Months, od.Days,
103 : od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
104 14 : CPPUNIT_ASSERT(bSuccess);
105 14 : CPPUNIT_ASSERT(eqDuration(rid, od));
106 14 : ::rtl::OUStringBuffer buf;
107 14 : Converter::convertDuration(buf, od);
108 : OSL_TRACE("%s",
109 : ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
110 14 : CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
111 14 : }
112 :
113 11 : static void doTestDurationF(char const*const pis)
114 : {
115 11 : util::Duration od;
116 : bool bSuccess = Converter::convertDuration(od,
117 11 : ::rtl::OUString::createFromAscii(pis));
118 : OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dH",
119 : od.Negative, od.Years, od.Months, od.Days,
120 : od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
121 11 : CPPUNIT_ASSERT(!bSuccess);
122 11 : }
123 :
124 1 : void ConverterTest::testDuration()
125 : {
126 : OSL_TRACE("\nSAX CONVERTER TEST BEGIN");
127 1 : doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
128 1 : doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
129 1 : doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
130 1 : doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
131 1 : doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
132 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
133 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190), "PT0.19S" );
134 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90), "PT0.09S" );
135 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.009S" );
136 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999),
137 1 : "PT9.999999999999999999999999999999S", "PT9.999S" );
138 1 : doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
139 : doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10),
140 1 : "-P7Y6M5DT4H3M2.01S" );
141 1 : doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
142 1 : doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
143 1 : doTestDurationF("1Y1M"); // invalid: no ^P
144 1 : doTestDurationF("P-1Y1M"); // invalid: - after P
145 1 : doTestDurationF("P1M1Y"); // invalid: Y after M
146 1 : doTestDurationF("PT1Y"); // invalid: Y after T
147 1 : doTestDurationF("P1Y1M1M"); // invalid: M twice, no T
148 1 : doTestDurationF("P1YT1MT1M"); // invalid: T twice
149 1 : doTestDurationF("P1YT"); // invalid: T but no H,M,S
150 1 : doTestDurationF("P99999999999Y"); // cannot parse so many Ys
151 1 : doTestDurationF("PT.1S"); // invalid: no 0 preceding .
152 1 : doTestDurationF("PT5M.134S"); // invalid: no 0 preceding .
153 1 : doTestDurationF("PT1.S"); // invalid: no digit following .
154 : OSL_TRACE("\nSAX CONVERTER TEST END");
155 1 : }
156 :
157 :
158 14 : static bool eqDateTime(util::DateTime a, util::DateTime b) {
159 : return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
160 : && a.Hours == b.Hours && a.Minutes == b.Minutes
161 : && a.Seconds == b.Seconds
162 14 : && a.HundredthSeconds == b.HundredthSeconds;
163 : }
164 :
165 14 : static void doTest(util::DateTime const & rdt, char const*const pis,
166 : char const*const i_pos = 0)
167 : {
168 14 : char const*const pos((i_pos) ? i_pos : pis);
169 14 : ::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
170 14 : util::DateTime odt;
171 14 : bool bSuccess( Converter::convertDateTime(odt, is) );
172 : OSL_TRACE("Y:%d M:%d D:%d H:%d M:%d S:%d H:%d",
173 : odt.Year, odt.Month, odt.Day,
174 : odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
175 14 : CPPUNIT_ASSERT(bSuccess);
176 14 : CPPUNIT_ASSERT(eqDateTime(rdt, odt));
177 14 : ::rtl::OUStringBuffer buf;
178 14 : Converter::convertDateTime(buf, odt, true);
179 : OSL_TRACE("%s",
180 : ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
181 14 : CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
182 14 : }
183 :
184 23 : static void doTestDateTimeF(char const*const pis)
185 : {
186 23 : util::DateTime odt;
187 : bool bSuccess = Converter::convertDateTime(odt,
188 23 : ::rtl::OUString::createFromAscii(pis));
189 : OSL_TRACE("Y:%d M:%d D:%d H:%dH M:%d S:%d H:%d",
190 : odt.Year, odt.Month, odt.Day,
191 : odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
192 23 : CPPUNIT_ASSERT(!bSuccess);
193 23 : }
194 :
195 1 : void ConverterTest::testDateTime()
196 : {
197 : OSL_TRACE("\nSAX CONVERTER TEST BEGIN");
198 1 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), "0001-01-01T00:00:00" );
199 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
200 1 : "0001-01-01T00:00:00Z", "0001-01-01T00:00:00" );
201 : // doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00" );
202 : // doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00Z" );
203 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
204 1 : "0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00" );
205 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
206 1 : "0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00" );
207 : doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 2, 1, 1)*/,
208 1 : "0001-01-02T00:00:00-12:00", "0001-01-02T00:00:00" );
209 : // "0001-02-01T12:00:00" );
210 : doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 1, 1, 1)*/,
211 1 : "0001-01-02T00:00:00+12:00", "0001-01-02T00:00:00" );
212 : // "0001-01-01T12:00:00" );
213 : doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
214 1 : "9999-12-31T23:59:59.99" );
215 : doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
216 1 : "9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.99" );
217 : doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
218 : "9999-12-31T23:59:59.9999999999999999999999999999999999999",
219 1 : "9999-12-31T23:59:59.99" );
220 : doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
221 : "9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
222 1 : "9999-12-31T23:59:59.99" );
223 : doTest( util::DateTime(0, 0, 0, 0, 29, 2, 2000), // leap year
224 1 : "2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00" );
225 : doTest( util::DateTime(0, 0, 0, 0, 29, 2, 1600), // leap year
226 1 : "1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00" );
227 : doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333)
228 : /*(0, 0, 0, 0, 2, 1, 333)*/,
229 1 : "0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
230 : // While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
231 : // in what we accept.
232 : doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
233 1 : "1-01-01T00:00:00", "0001-01-01T00:00:00" );
234 1 : doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
235 1 : doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
236 1 : doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
237 1 : doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
238 1 : doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
239 1 : doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
240 1 : doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
241 1 : doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
242 1 : doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
243 1 : doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
244 1 : doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
245 1 : doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
246 1 : doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: H > 59
247 1 : doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
248 1 : doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
249 1 : doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
250 1 : doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
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( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
254 1 : doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
255 1 : doTestDateTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
256 1 : doTestDateTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
257 : OSL_TRACE("\nSAX CONVERTER TEST END");
258 1 : }
259 :
260 48 : void doTestDouble(char const*const pis, double const rd,
261 : sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
262 : {
263 48 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
264 : double od;
265 48 : bool bSuccess(Converter::convertDouble(od, is, nSourceUnit, nTargetUnit));
266 : OSL_TRACE("%f", od);
267 48 : CPPUNIT_ASSERT(bSuccess);
268 48 : CPPUNIT_ASSERT_DOUBLES_EQUAL(rd, od, 0.00000001);
269 48 : ::rtl::OUStringBuffer buf;
270 48 : Converter::convertDouble(buf, od, true, nTargetUnit, nSourceUnit);
271 : OSL_TRACE("%s",
272 : ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
273 48 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
274 48 : }
275 :
276 1 : void ConverterTest::testDouble()
277 : {
278 1 : doTestDouble("42", 42.0, MeasureUnit::TWIP, MeasureUnit::TWIP);
279 1 : doTestDouble("42", 42.0, MeasureUnit::POINT, MeasureUnit::POINT);
280 1 : doTestDouble("42", 42.0, MeasureUnit::MM_100TH, MeasureUnit::MM_100TH);
281 1 : doTestDouble("42", 42.0, MeasureUnit::MM_10TH, MeasureUnit::MM_10TH);
282 1 : doTestDouble("42", 42.0, MeasureUnit::MM, MeasureUnit::MM); // identity don't seem to add unit?
283 1 : doTestDouble("42", 42.0, MeasureUnit::CM, MeasureUnit::CM);
284 1 : doTestDouble("42", 42.0, MeasureUnit::INCH, MeasureUnit::INCH);
285 1 : doTestDouble("2pt", 40.0, MeasureUnit::POINT, MeasureUnit::TWIP);
286 1 : doTestDouble("20pc", 1, MeasureUnit::TWIP, MeasureUnit::POINT);
287 1 : doTestDouble("4", 2.26771653543307, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
288 1 : doTestDouble("4", 22.6771653543307, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
289 1 : doTestDouble("4mm", 226.771653543307, MeasureUnit::MM, MeasureUnit::TWIP);
290 1 : doTestDouble("4cm", 2267.71653543307, MeasureUnit::CM, MeasureUnit::TWIP);
291 1 : doTestDouble("4in", 5760.0, MeasureUnit::INCH, MeasureUnit::TWIP);
292 1 : doTestDouble("1440pc", 1.0, MeasureUnit::TWIP, MeasureUnit::INCH);
293 1 : doTestDouble("567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::CM);
294 1 : doTestDouble("56.7pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM);
295 1 : doTestDouble("5.67pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_10TH);
296 1 : doTestDouble("0.567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_100TH);
297 1 : doTestDouble("42pt", 1.4816666666666, MeasureUnit::POINT, MeasureUnit::CM);
298 1 : doTestDouble("42pt", 14.816666666666, MeasureUnit::POINT, MeasureUnit::MM);
299 1 : doTestDouble("42pt", 148.16666666666, MeasureUnit::POINT, MeasureUnit::MM_10TH);
300 1 : doTestDouble("42pt", 1481.6666666666, MeasureUnit::POINT, MeasureUnit::MM_100TH);
301 1 : doTestDouble("72pt", 1.0, MeasureUnit::POINT, MeasureUnit::INCH);
302 1 : doTestDouble("3.5in", 8.89, MeasureUnit::INCH, MeasureUnit::CM);
303 1 : doTestDouble("3.5in", 88.9, MeasureUnit::INCH, MeasureUnit::MM);
304 1 : doTestDouble("3.5in", 889.0, MeasureUnit::INCH, MeasureUnit::MM_10TH);
305 1 : doTestDouble("3.5in", 8890.0, MeasureUnit::INCH, MeasureUnit::MM_100TH);
306 1 : doTestDouble("2in", 144, MeasureUnit::INCH, MeasureUnit::POINT);
307 1 : doTestDouble("5.08cm", 2.0, MeasureUnit::CM, MeasureUnit::INCH);
308 1 : doTestDouble("3.5cm", 3500.0, MeasureUnit::CM, MeasureUnit::MM_100TH);
309 1 : doTestDouble("3.5cm", 350.0, MeasureUnit::CM, MeasureUnit::MM_10TH);
310 1 : doTestDouble("3.5cm", 35.0, MeasureUnit::CM, MeasureUnit::MM);
311 1 : doTestDouble("10cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::POINT);
312 1 : doTestDouble("0.5cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::TWIP);
313 1 : doTestDouble("10mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::POINT);
314 1 : doTestDouble("0.5mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::TWIP);
315 1 : doTestDouble("10", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::POINT);
316 1 : doTestDouble("0.5", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
317 1 : doTestDouble("10", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::POINT);
318 1 : doTestDouble("0.5", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
319 1 : doTestDouble("10mm", 1.0, MeasureUnit::MM, MeasureUnit::CM);
320 1 : doTestDouble("10mm", 100.0, MeasureUnit::MM, MeasureUnit::MM_10TH);
321 1 : doTestDouble("20mm", 2000.0, MeasureUnit::MM, MeasureUnit::MM_100TH);
322 1 : doTestDouble("300", 30.0, MeasureUnit::MM_10TH, MeasureUnit::MM);
323 1 : doTestDouble("400", 4.0, MeasureUnit::MM_100TH, MeasureUnit::MM);
324 1 : doTestDouble("600", 6000.0, MeasureUnit::MM_10TH, MeasureUnit::MM_100TH);
325 1 : doTestDouble("700", 70.0, MeasureUnit::MM_100TH, MeasureUnit::MM_10TH);
326 1 : }
327 :
328 26 : void doTestStringToMeasure(sal_Int32 rValue, char const*const pis, sal_Int16 nTargetUnit, sal_Int32 nMin, sal_Int32 nMax)
329 : {
330 26 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
331 : sal_Int32 nVal;
332 26 : bool bSuccess(Converter::convertMeasure(nVal, is, nTargetUnit, nMin, nMax));
333 : OSL_TRACE("%i", nVal);
334 26 : CPPUNIT_ASSERT(bSuccess);
335 26 : CPPUNIT_ASSERT_EQUAL(rValue, nVal);
336 26 : }
337 :
338 11 : void doTestMeasureToString(char const*const pis, sal_Int32 nMeasure, sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
339 : {
340 11 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
341 11 : ::rtl::OUStringBuffer buf;
342 11 : Converter::convertMeasure(buf, nMeasure, nSourceUnit, nTargetUnit);
343 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
344 11 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
345 11 : }
346 :
347 1 : void ConverterTest::testMeasure()
348 : {
349 : //check all the measure units
350 1 : doTestStringToMeasure(1000, "10mm", MeasureUnit::MM_100TH, -1, 4321);
351 1 : doTestStringToMeasure(200, "20mm", MeasureUnit::MM_10TH, 12, 4567);
352 1 : doTestStringToMeasure(300, "300", MeasureUnit::MM, 31, 555);
353 1 : doTestStringToMeasure(400, "400", MeasureUnit::CM, 10, 4321);
354 1 : doTestStringToMeasure(120, "120", MeasureUnit::INCH_1000TH, 10, 4321);
355 1 : doTestStringToMeasure(111, "111", MeasureUnit::INCH_100TH, 10, 4321);
356 1 : doTestStringToMeasure(22, "22", MeasureUnit::INCH_10TH, 10, 4321);
357 1 : doTestStringToMeasure(27, "27", MeasureUnit::INCH, 10, 4321);
358 1 : doTestStringToMeasure(52, "52", MeasureUnit::POINT, 10, 4321);
359 1 : doTestStringToMeasure(120, "120", MeasureUnit::TWIP, 10, 4321);
360 1 : doTestStringToMeasure(666, "666", MeasureUnit::M, 10, 4321);
361 1 : doTestStringToMeasure(42, "42", MeasureUnit::KM, 10, 4321);
362 1 : doTestStringToMeasure(30, "30", MeasureUnit::PICA, 10, 4321);
363 1 : doTestStringToMeasure(20, "20", MeasureUnit::FOOT, 10, 4321);
364 1 : doTestStringToMeasure(40, "40", MeasureUnit::MILE, 10, 4321);
365 1 : doTestStringToMeasure(40, "40%", MeasureUnit::PERCENT, 10, 4321);
366 1 : doTestStringToMeasure(800, "800", MeasureUnit::PIXEL, 10, 4321);
367 1 : doTestStringToMeasure(600, "600px", MeasureUnit::PIXEL, 10, 4321);
368 1 : doTestStringToMeasure(777, "777", MeasureUnit::APPFONT, 10, 4321);
369 1 : doTestStringToMeasure(80000, "80000", MeasureUnit::SYSFONT, 10, 432100);
370 : //strange values (negative, too large etc.)
371 1 : doTestStringToMeasure(555, "666", MeasureUnit::MM, -1000, 555);
372 1 : doTestStringToMeasure(-1000, "-1001", MeasureUnit::MM, -1000, 555);
373 1 : doTestStringToMeasure(0, "-0", MeasureUnit::MM, -1, 0);
374 1 : doTestStringToMeasure(::std::numeric_limits<sal_Int32>::max(), "1234567890mm", MeasureUnit::MM_10TH, 12, ::std::numeric_limits<sal_Int32>::max());
375 1 : doTestStringToMeasure(-300, "-300", MeasureUnit::MM, -1000, 555);
376 1 : doTestStringToMeasure(::std::numeric_limits<sal_Int32>::min(), "-999999999999999px", MeasureUnit::PIXEL, ::std::numeric_limits<sal_Int32>::min(), 555); //really crazy numbers...
377 :
378 1 : doTestMeasureToString("6mm", 600, MeasureUnit::MM_100TH, MeasureUnit::MM);
379 1 : doTestMeasureToString("0.005cm", 000000005, MeasureUnit::MM_100TH, MeasureUnit::CM); // zeros in the front doesn't count
380 1 : doTestMeasureToString("3mm", 30, MeasureUnit::MM_10TH, MeasureUnit::MM);
381 1 : doTestMeasureToString("6.66cm", 666, MeasureUnit::MM_10TH, MeasureUnit::CM);
382 1 : doTestMeasureToString("-157.3pt", -555, MeasureUnit::MM_10TH, MeasureUnit::POINT);
383 1 : doTestMeasureToString("174976.378in", 44444000, MeasureUnit::MM_10TH, MeasureUnit::INCH); //let's check accuracy
384 1 : doTestMeasureToString("40%", 40, MeasureUnit::PERCENT, MeasureUnit::PERCENT);
385 1 : doTestMeasureToString("70.56mm", 4000, MeasureUnit::TWIP, MeasureUnit::MM);
386 1 : doTestMeasureToString("979.928cm", 555550, MeasureUnit::TWIP, MeasureUnit::CM);
387 1 : doTestMeasureToString("111.1pt", 2222, MeasureUnit::TWIP, MeasureUnit::POINT);
388 1 : doTestMeasureToString("385.7986in", 555550, MeasureUnit::TWIP, MeasureUnit::INCH);
389 1 : }
390 :
391 2 : void doTestStringToBool(bool bBool, char const*const pis)
392 : {
393 2 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
394 : bool bTemp;
395 2 : bool bSuccess(Converter::convertBool(bTemp, is));
396 : OSL_TRACE("%s", bTemp);
397 2 : CPPUNIT_ASSERT(bSuccess);
398 2 : CPPUNIT_ASSERT_EQUAL(bBool, bTemp);
399 :
400 2 : }
401 :
402 2 : void doTestBoolToString(char const*const pis, bool bValue )
403 : {
404 2 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
405 2 : ::rtl::OUStringBuffer buf;
406 2 : Converter::convertBool(buf, bValue);
407 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
408 2 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
409 2 : }
410 :
411 1 : void ConverterTest::testBool()
412 : {
413 1 : doTestStringToBool(true, "true");
414 1 : doTestStringToBool(false, "false");
415 1 : doTestBoolToString("true", true);
416 1 : doTestBoolToString("false", false);
417 1 : }
418 :
419 5 : void doTestStringToPercent(sal_Int32 nValue, char const*const pis)
420 : {
421 5 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
422 : sal_Int32 nTemp;
423 5 : bool bSuccess(Converter::convertPercent(nTemp, is));
424 : OSL_TRACE("%i", nTemp);
425 5 : CPPUNIT_ASSERT(bSuccess);
426 5 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
427 5 : }
428 :
429 4 : void doTestPercentToString(char const*const pis, sal_Int32 nValue)
430 : {
431 4 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
432 4 : ::rtl::OUStringBuffer buf;
433 4 : Converter::convertPercent(buf, nValue);
434 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
435 4 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
436 4 : }
437 :
438 1 : void ConverterTest::testPercent()
439 : {
440 1 : doTestStringToPercent(40, "40%");
441 1 : doTestStringToPercent(30, "30");
442 1 : doTestStringToPercent(120, "120%");
443 1 : doTestStringToPercent(-40, "-40%");
444 1 : doTestStringToPercent(0, "0%");
445 1 : doTestPercentToString("12%", 12);
446 1 : doTestPercentToString("-123%", -123);
447 1 : doTestPercentToString("0%", 0);
448 1 : doTestPercentToString("1%", 00001);
449 1 : }
450 :
451 4 : void doTestStringToColor(sal_Int32 nValue, char const*const pis)
452 : {
453 4 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
454 : sal_Int32 nTemp;
455 4 : bool bSuccess(Converter::convertColor(nTemp, is));
456 : OSL_TRACE("%i", nTemp);
457 4 : CPPUNIT_ASSERT(bSuccess);
458 4 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
459 4 : }
460 :
461 4 : void doTestColorToString(char const*const pis, sal_Int32 nValue)
462 : {
463 4 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
464 4 : ::rtl::OUStringBuffer buf;
465 4 : Converter::convertColor(buf, nValue);
466 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
467 4 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
468 4 : }
469 :
470 1 : void ConverterTest::testColor()
471 : {
472 1 : doTestStringToColor(11259375, "#abcdef");
473 1 : doTestStringToColor(160, "#0000a0");
474 1 : doTestStringToColor(40960, "#00a000");
475 1 : doTestStringToColor(0, "#000000");
476 1 : doTestColorToString("#000615", 1557);
477 1 : doTestColorToString("#5bcd15", 123456789);
478 1 : doTestColorToString("#fffac7", -1337);
479 1 : doTestColorToString("#000000", 0);
480 1 : }
481 :
482 5 : void doTestStringToNumber(sal_Int32 nValue, char const*const pis, sal_Int32 nMin, sal_Int32 nMax)
483 : {
484 5 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
485 : sal_Int32 nTemp;
486 5 : bool bSuccess(Converter::convertNumber(nTemp, is, nMin, nMax));
487 : OSL_TRACE("%i", nTemp);
488 5 : CPPUNIT_ASSERT(bSuccess);
489 5 : CPPUNIT_ASSERT_EQUAL(nValue, nTemp);
490 5 : }
491 :
492 5 : void doTestNumberToString(char const*const pis, sal_Int32 nValue)
493 : {
494 5 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
495 5 : ::rtl::OUStringBuffer buf;
496 5 : Converter::convertNumber(buf, nValue);
497 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
498 5 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
499 5 : }
500 :
501 1 : void ConverterTest::testNumber()
502 : {
503 1 : doTestStringToNumber(30, "30", 1, 40);
504 1 : doTestStringToNumber(1, "-5", 1, 300);
505 1 : doTestStringToNumber(-30, "7", -100, -30);
506 1 : doTestStringToNumber(0, "-0", 0, 1);
507 1 : doTestStringToNumber(0, "666", -0, 0);
508 1 : doTestNumberToString("333", 333);
509 1 : doTestNumberToString("-1", -1);
510 1 : doTestNumberToString("0", 0000);
511 1 : doTestNumberToString("-1", -0001);
512 1 : doTestNumberToString("0", -0);
513 1 : }
514 :
515 3 : void doTestEncodeBase64(char const*const pis, const uno::Sequence<sal_Int8> aPass)
516 : {
517 3 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
518 3 : ::rtl::OUStringBuffer buf;
519 3 : Converter::encodeBase64(buf, aPass);
520 : OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
521 3 : CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
522 3 : }
523 :
524 3 : void doTestDecodeBase64(const uno::Sequence<sal_Int8> aPass, char const*const pis)
525 : {
526 3 : ::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
527 3 : uno::Sequence< sal_Int8 > tempSequence;
528 3 : Converter::decodeBase64(tempSequence, is);
529 : OSL_TRACE("%s", ::rtl::OUStringToOString(is.getStr(), RTL_TEXTENCODING_UTF8).getStr());
530 3 : bool b = (tempSequence==aPass);
531 3 : CPPUNIT_ASSERT(b);
532 3 : }
533 :
534 1 : void ConverterTest::testBase64()
535 : {
536 1 : comphelper::SequenceAsVector< sal_Int8 > tempSeq(4);
537 5 : for(sal_Int8 i = 0; i<4; ++i)
538 4 : tempSeq.push_back(i);
539 1 : uno::Sequence< sal_Int8 > tempSequence = tempSeq.getAsConstList();
540 1 : doTestEncodeBase64("AAAAAAABAgM=", tempSequence);
541 1 : doTestDecodeBase64(tempSequence, "AAAAAAABAgM=");
542 1 : tempSeq[0] = sal_Int8(5);
543 1 : tempSeq[1] = sal_Int8(2);
544 1 : tempSeq[2] = sal_Int8(3);
545 1 : tempSequence = tempSeq.getAsConstList();
546 1 : doTestEncodeBase64("BQIDAAABAgM=", tempSequence);
547 1 : doTestDecodeBase64(tempSequence, "BQIDAAABAgM=");
548 1 : tempSeq[0] = sal_Int8(sal_uInt8(200));
549 1 : tempSeq[1] = sal_Int8(31);
550 1 : tempSeq[2] = sal_Int8(77);
551 1 : tempSeq[3] = sal_Int8(111);
552 1 : tempSequence = tempSeq.getAsConstList();
553 1 : doTestEncodeBase64("yB9NbwABAgM=", tempSequence);
554 1 : doTestDecodeBase64(tempSequence, "yB9NbwABAgM=");
555 1 : }
556 :
557 1 : CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest);
558 :
559 : }
560 :
561 4 : CPPUNIT_PLUGIN_IMPLEMENT();
562 :
563 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|