Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
sagaofsilence committed May 18, 2024
1 parent 417a12a commit ced017f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,10 @@ void operate() {
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.");
assertEquals(15, add.operate(10, 5));
assertEquals(5, subtract.operate(10, 5));
assertEquals(50, multiply.operate(10, 5));
assertEquals(2, divide.operate(10, 5));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,18 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

/** The Method reference test. */
/** Method reference test. */
public class MethodReferenceTest {
/** Static method reference. */
@Test
@Test
void staticMethodReference() {
List<Integer> numbers = List.of(1, -2, 3, -4, 5);
List<Integer> positiveNumbers = numbers.stream().map(Math::abs).toList();
positiveNumbers.forEach(
number -> Assertions.assertTrue(number > 0, "Number should be positive."));
positiveNumbers.forEach(number -> Assertions.assertTrue(number > 0));
}

/** The String number comparator. */
static class StringNumberComparator implements Comparator<String> {
static class StringNumberComparator implements Comparator<String> {
@Override
public int compare(String o1, String o2) {
if (o1 == null) {
Expand All @@ -35,33 +34,33 @@ public int compare(String o1, String o2) {
}

/** Instance method reference. */
@Test
@Test
void containingClassInstanceMethodReference() {
List<String> numbers = List.of("One", "Two", "Three");
List<Integer> numberChars = numbers.stream().map(String::length).toList();
numberChars.forEach(length -> Assertions.assertTrue(length > 0, "Number text is not empty."));
numberChars.forEach(length -> Assertions.assertTrue(length > 0));
}

/** Instance method reference. */
@Test
@Test
void containingObjectInstanceMethodReference() {
List<String> numbers = List.of("One", "Two", "Three");
StringNumberComparator comparator = new StringNumberComparator();
final List<String> sorted = numbers.stream().sorted(comparator::compare).toList();
final List<String> expected = List.of("One", "Three", "Two");
Assertions.assertEquals(expected, sorted, "Incorrect sorting.");
Assertions.assertEquals(expected, sorted);
}

/** Instance method arbitrary object particular type. */
@Test
@Test
void instanceMethodArbitraryObjectParticularType() {
List<Number> numbers = List.of(1, 2L, 3.0f, 4.0d);
List<Integer> numberIntValues = numbers.stream().map(Number::intValue).toList();
Assertions.assertEquals(List.of(1, 2, 3, 4), numberIntValues, "Int values are not same.");
Assertions.assertEquals(List.of(1, 2, 3, 4), numberIntValues);
}

/** Constructor reference. */
@Test
@Test
void constructorReference() {
List<String> numbers = List.of("1", "2", "3");
Map<String, BigInteger> numberMapping =
Expand All @@ -76,6 +75,6 @@ void constructorReference() {
put("3", BigInteger.valueOf(3));
}
};
Assertions.assertEquals(expected, numberMapping, "Mapped numbers do not match.");
Assertions.assertEquals(expected, numberMapping);
}
}

0 comments on commit ced017f

Please sign in to comment.