Author: raja
Date: 2005-06-22 10:42:13 -0400 (Wed, 22 Jun 2005)
New Revision: 46370

Modified:
   trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
   trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
   trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
   trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
Log:
In System.Collections.Generic:
        * Dictionary.cs (_generation, _enumeratorGeneration, _enumerators):
        New fields to implement fail-fast semantics.  All code that
        modifies the table increment _generation.
        (GetSlot): Use _hcp to compare keys.  Return the slot containing
        the key, rather than the index.  Avoid move-to-front heuristic
        when there's an enumerator coursing through the table.
        (this, Add, TryGetValue, ContainsKey, Remove): Update to change.
        (Enumerator.Enumerator): Save the generation of the dictionary.
        (Enumerator.Dispose): Inform dictionary that the enumerator is no more.
        (Enumerator.MoveNext, Enumerator.VerifyState): Fail if the
        dictionary has been modified.

In Test/System.Collections.Generic:
        * DictionaryTest.cs (KeyValueEnumeratorTest): Add test for infloop
        if dictionary is read while it's being enumerated.


Modified: trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2005-06-22 
14:39:03 UTC (rev 46369)
+++ trunk/mcs/class/corlib/System.Collections.Generic/ChangeLog 2005-06-22 
14:42:13 UTC (rev 46370)
@@ -1,21 +1,35 @@
+2005-06-22  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       * Dictionary.cs (_generation, _enumeratorGeneration, _enumerators):
+       New fields to implement fail-fast semantics.  All code that
+       modifies the table increment _generation.
+       (GetSlot): Use _hcp to compare keys.  Return the slot containing
+       the key, rather than the index.  Avoid move-to-front heuristic
+       when there's an enumerator coursing through the table.
+       (this, Add, TryGetValue, ContainsKey, Remove): Update to change.
+       (Enumerator.Enumerator): Save the generation of the dictionary.
+       (Enumerator.Dispose): Inform dictionary that the enumerator is no more.
+       (Enumerator.MoveNext, Enumerator.VerifyState): Fail if the
+       dictionary has been modified.
+
 2005-06-20  David waite  <[EMAIL PROTECTED]>
 
-    * List.cs : substantial changes and optimizations
-    (AddCollection, AddEnumerable): new internal specializations of AddRange
-    (AsReadOnly): returns specific IList<T> to match ms.net 2.0b2 api.
-    (Clear): reset size to zero on clear
-    (ConvertAll): catch null converter, use Add to prevent OutOfBounds
-    exception
-    (FindAll, FindIndex, FindLast, FindLastIndex, RemoveAll, TrueForAll): 
-    check for null match
-    (FindLastIndex): correct index parameters based on ms.net 2005b2 behavior
-    (ForEach): catch null action
-    (CheckIndex): new internal function similar to CheckRange for functions
-    which only provide a starting index
-    (InsertCollection, InsertEnumerable): new internal specializations of
-    InsertRange
-    (ReadOnlyList): removed, ReadOnlyCollection in 
-    System.Collections.ObjectModel is used instead now 
+       * List.cs : substantial changes and optimizations
+       (AddCollection, AddEnumerable): new internal specializations of AddRange
+       (AsReadOnly): returns specific IList<T> to match ms.net 2.0b2 api.
+       (Clear): reset size to zero on clear
+       (ConvertAll): catch null converter, use Add to prevent OutOfBounds
+       exception
+       (FindAll, FindIndex, FindLast, FindLastIndex, RemoveAll, TrueForAll): 
+       check for null match
+       (FindLastIndex): correct index parameters based on ms.net 2005b2 
behavior
+       (ForEach): catch null action
+       (CheckIndex): new internal function similar to CheckRange for functions
+       which only provide a starting index
+       (InsertCollection, InsertEnumerable): new internal specializations of
+       InsertRange
+       (ReadOnlyList): removed, ReadOnlyCollection in 
+       System.Collections.ObjectModel is used instead now 
     
 2005-06-16  David Waite  <[EMAIL PROTECTED]>
 

Modified: trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs
===================================================================
--- trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs     
2005-06-22 14:39:03 UTC (rev 46369)
+++ trunk/mcs/class/corlib/System.Collections.Generic/Dictionary.cs     
2005-06-22 14:42:13 UTC (rev 46370)
@@ -57,64 +57,70 @@
                private class Slot {
                        public KeyValuePair<TKey, TValue> Data;
                        public Slot Next;
-                       
-                       public Slot (KeyValuePair<TKey,TValue> Data, Slot Next)
+
+                       public Slot (TKey Key, TValue Value, Slot Next)
                        {
-                               this.Data = Data;
+                               this.Data.Key = Key;
+                               this.Data.Value = Value;
                                this.Next = Next;
                        }
                }
