From 899700f27e7357cdb27653c0cb7be1a06323888d Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Fri, 3 Nov 2023 15:45:12 +0700 Subject: [PATCH 1/4] [Update] add time field --- .../UUIDPositiveLongGeneratorUnitTest.java | 44 ++++++++++++++++--- 1 file changed, 37 insertions(+), 7 deletions(-) diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java index fab07df2a0..62b059f0e5 100644 --- a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java @@ -6,8 +6,7 @@ import org.slf4j.LoggerFactory; import java.lang.reflect.Method; import java.text.DecimalFormat; -import java.util.HashSet; -import java.util.Set; +import java.util.*; import static org.assertj.core.api.Assertions.assertThat; @@ -17,8 +16,11 @@ public class UUIDPositiveLongGeneratorUnitTest { private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator(); private final Logger logger = LoggerFactory.getLogger(UUIDPositiveLongGeneratorUnitTest.class); + List times = new ArrayList<>(); + @Test void whenForeachGenerateLongValue_thenCollisionsCheck() throws Exception { + times.clear(); printTableHeader(); for (Method method : uuidLongGenerator.getClass().getDeclaredMethods()) { collisionCheck(method); @@ -26,16 +28,17 @@ public class UUIDPositiveLongGeneratorUnitTest { } private void printTableHeader() { - logger.info(String.format("%-30s %-15s %-15s", "Approach(method name)", "collisions", "probability")); - logger.info("-----------------------------------------------------------------------"); + logger.info(String.format("%-30s %15s %15s %15s", "Approach(method name)", "collisions", "probability","Time")); + logger.info("--------------------------------------------------------------------------------"); } - private void printOutput(String method, int collisionsCount, double collisionsProbability) { + private void printOutput(String method, int collisionsCount, double collisionsProbability, String time) { DecimalFormat decimalFormat = new DecimalFormat("#.#####"); - logger.info(String.format("%-30s %-15s %-15s", method, collisionsCount, decimalFormat.format(collisionsProbability))); + logger.info(String.format("%-30s %15s %15s %15s", method, collisionsCount, decimalFormat.format(collisionsProbability),time)); } private void collisionCheck(Method method) throws Exception { + long start = System.currentTimeMillis(); Set uniqueValues = new HashSet<>(); int collisions = 0; for (int i = 0; i < NUMBER_OF_CHECKS; i++) { @@ -46,7 +49,34 @@ public class UUIDPositiveLongGeneratorUnitTest { } } double collisionsProbability = (double) collisions / NUMBER_OF_CHECKS; - printOutput(method.getName(), collisions, collisionsProbability); + long end = System.currentTimeMillis(); + String time = convertMillisToTime(end - start); + printOutput(method.getName(), collisions, collisionsProbability, time); assertThat(collisionsProbability).isLessThan(COLLISION_THRESHOLD); + + } + + private String convertMillisToTime(long currentTimeMillis) { + + long totalMilliseconds = currentTimeMillis % 1000; + times.add(totalMilliseconds); + long totalSeconds = (currentTimeMillis / 1000) % 60; + long totalMinutes = (currentTimeMillis / 60000) % 60; + long hours = (currentTimeMillis / 3600000); + StringJoiner stringBuffer = new StringJoiner(""); + + if (hours > 0){ + stringBuffer.add(hours+"h "); + } + if (totalMinutes > 0){ + stringBuffer.add(totalMinutes+" m "); + } + if (totalSeconds > 0){ + stringBuffer.add(totalSeconds+" s "); + } + if (totalMilliseconds > 0){ + stringBuffer.add(totalMilliseconds+" ms"); + } + return stringBuffer.toString(); } } From 32940c41e4a6f0db1a29e2093f7c79ae83e7e2d9 Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Sat, 4 Nov 2023 15:28:49 +0700 Subject: [PATCH 2/4] update time test --- .../com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java index 62b059f0e5..70b47df163 100644 --- a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java @@ -28,13 +28,13 @@ public class UUIDPositiveLongGeneratorUnitTest { } private void printTableHeader() { - logger.info(String.format("%-30s %15s %15s %15s", "Approach(method name)", "collisions", "probability","Time")); + logger.info(String.format("%-25s %15s %15s %15s", "Approach(method name)", "collisions", "probability","Testing Time")); logger.info("--------------------------------------------------------------------------------"); } private void printOutput(String method, int collisionsCount, double collisionsProbability, String time) { DecimalFormat decimalFormat = new DecimalFormat("#.#####"); - logger.info(String.format("%-30s %15s %15s %15s", method, collisionsCount, decimalFormat.format(collisionsProbability),time)); + logger.info(String.format("%-25s %15s %15s %15s", method, collisionsCount, decimalFormat.format(collisionsProbability),time)); } private void collisionCheck(Method method) throws Exception { From 0c8b5bd210b716979891651a7af36582987c6183 Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Sat, 18 Nov 2023 06:09:14 +0700 Subject: [PATCH 3/4] retry with colision --- .../baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java index 70b47df163..27ac4fe51e 100644 --- a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java @@ -56,6 +56,13 @@ public class UUIDPositiveLongGeneratorUnitTest { } + private boolean isUnique(long value) { + // Implementasikan logika pengecekan keunikan, misalnya, dengan memeriksa dalam database + // Mengembalikan true jika nilai unik, false jika ada tumbukan + return true; // Implementasikan sesuai kebutuhan aplikasi Anda + } + + private String convertMillisToTime(long currentTimeMillis) { long totalMilliseconds = currentTimeMillis % 1000; From 213fb362a0f94947f85e85eb9dafd4637f1daf1c Mon Sep 17 00:00:00 2001 From: "@hangga" Date: Sat, 18 Nov 2023 14:17:19 +0700 Subject: [PATCH 4/4] simplyfiying test --- .../UUIDPositiveLongGeneratorUnitTest.java | 77 +++---------------- 1 file changed, 10 insertions(+), 67 deletions(-) diff --git a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java index 27ac4fe51e..756bee94e6 100644 --- a/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java +++ b/core-java-modules/core-java-uuid/src/test/java/com/baeldung/uuid/UUIDPositiveLongGeneratorUnitTest.java @@ -1,89 +1,32 @@ package com.baeldung.uuid; import org.junit.jupiter.api.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import java.lang.reflect.Method; -import java.text.DecimalFormat; -import java.util.*; +import java.util.HashSet; +import java.util.Set; import static org.assertj.core.api.Assertions.assertThat; public class UUIDPositiveLongGeneratorUnitTest { - private final static int NUMBER_OF_CHECKS = 1000000; - private final static double COLLISION_THRESHOLD = 0.001; - private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator(); - private final Logger logger = LoggerFactory.getLogger(UUIDPositiveLongGeneratorUnitTest.class); - List times = new ArrayList<>(); + //private final UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator(); + + Set uniqueValues = new HashSet<>(); @Test void whenForeachGenerateLongValue_thenCollisionsCheck() throws Exception { - times.clear(); - printTableHeader(); + UUIDPositiveLongGenerator uuidLongGenerator = new UUIDPositiveLongGenerator(); for (Method method : uuidLongGenerator.getClass().getDeclaredMethods()) { - collisionCheck(method); - } - } - - private void printTableHeader() { - logger.info(String.format("%-25s %15s %15s %15s", "Approach(method name)", "collisions", "probability","Testing Time")); - logger.info("--------------------------------------------------------------------------------"); - } - - private void printOutput(String method, int collisionsCount, double collisionsProbability, String time) { - DecimalFormat decimalFormat = new DecimalFormat("#.#####"); - logger.info(String.format("%-25s %15s %15s %15s", method, collisionsCount, decimalFormat.format(collisionsProbability),time)); - } - - private void collisionCheck(Method method) throws Exception { - long start = System.currentTimeMillis(); - Set uniqueValues = new HashSet<>(); - int collisions = 0; - for (int i = 0; i < NUMBER_OF_CHECKS; i++) { - long uniqueValue = (long) method.invoke(uuidLongGenerator); + long uniqueValue; + do uniqueValue = (long) method.invoke(uuidLongGenerator); while (!isUnique(uniqueValue)); assertThat(uniqueValue).isPositive(); - if (!uniqueValues.add(uniqueValue)) { - collisions++; - } } - double collisionsProbability = (double) collisions / NUMBER_OF_CHECKS; - long end = System.currentTimeMillis(); - String time = convertMillisToTime(end - start); - printOutput(method.getName(), collisions, collisionsProbability, time); - assertThat(collisionsProbability).isLessThan(COLLISION_THRESHOLD); - } private boolean isUnique(long value) { - // Implementasikan logika pengecekan keunikan, misalnya, dengan memeriksa dalam database - // Mengembalikan true jika nilai unik, false jika ada tumbukan - return true; // Implementasikan sesuai kebutuhan aplikasi Anda + // Implement uniqueness checking logic, for example, by checking in the database + return uniqueValues.add(value); } - - private String convertMillisToTime(long currentTimeMillis) { - - long totalMilliseconds = currentTimeMillis % 1000; - times.add(totalMilliseconds); - long totalSeconds = (currentTimeMillis / 1000) % 60; - long totalMinutes = (currentTimeMillis / 60000) % 60; - long hours = (currentTimeMillis / 3600000); - StringJoiner stringBuffer = new StringJoiner(""); - - if (hours > 0){ - stringBuffer.add(hours+"h "); - } - if (totalMinutes > 0){ - stringBuffer.add(totalMinutes+" m "); - } - if (totalSeconds > 0){ - stringBuffer.add(totalSeconds+" s "); - } - if (totalMilliseconds > 0){ - stringBuffer.add(totalMilliseconds+" ms"); - } - return stringBuffer.toString(); - } }