Branch libreoffice-5-0-4
[LibreOffice.git] / sccomp / qa / unit / lpsolver.cxx
blob75c091bd782d306f963fcd20e6d40d7106111e86
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 void test();
30 void testSolver(const uno::Reference<sheet::XSolver>& xSolver);
32 public:
33 virtual void setUp() SAL_OVERRIDE;
34 virtual void tearDown() SAL_OVERRIDE;
36 CPPUNIT_TEST_SUITE(LpSolverTest);
37 CPPUNIT_TEST(test);
38 CPPUNIT_TEST_SUITE_END();
41 void LpSolverTest::setUp()
43 test::BootstrapFixture::setUp();
44 uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(m_xContext);
45 uno::Reference<lang::XComponent> xComponent(xComponentLoader->loadComponentFromURL(
46 "private:factory/scalc", "_blank", 0,
47 uno::Sequence < ::com::sun::star::beans::PropertyValue >()));
48 m_xDocument.set(xComponent, uno::UNO_QUERY_THROW);
51 void LpSolverTest::tearDown()
53 uno::Reference<lang::XComponent>(m_xDocument, uno::UNO_QUERY_THROW)->dispose();
54 test::BootstrapFixture::tearDown();
57 void LpSolverTest::test()
59 uno::Reference<container::XContentEnumerationAccess> xEnAc(
60 m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
61 uno::Reference<container::XEnumeration> xEnum = xEnAc->
62 createContentEnumeration( "com.sun.star.sheet.Solver" );
63 CPPUNIT_ASSERT(xEnum.is());
65 sal_Int32 nCount = 0;
66 while (xEnum->hasMoreElements())
68 uno::Reference<uno::XInterface> xIntFac;
69 xEnum->nextElement() >>= xIntFac;
70 CPPUNIT_ASSERT(xIntFac.is());
71 uno::Reference<lang::XServiceInfo> xInfo(xIntFac, uno::UNO_QUERY_THROW);
72 const OUString sName(xInfo->getImplementationName());
73 uno::Reference<sheet::XSolver> xSolver(m_xContext->getServiceManager()->
74 createInstanceWithContext(sName, m_xContext), uno::UNO_QUERY_THROW);
75 testSolver(xSolver);
77 uno::Reference<sheet::XSolverDescription> xDesc(xSolver, uno::UNO_QUERY_THROW);
78 const OString sMessage("Empty description for " +
79 OUStringToOString(sName, RTL_TEXTENCODING_UTF8));
80 CPPUNIT_ASSERT_MESSAGE(sMessage.getStr(), !xDesc->getComponentDescription().isEmpty());
81 ++nCount;
83 sal_Int32 nExpected = 0;
84 #ifdef ENABLE_COINMP
85 ++nExpected;
86 #endif
87 #ifdef ENABLE_LPSOLVE
88 ++nExpected;
89 #endif
90 CPPUNIT_ASSERT_EQUAL(nExpected, nCount);
93 void LpSolverTest::testSolver(const uno::Reference<sheet::XSolver>& xSolver)
95 table::CellAddress aObjective(0, 0, 0);
97 // "changing cells" - unknown variables
98 uno::Sequence<table::CellAddress> aVariables(1);
99 aVariables[0] = table::CellAddress(0, 0, 0);
101 // constraints
102 uno::Sequence<sheet::SolverConstraint> aConstraints(1);
103 aConstraints[0].Left = table::CellAddress(0, 0, 0);
104 aConstraints[0].Operator = sheet::SolverConstraintOperator_LESS_EQUAL;
105 aConstraints[0].Right <<= 5.0;
107 // initialize solver
108 xSolver->setDocument( m_xDocument );
109 xSolver->setObjective( aObjective );
110 xSolver->setVariables( aVariables );
111 xSolver->setConstraints( aConstraints );
112 xSolver->setMaximize( true );
114 // test results
115 xSolver->solve();
116 CPPUNIT_ASSERT(xSolver->getSuccess());
117 uno::Sequence<double> aSolution = xSolver->getSolution();
118 CPPUNIT_ASSERT_EQUAL(aSolution.getLength(), aVariables.getLength());
119 CPPUNIT_ASSERT_EQUAL(aSolution[0], (double)5.0);
122 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest);
126 CPPUNIT_PLUGIN_IMPLEMENT();
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */