Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions CompletableFuture/src/main/java/com/metaxiii/fr/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@
public class Main {

public Future<String> calculateAsync() {
return CompletableFuture.supplyAsync(() -> {
try {
Thread.sleep(500); // Simuler une tâche longue
return "Hello";
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
});
return CompletableFuture.supplyAsync(
() -> {
try {
Thread.sleep(500); // Simuler une tâche longue
return "Hello";
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return null;
}
});
}

public CompletableFuture<Integer> compute() {
Expand Down
91 changes: 51 additions & 40 deletions CompletableFuture/src/test/java/com/metaxiii/fr/MainTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,25 +34,28 @@ void init() {
@Test
void whenAddingThenAcceptToFuture_thenFunctionExecutesAfterComputationIsFinished() {
final var completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
final var future = completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));
final var future =
completableFuture.thenAccept(s -> System.out.println("Computation returned: " + s));
assertDoesNotThrow(() -> future.get());
}

@Test
void whenAddingThenApplyAsyncToFuture_thenFunctionExecutesAfterComputationIsFinished()
throws ExecutionException, InterruptedException {
throws ExecutionException, InterruptedException {
final var completableFuture = CompletableFuture.supplyAsync(() -> "Hello");
final var future = completableFuture.thenApplyAsync(s -> s + " World");
assertEquals("Hello World", future.get());
}

@Test
void whenAddingThenApplyToFuture_thenFunctionExecutesAfterComputationIsFinished()
throws ExecutionException, InterruptedException {
final var completableFuture = CompletableFuture.supplyAsync(() -> {
await().pollDelay(5, TimeUnit.SECONDS).untilAsserted(() -> assertTrue(true));
return "Hello";
});
throws ExecutionException, InterruptedException {
final var completableFuture =
CompletableFuture.supplyAsync(
() -> {
await().pollDelay(5, TimeUnit.SECONDS).untilAsserted(() -> assertTrue(true));
return "Hello";
});
final var future = completableFuture.thenApply(s -> s + " World");
assertEquals("Hello World", future.get());
}
Expand All @@ -66,13 +69,14 @@ void whenAddingThenRunToFuture_thenFunctionExecutesAfterComputationIsFinished()

@Test
void whenCreatingCompletableFutureWithSupplyAsync_thenFutureReturnsValue()
throws ExecutionException, InterruptedException {
throws ExecutionException, InterruptedException {
final var future = CompletableFuture.supplyAsync(() -> "Hello");
assertEquals("Hello", future.get());
}

@Test
void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws ExecutionException, InterruptedException {
void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone()
throws ExecutionException, InterruptedException {
final var future1 = CompletableFuture.supplyAsync(() -> "Hello");
final var future2 = CompletableFuture.supplyAsync(() -> "Beautiful");
final var future3 = CompletableFuture.supplyAsync(() -> "World");
Expand All @@ -81,26 +85,27 @@ void whenFutureCombinedWithAllOfCompletes_thenAllFuturesAreDone() throws Executi
assertTrue(future1.isDone());
assertTrue(future2.isDone());
assertTrue(future3.isDone());
final var combined = Stream
.of(future1, future2, future3)
.map(CompletableFuture::join)
.collect(Collectors.joining(" "));
final var combined =
Stream.of(future1, future2, future3)
.map(CompletableFuture::join)
.collect(Collectors.joining(" "));
assertEquals("Hello Beautiful World", combined);
}

@NullSource
@ParameterizedTest
@ValueSource(strings = "you")
void whenFutureThrows_thenHandleMethodReceivesException(final String name)
throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture = CompletableFuture
.supplyAsync(() -> {
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
})
.handle((s, t) -> s != null ? s : "Hello, Stranger!");
throws ExecutionException, InterruptedException {
CompletableFuture<String> completableFuture =
CompletableFuture.supplyAsync(
() -> {
if (name == null) {
throw new RuntimeException("Computation error!");
}
return "Hello, " + name;
})
.handle((s, t) -> s != null ? s : "Hello, Stranger!");
if (name == null) {
assertEquals("Hello, Stranger!", completableFuture.get());
} else {
Expand All @@ -109,55 +114,61 @@ void whenFutureThrows_thenHandleMethodReceivesException(final String name)
}

@Test
void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose() throws InterruptedException, ExecutionException {
CompletableFuture<Integer> finalResult = main.compute().thenCompose(integer -> main.computeAnother(integer));
void whenPassingPreviousStage_thenFunctionExecutionWithThenCompose()
throws InterruptedException, ExecutionException {
CompletableFuture<Integer> finalResult =
main.compute().thenCompose(integer -> main.computeAnother(integer));
assertEquals(20, (int) finalResult.get());
}

@Test
void whenPassingTransformation_thenFunctionExecutionWithThenApply() throws InterruptedException, ExecutionException {
void whenPassingTransformation_thenFunctionExecutionWithThenApply()
throws InterruptedException, ExecutionException {
final var finalResult = main.compute().thenApply(s -> s + 1);
assertEquals(11, finalResult.get());
}

@Test
void whenRunningCompletableFutureAsynchronously_thenGetMethodWaitsForResult()
throws InterruptedException, ExecutionException {
throws InterruptedException, ExecutionException {
final var completableFuture = main.calculateAsync();
final var result = completableFuture.get();
assertEquals("Hello", result);
}

@Test
void whenRunningCompletableFutureWithResult_thenGetMethodReturnsImmediately()
throws InterruptedException, ExecutionException {
throws InterruptedException, ExecutionException {
final var completableFuture = CompletableFuture.completedFuture("Hello");
final var result = completableFuture.get();
assertEquals("Hello", result);
}

@Test
void whenUsingThenAcceptBoth_thenWaitForExecutionOfBothFutures() {
assertDoesNotThrow(() ->
CompletableFuture
.supplyAsync(() -> "Hello")
.thenAcceptBoth(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> System.out.println(s1 + s2))
);
assertDoesNotThrow(
() ->
CompletableFuture.supplyAsync(() -> "Hello")
.thenAcceptBoth(
CompletableFuture.supplyAsync(() -> " World"),
(s1, s2) -> System.out.println(s1 + s2)));
}

@Test
void whenUsingThenCombine_thenWaitForExecutionOfBothFutures() throws ExecutionException, InterruptedException {
final var completableFuture = CompletableFuture
.supplyAsync(() -> "Hello")
.thenCombine(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> s1 + s2);
void whenUsingThenCombine_thenWaitForExecutionOfBothFutures()
throws ExecutionException, InterruptedException {
final var completableFuture =
CompletableFuture.supplyAsync(() -> "Hello")
.thenCombine(CompletableFuture.supplyAsync(() -> " World"), (s1, s2) -> s1 + s2);
assertEquals("Hello World", completableFuture.get());
}

@Test
void whenUsingThenCompose_thenFuturesExecuteSequentially() throws ExecutionException, InterruptedException {
final var completableFuture = CompletableFuture
.supplyAsync(() -> "Hello")
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
void whenUsingThenCompose_thenFuturesExecuteSequentially()
throws ExecutionException, InterruptedException {
final var completableFuture =
CompletableFuture.supplyAsync(() -> "Hello")
.thenCompose(s -> CompletableFuture.supplyAsync(() -> s + " World"));
assertEquals("Hello World", completableFuture.get());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ class AppTest {
static final String INPUT1 = "some text [THE IMPORTANT MESSAGE] something else";
static final String EXPECTED1 = "THE IMPORTANT MESSAGE";

static final String INPUT2 = "[La La Land], [The last Emperor], and [Life of Pi] are all great movies.";
static final String INPUT2 =
"[La La Land], [The last Emperor], and [Life of Pi] are all great movies.";
static final List<String> EXPECTED2 = List.of("La La Land", "The last Emperor", "Life of Pi");

@Test
Expand Down Expand Up @@ -90,6 +91,7 @@ void whenUsingSplitWithLimit_thenGetExpectedResult() {
assertTrue(Arrays.stream(strArray).toList().containsAll(List.of("", "THE IMPORTANT MESSAGE")));
strArray = "[THE IMPORTANT MESSAGE]".split("[\\[\\]]", -1);
assertEquals(3, strArray.length);
assertTrue(Arrays.stream(strArray).toList().containsAll(List.of("", "THE IMPORTANT MESSAGE", "")));
assertTrue(
Arrays.stream(strArray).toList().containsAll(List.of("", "THE IMPORTANT MESSAGE", "")));
}
}
7 changes: 3 additions & 4 deletions FloatvsDouble/src/test/java/com/metaxiii/fr/AppTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ void givenRangeLimits_whenExceedingBounds_thenFloatUnderflowsAndDoubleHandles()
void givenUnderflowScenario_whenExceedingFloatRange_thenFloatUnderflowsToZero() {
float underflowValue = 1.4e-45f / 2; // Smaller than the smallest normalized float value
assertEquals(
0.0f,
underflowValue,
"Float should underflow to zero for values smaller than the smallest representable number"
);
0.0f,
underflowValue,
"Float should underflow to zero for values smaller than the smallest representable number");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
class AppTest {

private static final String INPUT =
"This line contains <the first value>, <the second value>, and <the third value>.";
"This line contains <the first value>, <the second value>, and <the third value>.";

@Test
void whenCallingMatcherEnd_thenGetIndexesAfterTheMatchSequence() {
Expand Down Expand Up @@ -43,15 +43,13 @@ void whenUsingMatcherStartAndEndWithGroupIdx_thenGetIndexesOfMatches() {
result.add(matcher.group(1));
indexesOfMatches.put(matcher.start(1), matcher.end(1));
}
assertTrue(result.containsAll(List.of("the first value", "the second value", "the third value")));
assertTrue(
indexesOfMatches
.entrySet()
.stream()
.map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.toList()
.containsAll(List.of("the first value", "the second value", "the third value"))
);
result.containsAll(List.of("the first value", "the second value", "the third value")));
assertTrue(
indexesOfMatches.entrySet().stream()
.map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.toList()
.containsAll(List.of("the first value", "the second value", "the third value")));
}

@Test
Expand All @@ -64,15 +62,14 @@ void whenUsingMatcherStartAndEnd_thenGetIndexesOfMatches() {
result.add(matcher.group());
indexesOfMatches.put(matcher.start(), matcher.end());
}
assertTrue(result.containsAll(List.of("<the first value>", "<the second value>", "<the third value>")));
assertTrue(
indexesOfMatches
.entrySet()
.stream()
.map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.toList()
.containsAll(List.of("<the first value>", "<the second value>", "<the third value>"))
);
result.containsAll(
List.of("<the first value>", "<the second value>", "<the third value>")));
assertTrue(
indexesOfMatches.entrySet().stream()
.map(entry -> INPUT.substring(entry.getKey(), entry.getValue()))
.toList()
.containsAll(List.of("<the first value>", "<the second value>", "<the third value>")));
}

@Test
Expand All @@ -83,6 +80,8 @@ void whenUsingNorCharClass_thenGetExpectedTexts() {
while (matcher.find()) {
result.add(matcher.group());
}
assertTrue(result.containsAll(List.of("<the first value>", "<the second value>", "<the third value>")));
assertTrue(
result.containsAll(
List.of("<the first value>", "<the second value>", "<the third value>")));
}
}
38 changes: 20 additions & 18 deletions Guava - Maps/src/test/java/com/metaxiii/fr/GuavaMapsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,13 @@ void whenCreatingClassToInstanceMap_thenCorrect() {

@Test
void whenCreatingImmutableMap_thenCorrect() {
final var salary = ImmutableMap
.<String, Integer>builder()
.put("John", 1000)
.put("Jane", 1500)
.put("Adam", 2000)
.put("Tom", 2000)
.build();
final var salary =
ImmutableMap.<String, Integer>builder()
.put("John", 1000)
.put("Jane", 1500)
.put("Adam", 2000)
.put("Tom", 2000)
.build();

assertEquals(1000, salary.get("John"));
assertEquals(2000, salary.get("Tom"));
Expand All @@ -78,11 +78,12 @@ void whenCreatingTable_thenCorrect() {
@Test
void whenGroupingListsUsingMultimap_thenGrouped() {
final var names = Lists.newArrayList("John", "Adam", "Tom");
final var func = new Function<String, Integer>() {
public Integer apply(final String input) {
return input.length();
}
};
final var func =
new Function<String, Integer>() {
public Integer apply(final String input) {
return input.length();
}
};
final var groups = Multimaps.index(names, func);
assertTrue(groups.get(3).contains("Tom"));
assertTrue(groups.get(4).containsAll(List.of("John", "Adam")));
Expand All @@ -101,12 +102,13 @@ void whenTransposingTable_thenCorrect() {

@Test
void whenUsingSortedMap_thenKeysAreSorted() {
final var salary = new ImmutableSortedMap.Builder<String, Integer>(Ordering.natural())
.put("John", 1000)
.put("Jane", 1500)
.put("Adam", 2000)
.put("Tom", 2000)
.build();
final var salary =
new ImmutableSortedMap.Builder<String, Integer>(Ordering.natural())
.put("John", 1000)
.put("Jane", 1500)
.put("Adam", 2000)
.put("Tom", 2000)
.build();
assertEquals("Adam", salary.firstKey());
assertEquals(2000, Objects.requireNonNull(salary.lastEntry()).getValue().intValue());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import com.google.common.base.Predicate;
import java.nio.charset.Charset;
import java.util.Objects;

import org.junit.jupiter.api.Test;

class GuavaCharMatcherTest {
Expand All @@ -34,12 +33,13 @@ void whenCountCharInString_thenCorrect() {
void whenRemoveCharsNotInCharset_thenRemoved() {
final var charset = Charset.forName("cp437");
final var encoder = charset.newEncoder();
final var inRange = new Predicate<Character>() {
@Override
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
final var inRange =
new Predicate<Character>() {
@Override
public boolean apply(final Character c) {
return encoder.canEncode(c);
}
};
final var result = CharMatcher.forPredicate(inRange).retainFrom("helloは");
assertEquals("hello", result);
}
Expand All @@ -56,7 +56,8 @@ void whenRemoveNonASCIIChars_thenRemoved() {
@Test
void whenRemoveSpecialCharacters_thenRemoved() {
final var input = "H:*el.lo,}12";
final var matcher = CharMatcher.forPredicate(c -> Objects.nonNull(c) && Character.isLetterOrDigit(c));
final var matcher =
CharMatcher.forPredicate(c -> Objects.nonNull(c) && Character.isLetterOrDigit(c));
final var result = matcher.retainFrom(input);
assertEquals("Hello12", result);
}
Expand Down
Loading