// // SortedDictionaryTest.cs // // Author: // Atsushi Enomoto // // Copyright (C) 2006 Novell, Inc (http://www.novell.com) // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // #if NET_2_0 using System; using System.IO; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using NUnit.Framework; namespace MonoTests.System.Collections.Generic { [TestFixture] public class SortedDictionaryTest { [Test] public void CtorNullComparer () { SortedDictionary sd = new SortedDictionary ((IComparer) null); Assert.AreEqual (Comparer.Default, sd.Comparer); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void CtorNullDictionary () { new SortedDictionary (default (IDictionary)); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void CtorComparerDictionaryNullComparer () { new SortedDictionary (default (IDictionary), null); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void CtorComparerDictionaryNullDictionary () { new SortedDictionary (null, default (IComparer)); } [Test] public void CtorDefault () { SortedDictionary d = new SortedDictionary (); Assert.IsNotNull (d.Comparer); } [Test] public void CtorDictionary () { Dictionary src = new Dictionary (); src.Add (0, "Foo"); src.Add (4, "Bar"); src.Add (2, "Baz"); SortedDictionary d = new SortedDictionary (src); Assert.AreEqual (3, d.Count, "#1"); Assert.AreEqual ("Bar", d [4], "#2"); IDictionaryEnumerator e = d.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual ("Foo", e.Value, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual ("Baz", e.Value, "#6"); Assert.IsTrue (e.MoveNext (), "#7"); Assert.AreEqual ("Bar", e.Value, "#8"); src.Add (3, "Hoge"); // it does not affect. Assert.AreEqual (3, d.Count, "#9"); } [Test] [ExpectedException (typeof (ArgumentException))] public void AddDuplicate () { SortedDictionary d = new SortedDictionary (); d.Add (0, "A"); d.Add (0, "A"); } [Test] public void AddNullValue () { SortedDictionary d = new SortedDictionary (); d.Add (0, null); d.Add (1, "A"); d.Add (2, null); Assert.IsNull (d [0], "#0"); Assert.AreEqual ("A", d [1], "#1"); Assert.IsNull (d [2], "#2"); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void AddNullKey () { SortedDictionary d = new SortedDictionary (); d.Add (null, null); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void AddNullKeyNullable () { SortedDictionary d = new SortedDictionary (); d.Add (null, "TEST"); } [Test] [ExpectedException (typeof (KeyNotFoundException))] public void GetItemNonexistent () { SortedDictionary d = new SortedDictionary (); Assert.AreEqual (0, d [0]); // does not exist. } [Test] public void SetItemNonexistent () { SortedDictionary d = new SortedDictionary (); d [0] = 1; Assert.AreEqual (1, d.Count); } [Test] public void SetItemExistent () { SortedDictionary d = new SortedDictionary (); d.Add (0, 0); Assert.AreEqual (1, d.Count, "#1"); d [0] = 1; Assert.AreEqual (1, d.Count, "#2"); Assert.AreEqual (1, d [0], "#3"); } [Test] public void GetEnumerator1 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); SortedDictionary.Enumerator e = d.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#1"); Assert.AreEqual ("A", e.Current.Value, "#2"); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual ("C", e.Current.Value, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual ("B", e.Current.Value, "#6"); Assert.IsFalse (e.MoveNext (), "#7"); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void GetEnumerator2 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); IEnumerator e = d.GetEnumerator (); d.Add (4, "D"); e.MoveNext (); } [Test] public void CustomComparer () { SortedDictionary d = new SortedDictionary ( ReverseComparer.Instance); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); SortedDictionary.Enumerator e = d.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#1"); Assert.AreEqual ("B", e.Current.Value, "#2"); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual ("C", e.Current.Value, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual ("A", e.Current.Value, "#6"); Assert.IsFalse (e.MoveNext (), "#7"); } [Test] public void Remove () { SortedDictionary d = new SortedDictionary (); Assert.IsFalse (d.Remove (0), "#1"); d.Add (0, "Foo"); Assert.IsTrue (d.Remove (0), "#2"); Assert.IsFalse (d.Remove (0), "#3"); } [Test] public void TryGetValue () { SortedDictionary d = new SortedDictionary (); string s; Assert.IsFalse (d.TryGetValue (0, out s), "#1"); Assert.IsNull (s, "#2"); d.Add (0, "Test"); Assert.IsTrue (d.TryGetValue (0, out s), "#3"); Assert.AreEqual ("Test", s, "#4"); Assert.IsFalse (d.TryGetValue (1, out s), "#5"); Assert.IsNull (s, "#6"); } [Test] public void CopyTo () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); KeyValuePair [] array = new KeyValuePair [d.Count]; d.CopyTo (array, 0); Assert.AreEqual (1, array.Length); Assert.AreEqual (1, array [0].Key); Assert.AreEqual ("A", array [0].Value); d = new SortedDictionary (); array = new KeyValuePair [d.Count]; d.CopyTo (array, 0); Assert.AreEqual (0, array.Length); ICollection c = new SortedDictionary (); array = new KeyValuePair [c.Count]; c.CopyTo (array, 0); Assert.AreEqual (0, array.Length); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void IDictionaryAddKeyNull () { IDictionary d = new SortedDictionary (); d.Add (null, "A"); } [Test] [ExpectedException (typeof (ArgumentNullException))] public void IDictionaryAddKeyNullValueType () { IDictionary d = new SortedDictionary (); d.Add (null, "A"); } [Test] public void IDictionaryAddValueNull () { IDictionary d = new SortedDictionary (); // If we simply check "if (value is TValue)" it won't pass. d.Add ("A", null); } [Test] [ExpectedException (typeof (ArgumentException))] public void IDictionaryAddValueNullValueType () { IDictionary d = new SortedDictionary (); // If we simply allow null it won't result in ArgumentException. d.Add ("A", null); } [Test] [ExpectedException (typeof (NotSupportedException))] public void KeysICollectionAdd () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Keys; col.Add (2); } [Test] [ExpectedException (typeof (NotSupportedException))] public void KeysICollectionClear () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Keys; col.Clear (); } [Test] [ExpectedException (typeof (NotSupportedException))] public void KeysICollectionRemove () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Keys; col.Remove (1); } [Test] public void KeysICollectionCopyTo () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Keys; int[] array = new int [col.Count]; col.CopyTo (array, 0); Assert.AreEqual (1, array.Length); Assert.AreEqual (1, array [0]); // Bug #497720 d = new SortedDictionary (); col = d.Keys; array = new int [col.Count]; col.CopyTo (array, 0); } [Test] [ExpectedException (typeof (NotSupportedException))] public void ValuesICollectionAdd () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Values; col.Add ("B"); } [Test] [ExpectedException (typeof (NotSupportedException))] public void ValuesICollectionClear () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Values; col.Clear (); } [Test] [ExpectedException (typeof (NotSupportedException))] public void ValuesICollectionRemove () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Values; col.Remove ("A"); } [Test] public void ValuesICollectionCopyTo () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); ICollection col = d.Values; string[] array = new string [col.Count]; col.CopyTo (array, 0); Assert.AreEqual (1, array.Length); Assert.AreEqual ("A", array [0]); d = new SortedDictionary (); col = d.Values; array = new string [col.Count]; col.CopyTo (array, 0); } [Test] public void KeysGetEnumerator1 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); IEnumerator e = d.Keys.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#1"); Assert.AreEqual (1, e.Current, "#2"); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual (2, e.Current, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual (3, e.Current, "#6"); Assert.IsFalse (e.MoveNext (), "#7"); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void KeysGetEnumerator2 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); IEnumerator e = d.Keys.GetEnumerator (); d.Add (4, "D"); e.MoveNext (); } [Test] public void ValuesGetEnumerator1 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); IEnumerator e = d.Values.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#1"); Assert.AreEqual ("A", e.Current, "#2"); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual ("C", e.Current, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual ("B", e.Current, "#6"); Assert.IsFalse (e.MoveNext (), "#7"); } [Test] [ExpectedException (typeof (InvalidOperationException))] public void ValuesGetEnumerator2 () { SortedDictionary d = new SortedDictionary (); d.Add (1, "A"); d.Add (3, "B"); d.Add (2, "C"); IEnumerator e = d.Values.GetEnumerator (); d.Add (4, "D"); e.MoveNext (); } delegate void D (); bool Throws (D d) { try { d (); return false; } catch { return true; } } [Test] // based on #491858, #517415 public void Enumerator_Current () { var e1 = new SortedDictionary.Enumerator (); Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x);})); var d = new SortedDictionary (); var e2 = d.GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);})); e2.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);})); e2.Dispose (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x);})); var e3 = ((IEnumerable>) d).GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);})); e3.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);})); e3.Dispose (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x);})); var e4 = ((IEnumerable) d).GetEnumerator (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);})); e4.MoveNext (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);})); ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x);})); } [Test] // based on #491858, #517415 public void KeyEnumerator_Current () { var e1 = new SortedDictionary.KeyCollection.Enumerator (); Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); })); var d = new SortedDictionary ().Keys; var e2 = d.GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); e2.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); e2.Dispose (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); var e3 = ((IEnumerable) d).GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); e3.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); e3.Dispose (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); var e4 = ((IEnumerable) d).GetEnumerator (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); e4.MoveNext (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); } [Test] // based on #491858, #517415 public void ValueEnumerator_Current () { var e1 = new SortedDictionary.ValueCollection.Enumerator (); Assert.IsFalse (Throws (delegate { var x = e1.Current; GC.KeepAlive (x); })); var d = new SortedDictionary ().Values; var e2 = d.GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); e2.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); e2.Dispose (); Assert.IsFalse (Throws (delegate { var x = e2.Current; GC.KeepAlive (x); })); var e3 = ((IEnumerable) d).GetEnumerator (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); e3.MoveNext (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); e3.Dispose (); Assert.IsFalse (Throws (delegate { var x = e3.Current; GC.KeepAlive (x); })); var e4 = ((IEnumerable) d).GetEnumerator (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); e4.MoveNext (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); ((IDisposable) e4).Dispose (); Assert.IsTrue (Throws (delegate { var x = e4.Current; GC.KeepAlive (x); })); } // Serialize a dictionary out and deserialize it back in again SortedDictionary Roundtrip(SortedDictionary dic) { BinaryFormatter bf = new BinaryFormatter (); MemoryStream stream = new MemoryStream (); bf.Serialize (stream, dic); stream.Position = 0; return (SortedDictionary)bf.Deserialize (stream); } [Test] public void Serialize() { SortedDictionary test = new SortedDictionary(); test.Add(1, "a"); test.Add(3, "c"); test.Add(2, "b"); SortedDictionary result = Roundtrip(test); Assert.AreEqual(3, result.Count); Assert.AreEqual("a", result[1]); Assert.AreEqual("b", result[2]); Assert.AreEqual("c", result[3]); } [Test] public void SerializeReverseComparer() { SortedDictionary test = new SortedDictionary ( ReverseComparer.Instance); test.Add (1, "A"); test.Add (3, "B"); test.Add (2, "C"); SortedDictionary result = Roundtrip (test); SortedDictionary.Enumerator e = result.GetEnumerator (); Assert.IsTrue (e.MoveNext (), "#1"); Assert.AreEqual ("B", e.Current.Value, "#2"); Assert.IsTrue (e.MoveNext (), "#3"); Assert.AreEqual ("C", e.Current.Value, "#4"); Assert.IsTrue (e.MoveNext (), "#5"); Assert.AreEqual ("A", e.Current.Value, "#6"); Assert.IsFalse (e.MoveNext (), "#7"); } } [Serializable] class ReverseComparer : IComparer { static ReverseComparer instance = new ReverseComparer (); public static ReverseComparer Instance { get { return instance; } } ReverseComparer () { } public int Compare (T t1, T t2) { return Comparer.Default.Compare (t2, t1); } } } #endif