This repository was archived by the owner on Dec 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAppTest.java
More file actions
83 lines (69 loc) · 2.37 KB
/
AppTest.java
File metadata and controls
83 lines (69 loc) · 2.37 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
package app;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import org.junit.Test;
public class AppTest
{
@Test // Topic: Reverse string
public void shouldReverseString() {
Function<String, String> revStr =
str -> new StringBuilder(str).reverse().toString();
assertEquals(revStr.apply("disney"), "yensid");
}
@Test // Topic: Is palindrome
public void shouldBePalindrome() {
Function<String, Boolean> isPalindrome = str -> {
String temp = str.replaceAll("\\s+", "").toLowerCase();
return IntStream.range(0, temp.length()/2)
.allMatch(i -> temp.charAt(i) == temp.charAt(temp.length() - i - 1));
};
assertEquals(isPalindrome.apply("abcba"), true);
assertEquals(isPalindrome.apply("ab cba"), true);
assertEquals(isPalindrome.apply("abc"), false);
}
@Test // Topic: Reverse int
public void shouldReverseInt() {
Function<Integer, Integer> revInt = number -> Integer.parseInt(
new StringBuilder(Integer.toString(number))
.reverse().toString()
);
assertEquals(revInt.apply(Integer.valueOf(123)), Integer.valueOf(321));
}
@Test // Topic: Capitalize letters
public void shouldCapitalizeLetters() {
Function<String, String> capitalizeLetters =
str -> Arrays.stream(str.split("\\s+"))
.map(word -> word.substring(0, 1).toUpperCase() + word.substring(1))
.collect(Collectors.joining(" "));
assertEquals(capitalizeLetters.apply("ab cd"), "Ab Cd");
}
@Test // Topic: Max character
public void shouldGetMaxCharacter() {
Function<String, Integer> maxCharacter = str -> {
Map<Character, Integer> map = new HashMap<Character, Integer>();
for (char c : str.toCharArray()) {
if (!map.containsKey(c)) {
map.put(c, 1);
} else {
map.put(c, map.get(c) + 1);
}
}
int max = Integer.MIN_VALUE;
Set<Map.Entry<Character,Integer>> entries = map.entrySet();
for (Entry<Character,Integer> entry : entries) {
if (entry.getValue()>max) {
max=entry.getValue();
}
}
return max;
};
assertEquals(maxCharacter.apply("java"), Integer.valueOf(2));
}
}