PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/Examples/TestNumbers/NumberTests.cs

https://gitlab.com/meermalik/protobuf-net
C# | 293 lines | 268 code | 22 blank | 3 comment | 7 complexity | 88c4a0f39f7697bf86fec4cc55ce0ff1 MD5 | raw file
  1. using System;
  2. using System.ComponentModel;
  3. using System.Diagnostics;
  4. using System.IO;
  5. using NUnit.Framework;
  6. using ProtoBuf;
  7. namespace Examples.TestNumbers
  8. {
  9. [ProtoContract]
  10. class NumRig
  11. {
  12. [ProtoMember(1, DataFormat=DataFormat.Default)]
  13. public int Int32Default { get; set; }
  14. [ProtoMember(2, DataFormat = DataFormat.ZigZag)]
  15. public int Int32ZigZag { get; set; }
  16. [ProtoMember(3, DataFormat = DataFormat.TwosComplement)]
  17. public int Int32TwosComplement { get; set; }
  18. [ProtoMember(4, DataFormat = DataFormat.FixedSize)]
  19. public int Int32FixedSize { get; set; }
  20. [ProtoMember(5, DataFormat = DataFormat.Default)]
  21. public uint UInt32Default { get; set; }
  22. [ProtoMember(7, DataFormat = DataFormat.TwosComplement)]
  23. public uint UInt32TwosComplement { get; set; }
  24. [ProtoMember(8, DataFormat = DataFormat.FixedSize)]
  25. public uint UInt32FixedSize { get; set; }
  26. [ProtoMember(9, DataFormat = DataFormat.Default)]
  27. public long Int64Default { get; set; }
  28. [ProtoMember(10, DataFormat = DataFormat.ZigZag)]
  29. public long Int64ZigZag { get; set; }
  30. [ProtoMember(11, DataFormat = DataFormat.TwosComplement)]
  31. public long Int64TwosComplement { get; set; }
  32. [ProtoMember(12, DataFormat = DataFormat.FixedSize)]
  33. public long Int64FixedSize { get; set; }
  34. [ProtoMember(13, DataFormat = DataFormat.Default)]
  35. public ulong UInt64Default { get; set; }
  36. [ProtoMember(15, DataFormat = DataFormat.TwosComplement)]
  37. public ulong UInt64TwosComplement { get; set; }
  38. [ProtoMember(16, DataFormat = DataFormat.FixedSize)]
  39. public ulong UInt64FixedSize { get; set; }
  40. [ProtoMember(17)]
  41. public string Foo { get; set; }
  42. }
  43. [ProtoContract]
  44. class ZigZagInt32
  45. {
  46. [ProtoMember(1, DataFormat = DataFormat.ZigZag)]
  47. [DefaultValue(123456)]
  48. public int Foo { get; set; }
  49. }
  50. [ProtoContract]
  51. class TwosComplementInt32
  52. {
  53. [ProtoMember(1, DataFormat = DataFormat.TwosComplement)]
  54. [DefaultValue(123456)]
  55. public int Foo { get; set; }
  56. }
  57. [ProtoContract]
  58. class TwosComplementUInt32
  59. {
  60. [ProtoMember(1, DataFormat = DataFormat.TwosComplement)]
  61. public uint Foo { get; set; }
  62. }
  63. [ProtoContract]
  64. class ZigZagInt64
  65. {
  66. [ProtoMember(1, DataFormat = DataFormat.ZigZag)]
  67. public long Foo { get; set; }
  68. }
  69. [TestFixture]
  70. public class SignTests
  71. {
  72. [Test]
  73. public void RoundTripBigPosativeZigZagInt64()
  74. {
  75. ZigZagInt64 obj = new ZigZagInt64 { Foo = 123456789 },
  76. clone = Serializer.DeepClone(obj);
  77. Assert.AreEqual(obj.Foo, clone.Foo);
  78. }
  79. [Test]
  80. public void RoundTripBigPosativeZigZagInt64ForDateTime()
  81. {
  82. // this test to simulate a typical DateTime value
  83. ZigZagInt64 obj = new ZigZagInt64 { Foo = 1216669168515 },
  84. clone = Serializer.DeepClone(obj);
  85. Assert.AreEqual(obj.Foo, clone.Foo);
  86. }
  87. [Test]
  88. public void RoundTripBigNegativeZigZagInt64() {
  89. ZigZagInt64 obj = new ZigZagInt64 { Foo = -123456789 },
  90. clone = Serializer.DeepClone(obj);
  91. clone = Serializer.DeepClone(obj);
  92. Assert.AreEqual(obj.Foo, clone.Foo);
  93. }
  94. [Test]
  95. public void TestSignTwosComplementInt32_0()
  96. {
  97. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = 0 }, 0x08, 0x00), "0");
  98. }
  99. [Test]
  100. public void TestSignTwosComplementInt32_Default()
  101. {
  102. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = 123456 }), "123456");
  103. }
  104. [Test]
  105. public void TestSignTwosComplementInt32_1()
  106. {
  107. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = 1 }, 0x08, 0x01), "+1");
  108. }
  109. [Test]
  110. public void TestSignTwosComplementInt32_2()
  111. {
  112. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = 2 }, 0x08, 0x02), "+2");
  113. }
  114. [Test]
  115. public void TestSignTwosComplementInt32_m1()
  116. {
  117. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = -1 }, 0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01), "-1");
  118. }
  119. [Test]
  120. public void TestSignTwosComplementInt32_m2()
  121. {
  122. Assert.IsTrue(Program.CheckBytes(new TwosComplementInt32 { Foo = -2 }, 0x08, 0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01), "-2");
  123. }
  124. [Test]
  125. public void TestSignZigZagInt32_0()
  126. {
  127. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = 0 }, 0x08, 0x00), "0");
  128. }
  129. [Test]
  130. public void TestSignZigZagInt32_Default()
  131. {
  132. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = 123456 }), "123456");
  133. }
  134. [Test]
  135. public void TestSignZigZagInt32_1()
  136. {
  137. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = 1 }, 0x08, 0x02), "+1");
  138. }
  139. [Test]
  140. public void TestSignZigZagInt32_2()
  141. {
  142. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = 2 }, 0x08, 0x04), "+2");
  143. }
  144. [Test]
  145. public void TestSignZigZagInt32_m1()
  146. {
  147. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = -1 }, 0x08, 0x01), "-1");
  148. }
  149. [Test]
  150. public void TestSignZigZagInt32_m2()
  151. {
  152. Assert.IsTrue(Program.CheckBytes(new ZigZagInt32 { Foo = -2 }, 0x08, 0x03), "-2");
  153. }
  154. [Test]
  155. public void TestSignZigZagInt32_2147483647()
  156. {
  157. // encoding doc gives numbers in terms of uint equivalent
  158. ZigZagInt32 zz = new ZigZagInt32 { Foo = 2147483647 }, clone = Serializer.DeepClone(zz);
  159. Assert.AreEqual(zz.Foo, clone.Foo, "Roundtrip");
  160. TwosComplementUInt32 tc = Serializer.ChangeType<ZigZagInt32, TwosComplementUInt32>(zz);
  161. Assert.AreEqual(4294967294, tc.Foo);
  162. }
  163. [Test]
  164. public void TestSignZigZagInt32_m2147483648()
  165. {
  166. // encoding doc gives numbers in terms of uint equivalent
  167. ZigZagInt32 zz = new ZigZagInt32 { Foo = -2147483648 }, clone = Serializer.DeepClone(zz);
  168. Assert.AreEqual(zz.Foo, clone.Foo, "Roundtrip");
  169. TwosComplementUInt32 tc = Serializer.ChangeType<ZigZagInt32, TwosComplementUInt32>(zz);
  170. Assert.AreEqual(4294967295, tc.Foo);
  171. }
  172. [Test, ExpectedException(typeof(EndOfStreamException))]
  173. public void TestEOF()
  174. {
  175. Program.Build<ZigZagInt32>(0x08); // but no payload for field 1
  176. }
  177. [Test, ExpectedException(typeof(OverflowException))]
  178. public void TestOverflow()
  179. {
  180. Program.Build<ZigZagInt32>(0x08, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF);
  181. }
  182. [Test]
  183. public void SweepBitsInt32()
  184. {
  185. NumRig rig = new NumRig();
  186. const string SUCCESS = "bar";
  187. rig.Foo = SUCCESS; // to help test stream ending prematurely
  188. for (int i = 0; i < 32; i++)
  189. {
  190. int bigBit = i == 0 ? 0 : (1 << i - 1);
  191. for (int j = 0; j <= i; j++)
  192. {
  193. int smallBit = 1 << j;
  194. int val = bigBit | smallBit;
  195. rig.Int32Default
  196. = rig.Int32FixedSize
  197. = rig.Int32TwosComplement
  198. = rig.Int32ZigZag
  199. = val;
  200. NumRig clone = Serializer.DeepClone(rig);
  201. Assert.AreEqual(val, rig.Int32Default);
  202. Assert.AreEqual(val, rig.Int32FixedSize);
  203. Assert.AreEqual(val, rig.Int32TwosComplement);
  204. Assert.AreEqual(val, rig.Int32ZigZag);
  205. Assert.AreEqual(SUCCESS, rig.Foo);
  206. }
  207. }
  208. }
  209. [Test]
  210. public void SweepBitsInt64KnownTricky()
  211. {
  212. try
  213. {
  214. int i = 31, j = 31;
  215. long bigBit = i == 0 ? 0 : (1 << i - 1);
  216. long smallBit = 1 << j;
  217. long val = bigBit | smallBit;
  218. NumRig rig = new NumRig();
  219. rig.Int64Default // 9 => 72
  220. = rig.Int64FixedSize // 12 => 97?
  221. = rig.Int64TwosComplement // 11 => 88
  222. = rig.Int64ZigZag // 10 => 80
  223. = val;
  224. const string SUCCESS = "bar";
  225. rig.Foo = SUCCESS; // to help test stream ending prematurely
  226. MemoryStream ms = new MemoryStream();
  227. Serializer.Serialize(ms, rig);
  228. byte[] raw = ms.ToArray();
  229. ms.Position = 0;
  230. NumRig clone = Serializer.Deserialize<NumRig>(ms);
  231. Assert.AreEqual(val, clone.Int64Default, "Default");
  232. Assert.AreEqual(val, clone.Int64FixedSize, "FixedSize");
  233. Assert.AreEqual(val, clone.Int64ZigZag, "ZigZag");
  234. Assert.AreEqual(val, clone.Int64TwosComplement, "TwosComplement");
  235. Assert.AreEqual(SUCCESS, clone.Foo, "EOF check");
  236. }
  237. catch (Exception ex)
  238. {
  239. Debug.WriteLine(ex.StackTrace);
  240. Assert.Fail(ex.Message);
  241. }
  242. }
  243. [Test]
  244. public void SweepBitsInt64()
  245. {
  246. NumRig rig = new NumRig();
  247. const string SUCCESS = "bar";
  248. rig.Foo = SUCCESS; // to help test stream ending prematurely
  249. for (int i = 0; i < 64; i++)
  250. {
  251. long bigBit = i == 0 ? 0 : (1 << i - 1);
  252. for (int j = 0; j <= i; j++)
  253. {
  254. long smallBit = 1 << j;
  255. long val = bigBit | smallBit;
  256. rig.Int64Default
  257. = rig.Int64FixedSize
  258. = rig.Int64TwosComplement
  259. = rig.Int64ZigZag
  260. = val;
  261. NumRig clone = Serializer.DeepClone(rig);
  262. Assert.AreEqual(val, clone.Int64Default, "Default");
  263. Assert.AreEqual(val, clone.Int64FixedSize, "FixedSize");
  264. Assert.AreEqual(val, clone.Int64ZigZag, "ZigZag");
  265. Assert.AreEqual(val, clone.Int64TwosComplement, "TwosComplement");
  266. Assert.AreEqual(SUCCESS, clone.Foo, "EOF check: " + val.ToString());
  267. }
  268. }
  269. }
  270. }
  271. }