Commit 4a32243d authored by Colin DAMON's avatar Colin DAMON
Browse files

Merge branch '104-java-puzzle-code' into 'master'

Resolve "Java puzzle code"

Closes #104

See merge request !79
parents fca2359e 2a67fb19
...@@ -35,3 +35,4 @@ include: ...@@ -35,3 +35,4 @@ include:
- local: "/tennis/greenfield-2/.gitlab-ci.yml" - local: "/tennis/greenfield-2/.gitlab-ci.yml"
- local: "/mastermind/.gitlab-ci.yml" - local: "/mastermind/.gitlab-ci.yml"
- local: "/try-monade/.gitlab-ci.yml" - local: "/try-monade/.gitlab-ci.yml"
- local: "/java-puzzles/.gitlab-ci.yml"
package-java-puzzles:
variables:
PROJECT_FOLDER: "java-puzzles"
extends: .java-no-jacoco
only:
refs:
- master
- merge_requests
changes:
- ".gitlab-common-ci.yml"
- "java-puzzles/**/*"
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<version>1.0.0</version>
<groupId>fr.ippon.kata</groupId>
<artifactId>java-parent</artifactId>
<relativePath>../java-parent</relativePath>
</parent>
<version>1.0.0-SNAPSHOT</version>
<artifactId>java-puzzles</artifactId>
<name>JavaPuzzles</name>
<developers>
<developer>
<email>cdamon@ippon.fr</email>
<name>Colin DAMON</name>
</developer>
</developers>
</project>
# Java puzzles
Petits exercices de code Java
- **Auteurs** : Colin DAMON
- **Date** : 22/04/2021
- **Langage** : Java
- **Niveau** : TODO
- **Replay** : TODO
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import java.math.BigDecimal;
import org.junit.jupiter.api.Test;
public class PlayingWithBigDecimal {
@Test
public void roundingSubstract() {
BigDecimal a = new BigDecimal(2.00);
BigDecimal b = new BigDecimal(0.99);
// assertThat(a.subtract(b).toString()).isEqualTo("1");
// assertThat(a.subtract(b).toString()).isEqualTo("1.01");
assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625");
}
}
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PlayingWithFinally {
@Test
public void playingWithReturn() {
// assertThat(finallyReturn()).isEqualTo(42);
assertThat(finallyReturn()).isEqualTo(1337);
}
@SuppressWarnings("finally")
private int finallyReturn() {
try {
return 42;
} finally {
return 1337;
}
}
@Test
public void playingWithThrow() {
// assertThat(finallyThrow()).isEqualTo(42);
assertThatThrownBy(() -> finallyThrow()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Damned");
}
@SuppressWarnings("finally")
private int finallyThrow() {
try {
return 42;
} finally {
throw new IllegalArgumentException("Damned");
}
}
}
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PlayingWithInstanciations {
private static final long instanciation = System.currentTimeMillis();
@Test
public void playingWithInstanciationTime() throws InterruptedException {
Thread.sleep(10);
long start = System.currentTimeMillis();
// assertThat(start).isLessThanOrEqualTo(instanciation);
assertThat(start).isGreaterThan(instanciation);
MyPrivateClass.doStuff();
assertThat(start).isLessThanOrEqualTo(MyPrivateClass.instanciation);
// assertThat(start).isGreaterThan(MyPrivateClass.instanciation);
}
private static class MyPrivateClass {
private static final long instanciation = System.currentTimeMillis();
private static void doStuff() {}
}
}
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PlayingWithInteger {
@Test
public void comparingUniversaleAnswerIntegerReferences() {
Integer a = 42;
Integer b = 42;
assertThat(a).isSameAs(b);
// assertThat(a).isNotSameAs(b);
}
@Test
public void comparingDevilIntegerReferences() {
Integer a = 666;
Integer b = 666;
// assertThat(a).isSameAs(b);
assertThat(a).isNotSameAs(b);
}
@Test
public void checkingAbsoluteValue() {
// assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE);
assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MIN_VALUE);
}
}
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
public class PlayingWithString {
@Test
public void playingWithReferences() {
assertThat("coucou").isSameAs("coucou");
// assertThat("coucou").isNotSameAs("coucou");
}
@Test
public void playingWithStringObjectsReferences() {
// assertThat(new String("coucou")).isSameAs(new String("coucou"));
assertThat(new String("coucou")).isNotSameAs(new String("coucou"));
}
@Test
public void playingWithIntern() {
assertThat(new String("coucou").intern()).isSameAs(new String("coucou").intern());
// assertThat(new String("coucou").intern()).isNotSameAs(new
// String("coucou").intern());
}
}
package fr.ippon;
import static org.assertj.core.api.Assertions.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Test;
public class PlayingWithThreads {
private static boolean threadDoneFlag;
private static AtomicBoolean threadDoneAtomicFlag = new AtomicBoolean(false);
@Test
public void playingWithSimpleDateFormat() throws InterruptedException {
Set<Integer> days = Collections.newSetFromMap(new ConcurrentHashMap<>());
ExecutorService executor = Executors.newFixedThreadPool(10);
SimpleDateFormat format = new SimpleDateFormat("dd");
AtomicInteger counter = new AtomicInteger(1);
for (int i = 1; i < 32; ++i) {
executor.submit(
() -> {
Date date = new GregorianCalendar(2018, Calendar.JANUARY, counter.getAndIncrement()).getTime();
days.add(Integer.parseInt(format.format(date)));
}
);
}
executor.shutdown();
executor.awaitTermination(3, TimeUnit.SECONDS);
// assertThat(days).hasSize(31);
assertThat(days.size()).isLessThan(31);
}
@Test
public void firstAttemptToStopAThread() throws InterruptedException {
AtomicBoolean threadStopped = new AtomicBoolean();
new Thread(
() -> {
while (!threadDoneFlag) {}
threadStopped.set(true);
}
)
.start();
Thread.sleep(500);
threadDoneFlag = true;
Thread.sleep(50);
assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue();
}
@Test
public void secondAttemptToStopAThread() throws InterruptedException {
AtomicBoolean threadStopped = new AtomicBoolean();
new Thread(
() -> {
while (!threadDoneFlag) {
System.out.println(threadDoneFlag); // Ok, let's just log the flag
}
threadStopped.set(true);
}
)
.start();
Thread.sleep(500);
threadDoneFlag = true;
Thread.sleep(50);
// assertThat(threadStopped.get()).isFalse();
assertThat(threadStopped.get()).isTrue();
}
@Test
public void thirdAttemptToStopAThread() throws InterruptedException {
AtomicBoolean threadStopped = new AtomicBoolean();
new Thread(
() -> {
while (!threadDoneAtomicFlag.get()) {}
threadStopped.set(true);
}
)
.start();
Thread.sleep(500);
threadDoneAtomicFlag.set(true);
Thread.sleep(50);
// assertThat(threadStopped.get()).isFalse();
assertThat(threadStopped.get()).isTrue();
}
}
...@@ -43,6 +43,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser ...@@ -43,6 +43,7 @@ Un kata de code est un petit exercice pensé pour s'entrainer jusqu'à maitriser
- [Movie rental](/movie-rental) - [Movie rental](/movie-rental)
- [Bowling Game](/bowling-game) - [Bowling Game](/bowling-game)
- [Tennis Refactoring kata](/tennis/refactoring) - [Tennis Refactoring kata](/tennis/refactoring)
- [Puzzles](java-puzzles)
### Énervé ### Énervé
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment