From 99002ae47a3f88f64ff44dbd80452360e0e5fbc4 Mon Sep 17 00:00:00 2001 From: Bo Yang Date: Tue, 23 Jan 2018 15:46:06 -0800 Subject: Initialize python-wheel branch --- .../Collections/MapFieldTest.cs | 607 ---------------- .../Collections/ProtobufEqualityComparersTest.cs | 124 ---- .../Collections/RepeatedFieldTest.cs | 759 --------------------- 3 files changed, 1490 deletions(-) delete mode 100644 csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs delete mode 100644 csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs delete mode 100644 csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs (limited to 'csharp/src/Google.Protobuf.Test/Collections') diff --git a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs deleted file mode 100644 index 8791dffc..00000000 --- a/csharp/src/Google.Protobuf.Test/Collections/MapFieldTest.cs +++ /dev/null @@ -1,607 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections.Generic; -using Google.Protobuf.TestProtos; -using NUnit.Framework; -using System.Collections; -using System.Linq; - -namespace Google.Protobuf.Collections -{ - /// - /// Tests for MapField which aren't reliant on the encoded format - - /// tests for serialization/deserialization are part of GeneratedMessageTest. - /// - public class MapFieldTest - { - [Test] - public void Clone_ClonesMessages() - { - var message = new ForeignMessage { C = 20 }; - var map = new MapField { { "x", message } }; - var clone = map.Clone(); - map["x"].C = 30; - Assert.AreEqual(20, clone["x"].C); - } - - [Test] - public void NullValuesProhibited() - { - TestNullValues(0); - TestNullValues(""); - TestNullValues(new TestAllTypes()); - } - - private void TestNullValues(T nonNullValue) - { - var map = new MapField(); - var nullValue = (T) (object) null; - Assert.Throws(() => map.Add(0, nullValue)); - Assert.Throws(() => map[0] = nullValue); - map.Add(1, nonNullValue); - map[1] = nonNullValue; - } - - [Test] - public void Add_ForbidsNullKeys() - { - var map = new MapField(); - Assert.Throws(() => map.Add(null, new ForeignMessage())); - } - - [Test] - public void Indexer_ForbidsNullKeys() - { - var map = new MapField(); - Assert.Throws(() => map[null] = new ForeignMessage()); - } - - [Test] - public void AddPreservesInsertionOrder() - { - var map = new MapField(); - map.Add("a", "v1"); - map.Add("b", "v2"); - map.Add("c", "v3"); - map.Remove("b"); - map.Add("d", "v4"); - CollectionAssert.AreEqual(new[] { "a", "c", "d" }, map.Keys); - CollectionAssert.AreEqual(new[] { "v1", "v3", "v4" }, map.Values); - } - - [Test] - public void EqualityIsOrderInsensitive() - { - var map1 = new MapField(); - map1.Add("a", "v1"); - map1.Add("b", "v2"); - - var map2 = new MapField(); - map2.Add("b", "v2"); - map2.Add("a", "v1"); - - EqualityTester.AssertEquality(map1, map2); - } - - [Test] - public void EqualityIsKeySensitive() - { - var map1 = new MapField(); - map1.Add("first key", "v1"); - map1.Add("second key", "v2"); - - var map2 = new MapField(); - map2.Add("third key", "v1"); - map2.Add("fourth key", "v2"); - - EqualityTester.AssertInequality(map1, map2); - } - - [Test] - public void Equality_Simple() - { - var map = new MapField(); - EqualityTester.AssertEquality(map, map); - EqualityTester.AssertInequality(map, null); - Assert.IsFalse(map.Equals(new object())); - } - - [Test] - public void EqualityIsValueSensitive() - { - // Note: Without some care, it's a little easier than one might - // hope to see hash collisions, but only in some environments... - var map1 = new MapField(); - map1.Add("a", "first value"); - map1.Add("b", "second value"); - - var map2 = new MapField(); - map2.Add("a", "third value"); - map2.Add("b", "fourth value"); - - EqualityTester.AssertInequality(map1, map2); - } - - [Test] - public void Add_Dictionary() - { - var map1 = new MapField - { - { "x", "y" }, - { "a", "b" } - }; - var map2 = new MapField - { - { "before", "" }, - map1, - { "after", "" } - }; - var expected = new MapField - { - { "before", "" }, - { "x", "y" }, - { "a", "b" }, - { "after", "" } - }; - Assert.AreEqual(expected, map2); - CollectionAssert.AreEqual(new[] { "before", "x", "a", "after" }, map2.Keys); - } - - // General IDictionary behavior tests - [Test] - public void Add_KeyAlreadyExists() - { - var map = new MapField(); - map.Add("foo", "bar"); - Assert.Throws(() => map.Add("foo", "baz")); - } - - [Test] - public void Add_Pair() - { - var map = new MapField(); - ICollection> collection = map; - collection.Add(NewKeyValuePair("x", "y")); - Assert.AreEqual("y", map["x"]); - Assert.Throws(() => collection.Add(NewKeyValuePair("x", "z"))); - } - - [Test] - public void Contains_Pair() - { - var map = new MapField { { "x", "y" } }; - ICollection> collection = map; - Assert.IsTrue(collection.Contains(NewKeyValuePair("x", "y"))); - Assert.IsFalse(collection.Contains(NewKeyValuePair("x", "z"))); - Assert.IsFalse(collection.Contains(NewKeyValuePair("z", "y"))); - } - - [Test] - public void Remove_Key() - { - var map = new MapField(); - map.Add("foo", "bar"); - Assert.AreEqual(1, map.Count); - Assert.IsFalse(map.Remove("missing")); - Assert.AreEqual(1, map.Count); - Assert.IsTrue(map.Remove("foo")); - Assert.AreEqual(0, map.Count); - Assert.Throws(() => map.Remove(null)); - } - - [Test] - public void Remove_Pair() - { - var map = new MapField(); - map.Add("foo", "bar"); - ICollection> collection = map; - Assert.AreEqual(1, map.Count); - Assert.IsFalse(collection.Remove(NewKeyValuePair("wrong key", "bar"))); - Assert.AreEqual(1, map.Count); - Assert.IsFalse(collection.Remove(NewKeyValuePair("foo", "wrong value"))); - Assert.AreEqual(1, map.Count); - Assert.IsTrue(collection.Remove(NewKeyValuePair("foo", "bar"))); - Assert.AreEqual(0, map.Count); - Assert.Throws(() => collection.Remove(new KeyValuePair(null, ""))); - } - - [Test] - public void CopyTo_Pair() - { - var map = new MapField(); - map.Add("foo", "bar"); - ICollection> collection = map; - KeyValuePair[] array = new KeyValuePair[3]; - collection.CopyTo(array, 1); - Assert.AreEqual(NewKeyValuePair("foo", "bar"), array[1]); - } - - [Test] - public void Clear() - { - var map = new MapField { { "x", "y" } }; - Assert.AreEqual(1, map.Count); - map.Clear(); - Assert.AreEqual(0, map.Count); - map.Add("x", "y"); - Assert.AreEqual(1, map.Count); - } - - [Test] - public void Indexer_Get() - { - var map = new MapField { { "x", "y" } }; - Assert.AreEqual("y", map["x"]); - Assert.Throws(() => { var ignored = map["z"]; }); - } - - [Test] - public void Indexer_Set() - { - var map = new MapField(); - map["x"] = "y"; - Assert.AreEqual("y", map["x"]); - map["x"] = "z"; // This won't throw, unlike Add. - Assert.AreEqual("z", map["x"]); - } - - [Test] - public void GetEnumerator_NonGeneric() - { - IEnumerable map = new MapField { { "x", "y" } }; - CollectionAssert.AreEqual(new[] { new KeyValuePair("x", "y") }, - map.Cast().ToList()); - } - - // Test for the explicitly-implemented non-generic IDictionary interface - [Test] - public void IDictionary_GetEnumerator() - { - IDictionary map = new MapField { { "x", "y" } }; - var enumerator = map.GetEnumerator(); - - // Commented assertions show an ideal situation - it looks like - // the LinkedList enumerator doesn't throw when you ask for the current entry - // at an inappropriate time; fixing this would be more work than it's worth. - // Assert.Throws(() => enumerator.Current.GetHashCode()); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual("x", enumerator.Key); - Assert.AreEqual("y", enumerator.Value); - Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Current); - Assert.AreEqual(new DictionaryEntry("x", "y"), enumerator.Entry); - Assert.IsFalse(enumerator.MoveNext()); - // Assert.Throws(() => enumerator.Current.GetHashCode()); - enumerator.Reset(); - // Assert.Throws(() => enumerator.Current.GetHashCode()); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual("x", enumerator.Key); // Assume the rest are okay - } - - [Test] - public void IDictionary_Add() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - dictionary.Add("a", "b"); - Assert.AreEqual("b", map["a"]); - Assert.Throws(() => dictionary.Add("a", "duplicate")); - Assert.Throws(() => dictionary.Add(new object(), "key is bad")); - Assert.Throws(() => dictionary.Add("value is bad", new object())); - } - - [Test] - public void IDictionary_Contains() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - - Assert.IsFalse(dictionary.Contains("a")); - Assert.IsFalse(dictionary.Contains(5)); - // Surprising, but IDictionary.Contains is only about keys. - Assert.IsFalse(dictionary.Contains(new DictionaryEntry("x", "y"))); - Assert.IsTrue(dictionary.Contains("x")); - } - - [Test] - public void IDictionary_Remove() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - dictionary.Remove("a"); - Assert.AreEqual(1, dictionary.Count); - dictionary.Remove(5); - Assert.AreEqual(1, dictionary.Count); - dictionary.Remove(new DictionaryEntry("x", "y")); - Assert.AreEqual(1, dictionary.Count); - dictionary.Remove("x"); - Assert.AreEqual(0, dictionary.Count); - Assert.Throws(() => dictionary.Remove(null)); - } - - [Test] - public void IDictionary_CopyTo() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - var array = new DictionaryEntry[3]; - dictionary.CopyTo(array, 1); - CollectionAssert.AreEqual(new[] { default(DictionaryEntry), new DictionaryEntry("x", "y"), default(DictionaryEntry) }, - array); - var objectArray = new object[3]; - dictionary.CopyTo(objectArray, 1); - CollectionAssert.AreEqual(new object[] { null, new DictionaryEntry("x", "y"), null }, - objectArray); - } - - [Test] - public void IDictionary_IsFixedSize() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - Assert.IsFalse(dictionary.IsFixedSize); - } - - [Test] - public void IDictionary_Keys() - { - IDictionary dictionary = new MapField { { "x", "y" } }; - CollectionAssert.AreEqual(new[] { "x" }, dictionary.Keys); - } - - [Test] - public void IDictionary_Values() - { - IDictionary dictionary = new MapField { { "x", "y" } }; - CollectionAssert.AreEqual(new[] { "y" }, dictionary.Values); - } - - [Test] - public void IDictionary_IsSynchronized() - { - IDictionary dictionary = new MapField { { "x", "y" } }; - Assert.IsFalse(dictionary.IsSynchronized); - } - - [Test] - public void IDictionary_SyncRoot() - { - IDictionary dictionary = new MapField { { "x", "y" } }; - Assert.AreSame(dictionary, dictionary.SyncRoot); - } - - [Test] - public void IDictionary_Indexer_Get() - { - IDictionary dictionary = new MapField { { "x", "y" } }; - Assert.AreEqual("y", dictionary["x"]); - Assert.IsNull(dictionary["a"]); - Assert.IsNull(dictionary[5]); - Assert.Throws(() => dictionary[null].GetHashCode()); - } - - [Test] - public void IDictionary_Indexer_Set() - { - var map = new MapField { { "x", "y" } }; - IDictionary dictionary = map; - map["a"] = "b"; - Assert.AreEqual("b", map["a"]); - map["a"] = "c"; - Assert.AreEqual("c", map["a"]); - Assert.Throws(() => dictionary[5] = "x"); - Assert.Throws(() => dictionary["x"] = 5); - Assert.Throws(() => dictionary[null] = "z"); - Assert.Throws(() => dictionary["x"] = null); - } - - [Test] - public void KeysReturnsLiveView() - { - var map = new MapField(); - var keys = map.Keys; - CollectionAssert.AreEqual(new string[0], keys); - map["foo"] = "bar"; - map["x"] = "y"; - CollectionAssert.AreEqual(new[] { "foo", "x" }, keys); - } - - [Test] - public void ValuesReturnsLiveView() - { - var map = new MapField(); - var values = map.Values; - CollectionAssert.AreEqual(new string[0], values); - map["foo"] = "bar"; - map["x"] = "y"; - CollectionAssert.AreEqual(new[] { "bar", "y" }, values); - } - - // Just test keys - we know the implementation is the same for values - [Test] - public void ViewsAreReadOnly() - { - var map = new MapField(); - var keys = map.Keys; - Assert.IsTrue(keys.IsReadOnly); - Assert.Throws(() => keys.Clear()); - Assert.Throws(() => keys.Remove("a")); - Assert.Throws(() => keys.Add("a")); - } - - // Just test keys - we know the implementation is the same for values - [Test] - public void ViewCopyTo() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - var keys = map.Keys; - var array = new string[4]; - Assert.Throws(() => keys.CopyTo(array, 3)); - Assert.Throws(() => keys.CopyTo(array, -1)); - keys.CopyTo(array, 1); - CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); - } - - // Just test keys - we know the implementation is the same for values - [Test] - public void NonGenericViewCopyTo() - { - IDictionary map = new MapField { { "foo", "bar" }, { "x", "y" } }; - ICollection keys = map.Keys; - // Note the use of the Array type here rather than string[] - Array array = new string[4]; - Assert.Throws(() => keys.CopyTo(array, 3)); - Assert.Throws(() => keys.CopyTo(array, -1)); - keys.CopyTo(array, 1); - CollectionAssert.AreEqual(new[] { null, "foo", "x", null }, array); - } - - [Test] - public void KeysContains() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - var keys = map.Keys; - Assert.IsTrue(keys.Contains("foo")); - Assert.IsFalse(keys.Contains("bar")); // It's a value! - Assert.IsFalse(keys.Contains("1")); - // Keys can't be null, so we should prevent contains check - Assert.Throws(() => keys.Contains(null)); - } - - [Test] - public void KeysCopyTo() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - var keys = map.Keys.ToArray(); // Uses CopyTo internally - CollectionAssert.AreEquivalent(new[] { "foo", "x" }, keys); - } - - [Test] - public void ValuesContains() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - var values = map.Values; - Assert.IsTrue(values.Contains("bar")); - Assert.IsFalse(values.Contains("foo")); // It's a key! - Assert.IsFalse(values.Contains("1")); - // Values can be null, so this makes sense - Assert.IsFalse(values.Contains(null)); - } - - [Test] - public void ValuesCopyTo() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - var values = map.Values.ToArray(); // Uses CopyTo internally - CollectionAssert.AreEquivalent(new[] { "bar", "y" }, values); - } - - [Test] - public void ToString_StringToString() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - Assert.AreEqual("{ \"foo\": \"bar\", \"x\": \"y\" }", map.ToString()); - } - - [Test] - public void ToString_UnsupportedKeyType() - { - var map = new MapField { { 10, "foo" } }; - Assert.Throws(() => map.ToString()); - } - - [Test] - public void NaNValuesComparedBitwise() - { - var map1 = new MapField - { - { "x", SampleNaNs.Regular }, - { "y", SampleNaNs.SignallingFlipped } - }; - - var map2 = new MapField - { - { "x", SampleNaNs.Regular }, - { "y", SampleNaNs.PayloadFlipped } - }; - - var map3 = new MapField - { - { "x", SampleNaNs.Regular }, - { "y", SampleNaNs.SignallingFlipped } - }; - - EqualityTester.AssertInequality(map1, map2); - EqualityTester.AssertEquality(map1, map3); - Assert.True(map1.Values.Contains(SampleNaNs.SignallingFlipped)); - Assert.False(map2.Values.Contains(SampleNaNs.SignallingFlipped)); - } - - // This wouldn't usually happen, as protos can't use doubles as map keys, - // but let's be consistent. - [Test] - public void NaNKeysComparedBitwise() - { - var map = new MapField - { - { SampleNaNs.Regular, "x" }, - { SampleNaNs.SignallingFlipped, "y" } - }; - Assert.AreEqual("x", map[SampleNaNs.Regular]); - Assert.AreEqual("y", map[SampleNaNs.SignallingFlipped]); - string ignored; - Assert.False(map.TryGetValue(SampleNaNs.PayloadFlipped, out ignored)); - } - -#if !NET35 - [Test] - public void IDictionaryKeys_Equals_IReadOnlyDictionaryKeys() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - CollectionAssert.AreEquivalent(((IDictionary)map).Keys, ((IReadOnlyDictionary)map).Keys); - } - - [Test] - public void IDictionaryValues_Equals_IReadOnlyDictionaryValues() - { - var map = new MapField { { "foo", "bar" }, { "x", "y" } }; - CollectionAssert.AreEquivalent(((IDictionary)map).Values, ((IReadOnlyDictionary)map).Values); - } -#endif - - private static KeyValuePair NewKeyValuePair(TKey key, TValue value) - { - return new KeyValuePair(key, value); - } - } -} diff --git a/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs b/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs deleted file mode 100644 index c76d7ca1..00000000 --- a/csharp/src/Google.Protobuf.Test/Collections/ProtobufEqualityComparersTest.cs +++ /dev/null @@ -1,124 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2017 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using NUnit.Framework; -using System.Collections.Generic; -using System.Linq; -using static Google.Protobuf.Collections.ProtobufEqualityComparers; - -namespace Google.Protobuf.Collections -{ - public class ProtobufEqualityComparersTest - { - private static readonly double[] doubles = - { - 0, - 1, - 1.5, - -1.5, - double.PositiveInfinity, - double.NegativeInfinity, - // Three different types of NaN... - SampleNaNs.Regular, - SampleNaNs.SignallingFlipped, - SampleNaNs.PayloadFlipped - }; - - [Test] - public void GetEqualityComparer_Default() - { - // It's more pain than it's worth to try to parameterize these tests. - Assert.AreSame(EqualityComparer.Default, GetEqualityComparer()); - Assert.AreSame(EqualityComparer.Default, GetEqualityComparer()); - Assert.AreSame(EqualityComparer.Default, GetEqualityComparer()); - Assert.AreSame(EqualityComparer.Default, GetEqualityComparer()); - } - - [Test] - public void GetEqualityComparer_NotDefault() - { - // It's more pain than it's worth to try to parameterize these tests. - Assert.AreSame(BitwiseDoubleEqualityComparer, GetEqualityComparer()); - Assert.AreSame(BitwiseSingleEqualityComparer, GetEqualityComparer()); - Assert.AreSame(BitwiseNullableDoubleEqualityComparer, GetEqualityComparer()); - Assert.AreSame(BitwiseNullableSingleEqualityComparer, GetEqualityComparer()); - } - - [Test] - public void DoubleComparisons() - { - ValidateEqualityComparer(BitwiseDoubleEqualityComparer, doubles); - } - - [Test] - public void NullableDoubleComparisons() - { - ValidateEqualityComparer(BitwiseNullableDoubleEqualityComparer, doubles.Select(d => (double?) d).Concat(new double?[] { null })); - } - - [Test] - public void SingleComparisons() - { - ValidateEqualityComparer(BitwiseSingleEqualityComparer, doubles.Select(d => (float) d)); - } - - [Test] - public void NullableSingleComparisons() - { - ValidateEqualityComparer(BitwiseNullableSingleEqualityComparer, doubles.Select(d => (float?) d).Concat(new float?[] { null })); - } - - private static void ValidateEqualityComparer(EqualityComparer comparer, IEnumerable values) - { - var array = values.ToArray(); - // Each value should be equal to itself, but not to any other value. - for (int i = 0; i < array.Length; i++) - { - for (int j = 0; j < array.Length; j++) - { - if (i == j) - { - Assert.IsTrue(comparer.Equals(array[i], array[j]), - "{0} should be equal to itself", array[i], array[j]); - } - else - { - Assert.IsFalse(comparer.Equals(array[i], array[j]), - "{0} and {1} should not be equal", array[i], array[j]); - Assert.AreNotEqual(comparer.GetHashCode(array[i]), comparer.GetHashCode(array[j]), - "Hash codes for {0} and {1} should not be equal", array[i], array[j]); - } - } - } - } - } -} diff --git a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs b/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs deleted file mode 100644 index 129923b6..00000000 --- a/csharp/src/Google.Protobuf.Test/Collections/RepeatedFieldTest.cs +++ /dev/null @@ -1,759 +0,0 @@ -#region Copyright notice and license -// Protocol Buffers - Google's data interchange format -// Copyright 2015 Google Inc. All rights reserved. -// https://developers.google.com/protocol-buffers/ -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions are -// met: -// -// * Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// * Redistributions in binary form must reproduce the above -// copyright notice, this list of conditions and the following disclaimer -// in the documentation and/or other materials provided with the -// distribution. -// * Neither the name of Google Inc. nor the names of its -// contributors may be used to endorse or promote products derived from -// this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -#endregion - -using System; -using System.Collections; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Text; -using Google.Protobuf.TestProtos; -using Google.Protobuf.WellKnownTypes; -using NUnit.Framework; - -namespace Google.Protobuf.Collections -{ - public class RepeatedFieldTest - { - [Test] - public void NullValuesRejected() - { - var list = new RepeatedField(); - Assert.Throws(() => list.Add((string)null)); - Assert.Throws(() => list.Add((IEnumerable)null)); - Assert.Throws(() => list.Add((RepeatedField)null)); - Assert.Throws(() => list.Contains(null)); - Assert.Throws(() => list.IndexOf(null)); - } - - [Test] - public void Add_SingleItem() - { - var list = new RepeatedField(); - list.Add("foo"); - Assert.AreEqual(1, list.Count); - Assert.AreEqual("foo", list[0]); - } - - [Test] - public void Add_Sequence() - { - var list = new RepeatedField(); - list.Add(new[] { "foo", "bar" }); - Assert.AreEqual(2, list.Count); - Assert.AreEqual("foo", list[0]); - Assert.AreEqual("bar", list[1]); - } - - [Test] - public void AddRange_SlowPath() - { - var list = new RepeatedField(); - list.AddRange(new[] { "foo", "bar" }.Select(x => x)); - Assert.AreEqual(2, list.Count); - Assert.AreEqual("foo", list[0]); - Assert.AreEqual("bar", list[1]); - } - - [Test] - public void AddRange_SlowPath_NullsProhibited_ReferenceType() - { - var list = new RepeatedField(); - // It's okay for this to throw ArgumentNullException if necessary. - // It's not ideal, but not awful. - Assert.Catch(() => list.AddRange(new[] { "foo", null }.Select(x => x))); - } - - [Test] - public void AddRange_SlowPath_NullsProhibited_NullableValueType() - { - var list = new RepeatedField(); - // It's okay for this to throw ArgumentNullException if necessary. - // It's not ideal, but not awful. - Assert.Catch(() => list.AddRange(new[] { 20, (int?)null }.Select(x => x))); - } - - [Test] - public void AddRange_Optimized_NonNullableValueType() - { - var list = new RepeatedField(); - list.AddRange(new List { 20, 30 }); - Assert.AreEqual(2, list.Count); - Assert.AreEqual(20, list[0]); - Assert.AreEqual(30, list[1]); - } - - [Test] - public void AddRange_Optimized_ReferenceType() - { - var list = new RepeatedField(); - list.AddRange(new List { "foo", "bar" }); - Assert.AreEqual(2, list.Count); - Assert.AreEqual("foo", list[0]); - Assert.AreEqual("bar", list[1]); - } - - [Test] - public void AddRange_Optimized_NullableValueType() - { - var list = new RepeatedField(); - list.AddRange(new List { 20, 30 }); - Assert.AreEqual(2, list.Count); - Assert.AreEqual((int?) 20, list[0]); - Assert.AreEqual((int?) 30, list[1]); - } - - [Test] - public void AddRange_Optimized_NullsProhibited_ReferenceType() - { - // We don't just trust that a collection with a nullable element type doesn't contain nulls - var list = new RepeatedField(); - // It's okay for this to throw ArgumentNullException if necessary. - // It's not ideal, but not awful. - Assert.Catch(() => list.AddRange(new List { "foo", null })); - } - - [Test] - public void AddRange_Optimized_NullsProhibited_NullableValueType() - { - // We don't just trust that a collection with a nullable element type doesn't contain nulls - var list = new RepeatedField(); - // It's okay for this to throw ArgumentNullException if necessary. - // It's not ideal, but not awful. - Assert.Catch(() => list.AddRange(new List { 20, null })); - } - - [Test] - public void AddRange_AlreadyNotEmpty() - { - var list = new RepeatedField { 1, 2, 3 }; - list.AddRange(new List { 4, 5, 6 }); - CollectionAssert.AreEqual(new[] { 1, 2, 3, 4, 5, 6 }, list); - } - - [Test] - public void AddRange_RepeatedField() - { - var list = new RepeatedField { "original" }; - list.AddRange(new RepeatedField { "foo", "bar" }); - Assert.AreEqual(3, list.Count); - Assert.AreEqual("original", list[0]); - Assert.AreEqual("foo", list[1]); - Assert.AreEqual("bar", list[2]); - } - - [Test] - public void RemoveAt_Valid() - { - var list = new RepeatedField { "first", "second", "third" }; - list.RemoveAt(1); - CollectionAssert.AreEqual(new[] { "first", "third" }, list); - // Just check that these don't throw... - list.RemoveAt(list.Count - 1); // Now the count will be 1... - list.RemoveAt(0); - Assert.AreEqual(0, list.Count); - } - - [Test] - public void RemoveAt_Invalid() - { - var list = new RepeatedField { "first", "second", "third" }; - Assert.Throws(() => list.RemoveAt(-1)); - Assert.Throws(() => list.RemoveAt(3)); - } - - [Test] - public void Insert_Valid() - { - var list = new RepeatedField { "first", "second" }; - list.Insert(1, "middle"); - CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list); - list.Insert(3, "end"); - CollectionAssert.AreEqual(new[] { "first", "middle", "second", "end" }, list); - list.Insert(0, "start"); - CollectionAssert.AreEqual(new[] { "start", "first", "middle", "second", "end" }, list); - } - - [Test] - public void Insert_Invalid() - { - var list = new RepeatedField { "first", "second" }; - Assert.Throws(() => list.Insert(-1, "foo")); - Assert.Throws(() => list.Insert(3, "foo")); - Assert.Throws(() => list.Insert(0, null)); - } - - [Test] - public void Equals_RepeatedField() - { - var list = new RepeatedField { "first", "second" }; - Assert.IsFalse(list.Equals((RepeatedField) null)); - Assert.IsTrue(list.Equals(list)); - Assert.IsFalse(list.Equals(new RepeatedField { "first", "third" })); - Assert.IsFalse(list.Equals(new RepeatedField { "first" })); - Assert.IsTrue(list.Equals(new RepeatedField { "first", "second" })); - } - - [Test] - public void Equals_Object() - { - var list = new RepeatedField { "first", "second" }; - Assert.IsFalse(list.Equals((object) null)); - Assert.IsTrue(list.Equals((object) list)); - Assert.IsFalse(list.Equals((object) new RepeatedField { "first", "third" })); - Assert.IsFalse(list.Equals((object) new RepeatedField { "first" })); - Assert.IsTrue(list.Equals((object) new RepeatedField { "first", "second" })); - Assert.IsFalse(list.Equals(new object())); - } - - [Test] - public void GetEnumerator_GenericInterface() - { - IEnumerable list = new RepeatedField { "first", "second" }; - // Select gets rid of the optimizations in ToList... - CollectionAssert.AreEqual(new[] { "first", "second" }, list.Select(x => x).ToList()); - } - - [Test] - public void GetEnumerator_NonGenericInterface() - { - IEnumerable list = new RepeatedField { "first", "second" }; - CollectionAssert.AreEqual(new[] { "first", "second" }, list.Cast().ToList()); - } - - [Test] - public void CopyTo() - { - var list = new RepeatedField { "first", "second" }; - string[] stringArray = new string[4]; - list.CopyTo(stringArray, 1); - CollectionAssert.AreEqual(new[] { null, "first", "second", null }, stringArray); - } - - [Test] - public void Indexer_Get() - { - var list = new RepeatedField { "first", "second" }; - Assert.AreEqual("first", list[0]); - Assert.AreEqual("second", list[1]); - Assert.Throws(() => list[-1].GetHashCode()); - Assert.Throws(() => list[2].GetHashCode()); - } - - [Test] - public void Indexer_Set() - { - var list = new RepeatedField { "first", "second" }; - list[0] = "changed"; - Assert.AreEqual("changed", list[0]); - Assert.Throws(() => list[0] = null); - Assert.Throws(() => list[-1] = "bad"); - Assert.Throws(() => list[2] = "bad"); - } - - [Test] - public void Clone_ReturnsMutable() - { - var list = new RepeatedField { 0 }; - var clone = list.Clone(); - clone[0] = 1; - } - - [Test] - public void Enumerator() - { - var list = new RepeatedField { "first", "second" }; - using (var enumerator = list.GetEnumerator()) - { - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual("first", enumerator.Current); - Assert.IsTrue(enumerator.MoveNext()); - Assert.AreEqual("second", enumerator.Current); - Assert.IsFalse(enumerator.MoveNext()); - Assert.IsFalse(enumerator.MoveNext()); - } - } - - [Test] - public void AddEntriesFrom_PackedInt32() - { - uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - var length = CodedOutputStream.ComputeInt32Size(10) - + CodedOutputStream.ComputeInt32Size(999) - + CodedOutputStream.ComputeInt32Size(-1000); - output.WriteTag(packedTag); - output.WriteRawVarint32((uint) length); - output.WriteInt32(10); - output.WriteInt32(999); - output.WriteInt32(-1000); - output.Flush(); - stream.Position = 0; - - // Deliberately "expecting" a non-packed tag, but we detect that the data is - // actually packed. - uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var field = new RepeatedField(); - var input = new CodedInputStream(stream); - input.AssertNextTag(packedTag); - field.AddEntriesFrom(input, FieldCodec.ForInt32(nonPackedTag)); - CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void AddEntriesFrom_NonPackedInt32() - { - uint nonPackedTag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - output.WriteTag(nonPackedTag); - output.WriteInt32(10); - output.WriteTag(nonPackedTag); - output.WriteInt32(999); - output.WriteTag(nonPackedTag); - output.WriteInt32(-1000); // Just for variety... - output.Flush(); - stream.Position = 0; - - // Deliberately "expecting" a packed tag, but we detect that the data is - // actually not packed. - uint packedTag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var field = new RepeatedField(); - var input = new CodedInputStream(stream); - input.AssertNextTag(nonPackedTag); - field.AddEntriesFrom(input, FieldCodec.ForInt32(packedTag)); - CollectionAssert.AreEqual(new[] { 10, 999, -1000 }, field); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void AddEntriesFrom_String() - { - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - output.WriteTag(tag); - output.WriteString("Foo"); - output.WriteTag(tag); - output.WriteString(""); - output.WriteTag(tag); - output.WriteString("Bar"); - output.Flush(); - stream.Position = 0; - - var field = new RepeatedField(); - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - field.AddEntriesFrom(input, FieldCodec.ForString(tag)); - CollectionAssert.AreEqual(new[] { "Foo", "", "Bar" }, field); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void AddEntriesFrom_Message() - { - var message1 = new ForeignMessage { C = 2000 }; - var message2 = new ForeignMessage { C = -250 }; - - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - output.WriteTag(tag); - output.WriteMessage(message1); - output.WriteTag(tag); - output.WriteMessage(message2); - output.Flush(); - stream.Position = 0; - - var field = new RepeatedField(); - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - field.AddEntriesFrom(input, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); - CollectionAssert.AreEqual(new[] { message1, message2}, field); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void WriteTo_PackedInt32() - { - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var field = new RepeatedField { 10, 1000, 1000000 }; - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - field.WriteTo(output, FieldCodec.ForInt32(tag)); - output.Flush(); - stream.Position = 0; - - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - var length = input.ReadLength(); - Assert.AreEqual(10, input.ReadInt32()); - Assert.AreEqual(1000, input.ReadInt32()); - Assert.AreEqual(1000000, input.ReadInt32()); - Assert.IsTrue(input.IsAtEnd); - Assert.AreEqual(1 + CodedOutputStream.ComputeLengthSize(length) + length, stream.Length); - } - - [Test] - public void WriteTo_NonPackedInt32() - { - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.Varint); - var field = new RepeatedField { 10, 1000, 1000000}; - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - field.WriteTo(output, FieldCodec.ForInt32(tag)); - output.Flush(); - stream.Position = 0; - - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - Assert.AreEqual(10, input.ReadInt32()); - input.AssertNextTag(tag); - Assert.AreEqual(1000, input.ReadInt32()); - input.AssertNextTag(tag); - Assert.AreEqual(1000000, input.ReadInt32()); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void WriteTo_String() - { - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var field = new RepeatedField { "Foo", "", "Bar" }; - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - field.WriteTo(output, FieldCodec.ForString(tag)); - output.Flush(); - stream.Position = 0; - - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - Assert.AreEqual("Foo", input.ReadString()); - input.AssertNextTag(tag); - Assert.AreEqual("", input.ReadString()); - input.AssertNextTag(tag); - Assert.AreEqual("Bar", input.ReadString()); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void WriteTo_Message() - { - var message1 = new ForeignMessage { C = 20 }; - var message2 = new ForeignMessage { C = 25 }; - uint tag = WireFormat.MakeTag(10, WireFormat.WireType.LengthDelimited); - var field = new RepeatedField { message1, message2 }; - var stream = new MemoryStream(); - var output = new CodedOutputStream(stream); - field.WriteTo(output, FieldCodec.ForMessage(tag, ForeignMessage.Parser)); - output.Flush(); - stream.Position = 0; - - var input = new CodedInputStream(stream); - input.AssertNextTag(tag); - Assert.AreEqual(message1, input.ReadMessage(ForeignMessage.Parser)); - input.AssertNextTag(tag); - Assert.AreEqual(message2, input.ReadMessage(ForeignMessage.Parser)); - Assert.IsTrue(input.IsAtEnd); - } - - [Test] - public void CalculateSize_VariableSizeNonPacked() - { - var list = new RepeatedField { 1, 500, 1 }; - var tag = WireFormat.MakeTag(1, WireFormat.WireType.Varint); - // 2 bytes for the first entry, 3 bytes for the second, 2 bytes for the third - Assert.AreEqual(7, list.CalculateSize(FieldCodec.ForInt32(tag))); - } - - [Test] - public void CalculateSize_FixedSizeNonPacked() - { - var list = new RepeatedField { 1, 500, 1 }; - var tag = WireFormat.MakeTag(1, WireFormat.WireType.Fixed32); - // 5 bytes for the each entry - Assert.AreEqual(15, list.CalculateSize(FieldCodec.ForSFixed32(tag))); - } - - [Test] - public void CalculateSize_VariableSizePacked() - { - var list = new RepeatedField { 1, 500, 1}; - var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); - // 1 byte for the tag, 1 byte for the length, - // 1 byte for the first entry, 2 bytes for the second, 1 byte for the third - Assert.AreEqual(6, list.CalculateSize(FieldCodec.ForInt32(tag))); - } - - [Test] - public void CalculateSize_FixedSizePacked() - { - var list = new RepeatedField { 1, 500, 1 }; - var tag = WireFormat.MakeTag(1, WireFormat.WireType.LengthDelimited); - // 1 byte for the tag, 1 byte for the length, 4 bytes per entry - Assert.AreEqual(14, list.CalculateSize(FieldCodec.ForSFixed32(tag))); - } - - [Test] - public void TestNegativeEnumArray() - { - int arraySize = 1 + 1 + (11 * 5); - int msgSize = arraySize; - byte[] bytes = new byte[msgSize]; - CodedOutputStream output = new CodedOutputStream(bytes); - uint tag = WireFormat.MakeTag(8, WireFormat.WireType.Varint); - for (int i = 0; i >= -5; i--) - { - output.WriteTag(tag); - output.WriteEnum(i); - } - - Assert.AreEqual(0, output.SpaceLeft); - - CodedInputStream input = new CodedInputStream(bytes); - tag = input.ReadTag(); - - RepeatedField values = new RepeatedField(); - values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); - - Assert.AreEqual(6, values.Count); - Assert.AreEqual(SampleEnum.None, values[0]); - Assert.AreEqual(((SampleEnum)(-1)), values[1]); - Assert.AreEqual(SampleEnum.NegativeValue, values[2]); - Assert.AreEqual(((SampleEnum)(-3)), values[3]); - Assert.AreEqual(((SampleEnum)(-4)), values[4]); - Assert.AreEqual(((SampleEnum)(-5)), values[5]); - } - - - [Test] - public void TestNegativeEnumPackedArray() - { - int arraySize = 1 + (10 * 5); - int msgSize = 1 + 1 + arraySize; - byte[] bytes = new byte[msgSize]; - CodedOutputStream output = new CodedOutputStream(bytes); - // Length-delimited to show we want the packed representation - uint tag = WireFormat.MakeTag(8, WireFormat.WireType.LengthDelimited); - output.WriteTag(tag); - int size = 0; - for (int i = 0; i >= -5; i--) - { - size += CodedOutputStream.ComputeEnumSize(i); - } - output.WriteRawVarint32((uint)size); - for (int i = 0; i >= -5; i--) - { - output.WriteEnum(i); - } - Assert.AreEqual(0, output.SpaceLeft); - - CodedInputStream input = new CodedInputStream(bytes); - tag = input.ReadTag(); - - RepeatedField values = new RepeatedField(); - values.AddEntriesFrom(input, FieldCodec.ForEnum(tag, x => (int)x, x => (SampleEnum)x)); - - Assert.AreEqual(6, values.Count); - Assert.AreEqual(SampleEnum.None, values[0]); - Assert.AreEqual(((SampleEnum)(-1)), values[1]); - Assert.AreEqual(SampleEnum.NegativeValue, values[2]); - Assert.AreEqual(((SampleEnum)(-3)), values[3]); - Assert.AreEqual(((SampleEnum)(-4)), values[4]); - Assert.AreEqual(((SampleEnum)(-5)), values[5]); - } - - // Fairly perfunctory tests for the non-generic IList implementation - [Test] - public void IList_Indexer() - { - var field = new RepeatedField { "first", "second" }; - IList list = field; - Assert.AreEqual("first", list[0]); - list[1] = "changed"; - Assert.AreEqual("changed", field[1]); - } - - [Test] - public void IList_Contains() - { - IList list = new RepeatedField { "first", "second" }; - Assert.IsTrue(list.Contains("second")); - Assert.IsFalse(list.Contains("third")); - Assert.IsFalse(list.Contains(new object())); - } - - [Test] - public void IList_Add() - { - IList list = new RepeatedField { "first", "second" }; - list.Add("third"); - CollectionAssert.AreEqual(new[] { "first", "second", "third" }, list); - } - - [Test] - public void IList_Remove() - { - IList list = new RepeatedField { "first", "second" }; - list.Remove("third"); // No-op, no exception - list.Remove(new object()); // No-op, no exception - list.Remove("first"); - CollectionAssert.AreEqual(new[] { "second" }, list); - } - - [Test] - public void IList_IsFixedSize() - { - var field = new RepeatedField { "first", "second" }; - IList list = field; - Assert.IsFalse(list.IsFixedSize); - } - - [Test] - public void IList_IndexOf() - { - IList list = new RepeatedField { "first", "second" }; - Assert.AreEqual(1, list.IndexOf("second")); - Assert.AreEqual(-1, list.IndexOf("third")); - Assert.AreEqual(-1, list.IndexOf(new object())); - } - - [Test] - public void IList_SyncRoot() - { - IList list = new RepeatedField { "first", "second" }; - Assert.AreSame(list, list.SyncRoot); - } - - [Test] - public void IList_CopyTo() - { - IList list = new RepeatedField { "first", "second" }; - string[] stringArray = new string[4]; - list.CopyTo(stringArray, 1); - CollectionAssert.AreEqual(new[] { null, "first", "second", null }, stringArray); - - object[] objectArray = new object[4]; - list.CopyTo(objectArray, 1); - CollectionAssert.AreEqual(new[] { null, "first", "second", null }, objectArray); - - Assert.Throws(() => list.CopyTo(new StringBuilder[4], 1)); - Assert.Throws(() => list.CopyTo(new int[4], 1)); - } - - [Test] - public void IList_IsSynchronized() - { - IList list = new RepeatedField { "first", "second" }; - Assert.IsFalse(list.IsSynchronized); - } - - [Test] - public void IList_Insert() - { - IList list = new RepeatedField { "first", "second" }; - list.Insert(1, "middle"); - CollectionAssert.AreEqual(new[] { "first", "middle", "second" }, list); - } - - [Test] - public void ToString_Integers() - { - var list = new RepeatedField { 5, 10, 20 }; - var text = list.ToString(); - Assert.AreEqual("[ 5, 10, 20 ]", text); - } - - [Test] - public void ToString_Strings() - { - var list = new RepeatedField { "x", "y", "z" }; - var text = list.ToString(); - Assert.AreEqual("[ \"x\", \"y\", \"z\" ]", text); - } - - [Test] - public void ToString_Messages() - { - var list = new RepeatedField { new TestAllTypes { SingleDouble = 1.5 }, new TestAllTypes { SingleInt32 = 10 } }; - var text = list.ToString(); - Assert.AreEqual("[ { \"singleDouble\": 1.5 }, { \"singleInt32\": 10 } ]", text); - } - - [Test] - public void ToString_Empty() - { - var list = new RepeatedField { }; - var text = list.ToString(); - Assert.AreEqual("[ ]", text); - } - - [Test] - public void ToString_InvalidElementType() - { - var list = new RepeatedField { 15m }; - Assert.Throws(() => list.ToString()); - } - - [Test] - public void ToString_Timestamp() - { - var list = new RepeatedField { Timestamp.FromDateTime(new DateTime(2015, 10, 1, 12, 34, 56, DateTimeKind.Utc)) }; - var text = list.ToString(); - Assert.AreEqual("[ \"2015-10-01T12:34:56Z\" ]", text); - } - - [Test] - public void ToString_Struct() - { - var message = new Struct { Fields = { { "foo", new Value { NumberValue = 20 } } } }; - var list = new RepeatedField { message }; - var text = list.ToString(); - Assert.AreEqual(text, "[ { \"foo\": 20 } ]", message.ToString()); - } - - [Test] - public void NaNValuesComparedBitwise() - { - var list1 = new RepeatedField { SampleNaNs.Regular, SampleNaNs.SignallingFlipped }; - var list2 = new RepeatedField { SampleNaNs.Regular, SampleNaNs.PayloadFlipped }; - var list3 = new RepeatedField { SampleNaNs.Regular, SampleNaNs.SignallingFlipped }; - - EqualityTester.AssertInequality(list1, list2); - EqualityTester.AssertEquality(list1, list3); - Assert.True(list1.Contains(SampleNaNs.SignallingFlipped)); - Assert.False(list2.Contains(SampleNaNs.SignallingFlipped)); - } - } -} -- cgit v1.2.3