MessageBaseUnitTest.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using ShellChina_EPS_Project_CarPlatePay_EpsClient_App.MessageEntity.Base;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. namespace ShellChina_EPS_Project_CarPlatePay_EpsClient_App_Test
  6. {
  7. [TestClass]
  8. public class MessageBaseUnitTest
  9. {
  10. public static bool ValueEquals(IEnumerable<byte> array1, IEnumerable<byte> array2)
  11. {
  12. if (array1 == null && array2 == null)
  13. {
  14. return true;
  15. }
  16. if ((array1 == null) || (array2 == null))
  17. {
  18. return false;
  19. }
  20. if (array1.Count() != array2.Count())
  21. {
  22. return false;
  23. }
  24. if (array1.Equals(array2))
  25. {
  26. return true;
  27. }
  28. else
  29. {
  30. for (int Index = 0; Index < array1.Count(); Index++)
  31. {
  32. if (!Equals(array1.ElementAt(Index), array2.ElementAt(Index)))
  33. {
  34. return false;
  35. }
  36. }
  37. }
  38. return true;
  39. }
  40. [TestMethod]
  41. public void Serialize_TestMethod1()
  42. {
  43. MessageBase msg = new MessageBase();
  44. msg.TPDU = "3333";
  45. msg.MessageTypeIdentifier = 1234;
  46. msg.BitMap = new List<byte>() { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  47. msg.Content = new List<byte>() { 0x99, 0x99 };
  48. var parser
  49. = new ShellChina_EPS_Project_CarPlatePay_EpsClient_App.Parser();
  50. var actual = parser.Serialize(msg);
  51. var expect = new List<byte>() {
  52. 0x00,0x11,
  53. 0x00,0x00,0x00,0x33,0x33,
  54. 0x12,0x34,
  55. 0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  56. 0x99,0x99
  57. };
  58. Assert.AreEqual(true, ValueEquals(actual, expect),
  59. "expect: " + expect.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n)
  60. + "\r\n actual: " + actual.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n));
  61. }
  62. [TestMethod]
  63. public void Serialize_TestMethod2()
  64. {
  65. MessageBase msg = new MessageBase();
  66. msg.TPDU = "3333";
  67. msg.MessageTypeIdentifier = 1234;
  68. msg.BitMap = new List<byte>() { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  69. msg.Content = new List<byte>() { 0x99, 0x99, 0x88, 0x88, 0x77, 0x77 };
  70. var parser
  71. = new ShellChina_EPS_Project_CarPlatePay_EpsClient_App.Parser();
  72. var actual = parser.Serialize(msg);
  73. var expect = new List<byte>() {
  74. 0x00,0x15,
  75. 0x00,0x00,0x00,0x33,0x33,
  76. 0x12,0x34,
  77. 0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  78. 0x99,0x99, 0x88, 0x88, 0x77, 0x77
  79. };
  80. Assert.AreEqual(true, ValueEquals(actual, expect),
  81. "expect: " + expect.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n)
  82. + "\r\n actual: " + actual.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n));
  83. }
  84. [TestMethod]
  85. public void Serialize_TestMethod3()
  86. {
  87. MessageBase msg = new MessageBase();
  88. msg.TPDU = "1234563333";
  89. msg.MessageTypeIdentifier = 7890;
  90. msg.BitMap = new List<byte>() { 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08 };
  91. msg.Content = new List<byte>() { 0x99, 0x99, 0x88, 0x88, 0x77, 0x77 };
  92. var parser
  93. = new ShellChina_EPS_Project_CarPlatePay_EpsClient_App.Parser();
  94. var actual = parser.Serialize(msg);
  95. var expect = new List<byte>() {
  96. 0x00,0x15,
  97. 0x12,0x34,0x56,0x33,0x33,
  98. 0x78,0x90,
  99. 0x01,0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
  100. 0x99,0x99, 0x88, 0x88, 0x77, 0x77
  101. };
  102. Assert.AreEqual(true, ValueEquals(actual, expect),
  103. "expect: " + expect.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n)
  104. + "\r\n actual: " + actual.Select(e => e.ToString("X2")).Aggregate((acc, n) => acc + " " + n));
  105. }
  106. }
  107. }