-       
+
                Slot [] _table;
-       
                int _usedSlots;
-       
+               private int _threshold;
+
                IEqualityComparer<TKey> _hcp;
                SerializationInfo _serializationInfo;
-               
-               private int _threshold;
-       
+
+               private uint _generation;
+               private uint _enumeratorGeneration;
+               private uint _enumerators;
+
                public int Count {
                        get { return _usedSlots; }
                }
 
                public TValue this [TKey key] {
                        get {
-                               int index = GetSlot (key);
-                               if (index < 0)
+                               int index;
+                               Slot slot = GetSlot (key, out index);
+                               if (slot == null)
                                        throw new KeyNotFoundException ();
-                               return _table [index].Data.Value;
+                               return slot.Data.Value;
                        }
-                       
+
                        set {
-                               int index = GetSlot (key);
-                               if (index < 0)
+                               ++_generation;
+                               int index;
+                               Slot slot = GetSlot (key, out index);
+                               if (slot == null)
                                        DoAdd (index, key, value);
                                else
-                                       _table [index].Data.Value = value;
+                                       slot.Data.Value = value;
                        }
                }
-       
+
                public Dictionary ()
                {
                        Init (INITIAL_SIZE, null);
                }
-       
+
                public Dictionary (IEqualityComparer<TKey> comparer)
                {
                        Init (INITIAL_SIZE, comparer);
                }
-       
+
                public Dictionary (IDictionary<TKey, TValue> dictionary)
                        : this (dictionary, null)
                {
                }
-       
+
                public Dictionary (int capacity)
                {
                        Init (capacity, null);
                }
-       
+
                public Dictionary (IDictionary<TKey, TValue> dictionary, 
IEqualityComparer<TKey> comparer)
                {
                        if (dictionary == null)
@@ -125,12 +131,12 @@
                                this.Add (entry.Key, entry.Value);
                        }
                }
-       
+
                public Dictionary (int capacity, IEqualityComparer<TKey> 
comparer)
                {
                        Init (capacity, comparer);
                }
-       
+
                protected Dictionary (SerializationInfo info, StreamingContext 
context)
                {
                        _serializationInfo = info;
@@ -142,7 +148,7 @@
                        if (_threshold == 0 && _table.Length > 0)
                                _threshold = 1;
                }
-               
+
                private void Init (int capacity, IEqualityComparer<TKey> hcp)
                {
                        if (capacity < 0)
@@ -150,6 +156,9 @@
                        this._hcp = (hcp != null) ? hcp : 
EqualityComparer<TKey>.Default;
                        _table = new Slot [capacity];
                        SetThreshold ();
+                       _generation = 0;
+                       _enumeratorGeneration = 0;
+                       _enumerators = 0;
                }
 
                void CopyTo (KeyValuePair<TKey, TValue> [] array, int index)
