diff options
author | Philippe Jung <phil.jung@free.fr> | 2015-06-22 11:41:43 +0200 |
---|---|---|
committer | Philippe Jung <phil.jung@free.fr> | 2015-06-22 09:55:49 +0000 |
commit | 733f5887af46d1d5e2fbea6be93cabb2a1749752 (patch) | |
tree | f47a73da757035f73d7e51db91bffb1c26fc91f0 /chart2/qa/extras/chart2_trendcalculators.cxx | |
parent | 9908b6447ebbeec16544bd61eca6f22f1ecefbaa (diff) |
Add unit tests for chart2 trend calculators
Will be used to check tdf#70673 is fixed. Test will fail before the fix
and should pass after.
Change-Id: I9e0fdc696fb99ac2f0e24b04273391d526c629a6
Reviewed-on: https://gerrit.libreoffice.org/16406
Reviewed-by: Philippe Jung <phil.jung@free.fr>
Tested-by: Philippe Jung <phil.jung@free.fr>
Diffstat (limited to 'chart2/qa/extras/chart2_trendcalculators.cxx')
-rw-r--r-- | chart2/qa/extras/chart2_trendcalculators.cxx | 214 |
1 files changed, 214 insertions, 0 deletions
diff --git a/chart2/qa/extras/chart2_trendcalculators.cxx b/chart2/qa/extras/chart2_trendcalculators.cxx new file mode 100644 index 000000000000..7ddb4d80afac --- /dev/null +++ b/chart2/qa/extras/chart2_trendcalculators.cxx @@ -0,0 +1,214 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * 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/. + */ + +#include "charttest.hxx" +#include <com/sun/star/chart2/XRegressionCurveContainer.hpp> +#include <com/sun/star/chart2/XRegressionCurveCalculator.hpp> + +// Define the index of sheets in the test document +#define SHEET_POTENTIAL1 0 +#define SHEET_POTENTIAL2 ( SHEET_POTENTIAL1 + 1) +#define SHEET_POTENTIAL_LAST ( SHEET_POTENTIAL2 ) + +#define SHEET_LINEAR1 ( SHEET_POTENTIAL_LAST + 1 ) +#define SHEET_LINEAR_LAST ( SHEET_LINEAR1 ) + +#define SHEET_POLYNOMIAL1 ( SHEET_LINEAR_LAST + 1 ) +#define SHEET_POLYNOMIAL_LAST ( SHEET_POLYNOMIAL1 ) + +#define SHEET_EXPONENTIAL1 ( SHEET_POLYNOMIAL_LAST + 1 ) +#define SHEET_EXPONENTIAL2 ( SHEET_EXPONENTIAL1 + 1 ) +#define SHEET_EXPONENTIAL_LAST ( SHEET_EXPONENTIAL2 ) + +class Chart2TrendCalculators : public ChartTest +{ +public: + void setUp(); + void tearDown(); + + void testPotentialRegression1(); + void testPotentialRegression2(); + void testLinearRegression1(); + void testPolynomialRegression1(); + void testExponentialRegression1(); + void testExponentialRegression2(); + + CPPUNIT_TEST_SUITE(Chart2TrendCalculators); + CPPUNIT_TEST(testPotentialRegression1); + CPPUNIT_TEST(testPotentialRegression2); + CPPUNIT_TEST(testLinearRegression1); + CPPUNIT_TEST(testPolynomialRegression1); + CPPUNIT_TEST(testExponentialRegression1); + CPPUNIT_TEST(testExponentialRegression2); + CPPUNIT_TEST_SUITE_END(); + +private: + + Reference<chart2::XRegressionCurve> m_xCurve; + Reference< chart2::XRegressionCurveCalculator > m_xRegressionCurveCalculator; + + void loadCalculatorFromSheet(sal_Int32 nSheet); + void checkCalculator( + Sequence< double > xValues, Sequence< double > yValues, + OUString sExpectedFormula ); + +}; + +void Chart2TrendCalculators::setUp() +{ + ChartTest::setUp(); + load("/chart2/qa/extras/data/ods/", "trend_calculators.ods"); +} + +void Chart2TrendCalculators::tearDown() +{ + m_xRegressionCurveCalculator.clear(); + m_xCurve.clear(); + ChartTest::tearDown(); +} + +void Chart2TrendCalculators::loadCalculatorFromSheet(sal_Int32 nSheet) +{ + Reference<chart2::XChartDocument> xChartDoc = getChartDocFromSheet(nSheet, mxComponent); + CPPUNIT_ASSERT_MESSAGE("failed to load chart", xChartDoc.is()); + CPPUNIT_ASSERT(xChartDoc.is()); + + Reference<chart2::XDataSeries> xDataSeries = getDataSeriesFromDoc(xChartDoc, 0); + CPPUNIT_ASSERT(xDataSeries.is()); + + Reference<chart2::XRegressionCurveContainer> xRegressionCurveContainer(xDataSeries, UNO_QUERY_THROW); + CPPUNIT_ASSERT( xRegressionCurveContainer.is() ); + + Sequence< Reference< chart2::XRegressionCurve > > xRegressionCurveSequence = xRegressionCurveContainer->getRegressionCurves(); + CPPUNIT_ASSERT_EQUAL((sal_Int32) 1, xRegressionCurveSequence.getLength()); + + m_xCurve = xRegressionCurveSequence[0]; + CPPUNIT_ASSERT(m_xCurve.is()); + + m_xRegressionCurveCalculator = m_xCurve->getCalculator(); + CPPUNIT_ASSERT(m_xRegressionCurveCalculator.is()); +} + +void Chart2TrendCalculators::checkCalculator( + Sequence< double > xValues, Sequence< double > yValues, + OUString sExpectedFormula ) +{ + m_xRegressionCurveCalculator->recalculateRegression( xValues, yValues ); + OUString aRepresentation = m_xRegressionCurveCalculator->getRepresentation (); + CPPUNIT_ASSERT_EQUAL( sExpectedFormula, aRepresentation ); + double r2 = m_xRegressionCurveCalculator->getCorrelationCoefficient(); + CPPUNIT_ASSERT_DOUBLES_EQUAL( 1.0, r2, 1e-8 ); +} + +// test y = A x ^ B +void Chart2TrendCalculators::testPotentialRegression1() +{ + loadCalculatorFromSheet( SHEET_POTENTIAL1 ); + m_xRegressionCurveCalculator->setRegressionProperties( 0, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = 2.0 * pow ( d, 3 ); + } + checkCalculator( xValues, yValues, "f(x) = 2 x^3"); +} + +// test y = A x ^ B +void Chart2TrendCalculators::testPotentialRegression2() +{ + loadCalculatorFromSheet( SHEET_POTENTIAL2 ); + m_xRegressionCurveCalculator->setRegressionProperties( 0, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = -2.0 * pow ( d, 3 ); + } + checkCalculator( xValues, yValues, "f(x) = -2 x^3"); +} + +// test y = - 2 X - 5 +void Chart2TrendCalculators::testLinearRegression1() +{ + loadCalculatorFromSheet( SHEET_LINEAR1 ); + m_xRegressionCurveCalculator->setRegressionProperties( 1, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = - 2.0 * d - 5.0 ; + } + checkCalculator( xValues, yValues, "f(x) = - 2x - 5"); +} + +// test y = A x ^ B +void Chart2TrendCalculators::testPolynomialRegression1() +{ + loadCalculatorFromSheet( SHEET_POLYNOMIAL1 ); + m_xRegressionCurveCalculator->setRegressionProperties( 2, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = - 2.0 * d * d + 4 * d - 5; + } + checkCalculator( xValues, yValues, "f(x) = - 2x^2 + 4x - 5"); +} + +void Chart2TrendCalculators::testExponentialRegression1() +{ + loadCalculatorFromSheet( SHEET_EXPONENTIAL1 ); + m_xRegressionCurveCalculator->setRegressionProperties( 0, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = 2.0 * exp ( 0.3 * d ); + } + checkCalculator( xValues, yValues, "f(x) = 2 exp( 0.3 x )"); +} + +void Chart2TrendCalculators::testExponentialRegression2() +{ + loadCalculatorFromSheet( SHEET_EXPONENTIAL2 ); + m_xRegressionCurveCalculator->setRegressionProperties( 0, false, 0, 0 ); + Sequence< double > xValues( 7 ); + Sequence< double > yValues( 7 ); + double d; + for (int i=0; i<7; i++) + { + d = (double) i; + xValues[i] = d; + yValues[i] = -2.0 * exp ( 0.3 * d ); + } + checkCalculator( xValues, yValues, "f(x) = -2 exp( 0.3 x )"); +} + + +CPPUNIT_TEST_SUITE_REGISTRATION(Chart2TrendCalculators); + +CPPUNIT_PLUGIN_IMPLEMENT(); + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |