summaryrefslogtreecommitdiff
path: root/chart2/qa/extras/chart2_trendcalculators.cxx
blob: 0fd3c1d27fb77fda620bc47c6450640b0a1d90dd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/* -*- 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 )

class Chart2TrendCalculators : public ChartTest
{
public:
    void setUp() override;
    void tearDown() override;

    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(
        const Sequence< double >& xValues, const Sequence< double >& yValues,
        const 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(
    const Sequence< double >& xValues, const Sequence< double >& yValues,
    const 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 );
    for (int i=0; i<7; i++)
    {
        const double 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 );
    for (int i=0; i<7; i++)
    {
        const double 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 );
    for (int i=0; i<7; i++)
    {
        const double 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 );
    for (int i=0; i<7; i++)
    {
        const double 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 );
    for (int i=0; i<7; i++)
    {
        const double 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 );
    for (int i=0; i<7; i++)
    {
        const double 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: */