Skip to content
35 changes: 35 additions & 0 deletions CsharpExtrasTest/Dictionary/BijectionDictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,14 @@ class BijectionDictionaryTest
[Category("Unit")]
public void TestGivenUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenCanBeAccessed()
{
//Arrange
int maxCount = 10;
int lastIndex = maxCount - 1;

//Act
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

//Assert
Assert.AreEqual(maxCount, dict.Count);
Assert.AreEqual(GetTestStringBasedOnIndex(0), dict[0]);
Assert.AreEqual(GetTestStringBasedOnIndex(lastIndex), dict[lastIndex]);
Expand All @@ -29,10 +33,14 @@ public void TestGivenUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenCanBeAcce
[Category("Unit")]
public void TestGivenUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenReverseDictionaryCanBeAccessed()
{
//Arrange
int maxCount = 10;
int lastIndex = maxCount - 1;

//Act
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

//Assert
Assert.AreEqual(maxCount, dict.Count);
Assert.AreEqual(dict.Count, dict.Reverse.Count);

Expand All @@ -44,11 +52,14 @@ public void TestGivenUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenReverseDi
[Category("Unit")]
public void TestGivenNonUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenExceptionsThrownForDuplicates()
{
//Arrange
IBijectionDictionary<int, string> dict = new BijectionDictionaryImpl<int, string>();

//Act
dict.Add(0, GetTestStringBasedOnIndex(0));
int countBefore = dict.Count;

//Assert
// Exception thrown for duplicate keys
Assert.Throws<ArgumentException>(() => dict.Add(0, GetTestStringBasedOnIndex(0)));
Assert.AreEqual(countBefore, dict.Count, "Item added to dictionary after exception thrown");
Expand All @@ -67,12 +78,16 @@ public void TestGivenNonUniqueKeyValuePairsWhenAddedToTwoWayDictionaryThenExcept
[Category("Unit")]
public void TestGivenTwoWayDictionaryWhenRemoveItemsFromOriginalVersionThenItemsAlsoRemovedFromReversedVersion()
{
//Arrange
int maxCount = 10;
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

int indexToRemove = 5;

//Act
string valueToRemove = GetTestStringBasedOnIndex(indexToRemove);

//Assert
Assert.AreEqual(valueToRemove, dict[indexToRemove], "GIVEN: Dictionary is not setup correctly");
Assert.AreEqual(indexToRemove, dict.Reverse[valueToRemove], "GIVEN: Dictionary is not setup correctly");

Expand All @@ -87,12 +102,16 @@ public void TestGivenTwoWayDictionaryWhenRemoveItemsFromOriginalVersionThenItems
[Category("Unit")]
public void TestGivenTwoWayDictionaryWhenRemoveItemsFromReversedVersionThenItemsAlsoRemovedFromOriginalVersion()
{
//Arrange
int maxCount = 10;
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

int indexToRemove = 5;

//Act
string valueToRemove = GetTestStringBasedOnIndex(indexToRemove);

//Assert
Assert.AreEqual(valueToRemove, dict[indexToRemove], "GIVEN: Dictionary is not setup correctly");
Assert.AreEqual(indexToRemove, dict.Reverse[valueToRemove], "GIVEN: Dictionary is not setup correctly");

Expand All @@ -107,9 +126,13 @@ public void TestGivenTwoWayDictionaryWhenRemoveItemsFromReversedVersionThenItems
[Category("Unit")]
public void TestGivenTwoWayDictionaryWhenGettingReverseOfReverseThenReverseOfReverseIsTheSameObjectAsOriginal()
{
//Arrange
int maxCount = 10;

//Act
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

//Assert
Assert.AreEqual(maxCount, dict.Count);
Assert.AreEqual(maxCount, dict.Reverse.Count);
Assert.AreEqual(maxCount, dict.Reverse.Reverse.Count);
Expand All @@ -121,9 +144,13 @@ public void TestGivenTwoWayDictionaryWhenGettingReverseOfReverseThenReverseOfRev
[Category("Unit")]
public void TestGivenTwoWayDictionaryWhenAllItemsClearedThenReverseVersionIsAlsoEmpty()
{
//Arrange
int maxCount = 10;

//Act
IBijectionDictionary<int, string> dict = GenerateNewTestDictionary(maxCount);

//Assert
Assert.AreEqual(maxCount, dict.Count, "GIVEN: Dictionary not setup correctly");
Assert.AreEqual(maxCount, dict.Reverse.Count, "GIVEN: Dictionary not setup correctly");

Expand All @@ -137,13 +164,17 @@ public void TestGivenTwoWayDictionaryWhenAllItemsClearedThenReverseVersionIsAlso
[Category("Unit")]
public void TestGivenExistingDictionaryWhenWrappedInTwoWayDictionaryThenReverseVersionIsPopulated()
{
//Arrange
int maxCount = 10;

//Act
IDictionary<int, string> dict = new Dictionary<int, string>();
for (int i = 0; i < maxCount; i++)
{
dict.Add(i, GetTestStringBasedOnIndex(i));
}

//Assert
Assert.AreEqual(maxCount, dict.Count, "GIVEN: Source dictionary has incorrect length");

IBijectionDictionary<int, string> twoWayDict = new BijectionDictionaryImpl<int, string>(dict);
Expand All @@ -157,13 +188,17 @@ public void TestGivenExistingDictionaryWhenWrappedInTwoWayDictionaryThenReverseV
[Category("Unit")]
public void TestGivenExistingDictionaryWithDuplicateValuesWhenWrappedInTwoWayDictionaryThenExceptionIsThrown()
{
//Arrange
int maxCount = 2;

//Act
IDictionary<int, string> dict = new Dictionary<int, string>();
for (int i = 0; i < maxCount; i++)
{
dict.Add(i, "duplicate");
}

//Assert
Assert.AreEqual(maxCount, dict.Count, "GIVEN: Source dictionary has incorrect length");
Assert.Throws<ArgumentException>(() => new BijectionDictionaryImpl<int, string>(dict),
"Should throw exception if source dictionary has duplicate values");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,19 @@ public void Given_TwoMaps_When_SameElementsAddedInDifferentOrder_Then_MapsAreEqu
[Category("Unit")]
public void GivenMap_WhenTransform_ThenResultIsOfCorrectLengthAndContainsExpectedMappings()
{
//Arrange
ISetValuedDictionary<int, string> testMap = new SetValuedDictionaryImpl<int, string>();
testMap.Add(1, "Unity");
testMap.Add(1, "One");
testMap.Add(2, "Two");
//Intentionally add a value that'll map to the same as another value
testMap.Add(2, "Dha");
testMap.Add(3, "Three");

//Act
ISetValuedDictionary<int, int> transformedMap = testMap.TransformValues(s => s.Length);

//Assert
Assert.AreEqual(3, transformedMap.Count, "Transformed map should be the same length as original");
Assert.IsTrue(new HashSet<int>(new int[] {5, 3}).SetEquals(transformedMap[1]));
Assert.IsTrue(new HashSet<int>(new int[] {3}).SetEquals(transformedMap[2]));
Expand Down
14 changes: 14 additions & 0 deletions CsharpExtrasTest/Dictionary/RegexPatternDictionaryTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,10 @@ public class RegexPatternDictionaryTest
[Category("Unit")]
public void TestGivenRegexPatternToStringDictionaryWhenCheckingForMatchesThenCorrectResultReturned()
{
//Act
IRegexPatternDictionary<string> regexDict = BuildTestRegexPatternDict();

//Assert
Assert.True(regexDict.HasMatch("hello"), "Expected 'hello' to have a match in the dictionary");
Assert.True(regexDict.HasMatch("a day ago"), "Expected 'hi' to have a match in the dictionary");
Assert.True(regexDict.HasMatch("world"), "Expected 'world' to have a match in the dictionary");
Expand All @@ -29,8 +31,10 @@ public void TestGivenRegexPatternToStringDictionaryWhenCheckingForMatchesThenCor
[Category("Unit")]
public void TestGivenRegexPatternToStringDictionaryWhenFindingAllMatchesThenCorrectResultReturned()
{
//Act
IRegexPatternDictionary<string> regexDict = BuildTestRegexPatternDict();

//Assert
string allMatches = string.Join(" ", regexDict.FindAllValuesThatMatch("a hello world day"));
Assert.AreEqual("this is good", allMatches,
"Expected all matches for 'a hello world' to form the sentence 'this is good'. No other results should be returned.");
Expand All @@ -50,13 +54,16 @@ private IRegexPatternDictionary<string> BuildTestRegexPatternDict()
[Category("Unit")]
public void TestGivenRegexPatternToStringDictionaryWithEscapedPatternWhenCheckingForMatchesThenCorrectResultReturned()
{
//Arrange
IRegexPatternDictionary<string> regexDict = new RegexPatternDictionaryImpl<string>();

//Act
regexDict.AddEscapedFullMatchPattern("hello", "this");
regexDict.AddEscapedFullMatchPattern("wo+rld", "is");
regexDict.AddEscapedFullMatchPattern("a.*y", "good");
regexDict.AddEscapedFullMatchPattern("b.*", "bad");

//Assert
Assert.True(regexDict.HasMatch("hello"), "Expected 'hello' to have a match in the dictionary");
Assert.True(regexDict.HasMatch("a.*y"), "Expected 'a.*y' to have a match in the dictionary");
Assert.False(regexDict.HasMatch("hi"), "Expected 'hi' to not have a match in the dictionary");
Expand All @@ -69,12 +76,16 @@ public void TestGivenRegexPatternToStringDictionaryWithEscapedPatternWhenCheckin
[Category("Unit")]
public void TestGivenRegexPatternToStringDictionaryWithEscapedPatternWhenGetValueByPatternThenCorrectValueReturned()
{
//Arrange
IRegexPatternDictionary<string> regexDict = new RegexPatternDictionaryImpl<string>();

string pattern = "hel+o";
string value = "world";

//Act
regexDict.AddEscapedFullMatchPattern(pattern, value);

//Assert
Assert.True(regexDict.ContainsKey(pattern), "Pattern should exist as dictionary key");
Assert.AreEqual(value, regexDict[pattern], "Value tied to pattern should be correct");
}
Expand All @@ -83,13 +94,16 @@ public void TestGivenRegexPatternToStringDictionaryWithEscapedPatternWhenGetValu
[Category("Unit")]
public void TestGivenRegexPatternToStringDictionaryWithFullMatchPatternWhenCheckingForMatchesThenCorrectResultReturned()
{
//Arrange
IRegexPatternDictionary<string> regexDict = new RegexPatternDictionaryImpl<string>();

//Act
regexDict.AddFullMatchPattern("hello", "this");
regexDict.AddFullMatchPattern("wo+rld", "is");
regexDict.AddFullMatchPattern("a.*y", "good");
regexDict.AddFullMatchPattern("b.*", "bad");

//Assert
Assert.True(regexDict.HasMatch("hello"), "Expected 'hello' to have a match in the dictionary");
Assert.True(regexDict.HasMatch("wooorld"), "Expected 'wooorld' to have a match in the dictionary");
Assert.True(regexDict.HasMatch("a day"), "Expected 'a day' to have a match in the dictionary");
Expand Down
Loading