PageRenderTime 44ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/base/Applications/Tests/UnitTestTest/UnitTestTest.cs

#
C# | 215 lines | 164 code | 32 blank | 19 comment | 4 complexity | c48e823555d3189d4d4c2b6ce5e27cda MD5 | raw file
  1. ///////////////////////////////////////////////////////////////////////////////
  2. //
  3. // Microsoft Research Singularity
  4. //
  5. // Copyright (c) Microsoft Corporation. All rights reserved.
  6. //
  7. // Note: A test of the Unit testing code.
  8. //
  9. using System;
  10. using System.Collections;
  11. using System.Threading;
  12. using Microsoft.Singularity.UnitTest;
  13. using Microsoft.Singularity.Channels;
  14. using Microsoft.Contracts;
  15. using Microsoft.SingSharp.Reflection;
  16. using Microsoft.Singularity.Applications;
  17. using Microsoft.Singularity.Io;
  18. using Microsoft.Singularity.Configuration;
  19. [assembly: Transform(typeof(ApplicationResourceTransform))]
  20. namespace Microsoft.Singularity.Applications
  21. {
  22. [ConsoleCategory(DefaultAction=true)]
  23. internal class Parameters {
  24. [InputEndpoint("data")]
  25. public readonly TRef<UnicodePipeContract.Exp:READY> Stdin;
  26. [OutputEndpoint("data")]
  27. public readonly TRef<UnicodePipeContract.Imp:READY> Stdout;
  28. reflective internal Parameters();
  29. internal int AppMain() {
  30. return Test.AppMain(this);
  31. }
  32. }
  33. public class Test
  34. {
  35. private static void SimpleChecks()
  36. {
  37. Assert.True(true, "Expected true");
  38. Assert.False(false, "Expected false");
  39. Assert.Null(null, "Expected null");
  40. object low = (object)0;
  41. object high = (object)1;
  42. Assert.NonNull(low, "Expected non-null value");
  43. Assert.SameObject(low, low, "Expected same object");
  44. Assert.NotSameObject(low, new object(),
  45. "Expected different objects");
  46. Assert.Equal(low, low,
  47. "Expected equal objects");
  48. Assert.NotEqual(low, high, "Expected unequal objects.");
  49. }
  50. // -------------------------------------------------------------------
  51. // Byte tests
  52. //
  53. // Generics would be handy here,
  54. // but are not crucial as the overloads for binary
  55. // operators are script generated so we expect them to work
  56. // irrespective of type.
  57. internal delegate void BinaryOp(byte a, byte b, string msg);
  58. internal class WrappedBinaryOp
  59. {
  60. byte first;
  61. byte second;
  62. string! message;
  63. BinaryOp! op;
  64. internal WrappedBinaryOp(byte first,
  65. byte second,
  66. string! message,
  67. BinaryOp! op)
  68. {
  69. this.first = first;
  70. this.second = second;
  71. this.message = message;
  72. this.op = op;
  73. }
  74. public void Dispatch()
  75. {
  76. op(first, second, message);
  77. }
  78. }
  79. private static void TestBadByteOperations()
  80. {
  81. ArrayList ops = new ArrayList();
  82. int oldPasses = Assert.Passes;
  83. int oldFailures = Assert.Failures;
  84. ops.Add(new WrappedBinaryOp(0, 1, "Equal",
  85. new BinaryOp(Assert.Equal)));
  86. ops.Add(new WrappedBinaryOp(0, 1, "Greater",
  87. new BinaryOp(Assert.Greater)));
  88. ops.Add(new WrappedBinaryOp(0, 0, "Greater",
  89. new BinaryOp(Assert.Greater)));
  90. ops.Add(new WrappedBinaryOp(0, 1, "GreaterOrEqual",
  91. new BinaryOp(Assert.GreaterOrEqual)));
  92. ops.Add(new WrappedBinaryOp(1, 0, "Less",
  93. new BinaryOp(Assert.Less)));
  94. ops.Add(new WrappedBinaryOp(0, 0, "Less",
  95. new BinaryOp(Assert.Less)));
  96. ops.Add(new WrappedBinaryOp(1, 0, "LessOrEqual",
  97. new BinaryOp(Assert.LessOrEqual)));
  98. foreach (WrappedBinaryOp! op in ops) {
  99. bool missedException = false;
  100. try {
  101. op.Dispatch();
  102. missedException = true;
  103. }
  104. catch (FailedAssertionException) {
  105. missedException = false;
  106. }
  107. if (missedException) {
  108. throw new
  109. FailedAssertionException("Unexpected test pass.");
  110. }
  111. }
  112. Assert.Equal(oldPasses, Assert.Passes, "Assert Passes");
  113. Assert.Equal(oldFailures + 7, Assert.Failures, "Assert Failures");
  114. }
  115. private static void TestByteOperations()
  116. {
  117. int oldPasses = Assert.Passes;
  118. Assert.Equal((byte)0, (byte)0, "byte Equal Test");
  119. Assert.Greater((byte)1, (byte)0, "byte Greater Test");
  120. Assert.GreaterOrEqual((byte)1, (byte)0,
  121. "byte GreaterOrEqual Test");
  122. Assert.GreaterOrEqual((byte)0, (byte)0,
  123. "byte GreaterOrEqual Test 2");
  124. Assert.Less((byte)0, (byte)1, "byte Less Test");
  125. Assert.LessOrEqual((byte)0, (byte)1,
  126. "byte LessOrEqual Test");
  127. Assert.LessOrEqual((byte)0, (byte)0,
  128. "byte LessOrEqual Test 2");
  129. Assert.Equal(oldPasses + 7, Assert.Passes, "Assert Passes");
  130. }
  131. private static void TestWatchDog()
  132. {
  133. const int deferrals = 20;
  134. TimeSpan deferPeriod = TimeSpan.FromMilliseconds(250);
  135. TimeSpan halfDeferPeriod =
  136. TimeSpan.FromTicks(deferPeriod.Ticks / 2);
  137. WatchDogTimer wd = new WatchDogTimer(deferPeriod);
  138. wd.Start();
  139. Assert.True(wd.Running, "Watch dog failed to start");
  140. wd.Stop();
  141. Assert.False(wd.Running, "Watch dog failed to stop");
  142. wd.Start();
  143. Assert.True(wd.Running, "Watch dog failed to restart");
  144. Thread.Sleep(halfDeferPeriod);
  145. for (int i = 0; i < deferrals; i++) {
  146. wd.Defer();
  147. Thread.Sleep(halfDeferPeriod);
  148. Assert.True(wd.Running,
  149. "Watch dog timer stopped unexpectedly.");
  150. }
  151. // About to deliberately timeout watchdog
  152. // so announce upcoming assertion failure from watchdog.
  153. UnitTest.ExpectNextAssertionFails();
  154. Thread.Sleep(deferPeriod);
  155. Thread.Sleep(deferPeriod);
  156. UnitTest.ExpectNextAssertionPasses();
  157. Assert.False(wd.Running, "Watch dog timer did not timeout.");
  158. wd.Start();
  159. wd.Start();
  160. wd.Start();
  161. wd.Stop();
  162. wd.Stop();
  163. }
  164. internal static int AppMain(Parameters! config)
  165. {
  166. UnitTest.Add("SimpleChecks",
  167. new UnitTest.TestDelegate(SimpleChecks));
  168. UnitTest.Add("TestByteOperations",
  169. new UnitTest.TestDelegate(TestByteOperations));
  170. UnitTest.Add("TestBadByteOperations",
  171. new UnitTest.TestDelegate(TestBadByteOperations));
  172. if (UnitTest.Run(true) != UnitTest.Result.Passed)
  173. return -1;
  174. UnitTest.Clear();
  175. // The following test "fails" because
  176. // it needs to see the watchdog timeout.
  177. UnitTest.Add("TestWatchDog",
  178. new UnitTest.TestDelegate(TestWatchDog));
  179. UnitTest.Run(true);
  180. return 0;
  181. }
  182. }
  183. }