-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountCharTest.java
More file actions
90 lines (68 loc) · 2.26 KB
/
CountCharTest.java
File metadata and controls
90 lines (68 loc) · 2.26 KB
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
83
84
85
86
87
88
89
90
package manipulations.advance;
import com.google.common.base.CharMatcher;
import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CountCharTest {
@Test
public void java_core_simple_test() {
String someString = "elephant";
char someChar = 'e';
int count = 0;
for(int i = 0; i< someString.length(); ++i) {
if(someString.charAt(i) == someChar) {
count++;
}
}
Assertions.assertEquals(2, count);
}
@Test
public void recursion_test() {
String someString = "elephant";
char someChar = 'e';
int count = 0;
Assertions.assertEquals(2, useRecursion(someString, someChar, count));
}
@Test
public void regular_expression_test() {
Pattern pattern = Pattern.compile("[^e]*e");
Matcher matcher = pattern.matcher("elephant");
int count=0;
while (matcher.find()) {
count++;
}
Assertions.assertEquals(2, count);
}
@Test
public void java8_above_test() {
String someString = "elephant";
long count = someString.chars().filter(ch -> ch == 'e').count();
Assertions.assertEquals(2, count);
long count2 = someString.codePoints().filter(ch -> ch == 'e').count();
Assertions.assertEquals(2, count2);
}
@Test
public void apache_common_test() {
int count = StringUtils.countMatches("elephant", "e");
Assertions.assertEquals(2, count);
}
@Test
public void guava_test() {
int count = CharMatcher.is('e').countIn("elephant");
Assertions.assertEquals(2, count);
}
@Test
public void spring_test() {
int count = org.springframework.util.StringUtils.countOccurrencesOf("elephant", "e");
Assertions.assertEquals(2, count);
}
private static int useRecursion(String someString, char searchedChar, int index) {
if(index >= someString.length()) {
return 0;
}
int count = someString.charAt(index) == searchedChar ? 1: 0;
return count + useRecursion(someString, searchedChar, index+1);
}
}