Util.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using System;
  3. using System.Linq;
  4. using System.Text;
  5. namespace VeederRoot_Test
  6. {
  7. [TestClass]
  8. public class Util
  9. {
  10. [TestMethod]
  11. public void ConvertHexStrToLong_TestMethod1()
  12. {
  13. var raw = "43BA8C60";
  14. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  15. Assert.AreEqual(1136299104, c);
  16. }
  17. [TestMethod]
  18. public void ConvertHexStrToLong_TestMethod2()
  19. {
  20. var raw = "4 1 C 4 1 2 F C";
  21. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  22. Assert.AreEqual(1103368956, c);
  23. }
  24. [TestMethod]
  25. public void ConvertHexStrToLong_TestMethod3()
  26. {
  27. var raw = "43BA8CCD";
  28. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  29. Assert.AreEqual(1136299213, c);
  30. }
  31. [TestMethod]
  32. public void ConvertHexStrToLong_TestMethod4()
  33. {
  34. var raw = "43ba8ccd";
  35. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  36. Assert.AreEqual(1136299213, c);
  37. }
  38. [TestMethod]
  39. public void ConvertHexStrToLong_TestMethod5()
  40. {
  41. var raw = "01";
  42. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  43. Assert.AreEqual(1, c);
  44. }
  45. [TestMethod]
  46. public void ConvertHexStrToLong_TestMethod6()
  47. {
  48. var raw = "11";
  49. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  50. Assert.AreEqual(17, c);
  51. }
  52. [TestMethod]
  53. public void ConvertHexStrToLong_TestMethod7()
  54. {
  55. var raw = "A1";
  56. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  57. Assert.AreEqual(161, c);
  58. }
  59. [TestMethod]
  60. public void ConvertHexStrToLong_TestMethod8()
  61. {
  62. var raw = "1101";
  63. var c = VeederRoot_ATG_Console.Util.ConvertHexStrToLong(raw);
  64. Assert.AreEqual(4353, c);
  65. }
  66. [TestMethod]
  67. public void ConvertHexBcdStrToBytes_TestMethod1()
  68. {
  69. var raw = new byte[] { 0x34, 0x33, 0x42, 0x41, 0x38, 0x43, 0x43, 0x44 };
  70. var actual = VeederRoot_ATG_Console.Util.ConvertHexBcdStrToBytes(raw);
  71. Assert.AreEqual(true, Outgoing_Message.ValueEquals(new byte[] { 0x43, 0xBA, 0x8C, 0xCD }, actual));
  72. }
  73. [TestMethod]
  74. public void ConvertHexBcdStrToBytes_TestMethod2()
  75. {
  76. var raw = new byte[] { 0x31, 0x33, };
  77. var actual = VeederRoot_ATG_Console.Util.ConvertHexBcdStrToBytes(raw);
  78. Assert.AreEqual(true, Outgoing_Message.ValueEquals(new byte[] { 0x13 }, actual));
  79. }
  80. [TestMethod]
  81. public void ConvertHexBcdStrToBytes_TestMethod3()
  82. {
  83. var raw = new byte[] { 0x30, 0x33, };
  84. var actual = VeederRoot_ATG_Console.Util.ConvertHexBcdStrToBytes(raw);
  85. Assert.AreEqual(true, Outgoing_Message.ValueEquals(new byte[] { 0x03 }, actual));
  86. }
  87. #region ConvertIEEEWith4BytesToDouble
  88. [TestMethod]
  89. public void ConvertIEEEWith4BytesToDouble_TestMethod1()
  90. {
  91. byte[] raw = new byte[] { 0x3F, 0x80, 0x00, 0x00 };
  92. var c = VeederRoot_ATG_Console.Util.ConvertIEEEWith4BytesToDouble(raw);
  93. Assert.AreEqual(1, c);
  94. }
  95. [TestMethod]
  96. public void ConvertIEEEWith4BytesToDouble_TestMethod2()
  97. {
  98. byte[] raw = new byte[] { 0xB8, 0xD1, 0xB7, 0x17 };
  99. var c = VeederRoot_ATG_Console.Util.ConvertIEEEWith4BytesToDouble(raw, 4);
  100. Assert.AreEqual(-0.0001, c);
  101. }
  102. [TestMethod]
  103. public void ConvertIEEEWith4BytesToDouble_TestMethod3()
  104. {
  105. byte[] raw = new byte[] { 0xC2, 0xC7, 0xFA, 0xE1 };
  106. var c = VeederRoot_ATG_Console.Util.ConvertIEEEWith4BytesToDouble(raw, 2);
  107. //var f = Math.Round(c, 2);
  108. Assert.AreEqual(-99.99, c);
  109. }
  110. [TestMethod]
  111. public void ConvertIEEEWith4BytesToDouble_TestMethod4()
  112. {
  113. byte[] raw = new byte[] { 0x46, 0x1C, 0x40, 0x00 };
  114. var c = VeederRoot_ATG_Console.Util.ConvertIEEEWith4BytesToDouble(raw);
  115. Assert.AreEqual(10000, c);
  116. }
  117. [TestMethod]
  118. public void ConvertIEEEWith4BytesToDouble_TestMethod5()
  119. {
  120. byte[] raw = new byte[] { 0x43, 0xBA, 0x8C, 0xCD };
  121. var c = VeederRoot_ATG_Console.Util.ConvertIEEEWith4BytesToDouble(raw);
  122. Assert.AreEqual(373.1, Math.Round(c, 1));
  123. }
  124. #endregion
  125. #region ConvertDoubleToIEEE4Bytes
  126. [TestMethod]
  127. public void ConvertDoubleToIEEE4Bytes_TestMethod1()
  128. {
  129. byte[] expect = new byte[] { 0x3F, 0x80, 0x00, 0x00 };
  130. var actual = VeederRoot_ATG_Console.Util.ConvertDoubleToIEEE4Bytes(1);
  131. Assert.AreEqual(true, Outgoing_Message.ValueEquals(expect, actual));
  132. }
  133. [TestMethod]
  134. public void ConvertDoubleToIEEE4Bytes_TestMethod2()
  135. {
  136. byte[] expect = new byte[] { 0xB8, 0xD1, 0xB7, 0x17 };
  137. var actual = VeederRoot_ATG_Console.Util.ConvertDoubleToIEEE4Bytes(-0.0001);
  138. Assert.AreEqual(true, Outgoing_Message.ValueEquals(expect, actual));
  139. }
  140. [TestMethod]
  141. public void ConvertDoubleToIEEE4Bytes_TestMethod3()
  142. {
  143. byte[] expect = new byte[] { 0xC2, 0xC7, 0xFA, 0xE1 };
  144. var actual = VeederRoot_ATG_Console.Util.ConvertDoubleToIEEE4Bytes(-99.99);
  145. //var f = Math.Round(c, 2);
  146. Assert.AreEqual(true, Outgoing_Message.ValueEquals(expect, actual));
  147. }
  148. [TestMethod]
  149. public void ConvertDoubleToIEEE4Bytes_TestMethod4()
  150. {
  151. byte[] expect = new byte[] { 0x46, 0x1C, 0x40, 0x00 };
  152. var actual = VeederRoot_ATG_Console.Util.ConvertDoubleToIEEE4Bytes(10000);
  153. Assert.AreEqual(true, Outgoing_Message.ValueEquals(expect, actual));
  154. }
  155. #endregion
  156. }
  157. }