diff options
| author | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
|---|---|---|
| committer | Martial Simon <msimon_fr@hotmail.com> | 2025-09-15 01:08:27 +0200 |
| commit | c9b6b9a5ca082fe7c1b6f58d7713f785a9eb6a5c (patch) | |
| tree | 3e4f42f93c7ae89a364e4d51fff6e5cec4e55fa9 /graphs/java/test2 | |
add: graphs et rushs
Diffstat (limited to 'graphs/java/test2')
4 files changed, 336 insertions, 0 deletions
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 @@ +<?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> + <groupId>fr.epita.assistants</groupId> + <artifactId>test2</artifactId> + <version>1.0</version> + + <properties> + <versions.java>21</versions.java> + <versions.junit>5.9.1</versions.junit> + <versions.maven-compiler-plugin>3.13.0</versions.maven-compiler-plugin> + <versions.maven-surefire-plugin>3.5.0</versions.maven-surefire-plugin> + <versions.maven-jar-plugin>3.1.1</versions.maven-jar-plugin> + <versions.maven-install-plugin>3.1.0</versions.maven-install-plugin> + <versions.slf4j>1.7.36</versions.slf4j> + <versions.archunit>1.2.0</versions.archunit> + + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + + <surefire.reportsDirectory>${project.build.directory}/surefire-reports</surefire.reportsDirectory> + </properties> + + <dependencies> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>${versions.junit}</version> + </dependency> + <dependency> + <groupId>com.tngtech.archunit</groupId> + <artifactId>archunit-junit5</artifactId> + <version>${versions.archunit}</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-junit-platform</artifactId> + <version>${versions.maven-surefire-plugin}</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-compat</artifactId> + <version>3.9.8</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-api</artifactId> + <version>3.9.8</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-project</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-core</artifactId> + <version>3.8.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-monitor</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-utils</artifactId> + <version>3.0.24</version> + </dependency> + <dependency> + <groupId>org.apache.maven.shared</groupId> + <artifactId>maven-filtering</artifactId> + <version>3.3.2</version> + </dependency> + <dependency> + <groupId>org.codehaus.plexus</groupId> + <artifactId>plexus-interpolation</artifactId> + <version>1.13</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-profile</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-artifact-manager</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-plugin-registry</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>org.apache.maven</groupId> + <artifactId>maven-repository-metadata</artifactId> + <version>2.2.1</version> + </dependency> + <dependency> + <groupId>classworlds</groupId> + <artifactId>classworlds</artifactId> + <version>1.1</version> + </dependency> + <dependency> + <groupId>org.junit.platform</groupId> + <artifactId>junit-platform-commons</artifactId> + <version>1.9.3</version> + </dependency> + </dependencies> + + <build> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-compiler-plugin</artifactId> + <version>${versions.maven-compiler-plugin}</version> + <configuration> + <source>${versions.java}</source> + <target>${versions.java}</target> + </configuration> + </plugin> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-install-plugin</artifactId> + <version>${versions.maven-install-plugin}</version> + </plugin> + + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>${versions.maven-surefire-plugin}</version> + <configuration> + <reportsDirectory>${surefire.reportsDirectory}</reportsDirectory> + </configuration> + </plugin> + </plugins> + </build> +</project> 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)); + } +} |
