diff --git a/README.md b/README.md deleted file mode 100644 index ef1621b58c1231378029dd64789bee9c01011181..0000000000000000000000000000000000000000 --- a/README.md +++ /dev/null @@ -1,93 +0,0 @@ -# CTP_devmob_2024 - - - -## Getting started - -To make it easy for you to get started with GitLab, here's a list of recommended next steps. - -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files - -- [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files -- [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: - -``` -cd existing_repo -git remote add origin https://gitlab.univ-lille.fr/enseignement/ctp_devmob_2024.git -git branch -M main -git push -uf origin main -``` - -## Integrate with your tools - -- [ ] [Set up project integrations](https://gitlab.univ-lille.fr/enseignement/ctp_devmob_2024/-/settings/integrations) - -## Collaborate with your team - -- [ ] [Invite team members and collaborators](https://docs.gitlab.com/ee/user/project/members/) -- [ ] [Create a new merge request](https://docs.gitlab.com/ee/user/project/merge_requests/creating_merge_requests.html) -- [ ] [Automatically close issues from merge requests](https://docs.gitlab.com/ee/user/project/issues/managing_issues.html#closing-issues-automatically) -- [ ] [Enable merge request approvals](https://docs.gitlab.com/ee/user/project/merge_requests/approvals/) -- [ ] [Set auto-merge](https://docs.gitlab.com/ee/user/project/merge_requests/merge_when_pipeline_succeeds.html) - -## Test and Deploy - -Use the built-in continuous integration in GitLab. - -- [ ] [Get started with GitLab CI/CD](https://docs.gitlab.com/ee/ci/quick_start/index.html) -- [ ] [Analyze your code for known vulnerabilities with Static Application Security Testing (SAST)](https://docs.gitlab.com/ee/user/application_security/sast/) -- [ ] [Deploy to Kubernetes, Amazon EC2, or Amazon ECS using Auto Deploy](https://docs.gitlab.com/ee/topics/autodevops/requirements.html) -- [ ] [Use pull-based deployments for improved Kubernetes management](https://docs.gitlab.com/ee/user/clusters/agent/) -- [ ] [Set up protected environments](https://docs.gitlab.com/ee/ci/environments/protected_environments.html) - -*** - -# Editing this README - -When you're ready to make this README your own, just edit this file and use the handy template below (or feel free to structure it however you want - this is just a starting point!). Thanks to [makeareadme.com](https://www.makeareadme.com/) for this template. - -## Suggestions for a good README - -Every project is different, so consider which of these sections apply to yours. The sections used in the template are suggestions for most open source projects. Also keep in mind that while a README can be too long and detailed, too long is better than too short. If you think your README is too long, consider utilizing another form of documentation rather than cutting out information. - -## Name -Choose a self-explaining name for your project. - -## Description -Let people know what your project can do specifically. Provide context and add a link to any reference visitors might be unfamiliar with. A list of Features or a Background subsection can also be added here. If there are alternatives to your project, this is a good place to list differentiating factors. - -## Badges -On some READMEs, you may see small images that convey metadata, such as whether or not all the tests are passing for the project. You can use Shields to add some to your README. Many services also have instructions for adding a badge. - -## Visuals -Depending on what you are making, it can be a good idea to include screenshots or even a video (you'll frequently see GIFs rather than actual videos). Tools like ttygif can help, but check out Asciinema for a more sophisticated method. - -## Installation -Within a particular ecosystem, there may be a common way of installing things, such as using Yarn, NuGet, or Homebrew. However, consider the possibility that whoever is reading your README is a novice and would like more guidance. Listing specific steps helps remove ambiguity and gets people to using your project as quickly as possible. If it only runs in a specific context like a particular programming language version or operating system or has dependencies that have to be installed manually, also add a Requirements subsection. - -## Usage -Use examples liberally, and show the expected output if you can. It's helpful to have inline the smallest example of usage that you can demonstrate, while providing links to more sophisticated examples if they are too long to reasonably include in the README. - -## Support -Tell people where they can go to for help. It can be any combination of an issue tracker, a chat room, an email address, etc. - -## Roadmap -If you have ideas for releases in the future, it is a good idea to list them in the README. - -## Contributing -State if you are open to contributions and what your requirements are for accepting them. - -For people who want to make changes to your project, it's helpful to have some documentation on how to get started. Perhaps there is a script that they should run or some environment variables that they need to set. Make these steps explicit. These instructions could also be useful to your future self. - -You can also document commands to lint the code or run tests. These steps help to ensure high code quality and reduce the likelihood that the changes inadvertently break something. Having instructions for running tests is especially helpful if it requires external setup, such as starting a Selenium server for testing in a browser. - -## Authors and acknowledgment -Show your appreciation to those who have contributed to the project. - -## License -For open source projects, say how it is licensed. - -## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000000000000000000000000000000000000..7dfe385de27ffa5764c3ba79ad726f3ec0c9fc05 --- /dev/null +++ b/pom.xml @@ -0,0 +1,95 @@ +<?xml version="1.0" encoding="UTF-8"?> +<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns="http://maven.apache.org/POM/4.0.0" + 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> + + <artifactId>CTP_Jan_2024</artifactId> + <groupId>org.example</groupId> + <version>1.0-SNAPSHOT</version> + <packaging>jar</packaging> + + <name>consoleApp</name> + + <properties> + <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> + <kotlin.code.style>official</kotlin.code.style> + <kotlin.compiler.jvmTarget>1.8</kotlin.compiler.jvmTarget> + </properties> + + <repositories> + <repository> + <id>mavenCentral</id> + <url>https://repo1.maven.org/maven2/</url> + </repository> + </repositories> + + <build> + <sourceDirectory>src/main/kotlin</sourceDirectory> + <testSourceDirectory>src/test/kotlin</testSourceDirectory> + <plugins> + <plugin> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-maven-plugin</artifactId> + <version>1.9.0</version> + <executions> + <execution> + <id>compile</id> + <phase>compile</phase> + <goals> + <goal>compile</goal> + </goals> + </execution> + <execution> + <id>test-compile</id> + <phase>test-compile</phase> + <goals> + <goal>test-compile</goal> + </goals> + </execution> + </executions> + </plugin> + <plugin> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.22.2</version> + </plugin> + <plugin> + <artifactId>maven-failsafe-plugin</artifactId> + <version>2.22.2</version> + </plugin> + </plugins> + </build> + + <dependencies> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-test-junit5</artifactId> + <version>1.9.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-engine</artifactId> + <version>5.10.0</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.jetbrains.kotlin</groupId> + <artifactId>kotlin-stdlib</artifactId> + <version>1.9.0</version> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter</artifactId> + <version>RELEASE</version> + <scope>test</scope> + </dependency> + <dependency> + <groupId>org.junit.jupiter</groupId> + <artifactId>junit-jupiter-api</artifactId> + <version>5.10.1</version> + <scope>test</scope> + </dependency> + </dependencies> + +</project> diff --git a/src/main/kotlin/Liste.kt b/src/main/kotlin/Liste.kt new file mode 100644 index 0000000000000000000000000000000000000000..c1fe741a8ac9e1e72334c6e7db4e43ee6b96f64b --- /dev/null +++ b/src/main/kotlin/Liste.kt @@ -0,0 +1,116 @@ +package listes + +/* Listes */ +sealed class Liste<out A>{ + companion object { + /* Factory */ + fun <A> of(vararg xs : A) : Liste<A> = + if (xs.isEmpty()) Nil + else Cons(xs[0], of(*xs.sliceArray(1 until xs.size))) + + /* Somme d'entiers */ + fun sum(l : Liste<Int>) : Int = when (l) { + is Nil -> 0 + is Cons -> l.head + sum(l.tail) + } + + /* produit de doubles */ + fun product(l : Liste<Double>) : Double = when (l) { + is Nil -> 1.0 + is Cons -> l.head * product(l.tail) + } + + /* Destructurations: head et tail */ + fun <A> head(l : Liste<A>) : A = when (l) { + is Nil -> throw IllegalStateException("head: Nil n'a pas de tête") + is Cons -> l.head + } + + fun <A> tail(l : Liste<A>) : Liste<A> = when (l) { + is Nil -> throw IllegalStateException("tail: Nil n'a pas de queue") + is Cons -> l.tail + } + + /* Remplace le premier élément d'une liste par une autre valeur */ + fun <A> setHead(l : Liste<A>, x : A) : Liste<A> = when (l) { + is Nil -> throw IllegalStateException("setHead: Nil n'a pas de tête") + is Cons -> Cons(x, tail(l)) + } + + /* Supprime les n premiers éléments d'une liste. */ + fun <A> drop(l : Liste<A>, n : Int) : Liste<A> = when { + n<0 -> throw IllegalStateException("drop: n doit être positif ou nul") + n == 0 -> l + else -> when (l) { + Nil -> throw IllegalStateException("drop: liste trop petite") + else -> drop(tail(l), n-1) + } + } + + /* Supprime les premiers éléments d'une liste tant qu'ils satisfont une prédicat */ + fun <A> dropWhile(l: Liste<A>, p: (A) -> Boolean) : Liste<A> = when (l) { + is Nil -> Nil + is Cons -> if (p(l.head)) dropWhile(l.tail,p) else l + } + + /* Longueur cumulée des chaînes de caractères contenues dans une liste */ + fun lengthOfStrings(l : Liste<String>) : Int = when (l) { + is Nil -> 0 + is Cons -> l.head.length + lengthOfStrings(l.tail) + } + + /* foldRight : généralisation de sum, product */ + fun <A,B> foldRight(l : Liste<A>, z : B, f : (A,B) -> B ) : B = when (l) { + is Nil -> z + is Cons -> f(l.head, foldRight(l.tail,z,f)) + } + + /* Réimplémentation de sum, product et longueur */ + fun sum2(l : Liste<Int>) : Int = foldRight(l, 0, {a , b -> a+b} ) + fun product2(l : Liste<Double>) : Double = foldRight(l, 1.0, {a , b -> a*b} ) + fun lengthOfStrings2(l : Liste<String>) : Int = foldRight(l, 0, {a, b -> a.length + b}) + + /* Longueur d'une liste avec foldRight */ + fun <A> length(l : Liste<A>) : Int = foldRight(l, 0, { a, b -> 1 + b}) + + /* sum terminale */ + fun sumTerm(l : Liste<Int>) : Int { + tailrec fun sumTermAux(l: Liste<Int>, acc: Int): Int = when (l) { + is Nil -> acc + is Cons -> sumTermAux(l.tail, l.head + acc) + } + return sumTermAux(l, 0) + } + + /* product terminale */ + fun productTerm(l : Liste<Double>) : Double { + tailrec fun productTermAux(l: Liste<Double>, acc: Double): Double = when (l) { + is Nil -> acc + is Cons -> productTermAux(l.tail, l.head + acc) + } + return productTermAux(l, 1.0) + } + + /* lengthOfStrings terminale */ + fun lengthOfStringsTerm(l : Liste<String>) : Int { + tailrec fun lengthOfStringsTermAux(l: Liste<String>, acc: Int): Int = when (l) { + is Nil -> acc + is Cons -> lengthOfStringsTermAux(l.tail, l.head.length + acc) + } + return lengthOfStringsTermAux(l, 1) + } + + /* foldLeft */ + tailrec fun <A,B> foldLeft(l : Liste<A>, z : B, f : (B,A) -> B) : B = when (l) { + is Nil -> z + is Cons -> foldLeft(l.tail, f(z,l.head), f) + } + + fun sumTerm2 (l : Liste<Int>) : Int = foldLeft(l, 0, { a, b -> a + b }) + fun productTerm2(l : Liste<Double>) : Double = foldLeft(l, 1.0, { a, b -> a * b }) + fun lengthOfStringsTerm2(l : Liste<String>) : Int = foldLeft(l, 1, { a, b -> a * b.length }) + } +} + +object Nil : Liste<Nothing>() +data class Cons<out A>(val head : A, val tail : Liste<A>) : Liste<A>() diff --git a/src/main/kotlin/Main.kt b/src/main/kotlin/Main.kt new file mode 100644 index 0000000000000000000000000000000000000000..e1edab1f4a2d51bc2f9be236b992787e18f012f1 --- /dev/null +++ b/src/main/kotlin/Main.kt @@ -0,0 +1,69 @@ +import listes.Cons +import listes.Liste +import listes.Nil +import option.Option + + +/***************** + * PARTIE 1 + *****************/ + +/* Q1 - findFirst: recherche du 1er indice de chaîne de caractère dans une liste */ +fun findFirst(l : Liste<String>, s : String) : Int = TODO() + +/* Q2 - findFirst: recherche du premier element d'une liste satisfaisant un prédicat donné */ +// TODO + +/* Q3 - append: concatène deux listes */ +// TODO + +/* Q4 - all: renvoie true tous les elements de l satisfont p */ +fun <A> all(l : Liste<A>, p : (A) -> Boolean): Boolean = TODO() + +/* Q5 - flatMap: concatène les listes d'une liste de listes */ +// TODO + +/* Q6 - isSorted: renvoie true ssi la liste d'entiers est triée */ +fun Liste<Int>.isSorted() : Boolean = TODO() + +/* Q7 - data class Personne */ +// TODO + +/* Q8 - isSortedGeneric: renvoie true ssi la liste d'éléments Comparable est triée */ +// TODO + +/***************** + * PARTIE 2 + *****************/ + +/* Q9 - map: applique une fonction f à des valeurs optionnelles */ +fun <A,B> Option<A>.map(f:(A)->B): Option<B> = TODO() + +/* Q10 - incOption: incrémente un entier optionnel */ +fun incOption(a : Option<Int>) : Option<Int> = TODO() + +// Q11 - findFirst dont le traitement des exceptions est réalisé avec Option +fun <A> findFirstOption(l: Liste<A>, p: (A) -> Boolean) : Option<Int> = TODO() + +// Q12 - findFirst récursif terminal et traitement des exceptions avec Option +fun <A> findFirstOption2(l: Liste<A>, p: (A) -> Boolean) : Option<Int> = TODO() + +data class Employe(val nom: String, val departement: String, val manager: Option<Employe>) + +/* Q13 - lookupByName : renvoie le l'employe ayant le nom donné en paramètre */ +fun lookupByName(employes : Liste<Employe>, nom : String) : Option<Employe> = TODO() + +/* Q14 - flatMap */ +fun <A,B> Option<A>.flatMap(f : (A) -> Option<B>): Option<B> = TODO() + +/* Q15 - Salarie, Chef et Collaborateur */ +// TODO + +/* Q16 - fibonacci */ +fun fibonacci(n : Int) : Long = TODO() + +/* Q17 - fibonacci récursive terminale */ +fun fibonacciTerminal(n : Int) : Long = TODO() + +fun main() { +} \ No newline at end of file diff --git a/src/main/kotlin/Option.kt b/src/main/kotlin/Option.kt new file mode 100644 index 0000000000000000000000000000000000000000..d9ac755b4a4f1b9210c06610cc928cc4c1c83a09 --- /dev/null +++ b/src/main/kotlin/Option.kt @@ -0,0 +1,7 @@ +package option + +sealed class Option<out A> +data class Some<out A>(val x: A) : Option<A>() + +object None : Option<Nothing>() + diff --git a/src/test/kotlin/MainKtTest.kt b/src/test/kotlin/MainKtTest.kt new file mode 100644 index 0000000000000000000000000000000000000000..b313201e3e157fc1740aeaad3e7717a4f5c54e96 --- /dev/null +++ b/src/test/kotlin/MainKtTest.kt @@ -0,0 +1,423 @@ +import listes.Liste +import listes.Nil +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test +import org.junit.jupiter.api.assertThrows + +class MainKtTest{ + /********************** + * Q1 - findFirst: recherche du 1er indice de chaîne de caractère dans une liste + *********************** */ + + @Test + fun findFirst_existing_element_in_non_empty_list() { + assertEquals(0,findFirst(Liste.of("1","2","3"),"1")) + assertEquals(2,findFirst(Liste.of("1","2","3"),"3")) + } + + @Test + fun findFirst_absent_element_in_non_empty_list_should_raise_exception() { + assertThrows<Exception> { + findFirst(Liste.of("1","2","3"),"blop") + } + } + @Test + fun findFirst_element_in_empty_list_should_raise_exception(){ + assertThrows<Exception> { + findFirst(Nil, "blop") + } + } + + /* *************** + * Q2 - findFirst: recherche du premier element d'une liste satisfaisant un prédicat donné + * *************** */ + + /* + @Test + fun findFirstWithPredicate_existing_element_in_non_empty_list() { + assertEquals(0,findFirst(Liste.of(1,2,3)) { it == 1}) + assertEquals(2,findFirst(Liste.of(1,2,3)) { it == 3}) + } + + @Test + fun findFirstWithPredicate_absent_element_in_non_empty_list_should_raise_exception() { + assertThrows<Exception> { + findFirst(Liste.of(1,2,3)) { it < 0 } + } + } + @Test + fun findFirstWithPredicate_element_in_empty_list_should_raise_exception(){ + assertThrows<Exception> { + findFirst(Nil as Liste<Int>) { it == 0 } + } + } + */ + + /* *************** + * Q3 - append: concatène deux listes + * *************** */ + + /* + @Test + fun append_two_nil_listes_returns_nil_liste(){ + assertEquals(Nil, append(Nil,Nil)) + } + + @Test + fun append_nil_with_non_nil_l_returns_l(){ + assertEquals(Liste.of(1,2), append(Nil,Liste.of(1,2))) + assertEquals(Liste.of("1","2"), append(Nil,Liste.of("1","2"))) + } + + @Test + fun append_non_nil_l_with_nil_returns_l(){ + assertEquals(Liste.of(1,2), append(Liste.of(1,2),Nil)) + assertEquals(Liste.of("1","2"), append(Liste.of("1","2"),Nil)) + } + */ + + + /* *************** + * Q4 - all: renvoie true si tous les elements de l statisfont p + * *************** */ + + /* + @Test + fun all_with_nil_list_returns_true(){ + assertTrue(all(Nil, { x -> true })) + } + + @Test + fun all_with_non_nil_list_and_some_element_not_satisfies_p_returns_false(){ + assertFalse(all(Liste.of(1,2,3), { x -> x<2 })) + assertFalse(all(Liste.of(1,2,3), { x -> x>2 })) + } + + @Test + fun any_with_non_nil_list_and_all_element_satisfy_p_returns_true(){ + assertTrue(all(Liste.of(1,2,3), { x -> x<4 })) + } + */ + + /* *************** + * Q5 - flatMap: concatène les listes d'une liste de listes + * *************** */ + + /* + @Test + fun flatMap_of_nil_returns_nil(){ + assertEquals(Nil, flatMap(Nil as Liste<Liste<Any>>)) + } + + @Test + fun flatMap_of_lists_returns_concatenation(){ + assertEquals(Liste.of(1,2,3,4,5,6), flatMap(Liste.of(Liste.of(1,2), Nil, Liste.of(3,4), Liste.of(5,6)))) + } + */ + + /* *************** + * Q6 - isSorted: renvoie true ssi la liste d'entiers est triée + * *************** */ + + /* + @Test + fun isSorted_empty_list_returns_true(){ + assertTrue(Nil.isSorted()) + } + + @Test + fun isSorted_sorted_list_returns_true(){ + assertTrue(Liste.of(1,5,13).isSorted()) + assertTrue(Liste.of(1,1).isSorted()) + assertTrue(Liste.of(1).isSorted()) + } + + @Test + fun isSorted_non_sorted_list_returns_false(){ + assertFalse(Liste.of(5,7,1).isSorted()) + assertFalse(Liste.of(1,1,0).isSorted()) + } + */ + + /* *************** + * Q7 - classe Personne + * *************** */ + + /* + @Test + fun Bob_is_younger_than_alice(){ + val bob = Personne("Bob", "Dylan", 15) + val alice = Personne("Alice", "Cooper", 21) + assertTrue(bob.compareTo(alice)<0) + } + + @Test + fun Alice_is_older_than_bob(){ + val bob = Personne("Bob", "Dylan", 15) + val alice = Personne("Alice", "Cooper", 21) + assertTrue(alice.compareTo(bob)>0) + } + + @Test + fun same_age_are_equal(){ + assertTrue(Personne("Bob", "Dylan", 15).compareTo(Personne("Mick", "Jagger", 15))==0) + } + */ + + /* *************** + * Q8 - isSortedGeneric: renvoie true ssi la liste d'éléments Comparable est triée + * *************** */ + + /* + @Test + fun isSortedGeneric_empty_list_returns_true(){ + val l : Liste<Personne> = Nil + assertTrue(Nil.isSortedGeneric()) + } + + @Test + fun isSortedGeneric_sorted_list_returns_true(){ + val listeDPersonnesTriee1 = Liste.of( + Personne("robert", "aaa", 19), + Personne("georges", "bbb", 21), + Personne("louis", "ccc", 23)) + + val listeDPersonnesTriee2 = Liste.of( + Personne("robert", "aaa", 19), + Personne("georges", "bbb", 19)) + + assertTrue(listeDPersonnesTriee1.isSortedGeneric()) + assertTrue(listeDPersonnesTriee2.isSortedGeneric()) + assertTrue(Liste.of(Personne("robert", "aaa", 19)).isSortedGeneric()) + } + + @Test + fun isSortedGeneric_non_sorted_list_returns_false(){ + val listeDePersonnesNonTriee = Liste.of( + Personne("robert", "aaa", 19), + Personne("louis", "ccc", 23), + Personne("georges", "bbb", 21)) + + assertFalse(listeDePersonnesNonTriee.isSortedGeneric()) + } + + */ + + /******************************* + * PARTIE 2 + *******************************/ + + /* *************** + * Q9 - map: applique une fonction f à des valeurs optionnelles + * *************** */ + + /* + @Test + fun map_on_None_returns_None(){ + assertEquals(None,None.map({ x -> x })) + } + + @Test + fun map_on_Some_returns_Some(){ + assertEquals(Some(6),Some("Kotlin").map({s:String -> s.length})) + assertEquals(Some(28),Some(14).map({a -> 2*a})) + } + */ + + /* *************** + * Q10 - incOption: incrémente un entier optionnel + * *************** */ + /* + @Test + fun incOption_on_None_returns_None(){ + assertEquals(None,incOption(None)) + } + + @Test + fun incOption_on_Some_returns_next_Some(){ + assertEquals(Some(1),incOption(Some(0))) + assertEquals(Some(2),incOption(Some(1))) + } + */ + + /* *************** + * Q11 - findFirst et traitement des exceptions avec Option + * *************** */ + /* + @Test + fun findFirstOption_existing_element_in_non_empty_list() { + assertEquals(Some(0),findFirstOption(Liste.of(1,2,3)) { it == 1}) + assertEquals(Some(2),findFirstOption(Liste.of(1,2,3)) { it == 3}) + } + + @Test + fun findFirstOption_absent_element_in_non_empty_list_returns_None() { + assertEquals(None, findFirstOption(Liste.of(1,2,3)) { it < 0 }) + } + @Test + fun findFirstOption_element_in_empty_list_returns_None(){ + assertEquals(None, findFirstOption(Nil as Liste<Int>) { it == 0 }) + } + */ + + /* *************** + * Q12 - findFirst récursif terminal et traitement des exceptions avec Option + * *************** */ + + /* + @Test + fun findFirstOption2_existing_element_in_non_empty_list() { + assertEquals(Some(0),findFirstOption2(Liste.of(1,2,3)) { it == 1}) + assertEquals(Some(2),findFirstOption2(Liste.of(1,2,3)) { it == 3}) + } + + @Test + fun findFirstOption2_absent_element_in_non_empty_list_returns_None() { + assertEquals(None, findFirstOption2(Liste.of(1,2,3)) { it < 0 }) + } + + @Test + fun findFirstOption2_element_in_empty_list_returns_None(){ + assertEquals(None, findFirstOption2(Nil as Liste<Int>) { it == 0 }) + } + */ + + /* *************** + * Q13 - lookupByName : renvoie le l'employe ayant le nom donné en paramètre + * *************** */ + + /* + val sj = Employe("Steve Jobs", "Commercial", None) + val sw = Employe("Steve Wozniak", "Développement", None) + val rw = Employe("Ronald Wayne", "Logistique", None) + val bob = Employe("Bob", "Commercial", Some(sj)) + + val lesEmployes = Liste.of(sj,sw,rw,bob, + Employe("Boby", "Commercial", Some(bob)), + Employe("Alice", "Commercial", Some(sj)), + Employe("Robert", "Développement", Some(sw)), + Employe("Barbara", "Développement", Some(sw)), + Employe("Louise", "Logistique", Some(rw)) + ) + + @Test + fun lookupByName_on_empty_list_returns_None(){ + assertEquals(None,lookupByName(Nil,"Gérard")) + } + + @Test + fun lookupByName_on_list_with_absent_name_returns_None(){ + assertEquals(None,lookupByName(lesEmployes,"Gérard")) + } + + @Test + fun lookupByName_on_list_with_present_name_returns_person(){ + assertEquals(Some(bob),lookupByName(lesEmployes,"Bob")) + } + */ + + /* *************** + * Q14 - flatmap sur Option, test avec "manager" + * - manager : renvoie le manager d'un employé donné s'il y en a un et None sinon + * *************** */ + + /* + fun manager(employes : Liste<Employe>, nom : String) : Option<Employe> = + lookupByName(employes,nom).flatMap { it.manager } + + @Test + fun manager_on_empty_list_returns_None(){ + assertEquals(None,manager(Nil, "Bob")) + } + + @Test + fun manager_on_list_with_absent_employee_returns_None(){ + assertEquals(None,manager(lesEmployes, "Gerard")) + } + + @Test + fun manager_on_list_with_existing_employee_and_absent_manager_returns_None(){ + assertEquals(None,manager(lesEmployes, "Steve Jobs")) + } + + @Test + fun manager_on_list_with_existing_employee_and_manager_returns_manager(){ + assertEquals(Some(sj),manager(lesEmployes, "Bob")) + } + */ + + /* *************** + * Q15 - De meilleurs employes + * *************** */ + + /* + val sj2 = Chef("Steve Jobs", "Commercial") + val sw2 = Chef("Steve Wozniak", "Développement") + val rw2 = Chef("Ronald Wayne", "Logistique") + val bob2 = Collaborateur("Bob", sj2) + + val lesSalaries = Liste.of(sj2,sw2,rw2,bob2, + Collaborateur("Boby", bob2), + Collaborateur("Alice", sj2), + Collaborateur("Robert", sw2), + Collaborateur("Barbara", sw2), + Collaborateur("Louise", rw2) + ) + + @Test + fun collaborators_have_a_department() { + assertEquals("Commercial",bob2.departement) + assertEquals("Commercial",Collaborateur("Boby", bob2).departement) + } + + @Test + fun chiefs_have_no_manager(){ + assertEquals(None,sj2.manager) + } + */ + + + /* *************** + * Q16 - Fibonacci + * *************** */ + + /* + @Test + fun fibo_0_returns_0(){ + assertEquals(0, fibonacci(0)) + } + + @Test + fun fibo_1_returns_1(){ + assertEquals(1, fibonacci(1)) + } + + @Test + fun fibo_recursive_cases(){ + assertEquals(13,fibonacci(7)) + assertEquals(34,fibonacci(9)) + } + */ + + /* *************** + * Q16 - Fibonacci récursif terminal + * *************** */ + + /* + @Test + fun fiboterm_0_returns_0(){ + assertEquals(0, fibonacciTerminal(0)) + } + + @Test + fun fiboterm_1_returns_1(){ + assertEquals(1, fibonacciTerminal(1)) + } + + @Test + fun fiboterm_recursive_cases(){ + assertEquals(13,fibonacciTerminal(7)) + assertEquals(34,fibonacciTerminal(9)) + } + */ +}