Parser.cs 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace VeederRoot_Test
  7. {
  8. [TestClass]
  9. public class Parser
  10. {
  11. public static bool ValueEquals(IEnumerable<byte> array1, IEnumerable<byte> array2)
  12. {
  13. if (array1 == null && array2 == null)
  14. {
  15. return true;
  16. }
  17. if ((array1 == null) || (array2 == null))
  18. {
  19. return false;
  20. }
  21. if (array1.Count() != array2.Count())
  22. {
  23. return false;
  24. }
  25. if (array1.Equals(array2))
  26. {
  27. return true;
  28. }
  29. else
  30. {
  31. for (int Index = 0; Index < array1.Count(); Index++)
  32. {
  33. if (!Equals(array1.ElementAt(Index), array2.ElementAt(Index)))
  34. {
  35. return false;
  36. }
  37. }
  38. }
  39. return true;
  40. }
  41. [TestMethod]
  42. public void CaculateCheckSum_TestMethod1()
  43. {
  44. VeederRoot_ATG_Console.Parser parser = new VeederRoot_ATG_Console.Parser();
  45. //Example Report 1: <SOH>B000000012FE0A<EOT>
  46. var expect = 0x0F * Math.Pow(16, 3) + 0x0E * Math.Pow(16, 2) + 0x00 * 16 + 0x0A;
  47. var raw = new byte[] { 0x01 }.Concat(Encoding.ASCII.GetBytes("B000000012"));
  48. var actual = parser.CaculateCheckSum(raw.ToArray());
  49. Assert.AreEqual(true, actual == expect);
  50. }
  51. [TestMethod]
  52. public void CaculateCheckSum_TestMethod2()
  53. {
  54. VeederRoot_ATG_Console.Parser parser = new VeederRoot_ATG_Console.Parser();
  55. //Example Report 2: <SOH>B100000501FE06<EOT>
  56. var expect = 0x0F * Math.Pow(16, 3) + 0x0E * Math.Pow(16, 2) + 0x00 * 16 + 0x06;
  57. var raw = new byte[] { 0x01 }.Concat(Encoding.ASCII.GetBytes("B100000501"));
  58. var actual = parser.CaculateCheckSum(raw.ToArray());
  59. Assert.AreEqual(true, actual == expect);
  60. }
  61. }
  62. }