Commit 2a67fb19 authored by Colin DAMON's avatar Colin DAMON
Browse files

Puzzles resolution

parent 6079210f
package-java-puzzles: package-java-puzzles:
variables: variables:
PROJECT_FOLDER: "java-puzzles" PROJECT_FOLDER: "java-puzzles"
extends: .java extends: .java-no-jacoco
only: only:
refs: refs:
- master - master
......
...@@ -11,8 +11,9 @@ public class PlayingWithBigDecimal { ...@@ -11,8 +11,9 @@ public class PlayingWithBigDecimal {
public void roundingSubstract() { public void roundingSubstract() {
BigDecimal a = new BigDecimal(2.00); BigDecimal a = new BigDecimal(2.00);
BigDecimal b = new BigDecimal(0.99); 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");
// assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625"); // assertThat(a.subtract(b).toString()).isEqualTo("1.01");
assertThat(a.subtract(b).toString()).isEqualTo("1.0100000000000000088817841970012523233890533447265625");
} }
} }
...@@ -9,7 +9,7 @@ public class PlayingWithFinally { ...@@ -9,7 +9,7 @@ public class PlayingWithFinally {
@Test @Test
public void playingWithReturn() { public void playingWithReturn() {
// assertThat(finallyReturn()).isEqualTo(42); // assertThat(finallyReturn()).isEqualTo(42);
// assertThat(finallyReturn()).isEqualTo(1337); assertThat(finallyReturn()).isEqualTo(1337);
} }
@SuppressWarnings("finally") @SuppressWarnings("finally")
...@@ -24,9 +24,7 @@ public class PlayingWithFinally { ...@@ -24,9 +24,7 @@ public class PlayingWithFinally {
@Test @Test
public void playingWithThrow() { public void playingWithThrow() {
// assertThat(finallyThrow()).isEqualTo(42); // assertThat(finallyThrow()).isEqualTo(42);
// assertThatThrownBy(() -> finallyThrow()) assertThatThrownBy(() -> finallyThrow()).isInstanceOf(IllegalArgumentException.class).hasMessageContaining("Damned");
// .isInstanceOf(IllegalArgumentException.class)
// .hasMessageContaining("Damned");
} }
@SuppressWarnings("finally") @SuppressWarnings("finally")
......
...@@ -13,9 +13,9 @@ public class PlayingWithInstanciations { ...@@ -13,9 +13,9 @@ public class PlayingWithInstanciations {
long start = System.currentTimeMillis(); long start = System.currentTimeMillis();
// assertThat(start).isLessThanOrEqualTo(instanciation); // assertThat(start).isLessThanOrEqualTo(instanciation);
// assertThat(start).isGreaterThan(instanciation); assertThat(start).isGreaterThan(instanciation);
MyPrivateClass.doStuff(); MyPrivateClass.doStuff();
// assertThat(start).isLessThanOrEqualTo(MyPrivateClass.instanciation); assertThat(start).isLessThanOrEqualTo(MyPrivateClass.instanciation);
// assertThat(start).isGreaterThan(MyPrivateClass.instanciation); // assertThat(start).isGreaterThan(MyPrivateClass.instanciation);
} }
......
...@@ -10,7 +10,8 @@ public class PlayingWithInteger { ...@@ -10,7 +10,8 @@ public class PlayingWithInteger {
public void comparingUniversaleAnswerIntegerReferences() { public void comparingUniversaleAnswerIntegerReferences() {
Integer a = 42; Integer a = 42;
Integer b = 42; Integer b = 42;
// assertThat(a).isSameAs(b);
assertThat(a).isSameAs(b);
// assertThat(a).isNotSameAs(b); // assertThat(a).isNotSameAs(b);
} }
...@@ -18,14 +19,14 @@ public class PlayingWithInteger { ...@@ -18,14 +19,14 @@ public class PlayingWithInteger {
public void comparingDevilIntegerReferences() { public void comparingDevilIntegerReferences() {
Integer a = 666; Integer a = 666;
Integer b = 666; Integer b = 666;
// assertThat(a).isSameAs(b);
// assertThat(a).isNotSameAs(b); // assertThat(a).isSameAs(b);
assertThat(a).isNotSameAs(b);
} }
@Test @Test
public void checkingAbsoluteValue() { public void checkingAbsoluteValue() {
// assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE); // assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MAX_VALUE);
// assertThat(Math.abs(Integer.MIN_VALUE)) assertThat(Math.abs(Integer.MIN_VALUE)).isEqualTo(Integer.MIN_VALUE);
// .isEqualTo(Integer.MIN_VALUE);
} }
} }
...@@ -8,20 +8,19 @@ public class PlayingWithString { ...@@ -8,20 +8,19 @@ public class PlayingWithString {
@Test @Test
public void playingWithReferences() { public void playingWithReferences() {
// assertThat("coucou").isSameAs("coucou"); assertThat("coucou").isSameAs("coucou");
// assertThat("coucou").isNotSameAs("coucou"); // assertThat("coucou").isNotSameAs("coucou");
} }
@Test @Test
public void playingWithStringObjectsReferences() { public void playingWithStringObjectsReferences() {
// assertThat(new String("coucou")).isSameAs(new String("coucou")); // assertThat(new String("coucou")).isSameAs(new String("coucou"));
// assertThat(new String("coucou")).isNotSameAs(new String("coucou")); assertThat(new String("coucou")).isNotSameAs(new String("coucou"));
} }
@Test @Test
public void playingWithIntern() { public void playingWithIntern() {
// assertThat(new String("coucou").intern()) assertThat(new String("coucou").intern()).isSameAs(new String("coucou").intern());
// .isSameAs(new String("coucou").intern());
// assertThat(new String("coucou").intern()).isNotSameAs(new // assertThat(new String("coucou").intern()).isNotSameAs(new
// String("coucou").intern()); // String("coucou").intern());
} }
......
...@@ -26,11 +26,11 @@ public class PlayingWithThreads { ...@@ -26,11 +26,11 @@ public class PlayingWithThreads {
Set<Integer> days = Collections.newSetFromMap(new ConcurrentHashMap<>()); Set<Integer> days = Collections.newSetFromMap(new ConcurrentHashMap<>());
ExecutorService executor = Executors.newFixedThreadPool(10); ExecutorService executor = Executors.newFixedThreadPool(10);
SimpleDateFormat format = new SimpleDateFormat("dd");
AtomicInteger counter = new AtomicInteger(1); AtomicInteger counter = new AtomicInteger(1);
for (int i = 1; i < 32; ++i) { for (int i = 1; i < 32; ++i) {
executor.submit( executor.submit(
() -> { () -> {
SimpleDateFormat format = new SimpleDateFormat("dd");
Date date = new GregorianCalendar(2018, Calendar.JANUARY, counter.getAndIncrement()).getTime(); Date date = new GregorianCalendar(2018, Calendar.JANUARY, counter.getAndIncrement()).getTime();
days.add(Integer.parseInt(format.format(date))); days.add(Integer.parseInt(format.format(date)));
} }
...@@ -39,8 +39,9 @@ public class PlayingWithThreads { ...@@ -39,8 +39,9 @@ public class PlayingWithThreads {
executor.shutdown(); executor.shutdown();
executor.awaitTermination(3, TimeUnit.SECONDS); executor.awaitTermination(3, TimeUnit.SECONDS);
// assertThat(days).hasSize(31);
// assertThat(days.size()).isLessThan(31); // assertThat(days).hasSize(31);
assertThat(days.size()).isLessThan(31);
} }
@Test @Test
...@@ -61,7 +62,7 @@ public class PlayingWithThreads { ...@@ -61,7 +62,7 @@ public class PlayingWithThreads {
threadDoneFlag = true; threadDoneFlag = true;
Thread.sleep(50); Thread.sleep(50);
// assertThat(threadStopped.get()).isFalse(); assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue(); // assertThat(threadStopped.get()).isTrue();
} }
...@@ -85,8 +86,9 @@ public class PlayingWithThreads { ...@@ -85,8 +86,9 @@ public class PlayingWithThreads {
threadDoneFlag = true; threadDoneFlag = true;
Thread.sleep(50); Thread.sleep(50);
// assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue(); // assertThat(threadStopped.get()).isFalse();
assertThat(threadStopped.get()).isTrue();
} }
@Test @Test
...@@ -107,7 +109,8 @@ public class PlayingWithThreads { ...@@ -107,7 +109,8 @@ public class PlayingWithThreads {
threadDoneAtomicFlag.set(true); threadDoneAtomicFlag.set(true);
Thread.sleep(50); Thread.sleep(50);
// assertThat(threadStopped.get()).isFalse();
// assertThat(threadStopped.get()).isTrue(); // assertThat(threadStopped.get()).isFalse();
assertThat(threadStopped.get()).isTrue();
} }
} }
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