blob: 3e4eafe7b41f0fcb795c7e906a5c842b643f4d56 [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.Collections.Generic;
38using System.IO;
39using Google.ProtocolBuffers.TestProtos;
40using NUnit.Framework;
41using NestedMessage = Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage;
42
43namespace Google.ProtocolBuffers
44{
45 [TestFixture]
46 public class MessageStreamIteratorTest
47 {
48 [Test]
49 public void ThreeMessagesInMemory()
50 {
51 MemoryStream stream = new MemoryStream(MessageStreamWriterTest.ThreeMessageData);
52 IEnumerable<NestedMessage> iterator = MessageStreamIterator<NestedMessage>.FromStreamProvider(() => stream);
53 List<NestedMessage> messages = new List<NestedMessage>(iterator);
54
55 Assert.AreEqual(3, messages.Count);
56 Assert.AreEqual(5, messages[0].Bb);
57 Assert.AreEqual(1500, messages[1].Bb);
58 Assert.IsFalse(messages[2].HasBb);
59 }
60
61 [Test]
62 public void ManyMessagesShouldNotTriggerSizeAlert()
63 {
64 int messageSize = TestUtil.GetAllSet().SerializedSize;
65 // Enough messages to trigger the alert unless we've reset the size
66 // Note that currently we need to make this big enough to copy two whole buffers,
67 // as otherwise when we refill the buffer the second type, the alert triggers instantly.
68 int correctCount = (CodedInputStream.BufferSize*2)/messageSize + 1;
69 using (MemoryStream stream = new MemoryStream())
70 {
71 MessageStreamWriter<TestAllTypes> writer = new MessageStreamWriter<TestAllTypes>(stream);
72 for (int i = 0; i < correctCount; i++)
73 {
74 writer.Write(TestUtil.GetAllSet());
75 }
76 writer.Flush();
77
78 stream.Position = 0;
79
80 int count = 0;
81 foreach (var message in MessageStreamIterator<TestAllTypes>.FromStreamProvider(() => stream)
82 .WithSizeLimit(CodedInputStream.BufferSize*2))
83 {
84 count++;
85 TestUtil.AssertAllFieldsSet(message);
86 }
87 Assert.AreEqual(correctCount, count);
88 }
89 }
90 }
91}