Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

Generic Keyed List

, 27 Jan 2006
A KeyedList using C# 2.0 Generics.
cliftoncollectionsgeneric.zip
Clifton.Collections.Generic.UnitTests
Properties
Properties
CVS
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
gkeyedlist.zip
Entries
Entries.Extra
Entries.Extra.Old
Entries.Old
Repository
Root
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;

using Clifton.Collections.Generic;

using Vts.UnitTest;

namespace UnitTests
{
	[TestFixture]
	public class KeyedListTests
	{
		protected KeyedList<string, int> k;

		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			k = new KeyedList<string, int>();
			k.Add("Zero", 0);
			k.Add("One", 1);
			k.Add("Two", 2);
			k.Add("Three", 3);
			k.Add("Four", 4);
			k.Add("Five", 5);
			k.Add("Six", 6);
			k.Add("Seven", 7);
			k.Add("Eight", 8);
			k.Add("Nine", 9);
			k.Add("Ten", 10);
		}

		[Test]
		public void ValueIteratorTest()
		{
			int idx = 0;

			foreach (int val in k.Values)
			{
				Assertion.Assert(val == idx, "Itereator: Expected " + idx.ToString());
				++idx;
			}
		}

		[Test]
		public void ValueByIndexTests()
		{
			Assertion.Assert(k[0].Value == 0, "Indexer: Expected 0");
			Assertion.Assert(k[1].Value == 1, "Indexer: Expected 1");
			Assertion.Assert(k[2].Value == 2, "Indexer: Expected 2");
			Assertion.Assert(k[3].Value == 3, "Indexer: Expected 3");
			Assertion.Assert(k[4].Value == 4, "Indexer: Expected 4");
			Assertion.Assert(k[5].Value == 5, "Indexer: Expected 5");
			Assertion.Assert(k[6].Value == 6, "Indexer: Expected 6");
			Assertion.Assert(k[7].Value == 7, "Indexer: Expected 7");
			Assertion.Assert(k[8].Value == 8, "Indexer: Expected 8");
			Assertion.Assert(k[9].Value == 9, "Indexer: Expected 9");
			Assertion.Assert(k[10].Value == 10, "Indexer: Expected 10");
		}

		[Test]
		public void ValueByKeyTests()
		{
			Assertion.Assert(k["Zero"] == 0, "Lookup: Expected 0");
			Assertion.Assert(k["One"] == 1, "Lookup: Expected 1");
			Assertion.Assert(k["Two"] == 2, "Lookup: Expected 2");
			Assertion.Assert(k["Three"] == 3, "Lookup: Expected 3");
			Assertion.Assert(k["Four"] == 4, "Lookup: Expected 4");
			Assertion.Assert(k["Five"] == 5, "Lookup: Expected 5");
			Assertion.Assert(k["Six"] == 6, "Lookup: Expected 6");
			Assertion.Assert(k["Seven"] == 7, "Lookup: Expected 7");
			Assertion.Assert(k["Eight"] == 8, "Lookup: Expected 8");
			Assertion.Assert(k["Nine"] == 9, "Lookup: Expected 9");
			Assertion.Assert(k["Ten"] == 10, "Lookup: Expected 10");
		}

		[Test]
		public void IndexOfTests()
		{
			Assertion.Assert(k.IndexOf("Zero") == 0, "IndexOf: Expected 0");
			Assertion.Assert(k.IndexOf("One") == 1, "IndexOf: Expected 1");
			Assertion.Assert(k.IndexOf("Two") == 2, "IndexOf: Expected 2");
			Assertion.Assert(k.IndexOf("Three") == 3, "IndexOf: Expected 3");
			Assertion.Assert(k.IndexOf("Four") == 4, "IndexOf: Expected 4");
			Assertion.Assert(k.IndexOf("Five") == 5, "IndexOf: Expected 5");
			Assertion.Assert(k.IndexOf("Six") == 6, "IndexOf: Expected 6");
			Assertion.Assert(k.IndexOf("Seven") == 7, "IndexOf: Expected 7");
			Assertion.Assert(k.IndexOf("Eight") == 8, "IndexOf: Expected 8");
			Assertion.Assert(k.IndexOf("Nine") == 9, "IndexOf: Expected 9");
			Assertion.Assert(k.IndexOf("Ten") == 10, "IndexOf: Expected 10");
		}

