Skip to content

Commit 00014fe

Browse files
committed
Added special functions tests.
1 parent 91fa789 commit 00014fe

File tree

1 file changed

+72
-0
lines changed
  • core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function

1 file changed

+72
-0
lines changed

core-java/functional-programming/functional-interfaces/src/test/java/io/reflectoring/function/FunctionTest.java

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package io.reflectoring.function;
22

3+
import java.time.Instant;
4+
import java.time.LocalDateTime;
5+
import java.time.ZoneOffset;
6+
import java.util.Date;
37
import java.util.function.*;
48
import java.util.stream.DoubleStream;
59
import java.util.stream.IntStream;
@@ -119,4 +123,72 @@ void doubleToLongFunction() {
119123
long[] result = input.mapToLong(celsiusToFahrenheit).toArray();
120124
Assertions.assertArrayEquals(new long[] {32, 77, 212}, result);
121125
}
126+
127+
@Test
128+
void toDoubleFunction() {
129+
ToDoubleFunction<Integer> fahrenheitToCelsius =
130+
(fahrenheit) -> (double) ((fahrenheit - 32) * 5) / 9;
131+
Assertions.assertEquals(0.0, fahrenheitToCelsius.applyAsDouble(32));
132+
Assertions.assertEquals(25.0, fahrenheitToCelsius.applyAsDouble(77));
133+
Assertions.assertEquals(100.0, fahrenheitToCelsius.applyAsDouble(212));
134+
}
135+
136+
@Test
137+
void toDoubleBiFunction() {
138+
// 30% discount when it is SALE else 10% standard discount
139+
ToDoubleBiFunction<String, Double> discountedPrice =
140+
(code, price) -> "SALE".equals(code) ? price * 0.7 : price * 0.9;
141+
Assertions.assertEquals(14.0, discountedPrice.applyAsDouble("SALE", 20.0));
142+
Assertions.assertEquals(18.0, discountedPrice.applyAsDouble("OFF_SEASON", 20.0));
143+
}
144+
145+
@Test
146+
void toIntFunction() {
147+
ToIntFunction<String> charCount = input -> input == null ? 0 : input.trim().length();
148+
149+
Assertions.assertEquals(0, charCount.applyAsInt(null));
150+
Assertions.assertEquals(0, charCount.applyAsInt(""));
151+
Assertions.assertEquals(3, charCount.applyAsInt("JOY"));
152+
}
153+
154+
@Test
155+
void toIntBiFunction() {
156+
// discount on product
157+
ToIntBiFunction<String, Integer> discount =
158+
(season, quantity) -> "WINTER".equals(season) || quantity > 100 ? 40 : 10;
159+
160+
Assertions.assertEquals(40, discount.applyAsInt("WINTER", 50));
161+
Assertions.assertEquals(40, discount.applyAsInt("SUMMER", 150));
162+
Assertions.assertEquals(10, discount.applyAsInt("FALL", 50));
163+
}
164+
165+
@Test
166+
void toLongFunction() {
167+
ToLongFunction<Date> elapsedTime =
168+
input -> input == null ? 0 : input.toInstant().toEpochMilli();
169+
170+
Assertions.assertEquals(0L, elapsedTime.applyAsLong(null));
171+
long now = System.currentTimeMillis();
172+
Date nowDate = Date.from(Instant.ofEpochMilli(now));
173+
Assertions.assertEquals(now, elapsedTime.applyAsLong(nowDate));
174+
}
175+
176+
@Test
177+
void toLongBiFunction() {
178+
// discount on product
179+
ToLongBiFunction<LocalDateTime, ZoneOffset> elapsed =
180+
(localDateTime, zoneOffset) ->
181+
zoneOffset == null
182+
? localDateTime.toEpochSecond(ZoneOffset.UTC)
183+
: localDateTime.toEpochSecond(zoneOffset);
184+
185+
final long now = System.currentTimeMillis();
186+
final LocalDateTime nowLocalDateTime = LocalDateTime.ofEpochSecond(now, 0, ZoneOffset.UTC);
187+
Assertions.assertEquals(now, elapsed.applyAsLong(nowLocalDateTime, null));
188+
189+
final long later = now + 1000;
190+
final ZoneOffset offset = ZoneOffset.ofHours(5);
191+
final LocalDateTime laterLocalDateTime = LocalDateTime.ofEpochSecond(later, 0, offset);
192+
Assertions.assertEquals(later, elapsed.applyAsLong(laterLocalDateTime, offset));
193+
}
122194
}

0 commit comments

Comments
 (0)