TestPrimitiveTypes.cs 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using Topten.JsonKit;
  6. using System.Reflection;
  7. using Xunit;
  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 = "JsonKit!";
  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. [Obfuscation(Exclude = true, ApplyToMembers = true)]
  50. public class TestPrimitiveTypes
  51. {
  52. void Compare(AllTypes all, AllTypes all2)
  53. {
  54. Assert.Equal(all.String, all2.String);
  55. Assert.Equal(all.Char, all2.Char);
  56. Assert.Equal(all.Bool, all2.Bool);
  57. Assert.Equal(all.Byte, all2.Byte);
  58. Assert.Equal(all.SByte, all2.SByte);
  59. Assert.Equal(all.Short, all2.Short);
  60. Assert.Equal(all.UShort, all2.UShort);
  61. Assert.Equal(all.Int, all2.Int);
  62. Assert.Equal(all.UInt, all2.UInt);
  63. Assert.Equal(all.Long, all2.Long);
  64. Assert.Equal(all.ULong, all2.ULong);
  65. Assert.Equal(all.Decimal, all2.Decimal);
  66. Assert.Equal(all.Float, all2.Float);
  67. Assert.Equal(all.Double, all2.Double);
  68. Assert.Equal(all.DateTime, all2.DateTime);
  69. Assert.True((all.Blob==null && all2.Blob==null) || Convert.ToBase64String(all.Blob)==Convert.ToBase64String(all2.Blob));
  70. }
  71. [Fact]
  72. public void TestBasics()
  73. {
  74. var all = new AllTypes();
  75. all.Init();
  76. var json = Json.Format(all);
  77. var all2 = Json.Parse<AllTypes>(json);
  78. Compare(all, all2);
  79. }
  80. [Fact]
  81. public void TestNegatives()
  82. {
  83. var all = new AllTypes();
  84. all.Init();
  85. all.SByte = -1;
  86. all.Short = -2;
  87. all.Int = -3;
  88. all.Long = -4;
  89. all.Decimal = -5.1M;
  90. all.Float = -6.2f;
  91. all.Double = -7.3;
  92. var json = Json.Format(all);
  93. var all2 = Json.Parse<AllTypes>(json);
  94. Compare(all, all2);
  95. }
  96. [Fact]
  97. public void TestMaxValue()
  98. {
  99. var all = new AllTypes();
  100. all.Init();
  101. all.Bool = true;
  102. all.Byte = Byte.MaxValue;
  103. all.SByte = SByte.MaxValue;
  104. all.Short = short.MaxValue;
  105. all.UShort = ushort.MaxValue;
  106. all.Int = int.MaxValue;
  107. all.UInt = uint.MaxValue;
  108. all.Long = long.MaxValue;
  109. all.ULong = ulong.MaxValue;
  110. all.Decimal = decimal.MaxValue;
  111. all.Float = float.MaxValue;
  112. all.Double = double.MaxValue;
  113. var json = Json.Format(all);
  114. Console.WriteLine(json);
  115. var all2 = Json.Parse<AllTypes>(json);
  116. Compare(all, all2);
  117. }
  118. [Fact]
  119. public void TestMinValue()
  120. {
  121. var all = new AllTypes();
  122. all.String = null;
  123. all.Bool = false;
  124. all.Byte = Byte.MinValue;
  125. all.SByte = SByte.MinValue;
  126. all.Short = short.MinValue;
  127. all.UShort = ushort.MinValue;
  128. all.Int = int.MinValue;
  129. all.UInt = uint.MinValue;
  130. all.Long = long.MinValue;
  131. all.ULong = ulong.MinValue;
  132. all.Decimal = decimal.MinValue;
  133. all.Float = float.MinValue;
  134. all.Double = double.MinValue;
  135. all.Blob = null;
  136. var json = Json.Format(all);
  137. Console.WriteLine(json);
  138. var all2 = Json.Parse<AllTypes>(json);
  139. Compare(all, all2);
  140. }
  141. }
  142. }