Skip to content

Commit e385c74

Browse files
ola-dotunadamd1985
authored andcommitted
String to char array and char array to String (eugenp#1002)
* String to char array and char array to String. * Method change * Custom ThreadPool In Java 8 Parallel Streams * Implemented suggested edits from editor. * Broke long method signature * Changed primitive type int to long and formula. * Update wrapper type to Long
1 parent fb50f56 commit e385c74

3 files changed

Lines changed: 127 additions & 0 deletions

File tree

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class CharArrayToStringUnitTest {
8+
9+
@Test
10+
public void givenCharArray_whenCallingStringConstructor_shouldConvertToString() {
11+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
12+
String result = new String(charArray);
13+
String expectedValue = "character";
14+
15+
assertEquals(expectedValue, result);
16+
}
17+
18+
@Test
19+
public void givenCharArray_whenCallingStringConstructorWithOffsetAndLength_shouldConvertToString(){
20+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
21+
String result = new String(charArray, 4, 3);
22+
String expectedValue = "act";
23+
24+
assertEquals(expectedValue, result);
25+
}
26+
27+
@Test
28+
public void givenCharArray_whenCallingStringCopyValueOf_shouldConvertToString(){
29+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
30+
String result = String.copyValueOf(charArray);
31+
String expectedValue = "character";
32+
33+
assertEquals(expectedValue, result);
34+
}
35+
36+
@Test
37+
public void givenCharArray_whenCallingStringCopyValueOfWithOffsetAndLength_shouldConvertToString(){
38+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
39+
String result = String.copyValueOf(charArray, 0, 4);
40+
String expectedValue = "char";
41+
42+
assertEquals(expectedValue, result);
43+
}
44+
45+
@Test
46+
public void givenCharArray_whenCallingStringValueOf_shouldConvertToString(){
47+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
48+
String result = String.valueOf(charArray);
49+
String expectedValue = "character";
50+
51+
assertEquals(expectedValue, result);
52+
}
53+
54+
@Test
55+
public void givenCharArray_whenCallingStringValueOfWithOffsetAndLength_shouldConvertToString(){
56+
char[] charArray = {'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r'};
57+
String result = String.valueOf(charArray, 3, 4);
58+
String expectedValue = "ract";
59+
60+
assertEquals(expectedValue, result);
61+
}
62+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.baeldung;
2+
3+
import static org.junit.Assert.*;
4+
5+
import org.junit.Test;
6+
7+
public class StringToCharArrayUnitTest {
8+
9+
@Test
10+
public void givenString_whenCallingStringToCharArray_shouldConvertToCharArray() {
11+
String givenString = "characters";
12+
13+
char[] result = givenString.toCharArray();
14+
15+
char[] expectedCharArray = { 'c', 'h', 'a', 'r', 'a', 'c', 't', 'e', 'r', 's' };
16+
17+
assertArrayEquals(expectedCharArray, result);
18+
}
19+
20+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package org.baeldung.java.streams;
2+
3+
import static org.junit.Assert.assertEquals;
4+
import static org.junit.Assert.assertTrue;
5+
6+
import java.util.ArrayList;
7+
import java.util.List;
8+
import java.util.concurrent.ExecutionException;
9+
import java.util.concurrent.ForkJoinPool;
10+
import java.util.stream.Stream;
11+
12+
import org.junit.Test;
13+
14+
public class ThreadPoolInParallelStream {
15+
16+
@Test
17+
public void giveRangeOfLongs_whenSummedInParallel_shouldBeEqualToExpectedTotal()
18+
throws InterruptedException, ExecutionException {
19+
List<Long> aList = new ArrayList<>();
20+
long lastNum = 1_000_000;
21+
long firstNum = 1;
22+
23+
long expectedTotal = (lastNum + firstNum) * lastNum / 2;
24+
25+
for(long i = firstNum; i <= lastNum; i++){
26+
aList.add(i);
27+
}
28+
29+
ForkJoinPool customThreadPool = new ForkJoinPool(4);
30+
long actualTotal = customThreadPool.submit(() -> aList.parallelStream().reduce(
31+
0L, (x, y) -> {
32+
return x + y;
33+
})).get();
34+
35+
assertEquals(expectedTotal, actualTotal);
36+
}
37+
38+
@Test
39+
public void givenList_whenCallingParallelStream_shouldBeParallelStream(){
40+
List<Long> aList = new ArrayList<>();
41+
Stream<Long> parallelStream = aList.parallelStream();
42+
43+
assertTrue(parallelStream.isParallel());
44+
}
45+
}

0 commit comments

Comments
 (0)