|
| 1 | +package com.thealgorithms.bitmanipulation; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 6 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | + |
| 10 | +/** |
| 11 | + * Unit tests for BitRotate class covering typical, boundary, and edge cases. |
| 12 | + * Tests verify correct behavior for 32-bit circular bit rotations. |
| 13 | + * |
| 14 | + * @author Yajunesh |
| 15 | + */ |
| 16 | +public class BitRotateTest { |
| 17 | + |
| 18 | + // ===== rotateLeft Tests ===== |
| 19 | + |
| 20 | + @Test |
| 21 | + public void testRotateLeftBasic() { |
| 22 | + // Basic left rotation |
| 23 | + assertEquals(0b00000000_00000000_00000000_00000010, BitRotate.rotateLeft(1, 1)); |
| 24 | + assertEquals(0b00000000_00000000_00000000_00000100, BitRotate.rotateLeft(1, 2)); |
| 25 | + assertEquals(0b00000000_00000000_00000000_00001000, BitRotate.rotateLeft(1, 3)); |
| 26 | + } |
| 27 | + |
| 28 | + @Test |
| 29 | + public void testRotateLeftWithCarry() { |
| 30 | + // Test bits carrying from left to right |
| 31 | + // Binary: 10000000_00000000_00000000_00000001 |
| 32 | + int value = 0x80000001; |
| 33 | + // After left rotate by 1: 00000000_00000000_00000000_00000011 |
| 34 | + assertEquals(3, BitRotate.rotateLeft(value, 1)); |
| 35 | + |
| 36 | + // Binary: 11000000_00000000_00000000_00000000 |
| 37 | + value = 0xC0000000; |
| 38 | + // After left rotate by 1: 10000000_00000000_00000000_00000001 |
| 39 | + assertEquals(0x80000001, BitRotate.rotateLeft(value, 1)); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void testRotateLeftShift32() { |
| 44 | + // Shift of 32 should be same as shift of 0 (modulo behavior) |
| 45 | + int value = 0x12345678; |
| 46 | + assertEquals(value, BitRotate.rotateLeft(value, 32)); |
| 47 | + assertEquals(value, BitRotate.rotateLeft(value, 64)); |
| 48 | + assertEquals(value, BitRotate.rotateLeft(value, 96)); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testRotateLeftShiftNormalization() { |
| 53 | + // Test that shifts > 32 are properly normalized |
| 54 | + int value = 1; |
| 55 | + assertEquals(BitRotate.rotateLeft(value, 1), BitRotate.rotateLeft(value, 33)); |
| 56 | + assertEquals(BitRotate.rotateLeft(value, 5), BitRotate.rotateLeft(value, 37)); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testRotateLeftZeroShift() { |
| 61 | + // Zero shift should return original value |
| 62 | + int value = 0xABCD1234; |
| 63 | + assertEquals(value, BitRotate.rotateLeft(value, 0)); |
| 64 | + } |
| 65 | + |
| 66 | + // ===== rotateRight Tests ===== |
| 67 | + |
| 68 | + @Test |
| 69 | + public void testRotateRightBasic() { |
| 70 | + // Basic right rotation |
| 71 | + assertEquals(0b10000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 1)); |
| 72 | + assertEquals(0b01000000_00000000_00000000_00000000, BitRotate.rotateRight(1, 2)); |
| 73 | + assertEquals(0b00100000_00000000_00000000_00000000, BitRotate.rotateRight(1, 3)); |
| 74 | + } |
| 75 | + |
| 76 | + @Test |
| 77 | + public void testRotateRightWithCarry() { |
| 78 | + // Test bits carrying from right to left |
| 79 | + // Binary: 00000000_00000000_00000000_00000011 |
| 80 | + int value = 3; |
| 81 | + // After right rotate by 1: 10000000_00000000_00000000_00000001 |
| 82 | + assertEquals(0x80000001, BitRotate.rotateRight(value, 1)); |
| 83 | + |
| 84 | + // Binary: 00000000_00000000_00000000_00000001 |
| 85 | + value = 1; |
| 86 | + // After right rotate by 1: 10000000_00000000_00000000_00000000 |
| 87 | + assertEquals(0x80000000, BitRotate.rotateRight(value, 1)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void testRotateRightShift32() { |
| 92 | + // Shift of 32 should be same as shift of 0 (modulo behavior) |
| 93 | + int value = 0x9ABCDEF0; |
| 94 | + assertEquals(value, BitRotate.rotateRight(value, 32)); |
| 95 | + assertEquals(value, BitRotate.rotateRight(value, 64)); |
| 96 | + assertEquals(value, BitRotate.rotateRight(value, 96)); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testRotateRightShiftNormalization() { |
| 101 | + // Test that shifts > 32 are properly normalized |
| 102 | + int value = 1; |
| 103 | + assertEquals(BitRotate.rotateRight(value, 1), BitRotate.rotateRight(value, 33)); |
| 104 | + assertEquals(BitRotate.rotateRight(value, 7), BitRotate.rotateRight(value, 39)); |
| 105 | + } |
| 106 | + |
| 107 | + @Test |
| 108 | + public void testRotateRightZeroShift() { |
| 109 | + // Zero shift should return original value |
| 110 | + int value = 0xDEADBEEF; |
| 111 | + assertEquals(value, BitRotate.rotateRight(value, 0)); |
| 112 | + } |
| 113 | + |
| 114 | + // ===== Edge Case Tests ===== |
| 115 | + |
| 116 | + @Test |
| 117 | + public void testRotateLeftMaxValue() { |
| 118 | + // Test with maximum integer value |
| 119 | + int value = Integer.MAX_VALUE; // 0x7FFFFFFF |
| 120 | + int rotated = BitRotate.rotateLeft(value, 1); |
| 121 | + // MAX_VALUE << 1 should become 0xFFFFFFFE, but with rotation it becomes different |
| 122 | + assertEquals(0xFFFFFFFE, rotated); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testRotateRightMinValue() { |
| 127 | + // Test with minimum integer value (treated as unsigned) |
| 128 | + int value = Integer.MIN_VALUE; // 0x80000000 |
| 129 | + int rotated = BitRotate.rotateRight(value, 1); |
| 130 | + // MIN_VALUE >>> 1 should become 0x40000000, but with rotation from left |
| 131 | + assertEquals(0x40000000, rotated); |
| 132 | + } |
| 133 | + |
| 134 | + @Test |
| 135 | + public void testRotateAllOnes() { |
| 136 | + // Test with all bits set |
| 137 | + int value = 0xFFFFFFFF; // All ones |
| 138 | + assertEquals(value, BitRotate.rotateLeft(value, 13)); |
| 139 | + assertEquals(value, BitRotate.rotateRight(value, 27)); |
| 140 | + } |
| 141 | + |
| 142 | + @Test |
| 143 | + public void testRotateAllZeros() { |
| 144 | + // Test with all bits zero |
| 145 | + int value = 0x00000000; |
| 146 | + assertEquals(value, BitRotate.rotateLeft(value, 15)); |
| 147 | + assertEquals(value, BitRotate.rotateRight(value, 19)); |
| 148 | + } |
| 149 | + |
| 150 | + // ===== Exception Tests ===== |
| 151 | + |
| 152 | + @Test |
| 153 | + public void testRotateLeftNegativeShift() { |
| 154 | + // Negative shifts should throw IllegalArgumentException |
| 155 | + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateLeft(42, -1)); |
| 156 | + assertTrue(exception.getMessage().contains("negative")); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public void testRotateRightNegativeShift() { |
| 161 | + // Negative shifts should throw IllegalArgumentException |
| 162 | + Exception exception = assertThrows(IllegalArgumentException.class, () -> BitRotate.rotateRight(42, -5)); |
| 163 | + assertTrue(exception.getMessage().contains("negative")); |
| 164 | + } |
| 165 | + |
| 166 | + // ===== Complementary Operations Test ===== |
| 167 | + |
| 168 | + @Test |
| 169 | + public void testRotateLeftRightComposition() { |
| 170 | + // Rotating left then right by same amount should return original value |
| 171 | + int original = 0x12345678; |
| 172 | + int shift = 7; |
| 173 | + |
| 174 | + int leftRotated = BitRotate.rotateLeft(original, shift); |
| 175 | + int restored = BitRotate.rotateRight(leftRotated, shift); |
| 176 | + |
| 177 | + assertEquals(original, restored); |
| 178 | + } |
| 179 | + |
| 180 | + @Test |
| 181 | + public void testRotateRightLeftComposition() { |
| 182 | + // Rotating right then left by same amount should return original value |
| 183 | + int original = 0x9ABCDEF0; |
| 184 | + int shift = 13; |
| 185 | + |
| 186 | + int rightRotated = BitRotate.rotateRight(original, shift); |
| 187 | + int restored = BitRotate.rotateLeft(rightRotated, shift); |
| 188 | + |
| 189 | + assertEquals(original, restored); |
| 190 | + } |
| 191 | + |
| 192 | + @Test |
| 193 | + public void testRotateLeft31IsSameAsRotateRight1() { |
| 194 | + // Rotating left by 31 should be same as rotating right by 1 |
| 195 | + int value = 0x55555555; |
| 196 | + assertEquals(BitRotate.rotateLeft(value, 31), BitRotate.rotateRight(value, 1)); |
| 197 | + } |
| 198 | + |
| 199 | + @Test |
| 200 | + public void testTraversals() { |
| 201 | + // Test that methods don't throw exceptions |
| 202 | + assertDoesNotThrow(() -> BitRotate.rotateLeft(1, 1)); |
| 203 | + assertDoesNotThrow(() -> BitRotate.rotateRight(1, 1)); |
| 204 | + } |
| 205 | +} |
0 commit comments