Skip to content

Commit

Permalink
Added example and test for functional interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 5, 2024
1 parent 7c39259 commit c808ab5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.reflectoring.function.custom;

/** The arithmetic operation functional interface. */
public interface ArithmeticOperation {
/**
* Operates on two integer inputs to calculate a result.
*
* @param a the first number
* @param b the second number
* @return the result
*/
int operate(int a, int b);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package io.reflectoring.function.custom;

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

import org.junit.jupiter.api.Test;

class ArithmeticOperationTest {

@Test
void operate() {
// Define operations
ArithmeticOperation add = (a, b) -> a + b;
ArithmeticOperation subtract = (a, b) -> a - b;
ArithmeticOperation multiply = (a, b) -> a * b;
ArithmeticOperation divide = (a, b) -> a / b;

// Use the operations
int addition = add.operate(10, 5); // Returns 15
int subtraction = subtract.operate(10, 5); // Returns 5
int multiplication = multiply.operate(10, 5); // Returns 50
int division = divide.operate(10, 5); // Returns 2

// Verify results
assertEquals(15, addition, "Result of addition is not correct.");
assertEquals(5, subtraction, "Result of subtraction is not correct.");
assertEquals(50, multiplication, "Result of multiplication is not correct.");
assertEquals(2, division, "Result of division is not correct.");
}
}

0 comments on commit c808ab5

Please sign in to comment.