@@ -162,13 +171,13 @@
                                throw new ArgumentException ("index larger than 
largest valid index of array");
                        if (array.Length - index < _usedSlots)
                                throw new ArgumentException ("Destination array 
cannot hold the requested elements!");
-                       
+
                        for (int i = 0; i < _table.Length; ++i) {
                                for (Slot slot = _table [i]; slot != null; slot 
= slot.Next)
                                        array [index++] = slot.Data;
                        }
                }
-       
+
                private void Resize ()
                {
                        // From the SDK docs:
@@ -176,10 +185,10 @@
                        //       to the smallest prime number that is larger
                        //       than twice the current number of Hashtable 
buckets
                        uint newSize = (uint) Hashtable.ToPrime ((_table.Length 
<< 1) | 1);
-               
+
                        Slot nextslot = null;
                        Slot [] oldTable = _table;
-                       
+
                        _table = new Slot [newSize];
                        SetThreshold ();
 
@@ -197,33 +206,32 @@
 
                public void Add (TKey key, TValue value)
                {
-                       int index = GetSlot (key);
-                       if (index >= 0)
+                       ++_generation;
+                       int index;
+                       Slot slot = GetSlot (key, out index);
+
+                       if (slot != null)
                                throw new ArgumentException ("An element with 
the same key already exists in the dictionary.");
 
                        DoAdd (index, key, value);
                }
 
-               void DoAdd (int negated_index, TKey key, TValue value)
+               void DoAdd (int index, TKey key, TValue value)
                {
-                       int index = -negated_index - 1;
-
                        if (_usedSlots >= _threshold) {
                                Resize ();
                                index = DoHash (key);
                        }
 
-                       _table [index] = new Slot (
-                               new KeyValuePair<TKey,TValue>(key, value), 
-                               _table [index]);
+                       _table [index] = new Slot (key, value, _table [index]);
                        ++_usedSlots;
                }
-       
+
                private int DoHash (TKey key)
                {
                        if (key == null)
                                throw new ArgumentNullException ("key", "null 
key");
-       
+
                        int size = this._table.Length;
                        int h = _hcp.GetHashCode (key) & Int32.MaxValue;
                        int spot = (int) ((uint) h % size);
@@ -231,23 +239,23 @@
                }
 
                public IEqualityComparer<TKey> Comparer {
-                       get {
-                               return _hcp;
-                       }
+                       get { return _hcp; }
                }
-               
+
                public void Clear ()
                {
+                       ++_generation;
                        for (int i = 0; i < _table.Length; i++)
                                _table [i] = null;
                        _usedSlots = 0;
                }
-       
+
                public bool ContainsKey (TKey key)
                {
-                       return GetSlot (key) >= 0;
+                       int index;
+                       return GetSlot (key, out index) != null;
                }
-       
+
                public bool ContainsValue (TValue value)
                {
                        IEqualityComparer<TValue> cmp = 
EqualityComparer<TValue>.Default;
@@ -260,7 +268,7 @@
                        }
                        return false;
                }
-       
+
                [SecurityPermission (SecurityAction.LinkDemand, 
Flags=SecurityPermissionFlag.SerializationFormatter)]
                public virtual void GetObjectData (SerializationInfo info, 
StreamingContext context)
                {
@@ -276,7 +284,7 @@
                        info.AddValue ("data", data);
                        info.AddValue ("buckets_hint", _table.Length);
                }
-       
+
                public virtual void OnDeserialization (object sender)
                {
                        if (_serializationInfo == null)
@@ -284,7 +292,7 @@
 
                        _hcp = (IEqualityComparer<TKey>) 
_serializationInfo.GetValue ("hcp", typeof (IEqualityComparer<TKey>));
                        KeyValuePair<TKey, TValue> [] data =
-                               (KeyValuePair<TKey, TValue> []) 
+                               (KeyValuePair<TKey, TValue> [])
                                _serializationInfo.GetValue ("data", typeof 
(KeyValuePair<TKey, TValue> []));
 
                        int buckets = _serializationInfo.GetInt32 
("buckets_hint");
@@ -301,100 +309,94 @@
                        }
                        _serializationInfo = null;
                }
-       
+
                public bool Remove (TKey key)
                {
-                       int index = GetSlot (key);
+                       ++_generation;
+                       int index;
+                       Slot slot = GetSlot (key, out index);
 
-                       if (index < 0)
+                       if (slot == null)
                                return false;
 
-                       // If GetSlot returns a valid index, the given key is 
at the head of the chain.
+                       // If GetSlot returns a valid slot, the given key is at 
the head of the chain.
+                       if (_table [index] != slot)
+                               throw new SystemException ("dictionary has 
inconsistent state, aborting");
                        _table [index] = _table [index].Next;
                        --_usedSlots;
                        return true;
                }
 
                //
-               // 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)
+               // Return the slot containing key, and set 'index' to the chain 
the key was found in.
+               // Also try to ensure that the found key is the first element 
of its chain.
+               // If the key is not found, return null and set 'index' to the 
chain that would've contained the key.
+               //
+               private Slot GetSlot (TKey key, out int index)
                {
                        if (key == null)
                                throw new ArgumentNullException ("key");
-                       int index = DoHash (key);
-                       Slot slot = _table [index];
+                       index = DoHash (key);
                        Slot prev = null;
+                       Slot slot = _table [index];
 
-                       while (slot != null && !slot.Data.Key.Equals (key)) {
+                       while (slot != null && !_hcp.Equals (key, 
slot.Data.Key)) {
                                prev = slot;
                                slot = slot.Next;
                        }
-       
-                       if (slot == null)
-                               return - index - 1;
-       
-                       if (prev != null) {
+
+                       if (slot != null && prev != null &&
+                           (_enumeratorGeneration != _generation || 
_enumerators == 0)) {
                                // Move to the head of the list
                                prev.Next = slot.Next;
                                slot.Next = _table [index];
                                _table [index] = slot;
                        }
 
-                       return index;
+                       return slot;
                }
-       
+
                public bool TryGetValue (TKey key, out TValue value)
                {
-                       int index = GetSlot (key);
-                       bool found = index >= 0;
-                       value = found ? _table [index].Data.Value : default 
(TValue);
+                       int index;
+                       Slot slot = GetSlot (key, out index);
+                       bool found = slot != null;
+                       value = found ? slot.Data.Value : default (TValue);
                        return found;
                }
-       
+
                ICollection<TKey> IDictionary<TKey, TValue>.Keys {
-                       get {
-                               return Keys;
-                       }
+                       get { return Keys; }
                }
-       
+
                ICollection<TValue> IDictionary<TKey, TValue>.Values {
-                       get {
-                               return Values;
-                       }
+                       get { return Values; }
                }
 
                public KeyCollection Keys {
-                       get {
-                               return new KeyCollection (this);
-                       }
+                       get { return new KeyCollection (this); }
                }
 
                public ValueCollection Values {
-                       get {
-                               return new ValueCollection (this);
-                       }
+                       get { return new ValueCollection (this); }
                }
+
                ICollection IDictionary.Keys {
-                       get {
-                               return Keys;
-                       }
+                       get { return Keys; }
                }
+
                ICollection IDictionary.Values {
-                       get {
-                               return Values;
-                       }
+                       get { return Values; }
                }
 
                bool IDictionary.IsFixedSize {
                        get { return false; }
                }
-               
+
                bool IDictionary.IsReadOnly {
                        get { return false; }
                }
-               
+
                object IDictionary.this [object key] {
                        get {
                                if (!(key is TKey))
@@ -403,7 +405,7 @@
                        }
                        set { this [(TKey) key] = (TValue) value; }
                }
-       
+
                void IDictionary.Add (object key, object value)
                {
                        if (!(key is TKey))
@@ -412,49 +414,49 @@
                                throw new ArgumentException ("value is of not 
'" + typeof (TValue).ToString () + "'!");
                        this.Add ((TKey) key, (TValue) value);
                }
-       
+
                bool IDictionary.Contains (object key)
                {
                        return ContainsKey ((TKey) key);
                }
-       
+
                void IDictionary.Remove (object key)
                {
                        Remove ((TKey) key);
                }
-       
+
                bool ICollection.IsSynchronized {
                        get { return false; }
                }
+
                object ICollection.SyncRoot {
                        get { return this; }
                }
-       
+
                bool ICollection<KeyValuePair<TKey, TValue>>.IsReadOnly {
                        get { return false; }
                }
-       
+
                void ICollection<KeyValuePair<TKey, TValue>>.Add 
(KeyValuePair<TKey, TValue> keyValuePair)
                {
                        Add (keyValuePair.Key, keyValuePair.Value);
                }
-       
+
                bool ICollection<KeyValuePair<TKey, TValue>>.Contains 
(KeyValuePair<TKey, TValue> keyValuePair)
                {
                        return this.ContainsKey (keyValuePair.Key);
                }
-       
+
                void ICollection<KeyValuePair<TKey, TValue>>.CopyTo 
(KeyValuePair<TKey, TValue> [] array, int index)
                {
                        this.CopyTo (array, index);
                }
-       
+
                bool ICollection<KeyValuePair<TKey, TValue>>.Remove 
(KeyValuePair<TKey, TValue> keyValuePair)
                {
                        return Remove (keyValuePair.Key);
                }
-       
-       
+
                void ICollection.CopyTo (Array array, int index)
                {
                        // TODO: Verify this can be a KeyValuePair, and doesn't 
need to be
@@ -466,7 +468,7 @@
                {
                        return new Enumerator (this);
                }
-       
+
                IEnumerator<KeyValuePair<TKey, TValue>> 
IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator ()
                {
                        return new Enumerator (this);
@@ -476,24 +478,27 @@
                {
                        return new Enumerator (this);
                }
-       
+
                public Enumerator GetEnumerator ()
                {
                        return new Enumerator (this);
                }
-       
+
                [Serializable]
                public struct Enumerator : 
IEnumerator<KeyValuePair<TKey,TValue>>,
                        IDisposable, IDictionaryEnumerator, IEnumerator
                {
-                       Slot [] _dictionaryTable;
+                       Dictionary<TKey, TValue> _dictionary;
+                       uint _stamp;
 
                        Slot _current;
                        int _nextIndex;
-       
+
                        internal Enumerator (Dictionary<TKey, TValue> 
dictionary)
                        {
-                               _dictionaryTable = dictionary._table;
+                               ++dictionary._enumerators;
+                               _dictionary = dictionary;
+                               _stamp = dictionary._enumeratorGeneration = 
dictionary._generation;
 
                                // The following stanza is identical to 
IEnumerator.Reset (),
                                // but because of the
@@ -504,16 +509,18 @@
 
                        public bool MoveNext ()
                        {
-                               if (_dictionaryTable == null)
-                                       throw new ObjectDisposedException(null);
+                               if (_dictionary == null)
+                                       throw new ObjectDisposedException 
(null);
+                               if (_dictionary._generation != _stamp)
+                                       throw new InvalidOperationException 
("out of sync");
 
                                // 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++];
+                               while (_current == null && _nextIndex < 
_dictionary._table.Length)
+                                       _current = _dictionary._table 
[_nextIndex++];
 
                                // Post-condition: _current == null => this is 
the last call
                                // to MoveNext()
@@ -522,74 +529,75 @@
 
                        public KeyValuePair<TKey, TValue> Current {
                                get {
-                                       VerifyState();
+                                       VerifyState ();
                                        return _current.Data;
                                }
                        }
-       
+
                        object IEnumerator.Current {
                                get {
-                                       VerifyState();
-                                       return new 
DictionaryEntry(_current.Data.Key, _current.Data.Value);
+                                       VerifyState ();
+                                       return new DictionaryEntry 
(_current.Data.Key, _current.Data.Value);
                                }
                        }
-       
+
                        DictionaryEntry IDictionaryEnumerator.Entry {
                                get {
-                                       VerifyState();
+                                       VerifyState ();
                                        return new DictionaryEntry 
(_current.Data.Key, _current.Data.Value);
                                }
                        }
-       
+
                        void IEnumerator.Reset ()
                        {
                                _nextIndex = 0;
                                _current = null;
                        }
-       
+
                        object IDictionaryEnumerator.Key
                        {
-                               get
-                               {
-                                       VerifyState();
+                               get {
+                                       VerifyState ();
                                        return _current.Data.Key;
                                }
                        }
                        object IDictionaryEnumerator.Value
                        {
-                               get
-                               {
-                                       VerifyState();
+                               get {
+                                       VerifyState ();
                                        return _current.Data.Value;
                                }
                        }
 
-                       void VerifyState()
+                       void VerifyState ()
                        {
-                               if (_dictionaryTable == null)
-                                       throw new ObjectDisposedException(null);
+                               if (_dictionary == null)
+                                       throw new ObjectDisposedException 
(null);
+                               if (_dictionary._generation != _stamp)
+                                       throw new InvalidOperationException 
("out of sync");
                                if (_current == null)
-                                       throw new InvalidOperationException();
-                       }       
+                                       throw new InvalidOperationException ();
+                       }
                        public void Dispose ()
                        {
                                _current = null;
-                               _dictionaryTable = null;
+                               --_dictionary._enumerators;
+                               _dictionary = null;
                        }
                }
-       
+
                // This collection is a read only collection
                [Serializable]
                public sealed class KeyCollection : ICollection<TKey>, 
IEnumerable<TKey>, ICollection, IEnumerable {
                        Dictionary<TKey, TValue> _dictionary;
-       
+
                        public KeyCollection (Dictionary<TKey, TValue> 
dictionary)
                        {
                                if (dictionary == null)
                                        throw new ArgumentNullException 
("dictionary");
                                _dictionary = dictionary;
                        }
-       
+
                        public void CopyTo (TKey [] array, int index)
                        {
                                if (array == null)
@@ -601,31 +609,30 @@
                                if (array.Length - index < 
_dictionary._usedSlots)
                                        throw new ArgumentException 
("Destination array cannot hold the requested elements!");
 
-                               IEnumerable<TKey> enumerateThis = 
(IEnumerable<TKey>) this;
-                               foreach (TKey k in enumerateThis)
+                               foreach (TKey k in this)
                                        array [index++] = k;
                        }
-       
+
                        public Enumerator GetEnumerator ()
                        {
-                               return new Enumerator 
(_dictionary.GetEnumerator ());
+                               return new Enumerator (_dictionary);
                        }
 
                        void ICollection<TKey>.Add (TKey item)
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
                        }
-       
+
                        void ICollection<TKey>.Clear ()
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
                        }
-       
+
                        bool ICollection<TKey>.Contains (TKey item)
                        {
                                return _dictionary.ContainsKey (item);
                        }
-       
+
                        bool ICollection<TKey>.Remove (TKey item)
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
@@ -635,12 +642,12 @@
                        {
                                return this.GetEnumerator ();
                        }
-       
+
                        void ICollection.CopyTo (Array array, int index)
                        {
                                CopyTo ((TKey []) array, index);
                        }
-       
+
                        IEnumerator IEnumerable.GetEnumerator ()
                        {
                                return this.GetEnumerator ();
@@ -649,7 +656,7 @@
                        public int Count {
                                get { return _dictionary.Count; }
                        }
-       
+
                        bool ICollection<TKey>.IsReadOnly {
                                get { return true; }
                        }
@@ -661,37 +668,33 @@
                        object ICollection.SyncRoot {
                                get { return ((ICollection) 
_dictionary).SyncRoot; }
                        }
-       
+
                        public struct Enumerator : IEnumerator<TKey>, 
IDisposable, IEnumerator {
                                Dictionary<TKey, TValue>.Enumerator 
_hostEnumerator;
 
-                               internal Enumerator (Dictionary<TKey, 
TValue>.Enumerator hostEnumerator)
+                               internal Enumerator (Dictionary<TKey, TValue> 
host)
                                {
-                                       _hostEnumerator = hostEnumerator;
+                                       _hostEnumerator = host.GetEnumerator ();
                                }
-                               
+
                                public void Dispose ()
                                {
-                                       _hostEnumerator.Dispose();
+                                       _hostEnumerator.Dispose ();
                                }
-                               
+
                                public bool MoveNext ()
                                {
                                        return _hostEnumerator.MoveNext ();
                                }
-                               
+
                                public TKey Current {
-                                       get {
-                                               return 
_hostEnumerator.Current.Key;
-                                       }
+                                       get { return 
_hostEnumerator.Current.Key; }
                                }
-                               
+
                                object IEnumerator.Current {
-                                       get {
-                                               return 
_hostEnumerator.Current.Key;
-                                       }
+                                       get { return 
_hostEnumerator.Current.Key; }
                                }
-                               
+
                                void IEnumerator.Reset ()
                                {
                                        ((IEnumerator)_hostEnumerator).Reset ();
@@ -703,14 +706,14 @@
                [Serializable]
                public sealed class ValueCollection : ICollection<TValue>, 
IEnumerable<TValue>, ICollection, IEnumerable {
                        Dictionary<TKey, TValue> _dictionary;
-       
+
                        public ValueCollection (Dictionary<TKey, TValue> 
dictionary)
                        {
                                if (dictionary == null)
                                        throw new ArgumentNullException 
("dictionary");
                                _dictionary = dictionary;
                        }
-       
+
                        public void CopyTo (TValue [] array, int index)
                        {
                                if (array == null)
@@ -722,31 +725,30 @@
                                if (array.Length - index < 
_dictionary._usedSlots)
                                        throw new ArgumentException 
("Destination array cannot hold the requested elements!");
 
-                               IEnumerable<TValue> enumerateThis = 
(IEnumerable<TValue>) this;
-                               foreach (TValue k in enumerateThis)
+                               foreach (TValue k in this)
                                        array [index++] = k;
                        }
-       
+
                        public Enumerator GetEnumerator ()
                        {
-                               return new Enumerator 
(_dictionary.GetEnumerator ());
+                               return new Enumerator (_dictionary);
                        }
 
                        void ICollection<TValue>.Add (TValue item)
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
                        }
-       
+
                        void ICollection<TValue>.Clear ()
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
                        }
-       
+
                        bool ICollection<TValue>.Contains (TValue item)
                        {
                                return _dictionary.ContainsValue (item);
                        }
-       
+
                        bool ICollection<TValue>.Remove (TValue item)
                        {
                                throw new NotSupportedException ("this is a 
read-only collection");
@@ -756,12 +758,12 @@
                        {
                                return this.GetEnumerator ();
                        }
-       
+
                        void ICollection.CopyTo (Array array, int index)
                        {
                                CopyTo ((TValue []) array, index);
                        }
-       
+
                        IEnumerator IEnumerable.GetEnumerator ()
                        {
                                return this.GetEnumerator ();
@@ -770,7 +772,7 @@
                        public int Count {
                                get { return _dictionary.Count; }
                        }
-       
+
                        bool ICollection<TValue>.IsReadOnly {
                                get { return true; }
                        }
@@ -782,37 +784,33 @@
                        object ICollection.SyncRoot {
                                get { return ((ICollection) 
_dictionary).SyncRoot; }
                        }
-       
+
                        public struct Enumerator : IEnumerator<TValue>, 
IDisposable, IEnumerator {
                                Dictionary<TKey, TValue>.Enumerator 
_hostEnumerator;
 
-                               internal Enumerator 
(Dictionary<TKey,TValue>.Enumerator hostEnumerator)
+                               internal Enumerator (Dictionary<TKey,TValue> 
host)
                                {
-                                       _hostEnumerator = hostEnumerator;
+                                       _hostEnumerator = host.GetEnumerator ();
                                }
-                               
+
                                public void Dispose ()
                                {
                                        _hostEnumerator.Dispose();
                                }
-                               
+
                                public bool MoveNext ()
                                {
                                        return _hostEnumerator.MoveNext ();
                                }
-                               
+
                                public TValue Current {
-                                       get {
-                                               return 
_hostEnumerator.Current.Value;
-                                       }
+                                       get { return 
_hostEnumerator.Current.Value; }
                                }
-                               
+
                                object IEnumerator.Current {
-                                       get {
-                                               return 
_hostEnumerator.Current.Value;
-                                       }
+                                       get { return 
_hostEnumerator.Current.Value; }
                                }
-                               
+
                                void IEnumerator.Reset ()
                                {
                                        ((IEnumerator)_hostEnumerator).Reset ();

Modified: trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog
===================================================================
--- trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog    
2005-06-22 14:39:03 UTC (rev 46369)
+++ trunk/mcs/class/corlib/Test/System.Collections.Generic/ChangeLog    
2005-06-22 14:42:13 UTC (rev 46370)
@@ -1,18 +1,23 @@
+2005-06-22  Raja R Harinath  <[EMAIL PROTECTED]>
+
+       * DictionaryTest.cs (KeyValueEnumeratorTest): Add test for infloop
+       if dictionary is read while it's being enumerated.
+
 2005-06-20  David Waite  <[EMAIL PROTECTED]>
 
-    * ListTest.cs: Substantial new tests
+       * ListTest.cs: Substantial new tests
 
 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.
+       * 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]>
 

Modified: 
trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs
===================================================================
--- trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs    
2005-06-22 14:39:03 UTC (rev 46369)
+++ trunk/mcs/class/corlib/Test/System.Collections.Generic/DictionaryTest.cs    
2005-06-22 14:42:13 UTC (rev 46370)
@@ -84,7 +84,7 @@
                [Test]
                public void AddTest ()
                {
-                       _dictionary.Add ("key1", (object)"value");
+                       _dictionary.Add ("key1", "value");
                        Assert.AreEqual ("value", _dictionary ["key1"].ToString 
(), "Add failed!");
                }
        
@@ -149,7 +149,7 @@
                [Test]
                public void IndexerGetExistingTest ()
                {
-                       _dictionary.Add ("key1", (object)"value");
+                       _dictionary.Add ("key1", "value");
                        Assert.AreEqual ("value", _dictionary ["key1"].ToString 
(), "Add failed!");
                }
                
@@ -168,8 +168,8 @@
                [Test]
                public void IndexerSetExistingTest ()
                {
-                       _dictionary.Add ("key1", (object)"value1");
-                       _dictionary ["key1"] = (object) "value2";
+                       _dictionary.Add ("key1", "value1");
+                       _dictionary ["key1"] =  "value2";
                        Assert.AreEqual (1, _dictionary.Count);
                        Assert.AreEqual ("value2", _dictionary ["key1"]);
                }
@@ -177,7 +177,7 @@
                [Test]
                public void IndexerSetNonExistingTest ()
                {
-                       _dictionary ["key1"] = (object) "value1";
+                       _dictionary ["key1"] =  "value1";
                        Assert.AreEqual (1, _dictionary.Count);
                        Assert.AreEqual ("value1", _dictionary ["key1"]);
                }
@@ -218,10 +218,10 @@
                [Test]
                public void ClearTest ()
                {
-                       _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");
                        _dictionary.Clear ();
                        Assert.AreEqual (0, _dictionary.Count, "Clear method 
failed!");
                        Assert.IsFalse (_dictionary.ContainsKey ("key2"));
@@ -230,10 +230,10 @@
                [Test]
                public void ContainsKeyTest ()
                {
-                       _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");
                        bool contains = _dictionary.ContainsKey ("key4");
                        Assert.IsTrue (contains, "ContainsKey does not return 
correct value!");
                        contains = _dictionary.ContainsKey ("key5");
@@ -243,10 +243,10 @@
                [Test]
                public void ContainsValueTest ()
                {
-                       _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");
                        bool contains = _dictionary.ContainsValue ("value2");
                        Assert.IsTrue(contains, "ContainsValue does not return 
correct value!");
                        contains = _dictionary.ContainsValue ("@@daisofja@@");
@@ -256,10 +256,10 @@
                [Test]
                public void TryGetValueTest()
                {
-                       _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");
                        object value = "";
                        bool retrieved = _dictionary.TryGetValue ("key4", out 
value);
                        Assert.IsTrue (retrieved);
@@ -310,34 +310,34 @@
                {
                        Dictionary <object, object> dict = new Dictionary 
<object, object> ();
                        MyTest key1, key2, key3;
-                       dict.Add ((object) (key1 = new MyTest ("key1", 234)), 
(object)"value1");
-                       dict.Add ((object) (key2 = new MyTest ("key2", 444)), 
(object)"value2");
-                       dict.Add ((object) (key3 = new MyTest ("key3", 5655)), 
(object)"value3");
+                       dict.Add ( (key1 = new MyTest ("key1", 234)), "value1");
+                       dict.Add ( (key2 = new MyTest ("key2", 444)), "value2");
+                       dict.Add ( (key3 = new MyTest ("key3", 5655)), 
"value3");
        
-                       Assert.AreEqual ((object)"value2", dict [key2], "value 
is not returned!");
-                       Assert.AreEqual ((object)"value3", dict [(object)key3], 
"neg: exception should not be thrown!");
+                       Assert.AreEqual ("value2", dict [key2], "value is not 
returned!");
+                       Assert.AreEqual ("value3", dict [key3], "neg: exception 
should not be thrown!");
                }
        
                [Test, ExpectedException (typeof (ArgumentException))]
                public void IDictionaryAddTest ()
                {
                        IDictionary iDict = _dictionary as IDictionary;
-                       iDict.Add ((object)"key1", (object)"value1");
-                       iDict.Add ((object)"key2", (object)"value3");
+                       iDict.Add ("key1", "value1");
+                       iDict.Add ("key2", "value3");
                        Assert.AreEqual (2, iDict.Count, "IDictioanry interface 
add is not working!");
        
                        //Negative test case
-                       iDict.Add ((object)12, (object)"value");
-                       iDict.Add ((object)"key", (object)34);
+                       iDict.Add (12, "value");
+                       iDict.Add ("key", 34);
                }
        
                [Test]
                public void IEnumeratorTest ()
                {
-                       _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");
                        IEnumerator itr = 
((IEnumerable)_dictionary).GetEnumerator ();
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
@@ -353,10 +353,10 @@
                [Test]
                public void IEnumeratorGenericTest ()
                {
-                       _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");
                        IEnumerator <KeyValuePair <string, object>> itr = 
((IEnumerable <KeyValuePair <string, object>>)_dictionary).GetEnumerator ();
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
@@ -372,10 +372,10 @@
                [Test]
                public void IDictionaryEnumeratorTest ()
                {
-                       _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");
                        IDictionaryEnumerator itr = 
((IDictionary)_dictionary).GetEnumerator ();
                        while (itr.MoveNext ()) {
                                object o = itr.Current;
@@ -391,10 +391,10 @@
                [Test]
                public void ForEachTest ()
                {
-                       _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");
        
                        int i = 0;
                        foreach (KeyValuePair <string, object> entry in 
_dictionary)
@@ -422,13 +422,13 @@
                public void ResizeTest ()
                {
                        Dictionary <string, object> dictionary = new Dictionary 
<string, object> (3);
-                       dictionary.Add ("key1", (object)"value1");
-                       dictionary.Add ("key2", (object)"value2");
-                       dictionary.Add ("key3", (object)"value3");
+                       dictionary.Add ("key1", "value1");
+                       dictionary.Add ("key2", "value2");
+                       dictionary.Add ("key3", "value3");
        
                        Assert.AreEqual (3, dictionary.Count);
        
-                       dictionary.Add ("key4", (object)"value4");
+                       dictionary.Add ("key4", "value4");
                        Assert.AreEqual (4, dictionary.Count);
                        Assert.AreEqual ("value1", dictionary ["key1"].ToString 
(), "");
                        Assert.AreEqual ("value2", dictionary ["key2"].ToString 
(), "");
@@ -439,10 +439,10 @@
                [Test]
                public void KeyCollectionTest ()
                {
-                       _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");
        
                        ICollection <string> keys = ((IDictionary <string, 
object>)_dictionary).Keys;
                        Assert.AreEqual (4, keys.Count);
@@ -459,25 +459,32 @@
                {
                        IDictionary<int, int> d = new Dictionary<int, int>();
 
+                       // Values are chosen such that two keys map to the same 
bucket.
+                       // Default dictionary table size == 10
                        d [9] = 1;
                        d [10] = 2;
                        d [19] = 3;
 
-                       ICollection <int> vs = d.Values;
-       
+                       Assert.AreEqual (d.Count, d.Keys.Count, "d and d.Keys 
don't appear to match");
+                       Assert.AreEqual (d.Values.Count, d.Keys.Count, "d.Keys 
and d.Values don't appear to match");
+
                        int count = 0;
-                       foreach (int i in vs)
-                               count++;
-
-                       Assert.AreEqual (count, vs.Count);
-
-                       vs = d.Keys;
+                       foreach (int i in d.Values)
+                               ++count;
+                       Assert.AreEqual (count, d.Values.Count, "d.Values 
doesn't have the correct number of elements");
        
                        count = 0;
-                       foreach (int i in vs)
-                               count++;
+                       foreach (int i in d.Keys)
+                               ++count;
+                       Assert.AreEqual (count, d.Keys.Count, "d.Keys doesn't 
have the correct number of elements");
 
-                       Assert.AreEqual (count, vs.Count);
+                       int nkeys = count;
+                       count = 0;
+                       foreach (int i in d.Keys) {
+                               int foo = d [i];
+                               if (count++ >= nkeys)
+                                       Assert.Fail ("Reading a value appears 
to trash enumerator state");
+                       }
                }
 
                [Test]          // bug 75073
@@ -501,7 +508,7 @@
                        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);
+                       Assert.IsFalse(((object) enumerator.Current) is 
DictionaryEntry);
                }
 
                [Test]

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to