blob: 269d6f0bb92377dcc5748277e9d5fc3660076473 [file] [log] [blame]
Jon Skeetad748532009-06-25 16:55:58 +01001// Protocol Buffers - Google's data interchange format
2// Copyright 2008 Google Inc. All rights reserved.
3// http://github.com/jskeet/dotnet-protobufs/
4// Original C++/Java/Python code:
5// http://code.google.com/p/protobuf/
6//
7// Redistribution and use in source and binary forms, with or without
8// modification, are permitted provided that the following conditions are
9// met:
10//
11// * Redistributions of source code must retain the above copyright
12// notice, this list of conditions and the following disclaimer.
13// * Redistributions in binary form must reproduce the above
14// copyright notice, this list of conditions and the following disclaimer
15// in the documentation and/or other materials provided with the
16// distribution.
17// * Neither the name of Google Inc. nor the names of its
18// contributors may be used to endorse or promote products derived from
19// this software without specific prior written permission.
20//
21// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Jon Skeet68036862008-10-22 13:30:34 +010032using System.Collections.Generic;
33using System.IO;
Jon Skeetad748532009-06-25 16:55:58 +010034using Google.ProtocolBuffers.TestProtos;
Jon Skeet68036862008-10-22 13:30:34 +010035using NUnit.Framework;
36using NestedMessage = Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage;
37
38namespace Google.ProtocolBuffers {
39 [TestFixture]
40 public class MessageStreamIteratorTest {
41
42 [Test]
43 public void ThreeMessagesInMemory() {
44 MemoryStream stream = new MemoryStream(MessageStreamWriterTest.ThreeMessageData);
45 IEnumerable<NestedMessage> iterator = MessageStreamIterator<NestedMessage>.FromStreamProvider(() => stream);
46 List<NestedMessage> messages = new List<NestedMessage>(iterator);
47
48 Assert.AreEqual(3, messages.Count);
49 Assert.AreEqual(5, messages[0].Bb);
50 Assert.AreEqual(1500, messages[1].Bb);
51 Assert.IsFalse(messages[2].HasBb);
52 }
Jon Skeet2178b932009-06-25 07:52:07 +010053
54 [Test]
55 public void ManyMessagesShouldNotTriggerSizeAlert() {
56 int messageSize = TestUtil.GetAllSet().SerializedSize;
57 // Enough messages to trigger the alert unless we've reset the size
58 // Note that currently we need to make this big enough to copy two whole buffers,
59 // as otherwise when we refill the buffer the second type, the alert triggers instantly.
60 int correctCount = (CodedInputStream.BufferSize * 2) / messageSize + 1;
61 using (MemoryStream stream = new MemoryStream()) {
62 MessageStreamWriter<TestAllTypes> writer = new MessageStreamWriter<TestAllTypes>(stream);
63 for (int i = 0; i < correctCount; i++) {
64 writer.Write(TestUtil.GetAllSet());
65 }
66 writer.Flush();
67
68 stream.Position = 0;
69
70 int count = 0;
71 foreach (var message in MessageStreamIterator<TestAllTypes>.FromStreamProvider(() => stream)
72 .WithSizeLimit(CodedInputStream.BufferSize * 2)) {
73 count++;
74 TestUtil.AssertAllFieldsSet(message);
75 }
76 Assert.AreEqual(correctCount, count);
77 }
78 }
Jon Skeet68036862008-10-22 13:30:34 +010079 }
80}