From c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c Mon Sep 17 00:00:00 2001 From: Martial Simon Date: Mon, 15 Sep 2025 01:08:27 +0200 Subject: add: graphs et rushs --- graphs/java/test2/pom.xml | 141 +++++++++++++++++++++ .../java/fr/epita/assistants/server/MyServer.java | 42 ++++++ .../main/java/fr/epita/assistants/test2/Test2.java | 48 +++++++ .../java/fr/epita/assistants/test2/Test2Test.java | 105 +++++++++++++++ 4 files changed, 336 insertions(+) create mode 100644 graphs/java/test2/pom.xml create mode 100644 graphs/java/test2/src/main/java/fr/epita/assistants/server/MyServer.java create mode 100644 graphs/java/test2/src/main/java/fr/epita/assistants/test2/Test2.java create mode 100644 graphs/java/test2/src/test/java/fr/epita/assistants/test2/Test2Test.java (limited to 'graphs/java/test2') diff --git a/graphs/java/test2/pom.xml b/graphs/java/test2/pom.xml new file mode 100644 index 0000000..f0ab353 --- /dev/null +++ b/graphs/java/test2/pom.xml @@ -0,0 +1,141 @@ + + + 4.0.0 + fr.epita.assistants + test2 + 1.0 + + + 21 + 5.9.1 + 3.13.0 + 3.5.0 + 3.1.1 + 3.1.0 + 1.7.36 + 1.2.0 + + UTF-8 + + ${project.build.directory}/surefire-reports + + + + + org.junit.jupiter + junit-jupiter + ${versions.junit} + + + com.tngtech.archunit + archunit-junit5 + ${versions.archunit} + test + + + org.apache.maven.surefire + surefire-junit-platform + ${versions.maven-surefire-plugin} + + + org.apache.maven + maven-compat + 3.9.8 + + + org.apache.maven + maven-plugin-api + 3.9.8 + + + org.apache.maven + maven-project + 2.2.1 + + + org.apache.maven + maven-core + 3.8.1 + + + org.apache.maven + maven-monitor + 2.2.1 + + + org.codehaus.plexus + plexus-utils + 3.0.24 + + + org.apache.maven.shared + maven-filtering + 3.3.2 + + + org.codehaus.plexus + plexus-interpolation + 1.13 + + + org.apache.maven + maven-profile + 2.2.1 + + + org.apache.maven + maven-artifact-manager + 2.2.1 + + + org.apache.maven + maven-plugin-registry + 2.2.1 + + + org.apache.maven + maven-repository-metadata + 2.2.1 + + + classworlds + classworlds + 1.1 + + + org.junit.platform + junit-platform-commons + 1.9.3 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + ${versions.maven-compiler-plugin} + + ${versions.java} + ${versions.java} + + + + org.apache.maven.plugins + maven-install-plugin + ${versions.maven-install-plugin} + + + + org.apache.maven.plugins + maven-surefire-plugin + ${versions.maven-surefire-plugin} + + ${surefire.reportsDirectory} + + + + + diff --git a/graphs/java/test2/src/main/java/fr/epita/assistants/server/MyServer.java b/graphs/java/test2/src/main/java/fr/epita/assistants/server/MyServer.java new file mode 100644 index 0000000..9a2a213 --- /dev/null +++ b/graphs/java/test2/src/main/java/fr/epita/assistants/server/MyServer.java @@ -0,0 +1,42 @@ +package fr.epita.assistants.server; + +import com.sun.net.httpserver.HttpServer; + +import java.io.IOException; +import java.net.InetSocketAddress; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; + +import static java.net.HttpURLConnection.HTTP_OK; + +public class MyServer { + + private static HttpServer server = null; + private static ExecutorService executor; + + + public static void launchServer() throws IOException { + server = HttpServer.create(new InetSocketAddress("localhost", 8080), 0); + executor = Executors.newFixedThreadPool(10); + server.setExecutor(executor); + var context = server.createContext("/"); + context.setHandler((arg) -> { + try { + Thread.sleep(1500); + arg.sendResponseHeaders(HTTP_OK, 0); + arg.close(); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + }); + server.start(); + } + + + public static void stopServer() { + if (server == null) + return; + server.stop(0); + executor.shutdownNow(); + } +} diff --git a/graphs/java/test2/src/main/java/fr/epita/assistants/test2/Test2.java b/graphs/java/test2/src/main/java/fr/epita/assistants/test2/Test2.java new file mode 100644 index 0000000..8427b43 --- /dev/null +++ b/graphs/java/test2/src/main/java/fr/epita/assistants/test2/Test2.java @@ -0,0 +1,48 @@ +package fr.epita.assistants.test2; + +import java.io.IOException; +import java.net.HttpURLConnection; +import java.net.URL; + +public class Test2 { + /** + * Computes the nth value of the tribonacci suite. + * f(0) = 0, f(1) = 1, f(2) = 1, f(n+3) = f(n) + f(n+1) + f(n+2) + * + * @param n the nth sequence to compute + */ + public static long tribonacci(int n) { + if (n < 0) + throw new IllegalArgumentException("Error: n must be positive"); + + if (n == 0) + return 0; + if (n < 3) + return 1; + + long one = 0; + long two = 1; + long three = 1; + long res = 0; + + for (long i = 3; i <= n; i++) { + res = one + two + three; + one = two; + two = three; + three = res; + } + + return res; + } + + public static long serverGetResponseCode() throws IOException { + URL url = new URL("http://localhost:8080/"); + HttpURLConnection con = (HttpURLConnection) url.openConnection(); + con.setRequestMethod("GET"); + return con.getResponseCode(); + } + + public static int division(int a, int b) { + return a / b; + } +} diff --git a/graphs/java/test2/src/test/java/fr/epita/assistants/test2/Test2Test.java b/graphs/java/test2/src/test/java/fr/epita/assistants/test2/Test2Test.java new file mode 100644 index 0000000..0318dc2 --- /dev/null +++ b/graphs/java/test2/src/test/java/fr/epita/assistants/test2/Test2Test.java @@ -0,0 +1,105 @@ +package fr.epita.assistants.test2; + +import fr.epita.assistants.server.MyServer; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.Timeout; + +import java.io.IOException; +import java.net.ConnectException; + +import static fr.epita.assistants.test2.Test2.*; +import static org.junit.jupiter.api.Assertions.*; + +public class Test2Test { + @Test + void divPass() { + assertEquals(2, division(5, 2)); + } + + @Test + void divFZero() { + assertThrows(ArithmeticException.class, () -> division(5, 0)); + } + @Test + void divZeroZero() { + assertThrows(ArithmeticException.class, () -> division(0, 0)); + } + + @Test + @Timeout(1) + void divTimeout() { + assertEquals(0, division(0, 2)); + } + @Test + @Timeout(1) + void divTimeoutFatAss() { + assertNotEquals(554468611, division(745343524, 5546344)); + } + + @Test + void divServerPass() throws IOException { + MyServer.stopServer(); + MyServer.launchServer(); + long actual = serverGetResponseCode(); + assertEquals(200, actual); + MyServer.stopServer(); + } + @Test + void divServerSpam() throws IOException { + MyServer.stopServer(); + MyServer.launchServer(); + long actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + actual = serverGetResponseCode(); + assertEquals(200, actual); + + MyServer.stopServer(); + } + @Test + void divServerNotStarted() { + MyServer.stopServer(); + assertThrows(ConnectException.class, () -> serverGetResponseCode()); + } + @Test + @Timeout(1) + void divServerTimeout() throws IOException { + MyServer.stopServer(); + MyServer.launchServer(); + long actual = serverGetResponseCode(); + assertEquals(200, actual); + MyServer.stopServer(); + } + @Test + @Timeout(1) + void triboFail() { + assertThrows(IllegalArgumentException.class, () -> tribonacci(-1)); + } + @Test + @Timeout(1) + void triboPass() { + assertEquals(0, tribonacci(0)); + assertEquals(1, tribonacci(1)); + assertEquals(1, tribonacci(2)); + assertEquals(2, tribonacci(3)); + assertEquals(4, tribonacci(4)); + assertEquals(7, tribonacci(5)); + assertEquals(13, tribonacci(6)); + assertEquals(1, tribonacci(7)); + assertEquals(1, tribonacci(8)); + } + @Test + @Timeout(1) + void triboTime() { + assertEquals(-1, tribonacci(Integer.MAX_VALUE)); + } +} -- cgit v1.2.3