PageRenderTime 26ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Examples/ItemsWithLengthPrefix.cs

https://gitlab.com/meermalik/protobuf-net
C# | 217 lines | 200 code | 17 blank | 0 comment | 3 complexity | 8d8724fe65308b1253fb184cbb210efc MD5 | raw file
  1. using NUnit.Framework;
  2. using System.IO;
  3. using ProtoBuf;
  4. using System.Linq;
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. namespace Examples
  8. {
  9. [TestFixture]
  10. public class ItemsWithLengthPrefix
  11. {
  12. static Stream WriteData(int tag, PrefixStyle style, params int[] values)
  13. {
  14. MemoryStream ms = new MemoryStream();
  15. Foo foo = new Foo();
  16. foreach (int value in values)
  17. {
  18. foo.Value = value;
  19. Serializer.SerializeWithLengthPrefix(ms, foo, style, tag);
  20. }
  21. ms.Position = 0;
  22. return ms;
  23. }
  24. static int ReadIndividually(Stream source, int tag, PrefixStyle style, params int[] values)
  25. {
  26. int count = 0;
  27. foreach(int value in values)
  28. {
  29. if (source.Length == source.Position)
  30. {
  31. Debugger.Break();
  32. }
  33. Foo foo = Serializer.DeserializeWithLengthPrefix<Foo>(source, style, tag);
  34. Assert.AreEqual(value, foo.Value);
  35. count++;
  36. }
  37. return count;
  38. }
  39. static int ReadStreaming(Stream source, int tag, PrefixStyle style, params int[] values)
  40. {
  41. var list = Serializer.DeserializeItems<int>(source, style, tag).ToList();
  42. Assert.AreEqual(values.Length, list.Count, "Count");
  43. for (int i = 0; i < values.Length; i++ )
  44. {
  45. Assert.AreEqual(values[i], list[i], "Index " + i + ", value " + values[i]);
  46. }
  47. return values.Length;
  48. }
  49. private static int CheckIndividually(int tag, PrefixStyle style, params int[] values)
  50. {
  51. using(Stream source = WriteData(tag, style, values))
  52. {
  53. return ReadIndividually(source, tag, style, values);
  54. }
  55. }
  56. private static int CheckStreaming(int tag, PrefixStyle style, params int[] values)
  57. {
  58. using (Stream source = WriteData(tag, style, values))
  59. {
  60. return ReadStreaming(source, tag, style, values);
  61. }
  62. }
  63. [Test]
  64. public void ReadIndividuallyFixedLength()
  65. {
  66. Assert.AreEqual(8, CheckIndividually(0, PrefixStyle.Fixed32, -2,-1,0,1,2,3,4,5));
  67. }
  68. [Test]
  69. public void ReadIndividuallyBase128NoTag()
  70. {
  71. Assert.AreEqual(8, CheckIndividually(0, PrefixStyle.Base128, -2, -1, 0, 1, 2, 3, 4, 5));
  72. }
  73. [Test]
  74. public void ReadIndividuallyBase128Tag()
  75. {
  76. Assert.AreEqual(8, CheckIndividually(2, PrefixStyle.Base128, -2, -1, 0, 1, 2, 3, 4, 5));
  77. }
  78. [Test]
  79. public void ReadStreamingFixedLength()
  80. {
  81. Assert.AreEqual(8, CheckStreaming(0, PrefixStyle.Fixed32, -2, -1, 0, 1, 2, 3, 4, 5));
  82. }
  83. [Test]
  84. public void ReadStreamingBase128NoTag()
  85. {
  86. Assert.AreEqual(8, CheckStreaming(0, PrefixStyle.Base128, -2, -1, 0, 1, 2, 3, 4, 5));
  87. }
  88. [Test]
  89. public void ReadStreamingBase128Tag()
  90. {
  91. Assert.AreEqual(8, CheckStreaming(2, PrefixStyle.Base128, -2, -1, 0, 1, 2, 3, 4, 5));
  92. }
  93. [Test]
  94. public void ReadStreamingParentFixedLength()
  95. {
  96. MemoryStream ms = new MemoryStream();
  97. IMLParent a, b, c;
  98. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Fixed32);
  99. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Fixed32);
  100. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Fixed32);
  101. ms.Position = 0;
  102. var list = Serializer.DeserializeItems<IMLParent>(ms, PrefixStyle.Fixed32, 0).ToList();
  103. Assert.AreEqual(3, list.Count, "Count");
  104. InheritanceMidLevel.CheckParent(a, list[0]);
  105. InheritanceMidLevel.CheckParent(b, list[1]);
  106. InheritanceMidLevel.CheckParent(c, list[2]);
  107. }
  108. [Test]
  109. public void ReadStreamingParentBase128Tag()
  110. {
  111. MemoryStream ms = new MemoryStream();
  112. IMLParent a, b, c;
  113. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Base128, 3);
  114. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Base128, 3);
  115. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Base128, 3);
  116. ms.Position = 0;
  117. var list = Serializer.DeserializeItems<IMLParent>(ms, PrefixStyle.Base128, 3).ToList();
  118. Assert.AreEqual(3, list.Count, "Count");
  119. InheritanceMidLevel.CheckParent(a, list[0]);
  120. InheritanceMidLevel.CheckParent(b, list[1]);
  121. InheritanceMidLevel.CheckParent(c, list[2]);
  122. }
  123. [Test]
  124. public void ReadStreamingParentBase128NoTag()
  125. {
  126. MemoryStream ms = new MemoryStream();
  127. IMLParent a, b, c;
  128. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Base128, 0);
  129. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Base128, 0);
  130. Serializer.SerializeWithLengthPrefix<IMLParent>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Base128, 0);
  131. ms.Position = 0;
  132. var list = Serializer.DeserializeItems<IMLParent>(ms, PrefixStyle.Base128, 0).ToList();
  133. Assert.AreEqual(3, list.Count, "Count");
  134. InheritanceMidLevel.CheckParent(a, list[0]);
  135. InheritanceMidLevel.CheckParent(b, list[1]);
  136. InheritanceMidLevel.CheckParent(c, list[2]);
  137. }
  138. [Test]
  139. public void ReadStreamingChildFixedLength()
  140. {
  141. MemoryStream ms = new MemoryStream();
  142. IMLChild a, b, c;
  143. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Fixed32);
  144. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Fixed32);
  145. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Fixed32);
  146. ms.Position = 0;
  147. var list = Serializer.DeserializeItems<IMLChild>(ms, PrefixStyle.Fixed32, 0).ToList();
  148. Assert.AreEqual(3, list.Count, "Count");
  149. InheritanceMidLevel.CheckChild(a, list[0]);
  150. InheritanceMidLevel.CheckChild(b, list[1]);
  151. InheritanceMidLevel.CheckChild(c, list[2]);
  152. }
  153. [Test]
  154. public void ReadStreamingChildBase128Tag()
  155. {
  156. MemoryStream ms = new MemoryStream();
  157. IMLChild a, b, c;
  158. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Base128, 3);
  159. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Base128, 3);
  160. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Base128, 3);
  161. ms.Position = 0;
  162. var list = Serializer.DeserializeItems<IMLChild>(ms, PrefixStyle.Base128, 3).ToList();
  163. Assert.AreEqual(3, list.Count, "Count");
  164. InheritanceMidLevel.CheckChild(a, list[0]);
  165. InheritanceMidLevel.CheckChild(b, list[1]);
  166. InheritanceMidLevel.CheckChild(c, list[2]);
  167. }
  168. [Test]
  169. public void ReadStreamingChildBase128NoTag()
  170. {
  171. MemoryStream ms = new MemoryStream();
  172. IMLChild a, b, c;
  173. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, a = InheritanceMidLevel.CreateChild(123, 456, 789), PrefixStyle.Base128, 0);
  174. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, b = InheritanceMidLevel.CreateChild(100, 200, 300), PrefixStyle.Base128, 0);
  175. Serializer.SerializeWithLengthPrefix<IMLChild>(ms, c = InheritanceMidLevel.CreateChild(400, 500, 600), PrefixStyle.Base128, 0);
  176. ms.Position = 0;
  177. var list = Serializer.DeserializeItems<IMLChild>(ms, PrefixStyle.Base128, 0).ToList();
  178. Assert.AreEqual(3, list.Count, "Count");
  179. InheritanceMidLevel.CheckChild(a, list[0]);
  180. InheritanceMidLevel.CheckChild(b, list[1]);
  181. InheritanceMidLevel.CheckChild(c, list[2]);
  182. }
  183. [Test]
  184. public void TestEmptyStreams()
  185. {
  186. TestEmptyStreams<int>();
  187. TestEmptyStreams<IMLChild>();
  188. TestEmptyStreams<IMLParent>();
  189. }
  190. static void TestEmptyStreams<T>()
  191. {
  192. Assert.IsFalse(Serializer.DeserializeItems<T>(Stream.Null, PrefixStyle.Fixed32, 0).Any());
  193. Assert.IsFalse(Serializer.DeserializeItems<T>(Stream.Null, PrefixStyle.Base128, 0).Any());
  194. Assert.IsFalse(Serializer.DeserializeItems<T>(Stream.Null, PrefixStyle.Base128, 1).Any());
  195. Assert.IsFalse(Serializer.DeserializeItems<T>(new MemoryStream(), PrefixStyle.Fixed32, 0).Any());
  196. Assert.IsFalse(Serializer.DeserializeItems<T>(new MemoryStream(), PrefixStyle.Base128, 0).Any());
  197. Assert.IsFalse(Serializer.DeserializeItems<T>(new MemoryStream(), PrefixStyle.Base128, 1).Any());
  198. }
  199. }
  200. }