TestPrimitiveTypes.cs 4.5 KB

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