Exemple #1
0
        }               // public string DisplayBitMaskFromMultilineString (2 OF 2)

        #endregion      // Public Instance Methods


        #region Private Instance Methods
        private void InitializeInstance(
            int pintTypeInfoIndex,
            object pbytBitMask,
            BitArray32.BitDisplayOrder penmBitDisplayOrder)
        {
            _bclTypeInfo = BitHelpers.s_abclintegertypeinfo [pintTypeInfoIndex];

            _objUnderlyingInteger = pbytBitMask;
            _enmBitDisplayOrder   = penmBitDisplayOrder;

            _strDecimalRepresentation = string.Format(
                FORMAT_FIRST_ITEM_AS_DECIMAL,
                _objUnderlyingInteger);
            _strHexadecimalRepresentation = BitArray32.FormatIntegerAsHex(_objUnderlyingInteger);

            _strTens = BitArray32.GetRulerTens(
                (BitArray32.BitCount)_bclTypeInfo.CapacityInBits,
                _enmBitDisplayOrder);
            _strUnits = BitArray32.GetRulerUnits(
                (BitArray32.BitCount)_bclTypeInfo.CapacityInBits,
                _enmBitDisplayOrder);
            _strArrayOfBits = string.Format(
                new WizardWrx.BitMaskFormat( ),
                _enmBitDisplayOrder == BitArray32.BitDisplayOrder.LowBitToHighBit
                                        ? FORMAT_FIRST_ITEM_AS_REVERSED_BIT_ARRAY :
                FORMAT_FIRST_ITEM_AS_BIT_ARRAY,
                _objUnderlyingInteger);
        }               // private void InitializeInstance
		};	// s_aint64Examples

        public static void Exercise ( string pstrReportFileName )
        {
            const string HEXADECIMAL_8 = @"X8";
			const string BITMASK_FORMAT_STRING = "G";

			int intTestNbr = TestBit32ArrayToStringOverload ( );
			TestBitArrayRulers ( ref intTestNbr );
			TestBitMaskAndRuler ( ref intTestNbr );

            Console.WriteLine ( @"Begin BitArray32TestStand Exercises" );
            Console.WriteLine ( @"{1}    Test # {0} - Display public constants.{1}" , ++intTestNbr , Environment.NewLine );

			Console.WriteLine ( 
				"        Bit  1      = {0} (Individual bits: {1})" ,			// Format control string
				BitArray32.BIT_01.ToString ( HEXADECIMAL_8 ) ,					// Format Item 0 = Bit as hexadecimal integer
				BitArray32.FormatIntegerAsBitArray (							// Format Item 1 = Bit as array of bits
					BitArray32.BIT_01 ,											// Specify the same bit, raw.
					BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );					// This should be passing a value of 1.

			Console.WriteLine ( @"        Bit  2      = {0} (Individual bits: {1})" , BitArray32.BIT_02.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_02 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  3      = {0} (Individual bits: {1})" , BitArray32.BIT_03.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_03 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  4      = {0} (Individual bits: {1})" , BitArray32.BIT_04.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_04 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  5      = {0} (Individual bits: {1})" , BitArray32.BIT_05.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_05 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  6      = {0} (Individual bits: {1})" , BitArray32.BIT_06.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_06 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  7      = {0} (Individual bits: {1})" , BitArray32.BIT_07.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_07 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  8      = {0} (Individual bits: {1})" , BitArray32.BIT_08.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_08 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit  9      = {0} (Individual bits: {1})" , BitArray32.BIT_09.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_09 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit 10      = {0} (Individual bits: {1})" , BitArray32.BIT_10.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_10 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );

			Console.WriteLine ( @"        Bit 11      = {0} (Individual bits: {1})" , BitArray32.BIT_11.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_11 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit 12      = {0} (Individual bits: {1})" , BitArray32.BIT_12.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_12 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit 13      = {0} (Individual bits: {1})" , BitArray32.BIT_13.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_13 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit 14      = {0} (Individual bits: {1})" , BitArray32.BIT_14.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_14 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 15      = {0} (Individual bits: {1})" , BitArray32.BIT_15.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_15 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 16      = {0} (Individual bits: {1})" , BitArray32.BIT_16.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_16 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 17      = {0} (Individual bits: {1})" , BitArray32.BIT_17.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_17 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 18      = {0} (Individual bits: {1})" , BitArray32.BIT_18.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_18 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 19      = {0} (Individual bits: {1})" , BitArray32.BIT_19.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_19 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 20      = {0} (Individual bits: {1})" , BitArray32.BIT_20.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_20 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );

			Console.WriteLine ( @"        Bit 21      = {0} (Individual bits: {1})" , BitArray32.BIT_21.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_21 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        Bit 22      = {0} (Individual bits: {1})" , BitArray32.BIT_22.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_22 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 23      = {0} (Individual bits: {1})" , BitArray32.BIT_23.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_23 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 24      = {0} (Individual bits: {1})" , BitArray32.BIT_24.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_24 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 25      = {0} (Individual bits: {1})" , BitArray32.BIT_25.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_25 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 26      = {0} (Individual bits: {1})" , BitArray32.BIT_26.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_26 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 27      = {0} (Individual bits: {1})" , BitArray32.BIT_27.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_27 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 28      = {0} (Individual bits: {1})" , BitArray32.BIT_28.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_28 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 29      = {0} (Individual bits: {1})" , BitArray32.BIT_29.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_29 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 30      = {0} (Individual bits: {1})" , BitArray32.BIT_30.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_30 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );

            Console.WriteLine ( @"        Bit 31      = {0} (Individual bits: {1})" , BitArray32.BIT_31.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_31 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
            Console.WriteLine ( @"        Bit 32      = {0} (Individual bits: {1})" , BitArray32.BIT_32.ToString ( HEXADECIMAL_8 ) , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_32 , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );

			Console.WriteLine ( @"        BIT_NBR_MIN = {0} (Individual bits: {1})" , BitArray32.BIT_NBR_MIN , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_NBR_MIN , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );
			Console.WriteLine ( @"        BIT_NBR_MAX = {0} (Individual bits: {1})" , BitArray32.BIT_NBR_MAX , BitArray32.FormatIntegerAsBitArray ( BitArray32.BIT_NBR_MAX , BitArray32.MINIMUM_BIT_GROUP_IN_FORMAT ) );

			Console.WriteLine ( @"        OFF         = {0}" , BitArray32.OFF );
			Console.WriteLine ( @"        ON          = {0}" , BitArray32.ON );

            Console.WriteLine ( @"{1}    Test # {0} - Default Constructor.{1}" , ++intTestNbr , Environment.NewLine );

            BitArray32 Mask1 = new BitArray32 ( );
			Console.WriteLine ( @"        Initial value of default BitArray32 = {0} (Individual bits: {1})" , Mask1.ToString ( BITMASK_FORMAT_STRING ) , Mask1.ToString ( "B4" ) );

            Mask1.BitOn ( 10 );
			Console.WriteLine ( @"        Bit 10 was turned ON. Mask1 = {0} (Individual bits: {1})" , Mask1.ToString ( BITMASK_FORMAT_STRING ) , Mask1.ToString ( "B4" ) );

            Console.WriteLine ( @"{1}    Test # {0} - First overloaded Constructor.{1}" , ++intTestNbr , Environment.NewLine );

            BitArray32 Mask2 = new BitArray32 ( BitArray32.BIT_07 );

			//	----------------------------------------------------------------
			//	The following statement writes a two-line ruler above the area
			//	where the next three statements will display bit masks. Since
			//	this group prints beneath the ruler, it eschews the spacing in
			//	favor of the compact format that aligns correctly with the
			//	ruler.
			//	----------------------------------------------------------------

			Console.WriteLine (
				"{2}                                                                            {0}{2}                                                                            {1}" ,
				BitArray32.GetRulerTens (
					BitArray32.BitCount.Count32 ,
					BitArray32.BitDisplayOrder.HighBitToLowBit ) ,
				BitArray32.GetRulerUnits (
					BitArray32.BitCount.Count32 ,
					BitArray32.BitDisplayOrder.HighBitToLowBit ) ,
				Environment.NewLine );

			Console.WriteLine ( @"        Initial value of Overloaded BitArray32 = {0} (Individual bits: {1})" , Mask2.ToString ( BITMASK_FORMAT_STRING ) , Mask2.ToString ( "H" ) );

            Mask2.BitOn ( 10 );
			Console.WriteLine ( @"        Bit 10 was turned ON.       Mask2      = {0} (Individual bits: {1})" , Mask2.ToString ( BITMASK_FORMAT_STRING ) , Mask2.ToString ( "H" ) );

            Mask2.BitOff ( 7 );
			Console.WriteLine ( @"        Bit 7 was turned OFF.       Mask2      = {0} (Individual bits: {1})" , Mask2.ToString ( BITMASK_FORMAT_STRING ) , Mask2.ToString ( "H" ) );

            if ( Mask2.IsBitOn ( 10 ) )
                Console.WriteLine ( @"        Bit 10 is ON" );
            else
                Console.WriteLine ( @"        Bit 10 is OFF" );

            if ( Mask2.IsBitOn ( 7 ) )
                Console.WriteLine ( @"        Bit 7 is ON" );
            else
                Console.WriteLine ( @"        Bit 7 is OFF" );

            if ( Mask2.IsBitOff ( 10 ) )
                Console.WriteLine ( @"        Bit 10 is OFF" );
            else
                Console.WriteLine ( @"        Bit 10 is NOT OFF" );

            if ( Mask2.IsBitOff ( 7 ) )
                Console.WriteLine ( @"        Bit 7 is OFF" );
            else
                Console.WriteLine ( @"        Bit 7 is NOT OFF" );

            StreamWriter swReport = null;

            try
            {
                swReport = new StreamWriter (
                    pstrReportFileName ,
                    StandardConstants.FILE_OUT_CREATE ,
                    System.Text.Encoding.ASCII );
                RealWorldTests ( swReport );
            }
            catch ( Exception ex )
            {
                Console.WriteLine ( ex.Message );
            }
            finally
            {   // Clean up the StreamWriter.
                if ( swReport != null )
                {
                    swReport.Close ( );
                    swReport.Dispose ( );
                    swReport = null;
                }   // if ( swReport != null )
            }   // One way or another, the StreamWriter got cleaned up.

            Console.WriteLine (
                "{0}End BitArray32TestStand Exercises{0}" ,
                Environment.NewLine );
        }   // static Exercise method
        }   // private static void RealWorldTests


		private static void TestBitArrayRulers ( ref int pintTestNbr )
		{
			Console.WriteLine (
				"Test {0}, TestBitArrayRulers Begin:" ,
				++pintTestNbr );

			int intSampleIndex = Util.ARRAY_FIRST_ELEMENT;

			foreach ( BitArray32.BitCount enmBitCount in s_aenmBitCount )
			{
				Console.WriteLine (
					"{2}    Bit Count = {0} (integer value = {1}):" ,
					enmBitCount ,
					( int ) enmBitCount ,
					Environment.NewLine );

				foreach ( BitArray32.BitDisplayOrder enmBitDisplayOrder in s_aenmBitDisplayOrder )
				{
					Console.WriteLine (
						"{2}        Bit Order = {0} (integer value = {1}):{2}" ,
						enmBitDisplayOrder ,
						( int ) enmBitDisplayOrder ,
					Environment.NewLine );

					try
					{
						Console.WriteLine (
							"        {0}{6}        {1}{6}        {2} ({3} value as integer: hexadecimal = {4} = {5:N0}  decimal){6}" ,		// Format control string
							new object [ ]
							{
								BitArray32.GetRulerTens (
									enmBitCount ,
									enmBitDisplayOrder ) ,																					// Format Item 0 = Tens row of ruler
								BitArray32.GetRulerUnits (
									enmBitCount ,
									enmBitDisplayOrder ) ,																					// Format Item 1 = Units row of ruler
								string.Format (
									new WizardWrx.BitMaskFormat ( ) ,
									enmBitDisplayOrder == BitArray32.BitDisplayOrder.LowBitToHighBit
										? FORMAT_FIRST_ITEM_AS_REVERSED_BIT_ARRAY
										: FORMAT_FIRST_ITEM_AS_BIT_ARRAY ,
									s_aobjRulerSample [ intSampleIndex ] ) ,																// Format Item 2 = Bit mask of Sample
								BitHelpers.InfoForIntegralType ( s_aobjRulerSample [ intSampleIndex ].GetType ( ) ) ,						// Format Item 3 = BCLIntegerTypeInfo type information (ToString reports all of its properties.)
								BitArray32.FormatIntegerAsHex ( s_aobjRulerSample [ intSampleIndex ] ) ,									// Format Item 4 = Formatted Hexadecimal representation of Sample.
								s_aobjRulerSample [ intSampleIndex ] ,																		// Format Item 5 = Formatted Decimal representation of Sample.
								Environment.NewLine																							// Format Item 6 = Embedded Newline
							} );
					}
					catch ( Exception exAllKinds )
					{
						Console.WriteLine (
							"An {0} exception occurred:{4}{4}    Message:        {1}{4}    Method:         {2}{4}    Stack Trace: {3}{4}" ,
							new object [ ]
							{
								exAllKinds.GetType ( ).FullName ,				// Format Item 0 = Exception Type
								Util.BeautifyExceptionMessage (
									exAllKinds.Message , 4 ) ,					// Format Item 1 = Exception Message
								exAllKinds.TargetSite ,							// Format Item 2 = Exception Method
								Util.BeautifyStackTrace (
									exAllKinds.StackTrace , 17 ) ,				// Format Item 3 = Exception Stack Trace
								Environment.NewLine								// Format Item 4 = Embedded Newline
							} );

						if ( exAllKinds.InnerException == null )
						{
							Console.WriteLine (
								"There are further details.{0}" ,
								Environment.NewLine );
						}	// TRUE (There is at least one inner exception.) block, if ( exAllKinds.InnerException == null )
						else
						{
							ReportInnerException ( exAllKinds.InnerException );
						}	// FALSE (There are no nested exceptions.) block, if ( exAllKinds.InnerException == null )
					}	// catch ( Exception exAllKinds ) Try/Catch block
				}	// foreach ( BitArray32.BitDisplayOrder enmRulelerDirection in s_aenmBitDisplayOrder )

				intSampleIndex++;		// Increment the subscript AFTER we use it!
			}	// foreach ( BitArray32.BitCount enmBitCount in s_aenmBitCount )

			Console.WriteLine (
				"Test {0} Done{1}" ,	// Format control string
				pintTestNbr ,			// Format Item 0 = Test Number
				Environment.NewLine );	// Format Item 1 = Embedded newline
		}	// private static void TestBitArrayRulers