Author: dwaite
Date: 2005-06-18 22:36:21 -0400 (Sat, 18 Jun 2005)
New Revision: 46200
Modified:
trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
Log:
2005-06-16 David Waite <[EMAIL PROTECTED]>
* Dictionary.cs (EnumerationMode): Remove.
(Enumerator): Remove return type flag - legacy return is expected to
always return a DictionaryEntry
(Enumerator): Make constructor internal, it is not public on ms.net
beta 2
(Enumerator.MoveNext): Fix endless loop on enumerator use
(VerifyState): Added method to check state preconditions and throw
appropriate exceptions.
(KeyCollection,ValueCollection): Mark sealed to match ms.net beta 2
(KeyCollection.Enumerator._hostEnumerator): make exact struct type,
rather than boxing and using by interface
(KeyCollection.Enumerator.Dispose): use default to clear Enumerator
(ValueCollection.Enumerator._hostEnumerator): make exact struct type,
rather than boxing and using by interface
(ValueCollection.Enumerator.Dispose): use default to clear Enumerator
* EqualityComparer.cs (DefaultComparer, IEquatableOfTEqualityComparer):
Mark as serializable.
* DictionaryTest.cs (AddTest3): Added test of simple interaction with
struct dictionary
(AddDuplicateTest,IndexerGetNullTest,IndexerSetNullTest): Added tests
for exceptional cases to verify correct exceptions
(TryGetValueTest): check return value from TryGetValue
(KeyValueEnumeratorTest): Added test from verification of ms.net beta2
behavior for non-generic IEnumerator/IDictionaryEnumerator behavior
against Dictionary<>
(SerializationTest): Added test to utilize serialization routines.
Modified: trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2005-06-18
22:39:59 UTC (rev 46199)
+++ trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2005-06-19
02:36:21 UTC (rev 46200)
@@ -1,3 +1,23 @@
+2005-06-16 David Waite <[EMAIL PROTECTED]>
+
+ * Dictionary.cs (EnumerationMode): Remove.
+ (Enumerator): Remove return type flag - legacy return is expected to
+ always return a DictionaryEntry
+ (Enumerator): Make constructor internal, it is not public on ms.net
+ beta 2
+ (Enumerator.MoveNext): Fix endless loop on enumerator use
+ (VerifyState): Added method to check state preconditions and throw
+ appropriate exceptions.
+ (KeyCollection,ValueCollection): Mark sealed to match ms.net beta 2
+ (KeyCollection.Enumerator._hostEnumerator): make exact struct type,
+ rather than boxing and using by interface
+ (KeyCollection.Enumerator.Dispose): use default to clear Enumerator
+ (ValueCollection.Enumerator._hostEnumerator): make exact struct type,
+ rather than boxing and using by interface
+ (ValueCollection.Enumerator.Dispose): use default to clear Enumerator
+ * EqualityComparer.cs (DefaultComparer, IEquatableOfTEqualityComparer):
+ Mark as serializable.
+
2005-06-16 Raja R Harinath <[EMAIL PROTECTED]>
* Dictionary.cs (GetKeys, GetValues): Remove.
Modified: trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
2005-06-18 22:39:59 UTC (rev 46199)
+++ trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
2005-06-19 02:36:21 UTC (rev 46200)
@@ -316,7 +316,7 @@
}
//
- // Returns the index of the chain containing key. Also ensures
that the found key is the first element of the chain.
+ // Returns the index of the chain containing key. Also
ensures that the found key is the first element of the chain.
// If the key is not found, returns -h-1, where 'h' is the
index of the chain that would've contained the key.
//
private int GetSlot (TKey key)
@@ -464,7 +464,7 @@
IEnumerator IEnumerable.GetEnumerator ()
{
- return new Enumerator (this,
EnumerationMode.DictionaryEntry);
+ return new Enumerator (this);
}
IEnumerator<KeyValuePair<TKey, TValue>>
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
@@ -474,36 +474,29 @@
IDictionaryEnumerator IDictionary.GetEnumerator ()
{
- return new Enumerator (this,
EnumerationMode.DictionaryEntry);
+ return new Enumerator (this);
}
public Enumerator GetEnumerator ()
{
- return new Enumerator (this,
EnumerationMode.KeyValuePair);
+ return new Enumerator (this);
}
- internal enum EnumerationMode { DictionaryEntry, KeyValuePair };
-
[Serializable]
public struct Enumerator :
IEnumerator<KeyValuePair<TKey,TValue>>,
IDisposable, IDictionaryEnumerator, IEnumerator
{
Slot [] _dictionaryTable;
- EnumerationMode _navigationMode;
Slot _current;
int _nextIndex;
- public Enumerator (Dictionary<TKey, TValue> dictionary)
: this (dictionary, EnumerationMode.KeyValuePair)
+ internal Enumerator (Dictionary<TKey, TValue>
dictionary)
{
- }
-
- internal Enumerator (Dictionary<TKey, TValue>
dictionary, EnumerationMode mode)
- {
_dictionaryTable = dictionary._table;
- _navigationMode = mode;
- // The following stanza is identical to
IEnumerator.Reset (), but because of the
+ // The following stanza is identical to
IEnumerator.Reset (),
+ // but because of the
// definite assignment rule, we cannot call it
here.
_nextIndex = 0;
_current = null;
@@ -511,46 +504,40 @@
public bool MoveNext ()
{
+ if (_dictionaryTable == null)
+ throw new ObjectDisposedException(null);
+
+ // Pre-condition: _current == null => this is
the first call
+ // to MoveNext ()
if (_current != null)
_current = _current.Next;
while (_current == null && _nextIndex <
_dictionaryTable.Length)
_current = _dictionaryTable
[_nextIndex++];
+ // Post-condition: _current == null => this is
the last call
+ // to MoveNext()
return _current != null;
}
public KeyValuePair<TKey, TValue> Current {
get {
- if (_current == null)
- throw new
InvalidOperationException ();
+ VerifyState();
return _current.Data;
}
}
object IEnumerator.Current {
get {
- if (_current == null)
- throw new
InvalidOperationException ();
- switch (_navigationMode) {
- case EnumerationMode.DictionaryEntry:
- DictionaryEntry de = new
DictionaryEntry (_current.Data.Key, _current.Data.Value);
- return de;
- case EnumerationMode.KeyValuePair:
- default:
- return _current.Data;
- }
+ VerifyState();
+ return new
DictionaryEntry(_current.Data.Key, _current.Data.Value);
}
}
- DictionaryEntry IDictionaryEnumerator.Entry
- {
- get
- {
- if (_current == null)
- throw new
InvalidOperationException ();
- DictionaryEntry entry = new
DictionaryEntry (_current.Data.Key, _current.Data.Value);
- return entry;
+ DictionaryEntry IDictionaryEnumerator.Entry {
+ get {
+ VerifyState();
+ return new DictionaryEntry
(_current.Data.Key, _current.Data.Value);
}
}
@@ -564,8 +551,7 @@
{
get
{
- if (_current == null)
- throw new
InvalidOperationException ();
+ VerifyState();
return _current.Data.Key;
}
}
@@ -573,12 +559,18 @@
{
get
{
- if (_current == null)
- throw new
InvalidOperationException ();
+ VerifyState();
return _current.Data.Value;
}
}
-
+
+ void VerifyState()
+ {
+ if (_dictionaryTable == null)
+ throw new ObjectDisposedException(null);
+ if (_current == null)
+ throw new InvalidOperationException();
+ }
public void Dispose ()
{
_current = null;
@@ -588,7 +580,7 @@
// This collection is a read only collection
[Serializable]
- public class KeyCollection : ICollection<TKey>,
IEnumerable<TKey>, ICollection, IEnumerable {
+ public sealed class KeyCollection : ICollection<TKey>,
IEnumerable<TKey>, ICollection, IEnumerable {
Dictionary<TKey, TValue> _dictionary;
public KeyCollection (Dictionary<TKey, TValue>
dictionary)
@@ -671,16 +663,16 @@
}
public struct Enumerator : IEnumerator<TKey>,
IDisposable, IEnumerator {
- IEnumerator<KeyValuePair<TKey, TValue>>
_hostEnumerator;
+ Dictionary<TKey, TValue>.Enumerator
_hostEnumerator;
- internal Enumerator
(IEnumerator<KeyValuePair<TKey, TValue>> hostEnumerator)
+ internal Enumerator (Dictionary<TKey,
TValue>.Enumerator hostEnumerator)
{
_hostEnumerator = hostEnumerator;
}
public void Dispose ()
{
- _hostEnumerator = null;
+ _hostEnumerator.Dispose();
}
public bool MoveNext ()
@@ -702,14 +694,14 @@
void IEnumerator.Reset ()
{
- _hostEnumerator.Reset ();
+ ((IEnumerator)_hostEnumerator).Reset ();
}
}
}
// This collection is a read only collection
[Serializable]
- public class ValueCollection : ICollection<TValue>,
IEnumerable<TValue>, ICollection, IEnumerable {
+ public sealed class ValueCollection : ICollection<TValue>,
IEnumerable<TValue>, ICollection, IEnumerable {
Dictionary<TKey, TValue> _dictionary;
public ValueCollection (Dictionary<TKey, TValue>
dictionary)
@@ -792,16 +784,16 @@
}
public struct Enumerator : IEnumerator<TValue>,
IDisposable, IEnumerator {
- IEnumerator<KeyValuePair<TKey, TValue>>
_hostEnumerator;
+ Dictionary<TKey, TValue>.Enumerator
_hostEnumerator;
- internal Enumerator
(IEnumerator<KeyValuePair<TKey, TValue>> hostEnumerator)
+ internal Enumerator
(Dictionary<TKey,TValue>.Enumerator hostEnumerator)
{
_hostEnumerator = hostEnumerator;
}
public void Dispose ()
{
- _hostEnumerator = null;
+ _hostEnumerator.Dispose();
}
public bool MoveNext ()
@@ -823,7 +815,7 @@
void IEnumerator.Reset ()
{
- _hostEnumerator.Reset ();
+ ((IEnumerator)_hostEnumerator).Reset ();
}
}
}
Modified: trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
2005-06-18 22:39:59 UTC (rev 46199)
+++ trunk/mcs/class/corlib/System.Collections.Generic/EqualityComparer.cs
2005-06-19 02:36:21 UTC (rev 46200)
@@ -54,6 +54,7 @@
}
}
+ [Serializable]
class DefaultComparer : EqualityComparer<T> {
public override int GetHashCode (T obj)
@@ -71,6 +72,7 @@
}
}
+ [Serializable]
class IEquatableOfTEqualityComparer <T> : EqualityComparer <T> where T
: IEquatable <T> {
public override int GetHashCode (T obj)
Modified: trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
2005-06-18 22:39:59 UTC (rev 46199)
+++ trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
2005-06-19 02:36:21 UTC (rev 46200)
@@ -1,3 +1,15 @@
+2005-06-18 David Waite <[EMAIL PROTECTED]>
+
+ * DictionaryTest.cs (AddTest3): Added test of simple interaction with
+ struct dictionary
+ (AddDuplicateTest,IndexerGetNullTest,IndexerSetNullTest): Added tests
+ for exceptional cases to verify correct exceptions
+ (TryGetValueTest): check return value from TryGetValue
+ (KeyValueEnumeratorTest): Added test from verification of ms.net beta2
+ behavior for non-generic IEnumerator/IDictionaryEnumerator behavior
+ against Dictionary<>
+ (SerializationTest): Added test to utilize serialization routines.
+
2005-06-16 Raja R Harinath <[EMAIL PROTECTED]>
* DictionaryTest.cs (SliceCollectionsEnumeratorTest): New test
Modified:
trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
2005-06-18 22:39:59 UTC (rev 46199)
+++ trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
2005-06-19 02:36:21 UTC (rev 46200)
@@ -4,8 +4,10 @@
// Authors:
// Sureshkumar T ([EMAIL PROTECTED])
// Ankit Jain ([EMAIL PROTECTED])
+// David Waite ([EMAIL PROTECTED])
//
// Copyright (C) 2004 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005 David Waite ([EMAIL PROTECTED])
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
@@ -32,6 +34,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
+using System.IO;
+using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using NUnit.Framework;
@@ -67,12 +71,14 @@
Dictionary <string, object> _dictionary = null;
Dictionary <MyClass, MyClass> _dictionary2 = null;
+ Dictionary <int, int> _dictionary3 = null;
[SetUp]
public void SetUp ()
{
_dictionary = new Dictionary <string, object> ();
_dictionary2 = new Dictionary <MyClass, MyClass> ();
+ _dictionary3 = new Dictionary <int, int>();
}
[Test]
@@ -96,26 +102,43 @@
Assert.AreEqual (12, _dictionary2 [m3].Value, "#3");
}
+ [Test]
+ public void AddTest3 ()
+ {
+ _dictionary3.Add (1, 2);
+ _dictionary3.Add (2, 3);
+ _dictionary3.Add (3, 4);
+ Assert.AreEqual (2, _dictionary3[1], "#1");
+ Assert.AreEqual (3, _dictionary3[2], "#2");
+ Assert.AreEqual (4, _dictionary3[3], "#3");
+ }
+
[Test, ExpectedException(typeof(ArgumentNullException))]
- public void NullTest ()
+ public void AddNullTest ()
{
_dictionary.Add (null, "");
}
+ [Test, ExpectedException(typeof(ArgumentException))]
+ public void AddDuplicateTest ()
+ {
+ _dictionary.Add("foo", "bar");
+ _dictionary.Add("foo", "bar");
+ }
+
//Tests Add when resize takes place
[Test]
public void AddLargeTest ()
{
- Dictionary <int, int> _dict = new Dictionary <int, int>
();
int i, numElems = 50;
for (i = 0; i < numElems; i++)
{
- _dict.Add (i, i);
+ _dictionary3.Add (i, i);
}
i = 0;
- foreach (KeyValuePair <int, int> entry in _dict)
+ foreach (KeyValuePair <int, int> entry in _dictionary3)
{
i++;
}
@@ -136,6 +159,12 @@
object foo = _dictionary ["foo"];
}
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void IndexerGetNullTest()
+ {
+ object s = _dictionary[null];
+ }
+
[Test]
public void IndexerSetExistingTest ()
{
@@ -156,10 +185,10 @@
[Test]
public void RemoveTest ()
{
- _dictionary.Add ("key1", (object)"value1");
- _dictionary.Add ("key2", (object)"value2");
- _dictionary.Add ("key3", (object)"value3");
- _dictionary.Add ("key4", (object)"value4");
+ _dictionary.Add ("key1", "value1");
+ _dictionary.Add ("key2", "value2");
+ _dictionary.Add ("key3", "value3");
+ _dictionary.Add ("key4", "value4");
Assert.IsTrue (_dictionary.Remove ("key3"));
Assert.IsFalse (_dictionary.Remove ("foo"));
Assert.AreEqual (3, _dictionary.Count);
@@ -180,6 +209,12 @@
}
+ [Test, ExpectedException(typeof(ArgumentNullException))]
+ public void IndexerSetNullTest()
+ {
+ _dictionary[null] = "bar";
+ }
+
[Test]
public void ClearTest ()
{
@@ -226,10 +261,12 @@
_dictionary.Add ("key3", (object)"value3");
_dictionary.Add ("key4", (object)"value4");
object value = "";
- _dictionary.TryGetValue ("key4", out value);
+ bool retrieved = _dictionary.TryGetValue ("key4", out
value);
+ Assert.IsTrue (retrieved);
Assert.AreEqual ("value4", (string)value, "TryGetValue
does not return value!");
- _dictionary.TryGetValue ("key7", out value);
+ retrieved = _dictionary.TryGetValue ("key7", out value);
+ Assert.IsFalse (retrieved);
Assert.IsNull (value, "value for non existant value
should be null!");
}
@@ -363,26 +400,22 @@
foreach (KeyValuePair <string, object> entry in
_dictionary)
{
i++;
- Console.WriteLine (entry.ToString());
}
- if (i != 4)
- Assert.Fail("fail1: foreach entry failed!");
+ Assert.AreEqual(4, i, "fail1: foreach entry failed!");
i = 0;
foreach (DictionaryEntry entry in
((IEnumerable)_dictionary))
{
i++;
}
- if (i != 4)
- Assert.Fail("fail2: foreach entry failed!");
+ Assert.AreEqual(4, i, "fail2: foreach entry failed!");
i = 0;
foreach (DictionaryEntry entry in
((IDictionary)_dictionary))
{
i++;
}
- if (i != 4)
- Assert.Fail ("fail3: foreach entry failed!");
+ Assert.AreEqual (4, i, "fail3: foreach entry failed!");
}
[Test]
@@ -416,7 +449,6 @@
int i = 0;
foreach (string key in keys)
{
- Console.WriteLine("keys collection : " + key);
i++;
}
Assert.AreEqual(4, i);
@@ -459,6 +491,46 @@
Assert.IsTrue (ke is Dictionary<string,
int>.KeyCollection.Enumerator);
Assert.IsTrue (ve is Dictionary<string,
int>.ValueCollection.Enumerator);
}
+
+ [Test]
+ public void PlainEnumeratorReturnTest ()
+ {
+ // Test that we return a DictionaryEntry for
non-generic dictionary iteration
+ _dictionary["foo"] = "bar";
+ IEnumerator<KeyValuePair<string, object>> enumerator =
_dictionary.GetEnumerator();
+ Assert.IsTrue(enumerator.MoveNext());
+ Assert.IsTrue(((IEnumerator)enumerator).Current is
DictionaryEntry);
+
Assert.IsTrue(((IDictionaryEnumerator)enumerator).Current is DictionaryEntry);
+ Assert.IsFalse(((object)enumerator.Current) is
DictionaryEntry);
+ }
+
+ [Test]
+ public void SerializationTest()
+ {
+ for (int i = 0; i < 50; i++)
+ {
+ _dictionary3.Add(i, i);
+ }
+
+ BinaryFormatter formatter = new BinaryFormatter();
+ MemoryStream stream = new MemoryStream();
+ formatter.Serialize(stream, _dictionary3);
+
+ stream.Position = 0;
+ object deserialized = formatter.Deserialize(stream);
+
+ Assert.IsNotNull(deserialized);
+ Assert.IsFalse(deserialized == _dictionary3);
+
+ Assert.IsTrue(deserialized is Dictionary<int, int>);
+ Dictionary<int, int> d3 = deserialized as
Dictionary<int, int>;
+
+ Assert.AreEqual(50, d3.Count);
+ for (int i = 0; i < 50; i++)
+ {
+ Assert.AreEqual(i, d3[i]);
+ }
+ }
}
}
_______________________________________________
Mono-patches maillist - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches