6
6
import org .junit .jupiter .api .Test ;
7
7
8
8
public class HammingDistanceTest {
9
-
10
- HammingDistance hd ;
11
-
12
- @ BeforeEach
13
- void initialize () {
14
- hd = new HammingDistance ();
15
- }
16
-
17
9
@ Test
18
10
public void checkForDifferentBits () {
19
- String senderBits = "000" , receiverBits = "011" ;
20
- int answer = hd .getHammingDistanceBetweenBits (senderBits , receiverBits );
11
+ int answer = HammingDistance .compute ("000" , "011" );
21
12
Assertions .assertThat (answer ).isEqualTo (2 );
22
13
}
23
14
@@ -32,38 +23,62 @@ public void checkForDifferentBits() {
32
23
*/
33
24
@ Test
34
25
public void checkForDifferentBitsLength () {
35
- String senderBits = "10101" , receiverBits = "11110" ;
36
- int answer = hd .getHammingDistanceBetweenBits (senderBits , receiverBits );
26
+ int answer = HammingDistance .compute ("10101" , "11110" );
37
27
Assertions .assertThat (answer ).isEqualTo (3 );
38
28
}
39
29
40
30
@ Test
41
31
public void checkForSameBits () {
42
- String senderBits = "111" , receiverBits = "111" ;
43
- int answer = hd . getHammingDistanceBetweenBits ( senderBits , receiverBits );
32
+ String someBits = "111" ;
33
+ int answer = HammingDistance . compute ( someBits , someBits );
44
34
Assertions .assertThat (answer ).isEqualTo (0 );
45
35
}
46
36
47
37
@ Test
48
38
public void checkForLongDataBits () {
49
- String senderBits = "10010101101010000100110100" , receiverBits = "00110100001011001100110101" ;
50
- int answer = hd .getHammingDistanceBetweenBits (senderBits , receiverBits );
39
+ int answer = HammingDistance .compute ("10010101101010000100110100" , "00110100001011001100110101" );
51
40
Assertions .assertThat (answer ).isEqualTo (7 );
52
41
}
53
42
54
43
@ Test
55
44
public void mismatchDataBits () {
56
- String senderBits = "100010" , receiverBits = "00011" ;
45
+ Exception ex = org .junit .jupiter .api .Assertions .assertThrows (IllegalArgumentException .class , () -> { int answer = HammingDistance .compute ("100010" , "00011" ); });
46
+
47
+ Assertions .assertThat (ex .getMessage ()).contains ("must have the same length" );
48
+ }
57
49
58
- Exception ex = org .junit .jupiter .api .Assertions .assertThrows (IllegalArgumentException .class , () -> { int answer = hd .getHammingDistanceBetweenBits (senderBits , receiverBits ); });
50
+ @ Test
51
+ public void mismatchDataBits2 () {
52
+ Exception ex = org .junit .jupiter .api .Assertions .assertThrows (IllegalArgumentException .class , () -> { int answer = HammingDistance .compute ("1" , "11" ); });
59
53
60
- Assertions .assertThat (ex .getMessage ()).contains ("bits should be same" );
54
+ Assertions .assertThat (ex .getMessage ()).contains ("must have the same length " );
61
55
}
62
56
63
57
@ Test
64
58
public void checkForLongDataBitsSame () {
65
- String senderBits = "10010101101010000100110100" , receiverBits = "10010101101010000100110100" ;
66
- int answer = hd . getHammingDistanceBetweenBits ( senderBits , receiverBits );
59
+ String someBits = "10010101101010000100110100" ;
60
+ int answer = HammingDistance . compute ( someBits , someBits );
67
61
Assertions .assertThat (answer ).isEqualTo (0 );
68
62
}
63
+
64
+ @ Test
65
+ public void checkForEmptyInput () {
66
+ String someBits = "" ;
67
+ int answer = HammingDistance .compute (someBits , someBits );
68
+ Assertions .assertThat (answer ).isEqualTo (0 );
69
+ }
70
+
71
+ @ Test
72
+ public void checkForInputOfLength1 () {
73
+ String someBits = "0" ;
74
+ int answer = HammingDistance .compute (someBits , someBits );
75
+ Assertions .assertThat (answer ).isEqualTo (0 );
76
+ }
77
+
78
+ @ Test
79
+ public void computeThrowsExceptionWhenInputsAreNotBitStrs () {
80
+ Exception ex = org .junit .jupiter .api .Assertions .assertThrows (IllegalArgumentException .class , () -> { int answer = HammingDistance .compute ("1A" , "11" ); });
81
+
82
+ Assertions .assertThat (ex .getMessage ()).contains ("must be a binary string" );
83
+ }
69
84
}
0 commit comments