11package com .thealgorithms .bitmanipulation ;
22
3- import static org .junit .jupiter .api .Assertions .*;
4-
3+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5+ import static org .junit .jupiter .api .Assertions .assertTrue ;
6+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
57import org .junit .jupiter .api .Test ;
68
79/**
810 * Unit tests for BitRotate class covering typical, boundary, and edge cases.
911 * Tests verify correct behavior for 32-bit circular bit rotations.
12+ *
13+ * @author Yajunesh
1014 */
1115public class BitRotateTest {
1216
1317 // ===== rotateLeft Tests =====
1418
1519 @ Test
16- void testRotateLeftBasic () {
20+ public void testRotateLeftBasic () {
1721 // Basic left rotation
1822 assertEquals (0b00000000_00000000_00000000_00000010, BitRotate .rotateLeft (1 , 1 ));
1923 assertEquals (0b00000000_00000000_00000000_00000100, BitRotate .rotateLeft (1 , 2 ));
2024 assertEquals (0b00000000_00000000_00000000_00001000, BitRotate .rotateLeft (1 , 3 ));
2125 }
2226
2327 @ Test
24- void testRotateLeftWithCarry () {
28+ public void testRotateLeftWithCarry () {
2529 // Test bits carrying from left to right
2630 // Binary: 10000000_00000000_00000000_00000001
2731 int value = 0x80000001 ;
@@ -35,7 +39,7 @@ void testRotateLeftWithCarry() {
3539 }
3640
3741 @ Test
38- void testRotateLeftShift32 () {
42+ public void testRotateLeftShift32 () {
3943 // Shift of 32 should be same as shift of 0 (modulo behavior)
4044 int value = 0x12345678 ;
4145 assertEquals (value , BitRotate .rotateLeft (value , 32 ));
@@ -44,15 +48,15 @@ void testRotateLeftShift32() {
4448 }
4549
4650 @ Test
47- void testRotateLeftShiftNormalization () {
51+ public void testRotateLeftShiftNormalization () {
4852 // Test that shifts > 32 are properly normalized
4953 int value = 1 ;
5054 assertEquals (BitRotate .rotateLeft (value , 1 ), BitRotate .rotateLeft (value , 33 ));
5155 assertEquals (BitRotate .rotateLeft (value , 5 ), BitRotate .rotateLeft (value , 37 ));
5256 }
5357
5458 @ Test
55- void testRotateLeftZeroShift () {
59+ public void testRotateLeftZeroShift () {
5660 // Zero shift should return original value
5761 int value = 0xABCD1234 ;
5862 assertEquals (value , BitRotate .rotateLeft (value , 0 ));
@@ -61,15 +65,15 @@ void testRotateLeftZeroShift() {
6165 // ===== rotateRight Tests =====
6266
6367 @ Test
64- void testRotateRightBasic () {
68+ public void testRotateRightBasic () {
6569 // Basic right rotation
6670 assertEquals (0b10000000_00000000_00000000_00000000, BitRotate .rotateRight (1 , 1 ));
6771 assertEquals (0b01000000_00000000_00000000_00000000, BitRotate .rotateRight (1 , 2 ));
6872 assertEquals (0b00100000_00000000_00000000_00000000, BitRotate .rotateRight (1 , 3 ));
6973 }
7074
7175 @ Test
72- void testRotateRightWithCarry () {
76+ public void testRotateRightWithCarry () {
7377 // Test bits carrying from right to left
7478 // Binary: 00000000_00000000_00000000_00000011
7579 int value = 3 ;
@@ -83,7 +87,7 @@ void testRotateRightWithCarry() {
8387 }
8488
8589 @ Test
86- void testRotateRightShift32 () {
90+ public void testRotateRightShift32 () {
8791 // Shift of 32 should be same as shift of 0 (modulo behavior)
8892 int value = 0x9ABCDEF0 ;
8993 assertEquals (value , BitRotate .rotateRight (value , 32 ));
@@ -92,15 +96,15 @@ void testRotateRightShift32() {
9296 }
9397
9498 @ Test
95- void testRotateRightShiftNormalization () {
99+ public void testRotateRightShiftNormalization () {
96100 // Test that shifts > 32 are properly normalized
97101 int value = 1 ;
98102 assertEquals (BitRotate .rotateRight (value , 1 ), BitRotate .rotateRight (value , 33 ));
99103 assertEquals (BitRotate .rotateRight (value , 7 ), BitRotate .rotateRight (value , 39 ));
100104 }
101105
102106 @ Test
103- void testRotateRightZeroShift () {
107+ public void testRotateRightZeroShift () {
104108 // Zero shift should return original value
105109 int value = 0xDEADBEEF ;
106110 assertEquals (value , BitRotate .rotateRight (value , 0 ));
@@ -109,7 +113,7 @@ void testRotateRightZeroShift() {
109113 // ===== Edge Case Tests =====
110114
111115 @ Test
112- void testRotateLeftMaxValue () {
116+ public void testRotateLeftMaxValue () {
113117 // Test with maximum integer value
114118 int value = Integer .MAX_VALUE ; // 0x7FFFFFFF
115119 int rotated = BitRotate .rotateLeft (value , 1 );
@@ -118,7 +122,7 @@ void testRotateLeftMaxValue() {
118122 }
119123
120124 @ Test
121- void testRotateRightMinValue () {
125+ public void testRotateRightMinValue () {
122126 // Test with minimum integer value (treated as unsigned)
123127 int value = Integer .MIN_VALUE ; // 0x80000000
124128 int rotated = BitRotate .rotateRight (value , 1 );
@@ -127,15 +131,15 @@ void testRotateRightMinValue() {
127131 }
128132
129133 @ Test
130- void testRotateAllOnes () {
134+ public void testRotateAllOnes () {
131135 // Test with all bits set
132136 int value = 0xFFFFFFFF ; // All ones
133137 assertEquals (value , BitRotate .rotateLeft (value , 13 ));
134138 assertEquals (value , BitRotate .rotateRight (value , 27 ));
135139 }
136140
137141 @ Test
138- void testRotateAllZeros () {
142+ public void testRotateAllZeros () {
139143 // Test with all bits zero
140144 int value = 0x00000000 ;
141145 assertEquals (value , BitRotate .rotateLeft (value , 15 ));
@@ -145,49 +149,58 @@ void testRotateAllZeros() {
145149 // ===== Exception Tests =====
146150
147151 @ Test
148- void testRotateLeftNegativeShift () {
152+ public void testRotateLeftNegativeShift () {
149153 // Negative shifts should throw IllegalArgumentException
150- Exception exception = assertThrows (IllegalArgumentException .class , () -> BitRotate .rotateLeft (42 , -1 ));
154+ Exception exception = assertThrows (IllegalArgumentException .class ,
155+ () -> BitRotate .rotateLeft (42 , -1 ));
151156 assertTrue (exception .getMessage ().contains ("negative" ));
152157 }
153158
154159 @ Test
155- void testRotateRightNegativeShift () {
160+ public void testRotateRightNegativeShift () {
156161 // Negative shifts should throw IllegalArgumentException
157- Exception exception = assertThrows (IllegalArgumentException .class , () -> BitRotate .rotateRight (42 , -5 ));
162+ Exception exception = assertThrows (IllegalArgumentException .class ,
163+ () -> BitRotate .rotateRight (42 , -5 ));
158164 assertTrue (exception .getMessage ().contains ("negative" ));
159165 }
160166
161167 // ===== Complementary Operations Test =====
162168
163169 @ Test
164- void testRotateLeftRightComposition () {
170+ public void testRotateLeftRightComposition () {
165171 // Rotating left then right by same amount should return original value
166172 int original = 0x12345678 ;
167173 int shift = 7 ;
168-
174+
169175 int leftRotated = BitRotate .rotateLeft (original , shift );
170176 int restored = BitRotate .rotateRight (leftRotated , shift );
171-
177+
172178 assertEquals (original , restored );
173179 }
174180
175181 @ Test
176- void testRotateRightLeftComposition () {
182+ public void testRotateRightLeftComposition () {
177183 // Rotating right then left by same amount should return original value
178184 int original = 0x9ABCDEF0 ;
179185 int shift = 13 ;
180-
186+
181187 int rightRotated = BitRotate .rotateRight (original , shift );
182188 int restored = BitRotate .rotateLeft (rightRotated , shift );
183-
189+
184190 assertEquals (original , restored );
185191 }
186192
187193 @ Test
188- void testRotateLeft31IsSameAsRotateRight1 () {
194+ public void testRotateLeft31IsSameAsRotateRight1 () {
189195 // Rotating left by 31 should be same as rotating right by 1
190196 int value = 0x55555555 ;
191197 assertEquals (BitRotate .rotateLeft (value , 31 ), BitRotate .rotateRight (value , 1 ));
192198 }
193- }
199+
200+ @ Test
201+ public void testTraversals () {
202+ // Test that methods don't throw exceptions
203+ assertDoesNotThrow (() -> BitRotate .rotateLeft (1 , 1 ));
204+ assertDoesNotThrow (() -> BitRotate .rotateRight (1 , 1 ));
205+ }
206+ }
0 commit comments