S7WStringTests.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Microsoft.VisualStudio.TestTools.UnitTesting;
  2. using S7.Net.Types;
  3. using System;
  4. namespace S7.Net.UnitTest.TypeTests
  5. {
  6. [TestClass]
  7. public class S7WStringTests
  8. {
  9. [TestMethod]
  10. public void ReadEmptyStringWithZeroLength()
  11. {
  12. AssertFromByteArrayEquals("", 0, 0 , 0, 0);
  13. }
  14. [TestMethod]
  15. public void ReadEmptyStringWithOneCharLength()
  16. {
  17. AssertFromByteArrayEquals("", 0, 1, 0, 0, 0, 0);
  18. }
  19. [TestMethod]
  20. public void ReadEmptyStringWithOneCharGarbage()
  21. {
  22. AssertFromByteArrayEquals("", 0, 1, 0, 0, 0x00, 0x41);
  23. }
  24. [TestMethod]
  25. public void ReadMalformedStringTooShort()
  26. {
  27. Assert.ThrowsException<PlcException>(() => AssertFromByteArrayEquals("", 0, 1));
  28. }
  29. [TestMethod]
  30. public void ReadMalformedStringSizeLargerThanCapacity()
  31. {
  32. Assert.ThrowsException<PlcException>(() => S7WString.FromByteArray(new byte[] { 0, 3, 0, 5, 0, 0x00, 0x41, 0x00, 0x41, 0x00, 0x41}));
  33. }
  34. [TestMethod]
  35. public void ReadMalformedStringCapacityTooLarge()
  36. {
  37. Assert.ThrowsException<ArgumentException>(() => AssertToByteArrayAndBackEquals("", 20000, 0));
  38. }
  39. [TestMethod]
  40. public void ReadA()
  41. {
  42. AssertFromByteArrayEquals("A", 0, 1, 0, 1, 0x00, 0x41);
  43. }
  44. [TestMethod]
  45. public void ReadAbc()
  46. {
  47. AssertFromByteArrayEquals("Abc", 0, 3, 0, 3, 0x00, 0x41, 0x00, 0x62, 0x00, 0x63);
  48. }
  49. [TestMethod]
  50. public void WriteNullWithReservedLengthZero()
  51. {
  52. Assert.ThrowsException<ArgumentNullException>(() => AssertToByteArrayAndBackEquals(null, 0, 0, 0, 0, 0));
  53. }
  54. [TestMethod]
  55. public void WriteEmptyStringWithReservedLengthZero()
  56. {
  57. AssertToByteArrayAndBackEquals("", 0, 0, 0, 0, 0);
  58. }
  59. [TestMethod]
  60. public void WriteAWithReservedLengthZero()
  61. {
  62. AssertToByteArrayAndBackEquals("", 0, 0, 0, 0, 0);
  63. }
  64. [TestMethod]
  65. public void WriteNullWithReservedLengthOne()
  66. {
  67. Assert.ThrowsException<ArgumentNullException>(() => AssertToByteArrayAndBackEquals(null, 1, 0, 1 , 0, 0));
  68. }
  69. [TestMethod]
  70. public void WriteEmptyStringWithReservedLengthOne()
  71. {
  72. AssertToByteArrayAndBackEquals("", 1, 0, 1, 0, 0, 0, 0);
  73. }
  74. [TestMethod]
  75. public void WriteAWithReservedLengthOne()
  76. {
  77. AssertToByteArrayAndBackEquals("A", 1, 0, 1, 0, 1, 0x00, 0x41);
  78. }
  79. [TestMethod]
  80. public void WriteAWithReservedLengthTwo()
  81. {
  82. AssertToByteArrayAndBackEquals("A", 2, 0, 2, 0, 1, 0x00, 0x41, 0, 0);
  83. }
  84. [TestMethod]
  85. public void WriteAbcWithStringLargerThanReservedLength()
  86. {
  87. Assert.ThrowsException<ArgumentException>(() => S7WString.ToByteArray("Abc", 2));
  88. }
  89. [TestMethod]
  90. public void WriteAbcWithReservedLengthThree()
  91. {
  92. AssertToByteArrayAndBackEquals("Abc", 3, 0, 3, 0, 3, 0x00, 0x41, 0x00, 0x62, 0x00, 0x63);
  93. }
  94. [TestMethod]
  95. public void WriteAbcWithReservedLengthFour()
  96. {
  97. AssertToByteArrayAndBackEquals("Abc", 4, 0, 4, 0, 3, 0x00, 0x41, 0x00, 0x62, 0x00, 0x63, 0 , 0);
  98. }
  99. private static void AssertFromByteArrayEquals(string expected, params byte[] bytes)
  100. {
  101. var convertedString = S7WString.FromByteArray(bytes);
  102. Assert.AreEqual(expected, convertedString);
  103. }
  104. [TestMethod]
  105. public void OddS7WStringByteLength()
  106. {
  107. AssertVarTypeToByteLength(VarType.S7WString, 1, 6);
  108. }
  109. [TestMethod]
  110. public void EvenS7WStringByteLength()
  111. {
  112. AssertVarTypeToByteLength(VarType.S7WString, 2, 8);
  113. }
  114. private static void AssertToByteArrayAndBackEquals(string value, int reservedLength, params byte[] expected)
  115. {
  116. var convertedData = S7WString.ToByteArray(value, reservedLength);
  117. CollectionAssert.AreEqual(expected, convertedData);
  118. var convertedBack = S7WString.FromByteArray(convertedData);
  119. Assert.AreEqual(value, convertedBack);
  120. }
  121. private void AssertVarTypeToByteLength(VarType varType, int count, int expectedByteLength)
  122. {
  123. var byteLength = Plc.VarTypeToByteLength(varType, count);
  124. Assert.AreEqual(expectedByteLength, byteLength);
  125. }
  126. }
  127. }