DateTimeTests.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. using System;
  2. using System.Linq;
  3. using Microsoft.VisualStudio.TestTools.UnitTesting;
  4. namespace S7.Net.UnitTest.TypeTests
  5. {
  6. public static class DateTimeTests
  7. {
  8. private static readonly DateTime SampleDateTime = new DateTime(1993, 12, 25, 8, 12, 34, 567);
  9. private static readonly byte[] SampleByteArray = {0x93, 0x12, 0x25, 0x08, 0x12, 0x34, 0x56, 7 << 4 | 7};
  10. private static readonly byte[] SpecMinByteArray =
  11. {
  12. 0x90, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00, (byte) (int) (Types.DateTime.SpecMinimumDateTime.DayOfWeek + 1)
  13. };
  14. private static readonly byte[] SpecMaxByteArray =
  15. {
  16. 0x89, 0x12, 0x31, 0x23, 0x59, 0x59, 0x99, (byte) (9 << 4 | (int) (Types.DateTime.SpecMaximumDateTime.DayOfWeek + 1))
  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.DateTime.SpecMinimumDateTime, SpecMinByteArray);
  30. }
  31. [TestMethod]
  32. public void SpecMaximum()
  33. {
  34. AssertFromByteArrayEquals(Types.DateTime.SpecMaximumDateTime, SpecMaxByteArray);
  35. }
  36. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  37. public void ThrowsOnLessThan8Bytes()
  38. {
  39. Types.DateTime.FromByteArray(new byte[7]);
  40. }
  41. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  42. public void ThrowsOnMoreTHan8Bytes()
  43. {
  44. Types.DateTime.FromByteArray(new byte[9]);
  45. }
  46. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  47. public void ThrowsOnInvalidYear()
  48. {
  49. Types.DateTime.FromByteArray(MutateSample(0, 0xa0));
  50. }
  51. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  52. public void ThrowsOnZeroMonth()
  53. {
  54. Types.DateTime.FromByteArray(MutateSample(1, 0x00));
  55. }
  56. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  57. public void ThrowsOnTooLargeMonth()
  58. {
  59. Types.DateTime.FromByteArray(MutateSample(1, 0x13));
  60. }
  61. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  62. public void ThrowsOnZeroDay()
  63. {
  64. Types.DateTime.FromByteArray(MutateSample(2, 0x00));
  65. }
  66. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  67. public void ThrowsOnTooLargeDay()
  68. {
  69. Types.DateTime.FromByteArray(MutateSample(2, 0x32));
  70. }
  71. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  72. public void ThrowsOnInvalidHour()
  73. {
  74. Types.DateTime.FromByteArray(MutateSample(3, 0x24));
  75. }
  76. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  77. public void ThrowsOnInvalidMinute()
  78. {
  79. Types.DateTime.FromByteArray(MutateSample(4, 0x60));
  80. }
  81. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  82. public void ThrowsOnInvalidSecond()
  83. {
  84. Types.DateTime.FromByteArray(MutateSample(5, 0x60));
  85. }
  86. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  87. public void ThrowsOnInvalidFirstTwoMillisecondDigits()
  88. {
  89. Types.DateTime.FromByteArray(MutateSample(6, 0xa0));
  90. }
  91. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  92. public void ThrowsOnInvalidThirdMillisecondDigit()
  93. {
  94. Types.DateTime.FromByteArray(MutateSample(7, 10 << 4));
  95. }
  96. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  97. public void ThrowsOnZeroDayOfWeek()
  98. {
  99. Types.DateTime.FromByteArray(MutateSample(7, 0));
  100. }
  101. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  102. public void ThrowsOnTooLargeDayOfWeek()
  103. {
  104. Types.DateTime.FromByteArray(MutateSample(7, 8));
  105. }
  106. private static void AssertFromByteArrayEquals(DateTime expected, params byte[] bytes)
  107. {
  108. Assert.AreEqual(expected, Types.DateTime.FromByteArray(bytes));
  109. }
  110. private static byte[] MutateSample(int index, byte value) =>
  111. SampleByteArray.Select((b, i) => i == index ? value : b).ToArray();
  112. }
  113. [TestClass]
  114. public class ToByteArray
  115. {
  116. [TestMethod]
  117. public void Sample()
  118. {
  119. AssertToByteArrayEquals(SampleDateTime, SampleByteArray);
  120. }
  121. [TestMethod]
  122. public void SpecMinimum()
  123. {
  124. AssertToByteArrayEquals(Types.DateTime.SpecMinimumDateTime, SpecMinByteArray);
  125. }
  126. [TestMethod]
  127. public void SpecMaximum()
  128. {
  129. AssertToByteArrayEquals(Types.DateTime.SpecMaximumDateTime, SpecMaxByteArray);
  130. }
  131. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  132. public void ThrowsOnTimeBeforeSpecMinimum()
  133. {
  134. Types.DateTime.ToByteArray(new DateTime(1970, 1, 1));
  135. }
  136. [TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
  137. public void ThrowsOnTimeAfterSpecMaximum()
  138. {
  139. Types.DateTime.ToByteArray(new DateTime(2090, 1, 1));
  140. }
  141. private static void AssertToByteArrayEquals(DateTime value, params byte[] expected)
  142. {
  143. CollectionAssert.AreEqual(expected, Types.DateTime.ToByteArray(value));
  144. }
  145. }
  146. }
  147. }