singrdk/base/Applications/Runtime/Full/System/Diagnostics/Assert.cs

100 lines
3.4 KiB
C#
Raw Normal View History

2008-03-05 09:52:00 -05:00
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
2008-11-17 18:29:00 -05:00
namespace System.Diagnostics
{
2008-03-05 09:52:00 -05:00
using System;
using System.Reflection;
using System.Runtime.CompilerServices;
using Microsoft.Singularity;
// Class which handles code asserts. Asserts are used to explicitly protect
// assumptions made in the code. In general if an assert fails, it indicates
// a program bug so is immediately called to the attention of the user.
//| <include path='docs/doc[@for="Assert"]/*' />
internal class Assert
{
private static AssertFilter[] ListOfFilters = null;
private static int iNumOfFilters = 0;
private static int iFilterArraySize = 0;
private static AssertFilter DefFil = new DefaultFilter();
// AddFilter adds a new assert filter. This replaces the current
// filter, unless the filter returns FailContinue.
//
//| <include path='docs/doc[@for="Assert.AddFilter"]/*' />
public static void AddFilter(AssertFilter filter)
{
2008-11-17 18:29:00 -05:00
if (iFilterArraySize <= iNumOfFilters) {
2008-03-05 09:52:00 -05:00
AssertFilter[] newFilterArray = new AssertFilter [iFilterArraySize+2];
if (iNumOfFilters > 0)
Array.Copy(ListOfFilters, newFilterArray, iNumOfFilters);
iFilterArraySize += 2;
ListOfFilters = newFilterArray;
}
ListOfFilters [iNumOfFilters++] = filter;
}
// Called when an assertion is being made.
//
//| <include path='docs/doc[@for="Assert.Check"]/*' />
public static void Check(bool condition, String conditionString, String message)
{
2008-11-17 18:29:00 -05:00
if (!condition) {
2008-03-05 09:52:00 -05:00
Fail (conditionString, message);
}
}
//| <include path='docs/doc[@for="Assert.Fail"]/*' />
public static void Fail(String conditionString, String message)
{
// Run through the list of filters backwards (the last filter in the list
// is the default filter. So we're guaranteed that there will be at least
// one filter to handle the assert.
int iTemp = iNumOfFilters;
2008-11-17 18:29:00 -05:00
while (iTemp > 0) {
2008-03-05 09:52:00 -05:00
AssertFilters iResult = ListOfFilters [--iTemp].AssertFailure (conditionString, message);
2008-11-17 18:29:00 -05:00
if (iResult == AssertFilters.FailDebug) {
2008-03-05 09:52:00 -05:00
#if SINGULARITY_KERNEL
DebugStub.Break();
#elif SINGULARITY_PROCESS
VTable.DebugBreak();
#endif
break;
}
else if (iResult == AssertFilters.FailTerminate)
#if SINGULARITY_KERNEL
Kernel.Shutdown(-1);
#elif SINGULARITY_PROCESS
AppRuntime.Stop(-1);
#endif
else if (iResult == AssertFilters.FailIgnore)
break;
// If none of the above, it means that the Filter returned FailContinue.
// So invoke the next filter.
}
}
// Called when an assertion fails.
//
//| <include path='docs/doc[@for="Assert.ShowDefaultAssertDialog"]/*' />
public static int ShowDefaultAssertDialog(String conditionString, String message) {
throw new Exception("System.Diagnostics.Assert.ShowDefaultAssertDialog not implemented in Bartok!");
}
}
}