From 14f2222a50a18ff0d8772095587b9cdad455a7bb Mon Sep 17 00:00:00 2001 From: Jon Skeet Date: Fri, 3 Jul 2015 11:41:37 +0100 Subject: Lots more tests for FieldCodec, MapField, RepeatedField ... and some implementation changes to go with them. --- csharp/src/ProtocolBuffers/Collections/MapField.cs | 45 ++++++++++++++++++++-- 1 file changed, 41 insertions(+), 4 deletions(-) (limited to 'csharp/src/ProtocolBuffers/Collections/MapField.cs') diff --git a/csharp/src/ProtocolBuffers/Collections/MapField.cs b/csharp/src/ProtocolBuffers/Collections/MapField.cs index 779ff061..0f379eaa 100644 --- a/csharp/src/ProtocolBuffers/Collections/MapField.cs +++ b/csharp/src/ProtocolBuffers/Collections/MapField.cs @@ -367,11 +367,13 @@ namespace Google.Protobuf.Collections IDictionaryEnumerator IDictionary.GetEnumerator() { - throw new NotImplementedException(); + return new DictionaryEnumerator(GetEnumerator()); } void IDictionary.Remove(object key) { + ThrowHelper.ThrowIfNull(key, "key"); + this.CheckMutable(); if (!(key is TKey)) { return; @@ -381,7 +383,9 @@ namespace Google.Protobuf.Collections void ICollection.CopyTo(Array array, int index) { - throw new NotImplementedException(); + // This is ugly and slow as heck, but with any luck it will never be used anyway. + ICollection temp = this.Select(pair => new DictionaryEntry(pair.Key, pair.Value)).ToList(); + temp.CopyTo(array, index); } bool IDictionary.IsFixedSize { get { return IsFrozen; } } @@ -392,12 +396,13 @@ namespace Google.Protobuf.Collections bool ICollection.IsSynchronized { get { return false; } } - object ICollection.SyncRoot { get { return null; } } + object ICollection.SyncRoot { get { return this; } } object IDictionary.this[object key] { get { + ThrowHelper.ThrowIfNull(key, "key"); if (!(key is TKey)) { return null; @@ -407,10 +412,42 @@ namespace Google.Protobuf.Collections return value; } - set { this[(TKey)key] = (TValue)value; } + set + { + if (frozen) + { + throw new NotSupportedException("Dictionary is frozen"); + } + this[(TKey)key] = (TValue)value; + } } #endregion + private class DictionaryEnumerator : IDictionaryEnumerator + { + private readonly IEnumerator> enumerator; + + internal DictionaryEnumerator(IEnumerator> enumerator) + { + this.enumerator = enumerator; + } + + public bool MoveNext() + { + return enumerator.MoveNext(); + } + + public void Reset() + { + enumerator.Reset(); + } + + public object Current { get { return Entry; } } + public DictionaryEntry Entry { get { return new DictionaryEntry(Key, Value); } } + public object Key { get { return enumerator.Current.Key; } } + public object Value { get { return enumerator.Current.Value; } } + } + /// /// A codec for a specific map field. This contains all the information required to encoded and /// decode the nested messages. -- cgit v1.2.3