csharptest | e495477 | 2010-11-09 14:47:27 -0600 | [diff] [blame] | 1 | #region Copyright notice and license |
| 2 | // Protocol Buffers - Google's data interchange format |
| 3 | // Copyright 2008 Google Inc. All rights reserved. |
| 4 | // http://github.com/jskeet/dotnet-protobufs/ |
| 5 | // Original C++/Java/Python code: |
| 6 | // http://code.google.com/p/protobuf/ |
| 7 | // |
| 8 | // Redistribution and use in source and binary forms, with or without |
| 9 | // modification, are permitted provided that the following conditions are |
| 10 | // met: |
| 11 | // |
| 12 | // * Redistributions of source code must retain the above copyright |
| 13 | // notice, this list of conditions and the following disclaimer. |
| 14 | // * Redistributions in binary form must reproduce the above |
| 15 | // copyright notice, this list of conditions and the following disclaimer |
| 16 | // in the documentation and/or other materials provided with the |
| 17 | // distribution. |
| 18 | // * Neither the name of Google Inc. nor the names of its |
| 19 | // contributors may be used to endorse or promote products derived from |
| 20 | // this software without specific prior written permission. |
| 21 | // |
| 22 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 23 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 24 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 25 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 26 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 27 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 28 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 29 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 30 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 31 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 32 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 33 | #endregion |
| 34 | |
| 35 | using System; |
| 36 | using System.Collections.Generic; |
| 37 | using System.IO; |
| 38 | using Google.ProtocolBuffers; |
| 39 | using Google.ProtocolBuffers.TestProtos; |
| 40 | using NUnit.Framework; |
| 41 | |
| 42 | namespace Google.ProtocolBuffers { |
| 43 | [TestFixture] |
| 44 | public class AbstractMessageLiteTest { |
| 45 | |
| 46 | [Test] |
| 47 | public void TestMessageLiteToByteString() { |
| 48 | TestRequiredLite msg = TestRequiredLite.CreateBuilder() |
| 49 | .SetD(42) |
| 50 | .SetEn(ExtraEnum.EXLITE_BAZ) |
| 51 | .Build(); |
| 52 | |
| 53 | ByteString b = msg.ToByteString(); |
| 54 | Assert.AreEqual(4, b.Length); |
| 55 | Assert.AreEqual(TestRequiredLite.DFieldNumber << 3, b[0]); |
| 56 | Assert.AreEqual(42, b[1]); |
| 57 | Assert.AreEqual(TestRequiredLite.EnFieldNumber << 3, b[2]); |
| 58 | Assert.AreEqual((int)ExtraEnum.EXLITE_BAZ, b[3]); |
| 59 | } |
| 60 | |
| 61 | [Test] |
| 62 | public void TestMessageLiteToByteArray() { |
| 63 | TestRequiredLite msg = TestRequiredLite.CreateBuilder() |
| 64 | .SetD(42) |
| 65 | .SetEn(ExtraEnum.EXLITE_BAZ) |
| 66 | .Build(); |
| 67 | |
| 68 | ByteString b = msg.ToByteString(); |
| 69 | ByteString copy = ByteString.CopyFrom(msg.ToByteArray()); |
| 70 | Assert.AreEqual(b, copy); |
| 71 | } |
| 72 | |
| 73 | [Test] |
| 74 | public void TestMessageLiteWriteTo() { |
| 75 | TestRequiredLite msg = TestRequiredLite.CreateBuilder() |
| 76 | .SetD(42) |
| 77 | .SetEn(ExtraEnum.EXLITE_BAZ) |
| 78 | .Build(); |
| 79 | |
| 80 | MemoryStream ms = new MemoryStream(); |
| 81 | msg.WriteTo(ms); |
| 82 | Assert.AreEqual(msg.ToByteArray(), ms.ToArray()); |
| 83 | } |
| 84 | |
| 85 | [Test] |
| 86 | public void TestMessageLiteWriteDelimitedTo() { |
| 87 | TestRequiredLite msg = TestRequiredLite.CreateBuilder() |
| 88 | .SetD(42) |
| 89 | .SetEn(ExtraEnum.EXLITE_BAZ) |
| 90 | .Build(); |
| 91 | |
| 92 | MemoryStream ms = new MemoryStream(); |
| 93 | msg.WriteDelimitedTo(ms); |
| 94 | byte[] buffer = ms.ToArray(); |
| 95 | |
| 96 | Assert.AreEqual(5, buffer.Length); |
| 97 | Assert.AreEqual(4, buffer[0]); |
| 98 | byte[] msgBytes = new byte[4]; |
| 99 | Array.Copy(buffer, 1, msgBytes, 0, 4); |
| 100 | Assert.AreEqual(msg.ToByteArray(), msgBytes); |
| 101 | } |
| 102 | |
| 103 | [Test] |
| 104 | public void TestIMessageLiteWeakCreateBuilderForType() { |
| 105 | IMessageLite msg = TestRequiredLite.DefaultInstance; |
| 106 | Assert.AreEqual(typeof(TestRequiredLite.Builder), msg.WeakCreateBuilderForType().GetType()); |
| 107 | } |
| 108 | |
| 109 | [Test] |
| 110 | public void TestMessageLiteWeakToBuilder() { |
| 111 | IMessageLite msg = TestRequiredLite.CreateBuilder() |
| 112 | .SetD(42) |
| 113 | .SetEn(ExtraEnum.EXLITE_BAZ) |
| 114 | .Build(); |
| 115 | |
| 116 | IMessageLite copy = msg.WeakToBuilder().WeakBuild(); |
| 117 | Assert.AreEqual(msg.ToByteArray(), copy.ToByteArray()); |
| 118 | } |
| 119 | |
| 120 | [Test] |
| 121 | public void TestMessageLiteWeakDefaultInstanceForType() { |
| 122 | IMessageLite msg = TestRequiredLite.DefaultInstance; |
| 123 | Assert.IsTrue(Object.ReferenceEquals(TestRequiredLite.DefaultInstance, msg.WeakDefaultInstanceForType)); |
| 124 | } |
| 125 | } |
| 126 | } |