PageRenderTime 37ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/Examples/GuidTests.cs

https://gitlab.com/meermalik/protobuf-net
C# | 198 lines | 168 code | 24 blank | 6 comment | 0 complexity | 6f346c30bfdac98103651d49110cd11d MD5 | raw file
  1. using System;
  2. using System.IO;
  3. using NUnit.Framework;
  4. using ProtoBuf;
  5. using System.Data.Linq.Mapping;
  6. using System.ComponentModel;
  7. using System.Diagnostics;
  8. namespace Examples
  9. {
  10. [ProtoContract]
  11. class GuidData
  12. {
  13. [ProtoMember(1)]
  14. public Guid Bar { get; set; }
  15. }
  16. [ProtoContract(UseProtoMembersOnly = true)]
  17. [ProtoPartialMember(25, "GUID")]
  18. public partial class User { }
  19. public partial class User
  20. {
  21. [global::System.Data.Linq.Mapping.ColumnAttribute(Storage = "_GUID", DbType = "UniqueIdentifier", UpdateCheck = UpdateCheck.Never)]
  22. [global::System.Runtime.Serialization.DataMemberAttribute(Order = 26)]
  23. public System.Guid GUID
  24. {
  25. get;
  26. set;
  27. }
  28. }
  29. [ProtoContract]
  30. public class UserWithCrazyDefault
  31. {
  32. public UserWithCrazyDefault()
  33. {
  34. GUID = new Guid("01020304050607080102030405060708");
  35. }
  36. [ProtoMember(25), DefaultValue("01020304050607080102030405060708")]
  37. public System.Guid GUID { get; set; }
  38. }
  39. [TestFixture]
  40. public class GuidTests
  41. {
  42. public static int Measure<T>(T value)
  43. {
  44. using (var ms = new MemoryStream())
  45. {
  46. Serializer.Serialize<T>(ms, value);
  47. return (int)ms.Length;
  48. }
  49. }
  50. [Test]
  51. public void TestPartialWithGuid()
  52. {
  53. var user = new User();
  54. var clone = Serializer.DeepClone(user);
  55. Assert.AreEqual(user.GUID, clone.GUID);
  56. Assert.AreEqual(0, Measure(user));
  57. user = new User { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
  58. clone = Serializer.DeepClone(user);
  59. Assert.AreEqual(user.GUID, clone.GUID);
  60. Assert.AreEqual(21, Measure(user));
  61. Serializer.PrepareSerializer<User>();
  62. user = new User();
  63. clone = Serializer.DeepClone(user);
  64. Assert.AreEqual(user.GUID, clone.GUID);
  65. Assert.AreEqual(0, Measure(user));
  66. user = new User { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
  67. clone = Serializer.DeepClone(user);
  68. Assert.AreEqual(user.GUID, clone.GUID);
  69. Assert.AreEqual(21, Measure(user));
  70. }
  71. [Test]
  72. public void TestGuidWithCrazyDefault()
  73. {
  74. var user = new UserWithCrazyDefault();
  75. var clone = Serializer.DeepClone(user);
  76. Assert.AreEqual(user.GUID, clone.GUID);
  77. Assert.AreEqual(0, Measure(user));
  78. user = new UserWithCrazyDefault { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
  79. clone = Serializer.DeepClone(user);
  80. Assert.AreEqual(user.GUID, clone.GUID);
  81. Assert.AreEqual(21, Measure(user));
  82. user = new UserWithCrazyDefault { GUID = Guid.Empty };
  83. clone = Serializer.DeepClone(user);
  84. Assert.AreEqual(user.GUID, clone.GUID);
  85. Assert.AreEqual(3, Measure(user));
  86. Serializer.PrepareSerializer<UserWithCrazyDefault>();
  87. user = new UserWithCrazyDefault();
  88. clone = Serializer.DeepClone(user);
  89. Assert.AreEqual(user.GUID, clone.GUID);
  90. Assert.AreEqual(0, Measure(user));
  91. user = new UserWithCrazyDefault { GUID = new Guid("00112233445566778899AABBCCDDEEFF") };
  92. clone = Serializer.DeepClone(user);
  93. Assert.AreEqual(user.GUID, clone.GUID);
  94. Assert.AreEqual(21, Measure(user));
  95. user = new UserWithCrazyDefault { GUID = Guid.Empty };
  96. clone = Serializer.DeepClone(user);
  97. Assert.AreEqual(user.GUID, clone.GUID);
  98. Assert.AreEqual(3, Measure(user));
  99. }
  100. [Test]
  101. public void TestGuidLayout()
  102. {
  103. var guid = new Guid("00112233445566778899AABBCCDDEEFF");
  104. var msBlob = guid.ToByteArray();
  105. var msHex = BitConverter.ToString(msBlob);
  106. Assert.AreEqual("33-22-11-00-55-44-77-66-88-99-AA-BB-CC-DD-EE-FF", msHex);
  107. var obj = new GuidLayout { Value = guid };
  108. using (var ms = new MemoryStream())
  109. {
  110. Serializer.Serialize(ms, obj);
  111. string hex = BitConverter.ToString(ms.GetBuffer(), 0, (int)ms.Length);
  112. // 0A = 1010 = field 1, length delimited (sub-object)
  113. // 12 = length 18
  114. // 09 = field 1, fixed-length 64 bit
  115. // 33-22-11-00-55-44-77-66 = payload
  116. // 11 = field 2, fixed-length 64 bit
  117. // 88-99-AA-BB-CC-DD-EE-FF
  118. Assert.AreEqual(
  119. "0A-12-09-33-22-11-00-55-44-77-66-11-88-99-AA-BB-CC-DD-EE-FF",
  120. hex
  121. );
  122. }
  123. }
  124. [ProtoContract]
  125. public class GuidLayout
  126. {
  127. [ProtoMember(1)]
  128. public Guid Value { get; set; }
  129. }
  130. [Test]
  131. public void TestDeserializeEmptyWide()
  132. {
  133. GuidData data = Program.Build<GuidData>(
  134. 0x0A, 0x12, // prop 1, string:18
  135. 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, //1:fixed64:0
  136. 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 //2:fixed64:0
  137. );
  138. Assert.AreEqual(Guid.Empty, data.Bar);
  139. }
  140. [Test]
  141. public void TestDeserializeEmptyShort()
  142. {
  143. GuidData data = Program.Build<GuidData>(
  144. 0x0A, 0x00 // prop 1, string:0
  145. );
  146. Assert.AreEqual(Guid.Empty, data.Bar);
  147. }
  148. [Test]
  149. public void TestEmptyGuid() {
  150. GuidData foo = new GuidData { Bar = Guid.Empty };
  151. using (MemoryStream ms = new MemoryStream())
  152. {
  153. Serializer.Serialize(ms, foo);
  154. Assert.AreEqual(0, ms.Length); // 1 tag, 1 length (0)
  155. ms.Position = 0;
  156. GuidData clone = Serializer.Deserialize<GuidData>(ms);
  157. Assert.AreEqual(foo.Bar, clone.Bar);
  158. }
  159. }
  160. [Test]
  161. public void TestNonEmptyGuid()
  162. {
  163. GuidData foo = new GuidData { Bar = Guid.NewGuid() };
  164. using (MemoryStream ms = new MemoryStream())
  165. {
  166. Serializer.Serialize(ms, foo);
  167. Assert.AreEqual(20, ms.Length);
  168. ms.Position = 0;
  169. GuidData clone = Serializer.Deserialize<GuidData>(ms);
  170. Assert.AreEqual(foo.Bar, clone.Bar);
  171. }
  172. }
  173. }
  174. }