From ce77bc9c85e4fe97c9b584b5f3af0e50a01e5acb Mon Sep 17 00:00:00 2001 From: Miha Kralj Date: Sat, 29 Nov 2025 17:04:56 -0800 Subject: [PATCH] refactor: update NoWarn codes and clean up EmaValidationTests and EmaVectorTests --- Directory.Build.props | 2 +- lib/averages/ema/Ema.Validation.Tests.cs | 7 +- lib/averages/ema/EmaVector.Tests.cs | 130 +++++++++++------------ 3 files changed, 67 insertions(+), 72 deletions(-) diff --git a/Directory.Build.props b/Directory.Build.props index 76501f5f..f4ab4530 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -44,7 +44,7 @@ - S1944,S2053,S2222,S2259,S2583,S2589,S3329,S3655,S3776,S3900,S3949,S3966,S4158,S4347,S5773,S6781 + S1144,S1944,S2053,S2222,S2259,S2583,S2589,S3329,S3655,S3776,S3900,S3949,S3966,S4158,S4347,S5773,S6781 diff --git a/lib/averages/ema/Ema.Validation.Tests.cs b/lib/averages/ema/Ema.Validation.Tests.cs index c8e59e63..3bdac94f 100644 --- a/lib/averages/ema/Ema.Validation.Tests.cs +++ b/lib/averages/ema/Ema.Validation.Tests.cs @@ -10,7 +10,7 @@ using QuanTAlib; namespace QuanTAlib.Tests; -public class EmaValidationTests : IDisposable +public class EmaValidationTests { private readonly TBarSeries _bars; private readonly TSeries _data; @@ -45,11 +45,6 @@ public class EmaValidationTests : IDisposable } } - public void Dispose() - { - // Cleanup if needed - } - [Fact] public void Validate_Skender() { diff --git a/lib/averages/ema/EmaVector.Tests.cs b/lib/averages/ema/EmaVector.Tests.cs index ababacb8..f74bb2d1 100644 --- a/lib/averages/ema/EmaVector.Tests.cs +++ b/lib/averages/ema/EmaVector.Tests.cs @@ -12,9 +12,9 @@ public class EmaVectorTests { int[] periods = { 10, 20 }; var emaVector = new EmaVector(periods); - + var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); - + Assert.Equal(100.0, res[0].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 }; var emaVector = new EmaVector(alphas); - + var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); - + Assert.Equal(3, res.Length); Assert.Equal(100.0, res[0].Value, 1e-9); Assert.Equal(100.0, res[1].Value, 1e-9); @@ -37,40 +37,40 @@ public class EmaVectorTests public void Initialization_WithZeroPeriod_ThrowsArgumentException() { int[] periods = { 10, 0, 20 }; - - Assert.Throws(() => new EmaVector(periods)); + + Assert.Throws(() => new EmaVector(periods)); } [Fact] public void Initialization_WithNegativePeriod_ThrowsArgumentException() { int[] periods = { 10, -5, 20 }; - - Assert.Throws(() => new EmaVector(periods)); + + Assert.Throws(() => new EmaVector(periods)); } [Fact] public void Initialization_WithZeroAlpha_ThrowsArgumentException() { double[] alphas = { 0.1, 0.0, 0.5 }; - - Assert.Throws(() => new EmaVector(alphas)); + + Assert.Throws(() => new EmaVector(alphas)); } [Fact] public void Initialization_WithNegativeAlpha_ThrowsArgumentException() { double[] alphas = { 0.1, -0.1, 0.5 }; - - Assert.Throws(() => new EmaVector(alphas)); + + Assert.Throws(() => new EmaVector(alphas)); } [Fact] public void Initialization_WithAlphaGreaterThanOne_ThrowsArgumentException() { double[] alphas = { 0.1, 1.5, 0.5 }; - - Assert.Throws(() => new EmaVector(alphas)); + + Assert.Throws(() => new EmaVector(alphas)); } [Fact] @@ -78,9 +78,9 @@ public class EmaVectorTests { double[] alphas = { 0.1, 1.0, 0.5 }; var emaVector = new EmaVector(alphas); - + var res = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); - + Assert.Equal(3, res.Length); } @@ -90,7 +90,7 @@ public class EmaVectorTests int[] periods = { 5, 10, 20 }; var emaVector = new EmaVector(periods); var emaSingles = periods.Select(p => new Ema(p)).ToArray(); - + var values = new double[] { 10, 20, 30, 40, 50, 40, 30, 20, 10 }; var time = DateTime.UtcNow; @@ -98,14 +98,14 @@ public class EmaVectorTests { var tVal = new TValue(time, val); var multiRes = emaVector.Update(tVal); - + for (int i = 0; i < periods.Length; i++) { var singleRes = emaSingles[i].Update(tVal); Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9); Assert.Equal(singleRes.Time, multiRes[i].Time); } - + time = time.AddMinutes(1); } } @@ -116,26 +116,26 @@ public class EmaVectorTests int[] periods = { 5, 10, 20 }; var emaVector = new EmaVector(periods); var emaSingles = periods.Select(p => new Ema(p)).ToArray(); - + int len = 100; var t = new System.Collections.Generic.List(len); var v = new System.Collections.Generic.List(len); var now = DateTime.UtcNow; - + for (int i = 0; i < len; i++) { t.Add(now.AddMinutes(i).Ticks); v.Add(Math.Sin(i * 0.1) * 100); } - + var series = new TSeries(t, v); - + var multiRes = emaVector.Calculate(series); - + for (int i = 0; i < periods.Length; i++) { var singleRes = emaSingles[i].Update(series); - + Assert.Equal(singleRes.Count, multiRes[i].Count); for (int j = 0; j < len; j++) { @@ -150,27 +150,27 @@ public class EmaVectorTests int[] periods = { 5, 10, 20 }; var emaVectorBatch = new EmaVector(periods); var emaVectorStream = new EmaVector(periods); - + int len = 100; var t = new System.Collections.Generic.List(len); var v = new System.Collections.Generic.List(len); var now = DateTime.UtcNow; - + for (int i = 0; i < len; i++) { t.Add(now.AddMinutes(i).Ticks); v.Add(Math.Sin(i * 0.1) * 100); } - + var series = new TSeries(t, v); - + var batchRes = emaVectorBatch.Calculate(series); - + for (int i = 0; i < len; i++) { var tVal = new TValue(new DateTime(t[i]), v[i]); var streamRes = emaVectorStream.Update(tVal); - + for (int j = 0; j < periods.Length; j++) { Assert.Equal(batchRes[j].Values[i], streamRes[j].Value, 1e-9); @@ -182,25 +182,25 @@ public class EmaVectorTests public void Calculate_Static_MatchesInstanceMethod() { int[] periods = { 5, 10, 20 }; - + int len = 50; var t = new System.Collections.Generic.List(len); var v = new System.Collections.Generic.List(len); var now = DateTime.UtcNow; - + for (int i = 0; i < len; i++) { t.Add(now.AddMinutes(i).Ticks); v.Add(Math.Sin(i * 0.1) * 100); } - + var series = new TSeries(t, v); - + var instanceEma = new EmaVector(periods); var instanceRes = instanceEma.Calculate(series); - + var staticRes = EmaVector.Calculate(series, periods); - + for (int i = 0; i < periods.Length; i++) { Assert.Equal(instanceRes[i].Count, staticRes[i].Count); @@ -210,18 +210,18 @@ public class EmaVectorTests } } } - + [Fact] public void Reset_ClearsState() { int[] periods = { 10 }; var emaVector = new EmaVector(periods); - + emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Reset(); - + var res = emaVector.Update(new TValue(DateTime.UtcNow, 200.0)); - + Assert.Equal(200.0, res[0].Value, 1e-9); } @@ -230,12 +230,12 @@ public class EmaVectorTests { int[] periods = { 10, 20 }; var emaVector = new EmaVector(periods); - + emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); - + var resultAfterNaN = emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); - + foreach (var result in resultAfterNaN) { Assert.True(double.IsFinite(result.Value), $"Expected finite value but got {result.Value}"); @@ -247,16 +247,16 @@ public class EmaVectorTests { int[] periods = { 10, 20 }; var emaVector = new EmaVector(periods); - + emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); - + var resultAfterPosInf = emaVector.Update(new TValue(DateTime.UtcNow, double.PositiveInfinity)); foreach (var result in resultAfterPosInf) { Assert.True(double.IsFinite(result.Value)); } - + var resultAfterNegInf = emaVector.Update(new TValue(DateTime.UtcNow, double.NegativeInfinity)); foreach (var result in resultAfterNegInf) { @@ -269,15 +269,15 @@ public class EmaVectorTests { int[] periods = { 5, 10 }; var emaVector = new EmaVector(periods); - + emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, 110.0)); emaVector.Update(new TValue(DateTime.UtcNow, 120.0)); - + var r1 = 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)); - + 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 r3) Assert.True(double.IsFinite(result.Value)); @@ -288,21 +288,21 @@ public class EmaVectorTests { int[] periods = { 5, 10 }; var emaVector = new EmaVector(periods); - + var t = new System.Collections.Generic.List(); var v = new System.Collections.Generic.List(); var now = DateTime.UtcNow; - + t.Add(now.Ticks); v.Add(100.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(3).Ticks); v.Add(120.0); t.Add(now.AddMinutes(4).Ticks); v.Add(double.PositiveInfinity); t.Add(now.AddMinutes(5).Ticks); v.Add(130.0); - + var series = new TSeries(t, v); var results = emaVector.Calculate(series); - + foreach (var periodResults in results) { foreach (var val in periodResults.Values) @@ -317,12 +317,12 @@ public class EmaVectorTests { int[] periods = { 10 }; var emaVector = new EmaVector(periods); - + emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); emaVector.Update(new TValue(DateTime.UtcNow, double.NaN)); - + emaVector.Reset(); - + var result = emaVector.Update(new TValue(DateTime.UtcNow, 50.0)); Assert.Equal(50.0, result[0].Value, 1e-9); } @@ -333,7 +333,7 @@ public class EmaVectorTests int[] periods = { 5, 10, 20 }; var emaVector = new EmaVector(periods); var emaSingles = periods.Select(p => new Ema(p)).ToArray(); - + var values = new double[] { 10, 20, double.NaN, 40, double.PositiveInfinity, 60, 70 }; var time = DateTime.UtcNow; @@ -341,13 +341,13 @@ public class EmaVectorTests { var tVal = new TValue(time, val); var multiRes = emaVector.Update(tVal); - + for (int i = 0; i < periods.Length; i++) { var singleRes = emaSingles[i].Update(tVal); Assert.Equal(singleRes.Value, multiRes[i].Value, 1e-9); } - + time = time.AddMinutes(1); } } @@ -357,9 +357,9 @@ public class EmaVectorTests { int[] periods = { 5, 10 }; var emaVector = new EmaVector(periods); - + var result = emaVector.Update(new TValue(DateTime.UtcNow, 100.0)); - + Assert.Equal(result[0].Value, emaVector.Values[0].Value); Assert.Equal(result[1].Value, emaVector.Values[1].Value); } @@ -369,13 +369,13 @@ public class EmaVectorTests { int[] periods = { 5, 10 }; var emaVector = new EmaVector(periods); - + var t = new System.Collections.Generic.List { 100, 200, 300 }; var v = new System.Collections.Generic.List { 10.0, 20.0, 30.0 }; var series = new TSeries(t, v); - + var results = emaVector.Calculate(series); - + Assert.Equal(results[0].Last.Value, emaVector.Values[0].Value, 1e-9); Assert.Equal(results[1].Last.Value, emaVector.Values[1].Value, 1e-9); }