From 325e01157f5bbb852e5705d969d3f44ba50fcea8 Mon Sep 17 00:00:00 2001 From: Hawkynt Date: Tue, 6 Aug 2024 15:57:56 +0200 Subject: [PATCH] + logical shift left tests --- Corlib.Extensions/System/Math.T4.tt | 4 +-- Tests/Corlib.Tests/System/MathTest.cs | 37 ++++++++++++++++++++++++++- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/Corlib.Extensions/System/Math.T4.tt b/Corlib.Extensions/System/Math.T4.tt index 9af4327..8c1ca5c 100644 --- a/Corlib.Extensions/System/Math.T4.tt +++ b/Corlib.Extensions/System/Math.T4.tt @@ -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"}){#> diff --git a/Tests/Corlib.Tests/System/MathTest.cs b/Tests/Corlib.Tests/System/MathTest.cs index e92d7c8..ddb4441 100644 --- a/Tests/Corlib.Tests/System/MathTest.cs +++ b/Tests/Corlib.Tests/System/MathTest.cs @@ -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]