blob: f6a4e94b32e3ab044cc553df73ac79b44a5b0424 [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
csharptestd965c662011-05-20 13:57:07 -05003// Protocol Buffers - Google's data interchange format
4// Copyright 2008 Google Inc. All rights reserved.
5// http://github.com/jskeet/dotnet-protobufs/
6// Original C++/Java/Python code:
7// http://code.google.com/p/protobuf/
8//
9// Redistribution and use in source and binary forms, with or without
10// modification, are permitted provided that the following conditions are
11// met:
12//
13// * Redistributions of source code must retain the above copyright
14// notice, this list of conditions and the following disclaimer.
15// * Redistributions in binary form must reproduce the above
16// copyright notice, this list of conditions and the following disclaimer
17// in the documentation and/or other materials provided with the
18// distribution.
19// * Neither the name of Google Inc. nor the names of its
20// contributors may be used to endorse or promote products derived from
21// this software without specific prior written permission.
22//
23// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
csharptest71f662c2011-05-20 15:15:34 -050034
csharptestd965c662011-05-20 13:57:07 -050035#endregion
36
37using System;
csharptestd965c662011-05-20 13:57:07 -050038using System.IO;
csharptestd965c662011-05-20 13:57:07 -050039using Google.ProtocolBuffers.TestProtos;
Jon Skeetc5647502015-04-30 11:05:36 +010040using Xunit;
csharptestd965c662011-05-20 13:57:07 -050041
csharptest71f662c2011-05-20 15:15:34 -050042namespace Google.ProtocolBuffers
43{
csharptest71f662c2011-05-20 15:15:34 -050044 public class AbstractMessageLiteTest
45 {
Jon Skeetc5647502015-04-30 11:05:36 +010046 [Fact]
csharptest71f662c2011-05-20 15:15:34 -050047 public void TestMessageLiteToByteString()
48 {
49 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
50 .SetD(42)
51 .SetEn(ExtraEnum.EXLITE_BAZ)
52 .Build();
csharptestd965c662011-05-20 13:57:07 -050053
csharptest71f662c2011-05-20 15:15:34 -050054 ByteString b = msg.ToByteString();
Jon Skeetc5647502015-04-30 11:05:36 +010055 Assert.Equal(4, b.Length);
56 Assert.Equal(TestRequiredLite.DFieldNumber << 3, b[0]);
57 Assert.Equal(42, b[1]);
58 Assert.Equal(TestRequiredLite.EnFieldNumber << 3, b[2]);
59 Assert.Equal((int) ExtraEnum.EXLITE_BAZ, b[3]);
csharptest71f662c2011-05-20 15:15:34 -050060 }
61
Jon Skeetc5647502015-04-30 11:05:36 +010062 [Fact]
csharptest71f662c2011-05-20 15:15:34 -050063 public void TestMessageLiteToByteArray()
64 {
65 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
66 .SetD(42)
67 .SetEn(ExtraEnum.EXLITE_BAZ)
68 .Build();
69
70 ByteString b = msg.ToByteString();
71 ByteString copy = ByteString.CopyFrom(msg.ToByteArray());
Jon Skeetc5647502015-04-30 11:05:36 +010072 Assert.Equal(b, copy);
csharptest71f662c2011-05-20 15:15:34 -050073 }
74
Jon Skeetc5647502015-04-30 11:05:36 +010075 [Fact]
csharptest71f662c2011-05-20 15:15:34 -050076 public void TestMessageLiteWriteTo()
77 {
78 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
79 .SetD(42)
80 .SetEn(ExtraEnum.EXLITE_BAZ)
81 .Build();
82
83 MemoryStream ms = new MemoryStream();
84 msg.WriteTo(ms);
Jon Skeetc5647502015-04-30 11:05:36 +010085 Assert.Equal(msg.ToByteArray(), ms.ToArray());
csharptest71f662c2011-05-20 15:15:34 -050086 }
87
Jon Skeetc5647502015-04-30 11:05:36 +010088 [Fact]
csharptest71f662c2011-05-20 15:15:34 -050089 public void TestMessageLiteWriteDelimitedTo()
90 {
91 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
92 .SetD(42)
93 .SetEn(ExtraEnum.EXLITE_BAZ)
94 .Build();
95
96 MemoryStream ms = new MemoryStream();
97 msg.WriteDelimitedTo(ms);
98 byte[] buffer = ms.ToArray();
99
Jon Skeetc5647502015-04-30 11:05:36 +0100100 Assert.Equal(5, buffer.Length);
101 Assert.Equal(4, buffer[0]);
csharptest71f662c2011-05-20 15:15:34 -0500102 byte[] msgBytes = new byte[4];
103 Array.Copy(buffer, 1, msgBytes, 0, 4);
Jon Skeetc5647502015-04-30 11:05:36 +0100104 Assert.Equal(msg.ToByteArray(), msgBytes);
csharptest71f662c2011-05-20 15:15:34 -0500105 }
106
Jon Skeetc5647502015-04-30 11:05:36 +0100107 [Fact]
csharptest71f662c2011-05-20 15:15:34 -0500108 public void TestIMessageLiteWeakCreateBuilderForType()
109 {
110 IMessageLite msg = TestRequiredLite.DefaultInstance;
Jon Skeetc5647502015-04-30 11:05:36 +0100111 Assert.Equal(typeof(TestRequiredLite.Builder), msg.WeakCreateBuilderForType().GetType());
csharptest71f662c2011-05-20 15:15:34 -0500112 }
113
Jon Skeetc5647502015-04-30 11:05:36 +0100114 [Fact]
csharptest71f662c2011-05-20 15:15:34 -0500115 public void TestMessageLiteWeakToBuilder()
116 {
117 IMessageLite msg = TestRequiredLite.CreateBuilder()
118 .SetD(42)
119 .SetEn(ExtraEnum.EXLITE_BAZ)
120 .Build();
121
122 IMessageLite copy = msg.WeakToBuilder().WeakBuild();
Jon Skeetc5647502015-04-30 11:05:36 +0100123 Assert.Equal(msg.ToByteArray(), copy.ToByteArray());
csharptest71f662c2011-05-20 15:15:34 -0500124 }
125
Jon Skeetc5647502015-04-30 11:05:36 +0100126 [Fact]
csharptest71f662c2011-05-20 15:15:34 -0500127 public void TestMessageLiteWeakDefaultInstanceForType()
128 {
129 IMessageLite msg = TestRequiredLite.DefaultInstance;
Jon Skeetc5647502015-04-30 11:05:36 +0100130 Assert.True(Object.ReferenceEquals(TestRequiredLite.DefaultInstance, msg.WeakDefaultInstanceForType));
csharptest71f662c2011-05-20 15:15:34 -0500131 }
csharptestd965c662011-05-20 13:57:07 -0500132 }
csharptest71f662c2011-05-20 15:15:34 -0500133}