		[Test]
		public void ContainsKeyTests()
		{
			Assertion.Assert(k.ContainsKey("Zero"), "Expected true");
			Assertion.Assert(k.ContainsKey("One"), "Expected true");
			Assertion.Assert(k.ContainsKey("Two"), "Expected true");
			Assertion.Assert(k.ContainsKey("Three"), "Expected true");
			Assertion.Assert(k.ContainsKey("Four"), "Expected true");
			Assertion.Assert(k.ContainsKey("Five"), "Expected true");
			Assertion.Assert(k.ContainsKey("Six"), "Expected true");
			Assertion.Assert(k.ContainsKey("Seven"), "Expected true");
			Assertion.Assert(k.ContainsKey("Eight"), "Expected true");
			Assertion.Assert(k.ContainsKey("Nine"), "Expected true");
			Assertion.Assert(k.ContainsKey("Ten"), "Expected true");
		}

		[Test]
		public void ContainsTests()
		{
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Zero", 0)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("One", 1)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Two", 2)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Three", 3)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Four", 4)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Five", 5)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Six", 6)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Seven", 7)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Eight", 8)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Nine", 9)), "Expected true");
			Assertion.Assert(k.Contains(new KeyValuePair<string, int>("Ten", 10)), "Expected true");
		}

		[Test]
		public void NotContainsKeyTest()
		{
			Assertion.Assert(!k.ContainsKey("Eleven"), "Expected false");
		}

		[Test]
		void NotContainsTests()
		{
			Assertion.Assert(!k.Contains(new KeyValuePair<string, int>("Zero", 5)), "list does not contain kvp Zero-5");
			Assertion.Assert(!k.Contains(new KeyValuePair<string, int>("Eleven", 11)), "list does not contain kvp Eleven-11");
		}

		[Test]
		public void OrderedKeyTest()
		{
			int idx = 0;

			foreach (string key in k.OrderedKeys)
			{
				Assertion.Assert(key == k.GetKey(idx++), "Expected key to match indexed key.");
			}
		}

		[Test]
		public void OrderedValueTest()
		{
			int idx = 0;

			foreach (int val in k.OrderedValues)
			{
				Assertion.Assert(val==k[idx++].Value, "Expected value to match indexed value.");
			}
		}

		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void NegativeIndexTest()
		{
			int foo = k[-1].Value;
		}

		[Test]
		[ExpectedException(typeof(ArgumentOutOfRangeException))]
		public void IndexToLargeTest()
		{
			int foo = k[k.Count].Value;
		}

		[Test]
		[ExpectedException(typeof(KeyNotFoundException))]
		public void BadKeyTest()
		{
			int foo = k["Eleven"];
		}

		[Test]
		public void IndexOfNotFound()
		{
			Assertion.Assert(k.IndexOf("Foo") == -1, "Expected the returned index to be -1.");
		}
	}

	[TestFixture]
	[ProcessTest]
	public class SequencedKeyedListTests
	{
		protected KeyedList<string, double> k;

		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			k = new KeyedList<string, double>();
			k.Add("Zero", 0);
			k.Add("One", 1);
			k.Add("Two", 2);
			k.Add("Three", 3);
			k.Add("Four", 4);
			k.Add("Five", 5);
			k.Add("Six", 6);
			k.Add("Seven", 7);
			k.Add("Eight", 8);
			k.Add("Nine", 9);
			k.Add("Ten", 10);
		}


		[Test, Sequence(1)]
		public void CopyToTest()
		{
			KeyValuePair<string, double>[] kvpa = new KeyValuePair<string, double>[k.Count];
			k.CopyTo(kvpa, 0);
			int idx = 0;

			foreach (KeyValuePair<string, double> kvp in kvpa)
			{
				Assertion.Assert(kvp.Key == k.GetKey(idx), "KeyValuePair not ordered.");
				Assertion.Assert(kvp.Value == k[idx].Value, "KeyValuePair not ordered.");
				++idx;
			}
		}

		[Test, Sequence(2), ExpectedException(typeof(System.ArgumentException))]
		public void CopyToArraySizeExceptionTest()
		{
			KeyValuePair<string, double>[] kvpa = new KeyValuePair<string, double>[k.Count];
			k.CopyTo(kvpa, 5);
		}

		[Test, Sequence(3)]
		public void AddTest()
		{
			k.Add("Eleven", 11);
			Assertion.Assert(k[k.Count - 1].Value == 11, "Expected the last entry to be 11.");
		}

		[Test, Sequence(4)]
		public void AddTestByKeyValuePair()
		{
			k.Add(new KeyValuePair<string, double>("Twelve", 12));
			Assertion.Assert(k[k.Count - 1].Value == 12, "Expected the last entry to be 12.");
		}

		[Test, Sequence(5)]
		public void InsertTest()
		{
			k.Insert(2, "One Point Five", 1.5);
			Assertion.Assert(k[1].Value == 1, "Expected [1] entry to = 1");
			Assertion.Assert(k[2].Value == 1.5, "Expected [2] entry to = 1.5");
			Assertion.Assert(k[3].Value == 2, "Expected [3] entry to = 2");
		}

		[Test, Sequence(6)]
		public void RemoveTest()
		{
			bool ret=k.Remove("One Point Five");
			Assertion.Assert(ret, "Expected true");
			Assertion.Assert(k[1].Value == 1, "Expected [1] entry to = 1");
			Assertion.Assert(k[2].Value == 2, "Expected [2] entry to = 2");
		}

		[Test, Sequence(7)]
		public void RemoveNotFoundTest()
		{
			bool ret = k.Remove("One Point Five");
			Assertion.Assert(!ret, "Expected false");
		}

		[Test, Sequence(8)]
		public void RemoveByKeyValuePair()
		{
			KeyValuePair<string, double> kvp = new KeyValuePair<string, double>("Eleven", 11);
			bool ret = k.Remove(kvp);
			Assertion.Assert(ret, "Expected true");
			Assertion.Assert(k[11].Value == 12, "Expected entry 11 to be removed");
		}

		[Test, Sequence(9)]
		public void RemoveNotFoundByKeyValuePair()
		{
			// value is ignored.
			KeyValuePair<string, double> kvp = new KeyValuePair<string, double>("Eleven", 0);
			bool ret = k.Remove(kvp);
			Assertion.Assert(!ret, "Expected false");
		}

		[Test, Sequence(10)]
		public void RemoveAtTest()
		{
			k.RemoveAt(1);
			Assertion.Assert(k[0].Value == 0, "Expected [0] entry to = 0");
			Assertion.Assert(k[1].Value == 2, "Expected [1] entry to = 2");
		}

		[Test, Sequence(11)]
		public void TryGetValueTest()
		{
			double v;
			bool ret = k.TryGetValue("Ten", out v);
			Assertion.Assert(ret, "Expected true");
			Assertion.Assert(v == 10, "Expected return value = 10");
		}

		[Test, Sequence(12)]
		public void TryGetValueNotFoundTest()
		{
			double v;
			bool ret = k.TryGetValue("Elevent", out v);
			Assertion.Assert(!ret, "Expected false");
			Assertion.Assert(v == 0, "Expected return value = default value");
		}

		[Test, Sequence(13)]
		public void EmptyListInsertAtZero()
		{
			k.Clear();
			k.Insert(0, "Z", 0);
			Assertion.Assert(k[0].Value == 0, "Expected a value of 0");
		}
	}

	[TestFixture]
	public class EnumeratorTests
	{
		protected KeyedList<string, int> k;

		[TestFixtureSetUp]
		public void FixtureSetup()
		{
			k = new KeyedList<string, int>();
			k.Add("Zero", 0);
			k.Add("One", 1);
			k.Add("Two", 2);
			k.Add("Three", 3);
			k.Add("Four", 4);
			k.Add("Five", 5);
			k.Add("Six", 6);
			k.Add("Seven", 7);
			k.Add("Eight", 8);
			k.Add("Nine", 9);
			k.Add("Ten", 10);
		}

		[Test]
		public void DefaultEnumerator()
		{
			//foreach(object obj in k)
			//{
			//}
		}

		[Test]
		public void EnumerationTest()
		{
			int n = 0;

			foreach (KeyValuePair<string, int> kvp in k)
			{
				Assertion.Assert(kvp.Value == n, "Expected ordered list of values.");
				++n;
			}
		}

		[Test]
		public void ValueEnumeratorTest()
		{
			int n = 0;

			foreach (int i in k.OrderedValues)
			{
				Assertion.Assert(n == i, "Expected ordered list of values.");
				++n;
			}
		}

		[Test]
		public void KeyEnumeratorTest()
		{
			int n = 0;

			foreach (string s in k.OrderedKeys)
			{
				Assertion.Assert(k.GetKey(n) == s, "Expected ordered list of keys.");
				++n;
			}
		}
	}
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projets, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website, www.marcclifton.com, where you will find many of his articles and his blog.
 
Marc lives in Philmont, NY.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 27 Jan 2006
Article Copyright 2006 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid