csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 1 | #region Copyright notice and license
|
| 2 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 3 | // 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.
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 34 |
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 35 | #endregion
|
| 36 |
|
| 37 | using System;
|
| 38 | using System.Collections.Generic;
|
| 39 | using System.IO;
|
| 40 | using Google.ProtocolBuffers;
|
| 41 | using Google.ProtocolBuffers.TestProtos;
|
| 42 | using NUnit.Framework;
|
| 43 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 44 | namespace 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();
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 56 |
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 57 | 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 | }
|
csharptest | d965c66 | 2011-05-20 13:57:07 -0500 | [diff] [blame] | 135 | }
|
csharptest | 71f662c | 2011-05-20 15:15:34 -0500 | [diff] [blame] | 136 | } |