blob: c7f31c0ff72e8912272a5f22eb2028e09513d5bc [file] [log] [blame]
Jon Skeet60c059b2008-10-23 21:17:56 +01001using System.Collections;
Jon Skeet68036862008-10-22 13:30:34 +01002using System.Collections.Generic;
3using System.IO;
4using NUnit.Framework;
5using NestedMessage = Google.ProtocolBuffers.TestProtos.TestAllTypes.Types.NestedMessage;
Jon Skeet2178b932009-06-25 07:52:07 +01006using Google.ProtocolBuffers.TestProtos;
Jon Skeet68036862008-10-22 13:30:34 +01007
8namespace Google.ProtocolBuffers {
9 [TestFixture]
10 public class MessageStreamIteratorTest {
11
12 [Test]
13 public void ThreeMessagesInMemory() {
14 MemoryStream stream = new MemoryStream(MessageStreamWriterTest.ThreeMessageData);
15 IEnumerable<NestedMessage> iterator = MessageStreamIterator<NestedMessage>.FromStreamProvider(() => stream);
16 List<NestedMessage> messages = new List<NestedMessage>(iterator);
17
18 Assert.AreEqual(3, messages.Count);
19 Assert.AreEqual(5, messages[0].Bb);
20 Assert.AreEqual(1500, messages[1].Bb);
21 Assert.IsFalse(messages[2].HasBb);
22 }
Jon Skeet2178b932009-06-25 07:52:07 +010023
24 [Test]
25 public void ManyMessagesShouldNotTriggerSizeAlert() {
26 int messageSize = TestUtil.GetAllSet().SerializedSize;
27 // Enough messages to trigger the alert unless we've reset the size
28 // Note that currently we need to make this big enough to copy two whole buffers,
29 // as otherwise when we refill the buffer the second type, the alert triggers instantly.
30 int correctCount = (CodedInputStream.BufferSize * 2) / messageSize + 1;
31 using (MemoryStream stream = new MemoryStream()) {
32 MessageStreamWriter<TestAllTypes> writer = new MessageStreamWriter<TestAllTypes>(stream);
33 for (int i = 0; i < correctCount; i++) {
34 writer.Write(TestUtil.GetAllSet());
35 }
36 writer.Flush();
37
38 stream.Position = 0;
39
40 int count = 0;
41 foreach (var message in MessageStreamIterator<TestAllTypes>.FromStreamProvider(() => stream)
42 .WithSizeLimit(CodedInputStream.BufferSize * 2)) {
43 count++;
44 TestUtil.AssertAllFieldsSet(message);
45 }
46 Assert.AreEqual(correctCount, count);
47 }
48 }
Jon Skeet68036862008-10-22 13:30:34 +010049 }
50}