public void UInt64_ShouldReturnExpectedResult(ulong input, ulong expected)
            {
                // Act
                var actual = BinaryMath.RoundToNextPowerOfTwoOrZero(input);

                // Assert
                Assert.Equal(expected, actual);
            }
コード例 #2
0
            public void UInt64_ShouldReturnExpectedResult(ulong input, ulong expected)
            {
                // Act
                var actual = BinaryMath.SetMostSignificantBit(input);

                // Assert
                Assert.Equal(expected, actual);
            }
コード例 #3
0
            public void Int32_ShouldReturnExpectedResult(int input, int expected)
            {
                // Act
                var actual = BinaryMath.SetMostSignificantBit(input);

                // Assert
                Assert.Equal(expected, actual);
            }
コード例 #4
0
            public void UInt32_ShouldThrowOverflowException_OnOverflow()
            {
                // Act
                var exception = Record.Exception(() =>
                {
                    BinaryMath.Align(UInt32.MaxValue, 7);
                });

                // Assert
                Assert.IsType <OverflowException>(exception);
            }
コード例 #5
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
        /// <summary>
        /// Aligns the specified offset to the next boundary.
        /// </summary>
        /// <param name="offset">The offset.</param>
        /// <param name="boundary">The boundary.</param>
        /// <returns>The new offset.</returns>
        public static ulong Align(ulong offset, int boundary)
        {
            #region Contract
            if (boundary <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(boundary));
            }
            #endregion

            return(checked (offset + BinaryMath.GetPadding(offset, boundary)));
        }
コード例 #6
0
            public void Int64_ShouldThrowOverflowException_OnOverflow()
            {
                // Arrange
                long input = Int64.MaxValue;

                // Act
                var exception = Record.Exception(() =>
                {
                    BinaryMath.RoundToNextPowerOfTwoOrZero(input);
                });

                // Assert
                Assert.IsType <OverflowException>(exception);
            }
コード例 #7
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
        /// <summary>
        /// Calculates the padding needed to get the specified offset to a multiple of the specified boundary.
        /// </summary>
        /// <param name="offset">The offset.</param>
        /// <param name="boundary">The boundary.</param>
        /// <returns>The number of padding bytes to add.</returns>
        public static long GetPadding(long offset, int boundary)
        {
            #region Contract
            if (boundary <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(boundary));
            }
            #endregion

            if (BinaryMath.IsPowerOfTwoOrZero(boundary))
            {
                return((long)BinaryMath.GetPadding(unchecked ((ulong)offset), boundary));
            }
            else
            {
                return(IntegerMath.Modulo(boundary - IntegerMath.Modulo(offset, boundary), boundary));
            }
        }
コード例 #8
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
        /// <summary>
        /// Calculates the padding needed to get the specified offset to a multiple of the specified boundary.
        /// </summary>
        /// <param name="offset">The offset.</param>
        /// <param name="boundary">The boundary.</param>
        /// <returns>The number of padding bytes to add.</returns>
        public static ulong GetPadding(ulong offset, int boundary)
        {
            #region Contract
            if (boundary <= 0)
            {
                throw new ArgumentOutOfRangeException(nameof(boundary));
            }
            #endregion

            ulong b = unchecked ((ulong)boundary);
            if (BinaryMath.IsPowerOfTwoOrZero(boundary))
            {
                return((~offset + 1) & (b - 1));
            }
            else
            {
                return((b - (offset % b)) % b);
            }
        }
コード例 #9
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
 /// <summary>
 /// Determines whether a value is a positive power of two.
 /// </summary>
 /// <param name="value">The value to test.</param>
 /// <returns><see langword="true"/> when <paramref name="value"/> is a positive power of two;
 /// otherwise, <see langword="false"/>.</returns>
 public static bool IsPowerOfTwo(int value)
 {
     return(BinaryMath.IsPowerOfTwo((long)value));
 }
コード例 #10
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
 /// <summary>
 /// Determines whether a value is a positive power of two.
 /// </summary>
 /// <param name="value">The value to test.</param>
 /// <returns><see langword="true"/> when <paramref name="value"/> is a positive power of two;
 /// otherwise, <see langword="false"/>.</returns>
 public static bool IsPowerOfTwo(long value)
 {
     return(value > 0 && BinaryMath.IsPowerOfTwoOrZero(unchecked ((ulong)value)));
 }
コード例 #11
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
 /// <summary>
 /// Determines whether a value is a power of two, or zero.
 /// </summary>
 /// <param name="value">The value to test.</param>
 /// <returns><see langword="true"/> when <paramref name="value"/> is a power of two,
 /// or zero; otherwise, <see langword="false"/>.</returns>
 public static bool IsPowerOfTwoOrZero(uint value)
 {
     return(BinaryMath.IsPowerOfTwoOrZero((ulong)value));
 }
コード例 #12
0
ファイル: BinaryMath.cs プロジェクト: Virtlink/utilib
 /// <summary>
 /// Determines whether a value is a power of two.
 /// </summary>
 /// <param name="value">The value to test.</param>
 /// <returns><see langword="true"/> when <paramref name="value"/> is a power of two;
 /// otherwise, <see langword="false"/>.</returns>
 public static bool IsPowerOfTwo(ulong value)
 {
     return(value != 0 && BinaryMath.IsPowerOfTwoOrZero(value));
 }