Copied remotely
[mono.git] / mcs / class / Microsoft.VisualBasic / Test / Microsoft.VisualBasic / FinancialTest.cs
1 // FinancialTest.cs -   NUnit Test Cases for vb module Financial\r
2 //                                              (class Microsoft.VisualBasic.Financial)\r
3 //\r
4 // Rob Tillie (Rob@flep-tech.nl)\r
5 //\r
6 // (C) 2004 Rob Tillie\r
7 // \r
8
9 //
10 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
11 //
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:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
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.
30 //
31 \r
32 using NUnit.Framework;\r
33 using System;\r
34 using Microsoft.VisualBasic;\r
35 \r
36 namespace MonoTests.Microsoft.VisualBasic\r
37 {\r
38 \r
39         [TestFixture]\r
40         public class FinancialTest : Assertion {\r
41         \r
42                 [SetUp]\r
43                 public void GetReady() {}\r
44 \r
45                 [TearDown]\r
46                 public void Clean() {}\r
47                 \r
48                 \r
49                 // -- DDB Tests\r
50                 [Test]\r
51                 [ExpectedException(typeof(ArgumentException))]\r
52                 public void TestDDBArg1()\r
53                 {\r
54                         Financial.DDB (-1, 1, 1, 1, 1);\r
55                 }\r
56                 \r
57                 [Test]\r
58                 [ExpectedException(typeof(ArgumentException))]\r
59                 public void TestDDBArg2()\r
60                 {\r
61                         Financial.DDB (1, -1, 1, 1, 1);\r
62                 }\r
63                 \r
64                 [Test]\r
65                 [ExpectedException(typeof(ArgumentException))]\r
66                 public void TestDDBArg3()\r
67                 {\r
68                         Financial.DDB (1, 1, 0, 1, 1);\r
69                 }\r
70                 \r
71                 [Test]\r
72                 [ExpectedException(typeof(ArgumentException))]\r
73                 public void TestDDBArg4()\r
74                 {\r
75                         Financial.DDB (1, 1, 1, 1, 0);\r
76                 }\r
77                 \r
78                 [Test]\r
79                 [ExpectedException(typeof(ArgumentException))]\r
80                 public void TestDDBArg5()\r
81                 {\r
82                         // Period has to be > Life\r
83                         Financial.DDB (1, 1, 1, 2, 1);\r
84                 }\r
85                 \r
86                 [Test]\r
87                 public void TestDDB()\r
88                 {\r
89                         double ddb = Financial.DDB (1000, 50, 10, 5, 3);\r
90                         AssertEquals ("#DDB01", 1425, ddb, 0);\r
91                         \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
95                 }\r
96                 \r
97                 [Test]\r
98                 public void TestFV()\r
99                 {\r
100                         double d = Financial.FV (10, 5, 3, 7, DueDate.BegOfPeriod);\r
101                         AssertEquals ("#FV01", -1658822, d);\r
102                         \r
103                         d = Financial.FV (10, 5, 3, 7, DueDate.EndOfPeriod);\r
104                         AssertEquals ("#FV02", -1175672, d);\r
105                         \r
106                         d = Financial.FV (0, 5, 3, 7, DueDate.BegOfPeriod);\r
107                         AssertEquals ("#FV03", -22, d);\r
108                         \r
109                         d = Financial.FV(0, 1, 1, 1, DueDate.BegOfPeriod);\r
110                         AssertEquals ("#FV04", -2, d);\r
111                         \r
112                         d = Financial.FV (0, 0, 0, 0, DueDate.BegOfPeriod);\r
113                         AssertEquals ("#FV05", 0, d);\r
114                         \r
115                         d = Financial.FV (-3, -5, -6, -4, DueDate.BegOfPeriod);\r
116                         AssertEquals ("#FV06", -4.25, d);\r
117                 }\r
118                 \r
119                 [Test]\r
120                 [ExpectedException(typeof(ArgumentException))]\r
121                 public void TestIPmtArgs1()\r
122                 {\r
123                         Financial.IPmt (3, 6, 4, 2, 2, DueDate.BegOfPeriod);\r
124                 }\r
125                 \r
126                 [Test]\r
127                 [ExpectedException(typeof(ArgumentException))]\r
128                 public void TestIPmtArgs2()\r
129                 {\r
130                         Financial.IPmt (3, 0, 4, 2, 2, DueDate.BegOfPeriod);\r
131                 }\r
132                 \r
133                 [Test]\r
134                 public void TestIPmt()\r
135                 {\r
136                         double d = Financial.IPmt (10, 2, 3, 7, 9, DueDate.BegOfPeriod);\r
137                         AssertEquals ("#IPmt01", -6.25427204374573, d);\r
138                         \r
139                         d = Financial.IPmt (10, 4, 4, 7, 4, DueDate.EndOfPeriod);\r
140                         AssertEquals ("#IPmt02", -60.0068306011053, d);\r
141                         \r
142                         d = Financial.IPmt (0, 5, 7, 7, 2, DueDate.BegOfPeriod);\r
143                         AssertEquals ("#IPmt03", 0, d);\r
144                         \r
145                         d = Financial.IPmt (-5, 5, 7, -7, -2, DueDate.BegOfPeriod);\r
146                         AssertEquals ("#IPmt04", 8.92508391821792, d);\r
147                 }\r
148                 \r
149                 [Test]\r
150                 [ExpectedException(typeof(ArgumentException))]\r
151                 public void TestPmtArgs()\r
152                 {\r
153                         Financial.Pmt (1, 0, 1, 1, DueDate.BegOfPeriod);\r
154                 }\r
155                 \r
156                 [Test]\r
157                 public void TestPmt()\r
158                 {\r
159                         double d = Financial.Pmt (2, 5, 2, 3, DueDate.BegOfPeriod);\r
160                         AssertEquals ("#Pmt01", -1.34710743801653, d);\r
161                         \r
162                         d = Financial.Pmt (2, 5, 2, 3, DueDate.EndOfPeriod);\r
163                         AssertEquals ("#Pmt02", -4.04132231404959, d);\r
164                         \r
165                         d = Financial.Pmt (-3, -5, -3, -4, DueDate.BegOfPeriod);\r
166                         AssertEquals ("#Pmt03", -5.68181818181818, d);\r
167                         \r
168                         d = Financial.Pmt (-3, -5, -3, -4, DueDate.EndOfPeriod);\r
169                         AssertEquals ("#Pmt04", 11.3636363636364, d);\r
170                         \r
171                         d = Financial.Pmt (0, 1, 0, 0, DueDate.BegOfPeriod);\r
172                         AssertEquals ("#Pmt05", 0, d);\r
173                         \r
174                         d = Financial.Pmt (0, 1, 0, 0, DueDate.EndOfPeriod);\r
175                         AssertEquals ("#Pmt06", 0, d);\r
176                 }\r
177                 \r
178                 [Test]\r
179                 [ExpectedException(typeof(ArgumentException))]\r
180                 public void TestSLNArgs()\r
181                 {\r
182                         Financial.SLN (0, 0, 0);\r
183                 }               \r
184                 \r
185                 [Test]\r
186                 public void TestSLN()\r
187                 {\r
188                         double d = Financial.SLN (0, 0, 1);\r
189                         AssertEquals ("#SLN01", 0, d);\r
190         \r
191                         d = Financial.SLN (45, 32, 345);\r
192                         AssertEquals ("#SLN02", 0.0376811594202899, d, 0.0001);\r
193         \r
194                         d = Financial.SLN (-54, -4, -76);\r
195                         AssertEquals ("#SLN03", 0.657894736842105, d, 0.001);\r
196                 }\r
197                 \r
198                 [Test]\r
199                 [ExpectedException(typeof(ArgumentException))]\r
200                 public void TestSYDArgs1()\r
201                 {\r
202                         Financial.SYD (1, 1, 1, -1);\r
203                 }       \r
204                 \r
205                 [Test]\r
206                 [ExpectedException(typeof(ArgumentException))]\r
207                 public void TestSYDArgs2()\r
208                 {\r
209                         Financial.SYD (1, -1, 1, 1);\r
210                 }\r
211                 \r
212                 [Test]\r
213                 [ExpectedException(typeof(ArgumentException))]\r
214                 public void TestSYDArgs3()\r
215                 {\r
216                         Financial.SYD (1, 1, 1, 2);\r
217                 }\r
218                 \r
219                 [Test]\r
220                 public void TestSYD()\r
221                 {\r
222                         double d = Financial.SYD (23, 34, 26, 21);\r
223                         AssertEquals ("#SYD01", -0.188034188034188, d);\r
224 \r
225                         d = Financial.SYD (0, 1, 1, 1);\r
226                         AssertEquals ("#SYD02", -1, d);\r
227                 }\r
228                 \r
229                 [Test]\r
230                 [ExpectedException(typeof(ArgumentException))]\r
231                 public void TestIRRArgs1()\r
232                 {\r
233                         double [] arr = new double [0];\r
234                         Financial.IRR (ref arr, 0.1);\r
235                 }\r
236                 \r
237                 [Test]\r
238                 [ExpectedException(typeof(ArgumentException))]\r
239                 public void TestIRRArgs2()\r
240                 {\r
241                         double [] arr = new double [] {134};\r
242                         Financial.IRR (ref arr, 0.1);\r
243                 }\r
244                 \r
245                 [Test]\r
246                 [ExpectedException(typeof(ArgumentException))]\r
247                 public void TestIRRArgs3()\r
248                 {\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
252                 }\r
253                 \r
254                 [Test]\r
255                 public void TestIRR()\r
256                 {\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
260                 }\r
261                 \r
262                 [Test]\r
263                 [ExpectedException(typeof(ArgumentException))]\r
264                 public void TestNPVArgs1()\r
265                 {\r
266                         double [] arr = null;\r
267                         double d = Financial.NPV (0.0625, ref arr);\r
268                 }\r
269                 \r
270                 [Test]\r
271                 [ExpectedException(typeof(ArgumentException))]\r
272                 public void TestNPVArgs2()\r
273                 {\r
274                         double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};\r
275                         double d = Financial.NPV (-1, ref arr);\r
276                 }\r
277                         \r
278                 [Test]\r
279                 public void TestNPV()\r
280                 {\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
284                 }\r
285                 \r
286                 [Test]\r
287                 [ExpectedException(typeof(ArgumentException))]\r
288                 public void TestNPerArgs1()\r
289                 {\r
290                         double d = Financial.NPer (-1, 2, 2, 2, DueDate.BegOfPeriod);\r
291                 }\r
292                 \r
293                 [Test]\r
294                 [ExpectedException(typeof(ArgumentException))]\r
295                 public void TestNPerArgs2()\r
296                 {\r
297                         double d = Financial.NPer (0, 0, 2, 2, DueDate.BegOfPeriod);\r
298                 }\r
299                 \r
300                 [Test]\r
301                 public void TestNPer()\r
302                 {\r
303                         double d = Financial.NPer (3, 4, 6, 2, DueDate.BegOfPeriod);\r
304                         AssertEquals ("#NPer01", -0.882767373181489, d, 0.001);\r
305                         \r
306                         d = Financial.NPer (1, -4, -6, -2, DueDate.EndOfPeriod);\r
307                         AssertEquals ("#NPer02", -2.32192809488736, d, 0.001);\r
308                 }\r
309                 \r
310                 [Test]\r
311                 [ExpectedException(typeof(ArgumentException))]\r
312                 public void TestMIRRArgs1()\r
313                 {\r
314                         double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};\r
315                         double d = Financial.MIRR(ref arr, -1, 1);\r
316                 }\r
317                 \r
318                 [Test]\r
319                 [ExpectedException(typeof(ArgumentException))]\r
320                 public void TestMIRRArgs2()\r
321                 {\r
322                         double [] arr = new double [] {-70000, 22000, 25000, 28000, 31000};\r
323                         double d = Financial.MIRR(ref arr, 1, -1);\r
324                 }\r
325                 \r
326                 [Test]\r
327                 public void TestMIRR()\r
328                 {\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
332                         \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
336                 }\r
337                 \r
338                 [Test]\r
339                 [ExpectedException(typeof(ArgumentException))]\r
340                 public void TestPPmtArgs1()\r
341                 {\r
342                         double d = Financial.PPmt (2, -1, 1, 1, 1, DueDate.EndOfPeriod);\r
343                 }\r
344                 \r
345                 [Test]\r
346                 [ExpectedException(typeof(ArgumentException))]\r
347                 public void TestPPmtArgs2()\r
348                 {\r
349                         double d = Financial.PPmt (1, 2, 1, 1, 1, DueDate.BegOfPeriod);\r
350                 }\r
351                 \r
352                 [Test]\r
353                 public void TestPPmt()\r
354                 {\r
355                         double d = Financial.PPmt (10, 2, 3, 7, 9, DueDate.BegOfPeriod);\r
356                         AssertEquals("#PPmt01", -0.120300751879702, d);\r
357                         \r
358                         d = Financial.PPmt (10, 4, 4, 7, 4, DueDate.EndOfPeriod);\r
359                         AssertEquals("#PPmt02", -10.0006830600969, d);\r
360                         \r
361                         d = Financial.PPmt (0, 5, 7, 7, 2, DueDate.BegOfPeriod);\r
362                         AssertEquals("#PPmt03", -1.28571428571429, d);\r
363                         \r
364                         d = Financial.PPmt (-5, 5, 7, -7, -2, DueDate.BegOfPeriod);\r
365                         AssertEquals("#PPmt04", -0.175770521818777, d);\r
366                 }\r
367                 \r
368                 [Test]\r
369                 public void TestPV()\r
370                 {\r
371                         double d = Financial.PV (1, 1, 1, 1, DueDate.BegOfPeriod);\r
372                         AssertEquals ("#PV01", -1.5, d);\r
373                         \r
374                         d = Financial.PV (1, 1, 1, 1, DueDate.EndOfPeriod);\r
375                         AssertEquals ("#PV02", -1, d);\r
376                 }\r
377                 \r
378                 [Test]\r
379                 [ExpectedException(typeof(ArgumentException))]\r
380                 public void TestRateArgs1()\r
381                 {\r
382                         double d = Financial.Rate (-1, 1, 1, 1, DueDate.BegOfPeriod, 1);\r
383                 }\r
384                 \r
385                 [Test]\r
386                 public void TestRate()\r
387                 {\r
388                         double d = Financial.Rate (1, 1, 1, 1, DueDate.BegOfPeriod, 0.1);\r
389                         AssertEquals("#Rate01", -1.5, d, 0.01);\r
390                         \r
391                         d = Financial.Rate (1, -1, -1, -1, DueDate.BegOfPeriod, 0.1);\r
392                         AssertEquals("#Rate02", -1.50000000000001, d, 0.01);\r
393                         \r
394                         d = Financial.Rate (1, 2, 12, 10, DueDate.BegOfPeriod, 0.5);\r
395                         AssertEquals("#Rate03", -1.71428571428571, d);\r
396                 }\r
397         }\r
398 }\r