예제 #1
0
        public static void IEnumerable_Testing <TQueue>()
            where TQueue : IQueue <int>, new()
        {
            {             // enqueue only
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IQueue <int> queue  = new TQueue();
                values.Stepper(i => queue.Enqueue(i));
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(queue) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in queue)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // enqueue + dequeue
                int[]        values         = { 0, 1, 2, 3, 4, 5, };
                int[]        expectedValues = { 2, 3, 4, 5, };
                IQueue <int> queue          = new TQueue();
                values.Stepper(i => queue.Enqueue(i));
                queue.Dequeue();
                queue.Dequeue();
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(queue) == expectedValues.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                expectedValues.Stepper(i => set.Add(i));
                foreach (int i in queue)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // enqueue + dequeue
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IQueue <int> queue  = new TQueue();
                values.Stepper(i => queue.Enqueue(i));
                values.Stepper(i =>
                {
                    queue.Dequeue();
                    queue.Enqueue(i);
                });
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(queue) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in queue)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
        }
예제 #2
0
        public static void IEnumerable_Testing <TStack>()
            where TStack : IStack <int>, new()
        {
            {             // push only
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IStack <int> stack  = new TStack();
                values.Stepper(i => stack.Push(i));
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // push + pop
                int[]        values         = { 0, 1, 2, 3, 4, 5, };
                int[]        expectedValues = { 0, 1, 2, 3, };
                IStack <int> stack          = new TStack();
                values.Stepper(i => stack.Push(i));
                stack.Pop();
                stack.Pop();
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == expectedValues.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                expectedValues.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
            {             // push + pop
                int[]        values = { 0, 1, 2, 3, 4, 5, };
                IStack <int> stack  = new TStack();
                values.Stepper(i => stack.Push(i));
                values.Stepper(i =>
                {
                    stack.Pop();
                    stack.Push(i);
                });
#pragma warning disable CA1829 // Use Length/Count property instead of Count() when available
                Assert.IsTrue(System.Linq.Enumerable.Count(stack) == values.Length);
#pragma warning restore CA1829 // Use Length/Count property instead of Count() when available
                ISet <int> set = SetHashLinked.New <int>();
                values.Stepper(i => set.Add(i));
                foreach (int i in stack)
                {
                    Assert.IsTrue(set.Contains(i));
                    set.Remove(i);
                }
                Assert.IsTrue(set.Count == 0);
            }
        }
예제 #3
0
 public static void Stepper_Testing <TStack>()
     where TStack : IStack <int>, new()
 {
     {             // push only
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IStack <int> stack  = new TStack();
         values.Stepper(i => stack.Push(i));
         Assert.IsTrue(stack.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // push + pop
         int[]        values         = { 0, 1, 2, 3, 4, 5, };
         int[]        expectedValues = { 0, 1, 2, 3, };
         IStack <int> stack          = new TStack();
         values.Stepper(i => stack.Push(i));
         stack.Pop();
         stack.Pop();
         Assert.IsTrue(stack.Count == expectedValues.Length);
         ISet <int> set = SetHashLinked.New <int>();
         expectedValues.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // push + pop
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IStack <int> stack  = new TStack();
         values.Stepper(i => stack.Push(i));
         values.Stepper(i =>
         {
             stack.Pop();
             stack.Push(i);
         });
         Assert.IsTrue(stack.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         stack.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
 }
예제 #4
0
 public static void Stepper_Testing <TQueue>()
     where TQueue : IQueue <int>, new()
 {
     {             // enqueue only
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IQueue <int> queue  = new TQueue();
         values.Stepper(i => queue.Enqueue(i));
         Assert.IsTrue(queue.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         queue.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // enqueue + dequeue
         int[]        values         = { 0, 1, 2, 3, 4, 5, };
         int[]        expectedValues = { 2, 3, 4, 5, };
         IQueue <int> queue          = new TQueue();
         values.Stepper(i => queue.Enqueue(i));
         queue.Dequeue();
         queue.Dequeue();
         Assert.IsTrue(queue.Count == expectedValues.Length);
         ISet <int> set = SetHashLinked.New <int>();
         expectedValues.Stepper(i => set.Add(i));
         queue.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
     {             // enqueue + dequeue
         int[]        values = { 0, 1, 2, 3, 4, 5, };
         IQueue <int> queue  = new TQueue();
         values.Stepper(i => queue.Enqueue(i));
         values.Stepper(i =>
         {
             queue.Dequeue();
             queue.Enqueue(i);
         });
         Assert.IsTrue(queue.Count == values.Length);
         ISet <int> set = SetHashLinked.New <int>();
         values.Stepper(i => set.Add(i));
         queue.Stepper(i =>
         {
             Assert.IsTrue(set.Contains(i));
             set.Remove(i);
         });
         Assert.IsTrue(set.Count == 0);
     }
 }
예제 #5
0
        public void SetHashLinked_AddRunTime()
        {
            ISet <Person> set = SetHashLinked.New <Person>(
                (a, b) => a.Id == b.Id,
                x => x.Id.GetHashCode());

            foreach (Person person in RandomTestData !)
            {
                set.Add(person);
            }
        }
예제 #6
0
        public void Remove_Testing()
        {
            {             // int
                const int  count = 100000;
                ISet <int> set   = SetHashLinked.New <int>();
                Iterate(count, i => set.Add(i));
                for (int i = 0; i < count; i += 3)
                {
                    set.Remove(i);
                }
                for (int i = 0; i < count; i++)
                {
                    if (i % 3 == 0)
                    {
                        Assert.IsFalse(set.Contains(i));
                    }
                    else
                    {
                        Assert.IsTrue(set.Contains(i));
                    }
                }
                Assert.IsFalse(set.Contains(-1));
                Assert.IsFalse(set.Contains(count));
            }

            {             // string
                const int     count = 100000;
                ISet <string> set   = SetHashLinked.New <string>();
                Iterate(count, i => set.Add(i.ToString()));
                for (int i = 0; i < count; i += 3)
                {
                    set.Remove(i.ToString());
                }
                for (int i = 0; i < count; i++)
                {
                    if (i % 3 == 0)
                    {
                        Assert.IsFalse(set.Contains(i.ToString()));
                    }
                    else
                    {
                        Assert.IsTrue(set.Contains(i.ToString()));
                    }
                }
                Assert.IsFalse(set.Contains((-1).ToString()));
                Assert.IsFalse(set.Contains(count.ToString()));
            }
        }
예제 #7
0
        public void Add_Testing()
        {
            {             // int
                const int  count = 100000;
                ISet <int> set   = SetHashLinked.New <int>();
                Iterate(count, i => set.Add(i));
                set.Add(int.MinValue);
                set.Add(int.MaxValue);

                Iterate(count, i => Assert.IsTrue(set.Contains(i)));
                Assert.IsTrue(set.Contains(int.MinValue));
                Assert.IsTrue(set.Contains(int.MaxValue));
                Assert.IsFalse(set.Contains(-1));
                Assert.IsFalse(set.Contains(count));

                Assert.ThrowsException <ArgumentException>(() => set.Add(0));
                Assert.ThrowsException <ArgumentException>(() => set.Add(int.MinValue));
                Assert.ThrowsException <ArgumentException>(() => set.Add(int.MaxValue));
            }

            {             // string
                const int     count = 100000;
                ISet <string> set   = SetHashLinked.New <string>();
                Iterate(count, i => set.Add(i.ToString()));
                set.Add(int.MinValue.ToString());
                set.Add(int.MaxValue.ToString());

                Iterate(count, i => Assert.IsTrue(set.Contains(i.ToString())));
                Assert.IsTrue(set.Contains(int.MinValue.ToString()));
                Assert.IsTrue(set.Contains(int.MaxValue.ToString()));
                Assert.IsFalse(set.Contains((-1).ToString()));
                Assert.IsFalse(set.Contains(count.ToString()));

                Assert.ThrowsException <ArgumentException>(() => set.Add(0.ToString()));
                Assert.ThrowsException <ArgumentException>(() => set.Add(int.MinValue.ToString()));
                Assert.ThrowsException <ArgumentException>(() => set.Add(int.MaxValue.ToString()));
            }
        }