refactor: update NoWarn codes and clean up EmaValidationTests and EmaVectorTests

This commit is contained in:
Miha Kralj
2025-11-29 17:04:56 -08:00
parent 622e4c8ae6
commit ce77bc9c85
3 changed files with 67 additions and 72 deletions
+1 -1
View File
@@ -44,7 +44,7 @@
</PropertyGroup> </PropertyGroup>
<PropertyGroup> <PropertyGroup>
<NoWarn>S1944,S2053,S2222,S2259,S2583,S2589,S3329,S3655,S3776,S3900,S3949,S3966,S4158,S4347,S5773,S6781</NoWarn> <NoWarn>S1144,S1944,S2053,S2222,S2259,S2583,S2589,S3329,S3655,S3776,S3900,S3949,S3966,S4158,S4347,S5773,S6781</NoWarn>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
+1 -6
View File
@@ -10,7 +10,7 @@ using QuanTAlib;
namespace QuanTAlib.Tests; namespace QuanTAlib.Tests;
public class EmaValidationTests : IDisposable public class EmaValidationTests
{ {
private readonly TBarSeries _bars; private readonly TBarSeries _bars;
private readonly TSeries _data; private readonly TSeries _data;
@@ -45,11 +45,6 @@ public class EmaValidationTests : IDisposable
} }
} }
public void Dispose()
{
// Cleanup if needed
}
[Fact] [Fact]
public void Validate_Skender() public void Validate_Skender()
{ {
+65 -65
View File
@@ -12,9 +12,9 @@ public class EmaVectorTests
{ {
int[] periods = { 10, 20 }; int[] periods = { 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(100.0, res[0].Value, 1e-9); Assert.Equal(100.0, res[0].Value, 1e-9);
Assert.Equal(100.0, res[1].Value, 1e-9); Assert.Equal(100.0, res[1].Value, 1e-9);
} }
@@ -24,9 +24,9 @@ public class EmaVectorTests
{ {
double[] alphas = { 0.1, 0.2, 0.5 }; double[] alphas = { 0.1, 0.2, 0.5 };
var emaVector = new EmaVector(alphas); var emaVector = new EmaVector(alphas);
var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(3, res.Length); Assert.Equal(3, res.Length);
Assert.Equal(100.0, res[0].Value, 1e-9); Assert.Equal(100.0, res[0].Value, 1e-9);
Assert.Equal(100.0, res[1].Value, 1e-9); Assert.Equal(100.0, res[1].Value, 1e-9);
@@ -37,40 +37,40 @@ public class EmaVectorTests
public void Initialization_WithZeroPeriod_ThrowsArgumentException() public void Initialization_WithZeroPeriod_ThrowsArgumentException()
{ {
int[] periods = { 10, 0, 20 }; int[] periods = { 10, 0, 20 };
Assert.Throws<ArgumentException>(() => new EmaVector(periods)); Assert.Throws<ArgumentOutOfRangeException>(() => new EmaVector(periods));
} }
[Fact] [Fact]
public void Initialization_WithNegativePeriod_ThrowsArgumentException() public void Initialization_WithNegativePeriod_ThrowsArgumentException()
{ {
int[] periods = { 10, -5, 20 }; int[] periods = { 10, -5, 20 };
Assert.Throws<ArgumentException>(() => new EmaVector(periods)); Assert.Throws<ArgumentOutOfRangeException>(() => new EmaVector(periods));
} }
[Fact] [Fact]
public void Initialization_WithZeroAlpha_ThrowsArgumentException() public void Initialization_WithZeroAlpha_ThrowsArgumentException()
{ {
double[] alphas = { 0.1, 0.0, 0.5 }; double[] alphas = { 0.1, 0.0, 0.5 };
Assert.Throws<ArgumentException>(() => new EmaVector(alphas)); Assert.Throws<ArgumentOutOfRangeException>(() => new EmaVector(alphas));
} }
[Fact] [Fact]
public void Initialization_WithNegativeAlpha_ThrowsArgumentException() public void Initialization_WithNegativeAlpha_ThrowsArgumentException()
{ {
double[] alphas = { 0.1, -0.1, 0.5 }; double[] alphas = { 0.1, -0.1, 0.5 };
Assert.Throws<ArgumentException>(() => new EmaVector(alphas)); Assert.Throws<ArgumentOutOfRangeException>(() => new EmaVector(alphas));
} }
[Fact] [Fact]
public void Initialization_WithAlphaGreaterThanOne_ThrowsArgumentException() public void Initialization_WithAlphaGreaterThanOne_ThrowsArgumentException()
{ {
double[] alphas = { 0.1, 1.5, 0.5 }; double[] alphas = { 0.1, 1.5, 0.5 };
Assert.Throws<ArgumentException>(() => new EmaVector(alphas)); Assert.Throws<ArgumentOutOfRangeException>(() => new EmaVector(alphas));
} }
[Fact] [Fact]
@@ -78,9 +78,9 @@ public class EmaVectorTests
{ {
double[] alphas = { 0.1, 1.0, 0.5 }; double[] alphas = { 0.1, 1.0, 0.5 };
var emaVector = new EmaVector(alphas); var emaVector = new EmaVector(alphas);
var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(3, res.Length); Assert.Equal(3, res.Length);
} }
@@ -90,7 +90,7 @@ public class EmaVectorTests
int[] periods = { 5, 10, 20 }; int[] periods = { 5, 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var emaSingles = periods.Select(p => new Ema(p)).ToArray(); var emaSingles = periods.Select(p => new Ema(p)).ToArray();
var values = new double[] { 10, 20, 30, 40, 50, 40, 30, 20, 10 }; var values = new double[] { 10, 20, 30, 40, 50, 40, 30, 20, 10 };
var time = DateTime.UtcNow; var time = DateTime.UtcNow;
@@ -98,14 +98,14 @@ public class EmaVectorTests
{ {
var tVal = new TValue(time, val); var tVal = new TValue(time, val);
var multiRes = emaVector.Update(tVal); var multiRes = emaVector.Update(tVal);
for (int i = 0; i < periods.Length; i++) for (int i = 0; i < periods.Length; i++)
{ {
var singleRes = emaSingles[i].Update(tVal); var singleRes = emaSingles[i].Update(tVal);
Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9); Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9);
Assert.Equal(singleRes.Time, multiRes[i].Time); Assert.Equal(singleRes.Time, multiRes[i].Time);
} }
time = time.AddMinutes(1); time = time.AddMinutes(1);
} }
} }
@@ -116,26 +116,26 @@ public class EmaVectorTests
int[] periods = { 5, 10, 20 }; int[] periods = { 5, 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var emaSingles = periods.Select(p => new Ema(p)).ToArray(); var emaSingles = periods.Select(p => new Ema(p)).ToArray();
int len = 100; int len = 100;
var t = new System.Collections.Generic.List<long>(len); var t = new System.Collections.Generic.List<long>(len);
var v = new System.Collections.Generic.List<double>(len); var v = new System.Collections.Generic.List<double>(len);
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
t.Add(now.AddMinutes(i).Ticks); t.Add(now.AddMinutes(i).Ticks);
v.Add(Math.Sin(i * 0.1) * 100); v.Add(Math.Sin(i * 0.1) * 100);
} }
var series = new TSeries(t, v); var series = new TSeries(t, v);
var multiRes = emaVector.Calculate(series); var multiRes = emaVector.Calculate(series);
for (int i = 0; i < periods.Length; i++) for (int i = 0; i < periods.Length; i++)
{ {
var singleRes = emaSingles[i].Update(series); var singleRes = emaSingles[i].Update(series);
Assert.Equal(singleRes.Count, multiRes[i].Count); Assert.Equal(singleRes.Count, multiRes[i].Count);
for (int j = 0; j < len; j++) for (int j = 0; j < len; j++)
{ {
@@ -150,27 +150,27 @@ public class EmaVectorTests
int[] periods = { 5, 10, 20 }; int[] periods = { 5, 10, 20 };
var emaVectorBatch = new EmaVector(periods); var emaVectorBatch = new EmaVector(periods);
var emaVectorStream = new EmaVector(periods); var emaVectorStream = new EmaVector(periods);
int len = 100; int len = 100;
var t = new System.Collections.Generic.List<long>(len); var t = new System.Collections.Generic.List<long>(len);
var v = new System.Collections.Generic.List<double>(len); var v = new System.Collections.Generic.List<double>(len);
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
t.Add(now.AddMinutes(i).Ticks); t.Add(now.AddMinutes(i).Ticks);
v.Add(Math.Sin(i * 0.1) * 100); v.Add(Math.Sin(i * 0.1) * 100);
} }
var series = new TSeries(t, v); var series = new TSeries(t, v);
var batchRes = emaVectorBatch.Calculate(series); var batchRes = emaVectorBatch.Calculate(series);
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
var tVal = new TValue(new DateTime(t[i]), v[i]); var tVal = new TValue(new DateTime(t[i]), v[i]);
var streamRes = emaVectorStream.Update(tVal); var streamRes = emaVectorStream.Update(tVal);
for (int j = 0; j < periods.Length; j++) for (int j = 0; j < periods.Length; j++)
{ {
Assert.Equal(batchRes[j].Values[i], streamRes[j].Value, 1e-9); Assert.Equal(batchRes[j].Values[i], streamRes[j].Value, 1e-9);
@@ -182,25 +182,25 @@ public class EmaVectorTests
public void Calculate_Static_MatchesInstanceMethod() public void Calculate_Static_MatchesInstanceMethod()
{ {
int[] periods = { 5, 10, 20 }; int[] periods = { 5, 10, 20 };
int len = 50; int len = 50;
var t = new System.Collections.Generic.List<long>(len); var t = new System.Collections.Generic.List<long>(len);
var v = new System.Collections.Generic.List<double>(len); var v = new System.Collections.Generic.List<double>(len);
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
for (int i = 0; i < len; i++) for (int i = 0; i < len; i++)
{ {
t.Add(now.AddMinutes(i).Ticks); t.Add(now.AddMinutes(i).Ticks);
v.Add(Math.Sin(i * 0.1) * 100); v.Add(Math.Sin(i * 0.1) * 100);
} }
var series = new TSeries(t, v); var series = new TSeries(t, v);
var instanceEma = new EmaVector(periods); var instanceEma = new EmaVector(periods);
var instanceRes = instanceEma.Calculate(series); var instanceRes = instanceEma.Calculate(series);
var staticRes = EmaVector.Calculate(series, periods); var staticRes = EmaVector.Calculate(series, periods);
for (int i = 0; i < periods.Length; i++) for (int i = 0; i < periods.Length; i++)
{ {
Assert.Equal(instanceRes[i].Count, staticRes[i].Count); Assert.Equal(instanceRes[i].Count, staticRes[i].Count);
@@ -210,18 +210,18 @@ public class EmaVectorTests
} }
} }
} }
[Fact] [Fact]
public void Reset_ClearsState() public void Reset_ClearsState()
{ {
int[] periods = { 10 }; int[] periods = { 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
emaVector.Reset(); emaVector.Reset();
var res = emaVector.Update(new TValue(DateTime.UtcNow, 200.0)); var res = emaVector.Update(new TValue(DateTime.UtcNow, 200.0));
Assert.Equal(200.0, res[0].Value, 1e-9); Assert.Equal(200.0, res[0].Value, 1e-9);
} }
@@ -230,12 +230,12 @@ public class EmaVectorTests
{ {
int[] periods = { 10, 20 }; int[] periods = { 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0));
var resultAfterNaN = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); var resultAfterNaN = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
foreach (var result in resultAfterNaN) foreach (var result in resultAfterNaN)
{ {
Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}"); Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}");
@@ -247,16 +247,16 @@ public class EmaVectorTests
{ {
int[] periods = { 10, 20 }; int[] periods = { 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0));
var resultAfterPosInf = emaVector.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity)); var resultAfterPosInf = emaVector.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity));
foreach (var result in resultAfterPosInf) foreach (var result in resultAfterPosInf)
{ {
Assert.True(double.IsFinite(result.Value)); Assert.True(double.IsFinite(result.Value));
} }
var resultAfterNegInf = emaVector.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity)); var resultAfterNegInf = emaVector.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity));
foreach (var result in resultAfterNegInf) foreach (var result in resultAfterNegInf)
{ {
@@ -269,15 +269,15 @@ public class EmaVectorTests
{ {
int[] periods = { 5, 10 }; int[] periods = { 5, 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0));
emaVector.Update(new TValue(DateTime.UtcNow, 120.0)); emaVector.Update(new TValue(DateTime.UtcNow, 120.0));
var r1 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); var r1 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
var r2 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); var r2 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
var r3 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); var r3 = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
foreach (var result in r1) Assert.True(double.IsFinite(result.Value)); foreach (var result in r1) Assert.True(double.IsFinite(result.Value));
foreach (var result in r2) Assert.True(double.IsFinite(result.Value)); foreach (var result in r2) Assert.True(double.IsFinite(result.Value));
foreach (var result in r3) Assert.True(double.IsFinite(result.Value)); foreach (var result in r3) Assert.True(double.IsFinite(result.Value));
@@ -288,21 +288,21 @@ public class EmaVectorTests
{ {
int[] periods = { 5, 10 }; int[] periods = { 5, 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var t = new System.Collections.Generic.List<long>(); var t = new System.Collections.Generic.List<long>();
var v = new System.Collections.Generic.List<double>(); var v = new System.Collections.Generic.List<double>();
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
t.Add(now.Ticks); v.Add(100.0); t.Add(now.Ticks); v.Add(100.0);
t.Add(now.AddMinutes(1).Ticks); v.Add(110.0); t.Add(now.AddMinutes(1).Ticks); v.Add(110.0);
t.Add(now.AddMinutes(2).Ticks); v.Add(double.NaN); t.Add(now.AddMinutes(2).Ticks); v.Add(double.NaN);
t.Add(now.AddMinutes(3).Ticks); v.Add(120.0); t.Add(now.AddMinutes(3).Ticks); v.Add(120.0);
t.Add(now.AddMinutes(4).Ticks); v.Add(double.PositiveInfinity); t.Add(now.AddMinutes(4).Ticks); v.Add(double.PositiveInfinity);
t.Add(now.AddMinutes(5).Ticks); v.Add(130.0); t.Add(now.AddMinutes(5).Ticks); v.Add(130.0);
var series = new TSeries(t, v); var series = new TSeries(t, v);
var results = emaVector.Calculate(series); var results = emaVector.Calculate(series);
foreach (var periodResults in results) foreach (var periodResults in results)
{ {
foreach (var val in periodResults.Values) foreach (var val in periodResults.Values)
@@ -317,12 +317,12 @@ public class EmaVectorTests
{ {
int[] periods = { 10 }; int[] periods = { 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); emaVector.Update(new TValue(DateTime.UtcNow, double.NaN));
emaVector.Reset(); emaVector.Reset();
var result = emaVector.Update(new TValue(DateTime.UtcNow, 50.0)); var result = emaVector.Update(new TValue(DateTime.UtcNow, 50.0));
Assert.Equal(50.0, result[0].Value, 1e-9); Assert.Equal(50.0, result[0].Value, 1e-9);
} }
@@ -333,7 +333,7 @@ public class EmaVectorTests
int[] periods = { 5, 10, 20 }; int[] periods = { 5, 10, 20 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var emaSingles = periods.Select(p => new Ema(p)).ToArray(); var emaSingles = periods.Select(p => new Ema(p)).ToArray();
var values = new double[] { 10, 20, double.NaN, 40, double.PositiveInfinity, 60, 70 }; var values = new double[] { 10, 20, double.NaN, 40, double.PositiveInfinity, 60, 70 };
var time = DateTime.UtcNow; var time = DateTime.UtcNow;
@@ -341,13 +341,13 @@ public class EmaVectorTests
{ {
var tVal = new TValue(time, val); var tVal = new TValue(time, val);
var multiRes = emaVector.Update(tVal); var multiRes = emaVector.Update(tVal);
for (int i = 0; i < periods.Length; i++) for (int i = 0; i < periods.Length; i++)
{ {
var singleRes = emaSingles[i].Update(tVal); var singleRes = emaSingles[i].Update(tVal);
Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9); Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9);
} }
time = time.AddMinutes(1); time = time.AddMinutes(1);
} }
} }
@@ -357,9 +357,9 @@ public class EmaVectorTests
{ {
int[] periods = { 5, 10 }; int[] periods = { 5, 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var result = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); var result = emaVector.Update(new TValue(DateTime.UtcNow, 100.0));
Assert.Equal(result[0].Value, emaVector.Values[0].Value); Assert.Equal(result[0].Value, emaVector.Values[0].Value);
Assert.Equal(result[1].Value, emaVector.Values[1].Value); Assert.Equal(result[1].Value, emaVector.Values[1].Value);
} }
@@ -369,13 +369,13 @@ public class EmaVectorTests
{ {
int[] periods = { 5, 10 }; int[] periods = { 5, 10 };
var emaVector = new EmaVector(periods); var emaVector = new EmaVector(periods);
var t = new System.Collections.Generic.List<long> { 100, 200, 300 }; var t = new System.Collections.Generic.List<long> { 100, 200, 300 };
var v = new System.Collections.Generic.List<double> { 10.0, 20.0, 30.0 }; var v = new System.Collections.Generic.List<double> { 10.0, 20.0, 30.0 };
var series = new TSeries(t, v); var series = new TSeries(t, v);
var results = emaVector.Calculate(series); var results = emaVector.Calculate(series);
Assert.Equal(results[0].Last.Value, emaVector.Values[0].Value, 1e-9); Assert.Equal(results[0].Last.Value, emaVector.Values[0].Value, 1e-9);
Assert.Equal(results[1].Last.Value, emaVector.Values[1].Value, 1e-9); Assert.Equal(results[1].Last.Value, emaVector.Values[1].Value, 1e-9);
} }