blob: e2737914b05deaf37e0b470902a07431c5c7e4da [file] [log] [blame]
csharptest71f662c2011-05-20 15:15:34 -05001#region Copyright notice and license
2
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.
34
35#endregion
36
37using System.IO;
38using NUnit.Framework;
39using NestedMessage = Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage;
40
41namespace Google.ProtocolBuffers
42{
43 [TestFixture]
44 public class MessageStreamWriterTest
45 {
46 internal static readonly byte[] ThreeMessageData = new byte[]
47 {
48 (1 << 3) | 2, 2,
49 // Field 1, 2 bytes long (first message)
50 (1 << 3) | 0, 5, // Field 1, value 5
51 (1 << 3) | 2, 3,
52 // Field 1, 3 bytes long (second message)
53 (1 << 3) | 0, (1500 & 0x7f) | 0x80, 1500 >> 7,
54 // Field 1, value 1500
55 (1 << 3) | 2, 0, // Field 1, no data (third message)
56 };
57
58 [Test]
59 public void ThreeMessages()
60 {
61 NestedMessage message1 = new NestedMessage.Builder {Bb = 5}.Build();
62 NestedMessage message2 = new NestedMessage.Builder {Bb = 1500}.Build();
63 NestedMessage message3 = new NestedMessage.Builder().Build();
64
65 byte[] data;
66 using (MemoryStream stream = new MemoryStream())
67 {
68 MessageStreamWriter<NestedMessage> writer = new MessageStreamWriter<NestedMessage>(stream);
69 writer.Write(message1);
70 writer.Write(message2);
71 writer.Write(message3);
72 writer.Flush();
73 data = stream.ToArray();
74 }
75
76 TestUtil.AssertEqualBytes(ThreeMessageData, data);
77 }
78 }
79}