1 #region Copyright notice and license 2 // Protocol Buffers - Google's data interchange format 3 // Copyright 2015 Google Inc. All rights reserved. 4 // https://developers.google.com/protocol-buffers/ 5 // 6 // Redistribution and use in source and binary forms, with or without 7 // modification, are permitted provided that the following conditions are 8 // met: 9 // 10 // * Redistributions of source code must retain the above copyright 11 // notice, this list of conditions and the following disclaimer. 12 // * Redistributions in binary form must reproduce the above 13 // copyright notice, this list of conditions and the following disclaimer 14 // in the documentation and/or other materials provided with the 15 // distribution. 16 // * Neither the name of Google Inc. nor the names of its 17 // contributors may be used to endorse or promote products derived from 18 // this software without specific prior written permission. 19 // 20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 #endregion 32 33 using System.Collections.Generic; 34 using System.IO; 35 using System.Reflection; 36 using Google.Protobuf.TestProtos; 37 using NUnit.Framework; 38 39 namespace Google.Protobuf 40 { 41 public class FieldCodecTest 42 { 43 #pragma warning disable 0414 // Used by tests via reflection - do not remove! 44 private static readonly List<ICodecTestData> Codecs = new List<ICodecTestData> 45 { 46 new FieldCodecTestData<bool>(FieldCodec.ForBool(100), true, "FixedBool"), 47 new FieldCodecTestData<string>(FieldCodec.ForString(100), "sample", "String"), 48 new FieldCodecTestData<ByteString>(FieldCodec.ForBytes(100), ByteString.CopyFrom(1, 2, 3), "Bytes"), 49 new FieldCodecTestData<int>(FieldCodec.ForInt32(100), -1000, "Int32"), 50 new FieldCodecTestData<int>(FieldCodec.ForSInt32(100), -1000, "SInt32"), 51 new FieldCodecTestData<int>(FieldCodec.ForSFixed32(100), -1000, "SFixed32"), 52 new FieldCodecTestData<uint>(FieldCodec.ForUInt32(100), 1234, "UInt32"), 53 new FieldCodecTestData<uint>(FieldCodec.ForFixed32(100), 1234, "Fixed32"), 54 new FieldCodecTestData<long>(FieldCodec.ForInt64(100), -1000, "Int64"), 55 new FieldCodecTestData<long>(FieldCodec.ForSInt64(100), -1000, "SInt64"), 56 new FieldCodecTestData<long>(FieldCodec.ForSFixed64(100), -1000, "SFixed64"), 57 new FieldCodecTestData<ulong>(FieldCodec.ForUInt64(100), 1234, "UInt64"), 58 new FieldCodecTestData<ulong>(FieldCodec.ForFixed64(100), 1234, "Fixed64"), 59 new FieldCodecTestData<float>(FieldCodec.ForFloat(100), 1234.5f, "FixedFloat"), 60 new FieldCodecTestData<double>(FieldCodec.ForDouble(100), 1234567890.5d, "FixedDouble"), 61 new FieldCodecTestData<ForeignEnum>( 62 FieldCodec.ForEnum(100, t => (int) t, t => (ForeignEnum) t), ForeignEnum.ForeignBaz, "Enum"), 63 new FieldCodecTestData<ForeignMessage>( 64 FieldCodec.ForMessage(100, ForeignMessage.Parser), new ForeignMessage { C = 10 }, "Message"), 65 }; 66 #pragma warning restore 0414 67 68 [Test, TestCaseSource("Codecs")] RoundTripWithTag(ICodecTestData codec)69 public void RoundTripWithTag(ICodecTestData codec) 70 { 71 codec.TestRoundTripWithTag(); 72 } 73 74 [Test, TestCaseSource("Codecs")] RoundTripRaw(ICodecTestData codec)75 public void RoundTripRaw(ICodecTestData codec) 76 { 77 codec.TestRoundTripRaw(); 78 } 79 80 [Test, TestCaseSource("Codecs")] CalculateSize(ICodecTestData codec)81 public void CalculateSize(ICodecTestData codec) 82 { 83 codec.TestCalculateSizeWithTag(); 84 } 85 86 [Test, TestCaseSource("Codecs")] DefaultValue(ICodecTestData codec)87 public void DefaultValue(ICodecTestData codec) 88 { 89 codec.TestDefaultValue(); 90 } 91 92 [Test, TestCaseSource("Codecs")] FixedSize(ICodecTestData codec)93 public void FixedSize(ICodecTestData codec) 94 { 95 codec.TestFixedSize(); 96 } 97 98 // This is ugly, but it means we can have a non-generic interface. 99 // It feels like NUnit should support this better, but I don't know 100 // of any better ways right now. 101 public interface ICodecTestData 102 { TestRoundTripRaw()103 void TestRoundTripRaw(); TestRoundTripWithTag()104 void TestRoundTripWithTag(); TestCalculateSizeWithTag()105 void TestCalculateSizeWithTag(); TestDefaultValue()106 void TestDefaultValue(); TestFixedSize()107 void TestFixedSize(); 108 } 109 110 public class FieldCodecTestData<T> : ICodecTestData 111 { 112 private readonly FieldCodec<T> codec; 113 private readonly T sampleValue; 114 private readonly string name; 115 FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name)116 public FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name) 117 { 118 this.codec = codec; 119 this.sampleValue = sampleValue; 120 this.name = name; 121 } 122 TestRoundTripRaw()123 public void TestRoundTripRaw() 124 { 125 var stream = new MemoryStream(); 126 var codedOutput = new CodedOutputStream(stream); 127 codec.ValueWriter(codedOutput, sampleValue); 128 codedOutput.Flush(); 129 stream.Position = 0; 130 var codedInput = new CodedInputStream(stream); 131 Assert.AreEqual(sampleValue, codec.ValueReader(codedInput)); 132 Assert.IsTrue(codedInput.IsAtEnd); 133 } 134 TestRoundTripWithTag()135 public void TestRoundTripWithTag() 136 { 137 var stream = new MemoryStream(); 138 var codedOutput = new CodedOutputStream(stream); 139 codec.WriteTagAndValue(codedOutput, sampleValue); 140 codedOutput.Flush(); 141 stream.Position = 0; 142 var codedInput = new CodedInputStream(stream); 143 codedInput.AssertNextTag(codec.Tag); 144 Assert.AreEqual(sampleValue, codec.Read(codedInput)); 145 Assert.IsTrue(codedInput.IsAtEnd); 146 } 147 TestCalculateSizeWithTag()148 public void TestCalculateSizeWithTag() 149 { 150 var stream = new MemoryStream(); 151 var codedOutput = new CodedOutputStream(stream); 152 codec.WriteTagAndValue(codedOutput, sampleValue); 153 codedOutput.Flush(); 154 Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue)); 155 } 156 TestDefaultValue()157 public void TestDefaultValue() 158 { 159 // WriteTagAndValue ignores default values 160 var stream = new MemoryStream(); 161 var codedOutput = new CodedOutputStream(stream); 162 codec.WriteTagAndValue(codedOutput, codec.DefaultValue); 163 codedOutput.Flush(); 164 Assert.AreEqual(0, stream.Position); 165 Assert.AreEqual(0, codec.CalculateSizeWithTag(codec.DefaultValue)); 166 if (typeof(T).GetTypeInfo().IsValueType) 167 { 168 Assert.AreEqual(default(T), codec.DefaultValue); 169 } 170 171 // The plain ValueWriter/ValueReader delegates don't. 172 if (codec.DefaultValue != null) // This part isn't appropriate for message types. 173 { 174 codedOutput = new CodedOutputStream(stream); 175 codec.ValueWriter(codedOutput, codec.DefaultValue); 176 codedOutput.Flush(); 177 Assert.AreNotEqual(0, stream.Position); 178 Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); 179 stream.Position = 0; 180 var codedInput = new CodedInputStream(stream); 181 Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput)); 182 } 183 } 184 TestFixedSize()185 public void TestFixedSize() 186 { 187 Assert.AreEqual(name.Contains("Fixed"), codec.FixedSize != 0); 188 } 189 ToString()190 public override string ToString() 191 { 192 return name; 193 } 194 } 195 } 196 } 197