001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /*
018: * Created on May 23, 2005
019: *
020: */
021: package org.apache.poi.hssf.record.formula.functions;
022:
023: /**
024: * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
025: *
026: */
027: public class TestFinanceLib extends AbstractNumericTestCase {
028:
029: public void testFv() {
030: double f, r, y, p, x;
031: int n;
032: boolean t = false;
033:
034: r = 0;
035: n = 3;
036: y = 2;
037: p = 7;
038: t = true;
039: f = FinanceLib.fv(r, n, y, p, t);
040: x = -13;
041: assertEquals("fv ", x, f);
042:
043: r = 1;
044: n = 10;
045: y = 100;
046: p = 10000;
047: t = false;
048: f = FinanceLib.fv(r, n, y, p, t);
049: x = -10342300;
050: assertEquals("fv ", x, f);
051:
052: r = 1;
053: n = 10;
054: y = 100;
055: p = 10000;
056: t = true;
057: f = FinanceLib.fv(r, n, y, p, t);
058: x = -10444600;
059: assertEquals("fv ", x, f);
060:
061: r = 2;
062: n = 12;
063: y = 120;
064: p = 12000;
065: t = false;
066: f = FinanceLib.fv(r, n, y, p, t);
067: x = -6409178400d;
068: assertEquals("fv ", x, f);
069:
070: r = 2;
071: n = 12;
072: y = 120;
073: p = 12000;
074: t = true;
075: f = FinanceLib.fv(r, n, y, p, t);
076: x = -6472951200d;
077: assertEquals("fv ", x, f);
078:
079: // cross tests with pv
080: r = 2.95;
081: n = 13;
082: y = 13000;
083: p = -4406.78544294496;
084: t = false;
085: f = FinanceLib.fv(r, n, y, p, t);
086: x = 333891.230010986; // as returned by excel
087: assertEquals("fv ", x, f);
088:
089: r = 2.95;
090: n = 13;
091: y = 13000;
092: p = -17406.7852148156;
093: t = true;
094: f = FinanceLib.fv(r, n, y, p, t);
095: x = 333891.230102539; // as returned by excel
096: assertEquals("fv ", x, f);
097:
098: }
099:
100: public void testNpv() {
101: double r, v[], npv, x;
102:
103: r = 1;
104: v = new double[] { 100, 200, 300, 400 };
105: npv = FinanceLib.npv(r, v);
106: x = 162.5;
107: assertEquals("npv ", x, npv);
108:
109: r = 2.5;
110: v = new double[] { 1000, 666.66666, 333.33, 12.2768416 };
111: npv = FinanceLib.npv(r, v);
112: x = 347.99232604144827;
113: assertEquals("npv ", x, npv);
114:
115: r = 12.33333;
116: v = new double[] { 1000, 0, -900, -7777.5765 };
117: npv = FinanceLib.npv(r, v);
118: x = 74.3742433377061;
119: assertEquals("npv ", x, npv);
120:
121: r = 0.05;
122: v = new double[] { 200000, 300000.55, 400000, 1000000, 6000000,
123: 7000000, -300000 };
124: npv = FinanceLib.npv(r, v);
125: x = 11342283.4233124;
126: assertEquals("npv ", x, npv);
127: }
128:
129: public void testPmt() {
130: double f, r, y, p, x;
131: int n;
132: boolean t = false;
133:
134: r = 0;
135: n = 3;
136: p = 2;
137: f = 7;
138: t = true;
139: y = FinanceLib.pmt(r, n, p, f, t);
140: x = -3;
141: assertEquals("pmt ", x, y);
142:
143: // cross check with pv
144: r = 1;
145: n = 10;
146: p = -109.66796875;
147: f = 10000;
148: t = false;
149: y = FinanceLib.pmt(r, n, p, f, t);
150: x = 100;
151: assertEquals("pmt ", x, y);
152:
153: r = 1;
154: n = 10;
155: p = -209.5703125;
156: f = 10000;
157: t = true;
158: y = FinanceLib.pmt(r, n, p, f, t);
159: x = 100;
160: assertEquals("pmt ", x, y);
161:
162: // cross check with fv
163: r = 2;
164: n = 12;
165: f = -6409178400d;
166: p = 12000;
167: t = false;
168: y = FinanceLib.pmt(r, n, p, f, t);
169: x = 120;
170: assertEquals("pmt ", x, y);
171:
172: r = 2;
173: n = 12;
174: f = -6472951200d;
175: p = 12000;
176: t = true;
177: y = FinanceLib.pmt(r, n, p, f, t);
178: x = 120;
179: assertEquals("pmt ", x, y);
180: }
181:
182: public void testPv() {
183: double f, r, y, p, x;
184: int n;
185: boolean t = false;
186:
187: r = 0;
188: n = 3;
189: y = 2;
190: f = 7;
191: t = true;
192: f = FinanceLib.pv(r, n, y, f, t);
193: x = -13;
194: assertEquals("pv ", x, f);
195:
196: r = 1;
197: n = 10;
198: y = 100;
199: f = 10000;
200: t = false;
201: p = FinanceLib.pv(r, n, y, f, t);
202: x = -109.66796875;
203: assertEquals("pv ", x, p);
204:
205: r = 1;
206: n = 10;
207: y = 100;
208: f = 10000;
209: t = true;
210: p = FinanceLib.pv(r, n, y, f, t);
211: x = -209.5703125;
212: assertEquals("pv ", x, p);
213:
214: r = 2.95;
215: n = 13;
216: y = 13000;
217: f = 333891.23;
218: t = false;
219: p = FinanceLib.pv(r, n, y, f, t);
220: x = -4406.78544294496;
221: assertEquals("pv ", x, p);
222:
223: r = 2.95;
224: n = 13;
225: y = 13000;
226: f = 333891.23;
227: t = true;
228: p = FinanceLib.pv(r, n, y, f, t);
229: x = -17406.7852148156;
230: assertEquals("pv ", x, p);
231:
232: // cross tests with fv
233: r = 2;
234: n = 12;
235: y = 120;
236: f = -6409178400d;
237: t = false;
238: p = FinanceLib.pv(r, n, y, f, t);
239: x = 12000;
240: assertEquals("pv ", x, p);
241:
242: r = 2;
243: n = 12;
244: y = 120;
245: f = -6472951200d;
246: t = true;
247: p = FinanceLib.pv(r, n, y, f, t);
248: x = 12000;
249: assertEquals("pv ", x, p);
250:
251: }
252:
253: public void testNper() {
254: double f, r, y, p, x, n;
255: boolean t = false;
256:
257: r = 0;
258: y = 7;
259: p = 2;
260: f = 3;
261: t = false;
262: n = FinanceLib.nper(r, y, p, f, t);
263: x = -0.71428571429; // can you believe it? excel returns nper as a fraction!??
264: assertEquals("nper ", x, n);
265:
266: // cross check with pv
267: r = 1;
268: y = 100;
269: p = -109.66796875;
270: f = 10000;
271: t = false;
272: n = FinanceLib.nper(r, y, p, f, t);
273: x = 10;
274: assertEquals("nper ", x, n);
275:
276: r = 1;
277: y = 100;
278: p = -209.5703125;
279: f = 10000;
280: t = true;
281: n = FinanceLib.nper(r, y, p, f, t);
282: x = 10;
283: assertEquals("nper ", x, n);
284:
285: // cross check with fv
286: r = 2;
287: y = 120;
288: f = -6409178400d;
289: p = 12000;
290: t = false;
291: n = FinanceLib.nper(r, y, p, f, t);
292: x = 12;
293: assertEquals("nper ", x, n);
294:
295: r = 2;
296: y = 120;
297: f = -6472951200d;
298: p = 12000;
299: t = true;
300: n = FinanceLib.nper(r, y, p, f, t);
301: x = 12;
302: assertEquals("nper ", x, n);
303: }
304: }
|