Skip to content

Commit dbf0b75

Browse files
Fix MoveHash: correct null handling and add comprehensive test coverage
1 parent f472da1 commit dbf0b75

File tree

2 files changed

+46
-6
lines changed

2 files changed

+46
-6
lines changed

src/main/java/com/thealgorithms/twopointer/MoveHash.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
public class MoveHash {
1616

1717

18+
1819
/** default constructor */
1920
public MoveHash() {}
2021

@@ -27,6 +28,11 @@ public MoveHash() {}
2728
*/
2829

2930
public static String movehashtoend(String s) {
31+
/** return null if inputed string is null */
32+
if (s == null) {
33+
return null;
34+
}
35+
3036
/** converts string into character array
3137
for example.,
3238
string = roman,
@@ -35,11 +41,6 @@ public static String movehashtoend(String s) {
3541

3642
char[] c = s.toCharArray();
3743

38-
/** return null if inputed string is null */
39-
if (s == null) {
40-
return "null";
41-
}
42-
4344
/** j works like a tracker the hash position */
4445
int j = 0;
4546

src/test/java/com/thealgorithms/twopointer/MoveHashTest.java

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.thealgorithms.twopointer;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertNull;
45

56
import org.junit.jupiter.api.Test;
67

@@ -18,6 +19,44 @@ void testEmpty() {
1819

1920
@Test
2021
void testNull() {
21-
assertEquals(null, MoveHash.movehashtoend(null));
22+
assertNull(MoveHash.movehashtoend(null));
23+
}
24+
25+
@Test
26+
void testNoHash() {
27+
assertEquals("hello", MoveHash.movehashtoend("hello"));
28+
}
29+
30+
@Test
31+
void testAllHash() {
32+
assertEquals("#####", MoveHash.movehashtoend("#####"));
33+
}
34+
35+
@Test
36+
void testHashAtEnd() {
37+
assertEquals("hello##", MoveHash.movehashtoend("hello##"));
38+
}
39+
40+
@Test
41+
void testHashAtStart() {
42+
assertEquals("hello#####", MoveHash.movehashtoend("######hello"));
43+
}
44+
45+
@Test
46+
void testSingleCharacter() {
47+
assertEquals("#", MoveHash.movehashtoend("#"));
48+
}
49+
50+
@Test
51+
void testSingleCharacterNoHash() {
52+
assertEquals("a", MoveHash.movehashtoend("a"));
53+
}
54+
55+
@Test
56+
void testSwapFunction() {
57+
char[] arr = {'a', 'b', 'c'};
58+
MoveHash.swap(0, 2, arr);
59+
assertEquals('c', arr[0]);
60+
assertEquals('a', arr[2]);
2261
}
2362
}

0 commit comments

Comments
 (0)