From 8c0a87cb3b7cf8bc7515f70b29d734cae4566fe3 Mon Sep 17 00:00:00 2001 From: Chika Date: Mon, 19 Oct 2020 20:33:56 +0300 Subject: [PATCH] rewrite simple columns && start sumColumn --- .../.idea/contentModel.xml | 29 +- .../.idea/workspace.xml | 246 +++++++++------ .../Columns/BaseColumn.cs | 12 + .../Columns/BaseColumnWithXIRR.cs | 21 +- .../Columns/DateColumn.cs | 13 +- .../Columns/DateTempColumn.cs | 14 +- .../Columns/GPS_GrColumn.cs | 24 ++ .../Columns/KaskoNmperGrColumn.cs | 28 +- .../Columns/PercentPaymentColumn.cs | 15 +- .../Columns/SumColumn.cs | 51 ++- .../Columns/SumWithVATColumn.cs | 26 ++ .../Columns/TLM_GrColumn.cs | 29 ++ .../Columns/VATColumn.cs | 26 ++ .../Bisection.cs | 295 ++++++++++++++++++ .../EvoCalculator.Core.FinanceFormulas.csproj | 4 + .../GoalXIRR.cs | 23 ++ .../Calculation/Interfaces/IColumn.cs | 12 - .../Calculation/Interfaces/IColumnWithXIRR.cs | 12 - .../{Suite => }/Columns/DateColumnTests.cs | 21 +- .../{Suite => }/Columns/DateTempTests.cs | 18 +- .../Calculation/Columns/GPS_GrTests.cs | 263 ++++++++++++++++ .../{Suite => }/Columns/KaskoNmperGrTests.cs | 20 +- .../Columns/PercentPaymentTests.cs | 17 +- .../{Suite => }/Columns/SumColumnTest.cs | 22 +- .../Calculation/Columns/SumWithVatTests.cs | 101 ++++++ .../Calculation/Columns/TLM_GrTests.cs | 263 ++++++++++++++++ .../Calculation/Columns/VATColumnTests.cs | 102 ++++++ .../FinanceFormulasTests.cs | 1 + 28 files changed, 1436 insertions(+), 272 deletions(-) create mode 100644 EvoCalculator.Core.Calculation/Columns/BaseColumn.cs create mode 100644 EvoCalculator.Core.Calculation/Columns/GPS_GrColumn.cs create mode 100644 EvoCalculator.Core.Calculation/Columns/SumWithVATColumn.cs create mode 100644 EvoCalculator.Core.Calculation/Columns/TLM_GrColumn.cs create mode 100644 EvoCalculator.Core.Calculation/Columns/VATColumn.cs create mode 100644 EvoCalculator.Core.FinanceFormulas/Bisection.cs create mode 100644 EvoCalculator.Core.FinanceFormulas/GoalXIRR.cs delete mode 100644 EvoCalculator.Core.Models/Calculation/Interfaces/IColumn.cs delete mode 100644 EvoCalculator.Core.Models/Calculation/Interfaces/IColumnWithXIRR.cs rename EvoCalculator.Core.Tests/Calculation/{Suite => }/Columns/DateColumnTests.cs (91%) rename EvoCalculator.Core.Tests/Calculation/{Suite => }/Columns/DateTempTests.cs (93%) create mode 100644 EvoCalculator.Core.Tests/Calculation/Columns/GPS_GrTests.cs rename EvoCalculator.Core.Tests/Calculation/{Suite => }/Columns/KaskoNmperGrTests.cs (96%) rename EvoCalculator.Core.Tests/Calculation/{Suite => }/Columns/PercentPaymentTests.cs (95%) rename EvoCalculator.Core.Tests/Calculation/{Suite => }/Columns/SumColumnTest.cs (92%) create mode 100644 EvoCalculator.Core.Tests/Calculation/Columns/SumWithVatTests.cs create mode 100644 EvoCalculator.Core.Tests/Calculation/Columns/TLM_GrTests.cs create mode 100644 EvoCalculator.Core.Tests/Calculation/Columns/VATColumnTests.cs rename EvoCalculator.Core.Tests/Calculation/{Suite => FinanceFormulas}/FinanceFormulasTests.cs (99%) diff --git a/.idea/.idea.EvoCalculator.Core/.idea/contentModel.xml b/.idea/.idea.EvoCalculator.Core/.idea/contentModel.xml index 3856d67..84d008b 100644 --- a/.idea/.idea.EvoCalculator.Core/.idea/contentModel.xml +++ b/.idea/.idea.EvoCalculator.Core/.idea/contentModel.xml @@ -38,12 +38,17 @@ + + + + + @@ -68,7 +73,9 @@ + + @@ -83,8 +90,6 @@ - - @@ -116,15 +121,19 @@ + + + + + + + + + + + - - - - - - - - + diff --git a/.idea/.idea.EvoCalculator.Core/.idea/workspace.xml b/.idea/.idea.EvoCalculator.Core/.idea/workspace.xml index 8020301..f47f427 100644 --- a/.idea/.idea.EvoCalculator.Core/.idea/workspace.xml +++ b/.idea/.idea.EvoCalculator.Core/.idea/workspace.xml @@ -14,19 +14,35 @@ - - - - - + + + + + + + + + + + + + + - - - - - + + + + + + + + + + + + - + + - + + + + + + + + + + + + @@ -115,8 +143,9 @@ + - + @@ -164,7 +193,8 @@ - + + 1602593830686 @@ -208,90 +238,94 @@ - + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - - + + - + + + + + @@ -304,18 +338,18 @@ - + - + - + - + @@ -324,18 +358,36 @@ - + - + + + + + file://$PROJECT_DIR$/EvoCalculator.Core.Calculation/Columns/SumColumn.cs + 41 + + + + + + + + + + diff --git a/EvoCalculator.Core.Calculation/Columns/BaseColumn.cs b/EvoCalculator.Core.Calculation/Columns/BaseColumn.cs new file mode 100644 index 0000000..836f0be --- /dev/null +++ b/EvoCalculator.Core.Calculation/Columns/BaseColumn.cs @@ -0,0 +1,12 @@ +namespace EvoCalculator.Core.Calculation.Columns +{ + public abstract class BaseColumn + { + public T[] Values { get; set; } + + protected BaseColumn(int count) + { + Values = new T[count]; + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/BaseColumnWithXIRR.cs b/EvoCalculator.Core.Calculation/Columns/BaseColumnWithXIRR.cs index 51718f3..f6c3153 100644 --- a/EvoCalculator.Core.Calculation/Columns/BaseColumnWithXIRR.cs +++ b/EvoCalculator.Core.Calculation/Columns/BaseColumnWithXIRR.cs @@ -1,14 +1,19 @@ using System; using EvoCalculator.Core.FinanceFormulas; -using EvoCalculator.Core.Models.Calculation.Interfaces; +using EvoCalculator.Core.Models.Calculation.Models; -namespace EvoCalculator.Core.Models.Calculation.Models +namespace EvoCalculator.Core.Calculation.Columns { - public class BaseColumnWithXIRR : IColumnWithXIRR + public class BaseColumnWithXIRR : BaseColumn { - public double[] Values { get; set; } public double IRR { get; set; } - public DateTime[] Dates { get; set; } + private DateTime[] Dates { get; set; } + + + public BaseColumnWithXIRR(int count, DateTempColumn dateTempColumn) : base(count) + { + Dates = dateTempColumn.Values; + } protected Flow[] Flows { @@ -27,11 +32,5 @@ namespace EvoCalculator.Core.Models.Calculation.Models return flows; } } - - public void ComputeXIRR() - { - var XIRR = new XIRR(this.Flows); - IRR = XIRR.GetResult(); - } } } \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/DateColumn.cs b/EvoCalculator.Core.Calculation/Columns/DateColumn.cs index 3c03364..f146890 100644 --- a/EvoCalculator.Core.Calculation/Columns/DateColumn.cs +++ b/EvoCalculator.Core.Calculation/Columns/DateColumn.cs @@ -1,22 +1,15 @@ using System; -using EvoCalculator.Core.Models.Calculation.Interfaces; using EvoCalculator.Core.Models.Calculation.Models.Prepared; namespace EvoCalculator.Core.Calculation.Columns { - public class DateColumn : IColumn + public class DateColumn : BaseColumn { - public DateTime[] Values { get; set; } - - public DateColumn(int count) + public DateColumn(int count) : base(count) { - Values = new DateTime[count]; } - public void ComputeValues(PreparedValues preparedValues, - PreparedPayments preparedPayments, - Constants.Calculation constants, - params IColumn[] columns) + public void ComputeValues(PreparedValues preparedValues, Constants.Calculation constants) { Values[0] = preparedValues.DogDate; Values[1] = Values[0]; diff --git a/EvoCalculator.Core.Calculation/Columns/DateTempColumn.cs b/EvoCalculator.Core.Calculation/Columns/DateTempColumn.cs index 4c1a36a..44d1063 100644 --- a/EvoCalculator.Core.Calculation/Columns/DateTempColumn.cs +++ b/EvoCalculator.Core.Calculation/Columns/DateTempColumn.cs @@ -1,23 +1,15 @@ using System; -using EvoCalculator.Core.Models.Calculation.Interfaces; using EvoCalculator.Core.Models.Calculation.Models.Prepared; namespace EvoCalculator.Core.Calculation.Columns { - public class DateTempColumn : IColumn + public class DateTempColumn : BaseColumn { - public DateTime[] Values { get; set; } - - //TODO: extract 67 - public DateTempColumn(int count = 67) + public DateTempColumn(int count) : base(count) { - Values = new DateTime[count]; } - - public void ComputeValues(PreparedValues preparedValues, PreparedPayments preparedPayments, - Constants.Calculation constants, - params IColumn[] columns) + public void ComputeValues(PreparedValues preparedValues) { Values[0] = preparedValues.DogDate; Values[1] = Values[0]; diff --git a/EvoCalculator.Core.Calculation/Columns/GPS_GrColumn.cs b/EvoCalculator.Core.Calculation/Columns/GPS_GrColumn.cs new file mode 100644 index 0000000..2c73e97 --- /dev/null +++ b/EvoCalculator.Core.Calculation/Columns/GPS_GrColumn.cs @@ -0,0 +1,24 @@ +using EvoCalculator.Core.FinanceFormulas; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; + +namespace EvoCalculator.Core.Calculation.Columns +{ + public class GPS_GrColumn : BaseColumnWithXIRR + { + public GPS_GrColumn(int count, DateTempColumn dateTempColumn) : base(count, dateTempColumn) + { + } + + public void ComputeValues(PreparedPayments preparedPayments, PreparedValues preparedValues) + { + for (var i = 1; i < Values.Length; i++) + { + Values[i] = preparedPayments.Rows[i - 1].TlmCostPayment; + } + + + var XNPV = new XNPV(base.Flows, preparedValues.IrrExpected); + Values[0] = -XNPV.GetResult(); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/KaskoNmperGrColumn.cs b/EvoCalculator.Core.Calculation/Columns/KaskoNmperGrColumn.cs index 1352972..7fa58ff 100644 --- a/EvoCalculator.Core.Calculation/Columns/KaskoNmperGrColumn.cs +++ b/EvoCalculator.Core.Calculation/Columns/KaskoNmperGrColumn.cs @@ -1,21 +1,16 @@ -using System; -using EvoCalculator.Core.FinanceFormulas; -using EvoCalculator.Core.Models.Calculation.Interfaces; -using EvoCalculator.Core.Models.Calculation.Models; +using EvoCalculator.Core.FinanceFormulas; using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using TridentGoalSeek; namespace EvoCalculator.Core.Calculation.Columns { - public class KaskoNmperGrColumn : IColumn + public class KaskoNmperGrColumn : BaseColumnWithXIRR { - public double[] Values { get; set; } - - public KaskoNmperGrColumn(int count) + public KaskoNmperGrColumn(int count, DateTempColumn dateTempColumn) : base(count, dateTempColumn) { - Values = new double[count]; } - public void ComputeValues(PreparedValues preparedValues, IColumn dateTempColumn) + public void ComputeValues(PreparedValues preparedValues) { for (var i = 0; i < Values.Length; i++) { @@ -38,18 +33,7 @@ namespace EvoCalculator.Core.Calculation.Columns } - Flow[] flows = new Flow[Values.Length - 1]; - for (var i = 1; i < Values.Length; i++) - { - flows[i - 1] = new Flow() - { - Date = dateTempColumn.Values[i].Date, - Value = Values[i] - }; - } - - - var XNPV = new XNPV(flows, preparedValues.IrrExpected); + var XNPV = new XNPV(Flows, preparedValues.IrrExpected); Values[0] = -XNPV.GetResult(); } } diff --git a/EvoCalculator.Core.Calculation/Columns/PercentPaymentColumn.cs b/EvoCalculator.Core.Calculation/Columns/PercentPaymentColumn.cs index 3578803..5afc858 100644 --- a/EvoCalculator.Core.Calculation/Columns/PercentPaymentColumn.cs +++ b/EvoCalculator.Core.Calculation/Columns/PercentPaymentColumn.cs @@ -1,21 +1,14 @@ -using System.Linq; -using EvoCalculator.Core.Models.Calculation.Interfaces; -using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; namespace EvoCalculator.Core.Calculation.Columns { - public class PercentPaymentColumn : IColumn + public class PercentPaymentColumn : BaseColumn { - public double[] Values { get; set; } - - public PercentPaymentColumn(int count) + public PercentPaymentColumn(int count) : base(count) { - Values = new double[count]; } - public void ComputeValues(PreparedValues preparedValues, PreparedPayments preparedPayments, - Constants.Calculation constants, - params IColumn[] columns) + public void ComputeValues(PreparedPayments preparedPayments) { Values[0] = 0; Values[1] = 0; diff --git a/EvoCalculator.Core.Calculation/Columns/SumColumn.cs b/EvoCalculator.Core.Calculation/Columns/SumColumn.cs index f526f4a..015481a 100644 --- a/EvoCalculator.Core.Calculation/Columns/SumColumn.cs +++ b/EvoCalculator.Core.Calculation/Columns/SumColumn.cs @@ -1,35 +1,52 @@ using System; -using EvoCalculator.Core.Models.Calculation.Interfaces; -using EvoCalculator.Core.Models.Calculation.Models; +using System.Linq; +using EvoCalculator.Core.FinanceFormulas; using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using TridentGoalSeek; namespace EvoCalculator.Core.Calculation.Columns { - public class SumColumn : BaseColumnWithXIRR + public class SumColumn : BaseColumnWithXIRR, IGoalSeekAlgorithm { - public SumColumn(int count, DateTempColumn dateTempColumn) - { - base.Values = new double[count]; + private readonly PreparedValues _preparedValues; + private readonly PercentPaymentColumn _percentPaymentColumn; - base.Dates = new DateTime[dateTempColumn.Values.Length]; - for (var i = 0; i < dateTempColumn.Values.Length; i++) - { - base.Dates[i] = dateTempColumn.Values[i]; - } + public SumColumn(int count, DateTempColumn dateTempColumn + , PreparedValues preparedValues + , PercentPaymentColumn percentPaymentColumn) : base(count, dateTempColumn) + { + this._preparedValues = preparedValues; + this._percentPaymentColumn = percentPaymentColumn; } - public void ComputeValues(PreparedValues preparedValues, PercentPaymentColumn percentPaymentColumn) + public void ComputeValues() { - Values[0] = -preparedValues.BaseCost; - Values[1] = preparedValues.FirstPaymentSum; - Values[2] = 98647.7277641429; + Values[0] = -this._preparedValues.BaseCost; + Values[1] = this._preparedValues.FirstPaymentSum; for (var i = 3; i < Values.Length - 1; i++) { - Values[i] = Values[2] * percentPaymentColumn.Values[i] / 100; + Values[i] = Values[2] * _percentPaymentColumn.Values[i] / 100; } - Values[^1] = preparedValues.LastPaymentSum; + Values[^1] = _preparedValues.LastPaymentSum; + } + + public decimal Calculate(decimal inputVariable) + { + Values[2] = Convert.ToDouble(inputVariable); + this.ComputeValues(); + var XIRR = new XIRR(Flows); + IRR = XIRR.GetResult(); + + return Convert.ToDecimal(IRR); + } + + public void ComputeValues(decimal requiredIRR) + { + var goalSeek = new GoalSeek(this); + goalSeek.SeekResult(requiredIRR, + new GoalSeekOptions(initialTineSpacing: 10000)); } } } \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/SumWithVATColumn.cs b/EvoCalculator.Core.Calculation/Columns/SumWithVATColumn.cs new file mode 100644 index 0000000..e714646 --- /dev/null +++ b/EvoCalculator.Core.Calculation/Columns/SumWithVATColumn.cs @@ -0,0 +1,26 @@ +using System.Linq; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; + +namespace EvoCalculator.Core.Calculation.Columns +{ + public class SumWithVATColumn : BaseColumn + { + public SumWithVATColumn(int count) : base(count) + { + } + + public void ComputeValues(Constants.Calculation constants, SumColumn sumColumn, PreparedValues preparedValues) + { + Values[0] = 0; + Values[1] = preparedValues.FirstPaymentWithNdsAbs > 0 + ? preparedValues.FirstPaymentWithNdsAbs + : (1 + constants.VatValue) * sumColumn.Values[1]; + for (var i = 2; i < Values.Length; i++) + { + Values[i] = (1 + constants.VatValue) * sumColumn.Values[i]; + } + + Values[0] = Values.Sum(); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/TLM_GrColumn.cs b/EvoCalculator.Core.Calculation/Columns/TLM_GrColumn.cs new file mode 100644 index 0000000..6e95469 --- /dev/null +++ b/EvoCalculator.Core.Calculation/Columns/TLM_GrColumn.cs @@ -0,0 +1,29 @@ +using EvoCalculator.Core.FinanceFormulas; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; + +namespace EvoCalculator.Core.Calculation.Columns +{ + public class TLM_GrColumn : BaseColumnWithXIRR + { + public TLM_GrColumn(int count, DateTempColumn dateTempColumn) : base(count, dateTempColumn) + { + } + + public void ComputeValues(PreparedPayments preparedPayments, PreparedValues preparedValues) + { + for (var i = 0; i < Values.Length; i++) + { + Values[i] = 0; + } + + for (var i = 1; i < Values.Length; i++) + { + Values[i] = preparedPayments.Rows[i - 1].TlmCostPayment; + } + + + var XNPV = new XNPV(base.Flows, preparedValues.IrrExpected); + Values[0] = -XNPV.GetResult(); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Calculation/Columns/VATColumn.cs b/EvoCalculator.Core.Calculation/Columns/VATColumn.cs new file mode 100644 index 0000000..fca75df --- /dev/null +++ b/EvoCalculator.Core.Calculation/Columns/VATColumn.cs @@ -0,0 +1,26 @@ +using System.Linq; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; + +namespace EvoCalculator.Core.Calculation.Columns +{ + public class VATColumn : BaseColumn + { + public VATColumn(int count) : base(count) + { + } + + public void ComputeValues(Constants.Calculation constants, SumColumn sumColumn, PreparedValues preparedValues) + { + Values[0] = 0; + Values[1] = preparedValues.FirstPaymentNdsAbs > 0 + ? preparedValues.FirstPaymentNdsAbs + : constants.VatValue * sumColumn.Values[1]; + for (var i = 2; i < Values.Length; i++) + { + Values[i] = constants.VatValue * sumColumn.Values[i]; + } + + Values[0] = Values.Sum(); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.FinanceFormulas/Bisection.cs b/EvoCalculator.Core.FinanceFormulas/Bisection.cs new file mode 100644 index 0000000..1cf9a14 --- /dev/null +++ b/EvoCalculator.Core.FinanceFormulas/Bisection.cs @@ -0,0 +1,295 @@ +// using System; +// using System.Linq; +// using Money = System.Decimal; +// using Rate = System.Double; +// using System.Collections.Generic; +// +// public +// struct Pair +// { +// public Pair(T first, Z second) +// { +// First = first; +// Second = second; +// } +// +// public readonly T First; +// +// public readonly Z Second; +// } +// +// +// public class CashFlow +// { +// public CashFlow(Money amount, DateTime date) +// { +// Amount = amount; +// Date = date; +// } +// +// public readonly Money Amount; +// public readonly DateTime Date; +// } +// +// public struct AlgorithmResult +// { +// public AlgorithmResult(TKindOfResult kind, TValue value) +// { +// Kind = kind; +// Value = value; +// } +// +// public readonly TKindOfResult Kind; +// public readonly TValue Value; +// } +// +// public enum ApproximateResultKind +// { +// ApproximateSolution, +// ExactSolution, +// NoSolutionWithinTolerance +// } +// +// public static class Algorithms +// { +// internal static Money CalculateXNPV(IEnumerable cfs, Rate r) +// { +// if (r <= -1) +// r = -0.99999999; // Very funky ... Better check what an IRR <= -100% means +// +// return (from cf in cfs +// let startDate = cfs.OrderBy(cf1 => cf1.Date).First().Date +// select cf.Amount / (decimal) Math.Pow(1 + r, (cf.Date - startDate).Days / 365.0)).Sum(); +// } +// +// internal static Pair FindBrackets(Func, Rate, Money> func, +// IEnumerable cfs) +// { +// // Abracadabra magic numbers ... +// const int maxIter = 100; +// const Rate bracketStep = 0.5; +// const Rate guess = 0.1; +// +// Rate leftBracket = guess - bracketStep; +// Rate rightBracket = guess + bracketStep; +// var iter = 0; +// +// while (func(cfs, leftBracket) * func(cfs, rightBracket) > 0 && iter++ < maxIter) +// { +// leftBracket -= bracketStep; +// rightBracket += bracketStep; +// } +// +// if (iter >= maxIter) +// return new Pair(0, 0); +// +// return new Pair(leftBracket, rightBracket); +// } +// +// // From "Applied Numerical Analyis" by Gerald +// internal static AlgorithmResult Bisection(Func func, +// Pair brackets, Rate tol, int maxIters) +// { +// int iter = 1; +// +// Money f3 = 0; +// Rate x3 = 0; +// Rate x1 = brackets.First; +// Rate x2 = brackets.Second; +// +// do +// { +// var f1 = func(x1); +// var f2 = func(x2); +// +// if (f1 == 0 && f2 == 0) +// return new AlgorithmResult(ApproximateResultKind.NoSolutionWithinTolerance, +// x1); +// +// if (f1 * f2 > 0) +// throw new ArgumentException("x1 x2 values don't bracket a root"); +// +// x3 = (x1 + x2) / 2; +// f3 = func(x3); +// +// if (f3 * f1 < 0) +// x2 = x3; +// else +// x1 = x3; +// +// iter++; +// } while (Math.Abs(x1 - x2) / 2 > tol && f3 != 0 && iter < maxIters); +// +// if (f3 == 0) +// return new AlgorithmResult(ApproximateResultKind.ExactSolution, x3); +// +// if (Math.Abs(x1 - x2) / 2 < tol) +// return new AlgorithmResult(ApproximateResultKind.ApproximateSolution, x3); +// +// if (iter > maxIters) +// return new AlgorithmResult(ApproximateResultKind.NoSolutionWithinTolerance, +// x3); +// +// throw new Exception("It should never get here"); +// } +// +// public static AlgorithmResult CalculateXIRR(IEnumerable cfs, Rate tolerance, +// int maxIters) +// { +// var brackets = FindBrackets(CalculateXNPV, cfs); +// +// if (brackets.First == brackets.Second) +// return new AlgorithmResult(ApproximateResultKind.NoSolutionWithinTolerance, +// brackets.First); +// +// return Bisection(r => CalculateXNPV(cfs, r), brackets, tolerance, maxIters); +// } +// } +// +// // TESTS +// using Microsoft.VisualStudio.TestTools.UnitTesting; +// using System.Collections.Generic; +// using System; +// using Rate = System.Double; +// +// namespace TimeLineTest +// { +// [TestClass()] +// public class AlgorithmsTest +// { +// IEnumerable cfs = new CashFlow[] +// { +// new CashFlow(-10000, new DateTime(2008, 1, 1)), +// new CashFlow(2750, new DateTime(2008, 3, 1)), +// new CashFlow(4250, new DateTime(2008, 10, 30)), +// new CashFlow(3250, new DateTime(2009, 2, 15)), +// new CashFlow(2750, new DateTime(2009, 4, 1)) +// }; +// +// IEnumerable bigcfs = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(10, new DateTime(2002, 1, 2)), +// new CashFlow(20, new DateTime(2003, 1, 3)) +// }; +// +// IEnumerable negcfs = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(-1, new DateTime(2002, 1, 2)), +// new CashFlow(1, new DateTime(2003, 1, 3)) +// }; +// +// IEnumerable samedaysamecfs = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(10, new DateTime(2000, 1, 1)), +// }; +// +// IEnumerable samedaydifferentcfs = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(100, new DateTime(2000, 1, 1)), +// }; +// +// IEnumerable bigratecfs = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(20, new DateTime(2000, 5, 30)), +// }; +// +// IEnumerable zeroRate = new CashFlow[] +// { +// new CashFlow(-10, new DateTime(2000, 1, 1)), +// new CashFlow(10, new DateTime(2003, 1, 1)), +// }; +// +// IEnumerable doubleNegative = new CashFlow[] +// { +// new CashFlow(-10000, new DateTime(2008, 1, 1)), +// new CashFlow(2750, new DateTime(2008, 3, 1)), +// new CashFlow(-4250, new DateTime(2008, 10, 30)), +// new CashFlow(3250, new DateTime(2009, 2, 15)), +// new CashFlow(2750, new DateTime(2009, 4, 1)) +// }; +// +// IEnumerable badDoubleNegative = new CashFlow[] +// { +// new CashFlow(-10000, new DateTime(2008, 1, 1)), +// new CashFlow(2750, new DateTime(2008, 3, 1)), +// new CashFlow(-4250, new DateTime(2008, 10, 30)), +// new CashFlow(3250, new DateTime(2009, 2, 15)), +// new CashFlow(-2750, new DateTime(2009, 4, 1)) +// }; +// +// double r = 0.09; +// double tolerance = 0.0001; +// int maxIters = 100; +// +// private TestContext testContextInstance; +// +// public TestContext TestContext +// { +// get { return testContextInstance; } +// set { testContextInstance = value; } +// } +// +// [TestMethod()] +// public void CalculateXNPV() +// { +// Assert.AreEqual(2086.6476020315416570634272814M, Algorithms.CalculateXNPV(cfs, r)); +// Assert.AreEqual(-10.148147600710372651326920258M, Algorithms.CalculateXNPV(negcfs, 0.5)); +// Assert.AreEqual(4.9923725815954514810351876895M, Algorithms.CalculateXNPV(bigcfs, 0.3)); +// } +// +// [TestMethod] +// public void FindBrackets() +// { +// var brackets = Algorithms.FindBrackets(Algorithms.CalculateXNPV, cfs); +// Assert.IsTrue(brackets.First < 0.3733 && brackets.Second > 0.3733); +// +// brackets = Algorithms.FindBrackets(Algorithms.CalculateXNPV, bigcfs); +// Assert.IsTrue(brackets.First < 0.5196 && brackets.Second > 0.5196); +// +// brackets = Algorithms.FindBrackets(Algorithms.CalculateXNPV, negcfs); +// Assert.IsTrue(brackets.First < -0.6059 && brackets.Second > -0.6059); +// } +// +// [TestMethod] +// public void XIRRTest() +// { +// var irr = Algorithms.CalculateXIRR(cfs, tolerance, maxIters); +// Assert.AreEqual(0.3733, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(bigcfs, tolerance, maxIters); +// Assert.AreEqual(0.5196, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(negcfs, tolerance, maxIters); +// Assert.AreEqual(-0.6059, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(samedaysamecfs, tolerance, maxIters); +// Assert.AreEqual(ApproximateResultKind.NoSolutionWithinTolerance, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(samedaydifferentcfs, tolerance, maxIters); +// Assert.AreEqual(ApproximateResultKind.NoSolutionWithinTolerance, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(bigratecfs, tolerance, maxIters); +// Assert.AreEqual(4.40140, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(zeroRate, tolerance, maxIters); +// Assert.AreEqual(0, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(doubleNegative, tolerance, maxIters); +// Assert.AreEqual(-0.537055, irr.Value, 0.001); +// Assert.AreEqual(ApproximateResultKind.ApproximateSolution, irr.Kind); +// +// irr = Algorithms.CalculateXIRR(badDoubleNegative, tolerance, maxIters); +// Assert.AreEqual(ApproximateResultKind.NoSolutionWithinTolerance, irr.Kind); +// } +// } +// } \ No newline at end of file diff --git a/EvoCalculator.Core.FinanceFormulas/EvoCalculator.Core.FinanceFormulas.csproj b/EvoCalculator.Core.FinanceFormulas/EvoCalculator.Core.FinanceFormulas.csproj index 642f0ef..abd3f1d 100644 --- a/EvoCalculator.Core.FinanceFormulas/EvoCalculator.Core.FinanceFormulas.csproj +++ b/EvoCalculator.Core.FinanceFormulas/EvoCalculator.Core.FinanceFormulas.csproj @@ -9,4 +9,8 @@ + + + + diff --git a/EvoCalculator.Core.FinanceFormulas/GoalXIRR.cs b/EvoCalculator.Core.FinanceFormulas/GoalXIRR.cs new file mode 100644 index 0000000..a3f0998 --- /dev/null +++ b/EvoCalculator.Core.FinanceFormulas/GoalXIRR.cs @@ -0,0 +1,23 @@ +using TridentGoalSeek; + +namespace EvoCalculator.Core.FinanceFormulas +{ + public class GoalXIRR : IGoalSeekAlgorithm + { + private readonly int x; + + public GoalXIRR(int x) + { + this.x = x; + } + + public decimal Calculate(decimal inputVariable) + { + // Value[2]+=10; + // ComputeValues + // XIRR + // return XIRR + return inputVariable * this.x; + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Models/Calculation/Interfaces/IColumn.cs b/EvoCalculator.Core.Models/Calculation/Interfaces/IColumn.cs deleted file mode 100644 index 52b4c04..0000000 --- a/EvoCalculator.Core.Models/Calculation/Interfaces/IColumn.cs +++ /dev/null @@ -1,12 +0,0 @@ -using EvoCalculator.Core.Models.Calculation.Models.Prepared; - -namespace EvoCalculator.Core.Models.Calculation.Interfaces -{ - public interface IColumn - { - public T[] Values { get; set; } - // public void ComputeValues(PreparedValues preparedValues, PreparedPayments preparedPayments, - // Constants.Calculation constants, - // params IColumn[] columns); - } -} \ No newline at end of file diff --git a/EvoCalculator.Core.Models/Calculation/Interfaces/IColumnWithXIRR.cs b/EvoCalculator.Core.Models/Calculation/Interfaces/IColumnWithXIRR.cs deleted file mode 100644 index 61a64c4..0000000 --- a/EvoCalculator.Core.Models/Calculation/Interfaces/IColumnWithXIRR.cs +++ /dev/null @@ -1,12 +0,0 @@ -using System; -using EvoCalculator.Core.Models.Calculation.Models; - -namespace EvoCalculator.Core.Models.Calculation.Interfaces -{ - public interface IColumnWithXIRR : IColumn - { - public double IRR { get; set; } - public DateTime[] Dates { get; set; } - public void ComputeXIRR(); - } -} \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateColumnTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/DateColumnTests.cs similarity index 91% rename from EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateColumnTests.cs rename to EvoCalculator.Core.Tests/Calculation/Columns/DateColumnTests.cs index 1909d8a..27a6c19 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateColumnTests.cs +++ b/EvoCalculator.Core.Tests/Calculation/Columns/DateColumnTests.cs @@ -4,15 +4,12 @@ using EvoCalculator.Core.Models.Calculation.Models.Prepared; using Xunit; using Xunit.Abstractions; -namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns +namespace EvoCalculator.Core.Tests.Calculation.Columns { public class DateColumnTests { - private readonly ITestOutputHelper output; - public DateColumnTests(ITestOutputHelper output) { - this.output = output; } @@ -29,9 +26,9 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var dateColumn = new DateColumn(preparedValues.Nmper + 1); - dateColumn.ComputeValues(preparedValues, null, new Constants.Calculation(), null); + dateColumn.ComputeValues(preparedValues, new Constants.Calculation()); - var expected = new DateTime[] + var expected = new[] { new DateTime(2018, 10, 28), new DateTime(2018, 10, 28), @@ -83,9 +80,9 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var dateColumn = new DateColumn(preparedValues.Nmper + 1); - dateColumn.ComputeValues(preparedValues, null, new Constants.Calculation(), null); + dateColumn.ComputeValues(preparedValues, new Constants.Calculation()); - var expected = new DateTime[] + var expected = new[] { new DateTime(2018, 10, 28), new DateTime(2018, 10, 28), @@ -137,9 +134,9 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var dateColumn = new DateColumn(preparedValues.Nmper + 1); - dateColumn.ComputeValues(preparedValues, null, new Constants.Calculation(), null); + dateColumn.ComputeValues(preparedValues, new Constants.Calculation()); - var expected = new DateTime[] + var expected = new[] { new DateTime(2018, 10, 28), new DateTime(2018, 10, 28), @@ -191,9 +188,9 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var dateColumn = new DateColumn(preparedValues.Nmper + 1); - dateColumn.ComputeValues(preparedValues, null, new Constants.Calculation(), null); + dateColumn.ComputeValues(preparedValues, new Constants.Calculation()); - var expected = new DateTime[] + var expected = new[] { new DateTime(2018, 10, 31), new DateTime(2018, 10, 31), diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateTempTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/DateTempTests.cs similarity index 93% rename from EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateTempTests.cs rename to EvoCalculator.Core.Tests/Calculation/Columns/DateTempTests.cs index 20d538b..5e7b8b2 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/DateTempTests.cs +++ b/EvoCalculator.Core.Tests/Calculation/Columns/DateTempTests.cs @@ -3,7 +3,7 @@ using EvoCalculator.Core.Calculation.Columns; using EvoCalculator.Core.Models.Calculation.Models.Prepared; using Xunit; -namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns +namespace EvoCalculator.Core.Tests.Calculation.Columns { public class DateTempTests { @@ -17,12 +17,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns PaymentDateNew = null, }; - var dateTempColumn = new DateTempColumn(); - dateTempColumn.ComputeValues( - preparedValues - , null - , new Constants.Calculation() - , null); + var dateTempColumn = new DateTempColumn(67); + dateTempColumn.ComputeValues(preparedValues); var expected = new DateTime[] { @@ -109,12 +105,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns PaymentDateNew = new DateTime(2018, 12, 11), }; - var dateTempColumn = new DateTempColumn(); - dateTempColumn.ComputeValues( - preparedValues - , null - , new Constants.Calculation() - , null); + var dateTempColumn = new DateTempColumn(67); + dateTempColumn.ComputeValues(preparedValues); var expected = new DateTime[] { diff --git a/EvoCalculator.Core.Tests/Calculation/Columns/GPS_GrTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/GPS_GrTests.cs new file mode 100644 index 0000000..33a0103 --- /dev/null +++ b/EvoCalculator.Core.Tests/Calculation/Columns/GPS_GrTests.cs @@ -0,0 +1,263 @@ +using System; +using EvoCalculator.Core.Calculation.Columns; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using Xunit; + +namespace EvoCalculator.Core.Tests.Calculation.Columns +{ + public class GPS_GrTests + { + [Fact] + public void GPS_GrTest1() + { + var preparedValues = + new PreparedValues() + { + IrrExpected = 0.22, + }; + + var dateTempColumn = new DateTempColumn(67) + { + Values = new[] + { + new DateTime(2020, 10, 12), + new DateTime(2020, 10, 12), + new DateTime(2020, 11, 12), + new DateTime(2020, 12, 12), + new DateTime(2021, 1, 12), + new DateTime(2021, 2, 12), + new DateTime(2021, 3, 12), + new DateTime(2021, 4, 12), + new DateTime(2021, 5, 12), + new DateTime(2021, 6, 12), + new DateTime(2021, 7, 12), + new DateTime(2021, 8, 12), + new DateTime(2021, 9, 12), + new DateTime(2021, 10, 12), + new DateTime(2021, 11, 12), + new DateTime(2021, 12, 12), + new DateTime(2022, 1, 12), + new DateTime(2022, 2, 12), + new DateTime(2022, 3, 12), + new DateTime(2022, 4, 12), + new DateTime(2022, 5, 12), + new DateTime(2022, 6, 12), + new DateTime(2022, 7, 12), + new DateTime(2022, 8, 12), + new DateTime(2022, 9, 12), + new DateTime(2022, 10, 12), + new DateTime(2022, 11, 12), + new DateTime(2022, 12, 12), + new DateTime(2023, 1, 12), + new DateTime(2023, 2, 12), + new DateTime(2023, 3, 12), + new DateTime(2023, 4, 12), + new DateTime(2023, 5, 12), + new DateTime(2023, 6, 12), + new DateTime(2023, 7, 12), + new DateTime(2023, 8, 12), + new DateTime(2023, 9, 12), + new DateTime(2023, 10, 12), + new DateTime(2023, 11, 12), + new DateTime(2023, 12, 12), + new DateTime(2024, 1, 12), + new DateTime(2024, 2, 12), + new DateTime(2024, 3, 12), + new DateTime(2024, 4, 12), + new DateTime(2024, 5, 12), + new DateTime(2024, 6, 12), + new DateTime(2024, 7, 12), + new DateTime(2024, 8, 12), + new DateTime(2024, 9, 12), + new DateTime(2024, 10, 12), + new DateTime(2024, 11, 12), + new DateTime(2024, 12, 12), + new DateTime(2025, 1, 12), + new DateTime(2025, 2, 12), + new DateTime(2025, 3, 12), + new DateTime(2025, 4, 12), + new DateTime(2025, 5, 12), + new DateTime(2025, 6, 12), + new DateTime(2025, 7, 12), + new DateTime(2025, 8, 12), + new DateTime(2025, 9, 12), + new DateTime(2025, 10, 12), + new DateTime(2025, 11, 12), + new DateTime(2025, 12, 12), + new DateTime(2026, 1, 12), + new DateTime(2026, 2, 12), + new DateTime(2026, 3, 12), + } + }; + + var preparedPayments = new PreparedPayments() + { + Rows = new[] + { + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + new PaymentRow + { + TlmCostPayment = 800.00, + }, + } + }; + + + var gpsGrColumn = new GPS_GrColumn(preparedPayments.Rows.Length + 1, dateTempColumn); + gpsGrColumn.ComputeValues(preparedPayments, preparedValues); + + var expected = new[] + { + -19068.29452640964, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + 800.00, + }; + + var res = gpsGrColumn.Values; + Assert.Equal(expected, res); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/KaskoNmperGrTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/KaskoNmperGrTests.cs similarity index 96% rename from EvoCalculator.Core.Tests/Calculation/Suite/Columns/KaskoNmperGrTests.cs rename to EvoCalculator.Core.Tests/Calculation/Columns/KaskoNmperGrTests.cs index 066da1f..30edf5f 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/KaskoNmperGrTests.cs +++ b/EvoCalculator.Core.Tests/Calculation/Columns/KaskoNmperGrTests.cs @@ -3,7 +3,7 @@ using EvoCalculator.Core.Calculation.Columns; using EvoCalculator.Core.Models.Calculation.Models.Prepared; using Xunit; -namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns +namespace EvoCalculator.Core.Tests.Calculation.Columns { public class KaskoNmperGrTests { @@ -20,7 +20,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var dateTempColumn = new DateTempColumn() + var dateTempColumn = new DateTempColumn(67) { Values = new[] { @@ -95,8 +95,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1); - kaskoNmperGrColumn.ComputeValues(preparedValues, dateTempColumn); + var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1, dateTempColumn); + kaskoNmperGrColumn.ComputeValues(preparedValues); var expected = new[] @@ -151,7 +151,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var dateTempColumn = new DateTempColumn() + var dateTempColumn = new DateTempColumn(67) { Values = new[] { @@ -226,8 +226,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1); - kaskoNmperGrColumn.ComputeValues(preparedValues, dateTempColumn); + var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1, dateTempColumn); + kaskoNmperGrColumn.ComputeValues(preparedValues); var expected = new[] @@ -268,7 +268,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var dateTempColumn = new DateTempColumn() + var dateTempColumn = new DateTempColumn(67) { Values = new[] { @@ -343,8 +343,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; - var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1); - kaskoNmperGrColumn.ComputeValues(preparedValues, dateTempColumn); + var kaskoNmperGrColumn = new KaskoNmperGrColumn(preparedValues.Nmper + 1, dateTempColumn); + kaskoNmperGrColumn.ComputeValues(preparedValues); var expected = new[] diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/PercentPaymentTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/PercentPaymentTests.cs similarity index 95% rename from EvoCalculator.Core.Tests/Calculation/Suite/Columns/PercentPaymentTests.cs rename to EvoCalculator.Core.Tests/Calculation/Columns/PercentPaymentTests.cs index 89ecaeb..d57ec69 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/PercentPaymentTests.cs +++ b/EvoCalculator.Core.Tests/Calculation/Columns/PercentPaymentTests.cs @@ -1,9 +1,8 @@ -using System; -using EvoCalculator.Core.Calculation.Columns; +using EvoCalculator.Core.Calculation.Columns; using EvoCalculator.Core.Models.Calculation.Models.Prepared; using Xunit; -namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns +namespace EvoCalculator.Core.Tests.Calculation.Columns { public class PercentPaymentTests { @@ -114,11 +113,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var percentPaymentColumn = new PercentPaymentColumn(preparedPayments.Rows.Length + 1); - percentPaymentColumn.ComputeValues( - null - , preparedPayments - , new Constants.Calculation() - , null); + percentPaymentColumn.ComputeValues(preparedPayments); var expected = new double[] { @@ -308,11 +303,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns }; var percentPaymentColumn = new PercentPaymentColumn(preparedPayments.Rows.Length + 1); - percentPaymentColumn.ComputeValues( - null - , preparedPayments - , new Constants.Calculation() - , null); + percentPaymentColumn.ComputeValues(preparedPayments); var expected = new double[] { diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/SumColumnTest.cs b/EvoCalculator.Core.Tests/Calculation/Columns/SumColumnTest.cs similarity index 92% rename from EvoCalculator.Core.Tests/Calculation/Suite/Columns/SumColumnTest.cs rename to EvoCalculator.Core.Tests/Calculation/Columns/SumColumnTest.cs index ee44e30..901e074 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/Columns/SumColumnTest.cs +++ b/EvoCalculator.Core.Tests/Calculation/Columns/SumColumnTest.cs @@ -3,9 +3,9 @@ using EvoCalculator.Core.Calculation.Columns; using EvoCalculator.Core.Models.Calculation.Models.Prepared; using Xunit; -namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns +namespace EvoCalculator.Core.Tests.Calculation.Columns { - public class SumColumnTest + public class SumColumnTests { [Fact] public void SumColumnTest1() @@ -16,7 +16,8 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns BaseCost = 2842960.70661055, FirstPaymentSum = 636000, Nmper = 30, - LastPaymentSum = 25440 + LastPaymentSum = 25440, + IrrExpected = 0.22 }; @@ -58,7 +59,7 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns } }; - var dateTempColumn = new DateTempColumn() + var dateTempColumn = new DateTempColumn(67) { Values = new[] { @@ -132,9 +133,6 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns } }; - var sumColumn = new SumColumn(preparedValues.Nmper + 1, dateTempColumn); - sumColumn.ComputeValues(preparedValues, percentPaymentColumn); - var expected = new[] { @@ -171,12 +169,14 @@ namespace EvoCalculator.Core.Tests.Calculation.Suite.Columns 25440, }; + var sumColumn = new SumColumn( + preparedValues.Nmper + 1 + , dateTempColumn + , preparedValues + , percentPaymentColumn); + sumColumn.ComputeValues(Convert.ToDecimal(preparedValues.IrrExpected)); var values = sumColumn.Values; Assert.Equal(expected, values); - - - sumColumn.ComputeXIRR(); - Assert.InRange(sumColumn.IRR, 0.220325, 0.220327); } } } \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Columns/SumWithVatTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/SumWithVatTests.cs new file mode 100644 index 0000000..a337f5a --- /dev/null +++ b/EvoCalculator.Core.Tests/Calculation/Columns/SumWithVatTests.cs @@ -0,0 +1,101 @@ +using EvoCalculator.Core.Calculation.Columns; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using Xunit; + +namespace EvoCalculator.Core.Tests.Calculation.Columns +{ + public class SumWithVatTests + { + [Fact] + public void SumWithVatTest1() + { + var preparedValues = new PreparedValues + { + FirstPaymentWithNdsAbs = 50000, + Nmper = 30, + }; + + var dateTempColumn = new DateTempColumn(67); + + var sumColumn = new SumColumn(preparedValues.Nmper + 1, dateTempColumn, null, null) + { + Values = new[] + { + -2542903.66268442, + 625000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 25000.00 + } + }; + + + var sumWithVatColumn = new SumWithVATColumn(preparedValues.Nmper + 1); + sumWithVatColumn.ComputeValues(new Constants.Calculation(), sumColumn, preparedValues); + + var expected = new[] + { + 4112000.00, + 50000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 144000.00, + 30000.00, + }; + + var res = sumWithVatColumn.Values; + Assert.Equal(expected, res); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Columns/TLM_GrTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/TLM_GrTests.cs new file mode 100644 index 0000000..a9c2204 --- /dev/null +++ b/EvoCalculator.Core.Tests/Calculation/Columns/TLM_GrTests.cs @@ -0,0 +1,263 @@ +using System; +using EvoCalculator.Core.Calculation.Columns; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using Xunit; + +namespace EvoCalculator.Core.Tests.Calculation.Columns +{ + public class TLM_GrTests + { + [Fact] + public void TLM_GrTest1() + { + var preparedValues = + new PreparedValues() + { + IrrExpected = 0.22, + }; + + var dateTempColumn = new DateTempColumn(67) + { + Values = new[] + { + new DateTime(2020, 10, 12), + new DateTime(2020, 10, 12), + new DateTime(2020, 11, 12), + new DateTime(2020, 12, 12), + new DateTime(2021, 1, 12), + new DateTime(2021, 2, 12), + new DateTime(2021, 3, 12), + new DateTime(2021, 4, 12), + new DateTime(2021, 5, 12), + new DateTime(2021, 6, 12), + new DateTime(2021, 7, 12), + new DateTime(2021, 8, 12), + new DateTime(2021, 9, 12), + new DateTime(2021, 10, 12), + new DateTime(2021, 11, 12), + new DateTime(2021, 12, 12), + new DateTime(2022, 1, 12), + new DateTime(2022, 2, 12), + new DateTime(2022, 3, 12), + new DateTime(2022, 4, 12), + new DateTime(2022, 5, 12), + new DateTime(2022, 6, 12), + new DateTime(2022, 7, 12), + new DateTime(2022, 8, 12), + new DateTime(2022, 9, 12), + new DateTime(2022, 10, 12), + new DateTime(2022, 11, 12), + new DateTime(2022, 12, 12), + new DateTime(2023, 1, 12), + new DateTime(2023, 2, 12), + new DateTime(2023, 3, 12), + new DateTime(2023, 4, 12), + new DateTime(2023, 5, 12), + new DateTime(2023, 6, 12), + new DateTime(2023, 7, 12), + new DateTime(2023, 8, 12), + new DateTime(2023, 9, 12), + new DateTime(2023, 10, 12), + new DateTime(2023, 11, 12), + new DateTime(2023, 12, 12), + new DateTime(2024, 1, 12), + new DateTime(2024, 2, 12), + new DateTime(2024, 3, 12), + new DateTime(2024, 4, 12), + new DateTime(2024, 5, 12), + new DateTime(2024, 6, 12), + new DateTime(2024, 7, 12), + new DateTime(2024, 8, 12), + new DateTime(2024, 9, 12), + new DateTime(2024, 10, 12), + new DateTime(2024, 11, 12), + new DateTime(2024, 12, 12), + new DateTime(2025, 1, 12), + new DateTime(2025, 2, 12), + new DateTime(2025, 3, 12), + new DateTime(2025, 4, 12), + new DateTime(2025, 5, 12), + new DateTime(2025, 6, 12), + new DateTime(2025, 7, 12), + new DateTime(2025, 8, 12), + new DateTime(2025, 9, 12), + new DateTime(2025, 10, 12), + new DateTime(2025, 11, 12), + new DateTime(2025, 12, 12), + new DateTime(2026, 1, 12), + new DateTime(2026, 2, 12), + new DateTime(2026, 3, 12), + } + }; + + var preparedPayments = new PreparedPayments() + { + Rows = new[] + { + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + new PaymentRow + { + TlmCostPayment = 1000.00, + }, + } + }; + + + var tlmGrColumn = new TLM_GrColumn(preparedPayments.Rows.Length + 1, dateTempColumn); + tlmGrColumn.ComputeValues(preparedPayments, preparedValues); + + var expected = new[] + { + -23835.36815801206, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + 1000.00, + }; + + var res = tlmGrColumn.Values; + Assert.Equal(expected, res); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Columns/VATColumnTests.cs b/EvoCalculator.Core.Tests/Calculation/Columns/VATColumnTests.cs new file mode 100644 index 0000000..a14d7af --- /dev/null +++ b/EvoCalculator.Core.Tests/Calculation/Columns/VATColumnTests.cs @@ -0,0 +1,102 @@ +using EvoCalculator.Core.Calculation.Columns; +using EvoCalculator.Core.Models.Calculation.Models.Prepared; +using Xunit; + +namespace EvoCalculator.Core.Tests.Calculation.Columns +{ + public class VATColumnTests + { + [Fact] + public void VATColumnTest1() + { + var preparedValues = new PreparedValues + { + FirstPaymentNdsAbs = 0, + Nmper = 30, + }; + + var dateTempColumn = new DateTempColumn(67); + + var sumColumn = new SumColumn(preparedValues.Nmper + 1, dateTempColumn, null, null) + { + Values = new[] + { + -2542903.66268442, + 625000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 120000.00, + 25000.00 + } + }; + + + var vatColumn = new VATColumn(preparedValues.Nmper + 1); + vatColumn.ComputeValues(new Constants.Calculation(), sumColumn, preparedValues); + + var expected = new[] + { + 802000, + 125000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 24000.00, + 5000.00, + }; + + var res = vatColumn.Values; + + Assert.Equal(expected, res); + } + } +} \ No newline at end of file diff --git a/EvoCalculator.Core.Tests/Calculation/Suite/FinanceFormulasTests.cs b/EvoCalculator.Core.Tests/Calculation/FinanceFormulas/FinanceFormulasTests.cs similarity index 99% rename from EvoCalculator.Core.Tests/Calculation/Suite/FinanceFormulasTests.cs rename to EvoCalculator.Core.Tests/Calculation/FinanceFormulas/FinanceFormulasTests.cs index 022f0ab..75d0d7e 100644 --- a/EvoCalculator.Core.Tests/Calculation/Suite/FinanceFormulasTests.cs +++ b/EvoCalculator.Core.Tests/Calculation/FinanceFormulas/FinanceFormulasTests.cs @@ -2,6 +2,7 @@ using System.Linq; using EvoCalculator.Core.FinanceFormulas; using EvoCalculator.Core.Models.Calculation.Models; +using TridentGoalSeek; using Xunit; using Xunit.Abstractions;