From 76ded877b03ec831396a8fd6c7415a94a3ccd155 Mon Sep 17 00:00:00 2001 From: Nikolay Pozdnichenko Date: Thu, 11 Apr 2024 22:51:50 +0600 Subject: [PATCH] Add string extension for enum conversion Also made some refactorings. --- Libiada.Core.Tests/Core/AlphabetTests.cs | 87 ++++++++++++------- .../Extensions/ArrayExtensionsTests.cs | 2 +- Libiada.Core/Core/Chain.cs | 7 +- Libiada.Core/Extensions/StringExtensions.cs | 25 ++++++ ...ceBetweenOneDimensionalPointsCalculator.cs | 4 +- .../PoemsSegmenter/FrequencySegmenter.cs | 4 +- .../PoemsSegmenter/PartialOrlovCriterion.cs | 6 +- .../OrderGeneratorTests.cs | 4 +- .../IntervalsDistributionExtractor.cs | 2 +- 9 files changed, 97 insertions(+), 44 deletions(-) diff --git a/Libiada.Core.Tests/Core/AlphabetTests.cs b/Libiada.Core.Tests/Core/AlphabetTests.cs index cb543021..a4f3dab0 100644 --- a/Libiada.Core.Tests/Core/AlphabetTests.cs +++ b/Libiada.Core.Tests/Core/AlphabetTests.cs @@ -78,10 +78,13 @@ public void GetTest() firstAlphabet.Add(new ValueInt(3)); firstAlphabet.Add(new ValueInt(4)); firstAlphabet.Add(new ValueInt(5)); - Assert.That(firstAlphabet[0], Is.EqualTo(new ValueInt(2))); - Assert.That(firstAlphabet[1], Is.EqualTo(new ValueInt(3))); - Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(4))); - Assert.That(firstAlphabet[3], Is.EqualTo(new ValueInt(5))); + Assert.Multiple(() => + { + Assert.That(firstAlphabet[0], Is.EqualTo(new ValueInt(2))); + Assert.That(firstAlphabet[1], Is.EqualTo(new ValueInt(3))); + Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(4))); + Assert.That(firstAlphabet[3], Is.EqualTo(new ValueInt(5))); + }); } /// @@ -95,10 +98,13 @@ public void IndependentNumberTest() firstAlphabet.Add(new ValueInt(3)); firstAlphabet.Add(new ValueInt(4)); firstAlphabet[0] = new ValueInt(3); - Assert.That(firstAlphabet[0], Is.EqualTo(new ValueInt(2))); - Assert.That(firstAlphabet[1], Is.EqualTo(new ValueInt(1))); - Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(3))); - Assert.That(firstAlphabet[3], Is.EqualTo(new ValueInt(4))); + Assert.Multiple(() => + { + Assert.That(firstAlphabet[0], Is.EqualTo(new ValueInt(2))); + Assert.That(firstAlphabet[1], Is.EqualTo(new ValueInt(1))); + Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(3))); + Assert.That(firstAlphabet[3], Is.EqualTo(new ValueInt(4))); + }); } /// @@ -112,10 +118,13 @@ public void IndependentStringTest() firstAlphabet.Add(new ValueString("5")); firstAlphabet.Add(new ValueString("1")); firstAlphabet[0] = new ValueString("3"); - Assert.That(firstAlphabet[0], Is.EqualTo(new ValueString("2"))); - Assert.That(firstAlphabet[1], Is.EqualTo(new ValueString("3"))); - Assert.That(firstAlphabet[2], Is.EqualTo(new ValueString("5"))); - Assert.That(firstAlphabet[3], Is.EqualTo(new ValueString("1"))); + Assert.Multiple(() => + { + Assert.That(firstAlphabet[0], Is.EqualTo(new ValueString("2"))); + Assert.That(firstAlphabet[1], Is.EqualTo(new ValueString("3"))); + Assert.That(firstAlphabet[2], Is.EqualTo(new ValueString("5"))); + Assert.That(firstAlphabet[3], Is.EqualTo(new ValueString("1"))); + }); } /// @@ -150,8 +159,11 @@ public void RemoveTest() firstAlphabet.Add(new ValueInt(300)); firstAlphabet.Add(new ValueInt(400)); firstAlphabet.Remove(2); - Assert.That(firstAlphabet.Cardinality, Is.EqualTo(3)); - Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(400))); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.Cardinality, Is.EqualTo(3)); + Assert.That(firstAlphabet[2], Is.EqualTo(new ValueInt(400))); + }); } /// @@ -163,7 +175,6 @@ public void CloneTest() IBaseObject clone = firstAlphabet.Clone(); Assert.That(firstAlphabet, Is.Not.SameAs(clone)); - Assert.That(firstAlphabet, Is.EqualTo(clone)); } @@ -198,8 +209,11 @@ public void EqualsTests(string[] firstElementsList, string[] secondElementsList, secondAlphabet.Add(new ValueString(element)); } - Assert.That(firstAlphabet.Equals(secondAlphabet), Is.EqualTo(equals)); - Assert.That(secondAlphabet.Equals(firstAlphabet), Is.EqualTo(equals)); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.Equals(secondAlphabet), Is.EqualTo(equals)); + Assert.That(secondAlphabet.Equals(firstAlphabet), Is.EqualTo(equals)); + }); } /// @@ -233,8 +247,11 @@ public void SetEqualsTests(string[] firstElementsList, string[] secondElementsLi secondAlphabet.Add(new ValueString(element)); } - Assert.That(firstAlphabet.SetEquals(secondAlphabet), Is.EqualTo(equals)); - Assert.That(secondAlphabet.SetEquals(firstAlphabet), Is.EqualTo(equals)); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.SetEquals(secondAlphabet), Is.EqualTo(equals)); + Assert.That(secondAlphabet.SetEquals(firstAlphabet), Is.EqualTo(equals)); + }); } /// @@ -260,8 +277,11 @@ public void SetEqualsWithNullValueTest() firstAlphabet = [NullValue.Instance(), (ValueInt)1, (ValueString)"a", (ValueInt)2]; secondAlphabet = [(ValueInt)2, (ValueInt)1, (ValueString)"a"]; - Assert.That(firstAlphabet.SetEquals(secondAlphabet), Is.False); - Assert.That(secondAlphabet.SetEquals(firstAlphabet), Is.False); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.SetEquals(secondAlphabet), Is.False); + Assert.That(secondAlphabet.SetEquals(firstAlphabet), Is.False); + }); } /// @@ -273,10 +293,13 @@ public void ContainsTest() firstAlphabet.Add(new ValueString('a')); firstAlphabet.Add(new ValueString('b')); firstAlphabet.Add(new ValueString('c')); - Assert.That(firstAlphabet.Contains(new ValueString('a')), Is.True); - Assert.That(firstAlphabet.Contains(new ValueString('b')), Is.True); - Assert.That(firstAlphabet.Contains(new ValueString('c')), Is.True); - Assert.That(firstAlphabet.Contains(new ValueString('d')), Is.False); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.Contains(new ValueString('a')), Is.True); + Assert.That(firstAlphabet.Contains(new ValueString('b')), Is.True); + Assert.That(firstAlphabet.Contains(new ValueString('c')), Is.True); + Assert.That(firstAlphabet.Contains(new ValueString('d')), Is.False); + }); } /// @@ -288,9 +311,12 @@ public void IndexOfTest() firstAlphabet.Add(new ValueString('a')); firstAlphabet.Add(new ValueString('b')); firstAlphabet.Add(new ValueString('c')); - Assert.That(firstAlphabet.IndexOf(new ValueString('d')), Is.EqualTo(-1)); - Assert.That(firstAlphabet.IndexOf(new ValueString('a')), Is.Zero); - Assert.That(firstAlphabet.IndexOf(new ValueString('c')), Is.EqualTo(2)); + Assert.Multiple(() => + { + Assert.That(firstAlphabet.IndexOf(new ValueString('d')), Is.EqualTo(-1)); + Assert.That(firstAlphabet.IndexOf(new ValueString('a')), Is.Zero); + Assert.That(firstAlphabet.IndexOf(new ValueString('c')), Is.EqualTo(2)); + }); } /// @@ -328,8 +354,9 @@ public void ToStringTest() firstAlphabet.Add((ValueInt)1); firstAlphabet.Add((ValueString)"a"); firstAlphabet.Add((ValueInt)2); - + const string expected = "< -, 1, a, 2 >"; string result = firstAlphabet.ToString(); - Assert.That(result, Is.EqualTo("< -, 1, a, 2 >")); + + Assert.That(result, Is.EqualTo(expected)); } } diff --git a/Libiada.Core.Tests/Extensions/ArrayExtensionsTests.cs b/Libiada.Core.Tests/Extensions/ArrayExtensionsTests.cs index 0a3a4766..9fd992b0 100644 --- a/Libiada.Core.Tests/Extensions/ArrayExtensionsTests.cs +++ b/Libiada.Core.Tests/Extensions/ArrayExtensionsTests.cs @@ -112,7 +112,7 @@ public void ToStringWithDefaultDelimiterTest() { int[] source = [1, 2, 3]; - string expected = "1" + Environment.NewLine + "2" + Environment.NewLine + "3"; + string expected = $"1{Environment.NewLine}2{Environment.NewLine}3"; Assert.That(source.ToStringWithDefaultDelimiter(), Is.EqualTo(expected)); } diff --git a/Libiada.Core/Core/Chain.cs b/Libiada.Core/Core/Chain.cs index 76e84690..1f419b5c 100644 --- a/Libiada.Core/Core/Chain.cs +++ b/Libiada.Core/Core/Chain.cs @@ -119,12 +119,13 @@ public Chain(short[] source) order = new int[source.Length]; for (int i = 0; i < source.Length; i++) { - if (!alphabet.Contains(new ValueInt(source[i]))) + short element = source[i]; + if (!alphabet.Contains(new ValueInt(element))) { - alphabet.Add(new ValueInt(source[i])); + alphabet.Add(new ValueInt(element)); } - order[i] = alphabet.IndexOf(new ValueInt(source[i])); + order[i] = alphabet.IndexOf(new ValueInt(element)); } FillCongenericChains(); diff --git a/Libiada.Core/Extensions/StringExtensions.cs b/Libiada.Core/Extensions/StringExtensions.cs index 2646832e..b6ef5ddb 100644 --- a/Libiada.Core/Extensions/StringExtensions.cs +++ b/Libiada.Core/Extensions/StringExtensions.cs @@ -71,4 +71,29 @@ public static bool IsSubsetOf(this string first, string second) return words.All(second.Contains); } + + /// + /// Converts current string to given enum value. + /// + /// + /// Enum type. + /// + /// + /// Current value. + /// + /// + /// Value of the given enum type. + /// + /// + /// Thrown if invalid string representation of enum value is gprovided. + /// + public static T ToEnum(this string value) where T : struct, IComparable, IFormattable, IConvertible + { + ArgumentException.ThrowIfNullOrEmpty(value); + + if (!Enum.TryParse(value, true, out T result)) + throw new ArgumentException($"Invalind enum value. Enum: {typeof(T).FullName}, value: {value}.", nameof(value)); + + return result; + } } diff --git a/Libiada.Core/TimeSeries/OneDimensional/DistanceCalculators/HammingDistanceBetweenOneDimensionalPointsCalculator.cs b/Libiada.Core/TimeSeries/OneDimensional/DistanceCalculators/HammingDistanceBetweenOneDimensionalPointsCalculator.cs index a18428eb..00e21d8d 100644 --- a/Libiada.Core/TimeSeries/OneDimensional/DistanceCalculators/HammingDistanceBetweenOneDimensionalPointsCalculator.cs +++ b/Libiada.Core/TimeSeries/OneDimensional/DistanceCalculators/HammingDistanceBetweenOneDimensionalPointsCalculator.cs @@ -29,14 +29,14 @@ public double GetDistance(double firstPoint, double secondPoint) { for (int i = 0; i < firstPointStringArray[0].Length - secondPointStringArray[0].Length; i++) { - intPartSecond = "0" + intPartSecond; + intPartSecond = $"0{intPartSecond}"; } } else { for (int i = 0; i < secondPointStringArray[0].Length - firstPointStringArray[0].Length; i++) { - intPartFirst = "0" + intPartFirst; + intPartFirst = $"0{intPartFirst}"; } } diff --git a/Libiada.Segmenter/PoemsSegmenter/FrequencySegmenter.cs b/Libiada.Segmenter/PoemsSegmenter/FrequencySegmenter.cs index ff360650..f0ed54eb 100644 --- a/Libiada.Segmenter/PoemsSegmenter/FrequencySegmenter.cs +++ b/Libiada.Segmenter/PoemsSegmenter/FrequencySegmenter.cs @@ -69,7 +69,7 @@ public Dictionary Segmentation() } } - if (candidateConsonance == "") + if (candidateConsonance.Length == 0) { continue; } @@ -113,7 +113,7 @@ public Boolean CheckCandidateConsonance(string candidateConsonance) if (candidateConsonance == "ue") { - Console.WriteLine(candidateConsonance + " - " + deviation); + Console.WriteLine($"{candidateConsonance} - {deviation}"); } if (deviation >= threshold) diff --git a/Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs b/Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs index fff9bddc..37a02651 100644 --- a/Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs +++ b/Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs @@ -16,14 +16,14 @@ public double TheoreticalDictionaryVolume() double K = CalculateK(); double B = CalculateB(K); double Z = calculateZ(); - Console.WriteLine("Z: " + Z); + Console.WriteLine($"Z: {Z}"); return K * Z - B; } public double CalculateP1() { double maxEntriesNumber = consonancesDictionary.Values.Max(); // максимальное количество вхождений - Console.WriteLine("F1: " + maxEntriesNumber); + Console.WriteLine($"F1: {maxEntriesNumber}"); double sumEntries = consonancesDictionary.Sum(v => v.Value); // сумма всех вхождений return maxEntriesNumber / sumEntries; } @@ -39,7 +39,7 @@ public double CalculateK() public double CalculateB(double K) { double frequenceMaxEntriesNumber = CalculateP1(); - Console.WriteLine("P1: " + frequenceMaxEntriesNumber); + Console.WriteLine($"P1: {frequenceMaxEntriesNumber}"); return (K / frequenceMaxEntriesNumber) - 1; } diff --git a/Libiada.SequenceGenerator.Tests/OrderGeneratorTests.cs b/Libiada.SequenceGenerator.Tests/OrderGeneratorTests.cs index 6fc45a5e..7f01f2ba 100644 --- a/Libiada.SequenceGenerator.Tests/OrderGeneratorTests.cs +++ b/Libiada.SequenceGenerator.Tests/OrderGeneratorTests.cs @@ -73,8 +73,8 @@ public void OrderValid() const int expectedAlphabetCardinality = 3; OrderGenerator orderGenerator = new(); List actual = orderGenerator.StrictGenerateOrders(expectedLength, expectedAlphabetCardinality); - Assert.That(actual.All(o => o.Length == expectedLength),"Invalid length"); - Assert.That(actual.All(o => o.Max() == expectedAlphabetCardinality), "Invlaid alphabet cardinality"); + Assert.That(actual.TrueForAll(o => o.Length == expectedLength),"Invalid length"); + Assert.That(actual.TrueForAll(o => o.Max() == expectedAlphabetCardinality), "Invlaid alphabet cardinality"); foreach (int[] order in actual) { int currentMax = 1; diff --git a/Libiada.SequenceGenerator/IntervalsDistributionExtractor.cs b/Libiada.SequenceGenerator/IntervalsDistributionExtractor.cs index a63b2f36..cb9f6505 100644 --- a/Libiada.SequenceGenerator/IntervalsDistributionExtractor.cs +++ b/Libiada.SequenceGenerator/IntervalsDistributionExtractor.cs @@ -24,7 +24,7 @@ public static Dictionary> GetOrdersIntervalsD Dictionary> accordance = []; foreach (int[] order in orders) { - IntervalsDistribution orderIntervalsDistribution = IntervalsDistributionExtractor.GetIntervalsDistribution(order, link); + IntervalsDistribution orderIntervalsDistribution = GetIntervalsDistribution(order, link); if (accordance.ContainsKey(orderIntervalsDistribution)) { accordance[orderIntervalsDistribution].Add(order);