xref: /AOO41X/main/qadevOOo/tests/java/ifc/sheet/_XGoalSeek.java (revision ef39d40d3f5e66cf3f035b3e93783012b340500d)
1*ef39d40dSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*ef39d40dSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*ef39d40dSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*ef39d40dSAndrew Rist  * distributed with this work for additional information
6*ef39d40dSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*ef39d40dSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*ef39d40dSAndrew Rist  * "License"); you may not use this file except in compliance
9*ef39d40dSAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*ef39d40dSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*ef39d40dSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*ef39d40dSAndrew Rist  * software distributed under the License is distributed on an
15*ef39d40dSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*ef39d40dSAndrew Rist  * KIND, either express or implied.  See the License for the
17*ef39d40dSAndrew Rist  * specific language governing permissions and limitations
18*ef39d40dSAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*ef39d40dSAndrew Rist  *************************************************************/
21*ef39d40dSAndrew Rist 
22*ef39d40dSAndrew Rist 
23cdf0e10cSrcweir package ifc.sheet;
24cdf0e10cSrcweir 
25cdf0e10cSrcweir import com.sun.star.container.XIndexAccess;
26cdf0e10cSrcweir import com.sun.star.sheet.GoalResult;
27cdf0e10cSrcweir import com.sun.star.sheet.XGoalSeek;
28cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheet;
29cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheetDocument;
30cdf0e10cSrcweir import com.sun.star.sheet.XSpreadsheets;
31cdf0e10cSrcweir import com.sun.star.table.CellAddress;
32cdf0e10cSrcweir import com.sun.star.uno.UnoRuntime;
33cdf0e10cSrcweir import lib.MultiMethodTest;
34cdf0e10cSrcweir import lib.StatusException;
35cdf0e10cSrcweir 
36cdf0e10cSrcweir /**
37cdf0e10cSrcweir  *
38cdf0e10cSrcweir  */
39cdf0e10cSrcweir public class _XGoalSeek extends MultiMethodTest {
40cdf0e10cSrcweir     public XGoalSeek oObj = null;
41cdf0e10cSrcweir     XSpreadsheet xSheet = null;
42cdf0e10cSrcweir     CellAddress aFormula = null;
43cdf0e10cSrcweir     CellAddress aValue = null;
44cdf0e10cSrcweir 
before()45cdf0e10cSrcweir     public void before() {
46cdf0e10cSrcweir 		Exception ex = null;
47cdf0e10cSrcweir         // get two sheets
48cdf0e10cSrcweir         try {
49cdf0e10cSrcweir             XSpreadsheetDocument xSpreadsheetDocument = (XSpreadsheetDocument)
50cdf0e10cSrcweir                     UnoRuntime.queryInterface(XSpreadsheetDocument.class, oObj);
51cdf0e10cSrcweir             XSpreadsheets oSheets = xSpreadsheetDocument.getSheets();
52cdf0e10cSrcweir             XIndexAccess oIndexSheets = (XIndexAccess) UnoRuntime.queryInterface(
53cdf0e10cSrcweir                                                 XIndexAccess.class, oSheets);
54cdf0e10cSrcweir             xSheet = (XSpreadsheet) UnoRuntime.queryInterface(
55cdf0e10cSrcweir                                       XSpreadsheet.class, oIndexSheets.getByIndex(1));
56cdf0e10cSrcweir         }
57cdf0e10cSrcweir         catch(com.sun.star.lang.IndexOutOfBoundsException e) {
58cdf0e10cSrcweir             ex = e;
59cdf0e10cSrcweir         }
60cdf0e10cSrcweir         catch(com.sun.star.lang.WrappedTargetException e) {
61cdf0e10cSrcweir             ex = e;
62cdf0e10cSrcweir         }
63cdf0e10cSrcweir         catch(java.lang.NullPointerException e) {
64cdf0e10cSrcweir             ex = e;
65cdf0e10cSrcweir         }
66cdf0e10cSrcweir         if (ex != null) {
67cdf0e10cSrcweir             throw new StatusException("Could not get a sheet.", ex);
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         // set value and formula
71cdf0e10cSrcweir         try {
72cdf0e10cSrcweir             xSheet.getCellByPosition(3, 4).setValue(9);
73cdf0e10cSrcweir             xSheet.getCellByPosition(3, 5).setFormula("= SQRT(D5)");
74cdf0e10cSrcweir             aValue = new CellAddress((short)1, 3, 4);
75cdf0e10cSrcweir             aFormula = new CellAddress((short)1, 3, 5);
76cdf0e10cSrcweir         }
77cdf0e10cSrcweir         catch(Exception e) {
78cdf0e10cSrcweir             throw new StatusException("Could not get set formulas on the sheet.", e);
79cdf0e10cSrcweir         }
80cdf0e10cSrcweir     }
81cdf0e10cSrcweir 
_seekGoal()82cdf0e10cSrcweir     public void _seekGoal() {
83cdf0e10cSrcweir         boolean result = true;
84cdf0e10cSrcweir         double divergence = 0.01;
85cdf0e10cSrcweir         GoalResult goal = oObj.seekGoal(aFormula, aValue, "4");
86cdf0e10cSrcweir         log.println("Goal Result: " + goal.Result + "   Divergence: " + goal.Divergence);
87cdf0e10cSrcweir         result &= goal.Divergence < divergence;
88cdf0e10cSrcweir         result &= goal.Result > 16 - divergence || goal.Result < 16 + divergence;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir         goal = oObj.seekGoal(aFormula, aValue, "-4");
91cdf0e10cSrcweir         log.println("Goal Result: " + goal.Result + "   Divergence: " + goal.Divergence);
92cdf0e10cSrcweir         result &= goal.Divergence > 1/divergence;
93cdf0e10cSrcweir         result &= goal.Result < divergence || goal.Result > -divergence;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // just curious: let goal seek find a limiting value
96cdf0e10cSrcweir         try {
97cdf0e10cSrcweir             xSheet.getCellByPosition(3, 4).setValue(0.8);
98cdf0e10cSrcweir             xSheet.getCellByPosition(3, 5).setFormula("= (D5 ^ 2 - 1) / (D5 - 1)");
99cdf0e10cSrcweir         }
100cdf0e10cSrcweir         catch(Exception e) {}
101cdf0e10cSrcweir         goal = oObj.seekGoal(aFormula, aValue, "2");
102cdf0e10cSrcweir         log.println("Goal Result: " + goal.Result + "   Divergence: " + goal.Divergence);
103cdf0e10cSrcweir         result &= goal.Divergence < divergence;
104cdf0e10cSrcweir         result &= goal.Result > 16 - divergence || goal.Result < 16 + divergence;
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         tRes.tested("seekGoal()", result);
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir }
109