using System; using System.Collections.Generic; using System.Linq; using Developpez.Dotnet.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class OrderedDictionaryTests { [Test] public void Test_AddCountContainsGet() { var dict = new OrderedDictionary(); dict.Add("hello", 42); Assert.AreEqual(1, dict.Count); Assert.IsTrue(dict.ContainsKey("hello")); Assert.AreEqual(dict["hello"], 42); } [Test] public void Test_AddDuplicateKey() { var dict = new OrderedDictionary(); dict.Add("hello", 42); Assert.Throws(() => dict.Add("hello", 0)); } [Test] public void Test_GetMissingKey() { var dict = new OrderedDictionary(); dict.Add("hello", 42); Assert.Throws(() => Console.WriteLine(dict["hell"])); } [Test] public void Test_Set() { var dict = new OrderedDictionary(); dict.Add("hello", 42); dict["aa"] = 3; dict["hello"] = 1; Assert.AreEqual(3, dict["aa"]); Assert.AreEqual(1, dict["hello"]); } [Test] public void Test_KeysValues() { var dict = new OrderedDictionary(); dict.Add("hello", 42); dict.Add("aa", 3); dict["bb"] = 42; dict.Remove("aa"); dict["hello"] = 35; dict["aa"] = 50; dict["cc"] = 1; CollectionAssert.AreEqual(dict.Keys, dict.Select(kvp => kvp.Key)); CollectionAssert.AreEqual(dict.Values, dict.Select(kvp => kvp.Value)); } [Test] public void Test_OrderIsMaintained() { var dict = new OrderedDictionary(); dict.Add("hello", 42); dict.Add("aa", 3); dict["bb"] = 42; dict.Remove("aa"); dict["hello"] = 35; dict["aa"] = 50; dict["cc"] = 1; // dict.Keys, // new[] { "hello", "bb", "aa", "cc" }); CollectionAssert.AreEqual( dict.Keys, new[] { "hello", "bb", "aa", "cc" }); // dict.Values, // new[] { 35, 42, 50, 1 }); CollectionAssert.AreEqual( dict.Values, new[] { 35, 42, 50, 1 }); } } }