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 "private:factory/scalc", "_blank", 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();
63 test::BootstrapFixture::tearDown();
67 void LpSolverTest::testLpSolver()
69 testSolver("com.sun.star.comp.Calc.LpsolveSolver");
74 void LpSolverTest::testCoinMPSolver()
76 testSolver("com.sun.star.comp.Calc.CoinMPSolver");
80 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
81 void LpSolverTest::testSolver(OUString
const & rName
)
83 uno::Reference
<sheet::XSolver
> xSolver(m_xContext
->getServiceManager()->
84 createInstanceWithContext(rName
, m_xContext
), uno::UNO_QUERY_THROW
);
86 table::CellAddress
aObjective(0, 0, 0);
88 // "changing cells" - unknown variables
89 uno::Sequence
<table::CellAddress
> aVariables(1);
90 aVariables
[0] = table::CellAddress(0, 0, 0);
93 uno::Sequence
<sheet::SolverConstraint
> aConstraints(1);
94 aConstraints
[0].Left
= table::CellAddress(0, 0, 0);
95 aConstraints
[0].Operator
= sheet::SolverConstraintOperator_LESS_EQUAL
;
96 aConstraints
[0].Right
<<= 5.0;
99 xSolver
->setDocument( m_xDocument
);
100 xSolver
->setObjective( aObjective
);
101 xSolver
->setVariables( aVariables
);
102 xSolver
->setConstraints( aConstraints
);
103 xSolver
->setMaximize( true );
107 CPPUNIT_ASSERT(xSolver
->getSuccess());
108 uno::Sequence
<double> aSolution
= xSolver
->getSolution();
109 CPPUNIT_ASSERT_EQUAL(aSolution
.getLength(), aVariables
.getLength());
110 CPPUNIT_ASSERT_EQUAL(aSolution
[0], 5.0);
112 uno::Reference
<sheet::XSolverDescription
> xDesc(xSolver
, uno::UNO_QUERY_THROW
);
113 const OString
sMessage("Empty description for " + OUStringToOString(rName
, RTL_TEXTENCODING_UTF8
));
114 CPPUNIT_ASSERT_MESSAGE(sMessage
.getStr(), !xDesc
->getComponentDescription().isEmpty());
118 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest
);
122 CPPUNIT_PLUGIN_IMPLEMENT();
124 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */