public void OnUInt32(uint offset, int boundary, uint expected) { // Act var actual = BinaryMath.GetPadding(offset, boundary); // Assert Assert.Equal(expected, actual); }
public void OnInt64(long offset, int boundary, long expected) { // Act var actual = BinaryMath.GetPadding(offset, boundary); // Assert Assert.Equal(expected, actual); }
public void Int32_ShouldReturnExpectedResult(int offset, int boundary, int expected) { // Act var actual = BinaryMath.GetPadding(offset, boundary); // Assert Assert.Equal(expected, actual); }
/// <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))); }
/// <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)); } }