#define ORDERED_DICTIONARY
// ORDERED_DICTIONARYをundefするとSystem.Collections.Generic.Dictionaryと同じ動作となることの確認用テストコードとなる
//#undef ORDERED_DICTIONARY
using System;
using System.Collections.Generic;
using NUnit.Framework;
#if ORDERED_DICTIONARY
using Dict = OrderedDictionary<string, int>;
#else
using Dict = System.Collections.Generic.Dictionary<string, int>;
#endif
using Pair = System.Collections.Generic.KeyValuePair<string, int>;
[TestFixture]
public class OrderedDictionaryTests {
#if ORDERED_DICTIONARY
[Test]
public void MajorUseCase1()
{
var dict = new Dict();
dict.Add("foo", 16);
dict.Add("baz", 42);
dict.Insert(1, "bar", 72);
CollectionAssert.AreEqual(new[] {"foo", "bar", "baz"}, dict.Keys, "#1");
CollectionAssert.AreEqual(new[] {16, 72, 42}, dict.Values, "#2");
dict["bar"] = 36;
Assert.AreEqual(36, dict["bar"]);
CollectionAssert.AreEqual(new[] {"foo", "bar", "baz"}, dict.Keys, "#3");
CollectionAssert.AreEqual(new[] {16, 36, 42}, dict.Values, "#4");
dict.Clear();
CollectionAssert.IsEmpty(dict, "#5");
CollectionAssert.IsEmpty(dict.Keys, "#6");
CollectionAssert.IsEmpty(dict.Values, "#7");
}
[Test]
public void MajorUseCase2()
{
var dict = new Dict();
dict.Add("foo", 16);
dict.Add("bar", 72);
dict.Add("baz", 42);
var expectedKeys = new[] {"foo", "bar", "baz"};
var expectedValues = new[] {16, 72, 42};
for (var index = 0; index < dict.Count; index++) {
Assert.AreEqual(expectedKeys[index], dict[index].Key, "#1 key of index {0}", index);
Assert.AreEqual(expectedValues[index], dict[index].Value, "#2 value of index {0}", index);
}
}
#endif
[Test]
public void TestEqualityComparer()
{
var dict = new Dict(StringComparer.OrdinalIgnoreCase);
dict.Add("a", 1);
Assert.AreEqual(1, dict["A"], "#1");
#if ORDERED_DICTIONARY
Assert.AreEqual("a", dict[0].Key, "#2");
#endif
Assert.Throws<ArgumentException>(() => dict.Add("A", 2), "#3");
}
[Test]
public void TestSetByKey()
{
var dict = new Dict();
Assert.DoesNotThrow(() => dict["a"] = 1, "#1 new entry");
Assert.AreEqual(1, dict["a"], "#2");
Assert.DoesNotThrow(() => dict["a"] = 2, "#3 existent entry");
Assert.AreEqual(2, dict["a"], "#4");
}
#if ORDERED_DICTIONARY
[Test]
public void TestSetByIndex()
{
var dict = new Dict();
Assert.Throws<ArgumentOutOfRangeException>(() => dict[0] = new Pair("a", 1), "#1 new entry");
dict.Add("a", 1);
Assert.DoesNotThrow(() => dict[0] = new Pair("b", 2), "#2 existent entry");
Assert.AreEqual(1, dict.Count, "#3");
Assert.AreEqual(2, dict["b"], "#4");
}
[Test]
public void TestSetByIndexExistentKey()
{
var dict = new Dict();
dict.Add("a", 1);
dict.Add("b", 2);
Assert.Throws<ArgumentException>(() => dict[0] = new Pair("b", 3), "#1 existent key");
Assert.AreEqual("a", dict[0].Key, "#2");
}
#endif
[Test]
public void TestGetByKey()
{
var dict = new Dict();
dict["a"] = 1;
Assert.AreEqual(1, dict["a"], "#1 existent entry");
Assert.Throws<KeyNotFoundException>(() => {int b = dict["b"];}, "#2 non existent entry");
}
#if ORDERED_DICTIONARY
[Test]
public void TestGetByIndex()
{
var dict = new Dict();
dict["a"] = 1;
Assert.AreEqual("a", dict[0].Key, "#1 existent entry");
Assert.AreEqual(1, dict[0].Value, "#2 existent entry");
Assert.Throws<ArgumentOutOfRangeException>(() => {var pair = dict[1];}, "#3 non existent entry");
}
#endif
[Test]
public void TestAddKeyValue()
{
var dict = new Dict();
Assert.DoesNotThrow(() => dict.Add("a", 1), "#1 add new");
Assert.AreEqual(1, dict.Count, "#2");
Assert.AreEqual(1, dict["a"], "#3");
#if ORDERED_DICTIONARY
Assert.AreEqual("a", dict[0].Key, "#4");
Assert.AreEqual(1, dict[0].Value, "#5");
#endif
Assert.Throws<ArgumentException>(() => dict.Add("a", 2), "#6 add existent key");
#if ORDERED_DICTIONARY
Assert.Throws<ArgumentException>(() => dict.Add(new Pair("a", 3)), "#7 add existent key");
#endif
}
#if ORDERED_DICTIONARY
[Test]
public void TestInsertKeyValue()
{
var dict = new Dict();
dict.Add("a", 1);
Assert.DoesNotThrow(() => dict.Insert(0, "b", 2), "#1 insert new");
Assert.AreEqual(2, dict.Count, "#2");
Assert.AreEqual(2, dict["b"], "#3");
Assert.AreEqual("b", dict[0].Key, "#4");
Assert.AreEqual(2, dict[0].Value, "#5");
Assert.Throws<ArgumentException>(() => dict.Insert(0, "a", 2), "#6 insert existent key");
Assert.Throws<ArgumentException>(() => dict.Insert(1, "a", 2), "#7 insert existent key");
Assert.Throws<ArgumentException>(() => dict.Insert(0, new Pair("a", 3)), "#8 add existent key");
Assert.Throws<ArgumentException>(() => dict.Insert(1, new Pair("a", 3)), "#9 add existent key");
Assert.Throws<ArgumentOutOfRangeException>(() => dict.Insert(3, "c", 3), "#10 insert to out of range");
Assert.Throws<ArgumentOutOfRangeException>(() => dict.Insert(-1, "c", 3), "#11 insert to out of range");
}
#endif
[Test]
public void TestRemoveByKey()
{
var dict = new Dict();
dict.Add("a", 1);
Assert.IsFalse(dict.Remove("b"), "#1 remove non existent key");
Assert.AreEqual(1, dict.Count, "#2");
Assert.IsTrue(dict.Remove("a"), "#3 remove existent key");
Assert.AreEqual(0, dict.Count, "#4");
}
[Test]
public void TestContainsKey()
{
var dict = new Dict();
dict.Add("a", 1);
Assert.IsFalse(dict.ContainsKey("b"), "#1 non existent key");
Assert.IsTrue(dict.ContainsKey("a"), "#2 existent key");
}
[Test]
public void TestTryGetValue()
{
var dict = new Dict();
dict.Add("a", 1);
int val;
Assert.IsFalse(dict.TryGetValue("b", out val), "#1 non existent key");
Assert.IsTrue(dict.TryGetValue("a", out val), "#2 existent key");
Assert.AreEqual(1, val, "#3");
}
#if ORDERED_DICTIONARY
[Test]
public void TestKeysReadOnly()
{
var dict = new Dict();
Assert.Throws<NotSupportedException>(() => dict.Keys.Add("x"), "#1");
}
[Test]
public void TestValuesReadOnly()
{
var dict = new Dict();
Assert.Throws<NotSupportedException>(() => dict.Values.Add(-1), "#1");
}
#endif
[Test]
public void TestKeys()
{
var dict = new Dict();
Assert.IsNotNull(dict.Keys, "#1-1 initial state");
CollectionAssert.IsEmpty(dict.Keys, "#1-2 initial state");
dict.Add("a", 1);
CollectionAssert.AreEqual(new[] {"a"}, dict.Keys, "#2-1 after insert item");
dict.Add("b", 2);
CollectionAssert.AreEqual(new[] {"a", "b"}, dict.Keys, "#2-2 after insert item");
dict["a"] = 0;
CollectionAssert.AreEqual(new[] {"a", "b"}, dict.Keys, "#3 after set item");
dict.Remove("a");
CollectionAssert.AreEqual(new[] {"b"}, dict.Keys, "#3 after remove item");
dict.Clear();
CollectionAssert.IsEmpty(dict.Keys, "#4 after clear");
}
[Test]
public void TestValues()
{
var dict = new Dict();
Assert.IsNotNull(dict.Values, "#1-1 initial state");
CollectionAssert.IsEmpty(dict.Values, "#1-2 initial state");
dict.Add("a", 1);
CollectionAssert.AreEqual(new[] {1}, dict.Values, "#2-1 after insert item");
dict.Add("b", 2);
CollectionAssert.AreEqual(new[] {1, 2}, dict.Values, "#2-2 after insert item");
dict["a"] = 0;
CollectionAssert.AreEqual(new[] {0, 2}, dict.Values, "#3 after set item");
dict.Remove("a");
CollectionAssert.AreEqual(new[] {2}, dict.Values, "#3 after remove item");
dict.Clear();
CollectionAssert.IsEmpty(dict.Values, "#4 after clear");
}
}