diff options
author | Jon Skeet <jonskeet@google.com> | 2015-07-03 11:41:37 +0100 |
---|---|---|
committer | Jon Skeet <jonskeet@google.com> | 2015-07-09 08:26:07 +0100 |
commit | 14f2222a50a18ff0d8772095587b9cdad455a7bb (patch) | |
tree | 1422e99fad875a04921a0b8c881f48c63937f84c /csharp/src/ProtocolBuffers | |
parent | af259b77bf04fcfb68609776cb27f04d289a2c39 (diff) | |
download | protobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.tar.gz protobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.tar.bz2 protobuf-14f2222a50a18ff0d8772095587b9cdad455a7bb.zip |
Lots more tests for FieldCodec, MapField, RepeatedField
... and some implementation changes to go with them.
Diffstat (limited to 'csharp/src/ProtocolBuffers')
-rw-r--r-- | csharp/src/ProtocolBuffers/Collections/MapField.cs | 45 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/Collections/RepeatedField.cs | 12 | ||||
-rw-r--r-- | csharp/src/ProtocolBuffers/FieldCodec.cs | 8 |
3 files changed, 49 insertions, 16 deletions
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<KeyValuePair<TKey, TValue>> enumerator; + + internal DictionaryEnumerator(IEnumerator<KeyValuePair<TKey, TValue>> 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; } } + } + /// <summary> /// A codec for a specific map field. This contains all the information required to encoded and /// decode the nested messages. diff --git a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs index ebc711de..b6b52cb9 100644 --- a/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs +++ b/csharp/src/ProtocolBuffers/Collections/RepeatedField.cs @@ -376,6 +376,7 @@ namespace Google.Protobuf.Collections this.CheckMutable(); EnsureSize(count + 1); Array.Copy(array, index, array, index + 1, count - index); + array[index] = item; count++; } @@ -421,18 +422,12 @@ namespace Google.Protobuf.Collections void ICollection.CopyTo(Array array, int index) { - ThrowHelper.ThrowIfNull(array, "array"); - T[] strongArray = array as T[]; - if (strongArray == null) - { - throw new ArgumentException("Array is of incorrect type", "array"); - } - CopyTo(strongArray, index); + Array.Copy(this.array, 0, array, index, count); } bool ICollection.IsSynchronized { get { return false; } } - object ICollection.SyncRoot { get { return null; } } + object ICollection.SyncRoot { get { return this; } } object IList.this[int index] { @@ -490,6 +485,7 @@ namespace Google.Protobuf.Collections { if (index + 1 >= field.Count) { + index = field.Count; return false; } index++; diff --git a/csharp/src/ProtocolBuffers/FieldCodec.cs b/csharp/src/ProtocolBuffers/FieldCodec.cs index 2cebc1bb..c72a3e7b 100644 --- a/csharp/src/ProtocolBuffers/FieldCodec.cs +++ b/csharp/src/ProtocolBuffers/FieldCodec.cs @@ -68,12 +68,12 @@ namespace Google.Protobuf public static FieldCodec<uint> ForFixed32(uint tag) { - return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), CodedOutputStream.ComputeFixed32Size, tag); + return new FieldCodec<uint>(input => input.ReadFixed32(), (output, value) => output.WriteFixed32(value), 4, tag); } public static FieldCodec<int> ForSFixed32(uint tag) { - return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), CodedOutputStream.ComputeSFixed32Size, tag); + return new FieldCodec<int>(input => input.ReadSFixed32(), (output, value) => output.WriteSFixed32(value), 4, tag); } public static FieldCodec<uint> ForUInt32(uint tag) @@ -93,12 +93,12 @@ namespace Google.Protobuf public static FieldCodec<ulong> ForFixed64(uint tag) { - return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), CodedOutputStream.ComputeFixed64Size, tag); + return new FieldCodec<ulong>(input => input.ReadFixed64(), (output, value) => output.WriteFixed64(value), 8, tag); } public static FieldCodec<long> ForSFixed64(uint tag) { - return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), CodedOutputStream.ComputeSFixed64Size, tag); + return new FieldCodec<long>(input => input.ReadSFixed64(), (output, value) => output.WriteSFixed64(value), 8, tag); } public static FieldCodec<ulong> ForUInt64(uint tag) |