Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / sccomp / qa / unit / solver.cxx
blob76d94fee88c3948ec930a89422df9a154f352c51
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
21 using namespace css;
23 namespace {
25 class LpSolverTest: public test::BootstrapFixture
27 uno::Reference<sheet::XSpreadsheetDocument> m_xDocument;
29 #ifdef ENABLE_LPSOLVE
30 void testLpSolver();
31 #endif
32 #ifdef ENABLE_COINMP
33 void testCoinMPSolver();
34 #endif
36 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
37 void testSolver(OUString const & rName);
38 #endif
40 public:
41 virtual void setUp() override;
42 virtual void tearDown() override;
44 CPPUNIT_TEST_SUITE(LpSolverTest);
45 #ifdef ENABLE_LPSOLVE
46 CPPUNIT_TEST(testLpSolver);
47 #endif
48 #ifdef ENABLE_COINMP
49 CPPUNIT_TEST(testCoinMPSolver);
50 #endif
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();
70 #ifdef ENABLE_LPSOLVE
71 void LpSolverTest::testLpSolver()
73 testSolver("com.sun.star.comp.Calc.LpsolveSolver");
75 #endif
77 #ifdef ENABLE_COINMP
78 void LpSolverTest::testCoinMPSolver()
80 testSolver("com.sun.star.comp.Calc.CoinMPSolver");
82 #endif
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);
96 // constraints
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;
102 // initialize solver
103 xSolver->setDocument( m_xDocument );
104 xSolver->setObjective( aObjective );
105 xSolver->setVariables( aVariables );
106 xSolver->setConstraints( aConstraints );
107 xSolver->setMaximize( true );
109 // test results
110 xSolver->solve();
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());
120 #endif
122 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest);
126 CPPUNIT_PLUGIN_IMPLEMENT();
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */