Assert() public static method

public static Assert ( bool condition ) : void
condition bool
return void
Esempio n. 1
0
 ////////////////////////////////////////////////////////////////////////////
 //
 // Format the positive integer value to a string and perfix with assigned
 // length of leading zero.
 //
 // Parameters:
 //  value: The value to format
 //  len: The maximum length for leading zero.
 //  If the digits of the value is greater than len, no leading zero is added.
 //
 // Notes:
 //  The function can format to Int32.MaxValue.
 //
 ////////////////////////////////////////////////////////////////////////////
 internal static void FormatDigits(StringBuilder outputBuffer, int value, int len)
 {
     Contract.Assert(value >= 0, "DateTimeFormat.FormatDigits(): value >= 0");
     FormatDigits(outputBuffer, value, len, false);
 }
Esempio n. 2
0
 internal unsafe static void Memcpy(byte *dest, byte *src, int len)
 {
     Contract.Assert(len >= 0, "Negative length in memcopy!");
     Memmove(dest, src, (uint)len);
 }
Esempio n. 3
0
        [System.Security.SecurityCritical]  // auto-generated
        internal unsafe static int IndexOfByte(byte *src, byte value, int index, int count)
        {
            Contract.Assert(src != null, "src should not be null");

            byte *pByte = src + index;

            // Align up the pointer to sizeof(int).
            while (((int)pByte & 3) != 0)
            {
                if (count == 0)
                {
                    return(-1);
                }
                else if (*pByte == value)
                {
                    return((int)(pByte - src));
                }

                count--;
                pByte++;
            }

            // Fill comparer with value byte for comparisons
            //
            // comparer = 0/0/value/value
            uint comparer = (((uint)value << 8) + (uint)value);

            // comparer = value/value/value/value
            comparer = (comparer << 16) + comparer;

            // Run through buffer until we hit a 4-byte section which contains
            // the byte we're looking for or until we exhaust the buffer.
            while (count > 3)
            {
                // Test the buffer for presence of value. comparer contains the byte
                // replicated 4 times.
                uint t1 = *(uint *)pByte;
                t1 = t1 ^ comparer;
                uint t2 = 0x7efefeff + t1;
                t1 = t1 ^ 0xffffffff;
                t1 = t1 ^ t2;
                t1 = t1 & 0x81010100;

                // if t1 is zero then these 4-bytes don't contain a match
                if (t1 != 0)
                {
                    // We've found a match for value, figure out which position it's in.
                    int foundIndex = (int)(pByte - src);
                    if (pByte[0] == value)
                    {
                        return(foundIndex);
                    }
                    else if (pByte[1] == value)
                    {
                        return(foundIndex + 1);
                    }
                    else if (pByte[2] == value)
                    {
                        return(foundIndex + 2);
                    }
                    else if (pByte[3] == value)
                    {
                        return(foundIndex + 3);
                    }
                }

                count -= 4;
                pByte += 4;
            }

            // Catch any bytes that might be left at the tail of the buffer
            while (count > 0)
            {
                if (*pByte == value)
                {
                    return((int)(pByte - src));
                }

                count--;
                pByte++;
            }

            // If we don't have a match return -1;
            return(-1);
        }
