sonarcloud workflow

This commit is contained in:
Miha Kralj
2024-10-05 23:51:52 -07:00
parent b6ecf537cd
commit 2dd680e9ee
15 changed files with 1389 additions and 1267 deletions
+47 -45
View File
@@ -1,22 +1,25 @@
using Xunit;
using System.Reflection;
using System.Diagnostics.CodeAnalysis;
namespace QuanTAlib
namespace QuanTAlib;
[SuppressMessage("Security", "SCS0005:Weak random number generator.", Justification = "Acceptable for tests")]
public class IndicatorTests
{
public class IndicatorTests
private readonly Random rnd;
private const int SeriesLen = 1000;
private const int Corrections = 100;
public IndicatorTests()
{
private readonly Random rnd;
private const int SeriesLen = 1000;
private const int Corrections = 100;
rnd = new Random((int)DateTime.Now.Ticks);
}
public IndicatorTests()
{
rnd = new Random((int)DateTime.Now.Ticks);
}
private static readonly iTValue[] indicators =
[
new Ema(period: 10, useSma: true),
private static readonly iTValue[] indicators =
[
new Ema(period: 10, useSma: true),
new Alma(period: 14, offset: 0.85, sigma: 6),
new Afirma(periods: 4, taps: 4, window: Afirma.WindowType.Blackman),
new Convolution(new double[] { 1.0, 2, 3, 2, 1 }),
@@ -57,42 +60,41 @@ namespace QuanTAlib
new Variance(period: 14),
new Zscore(period: 14)
];
];
[Theory]
[MemberData(nameof(GetIndicators))]
public void IndicatorIsNew(iTValue indicator)
[Theory]
[MemberData(nameof(GetIndicators))]
public void IndicatorIsNew(iTValue indicator)
{
var indicator1 = indicator;
var indicator2 = indicator;
MethodInfo calcMethod = indicator.GetType().GetMethod("Calc")!;
if (calcMethod == null)
{
var indicator1 = indicator;
var indicator2 = indicator;
MethodInfo calcMethod = indicator.GetType().GetMethod("Calc")!;
if (calcMethod == null)
{
throw new Exception($"Calc method not found for indicator type: {indicator.GetType().Name}");
}
for (int i = 0; i < SeriesLen; i++)
{
TValue item1 = new(Time: DateTime.Now, Value: rnd.Next(-100, 100), IsNew: true);
calcMethod.Invoke(indicator1, new object[] { item1 });
for (int j = 0; j < Corrections; j++)
{
item1 = new(Time: DateTime.Now, Value: rnd.Next(-100, 100), IsNew: false);
calcMethod.Invoke(indicator1, new object[] { item1 });
}
var item2 = new TValue(item1.Time, item1.Value, IsNew: true);
calcMethod.Invoke(indicator2, new object[] { item2 });
Assert.Equal(indicator1.Value, indicator2.Value);
}
throw new Exception($"Calc method not found for indicator type: {indicator.GetType().Name}");
}
public static IEnumerable<object[]> GetIndicators()
for (int i = 0; i < SeriesLen; i++)
{
return indicators.Select(indicator => new object[] { indicator });
TValue item1 = new(Time: DateTime.Now, Value: rnd.Next(-100, 100), IsNew: true);
calcMethod.Invoke(indicator1, new object[] { item1 });
for (int j = 0; j < Corrections; j++)
{
item1 = new(Time: DateTime.Now, Value: rnd.Next(-100, 100), IsNew: false);
calcMethod.Invoke(indicator1, new object[] { item1 });
}
var item2 = new TValue(item1.Time, item1.Value, IsNew: true);
calcMethod.Invoke(indicator2, new object[] { item2 });
Assert.Equal(indicator1.Value, indicator2.Value);
}
}
}
public static IEnumerable<object[]> GetIndicators()
{
return indicators.Select(indicator => new object[] { indicator });
}
}