From f9af5aa84426161322c9f53f1eb55125e3727e6b Mon Sep 17 00:00:00 2001 From: Phil Date: Mon, 4 Mar 2013 19:37:43 -0800 Subject: [PATCH] Added GainLossRatio Tests Added tests around GainLossRatio, but I still have some questions as noted per test. --- .../FinancialTests/GainLossRatioTests.cs | 82 ++++++++++++++++++- src/UnitTests/UnitTests.csproj | 1 + 2 files changed, 82 insertions(+), 1 deletion(-) diff --git a/src/UnitTests/FinancialTests/GainLossRatioTests.cs b/src/UnitTests/FinancialTests/GainLossRatioTests.cs index 89b0aebb..d3c3536b 100644 --- a/src/UnitTests/FinancialTests/GainLossRatioTests.cs +++ b/src/UnitTests/FinancialTests/GainLossRatioTests.cs @@ -29,7 +29,6 @@ namespace MathNet.Numerics.UnitTests.FinancialTests using System; using System.Collections.Generic; using System.Linq; - using System.Text; using MathNet.Numerics.Financial; using MathNet.Numerics.Statistics; using NUnit.Framework; @@ -37,6 +36,87 @@ namespace MathNet.Numerics.UnitTests.FinancialTests [TestFixture] public class GainLossRatioTests { + [Test] + [ExpectedException(typeof(ArgumentNullException))] //assert + public void throws_when_input_data_is_null() + { + //arrange + List inputData = null; + //act + inputData.GainLossRatio(); + } + [Test] + //Not sure this is correct. Undefined may be more correct. + public void returns_zero_for_a_single_positive_input() + { + //arrange + var inputData = new[] { 1.0 }; + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(0.0, gainLossRatio); + } + + [Test] + //Not sure this is correct. Undefined may be more correct. + public void returns_zero_for_a_single_negative_input() + { + //arrange + var inputData = new[] { -1.0 }; + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(0.0, gainLossRatio); + } + + [Test] + //Not sure this is correct. Undefined may be more correct. + public void returns_zero_for_a_set_of_all_positive_numbers() + { + //arrange + var inputData = new[] { 1.0, 2.0, 3.0 }; + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(0.0, gainLossRatio); + } + + [Test] + //Not sure this is correct. Undefined may be more correct. + public void returns_zero_for_a_set_of_all_negative_numbers() + { + //arrange + var inputData = new[] { -1.0, -2.0, -3.0 }; + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(0.0, gainLossRatio); + } + + [Test] + public void handles_a_value_of_zero_as_a_positive() + { + //arrange + var inputData = new[] { 0.0, 1.0, 2.0 }; + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(0.0, gainLossRatio); + } + + [Test] + public void calculates_the_correct_ratio_given_a_set_of_gains_and_losses() + { + //arrange + var inputData = new[] { -2.0, -1.0, 0.0, 1.0, 2.0 }; + var meanOfGains = inputData.Where(x => x >= 0).Mean(); + var meanOfLosses = inputData.Where(x => x < 0).Mean(); + var expectedRatio = Math.Abs(meanOfGains / meanOfLosses); + //act + var gainLossRatio = inputData.GainLossRatio(); + //assert + Assert.AreEqual(expectedRatio, gainLossRatio); + } } } diff --git a/src/UnitTests/UnitTests.csproj b/src/UnitTests/UnitTests.csproj index 257c5945..51cb7a03 100644 --- a/src/UnitTests/UnitTests.csproj +++ b/src/UnitTests/UnitTests.csproj @@ -123,6 +123,7 @@ +