LCOV - code coverage report
Current view: top level - sccomp/qa/unit - lpsolver.cxx (source / functions) Hit Total Coverage
Test: commit 0e63ca4fde4e446f346e35849c756a30ca294aab Lines: 62 62 100.0 %
Date: 2014-04-11 Functions: 13 14 92.9 %
Legend: Lines: hit not hit

          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           2 :     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           4 :     while (xEnum->hasMoreElements())
      67             :     {
      68           2 :         uno::Reference<uno::XInterface> xIntFac;
      69           2 :         xEnum->nextElement() >>= xIntFac;
      70           2 :         CPPUNIT_ASSERT(xIntFac.is());
      71           4 :         uno::Reference<lang::XServiceInfo> xInfo(xIntFac, uno::UNO_QUERY_THROW);
      72           4 :         const OUString sName(xInfo->getImplementationName());
      73           4 :         uno::Reference<sheet::XSolver> xSolver(m_xContext->getServiceManager()->
      74           4 :                 createInstanceWithContext(sName, m_xContext), uno::UNO_QUERY_THROW);
      75           2 :         testSolver(xSolver);
      76             : 
      77           4 :         uno::Reference<sheet::XSolverDescription> xDesc(xSolver, uno::UNO_QUERY_THROW);
      78           4 :         const OString sMessage("Empty description for " +
      79           8 :                 OUStringToOString(sName, RTL_TEXTENCODING_UTF8));
      80           2 :         CPPUNIT_ASSERT_MESSAGE(sMessage.getStr(), !xDesc->getComponentDescription().isEmpty());
      81           2 :         ++nCount;
      82           2 :     }
      83           1 :     sal_Int32 nExpected = 0;
      84             : #ifdef ENABLE_COINMP
      85           1 :     ++nExpected;
      86             : #endif
      87             : #ifdef ENABLE_LPSOLVE
      88           1 :     ++nExpected;
      89             : #endif
      90           2 :     CPPUNIT_ASSERT_EQUAL(nExpected, nCount);
      91           1 : }
      92             : 
      93           2 : void LpSolverTest::testSolver(const uno::Reference<sheet::XSolver>& xSolver)
      94             : {
      95           2 :     table::CellAddress aObjective(0, 0, 0);
      96             : 
      97             :     // "changing cells" - unknown variables
      98           2 :     uno::Sequence<table::CellAddress> aVariables(1);
      99           2 :     aVariables[0] = table::CellAddress(0, 0, 0);
     100             : 
     101             :     // constraints
     102           4 :     uno::Sequence<sheet::SolverConstraint> aConstraints(1);
     103           2 :     aConstraints[0].Left = table::CellAddress(0, 0, 0);
     104           2 :     aConstraints[0].Operator = sheet::SolverConstraintOperator_LESS_EQUAL;
     105           2 :     aConstraints[0].Right <<= 5.0;
     106             : 
     107             :     // initialize solver
     108           2 :     xSolver->setDocument( m_xDocument );
     109           2 :     xSolver->setObjective( aObjective );
     110           2 :     xSolver->setVariables( aVariables );
     111           2 :     xSolver->setConstraints( aConstraints );
     112           2 :     xSolver->setMaximize( true );
     113             : 
     114             :     // test results
     115           2 :     xSolver->solve();
     116           2 :     CPPUNIT_ASSERT(xSolver->getSuccess());
     117           4 :     uno::Sequence<double> aSolution = xSolver->getSolution();
     118           2 :     CPPUNIT_ASSERT_EQUAL(aSolution.getLength(), aVariables.getLength());
     119           4 :     CPPUNIT_ASSERT_EQUAL(aSolution[0], (double)5.0);
     120           2 : }
     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: */

Generated by: LCOV version 1.10