Advertisement
Guest User

Untitled

a guest
Oct 14th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 15.33 KB | None | 0 0
  1. // Dictionary
  2. using System;
  3. using System.Collections.Generic;
  4. using Xunit;
  5. using Xunit.Abstractions;
  6.  
  7. namespace Test
  8. {
  9.     public class DictionaryTest
  10.     {
  11.         private readonly ITestOutputHelper _testOutputHelper;
  12.  
  13.         public DictionaryTest(ITestOutputHelper testOutputHelper)
  14.         {
  15.             _testOutputHelper = testOutputHelper;
  16.         }
  17.  
  18.         //private readonly ITestOutputHelper _testOutputHelper;
  19.         [Fact]
  20.         public void DictionaryOfInToIntChangeExistingValue()
  21.         {
  22.             // TODO: ....
  23.            
  24.             Dictionary<int, int> dictionary =
  25.                 new Dictionary<int, int>();
  26.            
  27.             dictionary.Add(1,10);
  28.  
  29.             Assert.Equal(10, dictionary[1]);
  30.  
  31.             // TODO: ....
  32.            
  33.             dictionary.Remove(1);
  34.             dictionary.Add(1,20);
  35.  
  36.             Assert.Equal(20, dictionary[1]);
  37.         }
  38.  
  39.         [Fact]
  40.         public void DictionaryOfInToIntRemoveValues()
  41.         {
  42.             // TODO: ....
  43.  
  44.             Dictionary<int, int> dictionary =
  45.                 new Dictionary<int, int>();
  46.            
  47.             dictionary.Add(1,10);
  48.             dictionary.Add(2,10);
  49.             dictionary.Add(3,10);
  50.            
  51.             Assert.True(dictionary.ContainsKey(1));
  52.             Assert.True(dictionary.ContainsKey(2));
  53.             Assert.True(dictionary.ContainsKey(3));
  54.  
  55.             // TODO: ....
  56.             // TODO: ....
  57.  
  58.             dictionary.Remove(2);
  59.             dictionary.Remove(3);
  60.  
  61.             Assert.True(dictionary.ContainsKey(1));
  62.             Assert.False(dictionary.ContainsKey(2));
  63.             Assert.False(dictionary.ContainsKey(3));
  64.         }
  65.  
  66.         [Fact]
  67.         public void DictionaryOfInToIntTryGetValue()
  68.         {
  69.             // TODO: ....
  70.  
  71.             Dictionary<int, int> dictionary =
  72.                 new Dictionary<int, int>();
  73.            
  74.             dictionary.Add(1,10);
  75.            
  76.             Assert.Equal(10, dictionary[1]);
  77.  
  78.             // TODO: ....
  79.  
  80.             dictionary.Add(10,10);
  81.             int value;
  82.             bool hasValue = dictionary.TryGetValue(10,out value);
  83.             Assert.True(hasValue);
  84.             Assert.Equal(10, value);
  85.         }
  86.  
  87.         [Fact]
  88.         public void DictionaryOfIntToIntCanBeCreatedInOneLine()
  89.         {
  90.             // TODO: ....
  91.  
  92.             Dictionary<int, int> dictionary =
  93.                 new Dictionary<int, int>();
  94.            
  95.             dictionary.Add(1,2);
  96.             dictionary.Add(3,4);
  97.             Assert.Equal(2, dictionary[1]);
  98.             Assert.Equal(4, dictionary[3]);
  99.         }
  100.  
  101.         [Fact]
  102.         public void DictionaryOfStringToStringThrowsWheKeyNotInDictionary()
  103.         {
  104.             // TODO: ....
  105.  
  106.             Dictionary<string, string> dictionary =
  107.                 new Dictionary<string, string>();
  108.            
  109.             dictionary.Add("A","B");
  110.            
  111.             Assert.Equal("B", dictionary["A"]);
  112.  
  113.             void AccessElement()
  114.             {
  115.                 // TODO: ....
  116.  
  117.                 _testOutputHelper.WriteLine(dictionary["B"]);
  118.                
  119.                 //_testOutputHelper.WriteLine(dictionary["B"]);
  120.             }
  121.  
  122.             Assert.Throws<KeyNotFoundException>(AccessElement);
  123.  
  124.             // TODO: ....
  125.            
  126.  
  127.             dictionary.Add("B","C");
  128.             AccessElement();
  129.         }
  130.  
  131.         [Fact]
  132.         public void DictionaryOfStringToStringThrowsWhenAddedElementExists()
  133.         {
  134.             // TODO: ....
  135.            
  136.             Dictionary<string, string> dictionary =
  137.                 new Dictionary<string, string>();
  138.            
  139.             dictionary.Add("A","B");
  140.  
  141.             Assert.Equal("B", dictionary["A"]);
  142.  
  143.             void AddElement()
  144.             {
  145.                 // TODO: ....
  146.  
  147.                 dictionary.Add("A", "C");
  148.             }
  149.            
  150.             Assert.Throws<ArgumentException>(AddElement);
  151.  
  152.             // TODO: ....
  153.            
  154.             dictionary.Remove("A");
  155.             AddElement();
  156.             Assert.Equal("C", dictionary["A"]);
  157.         }
  158.     }
  159. }
  160.  
  161.  
  162.  
  163. // HashTest
  164.  
  165. using System.Collections.Generic;
  166. using System.Linq;
  167. using Xunit;
  168.  
  169. namespace Test
  170. {
  171.     public class HashSetTest
  172.     {
  173.         [Fact]
  174.         public void HashSetStringBasicOperations()
  175.         {
  176.             // TODO: ....
  177.             HashSet<string> hashSet = new HashSet<string>();
  178.  
  179.             hashSet.Add("A");
  180.             hashSet.Add("B");
  181.             hashSet.Add("C");
  182.             Assert.Equal(3, hashSet.Count);
  183.             Assert.Contains("A", hashSet);
  184.             Assert.Contains("B", hashSet);
  185.             Assert.Contains("C", hashSet);
  186.  
  187.             // TODO: ....
  188.            
  189.             hashSet.Remove("A");
  190.  
  191.             Assert.DoesNotContain("A", hashSet);
  192.             Assert.Contains("B", hashSet);
  193.             Assert.Contains("C", hashSet);
  194.         }
  195.  
  196.         [Fact]
  197.         public void HashSetStringIsCommonPart()
  198.         {
  199.             // TODO: ....
  200.             // TODO: ....
  201.  
  202.             HashSet<string> hashSetA=new HashSet<string>();
  203.             hashSetA.Add("A");
  204.             hashSetA.Add("B");
  205.             hashSetA.Add("C");
  206.            
  207.             HashSet<string> hashSetB=new HashSet<string>();
  208.             hashSetB.Add("A");
  209.             hashSetB.Add("B");
  210.             hashSetB.Add("F");
  211.            
  212.             Assert.Equal(3, hashSetA.Count);
  213.             Assert.Contains("A", hashSetA);
  214.             Assert.Contains("B", hashSetA);
  215.             Assert.Contains("C", hashSetA);
  216.  
  217.             Assert.Equal(3, hashSetB.Count);
  218.             Assert.Contains("A", hashSetB);
  219.             Assert.Contains("B", hashSetB);
  220.             Assert.Contains("F", hashSetB);
  221.  
  222.             // TODO: ....
  223.  
  224.             //hashSetA.Remove("C");
  225.             hashSetA.IntersectWith(hashSetB);
  226.             Assert.Equal(2, hashSetA.Count);
  227.             Assert.Contains("A", hashSetA);
  228.             Assert.Contains("B", hashSetA);
  229.  
  230.             Assert.Equal(3, hashSetB.Count);
  231.             Assert.Contains("A", hashSetB);
  232.             Assert.Contains("B", hashSetB);
  233.             Assert.Contains("F", hashSetB);
  234.         }
  235.  
  236.         [Fact]
  237.         public void HashSetStringIsSubsetOfOtherSet()
  238.         {
  239.             // TODO: ....
  240.             // TODO: ....
  241.            
  242.             HashSet<int> hashSetSubset=new HashSet<int>();
  243.             HashSet<int> hashSetSuperset=new HashSet<int>();
  244.            
  245.             for (int i = 0; i < 5; i++)
  246.             {
  247.                 hashSetSuperset.Add(i);
  248.                 if (i < 3)
  249.                     hashSetSubset.Add(i);
  250.             }
  251.            
  252.  
  253.             Assert.Equal(5, hashSetSuperset.Count);
  254.             Assert.Equal(3, hashSetSubset.Count);
  255.  
  256.             Assert.True(hashSetSuperset.Overlaps(hashSetSubset));
  257.             Assert.True(hashSetSubset.Overlaps(hashSetSuperset));
  258.         }
  259.     }
  260. }
  261.  
  262. // Linked List
  263.  
  264. using System.Collections.Generic;
  265. using Xunit;
  266.  
  267. namespace Test
  268. {
  269.     public class LinkedListTest
  270.     {
  271.         [Fact]
  272.         public void LinkedListOdIntBasicOperations()
  273.         {
  274.             // TODO: ....
  275.            
  276.             LinkedList<int> linkedList = new LinkedList<int>();
  277.  
  278.             // TODO: ....
  279.  
  280.             var node = linkedList.AddFirst(1);
  281.  
  282.             Assert.Single(linkedList);
  283.             Assert.Equal(1, linkedList.First?.Value);
  284.  
  285.             // TODO: ....
  286.  
  287.             var node2 = linkedList.AddBefore(node, 2);
  288.            
  289.             Assert.Equal(2, linkedList.Count);
  290.             Assert.Equal(2, linkedList.First?.Value);
  291.             Assert.Equal(1, linkedList.Last?.Value);
  292.  
  293.             // TODO: ....
  294.  
  295.             linkedList.AddAfter(node2, 4);
  296.             // TODO: ....
  297.  
  298.             Assert.Equal(3, linkedList.Count);
  299.             Assert.Equal(2, linkedList.First?.Value);
  300.             Assert.Equal(4, linkedList.First?.Next?.Value);
  301.             Assert.Equal(1, linkedList.Last?.Value);
  302.             Assert.Equal(4, linkedList.Last?.Previous?.Value);
  303.         }
  304.     }
  305. }
  306.  
  307. // LinkedList
  308.  
  309. using System.Collections.Generic;
  310. using Xunit;
  311.  
  312. namespace Test
  313. {
  314.     public class LinkedListTest
  315.     {
  316.         [Fact]
  317.         public void LinkedListOdIntBasicOperations()
  318.         {
  319.             // TODO: ....
  320.            
  321.             LinkedList<int> linkedList = new LinkedList<int>();
  322.  
  323.             // TODO: ....
  324.  
  325.             var node = linkedList.AddFirst(1);
  326.  
  327.             Assert.Single(linkedList);
  328.             Assert.Equal(1, linkedList.First?.Value);
  329.  
  330.             // TODO: ....
  331.  
  332.             var node2 = linkedList.AddBefore(node, 2);
  333.            
  334.             Assert.Equal(2, linkedList.Count);
  335.             Assert.Equal(2, linkedList.First?.Value);
  336.             Assert.Equal(1, linkedList.Last?.Value);
  337.  
  338.             // TODO: ....
  339.  
  340.             linkedList.AddAfter(node2, 4);
  341.             // TODO: ....
  342.  
  343.             Assert.Equal(3, linkedList.Count);
  344.             Assert.Equal(2, linkedList.First?.Value);
  345.             Assert.Equal(4, linkedList.First?.Next?.Value);
  346.             Assert.Equal(1, linkedList.Last?.Value);
  347.             Assert.Equal(4, linkedList.Last?.Previous?.Value);
  348.         }
  349.     }
  350. }
  351.  
  352.  
  353. // List
  354.  
  355. using System.Collections.Generic;
  356. using Xunit;
  357.  
  358. namespace Test
  359. {
  360.     public class ListTest
  361.     {
  362.         [Fact]
  363.         public void ListOfIntHasSometimesHigherCapacityThanCount()
  364.         {
  365.             // TODO: ....
  366.             List<int> list=new List<int>();
  367.             list.Add(1);
  368.  
  369.             Assert.Single(list);
  370.             Assert.Equal(4, list.Capacity);
  371.         }
  372.  
  373.         [Fact]
  374.         public void ListOfIntHasSometimesTheSameSizeAndCapacity()
  375.         {
  376.             // TODO: ....
  377.  
  378.             List<int> listOne=new List<int>();
  379.             listOne.Add(1);
  380.             listOne.Add(1);
  381.             listOne.Add(1);
  382.             listOne.Add(1);
  383.            
  384.             Assert.Equal(4, listOne.Count);
  385.             Assert.Equal(4, listOne.Capacity);
  386.  
  387.             // TODO: ....
  388.  
  389.             List<int> listTwo=new List<int>();
  390.             listTwo.Add(1);
  391.             listTwo.Add(1);
  392.            
  393.             Assert.Equal(2, listTwo.Count);
  394.             Assert.Equal(4, listTwo.Capacity);
  395.         }
  396.  
  397.         [Fact]
  398.         public void ListOfIntSometimesGrowsCapacityAfterAdd()
  399.         {
  400.             // TODO: ....
  401.  
  402.             List<int> list=new List<int>();
  403.             list.Add(1);
  404.             list.Add(1);
  405.            
  406.             Assert.Equal(2, list.Count);
  407.             Assert.Equal(4, list.Capacity);
  408.  
  409.             // TODO: ....
  410.  
  411.             list.Add(1);
  412.            
  413.             Assert.Equal(3, list.Count);
  414.             Assert.Equal(4, list.Capacity);
  415.  
  416.             // TODO: ....
  417.  
  418.             list.Add(1);
  419.            
  420.             Assert.Equal(4, list.Count);
  421.             Assert.Equal(4, list.Capacity);
  422.  
  423.             // TODO: ....
  424.  
  425.             list.Add(2);
  426.            
  427.             Assert.Equal(5, list.Count);
  428.             Assert.Equal(8, list.Capacity);
  429.         }
  430.  
  431.         [Fact]
  432.         public void ListOfStringCanBeCreatedInOneLine()
  433.         {
  434.             // TODO: ....
  435.  
  436.             var list=new List<string>(){ "Foo", "Bar", "Baz"};
  437.             Assert.Equal(3, list.Count);
  438.             Assert.Equal("Foo", list[0]);
  439.             Assert.Equal("Bar", list[1]);
  440.             Assert.Equal("Baz", list[2]);
  441.         }
  442.     }
  443. }
  444.  
  445. // Queue
  446.  
  447. using System.Collections.Generic;
  448. using Xunit;
  449.  
  450. namespace Test
  451. {
  452.     public class QueueTest
  453.     {
  454.         [Fact]
  455.         public void QueueOfIntBasicOperations()
  456.         {
  457.             // TODO: ....
  458.            
  459.             Queue<int> queue = new Queue<int>();
  460.            
  461.             queue.Enqueue(1);
  462.             queue.Enqueue(2);
  463.             queue.Enqueue(3);
  464.  
  465.             int Next()
  466.             {
  467.                 // TODO: ....
  468.                 return queue.Dequeue();
  469.             }
  470.  
  471.             Assert.Equal(3, queue.Count);
  472.             Assert.Equal(1, Next());
  473.             Assert.Equal(2, Next());
  474.             Assert.Equal(3, Next());
  475.             Assert.Empty(queue);
  476.         }
  477.  
  478.         [Fact]
  479.         public void QueueOfIntCheckNextValueWithoutRemovingIt()
  480.         {
  481.             // TODO: ....
  482.             Queue<int> queue = new Queue<int>();
  483.            
  484.             queue.Enqueue(1);
  485.             queue.Enqueue(2);
  486.  
  487.             // TODO: ....
  488.  
  489.             int value = queue.Peek();
  490.            
  491.             Assert.Equal(1, value);
  492.  
  493.             int Next()
  494.             {
  495.                 // TODO: ....
  496.                 return queue.Dequeue();
  497.             }
  498.  
  499.             Assert.Equal(1, Next());
  500.             Assert.Equal(2, Next());
  501.         }
  502.     }
  503. }
  504.  
  505. // SortedList
  506.  
  507.  
  508. using System;
  509. using System.Collections;
  510. using System.Collections.Generic;
  511. using Xunit;
  512.  
  513. namespace Test
  514. {
  515.     public class SortedListTest
  516.     {
  517.         private class ReversedComparer : IComparer<int>  //TODO: ....
  518.         {
  519.             public int Compare(int x,int y) {
  520.                 return -1 * x.CompareTo(y);
  521.             }
  522.         }
  523.  
  524.         [Fact]
  525.         public void SortedListOfIntToStringIsSortedByKey()
  526.         {
  527.             // TODO: ....
  528.            
  529.             SortedList<int, String> list = new SortedList<int, String>();
  530.             list.Add(0, "C");
  531.             list.Add(1, "A");
  532.             list.Add(2,"B");
  533.            
  534.             Assert.Equal(3, list.Count);
  535.  
  536.             Assert.Equal("C", list[0]);
  537.             Assert.Equal("A", list[1]);
  538.             Assert.Equal("B", list[2]);
  539.  
  540.             Assert.Equal(0, list.Keys[0]);
  541.             Assert.Equal(1, list.Keys[1]);
  542.             Assert.Equal(2, list.Keys[2]);
  543.  
  544.             Assert.Equal("C", list.Values[0]);
  545.             Assert.Equal("A", list.Values[1]);
  546.             Assert.Equal("B", list.Values[2]);
  547.         }
  548.  
  549.         [Fact]
  550.         public void SortedListOfIntToStringWithCustomComparer()
  551.         {
  552.             //SortedList<int, String> list = new SortedList<int, String>(new ReversedComparer()<int,String>);
  553.            
  554.             SortedList<int, String> list = new SortedList<int, String>(new ReversedComparer());
  555.             list.Add(0, "C");
  556.             list.Add(1, "A");
  557.             list.Add(2,"B");
  558.            
  559.             Assert.Equal(3, list.Count);
  560.  
  561.             Assert.Equal("C", list[0]);
  562.             Assert.Equal("A", list[1]);
  563.             Assert.Equal("B", list[2]);
  564.  
  565.             Assert.Equal(2, list.Keys[0]);
  566.             Assert.Equal(1, list.Keys[1]);
  567.             Assert.Equal(0, list.Keys[2]);
  568.  
  569.             Assert.Equal("B", list.Values[0]);
  570.             Assert.Equal("A", list.Values[1]);
  571.             Assert.Equal("C", list.Values[2]);
  572.         }
  573.     }
  574. }
  575.  
  576. //Stack
  577.  
  578. using System.Collections.Generic;
  579. using Xunit;
  580.  
  581. namespace Test
  582. {
  583.     public class StackTest
  584.     {
  585.         [Fact]
  586.         public void StackOfIntBasicOperations()
  587.         {
  588.             // TODO: ....
  589.            
  590.             Stack<int> queue=new Stack<int>();
  591.            
  592.  
  593.             queue.Push(1);
  594.             queue.Push(2);
  595.             queue.Push(3);
  596.  
  597.             int Next()
  598.             {
  599.                 // TODO: ....
  600.                 return queue.Pop();
  601.             }
  602.  
  603.             Assert.Equal(3, queue.Count);
  604.  
  605.             Assert.Equal(3, Next());
  606.             Assert.Equal(2, Next());
  607.             Assert.Equal(1, Next());
  608.  
  609.             Assert.Empty(queue);
  610.         }
  611.     }
  612. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement