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 Google.Protobuf.TestProtos; 36 using NUnit.Framework; 37 38 namespace Google.Protobuf 39 { 40 public class FieldCodecTest 41 { 42 #pragma warning disable 0414 // Used by tests via reflection - do not remove! 43 private static readonly List<ICodecTestData> Codecs = new List<ICodecTestData> 44 { 45 new FieldCodecTestData<bool>(FieldCodec.ForBool(100), true, "Bool"), 46 new FieldCodecTestData<string>(FieldCodec.ForString(100), "sample", "String"), 47 new FieldCodecTestData<ByteString>(FieldCodec.ForBytes(100), ByteString.CopyFrom(1, 2, 3), "Bytes"), 48 new FieldCodecTestData<int>(FieldCodec.ForInt32(100), -1000, "Int32"), 49 new FieldCodecTestData<int>(FieldCodec.ForSInt32(100), -1000, "SInt32"), 50 new FieldCodecTestData<int>(FieldCodec.ForSFixed32(100), -1000, "SFixed32"), 51 new FieldCodecTestData<uint>(FieldCodec.ForUInt32(100), 1234, "UInt32"), 52 new FieldCodecTestData<uint>(FieldCodec.ForFixed32(100), 1234, "Fixed32"), 53 new FieldCodecTestData<long>(FieldCodec.ForInt64(100), -1000, "Int64"), 54 new FieldCodecTestData<long>(FieldCodec.ForSInt64(100), -1000, "SInt64"), 55 new FieldCodecTestData<long>(FieldCodec.ForSFixed64(100), -1000, "SFixed64"), 56 new FieldCodecTestData<ulong>(FieldCodec.ForUInt64(100), 1234, "UInt64"), 57 new FieldCodecTestData<ulong>(FieldCodec.ForFixed64(100), 1234, "Fixed64"), 58 new FieldCodecTestData<float>(FieldCodec.ForFloat(100), 1234.5f, "Float"), 59 new FieldCodecTestData<double>(FieldCodec.ForDouble(100), 1234567890.5d, "Double"), 60 new FieldCodecTestData<ForeignEnum>( 61 FieldCodec.ForEnum(100, t => (int) t, t => (ForeignEnum) t), ForeignEnum.ForeignBaz, "Enum"), 62 new FieldCodecTestData<ForeignMessage>( 63 FieldCodec.ForMessage(100, ForeignMessage.Parser), new ForeignMessage { C = 10 }, "Message"), 64 }; 65 #pragma warning restore 0414 66 67 [Test, TestCaseSource("Codecs")] RoundTripWithTag(ICodecTestData codec)68 public void RoundTripWithTag(ICodecTestData codec) 69 { 70 codec.TestRoundTripWithTag(); 71 } 72 73 [Test, TestCaseSource("Codecs")] RoundTripRaw(ICodecTestData codec)74 public void RoundTripRaw(ICodecTestData codec) 75 { 76 codec.TestRoundTripRaw(); 77 } 78 79 [Test, TestCaseSource("Codecs")] CalculateSize(ICodecTestData codec)80 public void CalculateSize(ICodecTestData codec) 81 { 82 codec.TestCalculateSizeWithTag(); 83 } 84 85 [Test, TestCaseSource("Codecs")] DefaultValue(ICodecTestData codec)86 public void DefaultValue(ICodecTestData codec) 87 { 88 codec.TestDefaultValue(); 89 } 90 91 [Test, TestCaseSource("Codecs")] FixedSize(ICodecTestData codec)92 public void FixedSize(ICodecTestData codec) 93 { 94 codec.TestFixedSize(); 95 } 96 97 // This is ugly, but it means we can have a non-generic interface. 98 // It feels like NUnit should support this better, but I don't know 99 // of any better ways right now. 100 public interface ICodecTestData 101 { TestRoundTripRaw()102 void TestRoundTripRaw(); TestRoundTripWithTag()103 void TestRoundTripWithTag(); TestCalculateSizeWithTag()104 void TestCalculateSizeWithTag(); TestDefaultValue()105 void TestDefaultValue(); TestFixedSize()106 void TestFixedSize(); 107 } 108 109 public class FieldCodecTestData<T> : ICodecTestData 110 { 111 private readonly FieldCodec<T> codec; 112 private readonly T sampleValue; 113 private readonly string name; 114 FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name)115 public FieldCodecTestData(FieldCodec<T> codec, T sampleValue, string name) 116 { 117 this.codec = codec; 118 this.sampleValue = sampleValue; 119 this.name = name; 120 } 121 TestRoundTripRaw()122 public void TestRoundTripRaw() 123 { 124 var stream = new MemoryStream(); 125 var codedOutput = new CodedOutputStream(stream); 126 codec.ValueWriter(codedOutput, sampleValue); 127 codedOutput.Flush(); 128 stream.Position = 0; 129 var codedInput = new CodedInputStream(stream); 130 Assert.AreEqual(sampleValue, codec.ValueReader(codedInput)); 131 Assert.IsTrue(codedInput.IsAtEnd); 132 } 133 TestRoundTripWithTag()134 public void TestRoundTripWithTag() 135 { 136 var stream = new MemoryStream(); 137 var codedOutput = new CodedOutputStream(stream); 138 codec.WriteTagAndValue(codedOutput, sampleValue); 139 codedOutput.Flush(); 140 stream.Position = 0; 141 var codedInput = new CodedInputStream(stream); 142 codedInput.AssertNextTag(codec.Tag); 143 Assert.AreEqual(sampleValue, codec.Read(codedInput)); 144 Assert.IsTrue(codedInput.IsAtEnd); 145 } 146 TestCalculateSizeWithTag()147 public void TestCalculateSizeWithTag() 148 { 149 var stream = new MemoryStream(); 150 var codedOutput = new CodedOutputStream(stream); 151 codec.WriteTagAndValue(codedOutput, sampleValue); 152 codedOutput.Flush(); 153 Assert.AreEqual(stream.Position, codec.CalculateSizeWithTag(sampleValue)); 154 } 155 TestDefaultValue()156 public void TestDefaultValue() 157 { 158 // WriteTagAndValue ignores default values 159 var stream = new MemoryStream(); 160 var codedOutput = new CodedOutputStream(stream); 161 codec.WriteTagAndValue(codedOutput, codec.DefaultValue); 162 codedOutput.Flush(); 163 Assert.AreEqual(0, stream.Position); 164 Assert.AreEqual(0, codec.CalculateSizeWithTag(codec.DefaultValue)); 165 if (typeof(T).IsValueType) 166 { 167 Assert.AreEqual(default(T), codec.DefaultValue); 168 } 169 170 // The plain ValueWriter/ValueReader delegates don't. 171 if (codec.DefaultValue != null) // This part isn't appropriate for message types. 172 { 173 codedOutput = new CodedOutputStream(stream); 174 codec.ValueWriter(codedOutput, codec.DefaultValue); 175 codedOutput.Flush(); 176 Assert.AreNotEqual(0, stream.Position); 177 Assert.AreEqual(stream.Position, codec.ValueSizeCalculator(codec.DefaultValue)); 178 stream.Position = 0; 179 var codedInput = new CodedInputStream(stream); 180 Assert.AreEqual(codec.DefaultValue, codec.ValueReader(codedInput)); 181 } 182 } 183 TestFixedSize()184 public void TestFixedSize() 185 { 186 Assert.AreEqual(name.Contains("Fixed"), codec.FixedSize != 0); 187 } 188 ToString()189 public override string ToString() 190 { 191 return name; 192 } 193 } 194 } 195 } 196