Skip to content

Commit

Permalink
+ logical shift left tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawkynt committed Aug 6, 2024
1 parent 310ac5f commit 325e011
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
4 changes: 2 additions & 2 deletions Corlib.Extensions/System/Math.T4.tt
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ public static partial class MathEx {
public static <#=type#> ArithmeticShiftRight(this <#=type#> @this, byte count) => (<#=type#>)(count >= <#=GetSizeInBits(type)-1#> ? 0 : @this >> count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static <#=type#> LogicalShiftLeft(this <#=type#> @this, byte count) => (<#=type#>)((<#=(type=="sbyte"?"byte":"u"+type)#>)@this << count);
public static <#=type#> LogicalShiftLeft(this <#=type#> @this, byte count) => (<#=type#>)ArithmeticShiftLeft((<#=(type=="sbyte"?"byte":"u"+type)#>)@this, count);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static <#=type#> LogicalShiftRight(this <#=type#> @this, byte count) => (<#=type#>)(@this >>> count);
public static <#=type#> LogicalShiftRight(this <#=type#> @this, byte count) => (<#=type#>)ArithmeticShiftRight(@this, count);

<#}#>
<#foreach (var type in new[]{"byte","ushort","uint","ulong"}){#>
Expand Down
37 changes: 36 additions & 1 deletion Tests/Corlib.Tests/System/MathTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8553,7 +8553,42 @@ public void MinMax(byte[] values, int minValue, int maxValue) {
[TestCase(unchecked((long)0x4000000000000000), 128, 0b00000000000000000000000000000000)]
public void ArithmeticShiftRightLong(long inp, byte count, long expected) => Assert.AreEqual(expected, inp.ArithmeticShiftRight(count));

// TODO: logic left shift
[Test]
[TestCase(unchecked((sbyte)0b00000001),0, unchecked((sbyte)0b00000001))]
[TestCase(unchecked((sbyte)0b00000001), 1, unchecked((sbyte)0b00000010))]
[TestCase(unchecked((sbyte)0b01000000), 1, unchecked((sbyte)0b10000000))]
[TestCase(unchecked((sbyte)0b01000000), 2, unchecked((sbyte)0b00000000))]
[TestCase(unchecked((sbyte)0b00000001), 8, unchecked((sbyte)0b00000000))]
[TestCase(unchecked((sbyte)0b00000001), 16, unchecked((sbyte)0b00000000))]
public void LogicalShiftLeftSByte(sbyte inp, byte count, sbyte expected) => Assert.AreEqual(expected, inp.LogicalShiftLeft(count));

[Test]
[TestCase(unchecked((short)0b0000000000000001), 0, unchecked((short)0b0000000000000001))]
[TestCase(unchecked((short)0b0000000000000001), 1, unchecked((short)0b0000000000000010))]
[TestCase(unchecked((short)0b0100000000000000), 1, unchecked((short)0b1000000000000000))]
[TestCase(unchecked((short)0b0100000000000000), 2, unchecked((short)0b0000000000000000))]
[TestCase(unchecked((short)0b0000000000000001), 16, unchecked((short)0b0000000000000000))]
[TestCase(unchecked((short)0b0000000000000001), 32, unchecked((short)0b0000000000000000))]
public void LogicalShiftLeftShort(short inp, byte count, short expected) => Assert.AreEqual(expected, inp.LogicalShiftLeft(count));

[Test]
[TestCase(unchecked((int)0b00000000000000000000000000000001), 0, unchecked((int)0b00000000000000000000000000000001))]
[TestCase(unchecked((int)0b00000000000000000000000000000001), 1, unchecked((int)0b00000000000000000000000000000010))]
[TestCase(unchecked((int)0b01000000000000000000000000000000), 1, unchecked((int)0b10000000000000000000000000000000))]
[TestCase(unchecked((int)0b01000000000000000000000000000000), 2, unchecked((int)0b00000000000000000000000000000000))]
[TestCase(unchecked((int)0b00000000000000000000000000000001), 32, unchecked((int)0b00000000000000000000000000000000))]
[TestCase(unchecked((int)0b00000000000000000000000000000001), 64, unchecked((int)0b00000000000000000000000000000000))]
public void LogicalShiftLeftInt(int inp, byte count, int expected) => Assert.AreEqual(expected, inp.LogicalShiftLeft(count));

[Test]
[TestCase(unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000001), 0, unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000001))]
[TestCase(unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000001), 1, unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000010))]
[TestCase(unchecked((long)0b0100000000000000000000000000000000000000000000000000000000000000), 1, unchecked((long)0b1000000000000000000000000000000000000000000000000000000000000000))]
[TestCase(unchecked((long)0b0100000000000000000000000000000000000000000000000000000000000000), 2, unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000000))]
[TestCase(unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000001), 64, unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000000))]
[TestCase(unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000001), 128, unchecked((long)0b0000000000000000000000000000000000000000000000000000000000000000))]
public void LogicalShiftLeftLong(long inp, byte count, long expected) => Assert.AreEqual(expected, inp.LogicalShiftLeft(count));

// TODO: logic right shift

[Test]
Expand Down

0 comments on commit 325e011

Please sign in to comment.