Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include <sal/config.h>
11 :
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>
18 :
19 : #include <address.hxx>
20 :
21 : using namespace css;
22 :
23 : namespace {
24 :
25 3 : class LpSolverTest: public test::BootstrapFixture
26 : {
27 : uno::Reference<sheet::XSpreadsheetDocument> m_xDocument;
28 :
29 : void test();
30 : void testSolver(const uno::Reference<sheet::XSolver>& xSolver);
31 :
32 : public:
33 : virtual void setUp() SAL_OVERRIDE;
34 : virtual void tearDown() SAL_OVERRIDE;
35 :
36 2 : CPPUNIT_TEST_SUITE(LpSolverTest);
37 1 : CPPUNIT_TEST(test);
38 5 : CPPUNIT_TEST_SUITE_END();
39 : };
40 :
41 1 : void LpSolverTest::setUp()
42 : {
43 1 : test::BootstrapFixture::setUp();
44 1 : uno::Reference<frame::XDesktop2> xComponentLoader = frame::Desktop::create(m_xContext);
45 1 : uno::Reference<lang::XComponent> xComponent(xComponentLoader->loadComponentFromURL(
46 : "private:factory/scalc", "_blank", 0,
47 2 : uno::Sequence < ::com::sun::star::beans::PropertyValue >()));
48 2 : m_xDocument.set(xComponent, uno::UNO_QUERY_THROW);
49 1 : }
50 :
51 1 : void LpSolverTest::tearDown()
52 : {
53 1 : uno::Reference<lang::XComponent>(m_xDocument, uno::UNO_QUERY_THROW)->dispose();
54 1 : test::BootstrapFixture::tearDown();
55 1 : }
56 :
57 1 : void LpSolverTest::test()
58 : {
59 : uno::Reference<container::XContentEnumerationAccess> xEnAc(
60 1 : m_xContext->getServiceManager(), uno::UNO_QUERY_THROW);
61 1 : uno::Reference<container::XEnumeration> xEnum = xEnAc->
62 2 : createContentEnumeration( "com.sun.star.sheet.Solver" );
63 1 : CPPUNIT_ASSERT(xEnum.is());
64 :
65 1 : sal_Int32 nCount = 0;
66 2 : while (xEnum->hasMoreElements())
67 : {
68 1 : uno::Reference<uno::XInterface> xIntFac;
69 1 : xEnum->nextElement() >>= xIntFac;
70 1 : CPPUNIT_ASSERT(xIntFac.is());
71 2 : uno::Reference<lang::XServiceInfo> xInfo(xIntFac, uno::UNO_QUERY_THROW);
72 2 : const OUString sName(xInfo->getImplementationName());
73 2 : uno::Reference<sheet::XSolver> xSolver(m_xContext->getServiceManager()->
74 1 : createInstanceWithContext(sName, m_xContext), uno::UNO_QUERY_THROW);
75 0 : testSolver(xSolver);
76 :
77 0 : uno::Reference<sheet::XSolverDescription> xDesc(xSolver, uno::UNO_QUERY_THROW);
78 0 : const OString sMessage("Empty description for " +
79 0 : OUStringToOString(sName, RTL_TEXTENCODING_UTF8));
80 0 : CPPUNIT_ASSERT_MESSAGE(sMessage.getStr(), !xDesc->getComponentDescription().isEmpty());
81 0 : ++nCount;
82 1 : }
83 0 : sal_Int32 nExpected = 0;
84 : #ifdef ENABLE_COINMP
85 0 : ++nExpected;
86 : #endif
87 : #ifdef ENABLE_LPSOLVE
88 0 : ++nExpected;
89 : #endif
90 1 : CPPUNIT_ASSERT_EQUAL(nExpected, nCount);
91 0 : }
92 :
93 0 : void LpSolverTest::testSolver(const uno::Reference<sheet::XSolver>& xSolver)
94 : {
95 0 : table::CellAddress aObjective(0, 0, 0);
96 :
97 : // "changing cells" - unknown variables
98 0 : uno::Sequence<table::CellAddress> aVariables(1);
99 0 : aVariables[0] = table::CellAddress(0, 0, 0);
100 :
101 : // constraints
102 0 : uno::Sequence<sheet::SolverConstraint> aConstraints(1);
103 0 : aConstraints[0].Left = table::CellAddress(0, 0, 0);
104 0 : aConstraints[0].Operator = sheet::SolverConstraintOperator_LESS_EQUAL;
105 0 : aConstraints[0].Right <<= 5.0;
106 :
107 : // initialize solver
108 0 : xSolver->setDocument( m_xDocument );
109 0 : xSolver->setObjective( aObjective );
110 0 : xSolver->setVariables( aVariables );
111 0 : xSolver->setConstraints( aConstraints );
112 0 : xSolver->setMaximize( true );
113 :
114 : // test results
115 0 : xSolver->solve();
116 0 : CPPUNIT_ASSERT(xSolver->getSuccess());
117 0 : uno::Sequence<double> aSolution = xSolver->getSolution();
118 0 : CPPUNIT_ASSERT_EQUAL(aSolution.getLength(), aVariables.getLength());
119 0 : CPPUNIT_ASSERT_EQUAL(aSolution[0], (double)5.0);
120 0 : }
121 :
122 1 : CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest);
123 :
124 : }
125 :
126 4 : CPPUNIT_PLUGIN_IMPLEMENT();
127 :
128 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|