Esempio n. 4
0
        //
        // This function will convert an ExceptionResource enum value to the resource string.
        //
        internal static string GetResourceName(ExceptionResource resource)
        {
            string resourceName = null;

            switch (resource)
            {
            case ExceptionResource.Argument_ImplementIComparable:
                resourceName = "Argument_ImplementIComparable";
                break;

            case ExceptionResource.Argument_AddingDuplicate:
                resourceName = "Argument_AddingDuplicate";
                break;

            case ExceptionResource.ArgumentOutOfRange_BiggerThanCollection:
                resourceName = "ArgumentOutOfRange_BiggerThanCollection";
                break;

            case ExceptionResource.ArgumentOutOfRange_Count:
                resourceName = "ArgumentOutOfRange_Count";
                break;

            case ExceptionResource.ArgumentOutOfRange_Index:
                resourceName = "ArgumentOutOfRange_Index";
                break;

            case ExceptionResource.ArgumentOutOfRange_InvalidThreshold:
                resourceName = "ArgumentOutOfRange_InvalidThreshold";
                break;

            case ExceptionResource.ArgumentOutOfRange_ListInsert:
                resourceName = "ArgumentOutOfRange_ListInsert";
                break;

            case ExceptionResource.ArgumentOutOfRange_NeedNonNegNum:
                resourceName = "ArgumentOutOfRange_NeedNonNegNum";
                break;

            case ExceptionResource.ArgumentOutOfRange_SmallCapacity:
                resourceName = "ArgumentOutOfRange_SmallCapacity";
                break;

            case ExceptionResource.Arg_ArrayPlusOffTooSmall:
                resourceName = "Arg_ArrayPlusOffTooSmall";
                break;

            case ExceptionResource.Arg_RankMultiDimNotSupported:
                resourceName = "Arg_RankMultiDimNotSupported";
                break;

            case ExceptionResource.Arg_NonZeroLowerBound:
                resourceName = "Arg_NonZeroLowerBound";
                break;

            case ExceptionResource.Argument_InvalidArrayType:
                resourceName = "Argument_InvalidArrayType";
                break;

            case ExceptionResource.Argument_InvalidOffLen:
                resourceName = "Argument_InvalidOffLen";
                break;

            case ExceptionResource.Argument_ItemNotExist:
                resourceName = "Argument_ItemNotExist";
                break;

            case ExceptionResource.InvalidOperation_CannotRemoveFromStackOrQueue:
                resourceName = "InvalidOperation_CannotRemoveFromStackOrQueue";
                break;

            case ExceptionResource.InvalidOperation_EmptyQueue:
                resourceName = "InvalidOperation_EmptyQueue";
                break;

            case ExceptionResource.InvalidOperation_EnumOpCantHappen:
                resourceName = "InvalidOperation_EnumOpCantHappen";
                break;

            case ExceptionResource.InvalidOperation_EnumFailedVersion:
                resourceName = "InvalidOperation_EnumFailedVersion";
                break;

            case ExceptionResource.InvalidOperation_EmptyStack:
                resourceName = "InvalidOperation_EmptyStack";
                break;

            case ExceptionResource.InvalidOperation_EnumNotStarted:
                resourceName = "InvalidOperation_EnumNotStarted";
                break;

            case ExceptionResource.InvalidOperation_EnumEnded:
                resourceName = "InvalidOperation_EnumEnded";
                break;

            case ExceptionResource.NotSupported_KeyCollectionSet:
                resourceName = "NotSupported_KeyCollectionSet";
                break;

            case ExceptionResource.NotSupported_ReadOnlyCollection:
                resourceName = "NotSupported_ReadOnlyCollection";
                break;

            case ExceptionResource.NotSupported_ValueCollectionSet:
                resourceName = "NotSupported_ValueCollectionSet";
                break;


            case ExceptionResource.NotSupported_SortedListNestedWrite:
                resourceName = "NotSupported_SortedListNestedWrite";
                break;


            case ExceptionResource.Serialization_InvalidOnDeser:
                resourceName = "Serialization_InvalidOnDeser";
                break;

            case ExceptionResource.Serialization_MissingKeys:
                resourceName = "Serialization_MissingKeys";
                break;

            case ExceptionResource.Serialization_NullKey:
                resourceName = "Serialization_NullKey";
                break;

            case ExceptionResource.Argument_InvalidType:
                resourceName = "Argument_InvalidType";
                break;

            case ExceptionResource.Argument_InvalidArgumentForComparison:
                resourceName = "Argument_InvalidArgumentForComparison";
                break;

            case ExceptionResource.InvalidOperation_NoValue:
                resourceName = "InvalidOperation_NoValue";
                break;

            case ExceptionResource.InvalidOperation_RegRemoveSubKey:
                resourceName = "InvalidOperation_RegRemoveSubKey";
                break;

            case ExceptionResource.Arg_RegSubKeyAbsent:
                resourceName = "Arg_RegSubKeyAbsent";
                break;

            case ExceptionResource.Arg_RegSubKeyValueAbsent:
                resourceName = "Arg_RegSubKeyValueAbsent";
                break;

            case ExceptionResource.Arg_RegKeyDelHive:
                resourceName = "Arg_RegKeyDelHive";
                break;

            case ExceptionResource.Security_RegistryPermission:
                resourceName = "Security_RegistryPermission";
                break;

            case ExceptionResource.Arg_RegSetStrArrNull:
                resourceName = "Arg_RegSetStrArrNull";
                break;

            case ExceptionResource.Arg_RegSetMismatchedKind:
                resourceName = "Arg_RegSetMismatchedKind";
                break;

            case ExceptionResource.UnauthorizedAccess_RegistryNoWrite:
                resourceName = "UnauthorizedAccess_RegistryNoWrite";
                break;

            case ExceptionResource.ObjectDisposed_RegKeyClosed:
                resourceName = "ObjectDisposed_RegKeyClosed";
                break;

            case ExceptionResource.Arg_RegKeyStrLenBug:
                resourceName = "Arg_RegKeyStrLenBug";
                break;

            case ExceptionResource.Argument_InvalidRegistryKeyPermissionCheck:
                resourceName = "Argument_InvalidRegistryKeyPermissionCheck";
                break;

            case ExceptionResource.NotSupported_InComparableType:
                resourceName = "NotSupported_InComparableType";
                break;

            case ExceptionResource.Argument_InvalidRegistryOptionsCheck:
                resourceName = "Argument_InvalidRegistryOptionsCheck";
                break;

            case ExceptionResource.Argument_InvalidRegistryViewCheck:
                resourceName = "Argument_InvalidRegistryViewCheck";
                break;

            default:
                Contract.Assert(false, "The enum value is not defined, please checked ExceptionArgumentName Enum.");
                return(string.Empty);
            }

            return(resourceName);
        }
