nassss

Untitled

Mar 27th, 2020
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.57 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using DictionaryWorkshop.Implementation;
  5. using DictionaryWorkshop.Implementation.Contracts;
  6. using DictionaryWorkshop.Models;
  7.  
  8. using Microsoft.VisualStudio.TestTools.UnitTesting;
  9.  
  10. namespace DictionaryWorkshop.Tests
  11. {
  12. [TestClass]
  13. public class DictionaryTests
  14. {
  15. [TestMethod]
  16. public void SholdAdd_EmptyDictionary_NoDups_1()
  17. {
  18. // Arrange
  19. var myDict = new MyDictionary<string, int>();
  20.  
  21. var expected = new MyKeyValuePair<string, int>[]
  22. {
  23. new MyKeyValuePair<string, int>("three", 3),
  24. new MyKeyValuePair<string, int>("one", 1),
  25. new MyKeyValuePair<string, int>("four", 4),
  26. };
  27.  
  28. // Act
  29. foreach (var item in expected)
  30. {
  31. myDict.Add(item.Key, item.Value);
  32. }
  33.  
  34. // Assert
  35. var actual = myDict.ToArray();
  36. CollectionAssert.AreEquivalent(expected, actual);
  37. }
  38. [TestMethod]
  39. public void SholdAdd_EmptyDictionary_NoDups_2()
  40. {
  41. // Arrange
  42. var myDict = new MyDictionary<Person, ContactInfo>();
  43.  
  44. var expected = new MyKeyValuePair<Person, ContactInfo>[]
  45. {
  46. new MyKeyValuePair<Person, ContactInfo>(new Person("Peter", DateTime.Now),new ContactInfo("0888-123-456")),
  47. new MyKeyValuePair<Person, ContactInfo>(new Person("Maria", DateTime.Now),new ContactInfo("0888-123-456")),
  48. new MyKeyValuePair<Person, ContactInfo>(new Person("Georgi", DateTime.Now),new ContactInfo("0888-123-456")),
  49. new MyKeyValuePair<Person, ContactInfo>(new Person("Kalina", DateTime.Now),new ContactInfo("0888-123-456")),
  50. };
  51.  
  52. // Act
  53. foreach (var item in expected)
  54. {
  55. myDict.Add(item.Key, item.Value);
  56. }
  57.  
  58. // Assert
  59. var actual = myDict.ToArray();
  60. CollectionAssert.AreEquivalent(expected, actual);
  61. }
  62. [TestMethod]
  63. public void ShouldAdd_LowCapacity()
  64. {
  65. // Arrange
  66. int totalItemsCount = 1000;
  67. int initialCapacity = 1;
  68. var myDict = new MyDictionary<string, string>(initialCapacity);
  69. var expected = new MyKeyValuePair<string, string>[totalItemsCount];
  70.  
  71. // Act
  72. for (int i = 0; i < totalItemsCount; i++)
  73. {
  74. expected[i] = new MyKeyValuePair<string, string>($"k:{i}", $"v:{i}");
  75. myDict.Add($"k:{i}", $"v:{i}");
  76. }
  77.  
  78. // Assert
  79. var actual = myDict.ToArray();
  80. CollectionAssert.AreEquivalent(expected, actual);
  81. }
  82. [TestMethod]
  83. public void Remove_ExistingElement_ShouldWorkCorrectly()
  84. {
  85. // Arrange
  86. MyDictionary<string, double> dictionary = new MyDictionary<string, double>
  87.  
  88. { { "Nasko", 12.5 }, { "Maria", 99.9 } };
  89.  
  90. // Assert
  91. Assert.AreEqual(2, dictionary.Count);
  92.  
  93. // Act
  94. bool removed = dictionary.Remove("Vasko");
  95.  
  96. // Assert
  97. Assert.IsTrue(removed);
  98. Assert.AreEqual(1, dictionary.Count);
  99. }
  100. [TestMethod]
  101. public void Remove_NonExistingElement_ShouldWorkCorrectly()
  102. {
  103. // Arrange
  104. MyDictionary<string, double> dictionary = new MyDictionary<string, double> { { "Peter", 12.5 }, { "Maria", 99.9 } };
  105.  
  106. // Assert
  107. Assert.AreEqual(2, dictionary.Count);
  108.  
  109. // Act
  110. bool removed = dictionary.Remove("Georgi");
  111.  
  112. // Assert
  113. Assert.IsFalse(removed);
  114. Assert.AreEqual(2, dictionary.Count);
  115. }
  116. [TestMethod]
  117. public void ContainsKey_ExistingElement_ShouldReturnTrue()
  118. {
  119. // Arrange
  120. MyDictionary<DateTime, string> dictionary = new MyDictionary<DateTime, string>();
  121. DateTime date = new DateTime(1995, 7, 18);
  122. dictionary.Add(date, "Some value");
  123.  
  124. // Act
  125. bool containsKey = dictionary.ContainsKey(date);
  126.  
  127. // Assert
  128. Assert.IsTrue(containsKey);
  129. }
  130. [TestMethod]
  131. public void ContainsKey_NonExistingElement_ShouldReturnFalse()
  132. {
  133. // Arrange
  134. MyDictionary<DateTime, string> dictionary = new MyDictionary<DateTime, string>();
  135. DateTime date = new DateTime(1995, 7, 18);
  136.  
  137. // Act
  138. bool containsKey = dictionary.ContainsKey(date);
  139.  
  140. // Assert
  141. Assert.IsFalse(containsKey);
  142. }
  143. [TestMethod]
  144. public void TryGetValue_ExistingElement_ShouldReturnTheValue()
  145. {
  146. // Arrange
  147. MyDictionary<int, string> hashTable = new MyDictionary<int, string> { { 555, "Vasko" } };
  148.  
  149. // Act
  150. bool result = hashTable.TryGetValue(555, out string value);
  151.  
  152. // Assert
  153. Assert.AreEqual("Vasko", value);
  154. Assert.IsTrue(result);
  155. }
  156. [TestMethod]
  157. public void TryGetValue_NonExistingElement_ShouldReturnFalse()
  158. {
  159. // Arrange
  160. var dictionary = new MyDictionary<int, string>();
  161.  
  162. // Act
  163. bool result = dictionary.TryGetValue(555, out string value);
  164.  
  165. // Assert
  166. Assert.IsFalse(result);
  167. }
  168. [TestMethod]
  169. public void Find_NonExistingElement_ShouldReturnNull()
  170. {
  171. // Arrange
  172. MyDictionary<string, DateTime> dictionary = new MyDictionary<string, DateTime>();
  173.  
  174. // Act
  175. var element = dictionary.Find("Maria");
  176.  
  177. // Assert
  178. Assert.IsNull(element);
  179. }
  180. [TestMethod]
  181. public void Keys_ShouldWorkCorrectly()
  182. {
  183. // Arrange
  184. var dictionary = new MyDictionary<string, double>();
  185.  
  186. // Assert
  187. CollectionAssert.AreEquivalent(new string[0], dictionary.GetKeys().ToArray());
  188.  
  189. // Arrange
  190. dictionary.Add("Vasko", 12.5);
  191. dictionary.Add("Maria", 99.9);
  192. dictionary["Vasko"] = 123.45;
  193.  
  194. // Act
  195. IEnumerable<string> keys = dictionary.GetKeys();
  196.  
  197. // Assert
  198. string[] expectedKeys = { "Vasko", "Maria" };
  199. CollectionAssert.AreEquivalent(expectedKeys, keys.ToArray());
  200. }
  201. [TestMethod]
  202. public void Values_ShouldWorkCorrectly()
  203. {
  204. // Arrange
  205. var dictionary = new MyDictionary<string, double>();
  206.  
  207. // Assert
  208. CollectionAssert.AreEquivalent(new string[0], dictionary.GetValues().ToArray());
  209.  
  210. // Arrange
  211. dictionary.Add("Vasko", 12.5);
  212. dictionary.Add("Maria", 99.9);
  213. dictionary["Vasko"] = 123.45;
  214.  
  215. // Act
  216. IEnumerable<double> values = dictionary.GetValues();
  217.  
  218. // Assert
  219. double[] expectedValues = { 99.9, 123.45 };
  220. CollectionAssert.AreEquivalent(expectedValues, values.ToArray());
  221. }
  222. [TestMethod]
  223. public void Indexer_ExistingElement_ShouldReturnTheValue()
  224. {
  225. // Arrange
  226. MyDictionary<int, string> dictionary = new MyDictionary<int, string> { { 555, "Vasko" } };
  227.  
  228. // Act
  229. string actualValue = dictionary[555];
  230.  
  231. // Assert
  232. Assert.AreEqual("Vasko", actualValue);
  233. }
  234.  
  235. [TestMethod]
  236. public void Indexer_NotExistingElement_ShouldReturnTheValue()
  237. {
  238. // Arrange
  239. MyDictionary<int, string> dictionary = new MyDictionary<int, string> { { 555, "Vasko" } };
  240. string[] expectedValues = { "Vasko", "Gosho" };
  241.  
  242. // Act
  243. dictionary[5] = "Gosho";
  244. IEnumerable<string> values = dictionary.GetValues();
  245.  
  246. // Assert
  247. CollectionAssert.AreEquivalent(expectedValues, values.ToArray());
  248. }
  249.  
  250. [TestMethod]
  251. [ExpectedException(typeof(KeyNotFoundException))]
  252. public void Indexer_NonExistingElement_ShouldThrowException()
  253. {
  254. // Arrange
  255. MyDictionary<int, string> dictionary = new MyDictionary<int, string>();
  256.  
  257. // Act
  258. string value = dictionary[12345];
  259. }
  260. }
  261. }
Advertisement
Add Comment
Please, Sign In to add comment