2010-10-14 08:27:31 +02:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-06-27 17:37:30 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the LibreOffice project.
|
|
|
|
*
|
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
|
|
|
*
|
|
|
|
* This file incorporates work covered by the following license notice:
|
|
|
|
*
|
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed
|
|
|
|
* with this work for additional information regarding copyright
|
|
|
|
* ownership. The ASF licenses this file to you under the Apache
|
|
|
|
* License, Version 2.0 (the "License"); you may not use this file
|
|
|
|
* except in compliance with the License. You may obtain a copy of
|
|
|
|
* the License at http://www.apache.org/licenses/LICENSE-2.0 .
|
|
|
|
*/
|
2010-05-19 18:35:11 +02:00
|
|
|
|
2012-06-27 16:05:04 +01:00
|
|
|
#include <sal/types.h>
|
2010-05-19 18:35:11 +02:00
|
|
|
#include <cppunit/TestAssert.h>
|
|
|
|
#include <cppunit/TestFixture.h>
|
|
|
|
#include <cppunit/extensions/HelperMacros.h>
|
|
|
|
#include <cppunit/plugin/TestPlugIn.h>
|
|
|
|
|
|
|
|
#include <rtl/ustrbuf.hxx>
|
|
|
|
|
|
|
|
#include <com/sun/star/util/DateTime.hpp>
|
|
|
|
#include <com/sun/star/util/Date.hpp>
|
|
|
|
#include <com/sun/star/util/Duration.hpp>
|
2012-04-20 18:39:36 +02:00
|
|
|
#include <com/sun/star/util/MeasureUnit.hpp>
|
2010-05-19 18:35:11 +02:00
|
|
|
|
|
|
|
#include "sax/tools/converter.hxx"
|
|
|
|
|
|
|
|
|
|
|
|
using namespace ::com::sun::star;
|
2012-06-29 12:59:50 +01:00
|
|
|
using namespace ::com::sun::star::util;
|
2010-05-19 18:35:11 +02:00
|
|
|
using sax::Converter;
|
|
|
|
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ConverterTest
|
|
|
|
: public ::CppUnit::TestFixture
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
virtual void setUp();
|
|
|
|
virtual void tearDown();
|
|
|
|
|
|
|
|
void testDuration();
|
|
|
|
void testDateTime();
|
2012-04-20 18:39:36 +02:00
|
|
|
void testDouble();
|
2012-08-09 01:09:25 +02:00
|
|
|
void testMeasure();
|
2012-08-10 14:30:07 +02:00
|
|
|
void testBool();
|
2010-05-19 18:35:11 +02:00
|
|
|
|
|
|
|
CPPUNIT_TEST_SUITE(ConverterTest);
|
|
|
|
CPPUNIT_TEST(testDuration);
|
|
|
|
CPPUNIT_TEST(testDateTime);
|
2012-04-20 18:39:36 +02:00
|
|
|
CPPUNIT_TEST(testDouble);
|
2012-08-09 01:09:25 +02:00
|
|
|
CPPUNIT_TEST(testMeasure);
|
2012-08-10 14:30:07 +02:00
|
|
|
CPPUNIT_TEST(testBool);
|
2010-05-19 18:35:11 +02:00
|
|
|
CPPUNIT_TEST_SUITE_END();
|
|
|
|
|
|
|
|
private:
|
|
|
|
};
|
|
|
|
|
|
|
|
void ConverterTest::setUp()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConverterTest::tearDown()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool eqDuration(util::Duration a, util::Duration b) {
|
|
|
|
return a.Years == b.Years && a.Months == b.Months && a.Days == b.Days
|
|
|
|
&& a.Hours == b.Hours && a.Minutes == b.Minutes
|
|
|
|
&& a.Seconds == b.Seconds
|
|
|
|
&& a.MilliSeconds == b.MilliSeconds
|
|
|
|
&& a.Negative == b.Negative;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doTest(util::Duration const & rid, char const*const pis,
|
|
|
|
char const*const i_pos = 0)
|
|
|
|
{
|
|
|
|
char const*const pos((i_pos) ? i_pos : pis);
|
|
|
|
util::Duration od;
|
|
|
|
::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
bool bSuccess = Converter::convertDuration(od, is);
|
|
|
|
OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dm",
|
|
|
|
od.Negative, od.Years, od.Months, od.Days,
|
|
|
|
od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
|
|
|
|
CPPUNIT_ASSERT(bSuccess);
|
|
|
|
CPPUNIT_ASSERT(eqDuration(rid, od));
|
|
|
|
::rtl::OUStringBuffer buf;
|
|
|
|
Converter::convertDuration(buf, od);
|
2011-09-27 20:21:15 +02:00
|
|
|
OSL_TRACE("%s",
|
|
|
|
::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
|
2010-05-19 18:35:11 +02:00
|
|
|
CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doTestDurationF(char const*const pis)
|
|
|
|
{
|
|
|
|
util::Duration od;
|
|
|
|
bool bSuccess = Converter::convertDuration(od,
|
|
|
|
::rtl::OUString::createFromAscii(pis));
|
|
|
|
OSL_TRACE("%d %dY %dM %dD %dH %dM %dS %dH",
|
|
|
|
od.Negative, od.Years, od.Months, od.Days,
|
|
|
|
od.Hours, od.Minutes, od.Seconds, od.MilliSeconds);
|
|
|
|
CPPUNIT_ASSERT(!bSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConverterTest::testDuration()
|
|
|
|
{
|
2011-09-20 10:18:12 +02:00
|
|
|
OSL_TRACE("\nSAX CONVERTER TEST BEGIN");
|
2010-05-19 18:35:11 +02:00
|
|
|
doTest( util::Duration(false, 1, 0, 0, 0, 0, 0, 0), "P1Y" );
|
|
|
|
doTest( util::Duration(false, 0, 42, 0, 0, 0, 0, 0), "P42M" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 111, 0, 0, 0, 0), "P111D" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 52, 0, 0, 0), "PT52H" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 717, 0, 0), "PT717M" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 121, 0), "PT121S" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 190), "PT0.19S" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 90), "PT0.09S" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 9), "PT0.009S" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 9, 999),
|
|
|
|
"PT9.999999999999999999999999999999S", "PT9.999S" );
|
|
|
|
doTest( util::Duration(true , 0, 0, 9999, 0, 0, 0, 0), "-P9999D" );
|
|
|
|
doTest( util::Duration(true , 7, 6, 5, 4, 3, 2, 10),
|
|
|
|
"-P7Y6M5DT4H3M2.01S" );
|
|
|
|
doTest( util::Duration(false, 0, 6, 0, 0, 3, 0, 0), "P6MT3M" );
|
|
|
|
doTest( util::Duration(false, 0, 0, 0, 0, 0, 0, 0), "P0D" );
|
|
|
|
doTestDurationF("1Y1M"); // invalid: no ^P
|
|
|
|
doTestDurationF("P-1Y1M"); // invalid: - after P
|
|
|
|
doTestDurationF("P1M1Y"); // invalid: Y after M
|
|
|
|
doTestDurationF("PT1Y"); // invalid: Y after T
|
|
|
|
doTestDurationF("P1Y1M1M"); // invalid: M twice, no T
|
|
|
|
doTestDurationF("P1YT1MT1M"); // invalid: T twice
|
|
|
|
doTestDurationF("P1YT"); // invalid: T but no H,M,S
|
|
|
|
doTestDurationF("P99999999999Y"); // cannot parse so many Ys
|
|
|
|
doTestDurationF("PT.1S"); // invalid: no 0 preceding .
|
|
|
|
doTestDurationF("PT5M.134S"); // invalid: no 0 preceding .
|
|
|
|
doTestDurationF("PT1.S"); // invalid: no digit following .
|
2011-09-20 10:18:12 +02:00
|
|
|
OSL_TRACE("\nSAX CONVERTER TEST END");
|
2010-05-19 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static bool eqDateTime(util::DateTime a, util::DateTime b) {
|
|
|
|
return a.Year == b.Year && a.Month == b.Month && a.Day == b.Day
|
|
|
|
&& a.Hours == b.Hours && a.Minutes == b.Minutes
|
|
|
|
&& a.Seconds == b.Seconds
|
|
|
|
&& a.HundredthSeconds == b.HundredthSeconds;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doTest(util::DateTime const & rdt, char const*const pis,
|
|
|
|
char const*const i_pos = 0)
|
|
|
|
{
|
|
|
|
char const*const pos((i_pos) ? i_pos : pis);
|
|
|
|
::rtl::OUString is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
util::DateTime odt;
|
|
|
|
bool bSuccess( Converter::convertDateTime(odt, is) );
|
|
|
|
OSL_TRACE("Y:%d M:%d D:%d H:%d M:%d S:%d H:%d",
|
|
|
|
odt.Year, odt.Month, odt.Day,
|
|
|
|
odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
|
|
|
|
CPPUNIT_ASSERT(bSuccess);
|
|
|
|
CPPUNIT_ASSERT(eqDateTime(rdt, odt));
|
|
|
|
::rtl::OUStringBuffer buf;
|
|
|
|
Converter::convertDateTime(buf, odt, true);
|
2011-09-27 20:21:15 +02:00
|
|
|
OSL_TRACE("%s",
|
|
|
|
::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
|
2010-05-19 18:35:11 +02:00
|
|
|
CPPUNIT_ASSERT(buf.makeStringAndClear().equalsAscii(pos));
|
|
|
|
}
|
|
|
|
|
|
|
|
static void doTestDateTimeF(char const*const pis)
|
|
|
|
{
|
|
|
|
util::DateTime odt;
|
|
|
|
bool bSuccess = Converter::convertDateTime(odt,
|
|
|
|
::rtl::OUString::createFromAscii(pis));
|
|
|
|
OSL_TRACE("Y:%d M:%d D:%d H:%dH M:%d S:%d H:%d",
|
|
|
|
odt.Year, odt.Month, odt.Day,
|
|
|
|
odt.Hours, odt.Minutes, odt.Seconds, odt.HundredthSeconds);
|
|
|
|
CPPUNIT_ASSERT(!bSuccess);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConverterTest::testDateTime()
|
|
|
|
{
|
2011-09-20 10:18:12 +02:00
|
|
|
OSL_TRACE("\nSAX CONVERTER TEST BEGIN");
|
2010-05-19 18:35:11 +02:00
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1), "0001-01-01T00:00:00" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
|
|
|
|
"0001-01-01T00:00:00Z", "0001-01-01T00:00:00" );
|
|
|
|
// doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00" );
|
|
|
|
// doTest( util::DateTime(0, 0, 0, 0, 1, 1, -1), "-0001-01-01T00:00:00Z" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
|
|
|
|
"0001-01-01T00:00:00-00:00", "0001-01-01T00:00:00" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
|
|
|
|
"0001-01-01T00:00:00+00:00", "0001-01-01T00:00:00" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 2, 1, 1)*/,
|
|
|
|
"0001-01-02T00:00:00-12:00", "0001-01-02T00:00:00" );
|
|
|
|
// "0001-02-01T12:00:00" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 2, 1, 1)/*(0, 0, 12, 0, 1, 1, 1)*/,
|
|
|
|
"0001-01-02T00:00:00+12:00", "0001-01-02T00:00:00" );
|
|
|
|
// "0001-01-01T12:00:00" );
|
|
|
|
doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
|
|
|
|
"9999-12-31T23:59:59.99" );
|
|
|
|
doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
|
|
|
|
"9999-12-31T23:59:59.99Z", "9999-12-31T23:59:59.99" );
|
|
|
|
doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
|
|
|
|
"9999-12-31T23:59:59.9999999999999999999999999999999999999",
|
|
|
|
"9999-12-31T23:59:59.99" );
|
|
|
|
doTest( util::DateTime(99, 59, 59, 23, 31, 12, 9999),
|
|
|
|
"9999-12-31T23:59:59.9999999999999999999999999999999999999Z",
|
|
|
|
"9999-12-31T23:59:59.99" );
|
2012-02-29 14:08:17 +01:00
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 29, 2, 2000), // leap year
|
|
|
|
"2000-02-29T00:00:00-00:00", "2000-02-29T00:00:00" );
|
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 29, 2, 1600), // leap year
|
|
|
|
"1600-02-29T00:00:00-00:00", "1600-02-29T00:00:00" );
|
2010-05-19 18:35:11 +02:00
|
|
|
doTest( util::DateTime(0, 0, 0, 24, 1, 1, 333)
|
|
|
|
/*(0, 0, 0, 0, 2, 1, 333)*/,
|
|
|
|
"0333-01-01T24:00:00"/*, "0333-01-02T00:00:00"*/ );
|
2011-12-01 23:00:07 +01:00
|
|
|
// While W3C XMLSchema specifies a minimum of 4 year digits we are lenient
|
|
|
|
// in what we accept.
|
2011-11-30 02:05:25 +01:00
|
|
|
doTest( util::DateTime(0, 0, 0, 0, 1, 1, 1),
|
|
|
|
"1-01-01T00:00:00", "0001-01-01T00:00:00" );
|
2011-12-01 23:00:07 +01:00
|
|
|
doTestDateTimeF( "+0001-01-01T00:00:00" ); // invalid: ^+
|
2010-05-19 18:35:11 +02:00
|
|
|
doTestDateTimeF( "0001-1-01T00:00:00" ); // invalid: < 2 M
|
|
|
|
doTestDateTimeF( "0001-01-1T00:00:00" ); // invalid: < 2 D
|
|
|
|
doTestDateTimeF( "0001-01-01T0:00:00" ); // invalid: < 2 H
|
|
|
|
doTestDateTimeF( "0001-01-01T00:0:00" ); // invalid: < 2 M
|
|
|
|
doTestDateTimeF( "0001-01-01T00:00:0" ); // invalid: < 2 S
|
|
|
|
doTestDateTimeF( "0001-01-01T00:00:00." ); // invalid: .$
|
|
|
|
doTestDateTimeF( "0001-01-01T00:00:00+1:00" ); // invalid: < 2 TZ H
|
|
|
|
doTestDateTimeF( "0001-01-01T00:00:00+00:1" ); // invalid: < 2 TZ M
|
|
|
|
doTestDateTimeF( "0001-13-01T00:00:00" ); // invalid: M > 12
|
|
|
|
doTestDateTimeF( "0001-01-32T00:00:00" ); // invalid: D > 31
|
|
|
|
doTestDateTimeF( "0001-01-01T25:00:00" ); // invalid: H > 24
|
|
|
|
doTestDateTimeF( "0001-01-01T00:60:00" ); // invalid: H > 59
|
|
|
|
doTestDateTimeF( "0001-01-01T00:00:60" ); // invalid: S > 59
|
|
|
|
doTestDateTimeF( "0001-01-01T24:01:00" ); // invalid: H=24, but M != 0
|
|
|
|
doTestDateTimeF( "0001-01-01T24:00:01" ); // invalid: H=24, but S != 0
|
|
|
|
doTestDateTimeF( "0001-01-01T24:00:00.1" ); // invalid: H=24, but H != 0
|
|
|
|
doTestDateTimeF( "0001-01-02T00:00:00+15:00" ); // invalid: TZ > +14:00
|
|
|
|
doTestDateTimeF( "0001-01-02T00:00:00+14:01" ); // invalid: TZ > +14:00
|
|
|
|
doTestDateTimeF( "0001-01-02T00:00:00-15:00" ); // invalid: TZ < -14:00
|
|
|
|
doTestDateTimeF( "0001-01-02T00:00:00-14:01" ); // invalid: TZ < -14:00
|
2012-02-29 14:08:17 +01:00
|
|
|
doTestDateTimeF( "2100-02-29T00:00:00-00:00" ); // invalid: no leap year
|
|
|
|
doTestDateTimeF( "1900-02-29T00:00:00-00:00" ); // invalid: no leap year
|
2011-09-20 10:18:12 +02:00
|
|
|
OSL_TRACE("\nSAX CONVERTER TEST END");
|
2010-05-19 18:35:11 +02:00
|
|
|
}
|
|
|
|
|
2012-04-20 18:39:36 +02:00
|
|
|
void doTestDouble(char const*const pis, double const rd,
|
|
|
|
sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
|
|
|
|
{
|
|
|
|
::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
double od;
|
|
|
|
bool bSuccess(Converter::convertDouble(od, is, nSourceUnit, nTargetUnit));
|
|
|
|
OSL_TRACE("%f", od);
|
|
|
|
CPPUNIT_ASSERT(bSuccess);
|
|
|
|
CPPUNIT_ASSERT_DOUBLES_EQUAL(rd, od, 0.00000001);
|
|
|
|
::rtl::OUStringBuffer buf;
|
|
|
|
Converter::convertDouble(buf, od, true, nTargetUnit, nSourceUnit);
|
|
|
|
OSL_TRACE("%s",
|
|
|
|
::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
|
|
|
|
}
|
|
|
|
|
|
|
|
void ConverterTest::testDouble()
|
|
|
|
{
|
2012-06-29 12:59:50 +01:00
|
|
|
doTestDouble("42", 42.0, MeasureUnit::TWIP, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::POINT, MeasureUnit::POINT);
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::MM_100TH, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::MM_10TH, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::MM, MeasureUnit::MM); // identity don't seem to add unit?
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::CM, MeasureUnit::CM);
|
|
|
|
doTestDouble("42", 42.0, MeasureUnit::INCH, MeasureUnit::INCH);
|
|
|
|
doTestDouble("2pt", 40.0, MeasureUnit::POINT, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("20pc", 1, MeasureUnit::TWIP, MeasureUnit::POINT);
|
|
|
|
doTestDouble("4", 2.26771653543307, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("4", 22.6771653543307, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("4mm", 226.771653543307, MeasureUnit::MM, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("4cm", 2267.71653543307, MeasureUnit::CM, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("4in", 5760.0, MeasureUnit::INCH, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("1440pc", 1.0, MeasureUnit::TWIP, MeasureUnit::INCH);
|
|
|
|
doTestDouble("567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::CM);
|
|
|
|
doTestDouble("56.7pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM);
|
|
|
|
doTestDouble("5.67pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("0.567pc", 1.000125, MeasureUnit::TWIP, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("42pt", 1.4816666666666, MeasureUnit::POINT, MeasureUnit::CM);
|
|
|
|
doTestDouble("42pt", 14.816666666666, MeasureUnit::POINT, MeasureUnit::MM);
|
|
|
|
doTestDouble("42pt", 148.16666666666, MeasureUnit::POINT, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("42pt", 1481.6666666666, MeasureUnit::POINT, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("72pt", 1.0, MeasureUnit::POINT, MeasureUnit::INCH);
|
|
|
|
doTestDouble("3.5in", 8.89, MeasureUnit::INCH, MeasureUnit::CM);
|
|
|
|
doTestDouble("3.5in", 88.9, MeasureUnit::INCH, MeasureUnit::MM);
|
|
|
|
doTestDouble("3.5in", 889.0, MeasureUnit::INCH, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("3.5in", 8890.0, MeasureUnit::INCH, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("2in", 144, MeasureUnit::INCH, MeasureUnit::POINT);
|
|
|
|
doTestDouble("5.08cm", 2.0, MeasureUnit::CM, MeasureUnit::INCH);
|
|
|
|
doTestDouble("3.5cm", 3500.0, MeasureUnit::CM, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("3.5cm", 350.0, MeasureUnit::CM, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("3.5cm", 35.0, MeasureUnit::CM, MeasureUnit::MM);
|
|
|
|
doTestDouble("10cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::POINT);
|
|
|
|
doTestDouble("0.5cm", 283.464566929134, MeasureUnit::CM, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("10mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::POINT);
|
|
|
|
doTestDouble("0.5mm", 28.3464566929134, MeasureUnit::MM, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("10", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::POINT);
|
|
|
|
doTestDouble("0.5", 2.83464566929134, MeasureUnit::MM_10TH, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("10", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::POINT);
|
|
|
|
doTestDouble("0.5", 0.283464566929134, MeasureUnit::MM_100TH, MeasureUnit::TWIP);
|
|
|
|
doTestDouble("10mm", 1.0, MeasureUnit::MM, MeasureUnit::CM);
|
|
|
|
doTestDouble("10mm", 100.0, MeasureUnit::MM, MeasureUnit::MM_10TH);
|
|
|
|
doTestDouble("20mm", 2000.0, MeasureUnit::MM, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("300", 30.0, MeasureUnit::MM_10TH, MeasureUnit::MM);
|
|
|
|
doTestDouble("400", 4.0, MeasureUnit::MM_100TH, MeasureUnit::MM);
|
|
|
|
doTestDouble("600", 6000.0, MeasureUnit::MM_10TH, MeasureUnit::MM_100TH);
|
|
|
|
doTestDouble("700", 70.0, MeasureUnit::MM_100TH, MeasureUnit::MM_10TH);
|
2012-04-20 18:39:36 +02:00
|
|
|
}
|
|
|
|
|
2012-08-09 01:09:25 +02:00
|
|
|
void doTestStringToMeasure(sal_Int32 rValue, char const*const pis, sal_Int16 nTargetUnit, sal_Int32 nMin, sal_Int32 nMax)
|
|
|
|
{
|
|
|
|
::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
sal_Int32 nVal;
|
|
|
|
bool bSuccess(Converter::convertMeasure(nVal, is, nTargetUnit, nMin, nMax));
|
|
|
|
OSL_TRACE("%i", nVal);
|
|
|
|
CPPUNIT_ASSERT(bSuccess);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(rValue, nVal);
|
|
|
|
}
|
|
|
|
|
2012-08-09 13:12:44 +02:00
|
|
|
void doTestMeasureToString(char const*const pis, sal_Int32 nMeasure, sal_Int16 const nSourceUnit, sal_Int16 const nTargetUnit)
|
|
|
|
{
|
|
|
|
::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
::rtl::OUStringBuffer buf;
|
|
|
|
Converter::convertMeasure(buf, nMeasure, nSourceUnit, nTargetUnit);
|
|
|
|
OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
|
|
|
|
}
|
|
|
|
|
2012-08-09 01:09:25 +02:00
|
|
|
void ConverterTest::testMeasure()
|
|
|
|
{
|
2012-08-09 22:42:09 +02:00
|
|
|
//check all the measure units
|
2012-08-09 01:41:22 +02:00
|
|
|
doTestStringToMeasure(1000, "10mm", MeasureUnit::MM_100TH, -1, 4321);
|
|
|
|
doTestStringToMeasure(200, "20mm", MeasureUnit::MM_10TH, 12, 4567);
|
|
|
|
doTestStringToMeasure(300, "300", MeasureUnit::MM, 31, 555);
|
|
|
|
doTestStringToMeasure(400, "400", MeasureUnit::CM, 10, 4321);
|
|
|
|
doTestStringToMeasure(120, "120", MeasureUnit::INCH_1000TH, 10, 4321);
|
|
|
|
doTestStringToMeasure(111, "111", MeasureUnit::INCH_100TH, 10, 4321);
|
|
|
|
doTestStringToMeasure(22, "22", MeasureUnit::INCH_10TH, 10, 4321);
|
|
|
|
doTestStringToMeasure(27, "27", MeasureUnit::INCH, 10, 4321);
|
|
|
|
doTestStringToMeasure(52, "52", MeasureUnit::POINT, 10, 4321);
|
|
|
|
doTestStringToMeasure(120, "120", MeasureUnit::TWIP, 10, 4321);
|
|
|
|
doTestStringToMeasure(666, "666", MeasureUnit::M, 10, 4321);
|
|
|
|
doTestStringToMeasure(42, "42", MeasureUnit::KM, 10, 4321);
|
|
|
|
doTestStringToMeasure(30, "30", MeasureUnit::PICA, 10, 4321);
|
|
|
|
doTestStringToMeasure(20, "20", MeasureUnit::FOOT, 10, 4321);
|
|
|
|
doTestStringToMeasure(40, "40", MeasureUnit::MILE, 10, 4321);
|
|
|
|
doTestStringToMeasure(40, "40%", MeasureUnit::PERCENT, 10, 4321);
|
|
|
|
doTestStringToMeasure(800, "800", MeasureUnit::PIXEL, 10, 4321);
|
|
|
|
doTestStringToMeasure(600, "600px", MeasureUnit::PIXEL, 10, 4321);
|
|
|
|
doTestStringToMeasure(777, "777", MeasureUnit::APPFONT, 10, 4321);
|
|
|
|
doTestStringToMeasure(80000, "80000", MeasureUnit::SYSFONT, 10, 432100);
|
2012-08-09 22:42:09 +02:00
|
|
|
//strange values (negative, too large etc.)
|
|
|
|
doTestStringToMeasure(-539222987, "1234567890mm", MeasureUnit::MM_10TH, 12, 12345678901);
|
|
|
|
doTestStringToMeasure(-300, "-300", MeasureUnit::MM, -1000, 555);
|
|
|
|
doTestStringToMeasure(1305424328, "-999999999999999px", MeasureUnit::PIXEL, -88888888888, 555); //really crazy numbers...
|
|
|
|
|
|
|
|
doTestMeasureToString("6mm", 600, MeasureUnit::MM_100TH, MeasureUnit::MM);
|
|
|
|
doTestMeasureToString("0.005cm", 000000005, MeasureUnit::MM_100TH, MeasureUnit::CM); // zeros in the front doesn't count
|
|
|
|
doTestMeasureToString("3mm", 30, MeasureUnit::MM_10TH, MeasureUnit::MM);
|
|
|
|
doTestMeasureToString("6.66cm", 666, MeasureUnit::MM_10TH, MeasureUnit::CM);
|
|
|
|
doTestMeasureToString("-157.3pt", -555, MeasureUnit::MM_10TH, MeasureUnit::POINT);
|
|
|
|
doTestMeasureToString("174976.378in", 44444000, MeasureUnit::MM_10TH, MeasureUnit::INCH); //let's check accuracy
|
|
|
|
doTestMeasureToString("40%", 40, MeasureUnit::PERCENT, MeasureUnit::PERCENT);
|
|
|
|
doTestMeasureToString("70.56mm", 4000, MeasureUnit::TWIP, MeasureUnit::MM);
|
|
|
|
doTestMeasureToString("979.928cm", 555550, MeasureUnit::TWIP, MeasureUnit::CM);
|
|
|
|
doTestMeasureToString("111.1pt", 2222, MeasureUnit::TWIP, MeasureUnit::POINT);
|
|
|
|
doTestMeasureToString("385.7986in", 555550, MeasureUnit::TWIP, MeasureUnit::INCH);
|
2012-08-09 01:09:25 +02:00
|
|
|
}
|
2012-08-10 14:30:07 +02:00
|
|
|
|
|
|
|
void doTestStringToBool(bool bBool, char const*const pis)
|
|
|
|
{
|
|
|
|
::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
bool bTemp;
|
|
|
|
bool bSuccess(Converter::convertBool(bTemp, is));
|
|
|
|
OSL_TRACE("%s", bTemp);
|
|
|
|
CPPUNIT_ASSERT(bSuccess);
|
|
|
|
CPPUNIT_ASSERT_EQUAL(bBool, bTemp);
|
|
|
|
|
|
|
|
}
|
2012-08-10 14:36:24 +02:00
|
|
|
|
|
|
|
void doTestBoolToString(char const*const pis, bool bValue )
|
|
|
|
{
|
|
|
|
::rtl::OUString const is(::rtl::OUString::createFromAscii(pis));
|
|
|
|
::rtl::OUStringBuffer buf;
|
|
|
|
Converter::convertBool(buf, bValue);
|
|
|
|
OSL_TRACE("%s", ::rtl::OUStringToOString(buf.getStr(), RTL_TEXTENCODING_UTF8).getStr());
|
|
|
|
CPPUNIT_ASSERT_EQUAL(is, buf.makeStringAndClear());
|
|
|
|
}
|
|
|
|
|
2012-08-10 14:30:07 +02:00
|
|
|
void ConverterTest::testBool()
|
|
|
|
{
|
|
|
|
doTestStringToBool(true, "true");
|
|
|
|
doTestStringToBool(false, "false");
|
2012-08-10 14:36:24 +02:00
|
|
|
doTestBoolToString("true", true);
|
|
|
|
doTestBoolToString("false", false);
|
2012-08-10 14:30:07 +02:00
|
|
|
}
|
2012-08-10 14:36:24 +02:00
|
|
|
|
2010-05-19 18:35:11 +02:00
|
|
|
CPPUNIT_TEST_SUITE_REGISTRATION(ConverterTest);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
CPPUNIT_PLUGIN_IMPLEMENT();
|
|
|
|
|
2010-10-14 08:27:31 +02:00
|
|
|
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|