Click here to Skip to main content
15,881,852 members
Articles / Programming Languages / C#

Set Collections for C#

Rate me:
Please Sign up or sign in to vote.
4.85/5 (18 votes)
3 Feb 2008CPOL10 min read 115K   642   54  
Describes a library of set collections I have written
// Copyright � Keith Barrett 2008.

using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using Iesi.Collections;
using KMB.Library;
using System.Collections;

namespace KMB.Library.Collections.Tests
{
    [TestFixture]
    public class SetOfObjectsTest
    {
        Customer cust1 = new Customer(1, "Fred", "Bloggs");
        Customer cust2 = new Customer(2, "Joe", "Doe");
        Customer cust3 = new Customer(3, "Bert", "Smith");
        Customer cust4 = new Customer(4, "Tom", "Jones");
        Customer cust5 = new Customer(5, "Dick", "Brown");
        Customer cust6 = new Customer(6, "Harry", "Wilson");

        #region ISet Members

        [Test]
        public void AddTest1()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();

            Assert.IsFalse(setOfCustomers.Contains(cust1), "setOfCustomers should not contain cust1");
            Assert.IsTrue(setOfCustomers.Add(cust1), "adding cust1 to setOfCustomers should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust1), "setOfCustomers should contain cust1");

            Assert.IsFalse(setOfCustomers.Contains(cust2), "setOfCustomers should not contain cust2");
            Assert.IsTrue(setOfCustomers.Add(cust2), "adding cust2 to setOfCustomers should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust2), "setOfCustomers should contain cust2");
            Assert.IsTrue(setOfCustomers.Contains(cust1), "setOfCustomers should contain cust1");

        }

        [Test]
        public void RemoveTest1()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            Assert.IsFalse(setOfCustomers.Contains(cust1), "setOfCustomers should not contain cust1");
            Assert.IsTrue(setOfCustomers.Add(cust1), "adding cust1 to setOfCustomers should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust1), "setOfCustomers should contain cust1");
            Assert.IsTrue(setOfCustomers.Remove(cust1), "removing cust1 from setOfCustomers should return true");
            Assert.IsFalse(setOfCustomers.Contains(cust1), "setOfCustomers should not contain cust1");
            Assert.IsFalse(setOfCustomers.Remove(cust2), "removing cust2 from setOfCustomers should return false");

            Assert.IsFalse(setOfCustomers.Contains(cust2), "setOfCustomers should not contain cust2");
            Assert.IsTrue(setOfCustomers.Add(cust2), "adding cust2 to setOfCustomers should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust2), "setOfCustomers should contain cust2");
            Assert.IsTrue(setOfCustomers.Remove(cust2), "removing cust2 from setOfCustomers should return true");
            Assert.IsFalse(setOfCustomers.Contains(cust2), "setOfCustomers should not contain cust2");

            Assert.IsFalse(setOfCustomers.Contains(cust3), "setOfCustomers should not contain cust3");
            Assert.IsTrue(setOfCustomers.Add(cust3), "adding cust3 to setOfCustomers should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust3), "setOfCustomers should contain cust3");
            Assert.IsTrue(setOfCustomers.Remove(cust3), "removing cust3 from setOfCustomers should return true");
            Assert.IsFalse(setOfCustomers.Contains(cust3), "setOfCustomers should not contain cust3");
        }

        [Test]
        public void ToStringTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            string s = setOfCustomers.ToString();
            Assert.AreEqual("{}", s);
            Assert.IsTrue(setOfCustomers.Add(cust1), "adding cust1 to setOfCustomers should return true");
            s = setOfCustomers.ToString();
            Assert.AreEqual("{customer1}", s);
            Assert.IsTrue(setOfCustomers.Add(cust2), "adding cust2 to setOfCustomers should return true");
            s = setOfCustomers.ToString();
            Assert.AreEqual("{customer2,customer1}", s);
            Assert.IsFalse(setOfCustomers.Add(cust1), "adding cust1 to setOfCustomers again should return false");
            s = setOfCustomers.ToString();
            Assert.AreEqual("{customer2,customer1}", s);
            Assert.IsTrue(setOfCustomers.Remove(cust1), "removing cust1 to setOfCustomers should return true");
            s = setOfCustomers.ToString();
            Assert.AreEqual("{customer2}", s);
        }

        [Test]
        public void AddAllTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            bool b = setOfCustomers.AddAll(new Customer[] { cust1, cust2, cust3, cust4 });
            Assert.AreEqual(true, b, "adding array of ints to setOfAllInts should return true");
            Assert.IsTrue(setOfCustomers.Contains(cust1), "setOfCustomers should contain cust1");
            Assert.IsTrue(setOfCustomers.Contains(cust2), "setOfCustomers should contain cust2");
            Assert.IsTrue(setOfCustomers.Contains(cust3), "setOfCustomers should contain cust3");
            Assert.IsTrue(setOfCustomers.Contains(cust4), "setOfCustomers should contain cust4");
            Assert.IsFalse(setOfCustomers.Contains(cust5), "setOfCustomers should not contain cust5");
        }

        [Test]
        public void RemoveAllTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            setOfCustomers.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Assert.AreEqual("{customer5,customer4,customer3,customer2,customer1}", setOfCustomers.ToString());
            Assert.IsTrue(setOfCustomers.RemoveAll(new Customer[] { cust2, cust3, cust4 }), "Remove all should return true");
            Assert.AreEqual("{customer5,customer1}", setOfCustomers.ToString());
        }

