chap3
This commit is contained in:
parent
990b77ed18
commit
babed7eb09
9
bac2/modalDev/tp1/.gitattributes
vendored
Normal file
9
bac2/modalDev/tp1/.gitattributes
vendored
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
#
|
||||||
|
# https://help.github.com/articles/dealing-with-line-endings/
|
||||||
|
#
|
||||||
|
# Linux start script should use lf
|
||||||
|
/gradlew text eol=lf
|
||||||
|
|
||||||
|
# These are Windows script files and should use crlf
|
||||||
|
*.bat text eol=crlf
|
||||||
|
|
5
bac2/modalDev/tp1/.gitignore
vendored
Normal file
5
bac2/modalDev/tp1/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
# Ignore Gradle project-specific cache directory
|
||||||
|
.gradle
|
||||||
|
|
||||||
|
# Ignore Gradle build output directory
|
||||||
|
build
|
28
bac2/modalDev/tp1/.project
Normal file
28
bac2/modalDev/tp1/.project
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>tp1</name>
|
||||||
|
<comment>Project tp1 created by Buildship.</comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1701330804548</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
13
bac2/modalDev/tp1/.settings/org.eclipse.buildship.core.prefs
Normal file
13
bac2/modalDev/tp1/.settings/org.eclipse.buildship.core.prefs
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
arguments=--init-script /home/tonitch/.dotfiles/vim/.vim/bundle/YouCompleteMe/third_party/ycmd/third_party/eclipse.jdt.ls/workspace/config_linux/org.eclipse.osgi/51/0/.cp/gradle/init/init.gradle
|
||||||
|
auto.sync=false
|
||||||
|
build.scans.enabled=false
|
||||||
|
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||||
|
connection.project.dir=app
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
gradle.user.home=
|
||||||
|
java.home=/usr/lib/jvm/java-17-openjdk
|
||||||
|
jvm.arguments=
|
||||||
|
offline.mode=false
|
||||||
|
override.workspace.settings=true
|
||||||
|
show.console.view=true
|
||||||
|
show.executions.view=true
|
BIN
bac2/modalDev/tp1/TP1.pdf
Normal file
BIN
bac2/modalDev/tp1/TP1.pdf
Normal file
Binary file not shown.
12
bac2/modalDev/tp1/answer.md
Normal file
12
bac2/modalDev/tp1/answer.md
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
# TP's Answer
|
||||||
|
|
||||||
|
## Exercice 2.1
|
||||||
|
|
||||||
|
1)
|
||||||
|
test1) ❎ devrais fail car $ E1 \neq E2 $
|
||||||
|
test2) ✅
|
||||||
|
test3) ❎ shoud throw an error (no real number)
|
||||||
|
|
||||||
|
2-4) dans fichiers
|
||||||
|
|
||||||
|
## Exercice 2.2
|
32
bac2/modalDev/tp1/app/.classpath
Normal file
32
bac2/modalDev/tp1/app/.classpath
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<classpath>
|
||||||
|
<classpathentry kind="src" output="bin/main" path="src/main/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="main"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="bin/main" path="src/main/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="main"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="main,test"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="bin/test" path="src/test/java">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="test"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="test"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="src" output="bin/test" path="src/test/resources">
|
||||||
|
<attributes>
|
||||||
|
<attribute name="gradle_scope" value="test"/>
|
||||||
|
<attribute name="gradle_used_by_scope" value="test"/>
|
||||||
|
<attribute name="test" value="true"/>
|
||||||
|
</attributes>
|
||||||
|
</classpathentry>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-17/"/>
|
||||||
|
<classpathentry kind="con" path="org.eclipse.buildship.core.gradleclasspathcontainer"/>
|
||||||
|
<classpathentry kind="output" path="bin/default"/>
|
||||||
|
</classpath>
|
34
bac2/modalDev/tp1/app/.project
Normal file
34
bac2/modalDev/tp1/app/.project
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<projectDescription>
|
||||||
|
<name>app</name>
|
||||||
|
<comment>Project app created by Buildship.</comment>
|
||||||
|
<projects>
|
||||||
|
</projects>
|
||||||
|
<buildSpec>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
<buildCommand>
|
||||||
|
<name>org.eclipse.buildship.core.gradleprojectbuilder</name>
|
||||||
|
<arguments>
|
||||||
|
</arguments>
|
||||||
|
</buildCommand>
|
||||||
|
</buildSpec>
|
||||||
|
<natures>
|
||||||
|
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||||
|
<nature>org.eclipse.buildship.core.gradleprojectnature</nature>
|
||||||
|
</natures>
|
||||||
|
<filteredResources>
|
||||||
|
<filter>
|
||||||
|
<id>1701330804524</id>
|
||||||
|
<name></name>
|
||||||
|
<type>30</type>
|
||||||
|
<matcher>
|
||||||
|
<id>org.eclipse.core.resources.regexFilterMatcher</id>
|
||||||
|
<arguments>node_modules|\.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__</arguments>
|
||||||
|
</matcher>
|
||||||
|
</filter>
|
||||||
|
</filteredResources>
|
||||||
|
</projectDescription>
|
@ -0,0 +1,13 @@
|
|||||||
|
arguments=
|
||||||
|
auto.sync=false
|
||||||
|
build.scans.enabled=false
|
||||||
|
connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER)
|
||||||
|
connection.project.dir=
|
||||||
|
eclipse.preferences.version=1
|
||||||
|
gradle.user.home=
|
||||||
|
java.home=
|
||||||
|
jvm.arguments=
|
||||||
|
offline.mode=false
|
||||||
|
override.workspace.settings=false
|
||||||
|
show.console.view=false
|
||||||
|
show.executions.view=false
|
@ -0,0 +1,32 @@
|
|||||||
|
package tp1.ex2_1;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||||
|
*/
|
||||||
|
public class SolveEquationTest {
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||||
|
assertEquals(se.getNbSolutions(), 2);
|
||||||
|
assertTrue(se.getSolutions().getE1()
|
||||||
|
.equals(se.getSolutions().getE2()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
double epsilon = 0.0001;
|
||||||
|
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||||
|
assertEquals(se.getSolutions().getE1(),
|
||||||
|
se.getSolutions().getE2(), epsilon);
|
||||||
|
assertEquals(se.getNbSolutions(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3() {
|
||||||
|
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||||
|
assertEquals(se.getSolutions().getE1(), se.getSolutions().getE2());
|
||||||
|
}
|
||||||
|
}
|
45
bac2/modalDev/tp1/app/build.gradle.kts
Normal file
45
bac2/modalDev/tp1/app/build.gradle.kts
Normal file
@ -0,0 +1,45 @@
|
|||||||
|
/*
|
||||||
|
* This file was generated by the Gradle 'init' task.
|
||||||
|
*
|
||||||
|
* This generated file contains a sample Java application project to get you started.
|
||||||
|
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.4/userguide/building_java_projects.html in the Gradle documentation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
// Apply the application plugin to add support for building a CLI application in Java.
|
||||||
|
application
|
||||||
|
}
|
||||||
|
|
||||||
|
repositories {
|
||||||
|
// Use Maven Central for resolving dependencies.
|
||||||
|
mavenCentral()
|
||||||
|
}
|
||||||
|
|
||||||
|
dependencies {
|
||||||
|
// Use JUnit Jupiter for testing.
|
||||||
|
testImplementation("org.junit.jupiter:junit-jupiter:5.9.3")
|
||||||
|
|
||||||
|
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
|
||||||
|
|
||||||
|
// This dependency is used by the application.
|
||||||
|
implementation("com.google.guava:guava:32.1.1-jre")
|
||||||
|
|
||||||
|
implementation("org.mockito:mockito-core:5.7.0")
|
||||||
|
}
|
||||||
|
|
||||||
|
// Apply a specific Java toolchain to ease working on different environments.
|
||||||
|
java {
|
||||||
|
toolchain {
|
||||||
|
languageVersion.set(JavaLanguageVersion.of(17))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
application {
|
||||||
|
// Define the main class for the application.
|
||||||
|
mainClass.set("tp1.App")
|
||||||
|
}
|
||||||
|
|
||||||
|
tasks.named<Test>("test") {
|
||||||
|
// Use JUnit Platform for unit tests.
|
||||||
|
useJUnitPlatform()
|
||||||
|
}
|
15
bac2/modalDev/tp1/app/src/main/java/tp1/App.java
Normal file
15
bac2/modalDev/tp1/app/src/main/java/tp1/App.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
/*
|
||||||
|
* This Java source file was generated by the Gradle 'init' task.
|
||||||
|
*/
|
||||||
|
package tp1;
|
||||||
|
|
||||||
|
public class App {
|
||||||
|
// for the sake of showing an example of testing
|
||||||
|
public String getGreeting() {
|
||||||
|
return "Hello World!";
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println(new App().getGreeting());
|
||||||
|
}
|
||||||
|
}
|
27
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_1/Pair.java
Normal file
27
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_1/Pair.java
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
package tp1.ex2_1;
|
||||||
|
|
||||||
|
public class Pair<T> {
|
||||||
|
private T e1, e2;
|
||||||
|
|
||||||
|
public Pair(T e1, T e2) {
|
||||||
|
this.e1 = e1;
|
||||||
|
this.e2 = e2;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getE1() {
|
||||||
|
return e1;
|
||||||
|
}
|
||||||
|
|
||||||
|
public T getE2() {
|
||||||
|
return e2;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object obj) {
|
||||||
|
if (obj instanceof Pair) {
|
||||||
|
Pair<?> p = (Pair<?>) obj;
|
||||||
|
return e1.equals(p.e1) && e2.equals(p.e2);
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
package tp1.ex2_1;
|
||||||
|
|
||||||
|
public class SolveEquation {
|
||||||
|
private double a, b, c;
|
||||||
|
|
||||||
|
public SolveEquation(double a, double b, double c) {
|
||||||
|
this.a = a;
|
||||||
|
this.b = b;
|
||||||
|
this.c = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Gives the number of solutions of the equation .
|
||||||
|
*
|
||||||
|
* @return the number of solution .
|
||||||
|
*
|
||||||
|
* @throws ArithmeticException if there
|
||||||
|
* is an infinite number of solutions .
|
||||||
|
*/
|
||||||
|
public int getNbSolutions() throws ArithmeticException {
|
||||||
|
double delta = b * b - 4 * a * c;
|
||||||
|
if (delta < 0)
|
||||||
|
throw new ArithmeticException("No real solution");
|
||||||
|
else if (delta == 0)
|
||||||
|
return 1;
|
||||||
|
else
|
||||||
|
return 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* * Gives the equation ’s solutions for x .
|
||||||
|
*
|
||||||
|
* @return the 2 solutions of the equation . If there is
|
||||||
|
* only 1 solution , the 2 members of the pair are equal .
|
||||||
|
*
|
||||||
|
* @throws ArithmeticException if there is no real
|
||||||
|
* solution for x , or if there is an infinite number
|
||||||
|
* of solutions .
|
||||||
|
*/
|
||||||
|
public Pair<Double> getSolutions() throws ArithmeticException {
|
||||||
|
double delta = b * b - 4 * a * c;
|
||||||
|
if (delta < 0)
|
||||||
|
throw new ArithmeticException("No real solution");
|
||||||
|
else if (delta == 0) {
|
||||||
|
double x = -b / (2 * a);
|
||||||
|
return new Pair<Double>(x, x);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
return new Pair<Double>(
|
||||||
|
(-b - Math.sqrt(delta)) / (2 * a),
|
||||||
|
(-b + Math.sqrt(delta)) / (2 * a));
|
||||||
|
}
|
||||||
|
}
|
40
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_2/Sqrt.java
Normal file
40
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_2/Sqrt.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package tp1.ex2_2;
|
||||||
|
|
||||||
|
import java.lang.Math;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calcule la racine carrée d'un nombre positif
|
||||||
|
*/
|
||||||
|
public class Sqrt
|
||||||
|
{
|
||||||
|
public static final double EPSILON = 1e-8;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cacule la racine carrée d'un nombre positif en utilisant la méhtode
|
||||||
|
* de Newton.
|
||||||
|
* Il s'agit de calculer la racine de l'équation x^2-b=0 avec b
|
||||||
|
* le nombre dont on veut la racine carrée.
|
||||||
|
* La méthode de Newton consiste à utiliser la formule suivante :
|
||||||
|
* x_i = x_(i-1) - f(x_(i-1)) / f'(x_(i-1)).
|
||||||
|
* @param val La valeur dont on cherche la racine carrée (val >= 0)
|
||||||
|
* @return La racine carrée de val avec une précision de 1e-12.
|
||||||
|
* @throws IllegalArgumentException Si val < 0
|
||||||
|
*/
|
||||||
|
public static double sqrt(double val) throws IllegalArgumentException
|
||||||
|
{
|
||||||
|
if(val < 0)
|
||||||
|
throw new IllegalArgumentException("ne peut pas être négatif");
|
||||||
|
if(val == 0 || val == 1)
|
||||||
|
return val;
|
||||||
|
|
||||||
|
double curr = nextIteration(val, val);
|
||||||
|
while(Math.abs(val - Math.pow(curr, 2)) > EPSILON){
|
||||||
|
curr = nextIteration(val, curr);
|
||||||
|
}
|
||||||
|
return curr;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double nextIteration(double val, double curr){
|
||||||
|
return (curr + (val / curr))/2;
|
||||||
|
}
|
||||||
|
}
|
19
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_3/Fibonacci.java
Normal file
19
bac2/modalDev/tp1/app/src/main/java/tp1/ex2_3/Fibonacci.java
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
package tp1.ex2_3;
|
||||||
|
|
||||||
|
public class Fibonacci {
|
||||||
|
public static int fibo(int n) throws IllegalArgumentException {
|
||||||
|
if (n < 0) {
|
||||||
|
throw new IllegalArgumentException();
|
||||||
|
} else {
|
||||||
|
int f_n_1 = 1;
|
||||||
|
int f_n = 0;
|
||||||
|
int tmp;
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
tmp = f_n;
|
||||||
|
f_n = f_n + f_n_1;
|
||||||
|
f_n_1 = tmp;
|
||||||
|
}
|
||||||
|
return f_n;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
14
bac2/modalDev/tp1/app/src/test/java/tp1/AppTest.java
Normal file
14
bac2/modalDev/tp1/app/src/test/java/tp1/AppTest.java
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* This Java source file was generated by the Gradle 'init' task.
|
||||||
|
*/
|
||||||
|
package tp1;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
class AppTest {
|
||||||
|
@Test void appHasAGreeting() {
|
||||||
|
App classUnderTest = new App();
|
||||||
|
assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
package tp1.ex2_1;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||||
|
*/
|
||||||
|
public class SolveEquationTest {
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||||
|
assertEquals(se.getNbSolutions(), 2);
|
||||||
|
assertFalse(se.getSolutions().getE1()
|
||||||
|
.equals(se.getSolutions().getE2()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
double epsilon = 0.0001;
|
||||||
|
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||||
|
assertEquals(se.getSolutions().getE1(),
|
||||||
|
se.getSolutions().getE2(), epsilon);
|
||||||
|
assertEquals(se.getNbSolutions(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3() {
|
||||||
|
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||||
|
assertThrows(ArithmeticException.class, () -> se.getSolutions(), "No real solution");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test4(){
|
||||||
|
SolveEquation se = new SolveEquation(3, 0, 0);
|
||||||
|
assertEquals(se.getNbSolutions(), 1);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
package tp1.ex2_1;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||||
|
*/
|
||||||
|
public class SolveEquationTest {
|
||||||
|
@Test
|
||||||
|
public void test1() {
|
||||||
|
SolveEquation se = new SolveEquation(1.0, -2.0, -3.0);
|
||||||
|
assertEquals(se.getNbSolutions(), 2);
|
||||||
|
assertTrue(se.getSolutions().getE1()
|
||||||
|
.equals(se.getSolutions().getE2()));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test2() {
|
||||||
|
double epsilon = 0.0001;
|
||||||
|
SolveEquation se = new SolveEquation(1.0, 0.0, 0.0);
|
||||||
|
assertEquals(se.getSolutions().getE1(),
|
||||||
|
se.getSolutions().getE2(), epsilon);
|
||||||
|
assertEquals(se.getNbSolutions(), 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void test3() {
|
||||||
|
SolveEquation se = new SolveEquation(5.0, 0.0, 3.0);
|
||||||
|
assertEquals(se.getSolutions().getE1(), se.getSolutions().getE2());
|
||||||
|
}
|
||||||
|
}
|
49
bac2/modalDev/tp1/app/src/test/java/tp1/ex2_2/SqrtTest.java
Normal file
49
bac2/modalDev/tp1/app/src/test/java/tp1/ex2_2/SqrtTest.java
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
package tp1.ex2_2;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.*;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Suite de tests pour vérifier la méthode sqrt dans la classe Sqrt
|
||||||
|
*/
|
||||||
|
public class SqrtTest {
|
||||||
|
@Test
|
||||||
|
public void testSortie(){
|
||||||
|
assertTrue(Sqrt.sqrt(42) >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testprecision(){
|
||||||
|
assertEquals(Math.sqrt(16), Sqrt.sqrt(16), Sqrt.EPSILON);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testValidInput(){
|
||||||
|
double t1 = Sqrt.sqrt(0.4);
|
||||||
|
assertTrue(t1 > 0 && t1 < 1);
|
||||||
|
|
||||||
|
double t2 = Sqrt.sqrt(21);
|
||||||
|
assertTrue(t2 > 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBorne(){
|
||||||
|
assertEquals(0, Sqrt.sqrt(0));
|
||||||
|
assertEquals(1, Sqrt.sqrt(1));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBig(){
|
||||||
|
double val = assertTimeoutPreemptively(Duration.ofSeconds(1), () -> Sqrt.sqrt(1e16));
|
||||||
|
assertEquals(Math.sqrt(1e16), val, Sqrt.EPSILON);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInvalid(){
|
||||||
|
assertThrows(IllegalArgumentException.class, () -> Sqrt.sqrt(-42));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package tp1.ex2_3;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
|
||||||
|
public class FibonacciTest {
|
||||||
|
@Test
|
||||||
|
void test1() {
|
||||||
|
assertEquals(2, Fibonacci.fibo(2));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void test2() {
|
||||||
|
assertTrue(Fibonacci.fibo(-2) >= 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test void test3() {
|
||||||
|
assertTrue(Fibonacci.fibo(4) == 3);
|
||||||
|
}
|
||||||
|
}
|
BIN
bac2/modalDev/tp1/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
BIN
bac2/modalDev/tp1/gradle/wrapper/gradle-wrapper.jar
vendored
Normal file
Binary file not shown.
7
bac2/modalDev/tp1/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
7
bac2/modalDev/tp1/gradle/wrapper/gradle-wrapper.properties
vendored
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
distributionBase=GRADLE_USER_HOME
|
||||||
|
distributionPath=wrapper/dists
|
||||||
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.4-bin.zip
|
||||||
|
networkTimeout=10000
|
||||||
|
validateDistributionUrl=true
|
||||||
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
|
zipStorePath=wrapper/dists
|
249
bac2/modalDev/tp1/gradlew
vendored
Executable file
249
bac2/modalDev/tp1/gradlew
vendored
Executable file
@ -0,0 +1,249 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
|
||||||
|
#
|
||||||
|
# Copyright © 2015-2021 the original authors.
|
||||||
|
#
|
||||||
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
# you may not use this file except in compliance with the License.
|
||||||
|
# You may obtain a copy of the License at
|
||||||
|
#
|
||||||
|
# https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
#
|
||||||
|
# Unless required by applicable law or agreed to in writing, software
|
||||||
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
# See the License for the specific language governing permissions and
|
||||||
|
# limitations under the License.
|
||||||
|
#
|
||||||
|
|
||||||
|
##############################################################################
|
||||||
|
#
|
||||||
|
# Gradle start up script for POSIX generated by Gradle.
|
||||||
|
#
|
||||||
|
# Important for running:
|
||||||
|
#
|
||||||
|
# (1) You need a POSIX-compliant shell to run this script. If your /bin/sh is
|
||||||
|
# noncompliant, but you have some other compliant shell such as ksh or
|
||||||
|
# bash, then to run this script, type that shell name before the whole
|
||||||
|
# command line, like:
|
||||||
|
#
|
||||||
|
# ksh Gradle
|
||||||
|
#
|
||||||
|
# Busybox and similar reduced shells will NOT work, because this script
|
||||||
|
# requires all of these POSIX shell features:
|
||||||
|
# * functions;
|
||||||
|
# * expansions «$var», «${var}», «${var:-default}», «${var+SET}»,
|
||||||
|
# «${var#prefix}», «${var%suffix}», and «$( cmd )»;
|
||||||
|
# * compound commands having a testable exit status, especially «case»;
|
||||||
|
# * various built-in commands including «command», «set», and «ulimit».
|
||||||
|
#
|
||||||
|
# Important for patching:
|
||||||
|
#
|
||||||
|
# (2) This script targets any POSIX shell, so it avoids extensions provided
|
||||||
|
# by Bash, Ksh, etc; in particular arrays are avoided.
|
||||||
|
#
|
||||||
|
# The "traditional" practice of packing multiple parameters into a
|
||||||
|
# space-separated string is a well documented source of bugs and security
|
||||||
|
# problems, so this is (mostly) avoided, by progressively accumulating
|
||||||
|
# options in "$@", and eventually passing that to Java.
|
||||||
|
#
|
||||||
|
# Where the inherited environment variables (DEFAULT_JVM_OPTS, JAVA_OPTS,
|
||||||
|
# and GRADLE_OPTS) rely on word-splitting, this is performed explicitly;
|
||||||
|
# see the in-line comments for details.
|
||||||
|
#
|
||||||
|
# There are tweaks for specific operating systems such as AIX, CygWin,
|
||||||
|
# Darwin, MinGW, and NonStop.
|
||||||
|
#
|
||||||
|
# (3) This script is generated from the Groovy template
|
||||||
|
# https://github.com/gradle/gradle/blob/HEAD/subprojects/plugins/src/main/resources/org/gradle/api/internal/plugins/unixStartScript.txt
|
||||||
|
# within the Gradle project.
|
||||||
|
#
|
||||||
|
# You can find Gradle at https://github.com/gradle/gradle/.
|
||||||
|
#
|
||||||
|
##############################################################################
|
||||||
|
|
||||||
|
# Attempt to set APP_HOME
|
||||||
|
|
||||||
|
# Resolve links: $0 may be a link
|
||||||
|
app_path=$0
|
||||||
|
|
||||||
|
# Need this for daisy-chained symlinks.
|
||||||
|
while
|
||||||
|
APP_HOME=${app_path%"${app_path##*/}"} # leaves a trailing /; empty if no leading path
|
||||||
|
[ -h "$app_path" ]
|
||||||
|
do
|
||||||
|
ls=$( ls -ld "$app_path" )
|
||||||
|
link=${ls#*' -> '}
|
||||||
|
case $link in #(
|
||||||
|
/*) app_path=$link ;; #(
|
||||||
|
*) app_path=$APP_HOME$link ;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
# This is normally unused
|
||||||
|
# shellcheck disable=SC2034
|
||||||
|
APP_BASE_NAME=${0##*/}
|
||||||
|
# Discard cd standard output in case $CDPATH is set (https://github.com/gradle/gradle/issues/25036)
|
||||||
|
APP_HOME=$( cd "${APP_HOME:-./}" > /dev/null && pwd -P ) || exit
|
||||||
|
|
||||||
|
# Use the maximum available, or set MAX_FD != -1 to use that value.
|
||||||
|
MAX_FD=maximum
|
||||||
|
|
||||||
|
warn () {
|
||||||
|
echo "$*"
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
die () {
|
||||||
|
echo
|
||||||
|
echo "$*"
|
||||||
|
echo
|
||||||
|
exit 1
|
||||||
|
} >&2
|
||||||
|
|
||||||
|
# OS specific support (must be 'true' or 'false').
|
||||||
|
cygwin=false
|
||||||
|
msys=false
|
||||||
|
darwin=false
|
||||||
|
nonstop=false
|
||||||
|
case "$( uname )" in #(
|
||||||
|
CYGWIN* ) cygwin=true ;; #(
|
||||||
|
Darwin* ) darwin=true ;; #(
|
||||||
|
MSYS* | MINGW* ) msys=true ;; #(
|
||||||
|
NONSTOP* ) nonstop=true ;;
|
||||||
|
esac
|
||||||
|
|
||||||
|
CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
# Determine the Java command to use to start the JVM.
|
||||||
|
if [ -n "$JAVA_HOME" ] ; then
|
||||||
|
if [ -x "$JAVA_HOME/jre/sh/java" ] ; then
|
||||||
|
# IBM's JDK on AIX uses strange locations for the executables
|
||||||
|
JAVACMD=$JAVA_HOME/jre/sh/java
|
||||||
|
else
|
||||||
|
JAVACMD=$JAVA_HOME/bin/java
|
||||||
|
fi
|
||||||
|
if [ ! -x "$JAVACMD" ] ; then
|
||||||
|
die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
JAVACMD=java
|
||||||
|
if ! command -v java >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
|
||||||
|
Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
location of your Java installation."
|
||||||
|
fi
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Increase the maximum file descriptors if we can.
|
||||||
|
if ! "$cygwin" && ! "$darwin" && ! "$nonstop" ; then
|
||||||
|
case $MAX_FD in #(
|
||||||
|
max*)
|
||||||
|
# In POSIX sh, ulimit -H is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
MAX_FD=$( ulimit -H -n ) ||
|
||||||
|
warn "Could not query maximum file descriptor limit"
|
||||||
|
esac
|
||||||
|
case $MAX_FD in #(
|
||||||
|
'' | soft) :;; #(
|
||||||
|
*)
|
||||||
|
# In POSIX sh, ulimit -n is undefined. That's why the result is checked to see if it worked.
|
||||||
|
# shellcheck disable=SC2039,SC3045
|
||||||
|
ulimit -n "$MAX_FD" ||
|
||||||
|
warn "Could not set maximum file descriptor limit to $MAX_FD"
|
||||||
|
esac
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Collect all arguments for the java command, stacking in reverse order:
|
||||||
|
# * args from the command line
|
||||||
|
# * the main class name
|
||||||
|
# * -classpath
|
||||||
|
# * -D...appname settings
|
||||||
|
# * --module-path (only if needed)
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, and GRADLE_OPTS environment variables.
|
||||||
|
|
||||||
|
# For Cygwin or MSYS, switch paths to Windows format before running java
|
||||||
|
if "$cygwin" || "$msys" ; then
|
||||||
|
APP_HOME=$( cygpath --path --mixed "$APP_HOME" )
|
||||||
|
CLASSPATH=$( cygpath --path --mixed "$CLASSPATH" )
|
||||||
|
|
||||||
|
JAVACMD=$( cygpath --unix "$JAVACMD" )
|
||||||
|
|
||||||
|
# Now convert the arguments - kludge to limit ourselves to /bin/sh
|
||||||
|
for arg do
|
||||||
|
if
|
||||||
|
case $arg in #(
|
||||||
|
-*) false ;; # don't mess with options #(
|
||||||
|
/?*) t=${arg#/} t=/${t%%/*} # looks like a POSIX filepath
|
||||||
|
[ -e "$t" ] ;; #(
|
||||||
|
*) false ;;
|
||||||
|
esac
|
||||||
|
then
|
||||||
|
arg=$( cygpath --path --ignore --mixed "$arg" )
|
||||||
|
fi
|
||||||
|
# Roll the args list around exactly as many times as the number of
|
||||||
|
# args, so each arg winds up back in the position where it started, but
|
||||||
|
# possibly modified.
|
||||||
|
#
|
||||||
|
# NB: a `for` loop captures its iteration list before it begins, so
|
||||||
|
# changing the positional parameters here affects neither the number of
|
||||||
|
# iterations, nor the values presented in `arg`.
|
||||||
|
shift # remove old arg
|
||||||
|
set -- "$@" "$arg" # push replacement arg
|
||||||
|
done
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
DEFAULT_JVM_OPTS='"-Xmx64m" "-Xms64m"'
|
||||||
|
|
||||||
|
# Collect all arguments for the java command:
|
||||||
|
# * DEFAULT_JVM_OPTS, JAVA_OPTS, JAVA_OPTS, and optsEnvironmentVar are not allowed to contain shell fragments,
|
||||||
|
# and any embedded shellness will be escaped.
|
||||||
|
# * For example: A user cannot expect ${Hostname} to be expanded, as it is an environment variable and will be
|
||||||
|
# treated as '${Hostname}' itself on the command line.
|
||||||
|
|
||||||
|
set -- \
|
||||||
|
"-Dorg.gradle.appname=$APP_BASE_NAME" \
|
||||||
|
-classpath "$CLASSPATH" \
|
||||||
|
org.gradle.wrapper.GradleWrapperMain \
|
||||||
|
"$@"
|
||||||
|
|
||||||
|
# Stop when "xargs" is not available.
|
||||||
|
if ! command -v xargs >/dev/null 2>&1
|
||||||
|
then
|
||||||
|
die "xargs is not available"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Use "xargs" to parse quoted args.
|
||||||
|
#
|
||||||
|
# With -n1 it outputs one arg per line, with the quotes and backslashes removed.
|
||||||
|
#
|
||||||
|
# In Bash we could simply go:
|
||||||
|
#
|
||||||
|
# readarray ARGS < <( xargs -n1 <<<"$var" ) &&
|
||||||
|
# set -- "${ARGS[@]}" "$@"
|
||||||
|
#
|
||||||
|
# but POSIX shell has neither arrays nor command substitution, so instead we
|
||||||
|
# post-process each arg (as a line of input to sed) to backslash-escape any
|
||||||
|
# character that might be a shell metacharacter, then use eval to reverse
|
||||||
|
# that process (while maintaining the separation between arguments), and wrap
|
||||||
|
# the whole thing up as a single "set" statement.
|
||||||
|
#
|
||||||
|
# This will of course break if any of these variables contains a newline or
|
||||||
|
# an unmatched quote.
|
||||||
|
#
|
||||||
|
|
||||||
|
eval "set -- $(
|
||||||
|
printf '%s\n' "$DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS" |
|
||||||
|
xargs -n1 |
|
||||||
|
sed ' s~[^-[:alnum:]+,./:=@_]~\\&~g; ' |
|
||||||
|
tr '\n' ' '
|
||||||
|
)" '"$@"'
|
||||||
|
|
||||||
|
exec "$JAVACMD" "$@"
|
92
bac2/modalDev/tp1/gradlew.bat
vendored
Normal file
92
bac2/modalDev/tp1/gradlew.bat
vendored
Normal file
@ -0,0 +1,92 @@
|
|||||||
|
@rem
|
||||||
|
@rem Copyright 2015 the original author or authors.
|
||||||
|
@rem
|
||||||
|
@rem Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
@rem you may not use this file except in compliance with the License.
|
||||||
|
@rem You may obtain a copy of the License at
|
||||||
|
@rem
|
||||||
|
@rem https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
@rem
|
||||||
|
@rem Unless required by applicable law or agreed to in writing, software
|
||||||
|
@rem distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
@rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
@rem See the License for the specific language governing permissions and
|
||||||
|
@rem limitations under the License.
|
||||||
|
@rem
|
||||||
|
|
||||||
|
@if "%DEBUG%"=="" @echo off
|
||||||
|
@rem ##########################################################################
|
||||||
|
@rem
|
||||||
|
@rem Gradle startup script for Windows
|
||||||
|
@rem
|
||||||
|
@rem ##########################################################################
|
||||||
|
|
||||||
|
@rem Set local scope for the variables with windows NT shell
|
||||||
|
if "%OS%"=="Windows_NT" setlocal
|
||||||
|
|
||||||
|
set DIRNAME=%~dp0
|
||||||
|
if "%DIRNAME%"=="" set DIRNAME=.
|
||||||
|
@rem This is normally unused
|
||||||
|
set APP_BASE_NAME=%~n0
|
||||||
|
set APP_HOME=%DIRNAME%
|
||||||
|
|
||||||
|
@rem Resolve any "." and ".." in APP_HOME to make it shorter.
|
||||||
|
for %%i in ("%APP_HOME%") do set APP_HOME=%%~fi
|
||||||
|
|
||||||
|
@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
|
||||||
|
set DEFAULT_JVM_OPTS="-Xmx64m" "-Xms64m"
|
||||||
|
|
||||||
|
@rem Find java.exe
|
||||||
|
if defined JAVA_HOME goto findJavaFromJavaHome
|
||||||
|
|
||||||
|
set JAVA_EXE=java.exe
|
||||||
|
%JAVA_EXE% -version >NUL 2>&1
|
||||||
|
if %ERRORLEVEL% equ 0 goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH.
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:findJavaFromJavaHome
|
||||||
|
set JAVA_HOME=%JAVA_HOME:"=%
|
||||||
|
set JAVA_EXE=%JAVA_HOME%/bin/java.exe
|
||||||
|
|
||||||
|
if exist "%JAVA_EXE%" goto execute
|
||||||
|
|
||||||
|
echo.
|
||||||
|
echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME%
|
||||||
|
echo.
|
||||||
|
echo Please set the JAVA_HOME variable in your environment to match the
|
||||||
|
echo location of your Java installation.
|
||||||
|
|
||||||
|
goto fail
|
||||||
|
|
||||||
|
:execute
|
||||||
|
@rem Setup the command line
|
||||||
|
|
||||||
|
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar
|
||||||
|
|
||||||
|
|
||||||
|
@rem Execute Gradle
|
||||||
|
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %*
|
||||||
|
|
||||||
|
:end
|
||||||
|
@rem End local scope for the variables with windows NT shell
|
||||||
|
if %ERRORLEVEL% equ 0 goto mainEnd
|
||||||
|
|
||||||
|
:fail
|
||||||
|
rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of
|
||||||
|
rem the _cmd.exe /c_ return code!
|
||||||
|
set EXIT_CODE=%ERRORLEVEL%
|
||||||
|
if %EXIT_CODE% equ 0 set EXIT_CODE=1
|
||||||
|
if not ""=="%GRADLE_EXIT_CONSOLE%" exit %EXIT_CODE%
|
||||||
|
exit /b %EXIT_CODE%
|
||||||
|
|
||||||
|
:mainEnd
|
||||||
|
if "%OS%"=="Windows_NT" endlocal
|
||||||
|
|
||||||
|
:omega
|
14
bac2/modalDev/tp1/settings.gradle.kts
Normal file
14
bac2/modalDev/tp1/settings.gradle.kts
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
/*
|
||||||
|
* This file was generated by the Gradle 'init' task.
|
||||||
|
*
|
||||||
|
* The settings file is used to specify which projects to include in your build.
|
||||||
|
* For more detailed information on multi-project builds, please refer to https://docs.gradle.org/8.4/userguide/building_swift_projects.html in the Gradle documentation.
|
||||||
|
*/
|
||||||
|
|
||||||
|
plugins {
|
||||||
|
// Apply the foojay-resolver plugin to allow automatic download of JDKs
|
||||||
|
id("org.gradle.toolchains.foojay-resolver-convention") version "0.7.0"
|
||||||
|
}
|
||||||
|
|
||||||
|
rootProject.name = "tp1"
|
||||||
|
include("app")
|
@ -2,7 +2,9 @@
|
|||||||
CC = gcc
|
CC = gcc
|
||||||
CFLAGS = -g -Wall
|
CFLAGS = -g -Wall
|
||||||
|
|
||||||
all: ex1 ex2 ex3 ex4 ex5 ex6 ex7 group
|
SRC = ex1 ex2 ex3 ex4 ex5 ex6 ex7 group
|
||||||
|
|
||||||
|
all: $(SRC)
|
||||||
|
|
||||||
group: group.o mergeSort.o
|
group: group.o mergeSort.o
|
||||||
$(CC) $(CFLAGS) -o $@ $+
|
$(CC) $(CFLAGS) -o $@ $+
|
||||||
@ -15,9 +17,8 @@ group: group.o mergeSort.o
|
|||||||
|
|
||||||
clean:
|
clean:
|
||||||
rm -f *.o core.*
|
rm -f *.o core.*
|
||||||
|
rm -f plot.png
|
||||||
mrproper: clean
|
rm -f $(SRC)
|
||||||
rm -f ex1
|
|
||||||
|
|
||||||
run: group
|
run: group
|
||||||
./$<
|
./$<
|
||||||
|
@ -6,18 +6,19 @@
|
|||||||
|
|
||||||
#define printlist(s, x) printf("%s = [", #x); for (int i = 0; i < s; ++i) { printf("%d, ", x[i]); } printf("\b\b]\n");
|
#define printlist(s, x) printf("%s = [", #x); for (int i = 0; i < s; ++i) { printf("%d, ", x[i]); } printf("\b\b]\n");
|
||||||
|
|
||||||
#define listSize 10000000
|
#define listSize 1000000
|
||||||
int list[listSize];
|
int list[listSize];
|
||||||
|
|
||||||
int main(void)
|
int main(void)
|
||||||
{
|
{
|
||||||
|
// liste de nombre aléatoires
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
for (int i = 0; i < listSize; ++i) {
|
for (int i = 0; i < listSize; ++i) {
|
||||||
list[i] = rand();
|
list[i] = rand()%listSize;
|
||||||
}
|
}
|
||||||
/* printlist(listSize, list); */
|
/* printlist(listSize, list); */
|
||||||
|
|
||||||
margs_t args = {list, 0, 9};
|
mrg_t args = merge_init(list, listSize);
|
||||||
merge_sort(&args);
|
merge_sort(&args);
|
||||||
printlist(listSize, list);
|
printlist(listSize, list);
|
||||||
}
|
}
|
||||||
|
18
bac2/os/chap3/group01-chap3/Makefile
Normal file
18
bac2/os/chap3/group01-chap3/Makefile
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
.PHONY: clean, all
|
||||||
|
CC = gcc
|
||||||
|
CFLAGS = -g -Wall
|
||||||
|
|
||||||
|
all: group
|
||||||
|
|
||||||
|
%.o: %.c
|
||||||
|
$(CC) $(CFLAGS) -c -o $@ $<
|
||||||
|
|
||||||
|
group: group.o mergeSort.o
|
||||||
|
$(CC) $(CFLAGS) -o $@ $+ $(LDFLAGS)
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o core.*
|
||||||
|
rm -f group
|
||||||
|
|
||||||
|
run: group
|
||||||
|
./$<
|
24
bac2/os/chap3/group01-chap3/group.c
Normal file
24
bac2/os/chap3/group01-chap3/group.c
Normal file
@ -0,0 +1,24 @@
|
|||||||
|
#include "mergeSort.h"
|
||||||
|
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <time.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define printlist(s, x) printf("%s = [", #x); for (int i = 0; i < s; ++i) { printf("%d, ", x[i]); } printf("\b\b]\n");
|
||||||
|
|
||||||
|
#define listSize 1000000
|
||||||
|
int list[listSize];
|
||||||
|
|
||||||
|
int main(void)
|
||||||
|
{
|
||||||
|
// liste de nombre aléatoires
|
||||||
|
srand(time(NULL));
|
||||||
|
for (int i = 0; i < listSize; ++i) {
|
||||||
|
list[i] = rand()%listSize;
|
||||||
|
}
|
||||||
|
/* printlist(listSize, list); */
|
||||||
|
|
||||||
|
mrg_t args = merge_init(list, listSize);
|
||||||
|
merge_sort(&args);
|
||||||
|
printlist(listSize, list);
|
||||||
|
}
|
58
bac2/os/chap3/group01-chap3/mergeSort.c
Normal file
58
bac2/os/chap3/group01-chap3/mergeSort.c
Normal file
@ -0,0 +1,58 @@
|
|||||||
|
#include "mergeSort.h"
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
#include <assert.h>
|
||||||
|
#include <pthread.h>
|
||||||
|
|
||||||
|
void* merge_sort(void* args) {
|
||||||
|
mrg_t* f = (mrg_t*) args;
|
||||||
|
if(f->slice.start < f->slice.final) {
|
||||||
|
int middle = (f->slice.start + f->slice.final) / 2;
|
||||||
|
|
||||||
|
mrg_t args1 = {f->array, f->_depth+1, {f->slice.start, middle}};
|
||||||
|
mrg_t args2 = {f->array, f->_depth+1, { middle+1, f->slice.final}};
|
||||||
|
|
||||||
|
if(f->_depth * f->_depth < THREADS){
|
||||||
|
pthread_t t1, t2;
|
||||||
|
assert(!pthread_create(&t1, NULL, merge_sort, (void*) &args1));
|
||||||
|
assert(!pthread_create(&t2, NULL, merge_sort, (void*) &args2));
|
||||||
|
assert(!pthread_join(t1, NULL));
|
||||||
|
assert(!pthread_join(t2, NULL));
|
||||||
|
}else{
|
||||||
|
merge_sort((void*) &args1);
|
||||||
|
merge_sort((void*) &args2);
|
||||||
|
}
|
||||||
|
merge(f->array, f->slice.start, middle, f->slice.final);
|
||||||
|
}
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final) {
|
||||||
|
|
||||||
|
uint8_t countL = middle - start + 1;
|
||||||
|
int *arrayL = malloc(countL * sizeof(int));
|
||||||
|
for(uint8_t currentL = 0; currentL < countL; currentL ++)
|
||||||
|
arrayL[currentL] = array[start + currentL ];
|
||||||
|
|
||||||
|
uint8_t countR = final - middle;
|
||||||
|
int* arrayR = malloc(countR * sizeof(int));
|
||||||
|
for(uint8_t currentR = 0; currentR < countR; currentR ++)
|
||||||
|
arrayR[currentR] = array[middle + 1 + currentR ];
|
||||||
|
|
||||||
|
uint8_t currentL, currentR, current;
|
||||||
|
for(currentL = 0, currentR = 0, current = start;
|
||||||
|
current <= final && currentL < countL && currentR < countR;
|
||||||
|
current ++)
|
||||||
|
if(arrayL[currentL] <= arrayR[currentR])
|
||||||
|
array[current] = arrayL[currentL++];
|
||||||
|
else
|
||||||
|
array[current] = arrayR[currentR++];
|
||||||
|
|
||||||
|
while(currentR < countR)
|
||||||
|
array[current++] = arrayR[currentR++];
|
||||||
|
while(currentL < countL)
|
||||||
|
array[current++] = arrayL[currentL++];
|
||||||
|
|
||||||
|
free(arrayL);
|
||||||
|
free(arrayR);
|
||||||
|
}
|
21
bac2/os/chap3/group01-chap3/mergeSort.h
Normal file
21
bac2/os/chap3/group01-chap3/mergeSort.h
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include <pthread.h>
|
||||||
|
#include <inttypes.h>
|
||||||
|
|
||||||
|
#define THREADS 4
|
||||||
|
#define merge_init(array, size) {(array), 0, {1, (size)}}
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
int start;
|
||||||
|
int final;
|
||||||
|
} SLICE;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
int* array;
|
||||||
|
int _depth;
|
||||||
|
SLICE slice;
|
||||||
|
} mrg_t;
|
||||||
|
|
||||||
|
void* merge_sort(void*);
|
||||||
|
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final);
|
@ -4,42 +4,42 @@
|
|||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
|
|
||||||
#define THREADS
|
|
||||||
|
|
||||||
pthread_t threads[THREADS];
|
|
||||||
|
|
||||||
void* merge_sort(void* args) {
|
void* merge_sort(void* args) {
|
||||||
margs_t* f = (margs_t*) args;
|
mrg_t* f = (mrg_t*) args;
|
||||||
if(f->start < f->final) {
|
if(f->slice.start < f->slice.final) {
|
||||||
|
int middle = (f->slice.start + f->slice.final) / 2;
|
||||||
|
|
||||||
|
mrg_t args1 = {f->array, f->_depth+1, {f->slice.start, middle}};
|
||||||
|
mrg_t args2 = {f->array, f->_depth+1, { middle+1, f->slice.final}};
|
||||||
|
|
||||||
|
if(f->_depth * f->_depth < THREADS){
|
||||||
pthread_t t1, t2;
|
pthread_t t1, t2;
|
||||||
index middle = (f->start + f->final) / 2;
|
|
||||||
|
|
||||||
margs_t args1 = {f->array, f->start, middle};
|
|
||||||
assert(!pthread_create(&t1, NULL, merge_sort, (void*) &args1));
|
assert(!pthread_create(&t1, NULL, merge_sort, (void*) &args1));
|
||||||
margs_t args2 = {f->array, middle+1, f->final};
|
|
||||||
assert(!pthread_create(&t2, NULL, merge_sort, (void*) &args2));
|
assert(!pthread_create(&t2, NULL, merge_sort, (void*) &args2));
|
||||||
|
assert(!pthread_join(t1, NULL));
|
||||||
assert(pthread_join(t1, NULL));
|
assert(!pthread_join(t2, NULL));
|
||||||
assert(pthread_join(t2, NULL));
|
}else{
|
||||||
merge(f->array, f->start, middle, f->final);
|
merge_sort((void*) &args1);
|
||||||
|
merge_sort((void*) &args2);
|
||||||
}
|
}
|
||||||
int *ret = 0;
|
merge(f->array, f->slice.start, middle, f->slice.final);
|
||||||
return ret;
|
}
|
||||||
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
void merge(int array[], index start, index middle, index final) {
|
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final) {
|
||||||
|
|
||||||
length countL = middle - start + 1;
|
uint8_t countL = middle - start + 1;
|
||||||
int *arrayL = malloc(countL * sizeof(int));
|
int *arrayL = malloc(countL * sizeof(int));
|
||||||
for(index currentL = 0; currentL < countL; currentL ++)
|
for(uint8_t currentL = 0; currentL < countL; currentL ++)
|
||||||
arrayL[currentL] = array[start + currentL ];
|
arrayL[currentL] = array[start + currentL ];
|
||||||
|
|
||||||
length countR = final - middle;
|
uint8_t countR = final - middle;
|
||||||
int* arrayR = malloc(countR * sizeof(int));
|
int* arrayR = malloc(countR * sizeof(int));
|
||||||
for(index currentR = 0; currentR < countR; currentR ++)
|
for(uint8_t currentR = 0; currentR < countR; currentR ++)
|
||||||
arrayR[currentR] = array[middle + 1 + currentR ];
|
arrayR[currentR] = array[middle + 1 + currentR ];
|
||||||
|
|
||||||
index currentL, currentR, current;
|
uint8_t currentL, currentR, current;
|
||||||
for(currentL = 0, currentR = 0, current = start;
|
for(currentL = 0, currentR = 0, current = start;
|
||||||
current <= final && currentL < countL && currentR < countR;
|
current <= final && currentL < countL && currentR < countR;
|
||||||
current ++)
|
current ++)
|
||||||
|
@ -1,15 +1,21 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
typedef unsigned int index;
|
#include <pthread.h>
|
||||||
typedef unsigned int length;
|
#include <inttypes.h>
|
||||||
|
|
||||||
typedef struct merge_args{
|
#define THREADS 4
|
||||||
|
#define merge_init(array, size) {(array), 0, {1, (size)}}
|
||||||
|
|
||||||
|
typedef struct{
|
||||||
|
int start;
|
||||||
|
int final;
|
||||||
|
} SLICE;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
int* array;
|
int* array;
|
||||||
index start;
|
int _depth;
|
||||||
index final;
|
SLICE slice;
|
||||||
} margs_t;
|
} mrg_t;
|
||||||
|
|
||||||
|
|
||||||
void merge(int array[], index start, index middle, index final);
|
|
||||||
/* void merge_sort(int array [], index start , index final ); */
|
|
||||||
void* merge_sort(void*);
|
void* merge_sort(void*);
|
||||||
|
void merge(int array[], uint8_t start, uint8_t middle, uint8_t final);
|
||||||
|
10000
bac2/os/chap3/plot
Normal file
10000
bac2/os/chap3/plot
Normal file
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user