コード例 #1
0
            public void OnUInt32(uint offset, int boundary, uint expected)
            {
                // Act
                var actual = BinaryMath.GetPadding(offset, boundary);

                // Assert
                Assert.Equal(expected, actual);
            }
コード例 #2
0
            public void OnInt64(long offset, int boundary, long expected)
            {
                // Act
                var actual = BinaryMath.GetPadding(offset, boundary);

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

                // Assert
                Assert.Equal(expected, actual);
            }
コード例 #4
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)));
        }
コード例 #5
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));
            }
        }