Skip to content

Commit f04650c

Browse files
authored
[impr-charAtIdx] extract char improvement (#18966)
1 parent 508aa50 commit f04650c

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed
Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.baeldung.stringapi;
22

3-
import org.junit.jupiter.api.Test;
4-
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
54
import static org.junit.jupiter.api.Assertions.assertEquals;
65
import static org.junit.jupiter.api.Assertions.assertThrows;
76

7+
import org.junit.jupiter.api.Test;
8+
89
public class StringCharAtUnitTest {
10+
911
@Test
1012
public void whenCallCharAt_thenSuccess() {
1113
String sample = "abcdefg";
@@ -15,15 +17,60 @@ public void whenCallCharAt_thenSuccess() {
1517
@Test()
1618
public void whenCharAtNonExist_thenIndexOutOfBoundsExceptionThrown() {
1719
String sample = "abcdefg";
18-
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(-1));
19-
assertThrows(IndexOutOfBoundsException.class, () -> sample.charAt(sample.length()));
20+
assertThrows(StringIndexOutOfBoundsException.class, () -> sample.charAt(-1));
21+
assertThrows(StringIndexOutOfBoundsException.class, () -> sample.charAt(sample.length()));
22+
}
23+
24+
@Test
25+
public void whenUsingToCharArrayThenIndexing_thenCorrect() {
26+
String sample = "abcdefg";
27+
char[] chars = sample.toCharArray();
28+
assertEquals('d', chars[3]);
29+
assertEquals('f', chars[5]);
30+
}
31+
32+
@Test
33+
public void whenUsingCodePointAt_thenCorrect() {
34+
String sample = "abcdefg";
35+
assertEquals('d', sample.codePointAt(3));
36+
assertEquals('f', sample.codePointAt(5));
37+
38+
String emojiString = "😊"; // '😊' is Unicode char: U+1F60A
39+
assertEquals(0x1F60A, emojiString.codePointAt(0));
40+
}
41+
42+
@Test
43+
public void whenUsingGetChars_thenCorrect() {
44+
String sample = "abcdefg";
45+
char[] singleTargetChar = new char[1];
46+
sample.getChars(3, 3 + 1, singleTargetChar, 0);
47+
assertEquals('d', singleTargetChar[0]);
48+
49+
char[] multiTargetChars = new char[3];
50+
sample.getChars(3, 3 + 3, multiTargetChars, 0);
51+
assertArrayEquals(new char[] { 'd', 'e', 'f' }, multiTargetChars);
52+
}
53+
54+
char getCharUsingStream(String str, int index) {
55+
return str.chars()
56+
.mapToObj(ch -> (char) ch)
57+
.toArray(Character[]::new)[index];
58+
}
59+
60+
@Test
61+
public void whenUsingStreamAPI_thenCorrect() {
62+
String sample = "abcdefg";
63+
assertEquals('d', getCharUsingStream(sample, 3));
64+
assertEquals('f', getCharUsingStream(sample, 5));
65+
66+
assertThrows(ArrayIndexOutOfBoundsException.class, () -> getCharUsingStream(sample, 100));
2067
}
2168

2269
@Test
2370
public void whenCallCharAt_thenReturnString() {
2471
String sample = "abcdefg";
25-
assertEquals("a", Character.toString(sample.charAt(0)));
26-
assertEquals("a", String.valueOf(sample.charAt(0)));
72+
assertEquals("d", Character.toString(sample.charAt(3)));
73+
assertEquals("d", String.valueOf(sample.charAt(3)));
2774
}
2875

2976
}

0 commit comments

Comments
 (0)