        [Test]
        public void ClearTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            setOfCustomers.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Assert.AreEqual("{customer5,customer4,customer3,customer2,customer1}", setOfCustomers.ToString());
            setOfCustomers.Clear();
            Assert.AreEqual("{}", setOfCustomers.ToString());
        }

        [Test]
        public void ContainsTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            setOfCustomers.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Assert.IsTrue(setOfCustomers.Contains(cust1), "setOfCustomers should contain cust1");
            Assert.IsTrue(setOfCustomers.Contains(cust2), "setOfCustomers should contain cust2");
            Assert.IsTrue(setOfCustomers.Contains(cust3), "setOfCustomers should contain cust3");
            Assert.IsTrue(setOfCustomers.Contains(cust4), "setOfCustomers should contain cust4");
            Assert.IsTrue(setOfCustomers.Contains(cust5), "setOfCustomers should contain cust5");
            Assert.IsFalse(setOfCustomers.Contains(cust6), "setOfCustomers should not contain cust6");
        }

        [Test]
        public void ContainsAllTest()
        {
            SetOf<Customer> setOfCustomers = new SetOf<Customer>();
            bool b = setOfCustomers.ContainsAll(new Customer[] {  cust1, cust3, cust5 });
            Assert.IsFalse(b, "setOfCustomers.ContainsAll({  cust1, cust3, cust5 }) should return false");
            b = setOfCustomers.AddAll(new Customer[] {  cust1, cust3, cust5 });
            Assert.IsTrue(b, "setOfCustomers.AddAll({  cust1, cust3, cust5 }) should return true");
            b = setOfCustomers.ContainsAll(new Customer[] {  cust1, cust3, cust5 });
            Assert.IsTrue(b, "setOfCustomers.ContainsAll({  cust1, cust3, cust5 }) should return true");
            b = setOfCustomers.ContainsAll(new Customer[] {  cust2, cust4, cust6 });
            Assert.IsFalse(b, "setOfCustomers.ContainsAll({  cust2, cust4, cust6 }) should return false");
        }

        [Test]
        public void EqualsTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Assert.IsTrue(set1.Equals(set1), "set1 should equal itself");
            SetOf<Customer> set2 = new SetOf<Customer>();
            set2.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Assert.IsTrue(set1.Equals(set2), "set1 should equal set2");
            Assert.IsTrue(set2.Equals(set1), "set2 should equal set1");
            set2.Remove(cust3);
            Assert.IsFalse(set1.Equals(set2), "set1 should not equal set2 following removal of set2.Remove(13)");
            Assert.IsFalse(set2.Equals(set1), "set2 should not equal set1 following removal of set2.Remove(13)");
            set1.Remove(cust3);
            Assert.IsTrue(set1.Equals(set2), "set1 should equal set2 following set1.Remove(13)");
            Assert.IsTrue(set2.Equals(set1), "set2 should equal set1 following set1.Remove(13)");
        }

        [Test]
        public void ExclusiveOrTest()
        {
            // the exclusive or contains elements in one or the other set but not both
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4 });
            SetOf<Customer> set2 = new SetOf<Customer>();
            set2.AddAll(new Customer[] { cust3, cust4, cust5, cust6 });
            ISet exorSet = set1.ExclusiveOr(set2);
            string s = exorSet.ToString();
            const string expected = "{customer6,customer5,customer2,customer1}";
            Assert.AreEqual(expected, s);
            // check we get the same result if we cast set2 to ISet:
            exorSet = set1.ExclusiveOr((ISet)set2);
            s = exorSet.ToString();
            Assert.AreEqual(expected, s);
            // and finally check the ExclusiveOr operator:
            exorSet = set1 ^ set2;
            s = exorSet.ToString();
            Assert.AreEqual(expected, s);
        }

        [Test]
        public void IntersectTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4 });
            SetOf<Customer> set2 = new SetOf<Customer>();
            set2.AddAll(new Customer[] { cust3, cust4, cust5, cust6 });
            ISet intersectSet = set1.Intersect(set2);
            string s = intersectSet.ToString();
            const string expected = "{customer4,customer3}";
            Assert.AreEqual(expected, s);
            // check we get the same result if we cast set2 to ISet:
            intersectSet = set1.Intersect((ISet)set2);
            s = intersectSet.ToString();
            Assert.AreEqual(expected, s);
            // and finally check the intersection operator:
            intersectSet = set1 & set2;
            s = intersectSet.ToString();
            Assert.AreEqual(expected, s);
        }

        [Test]
        public void IsEmptyTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            Assert.IsTrue(set1.IsEmpty, "set1 should be empty");
            set1.Add(cust1);
            Assert.IsFalse(set1.IsEmpty, "set1 should not be empty");
        }

        [Test]
        public void MinusTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4 });
            SetOf<Customer> set2 = new SetOf<Customer>();
            set2.AddAll(new Customer[] { cust3, cust4, cust5, cust6 });
            ISet minusSet = set1.Minus(set2);
            string s = minusSet.ToString();
            const string expected = "{customer2,customer1}";
            Assert.AreEqual(expected, s);
            // check we get the same result if we cast set2 to ISet:
            minusSet = set1.Minus((ISet)set2);
            s = minusSet.ToString();
            Assert.AreEqual(expected, s);
            // and finally check the minus operator:
            minusSet = set1 - set2;
            s = minusSet.ToString();
            Assert.AreEqual(expected, s);
        }

        [Test]
        public void UnionTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4 });
            SetOf<Customer> set2 = new SetOf<Customer>();
            set2.AddAll(new Customer[] { cust3, cust4, cust5, cust6 });
            ISet unionSet = set1.Union(set2);
            string s = unionSet.ToString();
            const string expected = "{customer6,customer5,customer4,customer3,customer2,customer1}";
            Assert.AreEqual(expected, s);
            // check we get the same result if we cast set2 to ISet:
            unionSet = set1.Union((ISet)set2);
            s = unionSet.ToString();
            Assert.AreEqual(expected, s);
            // and finally check the union operator:
            unionSet = set1 | set2;
            s = unionSet.ToString();
            Assert.AreEqual(expected, s);
        }

        #endregion

        #region ICollection Members

        [Test]
        public void CopyToTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            Object[] objArray = new Object[10];
            // this should not throw any errors:
            set1.CopyTo(objArray, 0);
            string s = GetElementsAsCommaSeparatedList(objArray);
            Assert.AreEqual("customer5,customer4,customer3,customer2,customer1", s);
        }

        [Test]
        public void CountTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5, cust6 });
            Assert.AreEqual(6, set1.Count, "set1.Count");
            set1.Remove(cust3);
            Assert.AreEqual(5, set1.Count, "set1.Count");
        }

        #endregion

        #region IEnumerable Members

        [Test]
        public void GetEnumeratorTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            string s = GetElementsAsCommaSeparatedList(set1);
            Assert.AreEqual("customer5,customer4,customer3,customer2,customer1", s);
            long n = CountElements(set1);
            Assert.AreEqual(5, n);
        }

        string GetElementsAsCommaSeparatedList(IEnumerable collection)
        {
            string s = "";
            foreach (object o in collection)
            {
                if (o != null)
                {
                    if (s.Length > 0)
                        s += ",";
                    s += o.ToString();
                }
            }
            return s;
        }

        long CountElements(IEnumerable collection)
        {
            long n = 0;
            foreach (object o in collection)
                n++;
            return n;
        }


        #endregion

        #region ICloneable Members

        [Test]
        public void CloneTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            set1.AddAll(new Customer[] { cust1, cust2, cust3, cust4, cust5 });
            ISet p = (ISet)set1.Clone();
            Assert.IsNotNull(p, "return from Clone() should not be null");
            Assert.IsTrue(p.Equals(set1), "return from Clone() should equal set1");
        }
        #endregion

        [Test]
        public void GetElementTypeTest()
        {
            SetOf<Customer> set1 = new SetOf<Customer>();
            // how can I get this to work:
            //Type t = set1.GetType();
            //Type et = t.GetElementType(); //! this returns null
            Type et = set1.GetElementType();
            string s = (et == null) ? "null" : et.ToString();
            Assert.AreEqual("KMB.Library.Collections.Tests.Customer", s, "element type should be Customer");
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Imagine Communications
United Kingdom United Kingdom
I have been working in IT since 1975, in various roles from junior programmer to system architect, and with many different languages and platforms. I have written shedloads of code.

I now live in Bedfordshire, England. As well as working full time I am the primary carer for my wife who has MS. I am learning to play the piano. I have three grown up children and a cat.

Comments and Discussions