Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Dictionary
- using System;
- using System.Collections.Generic;
- using Xunit;
- using Xunit.Abstractions;
- namespace Test
- {
- public class DictionaryTest
- {
- private readonly ITestOutputHelper _testOutputHelper;
- public DictionaryTest(ITestOutputHelper testOutputHelper)
- {
- _testOutputHelper = testOutputHelper;
- }
- //private readonly ITestOutputHelper _testOutputHelper;
- [Fact]
- public void DictionaryOfInToIntChangeExistingValue()
- {
- // TODO: ....
- Dictionary<int, int> dictionary =
- new Dictionary<int, int>();
- dictionary.Add(1,10);
- Assert.Equal(10, dictionary[1]);
- // TODO: ....
- dictionary.Remove(1);
- dictionary.Add(1,20);
- Assert.Equal(20, dictionary[1]);
- }
- [Fact]
- public void DictionaryOfInToIntRemoveValues()
- {
- // TODO: ....
- Dictionary<int, int> dictionary =
- new Dictionary<int, int>();
- dictionary.Add(1,10);
- dictionary.Add(2,10);
- dictionary.Add(3,10);
- Assert.True(dictionary.ContainsKey(1));
- Assert.True(dictionary.ContainsKey(2));
- Assert.True(dictionary.ContainsKey(3));
- // TODO: ....
- // TODO: ....
- dictionary.Remove(2);
- dictionary.Remove(3);
- Assert.True(dictionary.ContainsKey(1));
- Assert.False(dictionary.ContainsKey(2));
- Assert.False(dictionary.ContainsKey(3));
- }
- [Fact]
- public void DictionaryOfInToIntTryGetValue()
- {
- // TODO: ....
- Dictionary<int, int> dictionary =
- new Dictionary<int, int>();
- dictionary.Add(1,10);
- Assert.Equal(10, dictionary[1]);
- // TODO: ....
- dictionary.Add(10,10);
- int value;
- bool hasValue = dictionary.TryGetValue(10,out value);
- Assert.True(hasValue);
- Assert.Equal(10, value);
- }
- [Fact]
- public void DictionaryOfIntToIntCanBeCreatedInOneLine()
- {
- // TODO: ....
- Dictionary<int, int> dictionary =
- new Dictionary<int, int>();
- dictionary.Add(1,2);
- dictionary.Add(3,4);
- Assert.Equal(2, dictionary[1]);
- Assert.Equal(4, dictionary[3]);
- }
- [Fact]
- public void DictionaryOfStringToStringThrowsWheKeyNotInDictionary()
- {
- // TODO: ....
- Dictionary<string, string> dictionary =
- new Dictionary<string, string>();
- dictionary.Add("A","B");
- Assert.Equal("B", dictionary["A"]);
- void AccessElement()
- {
- // TODO: ....
- _testOutputHelper.WriteLine(dictionary["B"]);
- //_testOutputHelper.WriteLine(dictionary["B"]);
- }
- Assert.Throws<KeyNotFoundException>(AccessElement);
- // TODO: ....
- dictionary.Add("B","C");
- AccessElement();
- }
- [Fact]
- public void DictionaryOfStringToStringThrowsWhenAddedElementExists()
- {
- // TODO: ....
- Dictionary<string, string> dictionary =
- new Dictionary<string, string>();
- dictionary.Add("A","B");
- Assert.Equal("B", dictionary["A"]);
- void AddElement()
- {
- // TODO: ....
- dictionary.Add("A", "C");
- }
- Assert.Throws<ArgumentException>(AddElement);
- // TODO: ....
- dictionary.Remove("A");
- AddElement();
- Assert.Equal("C", dictionary["A"]);
- }
- }
- }
- // HashTest
- using System.Collections.Generic;
- using System.Linq;
- using Xunit;
- namespace Test
- {
- public class HashSetTest
- {
- [Fact]
- public void HashSetStringBasicOperations()
- {
- // TODO: ....
- HashSet<string> hashSet = new HashSet<string>();
- hashSet.Add("A");
- hashSet.Add("B");
- hashSet.Add("C");
- Assert.Equal(3, hashSet.Count);
- Assert.Contains("A", hashSet);
- Assert.Contains("B", hashSet);
- Assert.Contains("C", hashSet);
- // TODO: ....
- hashSet.Remove("A");
- Assert.DoesNotContain("A", hashSet);
- Assert.Contains("B", hashSet);
- Assert.Contains("C", hashSet);
- }
- [Fact]
- public void HashSetStringIsCommonPart()
- {
- // TODO: ....
- // TODO: ....
- HashSet<string> hashSetA=new HashSet<string>();
- hashSetA.Add("A");
- hashSetA.Add("B");
- hashSetA.Add("C");
- HashSet<string> hashSetB=new HashSet<string>();
- hashSetB.Add("A");
- hashSetB.Add("B");
- hashSetB.Add("F");
- Assert.Equal(3, hashSetA.Count);
- Assert.Contains("A", hashSetA);
- Assert.Contains("B", hashSetA);
- Assert.Contains("C", hashSetA);
- Assert.Equal(3, hashSetB.Count);
- Assert.Contains("A", hashSetB);
- Assert.Contains("B", hashSetB);
- Assert.Contains("F", hashSetB);
- // TODO: ....
- //hashSetA.Remove("C");
- hashSetA.IntersectWith(hashSetB);
- Assert.Equal(2, hashSetA.Count);
- Assert.Contains("A", hashSetA);
- Assert.Contains("B", hashSetA);
- Assert.Equal(3, hashSetB.Count);
- Assert.Contains("A", hashSetB);
- Assert.Contains("B", hashSetB);
- Assert.Contains("F", hashSetB);
- }
- [Fact]
- public void HashSetStringIsSubsetOfOtherSet()
- {
- // TODO: ....
- // TODO: ....
- HashSet<int> hashSetSubset=new HashSet<int>();
- HashSet<int> hashSetSuperset=new HashSet<int>();
- for (int i = 0; i < 5; i++)
- {
- hashSetSuperset.Add(i);
- if (i < 3)
- hashSetSubset.Add(i);
- }
- Assert.Equal(5, hashSetSuperset.Count);
- Assert.Equal(3, hashSetSubset.Count);
- Assert.True(hashSetSuperset.Overlaps(hashSetSubset));
- Assert.True(hashSetSubset.Overlaps(hashSetSuperset));
- }
- }
- }
- // Linked List
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class LinkedListTest
- {
- [Fact]
- public void LinkedListOdIntBasicOperations()
- {
- // TODO: ....
- LinkedList<int> linkedList = new LinkedList<int>();
- // TODO: ....
- var node = linkedList.AddFirst(1);
- Assert.Single(linkedList);
- Assert.Equal(1, linkedList.First?.Value);
- // TODO: ....
- var node2 = linkedList.AddBefore(node, 2);
- Assert.Equal(2, linkedList.Count);
- Assert.Equal(2, linkedList.First?.Value);
- Assert.Equal(1, linkedList.Last?.Value);
- // TODO: ....
- linkedList.AddAfter(node2, 4);
- // TODO: ....
- Assert.Equal(3, linkedList.Count);
- Assert.Equal(2, linkedList.First?.Value);
- Assert.Equal(4, linkedList.First?.Next?.Value);
- Assert.Equal(1, linkedList.Last?.Value);
- Assert.Equal(4, linkedList.Last?.Previous?.Value);
- }
- }
- }
- // LinkedList
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class LinkedListTest
- {
- [Fact]
- public void LinkedListOdIntBasicOperations()
- {
- // TODO: ....
- LinkedList<int> linkedList = new LinkedList<int>();
- // TODO: ....
- var node = linkedList.AddFirst(1);
- Assert.Single(linkedList);
- Assert.Equal(1, linkedList.First?.Value);
- // TODO: ....
- var node2 = linkedList.AddBefore(node, 2);
- Assert.Equal(2, linkedList.Count);
- Assert.Equal(2, linkedList.First?.Value);
- Assert.Equal(1, linkedList.Last?.Value);
- // TODO: ....
- linkedList.AddAfter(node2, 4);
- // TODO: ....
- Assert.Equal(3, linkedList.Count);
- Assert.Equal(2, linkedList.First?.Value);
- Assert.Equal(4, linkedList.First?.Next?.Value);
- Assert.Equal(1, linkedList.Last?.Value);
- Assert.Equal(4, linkedList.Last?.Previous?.Value);
- }
- }
- }
- // List
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class ListTest
- {
- [Fact]
- public void ListOfIntHasSometimesHigherCapacityThanCount()
- {
- // TODO: ....
- List<int> list=new List<int>();
- list.Add(1);
- Assert.Single(list);
- Assert.Equal(4, list.Capacity);
- }
- [Fact]
- public void ListOfIntHasSometimesTheSameSizeAndCapacity()
- {
- // TODO: ....
- List<int> listOne=new List<int>();
- listOne.Add(1);
- listOne.Add(1);
- listOne.Add(1);
- listOne.Add(1);
- Assert.Equal(4, listOne.Count);
- Assert.Equal(4, listOne.Capacity);
- // TODO: ....
- List<int> listTwo=new List<int>();
- listTwo.Add(1);
- listTwo.Add(1);
- Assert.Equal(2, listTwo.Count);
- Assert.Equal(4, listTwo.Capacity);
- }
- [Fact]
- public void ListOfIntSometimesGrowsCapacityAfterAdd()
- {
- // TODO: ....
- List<int> list=new List<int>();
- list.Add(1);
- list.Add(1);
- Assert.Equal(2, list.Count);
- Assert.Equal(4, list.Capacity);
- // TODO: ....
- list.Add(1);
- Assert.Equal(3, list.Count);
- Assert.Equal(4, list.Capacity);
- // TODO: ....
- list.Add(1);
- Assert.Equal(4, list.Count);
- Assert.Equal(4, list.Capacity);
- // TODO: ....
- list.Add(2);
- Assert.Equal(5, list.Count);
- Assert.Equal(8, list.Capacity);
- }
- [Fact]
- public void ListOfStringCanBeCreatedInOneLine()
- {
- // TODO: ....
- var list=new List<string>(){ "Foo", "Bar", "Baz"};
- Assert.Equal(3, list.Count);
- Assert.Equal("Foo", list[0]);
- Assert.Equal("Bar", list[1]);
- Assert.Equal("Baz", list[2]);
- }
- }
- }
- // Queue
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class QueueTest
- {
- [Fact]
- public void QueueOfIntBasicOperations()
- {
- // TODO: ....
- Queue<int> queue = new Queue<int>();
- queue.Enqueue(1);
- queue.Enqueue(2);
- queue.Enqueue(3);
- int Next()
- {
- // TODO: ....
- return queue.Dequeue();
- }
- Assert.Equal(3, queue.Count);
- Assert.Equal(1, Next());
- Assert.Equal(2, Next());
- Assert.Equal(3, Next());
- Assert.Empty(queue);
- }
- [Fact]
- public void QueueOfIntCheckNextValueWithoutRemovingIt()
- {
- // TODO: ....
- Queue<int> queue = new Queue<int>();
- queue.Enqueue(1);
- queue.Enqueue(2);
- // TODO: ....
- int value = queue.Peek();
- Assert.Equal(1, value);
- int Next()
- {
- // TODO: ....
- return queue.Dequeue();
- }
- Assert.Equal(1, Next());
- Assert.Equal(2, Next());
- }
- }
- }
- // SortedList
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class SortedListTest
- {
- private class ReversedComparer : IComparer<int> //TODO: ....
- {
- public int Compare(int x,int y) {
- return -1 * x.CompareTo(y);
- }
- }
- [Fact]
- public void SortedListOfIntToStringIsSortedByKey()
- {
- // TODO: ....
- SortedList<int, String> list = new SortedList<int, String>();
- list.Add(0, "C");
- list.Add(1, "A");
- list.Add(2,"B");
- Assert.Equal(3, list.Count);
- Assert.Equal("C", list[0]);
- Assert.Equal("A", list[1]);
- Assert.Equal("B", list[2]);
- Assert.Equal(0, list.Keys[0]);
- Assert.Equal(1, list.Keys[1]);
- Assert.Equal(2, list.Keys[2]);
- Assert.Equal("C", list.Values[0]);
- Assert.Equal("A", list.Values[1]);
- Assert.Equal("B", list.Values[2]);
- }
- [Fact]
- public void SortedListOfIntToStringWithCustomComparer()
- {
- //SortedList<int, String> list = new SortedList<int, String>(new ReversedComparer()<int,String>);
- SortedList<int, String> list = new SortedList<int, String>(new ReversedComparer());
- list.Add(0, "C");
- list.Add(1, "A");
- list.Add(2,"B");
- Assert.Equal(3, list.Count);
- Assert.Equal("C", list[0]);
- Assert.Equal("A", list[1]);
- Assert.Equal("B", list[2]);
- Assert.Equal(2, list.Keys[0]);
- Assert.Equal(1, list.Keys[1]);
- Assert.Equal(0, list.Keys[2]);
- Assert.Equal("B", list.Values[0]);
- Assert.Equal("A", list.Values[1]);
- Assert.Equal("C", list.Values[2]);
- }
- }
- }
- //Stack
- using System.Collections.Generic;
- using Xunit;
- namespace Test
- {
- public class StackTest
- {
- [Fact]
- public void StackOfIntBasicOperations()
- {
- // TODO: ....
- Stack<int> queue=new Stack<int>();
- queue.Push(1);
- queue.Push(2);
- queue.Push(3);
- int Next()
- {
- // TODO: ....
- return queue.Pop();
- }
- Assert.Equal(3, queue.Count);
- Assert.Equal(3, Next());
- Assert.Equal(2, Next());
- Assert.Equal(1, Next());
- Assert.Empty(queue);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement