1
0

ScheduledItemTest.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reactive.Concurrency;
  5. using System.Reactive.Disposables;
  6. using Microsoft.Reactive.Testing;
  7. using Microsoft.VisualStudio.TestTools.UnitTesting;
  8. namespace ReactiveTests
  9. {
  10. [TestClass]
  11. public class ScheduledItemTest : ReactiveTest
  12. {
  13. [TestMethod]
  14. public void ArgumentChecking()
  15. {
  16. ReactiveAssert.Throws<ArgumentNullException>(() => new ScheduledItem<DateTimeOffset, int>(default(IScheduler), 42, (x, y) => Disposable.Empty, DateTimeOffset.Now));
  17. ReactiveAssert.Throws<ArgumentNullException>(() => new ScheduledItem<DateTimeOffset, int>(Scheduler.Default, 42, default(Func<IScheduler, int, IDisposable>), DateTimeOffset.Now));
  18. ReactiveAssert.Throws<ArgumentNullException>(() => new ScheduledItem<DateTimeOffset, int>(default(IScheduler), 42, (x, y) => Disposable.Empty, DateTimeOffset.Now, Comparer<DateTimeOffset>.Default));
  19. ReactiveAssert.Throws<ArgumentNullException>(() => new ScheduledItem<DateTimeOffset, int>(Scheduler.Default, 42, default(Func<IScheduler, int, IDisposable>), DateTimeOffset.Now, Comparer<DateTimeOffset>.Default));
  20. ReactiveAssert.Throws<ArgumentNullException>(() => new ScheduledItem<DateTimeOffset, int>(Scheduler.Default, 42, (x, y) => Disposable.Empty, DateTimeOffset.Now, default(IComparer<DateTimeOffset>)));
  21. }
  22. [TestMethod]
  23. public void Inequalities()
  24. {
  25. var si1 = new SI(42);
  26. var si2 = new SI(43);
  27. var si3 = new SI(42);
  28. Assert.IsTrue(si1 < si2);
  29. Assert.IsFalse(si1 < si3);
  30. Assert.IsTrue(si1 <= si2);
  31. Assert.IsTrue(si1 <= si3);
  32. Assert.IsTrue(si2 > si1);
  33. Assert.IsFalse(si3 > si1);
  34. Assert.IsTrue(si2 >= si1);
  35. Assert.IsTrue(si3 >= si1);
  36. Assert.IsTrue(si1.CompareTo(si2) < 0);
  37. Assert.IsTrue(si2.CompareTo(si1) > 0);
  38. Assert.IsTrue(si1.CompareTo(si1) == 0);
  39. Assert.IsTrue(si1.CompareTo(si3) == 0);
  40. Assert.IsTrue(si2 > null);
  41. Assert.IsTrue(si2 >= null);
  42. Assert.IsFalse(si2 < null);
  43. Assert.IsFalse(si2 <= null);
  44. Assert.IsTrue(null < si1);
  45. Assert.IsTrue(null <= si1);
  46. Assert.IsFalse(null > si1);
  47. Assert.IsFalse(null >= si1);
  48. Assert.IsTrue(si1.CompareTo(null) > 0);
  49. var si4 = new SI2(43, -1);
  50. var si5 = new SI2(44, -1);
  51. Assert.IsTrue(si4 > si1);
  52. Assert.IsTrue(si4 >= si1);
  53. Assert.IsTrue(si1 < si4);
  54. Assert.IsTrue(si1 <= si4);
  55. Assert.IsFalse(si4 > si2);
  56. Assert.IsTrue(si4 >= si2);
  57. Assert.IsFalse(si2 < si4);
  58. Assert.IsTrue(si2 <= si4);
  59. Assert.IsTrue(si5 > si4);
  60. Assert.IsTrue(si5 >= si4);
  61. Assert.IsFalse(si4 > si5);
  62. Assert.IsFalse(si4 >= si5);
  63. Assert.IsTrue(si4 < si5);
  64. Assert.IsTrue(si4 <= si5);
  65. Assert.IsFalse(si5 < si4);
  66. Assert.IsFalse(si5 <= si4);
  67. }
  68. [TestMethod]
  69. public void Equalities()
  70. {
  71. var si1 = new SI2(42, 123);
  72. var si2 = new SI2(42, 123);
  73. var si3 = new SI2(42, 321);
  74. var si4 = new SI2(43, 123);
  75. #pragma warning disable 1718
  76. Assert.IsFalse(si1 != si1);
  77. Assert.IsTrue(si1 == si1);
  78. #pragma warning restore 1718
  79. Assert.IsTrue(si1.Equals(si1));
  80. Assert.IsTrue(si1 != si2);
  81. Assert.IsFalse(si1 == si2);
  82. Assert.IsFalse(si1.Equals(si2));
  83. Assert.IsTrue(si1 != null);
  84. Assert.IsTrue(null != si1);
  85. Assert.IsFalse(si1 == null);
  86. Assert.IsFalse(null == si1);
  87. Assert.AreEqual(si1.GetHashCode(), si1.GetHashCode());
  88. Assert.AreNotEqual(si1.GetHashCode(), si2.GetHashCode());
  89. Assert.AreNotEqual(si1.GetHashCode(), si3.GetHashCode());
  90. }
  91. class SI : ScheduledItem<int>
  92. {
  93. public SI(int dueTime)
  94. : base(dueTime, Comparer<int>.Default)
  95. {
  96. }
  97. protected override IDisposable InvokeCore()
  98. {
  99. throw new NotImplementedException();
  100. }
  101. }
  102. class SI2 : ScheduledItem<int>
  103. {
  104. private readonly int _value;
  105. public SI2(int dueTime, int value)
  106. : base(dueTime, Comparer<int>.Default)
  107. {
  108. _value = value;
  109. }
  110. protected override IDisposable InvokeCore()
  111. {
  112. throw new NotImplementedException();
  113. }
  114. }
  115. }
  116. }