using System; using Developpez.Dotnet.Collections; using NUnit.Framework; namespace Developpez.Dotnet.Tests.Collections { [TestFixture] public class DefaultDictionaryTests { [Test] public void Test_WithGenerator() { Func generator = key => key.Length; var dic = new DefaultDictionary(generator); string s = "Hello world !"; int expected = s.Length; int actual = dic[s]; Assert.AreEqual(expected, actual); // key should be added Assert.IsTrue(dic.ContainsKey(s)); s = "foo"; expected = s.Length; // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key shouldn't be added Assert.IsTrue(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } [Test] public void Test_WithDefaultValue() { int defaultValue = 42; var dic = new DefaultDictionary(defaultValue); string s = "Hello world !"; int expected = defaultValue; int actual = dic[s]; Assert.AreEqual(expected, actual); // key should be added Assert.IsTrue(dic.ContainsKey(s)); s = "foo"; expected = defaultValue; // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key should be added Assert.IsTrue(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } [Test] public void Test_WithoutDefault() { var dic = new DefaultDictionary(); string s = "Hello world !"; int expected = default(int); int actual = dic[s]; Assert.AreEqual(expected, actual); // key should be added Assert.IsTrue(dic.ContainsKey(s)); s = "foo"; expected = default(int); // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key should be added Assert.IsTrue(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } [Test] public void Test_WithGenerator_NoAddKeys() { Func generator = key => key.Length; var dic = new DefaultDictionary(generator, false); string s = "Hello world !"; int expected = s.Length; int actual = dic[s]; Assert.AreEqual(expected, actual); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); s = "foo"; expected = s.Length; // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } [Test] public void Test_WithDefaultValue_NoAddKeys() { int defaultValue = 42; var dic = new DefaultDictionary(defaultValue, false); string s = "Hello world !"; int expected = defaultValue; int actual = dic[s]; Assert.AreEqual(expected, actual); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); s = "foo"; expected = defaultValue; // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } [Test] public void Test_WithoutDefault_NoAddKeys() { var dic = new DefaultDictionary(false); string s = "Hello world !"; int expected = default(int); int actual = dic[s]; Assert.AreEqual(expected, actual); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); s = "foo"; expected = default(int); // TryGetValue should always return true Assert.IsTrue(dic.TryGetValue(s, out actual)); // key shouldn't be added Assert.IsFalse(dic.ContainsKey(s)); Assert.AreEqual(expected, actual); } } }