1 // FinancialTest.cs - NUnit Test Cases for vb module Financial
\r
2 // (class Microsoft.VisualBasic.Financial)
\r
4 // Rob Tillie (Rob@flep-tech.nl)
\r
6 // (C) 2004 Rob Tillie
\r
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using NUnit.Framework;
\r
34 using Microsoft.VisualBasic;
\r
36 namespace MonoTests.Microsoft.VisualBasic
\r
40 public class FinancialTest : Assertion {
\r
43 public void GetReady() {}
\r
46 public void Clean() {}
\r
51 [ExpectedException(typeof(ArgumentException))]
\r
52 public void TestDDBArg1()
\r
54 Financial.DDB (-1, 1, 1, 1, 1);
\r
58 [ExpectedException(typeof(ArgumentException))]
\r
59 public void TestDDBArg2()
\r
61 Financial.DDB (1, -1, 1, 1, 1);
\r
65 [ExpectedException(typeof(ArgumentException))]
\r
66 public void TestDDBArg3()
\r
68 Financial.DDB (1, 1, 0, 1, 1);
\r
72 [ExpectedException(typeof(ArgumentException))]
\r
73 public void TestDDBArg4()
\r
75 Financial.DDB (1, 1, 1, 1, 0);
\r
79 [ExpectedException(typeof(ArgumentException))]
\r
80 public void TestDDBArg5()
\r
82 // Period has to be > Life
\r
83 Financial.DDB (1, 1, 1, 2, 1);
\r
87 public void TestDDB()
\r
89 double ddb = Financial.DDB (1000, 50, 10, 5, 3);
\r
90 AssertEquals ("#DDB01", 1425, ddb, 0);
\r
92 // TODO: How should we test an optional parameter in C#?
\r
93 ddb = Financial.DDB (1000, 50, 10, 5, 2);
\r
94 AssertEquals ("#DDB02", 950, ddb, 0);
\r
98 public void TestFV()
\r
100 double d = Financial.FV (10, 5, 3, 7, DueDate.BegOfPeriod);
\r
101 AssertEquals ("#FV01", -1658822, d);
\r
103 d = Financial.FV (10, 5, 3, 7, DueDate.EndOfPeriod);
\r
104 AssertEquals ("#FV02", -1175672, d);
\r
106 d = Financial.FV (0, 5, 3, 7, DueDate.BegOfPeriod);
\r
107 AssertEquals ("#FV03", -22, d);
\r
109 d = Financial.FV(0, 1, 1, 1, DueDate.BegOfPeriod);
\r
110 AssertEquals ("#FV04", -2, d);
\r
112 d = Financial.FV (0, 0, 0, 0, DueDate.BegOfPeriod);
\r
113 AssertEquals ("#FV05", 0, d);
\r
115 d = Financial.FV (-3, -5, -6, -4, DueDate.BegOfPeriod);
\r
116 AssertEquals ("#FV06", -4.25, d);
\r
120 [ExpectedException(typeof(ArgumentException))]
\r
121 public void TestIPmtArgs1()
\r
123 Financial.IPmt (3, 6, 4, 2, 2, DueDate.BegOfPeriod);
\r
127 [ExpectedException(typeof(ArgumentException))]
\r
128 public void TestIPmtArgs2()
\r
130 Financial.IPmt (3, 0, 4, 2, 2, DueDate.BegOfPeriod);
\r
134 public void TestIPmt()
\r
136 double d = Financial.IPmt (10, 2, 3, 7, 9, DueDate.BegOfPeriod);
\r
137 AssertEquals ("#IPmt01", -6.25427204374573, d);
\r
139 d = Financial.IPmt (10, 4, 4, 7, 4, DueDate.EndOfPeriod);
\r
140 AssertEquals ("#IPmt02", -60.0068306011053, d);
\r
142 d = Financial.IPmt (0, 5, 7, 7, 2, DueDate.BegOfPeriod);
\r
143 AssertEquals ("#IPmt03", 0, d);
\r
145 d = Financial.IPmt (-5, 5, 7, -7, -2, DueDate.BegOfPeriod);
\r
146 AssertEquals ("#IPmt04", 8.92508391821792, d);
\r
150 [ExpectedException(typeof(ArgumentException))]
\r
151 public void TestPmtArgs()
\r
153 Financial.Pmt (1, 0, 1, 1, DueDate.BegOfPeriod);
\r
157 public void TestPmt()
\r
159 double d = Financial.Pmt (2, 5, 2, 3, DueDate.BegOfPeriod);
\r
160 AssertEquals ("#Pmt01", -1.34710743801653, d);
\r
162 d = Financial.Pmt (2, 5, 2, 3, DueDate.EndOfPeriod);
\r
163 AssertEquals ("#Pmt02", -4.04132231404959, d);
\r
165 d = Financial.Pmt (-3, -5, -3, -4, DueDate.BegOfPeriod);
\r
166 AssertEquals ("#Pmt03", -5.68181818181818, d);
\r
168 d = Financial.Pmt (-3, -5, -3, -4, DueDate.EndOfPeriod);
\r
169 AssertEquals ("#Pmt04", 11.3636363636364, d);
\r
171 d = Financial.Pmt (0, 1, 0, 0, DueDate.BegOfPeriod);
\r
172 AssertEquals ("#Pmt05", 0, d);
\r
174 d = Financial.Pmt (0, 1, 0, 0, DueDate.EndOfPeriod);
\r
175 AssertEquals ("#Pmt06", 0, d);
\r
179 [ExpectedException(typeof(ArgumentException))]
\r
180 public void TestSLNArgs()
\r
182 Financial.SLN (0, 0, 0);
\r
186 public void TestSLN()
\r
188 double d = Financial.SLN (0, 0, 1);
\r
189 AssertEquals ("#SLN01", 0, d);
\r
191 d = Financial.SLN (45, 32, 345);
\r
192 AssertEquals ("#SLN02", 0.0376811594202899, d, 0.0001);
\r
194 d = Financial.SLN (-54, -4, -76);
\r
195 AssertEquals ("#SLN03", 0.657894736842105, d, 0.001);
\r
199 [ExpectedException(typeof(ArgumentException))]
\r
200 public void TestSYDArgs1()
\r
202 Financial.SYD (1, 1, 1, -1);
\r
206 [ExpectedException(typeof(ArgumentException))]
\r
207 public void TestSYDArgs2()
\r
209 Financial.SYD (1, -1, 1, 1);
\r
213 [ExpectedException(typeof(ArgumentException))]
\r
214 public void TestSYDArgs3()
\r
216 Financial.SYD (1, 1, 1, 2);
\r
220 public void TestSYD()
\r
222 double d = Financial.SYD (23, 34, 26, 21);
\r
223 AssertEquals ("#SYD01", -0.188034188034188, d);
\r
225 d = Financial.SYD (0, 1, 1, 1);
\r
226 AssertEquals ("#SYD02", -1, d);
\r
230 [ExpectedException(typeof(ArgumentException))]
\r
231 public void TestIRRArgs1()
\r
233 double [] arr = new double [0];
\r
234 Financial.IRR (ref arr, 0.1);
\r
238 [ExpectedException(typeof(ArgumentException))]
\r
239 public void TestIRRArgs2()
\r
241 double [] arr = new double [] {134};
\r
242 Financial.IRR (ref arr, 0.1);
\r
246 [ExpectedException(typeof(ArgumentException))]
\r
247 public void TestIRRArgs3()
\r
249 // -0.99 as Guess throws an exception on MS.NET, -0.98 doesn't
\r
250 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
251 double d = Financial.IRR (ref arr, -0.99);
\r
255 public void TestIRR()
\r
257 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
258 double d = Financial.IRR (ref arr, 0.1);
\r
259 AssertEquals ("#IRR01", 0.177435884422527, d);
\r
263 [ExpectedException(typeof(ArgumentException))]
\r
264 public void TestNPVArgs1()
\r
266 double [] arr = null;
\r
267 double d = Financial.NPV (0.0625, ref arr);
\r
271 [ExpectedException(typeof(ArgumentException))]
\r
272 public void TestNPVArgs2()
\r
274 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
275 double d = Financial.NPV (-1, ref arr);
\r
279 public void TestNPV()
\r
281 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
282 double d = Financial.NPV (0.0625, ref arr);
\r
283 AssertEquals ("#NPV01", 19312.5702095352, d);
\r
287 [ExpectedException(typeof(ArgumentException))]
\r
288 public void TestNPerArgs1()
\r
290 double d = Financial.NPer (-1, 2, 2, 2, DueDate.BegOfPeriod);
\r
294 [ExpectedException(typeof(ArgumentException))]
\r
295 public void TestNPerArgs2()
\r
297 double d = Financial.NPer (0, 0, 2, 2, DueDate.BegOfPeriod);
\r
301 public void TestNPer()
\r
303 double d = Financial.NPer (3, 4, 6, 2, DueDate.BegOfPeriod);
\r
304 AssertEquals ("#NPer01", -0.882767373181489, d, 0.001);
\r
306 d = Financial.NPer (1, -4, -6, -2, DueDate.EndOfPeriod);
\r
307 AssertEquals ("#NPer02", -2.32192809488736, d, 0.001);
\r
311 [ExpectedException(typeof(ArgumentException))]
\r
312 public void TestMIRRArgs1()
\r
314 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
315 double d = Financial.MIRR(ref arr, -1, 1);
\r
319 [ExpectedException(typeof(ArgumentException))]
\r
320 public void TestMIRRArgs2()
\r
322 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
323 double d = Financial.MIRR(ref arr, 1, -1);
\r
327 public void TestMIRR()
\r
329 double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
330 double d = Financial.MIRR (ref arr, 1, 1);
\r
331 AssertEquals ("#MIRR01", 0.509044845533018, d);
\r
333 arr = new double [] {-70000, 22000, 25000, 28000, 31000};
\r
334 d = Financial.MIRR (ref arr, 5, 5);
\r
335 AssertEquals ("#MIRR02", 2.02366041666348, d);
\r
339 [ExpectedException(typeof(ArgumentException))]
\r
340 public void TestPPmtArgs1()
\r
342 double d = Financial.PPmt (2, -1, 1, 1, 1, DueDate.EndOfPeriod);
\r
346 [ExpectedException(typeof(ArgumentException))]
\r
347 public void TestPPmtArgs2()
\r
349 double d = Financial.PPmt (1, 2, 1, 1, 1, DueDate.BegOfPeriod);
\r
353 public void TestPPmt()
\r
355 double d = Financial.PPmt (10, 2, 3, 7, 9, DueDate.BegOfPeriod);
\r
356 AssertEquals("#PPmt01", -0.120300751879702, d);
\r
358 d = Financial.PPmt (10, 4, 4, 7, 4, DueDate.EndOfPeriod);
\r
359 AssertEquals("#PPmt02", -10.0006830600969, d);
\r
361 d = Financial.PPmt (0, 5, 7, 7, 2, DueDate.BegOfPeriod);
\r
362 AssertEquals("#PPmt03", -1.28571428571429, d);
\r
364 d = Financial.PPmt (-5, 5, 7, -7, -2, DueDate.BegOfPeriod);
\r
365 AssertEquals("#PPmt04", -0.175770521818777, d);
\r
369 public void TestPV()
\r
371 double d = Financial.PV (1, 1, 1, 1, DueDate.BegOfPeriod);
\r
372 AssertEquals ("#PV01", -1.5, d);
\r
374 d = Financial.PV (1, 1, 1, 1, DueDate.EndOfPeriod);
\r
375 AssertEquals ("#PV02", -1, d);
\r
379 [ExpectedException(typeof(ArgumentException))]
\r
380 public void TestRateArgs1()
\r
382 double d = Financial.Rate (-1, 1, 1, 1, DueDate.BegOfPeriod, 1);
\r
386 public void TestRate()
\r
388 double d = Financial.Rate (1, 1, 1, 1, DueDate.BegOfPeriod, 0.1);
\r
389 AssertEquals("#Rate01", -1.5, d, 0.01);
\r
391 d = Financial.Rate (1, -1, -1, -1, DueDate.BegOfPeriod, 0.1);
\r
392 AssertEquals("#Rate02", -1.50000000000001, d, 0.01);
\r
394 d = Financial.Rate (1, 2, 12, 10, DueDate.BegOfPeriod, 0.5);
\r
395 AssertEquals("#Rate03", -1.71428571428571, d);
\r