blob: 5ef9636d863c5fe389578e47f4b27ab0003b95f4 [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;
38using System.Collections.Generic;
39using System.IO;
40using Google.ProtocolBuffers;
41using Google.ProtocolBuffers.TestProtos;
42using NUnit.Framework;
43
csharptest71f662c2011-05-20 15:15:34 -050044namespace Google.ProtocolBuffers
45{
46 [TestFixture]
47 public class AbstractMessageLiteTest
48 {
49 [Test]
50 public void TestMessageLiteToByteString()
51 {
52 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
53 .SetD(42)
54 .SetEn(ExtraEnum.EXLITE_BAZ)
55 .Build();
csharptestd965c662011-05-20 13:57:07 -050056
csharptest71f662c2011-05-20 15:15:34 -050057 ByteString b = msg.ToByteString();
58 Assert.AreEqual(4, b.Length);
59 Assert.AreEqual(TestRequiredLite.DFieldNumber << 3, b[0]);
60 Assert.AreEqual(42, b[1]);
61 Assert.AreEqual(TestRequiredLite.EnFieldNumber << 3, b[2]);
62 Assert.AreEqual((int) ExtraEnum.EXLITE_BAZ, b[3]);
63 }
64
65 [Test]
66 public void TestMessageLiteToByteArray()
67 {
68 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
69 .SetD(42)
70 .SetEn(ExtraEnum.EXLITE_BAZ)
71 .Build();
72
73 ByteString b = msg.ToByteString();
74 ByteString copy = ByteString.CopyFrom(msg.ToByteArray());
75 Assert.AreEqual(b, copy);
76 }
77
78 [Test]
79 public void TestMessageLiteWriteTo()
80 {
81 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
82 .SetD(42)
83 .SetEn(ExtraEnum.EXLITE_BAZ)
84 .Build();
85
86 MemoryStream ms = new MemoryStream();
87 msg.WriteTo(ms);
88 Assert.AreEqual(msg.ToByteArray(), ms.ToArray());
89 }
90
91 [Test]
92 public void TestMessageLiteWriteDelimitedTo()
93 {
94 TestRequiredLite msg = TestRequiredLite.CreateBuilder()
95 .SetD(42)
96 .SetEn(ExtraEnum.EXLITE_BAZ)
97 .Build();
98
99 MemoryStream ms = new MemoryStream();
100 msg.WriteDelimitedTo(ms);
101 byte[] buffer = ms.ToArray();
102
103 Assert.AreEqual(5, buffer.Length);
104 Assert.AreEqual(4, buffer[0]);
105 byte[] msgBytes = new byte[4];
106 Array.Copy(buffer, 1, msgBytes, 0, 4);
107 Assert.AreEqual(msg.ToByteArray(), msgBytes);
108 }
109
110 [Test]
111 public void TestIMessageLiteWeakCreateBuilderForType()
112 {
113 IMessageLite msg = TestRequiredLite.DefaultInstance;
114 Assert.AreEqual(typeof (TestRequiredLite.Builder), msg.WeakCreateBuilderForType().GetType());
115 }
116
117 [Test]
118 public void TestMessageLiteWeakToBuilder()
119 {
120 IMessageLite msg = TestRequiredLite.CreateBuilder()
121 .SetD(42)
122 .SetEn(ExtraEnum.EXLITE_BAZ)
123 .Build();
124
125 IMessageLite copy = msg.WeakToBuilder().WeakBuild();
126 Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray());
127 }
128
129 [Test]
130 public void TestMessageLiteWeakDefaultInstanceForType()
131 {
132 IMessageLite msg = TestRequiredLite.DefaultInstance;
133 Assert.IsTrue(Object.ReferenceEquals(TestRequiredLite.DefaultInstance, msg.WeakDefaultInstanceForType));
134 }
csharptestd965c662011-05-20 13:57:07 -0500135 }
csharptest71f662c2011-05-20 15:15:34 -0500136}