Esempio n. 5
0
        //
        // This function will convert an ExceptionArgument enum value to the argument name string.
        //
        internal static string GetArgumentName(ExceptionArgument argument)
        {
            string argumentName = null;

            switch (argument)
            {
            case ExceptionArgument.array:
                argumentName = "array";
                break;

            case ExceptionArgument.arrayIndex:
                argumentName = "arrayIndex";
                break;

            case ExceptionArgument.capacity:
                argumentName = "capacity";
                break;

            case ExceptionArgument.collection:
                argumentName = "collection";
                break;

            case ExceptionArgument.list:
                argumentName = "list";
                break;

            case ExceptionArgument.converter:
                argumentName = "converter";
                break;

            case ExceptionArgument.count:
                argumentName = "count";
                break;

            case ExceptionArgument.dictionary:
                argumentName = "dictionary";
                break;

            case ExceptionArgument.dictionaryCreationThreshold:
                argumentName = "dictionaryCreationThreshold";
                break;

            case ExceptionArgument.index:
                argumentName = "index";
                break;

            case ExceptionArgument.info:
                argumentName = "info";
                break;

            case ExceptionArgument.key:
                argumentName = "key";
                break;

            case ExceptionArgument.match:
                argumentName = "match";
                break;

            case ExceptionArgument.obj:
                argumentName = "obj";
                break;

            case ExceptionArgument.queue:
                argumentName = "queue";
                break;

            case ExceptionArgument.stack:
                argumentName = "stack";
                break;

            case ExceptionArgument.startIndex:
                argumentName = "startIndex";
                break;

            case ExceptionArgument.value:
                argumentName = "value";
                break;

            case ExceptionArgument.name:
                argumentName = "name";
                break;

            case ExceptionArgument.mode:
                argumentName = "mode";
                break;

            case ExceptionArgument.item:
                argumentName = "item";
                break;

            case ExceptionArgument.options:
                argumentName = "options";
                break;

            case ExceptionArgument.view:
                argumentName = "view";
                break;

            case ExceptionArgument.sourceBytesToCopy:
                argumentName = "sourceBytesToCopy";
                break;

            default:
                Contract.Assert(false, "The enum value is not defined, please checked ExceptionArgumentName Enum.");
                return(string.Empty);
            }

            return(argumentName);
        }
