Skip to content

Commit

Permalink
add three more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
CristianLopez3 committed Jun 6, 2024
1 parent c1ad391 commit ea3f790
Show file tree
Hide file tree
Showing 5 changed files with 140 additions and 23 deletions.
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<maven.compiler.source>21</maven.compiler.source>
<maven.compiler.target>21</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

Expand Down
39 changes: 39 additions & 0 deletions src/test/java/com/finance/programming/FilterAList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.finance.programming;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.Predicate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertIterableEquals;

public class FilterAList {

/**
* Filtering a list Description: Filter a list of integers to keep only the even numbers using streams and Predicate.
*/

@Test
@DisplayName("Test Even Numbers Filter")
void testEvenNumbers(){
List<Integer> givenNumbers = List.of(1, 3, 4, 10, 20, 23, 15, 17, 19, 402);
List<Integer> expectedNumbers = List.of(4, 10, 20, 402);

Predicate<Integer> filterEvenNumbers = number -> number % 2 == 0;

List<Integer> actualNumbers = givenNumbers.stream()
.filter(filterEvenNumbers)
.toList();

assertIterableEquals(expectedNumbers, actualNumbers,
"The content of the arrays is not the same");

assertEquals(expectedNumbers.size(), actualNumbers.size(),
"The size of the arrays does not match");

}


}
40 changes: 40 additions & 0 deletions src/test/java/com/finance/programming/MappingAList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.finance.programming;

import org.junit.jupiter.api.Test;

import java.util.List;

import static org.junit.jupiter.api.Assertions.*;

public class MappingAList {


/**
* Take a list of strings and use map to convert all strings to uppercase.
*/


@Test
void testStringToUpperCase(){
List<String> givenList = List.of("Spring", "java", "microservices", "aws", "api", "kubernetes");
List<String> expectedList = List.of("SPRING", "JAVA", "MICROSERVICES", "AWS", "API", "KUBERNETES");

List<String> actualList = givenList.stream()
.map(String::toUpperCase)
.toList();

assertIterableEquals(expectedList, actualList,
"The lists does not match their contents");

assertEquals(expectedList.get(1), actualList.get(1),
"The content of the given index does not match");

assertNotEquals(expectedList, givenList,
"The list match its content, the content must not match");

assertEquals(expectedList.size(), actualList.size(),
"The size does not match");

}

}
38 changes: 38 additions & 0 deletions src/test/java/com/finance/programming/ReduceAList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.finance.programming;

import org.junit.jupiter.api.Test;

import java.util.List;
import java.util.function.BinaryOperator;

import static org.junit.jupiter.api.Assertions.*;

public class ReduceAList {

/**
* Use reduce to sum all the elements of a list of integers.
*/

@Test
void testReduceToASumOfNumbers(){
List<Integer> givenList = List.of(1, 2, 3, 4, 5);
int expectedSum = 15;

// BinaryOperator<Integer> findASum = (x, y) -> x + y;
BinaryOperator<Integer> findASum = Integer::sum;


int actualSum = givenList.stream()
.reduce(findASum)
.orElse(0);

assertEquals(expectedSum, actualSum,
"The result does not match");

assertNotEquals(10, actualSum,
"The result match, it must not be the same");

}


}
42 changes: 21 additions & 21 deletions tests.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,63 +3,63 @@
Take a look to the exercises that I solve in the **test folder**

### Basic Functional Programming

1. **Exercise 1: Basic usage of lambdas**
✔️ **Exercise 1: Basic usage of lambdas**
Description: Create a lambda expression that takes two integers and returns their sum.

2. **Exercise 2: Using Function**
✔️ **Exercise 2: Using Function**
Description: Define a function that takes a string and returns its length using the Function interface.

3. **Exercise 3: Using Predicate**
✔️ **Exercise 3: Using Predicate**
Description: Define a Predicate that takes an integer and returns true if it is even.

4. **Exercise 4: Using Consumer**
✔️ **Exercise 4: Using Consumer**
Description: Define a Consumer that takes a string and prints it in uppercase.

5. **Exercise 5: Using Supplier**
✔️ **Exercise 5: Using Supplier**
Description: Define a Supplier that returns a random string of 10 characters.

6. **Exercise 6: Filtering a list**
✔️ **Exercise 6: Filtering a list**
Description: Filter a list of integers to keep only the even numbers using streams and Predicate.

7. **Exercise 7: Mapping a list**

✔️ **Exercise 7: Mapping a list**
Description: Take a list of strings and use map to convert all strings to uppercase.

8. **Exercise 8: Reducing a list**
✔️ **Exercise 8: Reducing a list**
Description: Use reduce to sum all the elements of a list of integers.

9. **Exercise 9: Using flatMap**
**Exercise 9: Using flatMap**
Description: Take a list of lists of integers and use flatMap to convert it into a flat list of integers.

10. **Exercise 10: Sorting a list**
**Exercise 10: Sorting a list**
Description: Sort a list of strings in reverse alphabetical order using streams.

11. **Exercise 11: Using collect**
**Exercise 11: Using collect**
Description: Convert a list of strings into a single comma-separated string using Collectors.joining.

12. **Exercise 12: Creating an infinite Stream**
**Exercise 12: Creating an infinite Stream**
Description: Use Stream.iterate to create an infinite stream of even numbers and take the first 10 elements.

13. **Exercise 13: Using Optional**
**Exercise 13: Using Optional**
Description: Use Optional to handle the possibility of null values when retrieving an element from a list.

14. **Exercise 14: Function composition**
**Exercise 14: Function composition**
Description: Combine two functions: one that multiplies a number by 2 and another that adds 3, and apply the composed function to a number.

15. **Exercise 15: Creating a Stream of objects**
**Exercise 15: Creating a Stream of objects**
Description: Create a Person class with name and age attributes. Then, create a stream of Person objects and filter those who are over 18 years old.

16. **Exercise 16: Grouping elements**
**Exercise 16: Grouping elements**
Description: Use Collectors.groupingBy to group a list of strings by their length.

17. **Exercise 17: Partitioning elements**
**Exercise 17: Partitioning elements**
Description: Use Collectors.partitioningBy to divide a list of integers into even and odd numbers.

18. **Exercise 18: Counting elements**
**Exercise 18: Counting elements**
Description: Use Collectors.counting to count the number of strings in a list.

19. **Exercise 19: Calculating statistics**
**Exercise 19: Calculating statistics**
Description: Use Collectors.summarizingInt to obtain statistics (sum, average, max, min) from a list of integers.

20. **Exercise 20: Using parallelStream**
**Exercise 20: Using parallelStream**
Description: Convert a list of integers into a parallelStream and find the sum of all elements.

0 comments on commit ea3f790

Please sign in to comment.