DateTimeLongTests.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. namespace S7.Net.UnitTest.TypeTests
  5. {
  6. public static class DateTimeLongTests
  7. {
  8. private static readonly DateTime SampleDateTime = new DateTime(1993, 12, 25, 8, 12, 34, 567);
  9. private static readonly byte[] SampleByteArray = {0x07, 0xC9, 0x0C, 0x19, 0x07, 0x08, 0x0C, 0x22, 0x21, 0xCB, 0xBB, 0xC0 };
  10. private static readonly byte[] SpecMinByteArray =
  11. {
  12. 0x07, 0xB2, 0x01, 0x01, (byte) (int) (Types.DateTimeLong.SpecMinimumDateTime.DayOfWeek + 1), 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
  13. };
  14. private static readonly byte[] SpecMaxByteArray =
  15. {
  16. 0x08, 0xD6, 0x04, 0x0B, (byte) (int) (Types.DateTimeLong.SpecMaximumDateTime.DayOfWeek + 1), 0x17, 0x2F, 0x10, 0x32, 0xE7, 0x01, 0x80
  17. };
  18. [TestClass]
  19. public class FromByteArray
  20. {
  21. [TestMethod]
  22. public void Sample()
  23. {
  24. AssertFromByteArrayEquals(SampleDateTime, SampleByteArray);
  25. }
  26. [TestMethod]
  27. public void SpecMinimum()
  28. {
  29. AssertFromByteArrayEquals(Types.DateTimeLong.SpecMinimumDateTime, SpecMinByteArray);
  30. }
  31. [TestMethod]
  32. public void SpecMaximum()
  33. {
  34. AssertFromByteArrayEquals(Types.DateTimeLong.SpecMaximumDateTime, SpecMaxByteArray);
  35. }
  36. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  37. public void ThrowsOnLessThan12Bytes()
  38. {
  39. Types.DateTimeLong.FromByteArray(new byte[11]);
  40. }
  41. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  42. public void ThrowsOnMoreTHan12Bytes()
  43. {
  44. Types.DateTimeLong.FromByteArray(new byte[13]);
  45. }
  46. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  47. public void ThrowsOnInvalidYear()
  48. {
  49. Types.DateTimeLong.FromByteArray(MutateSample(0, 0xa0));
  50. }
  51. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  52. public void ThrowsOnZeroMonth()
  53. {
  54. Types.DateTimeLong.FromByteArray(MutateSample(2, 0x00));
  55. }
  56. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  57. public void ThrowsOnTooLargeMonth()
  58. {
  59. Types.DateTimeLong.FromByteArray(MutateSample(2, 0x13));
  60. }
  61. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  62. public void ThrowsOnZeroDay()
  63. {
  64. Types.DateTimeLong.FromByteArray(MutateSample(3, 0x00));
  65. }
  66. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  67. public void ThrowsOnTooLargeDay()
  68. {
  69. Types.DateTimeLong.FromByteArray(MutateSample(3, 0x32));
  70. }
  71. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  72. public void ThrowsOnInvalidHour()
  73. {
  74. Types.DateTimeLong.FromByteArray(MutateSample(5, 0x24));
  75. }
  76. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  77. public void ThrowsOnInvalidMinute()
  78. {
  79. Types.DateTimeLong.FromByteArray(MutateSample(6, 0x60));
  80. }
  81. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  82. public void ThrowsOnInvalidSecond()
  83. {
  84. Types.DateTimeLong.FromByteArray(MutateSample(7, 0x60));
  85. }
  86. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  87. public void ThrowsOnInvalidNanosecondsFirstDigit()
  88. {
  89. Types.DateTimeLong.FromByteArray(MutateSample(8, 0x3B));
  90. }
  91. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  92. public void ThrowsOnZeroDayOfWeek()
  93. {
  94. Types.DateTimeLong.FromByteArray(MutateSample(4, 0));
  95. }
  96. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  97. public void ThrowsOnTooLargeDayOfWeek()
  98. {
  99. Types.DateTimeLong.FromByteArray(MutateSample(4, 8));
  100. }
  101. private static void AssertFromByteArrayEquals(DateTime expected, params byte[] bytes)
  102. {
  103. Assert.AreEqual(expected, Types.DateTimeLong.FromByteArray(bytes));
  104. }
  105. private static byte[] MutateSample(int index, byte value) =>
  106. SampleByteArray.Select((b, i) => i == index ? value : b).ToArray();
  107. }
  108. [TestClass]
  109. public class ToByteArray
  110. {
  111. [TestMethod]
  112. public void Sample()
  113. {
  114. AssertToByteArrayEquals(SampleDateTime, SampleByteArray);
  115. }
  116. [TestMethod]
  117. public void SpecMinimum()
  118. {
  119. AssertToByteArrayEquals(Types.DateTimeLong.SpecMinimumDateTime, SpecMinByteArray);
  120. }
  121. [TestMethod]
  122. public void SpecMaximum()
  123. {
  124. AssertToByteArrayEquals(Types.DateTimeLong.SpecMaximumDateTime, SpecMaxByteArray);
  125. }
  126. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  127. public void ThrowsOnTimeBeforeSpecMinimum()
  128. {
  129. Types.DateTimeLong.ToByteArray(new DateTime(1950, 1, 1));
  130. }
  131. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  132. public void ThrowsOnTimeAfterSpecMaximum()
  133. {
  134. Types.DateTimeLong.ToByteArray(new DateTime(2790, 1, 1));
  135. }
  136. private static void AssertToByteArrayEquals(DateTime value, params byte[] expected)
  137. {
  138. CollectionAssert.AreEqual(expected, Types.DateTimeLong.ToByteArray(value));
  139. }
  140. }
  141. }
  142. }