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/frame/Desktop.hpp>
13 #include <com/sun/star/sheet/XSolver.hpp>
14 #include <com/sun/star/sheet/XSolverDescription.hpp>
15 #include <test/bootstrapfixture.hxx>
21 class LpSolverTest
: public test::BootstrapFixture
23 uno::Reference
<sheet::XSpreadsheetDocument
> m_xDocument
;
29 void testCoinMPSolver();
32 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
33 void testSolver(OUString
const & rName
);
37 virtual void setUp() override
;
38 virtual void tearDown() override
;
40 CPPUNIT_TEST_SUITE(LpSolverTest
);
42 CPPUNIT_TEST(testLpSolver
);
45 CPPUNIT_TEST(testCoinMPSolver
);
47 CPPUNIT_TEST_SUITE_END();
50 void LpSolverTest::setUp()
52 test::BootstrapFixture::setUp();
53 uno::Reference
<frame::XDesktop2
> xComponentLoader
= frame::Desktop::create(m_xContext
);
54 uno::Reference
<lang::XComponent
> xComponent(xComponentLoader
->loadComponentFromURL(
55 u
"private:factory/scalc"_ustr
, u
"_blank"_ustr
, 0,
56 uno::Sequence
< css::beans::PropertyValue
>()));
57 m_xDocument
.set(xComponent
, uno::UNO_QUERY_THROW
);
60 void LpSolverTest::tearDown()
62 uno::Reference
<lang::XComponent
>(m_xDocument
, uno::UNO_QUERY_THROW
)->dispose();
64 test::BootstrapFixture::tearDown();
68 void LpSolverTest::testLpSolver()
70 testSolver(u
"com.sun.star.comp.Calc.LpsolveSolver"_ustr
);
75 void LpSolverTest::testCoinMPSolver()
77 testSolver(u
"com.sun.star.comp.Calc.CoinMPSolver"_ustr
);
81 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
82 void LpSolverTest::testSolver(OUString
const & rName
)
84 uno::Reference
<sheet::XSolver
> xSolver(m_xContext
->getServiceManager()->
85 createInstanceWithContext(rName
, m_xContext
), uno::UNO_QUERY_THROW
);
87 table::CellAddress
aObjective(0, 0, 0);
89 // "changing cells" - unknown variables
90 uno::Sequence
<table::CellAddress
> aVariables
{ {0, 0, 0 } };
93 uno::Sequence
<sheet::SolverConstraint
> aConstraints
{
94 { /* Left */ table::CellAddress(0, 0, 0),
95 /* Operator */ sheet::SolverConstraintOperator_LESS_EQUAL
,
96 /* Right */ uno::Any(5.0) }
100 xSolver
->setDocument( m_xDocument
);
101 xSolver
->setObjective( aObjective
);
102 xSolver
->setVariables( aVariables
);
103 xSolver
->setConstraints( aConstraints
);
104 xSolver
->setMaximize( true );
108 CPPUNIT_ASSERT(xSolver
->getSuccess());
109 uno::Sequence
<double> aSolution
= xSolver
->getSolution();
110 CPPUNIT_ASSERT_EQUAL(aSolution
.getLength(), aVariables
.getLength());
111 CPPUNIT_ASSERT_EQUAL(5.0, aSolution
[0]);
113 uno::Reference
<sheet::XSolverDescription
> xDesc(xSolver
, uno::UNO_QUERY_THROW
);
114 const OString
sMessage("Empty description for " + OUStringToOString(rName
, RTL_TEXTENCODING_UTF8
));
115 CPPUNIT_ASSERT_MESSAGE(sMessage
.getStr(), !xDesc
->getComponentDescription().isEmpty());
119 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest
);
123 CPPUNIT_PLUGIN_IMPLEMENT();
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */