-
Notifications
You must be signed in to change notification settings - Fork 19.9k
/
Copy pathHammingDistanceTest.java
82 lines (65 loc) · 2.49 KB
/
HammingDistanceTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.thealgorithms.others.cn;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
public class HammingDistanceTest {
@Test
public void checkForDifferentBits() {
int answer = HammingDistance.compute("000", "011");
Assertions.assertThat(answer).isEqualTo(2);
}
/*
1 0 1 0 1
1 1 1 1 0
----------
0 1 0 1 1
*/
@Test
public void checkForDifferentBitsLength() {
int answer = HammingDistance.compute("10101", "11110");
Assertions.assertThat(answer).isEqualTo(3);
}
@Test
public void checkForSameBits() {
String someBits = "111";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForLongDataBits() {
int answer = HammingDistance.compute("10010101101010000100110100", "00110100001011001100110101");
Assertions.assertThat(answer).isEqualTo(7);
}
@Test
public void mismatchDataBits() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("100010", "00011"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
}
@Test
public void mismatchDataBits2() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must have the same length");
}
@Test
public void checkForLongDataBitsSame() {
String someBits = "10010101101010000100110100";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForEmptyInput() {
String someBits = "";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void checkForInputOfLength1() {
String someBits = "0";
int answer = HammingDistance.compute(someBits, someBits);
Assertions.assertThat(answer).isEqualTo(0);
}
@Test
public void computeThrowsExceptionWhenInputsAreNotBitStrs() {
Exception ex = org.junit.jupiter.api.Assertions.assertThrows(IllegalArgumentException.class, () -> { HammingDistance.compute("1A", "11"); });
Assertions.assertThat(ex.getMessage()).contains("must be a binary string");
}
}