summaryrefslogtreecommitdiff
path: root/graphs/java/test2/src
diff options
context:
space:
mode:
authorMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
committerMartial Simon <msimon_fr@hotmail.com>2025-09-15 01:08:27 +0200
commitc9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch)
tree3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/java/test2/src
add: graphs et rushs
Diffstat (limited to 'graphs/java/test2/src')
-rw-r--r--graphs/java/test2/src/main/java/fr/epita/assistants/server/MyServer.java42
-rw-r--r--graphs/java/test2/src/main/java/fr/epita/assistants/test2/Test2.java48
-rw-r--r--graphs/java/test2/src/test/java/fr/epita/assistants/test2/Test2Test.java105
3 files changed, 195 insertions, 0 deletions
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));
+ }
+}