public void UInt64_ShouldReturnExpectedResult(ulong input, ulong expected) { // Act var actual = BinaryMath.RoundToNextPowerOfTwoOrZero(input); // Assert Assert.Equal(expected, actual); }
public void UInt64_ShouldReturnExpectedResult(ulong input, ulong expected) { // Act var actual = BinaryMath.SetMostSignificantBit(input); // Assert Assert.Equal(expected, actual); }
public void Int32_ShouldReturnExpectedResult(int input, int expected) { // Act var actual = BinaryMath.SetMostSignificantBit(input); // Assert Assert.Equal(expected, actual); }
public void UInt32_ShouldThrowOverflowException_OnOverflow() { // Act var exception = Record.Exception(() => { BinaryMath.Align(UInt32.MaxValue, 7); }); // Assert Assert.IsType <OverflowException>(exception); }
/// <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))); }
public void Int64_ShouldThrowOverflowException_OnOverflow() { // Arrange long input = Int64.MaxValue; // Act var exception = Record.Exception(() => { BinaryMath.RoundToNextPowerOfTwoOrZero(input); }); // Assert Assert.IsType <OverflowException>(exception); }
/// <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)); } }
/// <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); } }
/// <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)); }
/// <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))); }
/// <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)); }
/// <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)); }