Esempio n. 6
0
        [System.Security.SecuritySafeCritical]  // auto-generated
        public Variant(Object obj)
        {
            m_data1 = 0;
            m_data2 = 0;

            VarEnum vt = VarEnum.VT_EMPTY;

            if (obj is DateTime)
            {
                m_objref = null;
                m_flags  = CV_DATETIME;
                ulong ticks = (ulong)((DateTime)obj).Ticks;
                m_data1 = (int)ticks;
                m_data2 = (int)(ticks >> 32);
                return;
            }

            if (obj is String)
            {
                m_flags  = CV_STRING;
                m_objref = obj;
                return;
            }

            if (obj == null)
            {
                this = Empty;
                return;
            }
            if (obj == System.DBNull.Value)
            {
                this = DBNull;
                return;
            }
            if (obj == Type.Missing)
            {
                this = Missing;
                return;
            }

            if (obj is Array)
            {
                m_flags  = CV_OBJECT | ArrayBitMask;
                m_objref = obj;
                return;
            }

            // Compiler appeasement
            m_flags  = CV_EMPTY;
            m_objref = null;

            // Check to see if the object passed in is a wrapper object.
            if (obj is UnknownWrapper)
            {
                vt  = VarEnum.VT_UNKNOWN;
                obj = ((UnknownWrapper)obj).WrappedObject;
            }
            else if (obj is DispatchWrapper)
            {
                vt  = VarEnum.VT_DISPATCH;
                obj = ((DispatchWrapper)obj).WrappedObject;
            }
            else if (obj is ErrorWrapper)
            {
                vt  = VarEnum.VT_ERROR;
                obj = (Object)(((ErrorWrapper)obj).ErrorCode);
                Contract.Assert(obj != null, "obj != null");
            }
            else if (obj is CurrencyWrapper)
            {
                vt  = VarEnum.VT_CY;
                obj = (Object)(((CurrencyWrapper)obj).WrappedObject);
                Contract.Assert(obj != null, "obj != null");
            }
            else if (obj is BStrWrapper)
            {
                vt  = VarEnum.VT_BSTR;
                obj = (Object)(((BStrWrapper)obj).WrappedObject);
            }

            if (obj != null)
            {
                SetFieldsObject(obj);
            }

            // If the object passed in is one of the wrappers then set the VARIANT type.
            if (vt != VarEnum.VT_EMPTY)
            {
                m_flags |= ((int)vt << VTBitShift);
            }
        }
        public static String Normalize(this string value, NormalizationForm normalizationForm)
        {
            Contract.Requires(value != null);

            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            // we depend on Win32 last error when calling NormalizeString
            Interop.mincore.SetLastError(Interop.ERROR_SUCCESS);

            // Guess our buffer size first
            int iLength = Interop.mincore.NormalizeString((int)normalizationForm, value, value.Length, null, 0);

            int lastError = Marshal.GetLastWin32Error();

            // Could have an error (actually it'd be quite hard to have an error here)
            if ((lastError != Interop.ERROR_SUCCESS && lastError != Interop.LAST_ERROR_TRASH_VALUE) ||
                iLength < 0)
            {
                if (lastError == Interop.ERROR_INVALID_PARAMETER)
                {
                    throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, "value");
                }

                // We shouldn't really be able to get here..., guessing length is
                // a trivial math function...
                // Can't really be Out of Memory, but just in case:
                if (lastError == Interop.ERROR_NOT_ENOUGH_MEMORY)
                {
                    throw new OutOfMemoryException(SR.Arg_OutOfMemoryException);
                }

                // Who knows what happened?  Not us!
                throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError));
            }

            // Don't break for empty strings (only possible for D & KD and not really possible at that)
            if (iLength == 0)
            {
                return(String.Empty);
            }

            // Someplace to stick our buffer
            char[] cBuffer = null;

            for (; ;)
            {
                // (re)allocation buffer and normalize string
                cBuffer = new char[iLength];

                // Reset last error
                Interop.mincore.SetLastError(Interop.ERROR_SUCCESS);
                iLength   = Interop.mincore.NormalizeString((int)normalizationForm, value, value.Length, cBuffer, cBuffer.Length);
                lastError = Marshal.GetLastWin32Error();

                if (lastError == Interop.ERROR_SUCCESS || lastError == Interop.LAST_ERROR_TRASH_VALUE)
                {
                    break;
                }

                // Could have an error (actually it'd be quite hard to have an error here)
                switch (lastError)
                {
                // Do appropriate stuff for the individual errors:
                case Interop.ERROR_INSUFFICIENT_BUFFER:
                    iLength = Math.Abs(iLength);
                    Contract.Assert(iLength > cBuffer.Length, "Buffer overflow should have iLength > cBuffer.Length");
                    continue;

                case Interop.ERROR_INVALID_PARAMETER:
                case Interop.ERROR_NO_UNICODE_TRANSLATION:
                    // Illegal code point or order found.  Ie: FFFE or D800 D800, etc.
                    throw new ArgumentException(SR.Argument_InvalidCharSequenceNoIndex, "value");

                case Interop.ERROR_NOT_ENOUGH_MEMORY:
                    throw new OutOfMemoryException(SR.Arg_OutOfMemoryException);

                default:
                    // We shouldn't get here...
                    throw new InvalidOperationException(SR.Format(SR.UnknownError_Num, lastError));
                }
            }

            // Copy our buffer into our new string, which will be the appropriate size
            return(new String(cBuffer, 0, iLength));
        }
 public void Resuming()
 {
     Contract.Assert(isPaused);
     isPaused = false;
     ResumeEvent.Set();
 }