Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using DictionaryWorkshop.Implementation;
- using DictionaryWorkshop.Implementation.Contracts;
- using DictionaryWorkshop.Models;
- using Microsoft.VisualStudio.TestTools.UnitTesting;
- namespace DictionaryWorkshop.Tests
- {
- [TestClass]
- public class DictionaryTests
- {
- [TestMethod]
- public void SholdAdd_EmptyDictionary_NoDups_1()
- {
- // Arrange
- var myDict = new MyDictionary<string, int>();
- var expected = new MyKeyValuePair<string, int>[]
- {
- new MyKeyValuePair<string, int>("three", 3),
- new MyKeyValuePair<string, int>("one", 1),
- new MyKeyValuePair<string, int>("four", 4),
- };
- // Act
- foreach (var item in expected)
- {
- myDict.Add(item.Key, item.Value);
- }
- // Assert
- var actual = myDict.ToArray();
- CollectionAssert.AreEquivalent(expected, actual);
- }
- [TestMethod]
- public void SholdAdd_EmptyDictionary_NoDups_2()
- {
- // Arrange
- var myDict = new MyDictionary<Person, ContactInfo>();
- var expected = new MyKeyValuePair<Person, ContactInfo>[]
- {
- new MyKeyValuePair<Person, ContactInfo>(new Person("Peter", DateTime.Now),new ContactInfo("0888-123-456")),
- new MyKeyValuePair<Person, ContactInfo>(new Person("Maria", DateTime.Now),new ContactInfo("0888-123-456")),
- new MyKeyValuePair<Person, ContactInfo>(new Person("Georgi", DateTime.Now),new ContactInfo("0888-123-456")),
- new MyKeyValuePair<Person, ContactInfo>(new Person("Kalina", DateTime.Now),new ContactInfo("0888-123-456")),
- };
- // Act
- foreach (var item in expected)
- {
- myDict.Add(item.Key, item.Value);
- }
- // Assert
- var actual = myDict.ToArray();
- CollectionAssert.AreEquivalent(expected, actual);
- }
- [TestMethod]
- public void ShouldAdd_LowCapacity()
- {
- // Arrange
- int totalItemsCount = 1000;
- int initialCapacity = 1;
- var myDict = new MyDictionary<string, string>(initialCapacity);
- var expected = new MyKeyValuePair<string, string>[totalItemsCount];
- // Act
- for (int i = 0; i < totalItemsCount; i++)
- {
- expected[i] = new MyKeyValuePair<string, string>($"k:{i}", $"v:{i}");
- myDict.Add($"k:{i}", $"v:{i}");
- }
- // Assert
- var actual = myDict.ToArray();
- CollectionAssert.AreEquivalent(expected, actual);
- }
- [TestMethod]
- public void Remove_ExistingElement_ShouldWorkCorrectly()
- {
- // Arrange
- MyDictionary<string, double> dictionary = new MyDictionary<string, double>
- { { "Nasko", 12.5 }, { "Maria", 99.9 } };
- // Assert
- Assert.AreEqual(2, dictionary.Count);
- // Act
- bool removed = dictionary.Remove("Vasko");
- // Assert
- Assert.IsTrue(removed);
- Assert.AreEqual(1, dictionary.Count);
- }
- [TestMethod]
- public void Remove_NonExistingElement_ShouldWorkCorrectly()
- {
- // Arrange
- MyDictionary<string, double> dictionary = new MyDictionary<string, double> { { "Peter", 12.5 }, { "Maria", 99.9 } };
- // Assert
- Assert.AreEqual(2, dictionary.Count);
- // Act
- bool removed = dictionary.Remove("Georgi");
- // Assert
- Assert.IsFalse(removed);
- Assert.AreEqual(2, dictionary.Count);
- }
- [TestMethod]
- public void ContainsKey_ExistingElement_ShouldReturnTrue()
- {
- // Arrange
- MyDictionary<DateTime, string> dictionary = new MyDictionary<DateTime, string>();
- DateTime date = new DateTime(1995, 7, 18);
- dictionary.Add(date, "Some value");
- // Act
- bool containsKey = dictionary.ContainsKey(date);
- // Assert
- Assert.IsTrue(containsKey);
- }
- [TestMethod]
- public void ContainsKey_NonExistingElement_ShouldReturnFalse()
- {
- // Arrange
- MyDictionary<DateTime, string> dictionary = new MyDictionary<DateTime, string>();
- DateTime date = new DateTime(1995, 7, 18);
- // Act
- bool containsKey = dictionary.ContainsKey(date);
- // Assert
- Assert.IsFalse(containsKey);
- }
- [TestMethod]
- public void TryGetValue_ExistingElement_ShouldReturnTheValue()
- {
- // Arrange
- MyDictionary<int, string> hashTable = new MyDictionary<int, string> { { 555, "Vasko" } };
- // Act
- bool result = hashTable.TryGetValue(555, out string value);
- // Assert
- Assert.AreEqual("Vasko", value);
- Assert.IsTrue(result);
- }
- [TestMethod]
- public void TryGetValue_NonExistingElement_ShouldReturnFalse()
- {
- // Arrange
- var dictionary = new MyDictionary<int, string>();
- // Act
- bool result = dictionary.TryGetValue(555, out string value);
- // Assert
- Assert.IsFalse(result);
- }
- [TestMethod]
- public void Find_NonExistingElement_ShouldReturnNull()
- {
- // Arrange
- MyDictionary<string, DateTime> dictionary = new MyDictionary<string, DateTime>();
- // Act
- var element = dictionary.Find("Maria");
- // Assert
- Assert.IsNull(element);
- }
- [TestMethod]
- public void Keys_ShouldWorkCorrectly()
- {
- // Arrange
- var dictionary = new MyDictionary<string, double>();
- // Assert
- CollectionAssert.AreEquivalent(new string[0], dictionary.GetKeys().ToArray());
- // Arrange
- dictionary.Add("Vasko", 12.5);
- dictionary.Add("Maria", 99.9);
- dictionary["Vasko"] = 123.45;
- // Act
- IEnumerable<string> keys = dictionary.GetKeys();
- // Assert
- string[] expectedKeys = { "Vasko", "Maria" };
- CollectionAssert.AreEquivalent(expectedKeys, keys.ToArray());
- }
- [TestMethod]
- public void Values_ShouldWorkCorrectly()
- {
- // Arrange
- var dictionary = new MyDictionary<string, double>();
- // Assert
- CollectionAssert.AreEquivalent(new string[0], dictionary.GetValues().ToArray());
- // Arrange
- dictionary.Add("Vasko", 12.5);
- dictionary.Add("Maria", 99.9);
- dictionary["Vasko"] = 123.45;
- // Act
- IEnumerable<double> values = dictionary.GetValues();
- // Assert
- double[] expectedValues = { 99.9, 123.45 };
- CollectionAssert.AreEquivalent(expectedValues, values.ToArray());
- }
- [TestMethod]
- public void Indexer_ExistingElement_ShouldReturnTheValue()
- {
- // Arrange
- MyDictionary<int, string> dictionary = new MyDictionary<int, string> { { 555, "Vasko" } };
- // Act
- string actualValue = dictionary[555];
- // Assert
- Assert.AreEqual("Vasko", actualValue);
- }
- [TestMethod]
- public void Indexer_NotExistingElement_ShouldReturnTheValue()
- {
- // Arrange
- MyDictionary<int, string> dictionary = new MyDictionary<int, string> { { 555, "Vasko" } };
- string[] expectedValues = { "Vasko", "Gosho" };
- // Act
- dictionary[5] = "Gosho";
- IEnumerable<string> values = dictionary.GetValues();
- // Assert
- CollectionAssert.AreEquivalent(expectedValues, values.ToArray());
- }
- [TestMethod]
- [ExpectedException(typeof(KeyNotFoundException))]
- public void Indexer_NonExistingElement_ShouldThrowException()
- {
- // Arrange
- MyDictionary<int, string> dictionary = new MyDictionary<int, string>();
- // Act
- string value = dictionary[12345];
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment