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