-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathReplaceQCharToAvoidConsecutiveRepeatingCharsTest.java
65 lines (52 loc) · 2.09 KB
/
ReplaceQCharToAvoidConsecutiveRepeatingCharsTest.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
package by.andd3dfx.string;
import org.junit.Before;
import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
public class ReplaceQCharToAvoidConsecutiveRepeatingCharsTest {
private ReplaceQCharToAvoidConsecutiveRepeatingChars instance;
@Before
public void setUp() throws Exception {
instance = new ReplaceQCharToAvoidConsecutiveRepeatingChars();
}
@Test
public void modifyString() {
var items = new String[]{"?zs", "ubv?w", "x?y??z?", "j?qg??b"};
for (var item : items) {
var result = instance.modifyString(item);
System.out.println(item + "->" + result);
checkAsserts(item, result);
}
}
@Test
public void modifyStringForNullOrEmpty() {
var items = new String[]{null, ""};
for (var item : items) {
var result = instance.modifyString(item);
assertThat(item).isEqualTo(result);
}
}
private void checkAsserts(String in, String out) {
// The length of both strings should be the same
assertThat(in.length()).isEqualTo(out.length());
// Check that all non-'?' chars from initial string present in result string
for (int i = 0; i < in.length(); i++) {
var ch = in.charAt(i);
if (ch != '?') {
assertThat(ch).isEqualTo(out.charAt(i));
}
}
// Check absence of '?' in result string
assertFalse("'?' should absent in result string", out.contains("?"));
// Check the absence of consecutive repeating characters in result string
var last = out.charAt(0);
for (var i = 1; i < out.length(); i++) {
var curr = out.charAt(i);
var errMsg = "Chars on positions %d and %d for conversion %s->%s should differ"
.formatted(i - 1, i, in, out);
assertNotEquals(errMsg, curr, last);
last = curr;
}
}
}