From 3976ad2a517dec43b9ec7112f8dad60bffb2f612 Mon Sep 17 00:00:00 2001 From: Yajunesh M R Date: Fri, 31 Oct 2025 19:03:55 +0530 Subject: [PATCH 1/4] feat: Add BitRotate utility for circular bit rotations --- .../bitmanipulation/BitRotate.java | 83 ++++++++ .../bitmanipulation/BitRotateTest.java | 194 ++++++++++++++++++ 2 files changed, 277 insertions(+) create mode 100644 src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java create mode 100644 src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java b/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java new file mode 100644 index 000000000000..3ef0841d5d5e --- /dev/null +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java @@ -0,0 +1,83 @@ +package com.thealgorithms.bitmanipulation; + +/** + * Utility class for performing circular bit rotations on 32-bit integers. + * Bit rotation is a circular shift operation where bits shifted out on one end + * are reinserted on the opposite end. + * + *

This class provides methods for both left and right circular rotations, + * supporting only 32-bit integer operations with proper shift normalization + * and error handling.

+ * + * @see Bit Rotation + */ +public final class BitRotate { + + /** + * Private constructor to prevent instantiation. + * This is a utility class with only static methods. + */ + private BitRotate() { + throw new UnsupportedOperationException("This is a utility class and cannot be instantiated"); + } + + /** + * Performs a circular left rotation (left shift) on a 32-bit integer. + * Bits shifted out from the left side are inserted on the right side. + * + * @param value the 32-bit integer value to rotate + * @param shift the number of positions to rotate left (must be non-negative) + * @return the result of left rotating the value by the specified shift amount + * @throws IllegalArgumentException if shift is negative + * + * @example + * // Binary: 10000000 00000000 00000000 00000001 + * rotateLeft(0x80000001, 1) + * // Returns: 3 (binary: 00000000 00000000 00000000 00000011) + */ + public static int rotateLeft(int value, int shift) { + if (shift < 0) { + throw new IllegalArgumentException("Shift amount cannot be negative: " + shift); + } + + // Normalize shift to the range [0, 31] using modulo 32 + shift = shift % 32; + + if (shift == 0) { + return value; + } + + // Left rotation: (value << shift) | (value >>> (32 - shift)) + return (value << shift) | (value >>> (32 - shift)); + } + + /** + * Performs a circular right rotation (right shift) on a 32-bit integer. + * Bits shifted out from the right side are inserted on the left side. + * + * @param value the 32-bit integer value to rotate + * @param shift the number of positions to rotate right (must be non-negative) + * @return the result of right rotating the value by the specified shift amount + * @throws IllegalArgumentException if shift is negative + * + * @example + * // Binary: 00000000 00000000 00000000 00000011 + * rotateRight(3, 1) + * // Returns: -2147483647 (binary: 10000000 00000000 00000000 00000001) + */ + public static int rotateRight(int value, int shift) { + if (shift < 0) { + throw new IllegalArgumentException("Shift amount cannot be negative: " + shift); + } + + // Normalize shift to the range [0, 31] using modulo 32 + shift = shift % 32; + + if (shift == 0) { + return value; + } + + // Right rotation: (value >>> shift) | (value << (32 - shift)) + return (value >>> shift) | (value << (32 - shift)); + } +} \ No newline at end of file diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java new file mode 100644 index 000000000000..d5470c2c7565 --- /dev/null +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java @@ -0,0 +1,194 @@ +package com.thealgorithms.bitmanipulation; + +import static org.junit.jupiter.api.Assertions.*; +import org.junit.jupiter.api.Test; + +/** + * Unit tests for BitRotate class covering typical, boundary, and edge cases. + * Tests verify correct behavior for 32-bit circular bit rotations. + */ +public class BitRotateTest { + + // ===== rotateLeft Tests ===== + + @Test + void testRotateLeftBasic() { + // Basic left rotation + assertEquals(0b00000000_00000000_00000000_00000010, BitRotate.rotateLeft(1, 1)); + assertEquals(0b00000000_00000000_00000000_00000100, BitRotate.rotateLeft(1, 2)); + assertEquals(0b00000000_00000000_00000000_00001000, BitRotate.rotateLeft(1, 3)); + } + + @Test + void testRotateLeftWithCarry() { + // Test bits carrying from left to right + // Binary: 10000000_00000000_00000000_00000001 + int value = 0x80000001; + // After left rotate by 1: 00000000_00000000_00000000_00000011 + assertEquals(3, BitRotate.rotateLeft(value, 1)); + + // Binary: 11000000_00000000_00000000_00000000 + value = 0xC0000000; + // After left rotate by 1: 10000000_00000000_00000000_00000001 + assertEquals(0x80000001, BitRotate.rotateLeft(value, 1)); + } + + @Test + void testRotateLeftShift32() { + // Shift of 32 should be same as shift of 0 (modulo behavior) + int value = 0x12345678; + assertEquals(value, BitRotate.rotateLeft(value, 32)); + assertEquals(value, BitRotate.rotateLeft(value, 64)); + assertEquals(value, BitRotate.rotateLeft(value, 96)); + } + + @Test + void testRotateLeftShiftNormalization() { + // Test that shifts > 32 are properly normalized + int value = 1; + assertEquals(BitRotate.rotateLeft(value, 1), BitRotate.rotateLeft(value, 33)); + assertEquals(BitRotate.rotateLeft(value, 5), BitRotate.rotateLeft(value, 37)); + } + + @Test + void testRotateLeftZeroShift() { + // Zero shift should return original value + int value = 0xABCD1234; + assertEquals(value, BitRotate.rotateLeft(value, 0)); + } + + // ===== rotateRight Tests ===== + + @Test + void testRotateRightBasic() { + // Basic right rotation + assertEquals(0b10000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 1)); + assertEquals(0b01000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 2)); + assertEquals(0b00100000_00000000_00000000_00000000, BitRotate.rotateRight(1, 3)); + } + + @Test + void testRotateRightWithCarry() { + // Test bits carrying from right to left + // Binary: 00000000_00000000_00000000_00000011 + int value = 3; + // After right rotate by 1: 10000000_00000000_00000000_00000001 + assertEquals(0x80000001, BitRotate.rotateRight(value, 1)); + + // Binary: 00000000_00000000_00000000_00000001 + value = 1; + // After right rotate by 1: 10000000_00000000_00000000_00000000 + assertEquals(0x80000000, BitRotate.rotateRight(value, 1)); + } + + @Test + void testRotateRightShift32() { + // Shift of 32 should be same as shift of 0 (modulo behavior) + int value = 0x9ABCDEF0; + assertEquals(value, BitRotate.rotateRight(value, 32)); + assertEquals(value, BitRotate.rotateRight(value, 64)); + assertEquals(value, BitRotate.rotateRight(value, 96)); + } + + @Test + void testRotateRightShiftNormalization() { + // Test that shifts > 32 are properly normalized + int value = 1; + assertEquals(BitRotate.rotateRight(value, 1), BitRotate.rotateRight(value, 33)); + assertEquals(BitRotate.rotateRight(value, 7), BitRotate.rotateRight(value, 39)); + } + + @Test + void testRotateRightZeroShift() { + // Zero shift should return original value + int value = 0xDEADBEEF; + assertEquals(value, BitRotate.rotateRight(value, 0)); + } + + // ===== Edge Case Tests ===== + + @Test + void testRotateLeftMaxValue() { + // Test with maximum integer value + int value = Integer.MAX_VALUE; // 0x7FFFFFFF + int rotated = BitRotate.rotateLeft(value, 1); + // MAX_VALUE << 1 should become 0xFFFFFFFE, but with rotation it becomes different + assertEquals(0xFFFFFFFE, rotated); + } + + @Test + void testRotateRightMinValue() { + // Test with minimum integer value (treated as unsigned) + int value = Integer.MIN_VALUE; // 0x80000000 + int rotated = BitRotate.rotateRight(value, 1); + // MIN_VALUE >>> 1 should become 0x40000000, but with rotation from left + assertEquals(0x40000000, rotated); + } + + @Test + void testRotateAllOnes() { + // Test with all bits set + int value = 0xFFFFFFFF; // All ones + assertEquals(value, BitRotate.rotateLeft(value, 13)); + assertEquals(value, BitRotate.rotateRight(value, 27)); + } + + @Test + void testRotateAllZeros() { + // Test with all bits zero + int value = 0x00000000; + assertEquals(value, BitRotate.rotateLeft(value, 15)); + assertEquals(value, BitRotate.rotateRight(value, 19)); + } + + // ===== Exception Tests ===== + + @Test + void testRotateLeftNegativeShift() { + // Negative shifts should throw IllegalArgumentException + Exception exception = assertThrows(IllegalArgumentException.class, + () -> BitRotate.rotateLeft(42, -1)); + assertTrue(exception.getMessage().contains("negative")); + } + + @Test + void testRotateRightNegativeShift() { + // Negative shifts should throw IllegalArgumentException + Exception exception = assertThrows(IllegalArgumentException.class, + () -> BitRotate.rotateRight(42, -5)); + assertTrue(exception.getMessage().contains("negative")); + } + + // ===== Complementary Operations Test ===== + + @Test + void testRotateLeftRightComposition() { + // Rotating left then right by same amount should return original value + int original = 0x12345678; + int shift = 7; + + int leftRotated = BitRotate.rotateLeft(original, shift); + int restored = BitRotate.rotateRight(leftRotated, shift); + + assertEquals(original, restored); + } + + @Test + void testRotateRightLeftComposition() { + // Rotating right then left by same amount should return original value + int original = 0x9ABCDEF0; + int shift = 13; + + int rightRotated = BitRotate.rotateRight(original, shift); + int restored = BitRotate.rotateLeft(rightRotated, shift); + + assertEquals(original, restored); + } + + @Test + void testRotateLeft31IsSameAsRotateRight1() { + // Rotating left by 31 should be same as rotating right by 1 + int value = 0x55555555; + assertEquals(BitRotate.rotateLeft(value, 31), BitRotate.rotateRight(value, 1)); + } +} \ No newline at end of file From 1a77cf54f5a8ac5e5af71a472b6392694cbc1cc6 Mon Sep 17 00:00:00 2001 From: Yajunesh M R Date: Fri, 31 Oct 2025 19:11:13 +0530 Subject: [PATCH 2/4] feat: Add BitRotate utility for circular bit rotations --- .../bitmanipulation/BitRotate.java | 26 +++++++++---------- .../bitmanipulation/BitRotateTest.java | 23 ++++++++-------- 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java b/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java index 3ef0841d5d5e..226e09e78d1f 100644 --- a/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java +++ b/src/main/java/com/thealgorithms/bitmanipulation/BitRotate.java @@ -4,7 +4,7 @@ * Utility class for performing circular bit rotations on 32-bit integers. * Bit rotation is a circular shift operation where bits shifted out on one end * are reinserted on the opposite end. - * + * *

This class provides methods for both left and right circular rotations, * supporting only 32-bit integer operations with proper shift normalization * and error handling.

@@ -29,24 +29,24 @@ private BitRotate() { * @param shift the number of positions to rotate left (must be non-negative) * @return the result of left rotating the value by the specified shift amount * @throws IllegalArgumentException if shift is negative - * - * @example + * + * @example * // Binary: 10000000 00000000 00000000 00000001 - * rotateLeft(0x80000001, 1) + * rotateLeft(0x80000001, 1) * // Returns: 3 (binary: 00000000 00000000 00000000 00000011) */ public static int rotateLeft(int value, int shift) { if (shift < 0) { throw new IllegalArgumentException("Shift amount cannot be negative: " + shift); } - + // Normalize shift to the range [0, 31] using modulo 32 shift = shift % 32; - + if (shift == 0) { return value; } - + // Left rotation: (value << shift) | (value >>> (32 - shift)) return (value << shift) | (value >>> (32 - shift)); } @@ -59,9 +59,9 @@ public static int rotateLeft(int value, int shift) { * @param shift the number of positions to rotate right (must be non-negative) * @return the result of right rotating the value by the specified shift amount * @throws IllegalArgumentException if shift is negative - * + * * @example - * // Binary: 00000000 00000000 00000000 00000011 + * // Binary: 00000000 00000000 00000000 00000011 * rotateRight(3, 1) * // Returns: -2147483647 (binary: 10000000 00000000 00000000 00000001) */ @@ -69,15 +69,15 @@ public static int rotateRight(int value, int shift) { if (shift < 0) { throw new IllegalArgumentException("Shift amount cannot be negative: " + shift); } - + // Normalize shift to the range [0, 31] using modulo 32 shift = shift % 32; - + if (shift == 0) { return value; } - + // Right rotation: (value >>> shift) | (value << (32 - shift)) return (value >>> shift) | (value << (32 - shift)); } -} \ No newline at end of file +} diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java index d5470c2c7565..03de91df17b6 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java @@ -1,6 +1,7 @@ package com.thealgorithms.bitmanipulation; import static org.junit.jupiter.api.Assertions.*; + import org.junit.jupiter.api.Test; /** @@ -23,10 +24,10 @@ void testRotateLeftBasic() { void testRotateLeftWithCarry() { // Test bits carrying from left to right // Binary: 10000000_00000000_00000000_00000001 - int value = 0x80000001; + int value = 0x80000001; // After left rotate by 1: 00000000_00000000_00000000_00000011 assertEquals(3, BitRotate.rotateLeft(value, 1)); - + // Binary: 11000000_00000000_00000000_00000000 value = 0xC0000000; // After left rotate by 1: 10000000_00000000_00000000_00000001 @@ -74,7 +75,7 @@ void testRotateRightWithCarry() { int value = 3; // After right rotate by 1: 10000000_00000000_00000000_00000001 assertEquals(0x80000001, BitRotate.rotateRight(value, 1)); - + // Binary: 00000000_00000000_00000000_00000001 value = 1; // After right rotate by 1: 10000000_00000000_00000000_00000000 @@ -146,16 +147,14 @@ void testRotateAllZeros() { @Test void testRotateLeftNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, - () -> BitRotate.rotateLeft(42, -1)); + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateLeft(42, -1)); assertTrue(exception.getMessage().contains("negative")); } @Test void testRotateRightNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, - () -> BitRotate.rotateRight(42, -5)); + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateRight(42, -5)); assertTrue(exception.getMessage().contains("negative")); } @@ -166,10 +165,10 @@ void testRotateLeftRightComposition() { // Rotating left then right by same amount should return original value int original = 0x12345678; int shift = 7; - + int leftRotated = BitRotate.rotateLeft(original, shift); int restored = BitRotate.rotateRight(leftRotated, shift); - + assertEquals(original, restored); } @@ -178,10 +177,10 @@ void testRotateRightLeftComposition() { // Rotating right then left by same amount should return original value int original = 0x9ABCDEF0; int shift = 13; - + int rightRotated = BitRotate.rotateRight(original, shift); int restored = BitRotate.rotateLeft(rightRotated, shift); - + assertEquals(original, restored); } @@ -191,4 +190,4 @@ void testRotateLeft31IsSameAsRotateRight1() { int value = 0x55555555; assertEquals(BitRotate.rotateLeft(value, 31), BitRotate.rotateRight(value, 1)); } -} \ No newline at end of file +} From 9ec424487dcb7e1f26fad301740342101227d663 Mon Sep 17 00:00:00 2001 From: Yajunesh M R Date: Fri, 31 Oct 2025 19:23:38 +0530 Subject: [PATCH 3/4] feat: Add BitRotate utility for circular bit rotations --- .../bitmanipulation/BitRotateTest.java | 69 +++++++++++-------- 1 file changed, 41 insertions(+), 28 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java index 03de91df17b6..3c650c46b418 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java @@ -1,19 +1,23 @@ package com.thealgorithms.bitmanipulation; -import static org.junit.jupiter.api.Assertions.*; - +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import org.junit.jupiter.api.Test; /** * Unit tests for BitRotate class covering typical, boundary, and edge cases. * Tests verify correct behavior for 32-bit circular bit rotations. + * + * @author Yajunesh */ public class BitRotateTest { // ===== rotateLeft Tests ===== @Test - void testRotateLeftBasic() { + public void testRotateLeftBasic() { // Basic left rotation assertEquals(0b00000000_00000000_00000000_00000010, BitRotate.rotateLeft(1, 1)); assertEquals(0b00000000_00000000_00000000_00000100, BitRotate.rotateLeft(1, 2)); @@ -21,7 +25,7 @@ void testRotateLeftBasic() { } @Test - void testRotateLeftWithCarry() { + public void testRotateLeftWithCarry() { // Test bits carrying from left to right // Binary: 10000000_00000000_00000000_00000001 int value = 0x80000001; @@ -35,7 +39,7 @@ void testRotateLeftWithCarry() { } @Test - void testRotateLeftShift32() { + public void testRotateLeftShift32() { // Shift of 32 should be same as shift of 0 (modulo behavior) int value = 0x12345678; assertEquals(value, BitRotate.rotateLeft(value, 32)); @@ -44,7 +48,7 @@ void testRotateLeftShift32() { } @Test - void testRotateLeftShiftNormalization() { + public void testRotateLeftShiftNormalization() { // Test that shifts > 32 are properly normalized int value = 1; assertEquals(BitRotate.rotateLeft(value, 1), BitRotate.rotateLeft(value, 33)); @@ -52,7 +56,7 @@ void testRotateLeftShiftNormalization() { } @Test - void testRotateLeftZeroShift() { + public void testRotateLeftZeroShift() { // Zero shift should return original value int value = 0xABCD1234; assertEquals(value, BitRotate.rotateLeft(value, 0)); @@ -61,7 +65,7 @@ void testRotateLeftZeroShift() { // ===== rotateRight Tests ===== @Test - void testRotateRightBasic() { + public void testRotateRightBasic() { // Basic right rotation assertEquals(0b10000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 1)); assertEquals(0b01000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 2)); @@ -69,7 +73,7 @@ void testRotateRightBasic() { } @Test - void testRotateRightWithCarry() { + public void testRotateRightWithCarry() { // Test bits carrying from right to left // Binary: 00000000_00000000_00000000_00000011 int value = 3; @@ -83,7 +87,7 @@ void testRotateRightWithCarry() { } @Test - void testRotateRightShift32() { + public void testRotateRightShift32() { // Shift of 32 should be same as shift of 0 (modulo behavior) int value = 0x9ABCDEF0; assertEquals(value, BitRotate.rotateRight(value, 32)); @@ -92,7 +96,7 @@ void testRotateRightShift32() { } @Test - void testRotateRightShiftNormalization() { + public void testRotateRightShiftNormalization() { // Test that shifts > 32 are properly normalized int value = 1; assertEquals(BitRotate.rotateRight(value, 1), BitRotate.rotateRight(value, 33)); @@ -100,7 +104,7 @@ void testRotateRightShiftNormalization() { } @Test - void testRotateRightZeroShift() { + public void testRotateRightZeroShift() { // Zero shift should return original value int value = 0xDEADBEEF; assertEquals(value, BitRotate.rotateRight(value, 0)); @@ -109,7 +113,7 @@ void testRotateRightZeroShift() { // ===== Edge Case Tests ===== @Test - void testRotateLeftMaxValue() { + public void testRotateLeftMaxValue() { // Test with maximum integer value int value = Integer.MAX_VALUE; // 0x7FFFFFFF int rotated = BitRotate.rotateLeft(value, 1); @@ -118,7 +122,7 @@ void testRotateLeftMaxValue() { } @Test - void testRotateRightMinValue() { + public void testRotateRightMinValue() { // Test with minimum integer value (treated as unsigned) int value = Integer.MIN_VALUE; // 0x80000000 int rotated = BitRotate.rotateRight(value, 1); @@ -127,7 +131,7 @@ void testRotateRightMinValue() { } @Test - void testRotateAllOnes() { + public void testRotateAllOnes() { // Test with all bits set int value = 0xFFFFFFFF; // All ones assertEquals(value, BitRotate.rotateLeft(value, 13)); @@ -135,7 +139,7 @@ void testRotateAllOnes() { } @Test - void testRotateAllZeros() { + public void testRotateAllZeros() { // Test with all bits zero int value = 0x00000000; assertEquals(value, BitRotate.rotateLeft(value, 15)); @@ -145,49 +149,58 @@ void testRotateAllZeros() { // ===== Exception Tests ===== @Test - void testRotateLeftNegativeShift() { + public void testRotateLeftNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateLeft(42, -1)); + Exception exception = assertThrows(IllegalArgumentException.class, + () -> BitRotate.rotateLeft(42, -1)); assertTrue(exception.getMessage().contains("negative")); } @Test - void testRotateRightNegativeShift() { + public void testRotateRightNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateRight(42, -5)); + Exception exception = assertThrows(IllegalArgumentException.class, + () -> BitRotate.rotateRight(42, -5)); assertTrue(exception.getMessage().contains("negative")); } // ===== Complementary Operations Test ===== @Test - void testRotateLeftRightComposition() { + public void testRotateLeftRightComposition() { // Rotating left then right by same amount should return original value int original = 0x12345678; int shift = 7; - + int leftRotated = BitRotate.rotateLeft(original, shift); int restored = BitRotate.rotateRight(leftRotated, shift); - + assertEquals(original, restored); } @Test - void testRotateRightLeftComposition() { + public void testRotateRightLeftComposition() { // Rotating right then left by same amount should return original value int original = 0x9ABCDEF0; int shift = 13; - + int rightRotated = BitRotate.rotateRight(original, shift); int restored = BitRotate.rotateLeft(rightRotated, shift); - + assertEquals(original, restored); } @Test - void testRotateLeft31IsSameAsRotateRight1() { + public void testRotateLeft31IsSameAsRotateRight1() { // Rotating left by 31 should be same as rotating right by 1 int value = 0x55555555; assertEquals(BitRotate.rotateLeft(value, 31), BitRotate.rotateRight(value, 1)); } -} + + @Test + public void testTraversals() { + // Test that methods don't throw exceptions + assertDoesNotThrow(() -> BitRotate.rotateLeft(1, 1)); + assertDoesNotThrow(() -> BitRotate.rotateRight(1, 1)); + } +} \ No newline at end of file From 47f36c762a7993eb6f4dfd812f280c53b30140f6 Mon Sep 17 00:00:00 2001 From: Yajunesh M R Date: Fri, 31 Oct 2025 19:32:26 +0530 Subject: [PATCH 4/4] fix: Remove trailing spaces and add newline at EOF --- .../bitmanipulation/BitRotateTest.java | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java index 3c650c46b418..0595ae5a73e1 100644 --- a/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java +++ b/src/test/java/com/thealgorithms/bitmanipulation/BitRotateTest.java @@ -1,9 +1,10 @@ package com.thealgorithms.bitmanipulation; +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; + import org.junit.jupiter.api.Test; /** @@ -151,16 +152,14 @@ public void testRotateAllZeros() { @Test public void testRotateLeftNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, - () -> BitRotate.rotateLeft(42, -1)); + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateLeft(42, -1)); assertTrue(exception.getMessage().contains("negative")); } @Test public void testRotateRightNegativeShift() { // Negative shifts should throw IllegalArgumentException - Exception exception = assertThrows(IllegalArgumentException.class, - () -> BitRotate.rotateRight(42, -5)); + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateRight(42, -5)); assertTrue(exception.getMessage().contains("negative")); } @@ -171,10 +170,10 @@ public void testRotateLeftRightComposition() { // Rotating left then right by same amount should return original value int original = 0x12345678; int shift = 7; - + int leftRotated = BitRotate.rotateLeft(original, shift); int restored = BitRotate.rotateRight(leftRotated, shift); - + assertEquals(original, restored); } @@ -183,10 +182,10 @@ public void testRotateRightLeftComposition() { // Rotating right then left by same amount should return original value int original = 0x9ABCDEF0; int shift = 13; - + int rightRotated = BitRotate.rotateRight(original, shift); int restored = BitRotate.rotateLeft(rightRotated, shift); - + assertEquals(original, restored); } @@ -203,4 +202,4 @@ public void testTraversals() { assertDoesNotThrow(() -> BitRotate.rotateLeft(1, 1)); assertDoesNotThrow(() -> BitRotate.rotateRight(1, 1)); } -} \ No newline at end of file +}