TestPrimitiveTypes.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using PetaTest;
  6. using PetaJson;
  7. using System.Reflection;
  8. namespace TestCases
  9. {
  10. [Json]
  11. class AllTypes
  12. {
  13. public string String;
  14. public char Char;
  15. public bool Bool;
  16. public byte Byte;
  17. public sbyte SByte;
  18. public short Short;
  19. public ushort UShort;
  20. public int Int;
  21. public uint UInt;
  22. public long Long;
  23. public ulong ULong;
  24. public decimal Decimal;
  25. public float Float;
  26. public double Double;
  27. public DateTime DateTime;
  28. public byte[] Blob;
  29. public void Init()
  30. {
  31. String = "PetaJson!";
  32. Char = 'J';
  33. Bool = false;
  34. Byte = 1;
  35. SByte = 2;
  36. Short = 3;
  37. UShort = 4;
  38. Int = 5;
  39. UInt = 6;
  40. Long = 7;
  41. ULong = 8;
  42. Decimal = 9.1M;
  43. Float = 10.2f;
  44. Double = 11.3;
  45. DateTime = new DateTime(2014, 1, 1, 13, 23, 24);
  46. Blob = new byte[] { 12, 13, 14, 15 };
  47. }
  48. }
  49. [TestFixture]
  50. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  51. public class TestPrimitiveTypes
  52. {
  53. void Compare(AllTypes all, AllTypes all2)
  54. {
  55. Assert.AreEqual(all.String, all2.String);
  56. Assert.AreEqual(all.Char, all2.Char);
  57. Assert.AreEqual(all.Bool, all2.Bool);
  58. Assert.AreEqual(all.Byte, all2.Byte);
  59. Assert.AreEqual(all.SByte, all2.SByte);
  60. Assert.AreEqual(all.Short, all2.Short);
  61. Assert.AreEqual(all.UShort, all2.UShort);
  62. Assert.AreEqual(all.Int, all2.Int);
  63. Assert.AreEqual(all.UInt, all2.UInt);
  64. Assert.AreEqual(all.Long, all2.Long);
  65. Assert.AreEqual(all.ULong, all2.ULong);
  66. Assert.AreEqual(all.Decimal, all2.Decimal);
  67. Assert.AreEqual(all.Float, all2.Float);
  68. Assert.AreEqual(all.Double, all2.Double);
  69. Assert.AreEqual(all.DateTime, all2.DateTime);
  70. Assert.IsTrue((all.Blob==null && all2.Blob==null) || Convert.ToBase64String(all.Blob)==Convert.ToBase64String(all2.Blob));
  71. }
  72. [Test]
  73. public void TestBasics()
  74. {
  75. var all = new AllTypes();
  76. all.Init();
  77. var json = Json.Format(all);
  78. var all2 = Json.Parse<AllTypes>(json);
  79. Compare(all, all2);
  80. }
  81. [Test]
  82. public void TestNegatives()
  83. {
  84. var all = new AllTypes();
  85. all.Init();
  86. all.SByte = -1;
  87. all.Short = -2;
  88. all.Int = -3;
  89. all.Long = -4;
  90. all.Decimal = -5.1M;
  91. all.Float = -6.2f;
  92. all.Double = -7.3;
  93. var json = Json.Format(all);
  94. var all2 = Json.Parse<AllTypes>(json);
  95. Compare(all, all2);
  96. }
  97. [Test]
  98. public void TestMaxValue()
  99. {
  100. var all = new AllTypes();
  101. all.Init();
  102. all.Bool = true;
  103. all.Byte = Byte.MaxValue;
  104. all.SByte = SByte.MaxValue;
  105. all.Short = short.MaxValue;
  106. all.UShort = ushort.MaxValue;
  107. all.Int = int.MaxValue;
  108. all.UInt = uint.MaxValue;
  109. all.Long = long.MaxValue;
  110. all.ULong = ulong.MaxValue;
  111. all.Decimal = decimal.MaxValue;
  112. all.Float = float.MaxValue;
  113. all.Double = double.MaxValue;
  114. var json = Json.Format(all);
  115. Console.WriteLine(json);
  116. var all2 = Json.Parse<AllTypes>(json);
  117. Compare(all, all2);
  118. }
  119. [Test]
  120. public void TestMinValue()
  121. {
  122. var all = new AllTypes();
  123. all.String = null;
  124. all.Bool = false;
  125. all.Byte = Byte.MinValue;
  126. all.SByte = SByte.MinValue;
  127. all.Short = short.MinValue;
  128. all.UShort = ushort.MinValue;
  129. all.Int = int.MinValue;
  130. all.UInt = uint.MinValue;
  131. all.Long = long.MinValue;
  132. all.ULong = ulong.MinValue;
  133. all.Decimal = decimal.MinValue;
  134. all.Float = float.MinValue;
  135. all.Double = double.MinValue;
  136. all.Blob = null;
  137. var json = Json.Format(all);
  138. Console.WriteLine(json);
  139. var all2 = Json.Parse<AllTypes>(json);
  140. Compare(all, all2);
  141. }
  142. }
  143. }