view Collections/CollectionsTest.cs @ 40:8e8d0dc4ba20

Re #48: Collection equality * Add collection equality tests for dictionaries
author IBBoard <dev@ibboard.co.uk>
date Tue, 26 Apr 2011 14:31:36 +0000
parents 8b7aa46c4513
children 391b0b7f520e
line wrap: on
line source

//  This file (CollectionsTest.cs) is a part of the IBBoard.Tests project and is copyright 2011 IBBoard
// 
//  The file and the library/program it is in are licensed and distributed, without warranty, under the GNU LGPL, either version 3 of the License or (at your option) any later version. Please see COPYING for more information and the full license.
using System;
using NUnit.Framework;
using System.Collections.Generic;
using NUnit.Framework.SyntaxHelpers;

namespace IBBoard.Collections
{
	[TestFixture()]
	public class CollectionsTest
	{
		[Test]
		public void TestListSelfEquality()
		{
			IList<int> list1 = new List<int>();
			Assert.That(Collections.AreEqual(list1, list1), Is.True);
		}

		[Test()]
		public void TestEmptyListEquality()
		{
			IList<int> list1 = new List<int>();
			IList<int> list2 = new List<int>();
			Assert.That(Collections.AreEqual(list1, list2), Is.True);
			Assert.That(Collections.AreEqual(list2, list1), Is.True);
		}

		[Test()]
		public void TestEmptyAndNonEmptyListForInequality()
		{
			IList<int> list1 = new List<int>();
			IList<int> list2 = new List<int>();
			list1.Add(1);
			Assert.That(Collections.AreEqual(list1, list2), Is.False);
			Assert.That(Collections.AreEqual(list2, list1), Is.False);
		}

		[Test]
		public void TestDifferentNonEmptyListsForInequality()
		{
			IList<int> list1 = new List<int>();
			list1.Add(1);
			IList<int> list2 = new List<int>();
			list2.Add(2);
			Assert.That(Collections.AreEqual(list1, list2), Is.False);
			Assert.That(Collections.AreEqual(list2, list1), Is.False);
		}

		[Test]
		public void TestMatchingNonEmptyListsForEquality()
		{
			IList<int> list1 = new List<int>();
			list1.Add(1);
			IList<int> list2 = new List<int>();
			list2.Add(1);
			Assert.That(Collections.AreEqual(list1, list2), Is.True);
			Assert.That(Collections.AreEqual(list2, list1), Is.True);
		}

		[Test]
		public void TestDifferentOrderedListsForInequality()
		{
			IList<int> list1 = new List<int>();
			list1.Add(1);
			list1.Add(2);
			list1.Add(3);
			IList<int> list2 = new List<int>();
			list2.Add(3);
			list2.Add(2);
			list2.Add(1);
			Assert.That(Collections.AreEqual(list1, list2), Is.False);
			Assert.That(Collections.AreEqual(list2, list1), Is.False);
		}

		[Test]
		public void TestDictionarySelfEquality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			Assert.That(Collections.AreEqual(dict1, dict1), Is.True);
		}

		[Test()]
		public void TestEmptyDictionaryEquality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			Assert.That(Collections.AreEqual(dict1, dict2), Is.True);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.True);
		}

		[Test()]
		public void TestEmptyAndNonEmptyDictionaryForInequality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.False);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.False);
		}

		[Test]
		public void TestDifferentNonEmptyDictionarysForInequality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict2.Add("key2", 2);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.False);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.False);
		}

		[Test]
		public void TestMatchingNonEmptyDictionarysForEquality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict2.Add("key1", 1);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.True);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.True);
		}

		[Test]
		public void TestDifferentKeyedDictionarysForInequality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			dict1.Add("key2", 2);
			dict1.Add("key3", 3);
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict2.Add("key4", 1);
			dict2.Add("key5", 2);
			dict2.Add("key6", 3);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.False);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.False);
		}

		[Test]
		public void TestDifferentValuedDictionarysForInequality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			dict1.Add("key2", 2);
			dict1.Add("key3", 3);
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict2.Add("key3", 4);
			dict2.Add("key2", 5);
			dict2.Add("key1", 6);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.False);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.False);
		}

		[Test]
		public void TestDifferentKeyValuePairingDictionarysForInequality()
		{
			IDictionary<string, int> dict1 = new Dictionary<string, int>();
			dict1.Add("key1", 1);
			dict1.Add("key2", 2);
			dict1.Add("key3", 3);
			IDictionary<string, int> dict2 = new Dictionary<string, int>();
			dict2.Add("key1", 3);
			dict2.Add("key2", 2);
			dict2.Add("key3", 1);
			Assert.That(Collections.AreEqual(dict1, dict2), Is.False);
			Assert.That(Collections.AreEqual(dict2, dict1), Is.False);
		}
	}
}