/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ #include #include "helper/qahelper.hxx" #include #include #include #include using namespace sc; class SolverTest : public ScModelTestBase { public: SolverTest() : ScModelTestBase("sc/qa/unit/data") { } std::vector CreateConstraintsModelA(); void TestConstraintsModelA(SolverSettings* pSettings); }; // Creates a simple set of constraints for testing std::vector SolverTest::CreateConstraintsModelA() { std::vector aConstraints; ModelConstraint aConstr1; aConstr1.aLeftStr = "C1:C10"; aConstr1.nOperator = CO_LESS_EQUAL; aConstr1.aRightStr = "100"; aConstraints.push_back(aConstr1); ModelConstraint aConstr2; aConstr2.aLeftStr = "F5"; aConstr2.nOperator = CO_EQUAL; aConstr2.aRightStr = "500"; aConstraints.push_back(aConstr2); ModelConstraint aConstr3; aConstr3.aLeftStr = "D1:D5"; aConstr3.nOperator = CO_BINARY; aConstr3.aRightStr = ""; aConstraints.push_back(aConstr3); return aConstraints; } // Tests the contents of the three constraints void SolverTest::TestConstraintsModelA(SolverSettings* pSettings) { std::vector aConstraints = pSettings->GetConstraints(); CPPUNIT_ASSERT_EQUAL(OUString("C1:C10"), aConstraints[0].aLeftStr); CPPUNIT_ASSERT_EQUAL(CO_LESS_EQUAL, aConstraints[0].nOperator); CPPUNIT_ASSERT_EQUAL(OUString("100"), aConstraints[0].aRightStr); CPPUNIT_ASSERT_EQUAL(OUString("F5"), aConstraints[1].aLeftStr); CPPUNIT_ASSERT_EQUAL(CO_EQUAL, aConstraints[1].nOperator); CPPUNIT_ASSERT_EQUAL(OUString("500"), aConstraints[1].aRightStr); CPPUNIT_ASSERT_EQUAL(OUString("D1:D5"), aConstraints[2].aLeftStr); CPPUNIT_ASSERT_EQUAL(CO_BINARY, aConstraints[2].nOperator); CPPUNIT_ASSERT_EQUAL(OUString(""), aConstraints[2].aRightStr); } /* This test creates a model in a single tab and test if the model info * is correctly stored in the object */ CPPUNIT_TEST_FIXTURE(SolverTest, testSingleModel) { createScDoc(); ScDocument* pDoc = getScDoc(); ScTable* pTable = pDoc->FetchTable(0); std::shared_ptr pSettings = pTable->GetSolverSettings(); CPPUNIT_ASSERT(pSettings); // Test solver default settings on an empty tab // Here we only test default settings that are not engine-dependent CPPUNIT_ASSERT_EQUAL(OUString(""), pSettings->GetParameter(SP_OBJ_CELL)); CPPUNIT_ASSERT_EQUAL(static_cast(OT_MAXIMIZE), pSettings->GetParameter(SP_OBJ_TYPE).toInt32()); CPPUNIT_ASSERT_EQUAL(OUString(""), pSettings->GetParameter(SP_OBJ_VAL)); CPPUNIT_ASSERT_EQUAL(OUString(""), pSettings->GetParameter(SP_VAR_CELLS)); CPPUNIT_ASSERT_EQUAL(sal_Int32(0), pSettings->GetParameter(SP_CONSTR_COUNT).toInt32()); // Create a simple model pSettings->SetParameter(SP_OBJ_CELL, OUString("A1")); pSettings->SetParameter(SP_OBJ_TYPE, OUString::number(OT_MINIMIZE)); pSettings->SetParameter(SP_OBJ_VAL, OUString::number(0)); pSettings->SetParameter(SP_VAR_CELLS, OUString("D1:D5")); std::vector aConstraints = CreateConstraintsModelA(); pSettings->SetConstraints(aConstraints); // Test if the model parameters were set CPPUNIT_ASSERT_EQUAL(OUString("A1"), pSettings->GetParameter(SP_OBJ_CELL)); CPPUNIT_ASSERT_EQUAL(static_cast(OT_MINIMIZE), pSettings->GetParameter(SP_OBJ_TYPE).toInt32()); CPPUNIT_ASSERT_EQUAL(OUString("0"), pSettings->GetParameter(SP_OBJ_VAL)); CPPUNIT_ASSERT_EQUAL(OUString("D1:D5"), pSettings->GetParameter(SP_VAR_CELLS)); // Test if the constraints were correctly set before saving CPPUNIT_ASSERT_EQUAL(sal_Int32(3), pSettings->GetParameter(SP_CONSTR_COUNT).toInt32()); TestConstraintsModelA(pSettings.get()); // Save and reload the file pSettings->SaveSolverSettings(); saveAndReload("calc8"); pDoc = getScDoc(); pTable = pDoc->FetchTable(0); pSettings = pTable->GetSolverSettings(); CPPUNIT_ASSERT(pSettings); // Test if the model parameters remain set in the file CPPUNIT_ASSERT_EQUAL(OUString("A1"), pSettings->GetParameter(SP_OBJ_CELL)); CPPUNIT_ASSERT_EQUAL(static_cast(OT_MINIMIZE), pSettings->GetParameter(SP_OBJ_TYPE).toInt32()); CPPUNIT_ASSERT_EQUAL(OUString("0"), pSettings->GetParameter(SP_OBJ_VAL)); CPPUNIT_ASSERT_EQUAL(OUString("D1:D5"), pSettings->GetParameter(SP_VAR_CELLS)); // Test if the constraints remain correct after saving CPPUNIT_ASSERT_EQUAL(sal_Int32(3), pSettings->GetParameter(SP_CONSTR_COUNT).toInt32()); TestConstraintsModelA(pSettings.get()); } // Tests if references remain valid after a sheet is renamed CPPUNIT_TEST_FIXTURE(SolverTest, tdf156815) { createScDoc("ods/tdf156815.ods"); ScDocument* pDoc = getScDoc(); ScTable* pTable = pDoc->FetchTable(0); std::shared_ptr pSettings = pTable->GetSolverSettings(); CPPUNIT_ASSERT(pSettings); // Check current values in the solver model CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$1"), pSettings->GetParameter(SP_OBJ_CELL)); CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$3:$B$3"), pSettings->GetParameter(SP_VAR_CELLS)); std::vector aConstraints = pSettings->GetConstraints(); CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$A$2"), aConstraints[0].aLeftStr); CPPUNIT_ASSERT_EQUAL(OUString("$Sheet2.$B$2"), aConstraints[0].aRightStr); // Rename Sheet2 to NewName ScDocFunc& rDocFunc = getScDocShell()->GetDocFunc(); rDocFunc.RenameTable(1, "NewName", false, true); // Check whether the ranges where updated pSettings = pTable->GetSolverSettings(); CPPUNIT_ASSERT(pSettings); CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$1"), pSettings->GetParameter(SP_OBJ_CELL)); CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$3:$B$3"), pSettings->GetParameter(SP_VAR_CELLS)); aConstraints = pSettings->GetConstraints(); CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$A$2"), aConstraints[0].aLeftStr); CPPUNIT_ASSERT_EQUAL(OUString("$NewName.$B$2"), aConstraints[0].aRightStr); } CPPUNIT_PLUGIN_IMPLEMENT();