Move the FormulaEvaluator code out of scratchpad
[poi.git] / src / testcases / org / apache / poi / hssf / record / formula / functions / TestMid.java
blobdc3d595aedd77e993c0caefa10d44c0440d78ff4
1 /* ====================================================================
2 Licensed to the Apache Software Foundation (ASF) under one or more
3 contributor license agreements. See the NOTICE file distributed with
4 this work for additional information regarding copyright ownership.
5 The ASF licenses this file to You under the Apache License, Version 2.0
6 (the "License"); you may not use this file except in compliance with
7 the License. You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
16 ==================================================================== */
18 package org.apache.poi.hssf.record.formula.functions;
20 import org.apache.poi.hssf.record.formula.AreaPtg;
21 import org.apache.poi.hssf.record.formula.ReferencePtg;
22 import org.apache.poi.hssf.record.formula.eval.Area2DEval;
23 import org.apache.poi.hssf.record.formula.eval.AreaEval;
24 import org.apache.poi.hssf.record.formula.eval.BlankEval;
25 import org.apache.poi.hssf.record.formula.eval.BoolEval;
26 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
27 import org.apache.poi.hssf.record.formula.eval.Eval;
28 import org.apache.poi.hssf.record.formula.eval.NumberEval;
29 import org.apache.poi.hssf.record.formula.eval.Ref2DEval;
30 import org.apache.poi.hssf.record.formula.eval.RefEval;
31 import org.apache.poi.hssf.record.formula.eval.StringEval;
32 import org.apache.poi.hssf.record.formula.eval.ValueEval;
34 import junit.framework.TestCase;
35 /**
36 * Tests for Excel function MID()
38 * @author Josh Micich
40 public final class TestMid extends TestCase {
43 private static Eval invokeMid(Eval text, Eval startPos, Eval numChars) {
44 Eval[] args = new Eval[] { text, startPos, numChars, };
45 return new Mid().evaluate(args, -1, (short)-1);
48 private void confirmMid(Eval text, Eval startPos, Eval numChars, String expected) {
49 Eval result = invokeMid(text, startPos, numChars);
50 assertEquals(StringEval.class, result.getClass());
51 assertEquals(expected, ((StringEval)result).getStringValue());
54 private void confirmMid(Eval text, Eval startPos, Eval numChars, ErrorEval expectedError) {
55 Eval result = invokeMid(text, startPos, numChars);
56 assertEquals(ErrorEval.class, result.getClass());
57 assertEquals(expectedError.getErrorCode(), ((ErrorEval)result).getErrorCode());
60 public void testBasic() {
62 confirmMid(new StringEval("galactic"), new NumberEval(3), new NumberEval(4), "lact");
65 /**
66 * Valid cases where args are not precisely (string, int, int) but can be resolved OK.
68 public void testUnusualArgs() {
69 // startPos with fractional digits
70 confirmMid(new StringEval("galactic"), new NumberEval(3.1), new NumberEval(4), "lact");
72 // string startPos
73 confirmMid(new StringEval("galactic"), new StringEval("3"), new NumberEval(4), "lact");
75 // text (first) arg type is number, other args are strings with fractional digits
76 confirmMid(new NumberEval(123456), new StringEval("3.1"), new StringEval("2.9"), "34");
78 // startPos is 1x1 area ref, numChars is cell ref
79 AreaEval aeStart = new Area2DEval(new AreaPtg("A1:A1"), new ValueEval[] { new NumberEval(2), } );
80 RefEval reNumChars = new Ref2DEval(new ReferencePtg("B1"), new NumberEval(3));
81 confirmMid(new StringEval("galactic"), aeStart, reNumChars, "ala");
83 confirmMid(new StringEval("galactic"), new NumberEval(3.1), BlankEval.INSTANCE, "");
85 confirmMid(new StringEval("galactic"), new NumberEval(3), BoolEval.FALSE, "");
86 confirmMid(new StringEval("galactic"), new NumberEval(3), BoolEval.TRUE, "l");
87 confirmMid(BlankEval.INSTANCE, new NumberEval(3), BoolEval.TRUE, "");
91 /**
92 * Extreme values for startPos and numChars
94 public void testExtremes() {
95 confirmMid(new StringEval("galactic"), new NumberEval(4), new NumberEval(400), "actic");
97 confirmMid(new StringEval("galactic"), new NumberEval(30), new NumberEval(4), "");
98 confirmMid(new StringEval("galactic"), new NumberEval(3), new NumberEval(0), "");
102 * All sorts of ways to make MID return defined errors.
104 public void testErrors() {
105 confirmMid(ErrorEval.NAME_INVALID, new NumberEval(3), new NumberEval(4), ErrorEval.NAME_INVALID);
106 confirmMid(new StringEval("galactic"), ErrorEval.NAME_INVALID, new NumberEval(4), ErrorEval.NAME_INVALID);
107 confirmMid(new StringEval("galactic"), new NumberEval(3), ErrorEval.NAME_INVALID, ErrorEval.NAME_INVALID);
108 confirmMid(new StringEval("galactic"), ErrorEval.DIV_ZERO, ErrorEval.NAME_INVALID, ErrorEval.DIV_ZERO);
110 confirmMid(new StringEval("galactic"), BlankEval.INSTANCE, new NumberEval(3.1), ErrorEval.VALUE_INVALID);
112 confirmMid(new StringEval("galactic"), new NumberEval(0), new NumberEval(4), ErrorEval.VALUE_INVALID);
113 confirmMid(new StringEval("galactic"), new NumberEval(1), new NumberEval(-1), ErrorEval.VALUE_INVALID);