1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <sal/config.h>
12 #include <com/sun/star/container/XContentEnumerationAccess.hpp>
13 #include <com/sun/star/frame/Desktop.hpp>
14 #include <com/sun/star/lang/XServiceInfo.hpp>
15 #include <com/sun/star/sheet/XSolver.hpp>
16 #include <com/sun/star/sheet/XSolverDescription.hpp>
17 #include <test/bootstrapfixture.hxx>
19 #include <address.hxx>
25 class LpSolverTest
: public test::BootstrapFixture
27 uno::Reference
<sheet::XSpreadsheetDocument
> m_xDocument
;
33 void testCoinMPSolver();
36 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
37 void testSolver(OUString
const & rName
);
41 virtual void setUp() override
;
42 virtual void tearDown() override
;
44 CPPUNIT_TEST_SUITE(LpSolverTest
);
46 CPPUNIT_TEST(testLpSolver
);
49 CPPUNIT_TEST(testCoinMPSolver
);
51 CPPUNIT_TEST_SUITE_END();
54 void LpSolverTest::setUp()
56 test::BootstrapFixture::setUp();
57 uno::Reference
<frame::XDesktop2
> xComponentLoader
= frame::Desktop::create(m_xContext
);
58 uno::Reference
<lang::XComponent
> xComponent(xComponentLoader
->loadComponentFromURL(
59 "private:factory/scalc", "_blank", 0,
60 uno::Sequence
< css::beans::PropertyValue
>()));
61 m_xDocument
.set(xComponent
, uno::UNO_QUERY_THROW
);
64 void LpSolverTest::tearDown()
66 uno::Reference
<lang::XComponent
>(m_xDocument
, uno::UNO_QUERY_THROW
)->dispose();
67 test::BootstrapFixture::tearDown();
71 void LpSolverTest::testLpSolver()
73 testSolver("com.sun.star.comp.Calc.LpsolveSolver");
78 void LpSolverTest::testCoinMPSolver()
80 testSolver("com.sun.star.comp.Calc.CoinMPSolver");
84 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
85 void LpSolverTest::testSolver(OUString
const & rName
)
87 uno::Reference
<sheet::XSolver
> xSolver(m_xContext
->getServiceManager()->
88 createInstanceWithContext(rName
, m_xContext
), uno::UNO_QUERY_THROW
);
90 table::CellAddress
aObjective(0, 0, 0);
92 // "changing cells" - unknown variables
93 uno::Sequence
<table::CellAddress
> aVariables(1);
94 aVariables
[0] = table::CellAddress(0, 0, 0);
97 uno::Sequence
<sheet::SolverConstraint
> aConstraints(1);
98 aConstraints
[0].Left
= table::CellAddress(0, 0, 0);
99 aConstraints
[0].Operator
= sheet::SolverConstraintOperator_LESS_EQUAL
;
100 aConstraints
[0].Right
<<= 5.0;
103 xSolver
->setDocument( m_xDocument
);
104 xSolver
->setObjective( aObjective
);
105 xSolver
->setVariables( aVariables
);
106 xSolver
->setConstraints( aConstraints
);
107 xSolver
->setMaximize( true );
111 CPPUNIT_ASSERT(xSolver
->getSuccess());
112 uno::Sequence
<double> aSolution
= xSolver
->getSolution();
113 CPPUNIT_ASSERT_EQUAL(aSolution
.getLength(), aVariables
.getLength());
114 CPPUNIT_ASSERT_EQUAL(aSolution
[0], 5.0);
116 uno::Reference
<sheet::XSolverDescription
> xDesc(xSolver
, uno::UNO_QUERY_THROW
);
117 const OString
sMessage("Empty description for " + OUStringToOString(rName
, RTL_TEXTENCODING_UTF8
));
118 CPPUNIT_ASSERT_MESSAGE(sMessage
.getStr(), !xDesc
->getComponentDescription().isEmpty());
122 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest
);
126 CPPUNIT_PLUGIN_IMPLEMENT();
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */