Skip to content

Commit

Permalink
Add string extension for enum conversion
Browse files Browse the repository at this point in the history
Also made some refactorings.
  • Loading branch information
ChainsManipulator committed Apr 11, 2024
1 parent 80a455b commit 76ded87
Show file tree
Hide file tree
Showing 9 changed files with 97 additions and 44 deletions.
87 changes: 57 additions & 30 deletions Libiada.Core.Tests/Core/AlphabetTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
});
}

/// <summary>
Expand All @@ -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)));
});
}

/// <summary>
Expand All @@ -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")));
});
}

/// <summary>
Expand Down Expand Up @@ -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)));
});
}

/// <summary>
Expand All @@ -163,7 +175,6 @@ public void CloneTest()
IBaseObject clone = firstAlphabet.Clone();

Assert.That(firstAlphabet, Is.Not.SameAs(clone));

Assert.That(firstAlphabet, Is.EqualTo(clone));
}

Expand Down Expand Up @@ -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));
});
}

/// <summary>
Expand Down Expand Up @@ -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));
});
}

/// <summary>
Expand All @@ -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);
});
}

/// <summary>
Expand All @@ -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);
});
}

/// <summary>
Expand All @@ -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));
});
}

/// <summary>
Expand Down Expand Up @@ -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));
}
}
2 changes: 1 addition & 1 deletion Libiada.Core.Tests/Extensions/ArrayExtensionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
7 changes: 4 additions & 3 deletions Libiada.Core/Core/Chain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
25 changes: 25 additions & 0 deletions Libiada.Core/Extensions/StringExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,29 @@ public static bool IsSubsetOf(this string first, string second)

return words.All(second.Contains);
}

/// <summary>
/// Converts current string to given enum value.
/// </summary>
/// <typeparam name="T">
/// Enum type.
/// </typeparam>
/// <param name="value">
/// Current value.
/// </param>
/// <returns>
/// Value of the given enum type.
/// </returns>
/// <exception cref="System.ArgumentException">
/// Thrown if invalid string representation of enum value is gprovided.
/// </exception>
public static T ToEnum<T>(this string value) where T : struct, IComparable, IFormattable, IConvertible
{
ArgumentException.ThrowIfNullOrEmpty(value);

if (!Enum.TryParse<T>(value, true, out T result))
throw new ArgumentException($"Invalind enum value. Enum: {typeof(T).FullName}, value: {value}.", nameof(value));

return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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}";
}
}

Expand Down
4 changes: 2 additions & 2 deletions Libiada.Segmenter/PoemsSegmenter/FrequencySegmenter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public Dictionary<string, int> Segmentation()
}
}

if (candidateConsonance == "")
if (candidateConsonance.Length == 0)
{
continue;
}
Expand Down Expand Up @@ -113,7 +113,7 @@ public Boolean CheckCandidateConsonance(string candidateConsonance)

if (candidateConsonance == "ue")
{
Console.WriteLine(candidateConsonance + " - " + deviation);
Console.WriteLine($"{candidateConsonance} - {deviation}");
}

if (deviation >= threshold)
Expand Down
6 changes: 3 additions & 3 deletions Libiada.Segmenter/PoemsSegmenter/PartialOrlovCriterion.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;

}
Expand Down
4 changes: 2 additions & 2 deletions Libiada.SequenceGenerator.Tests/OrderGeneratorTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ public void OrderValid()
const int expectedAlphabetCardinality = 3;
OrderGenerator orderGenerator = new();
List<int[]> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public static Dictionary<IntervalsDistribution, List<int[]>> GetOrdersIntervalsD
Dictionary<IntervalsDistribution, List<int[]>> 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);
Expand Down

0 comments on commit 76ded87

Please sign in to comment.