tdf#130857 qt weld: Implement QtInstanceWidget::get_text_height
[LibreOffice.git] / sccomp / qa / unit / solver.cxx
blob536df7425bb96079c6cb9db00b480c83d7799efd
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/frame/Desktop.hpp>
13 #include <com/sun/star/sheet/XSolver.hpp>
14 #include <com/sun/star/sheet/XSolverDescription.hpp>
15 #include <test/bootstrapfixture.hxx>
17 using namespace css;
19 namespace {
21 class LpSolverTest: public test::BootstrapFixture
23 uno::Reference<sheet::XSpreadsheetDocument> m_xDocument;
25 #ifdef ENABLE_LPSOLVE
26 void testLpSolver();
27 #endif
28 #ifdef ENABLE_COINMP
29 void testCoinMPSolver();
30 #endif
32 #if defined(ENABLE_LPSOLVE) || defined(ENABLE_COINMP)
33 void testSolver(OUString const & rName);
34 #endif
36 public:
37 virtual void setUp() override;
38 virtual void tearDown() override;
40 CPPUNIT_TEST_SUITE(LpSolverTest);
41 #ifdef ENABLE_LPSOLVE
42 CPPUNIT_TEST(testLpSolver);
43 #endif
44 #ifdef ENABLE_COINMP
45 CPPUNIT_TEST(testCoinMPSolver);
46 #endif
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();
63 m_xDocument.clear();
64 test::BootstrapFixture::tearDown();
67 #ifdef ENABLE_LPSOLVE
68 void LpSolverTest::testLpSolver()
70 testSolver(u"com.sun.star.comp.Calc.LpsolveSolver"_ustr);
72 #endif
74 #ifdef ENABLE_COINMP
75 void LpSolverTest::testCoinMPSolver()
77 testSolver(u"com.sun.star.comp.Calc.CoinMPSolver"_ustr);
79 #endif
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 } };
92 // constraints
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) }
99 // initialize solver
100 xSolver->setDocument( m_xDocument );
101 xSolver->setObjective( aObjective );
102 xSolver->setVariables( aVariables );
103 xSolver->setConstraints( aConstraints );
104 xSolver->setMaximize( true );
106 // test results
107 xSolver->solve();
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());
117 #endif
119 CPPUNIT_TEST_SUITE_REGISTRATION(LpSolverTest);
123 CPPUNIT_PLUGIN_IMPLEMENT();
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */