(real) fix for bug #44691. Modified Pmt.java to allow for 3 arguments. Added TestPm...
[poi.git] / src / scratchpad / testcases / org / apache / poi / hssf / record / formula / functions / TestPmt.java
blob935615acaed239f55feafe1cb91b3b0e8220b79f
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 junit.framework.AssertionFailedError;
21 import junit.framework.TestCase;
23 import org.apache.poi.hssf.record.formula.eval.ErrorEval;
24 import org.apache.poi.hssf.record.formula.eval.Eval;
25 import org.apache.poi.hssf.record.formula.eval.NumberEval;
26 import org.apache.poi.hssf.usermodel.HSSFErrorConstants;
28 /**
30 * @author Josh Micich
32 public final class TestPmt extends TestCase {
34 private static void confirm(double expected, NumberEval ne) {
35 // only asserting accuracy to 4 fractional digits
36 assertEquals(expected, ne.getNumberValue(), 0.00005);
38 private static Eval invoke(Eval[] args) {
39 return new Pmt().evaluate(args, -1, (short)-1);
41 /**
42 * Invocation when not expecting an error result
44 private static NumberEval invokeNormal(Eval[] args) {
45 Eval ev = invoke(args);
46 if(ev instanceof ErrorEval) {
47 throw new AssertionFailedError("Normal evaluation failed with error code: "
48 + ev.toString());
50 return (NumberEval) ev;
53 private static void confirm(double expected, double rate, double nper, double pv, double fv, boolean isBeginning) {
54 Eval[] args = {
55 new NumberEval(rate),
56 new NumberEval(nper),
57 new NumberEval(pv),
58 new NumberEval(fv),
59 new NumberEval(isBeginning ? 1 : 0),
61 confirm(expected, invokeNormal(args));
65 public void testBasic() {
66 confirm(-1037.0321, (0.08/12), 10, 10000, 0, false);
67 confirm(-1030.1643, (0.08/12), 10, 10000, 0, true);
70 public void test3args() {
72 Eval[] args = {
73 new NumberEval(0.005),
74 new NumberEval(24),
75 new NumberEval(1000),
77 Eval ev = invoke(args);
78 if(ev instanceof ErrorEval) {
79 ErrorEval err = (ErrorEval) ev;
80 if(err.getErrorCode() == HSSFErrorConstants.ERROR_VALUE) {
81 throw new AssertionFailedError("Identified bug 44691");
85 confirm(-44.3206, invokeNormal(args));