Skip to content

Commit 40ef6c6

Browse files
committed
Add Longs tests
Signed-off-by: Gábor Lipták <gliptak@gmail.com>
1 parent 33a1e5d commit 40ef6c6

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

core/src/main/java/fj/function/Longs.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,16 @@
11
package fj.function;
22

33
import fj.F;
4+
import fj.Monoid;
5+
import fj.data.List;
6+
import fj.data.Option;
47

58
import static fj.Function.curry;
69
import static fj.Semigroup.longAdditionSemigroup;
710
import static fj.Semigroup.longMultiplicationSemigroup;
811

12+
import static fj.data.Option.none;
13+
import static fj.data.Option.some;
914
import static java.lang.Math.abs;
1015

1116
/**
@@ -48,4 +53,38 @@ private Longs() {
4853
*/
4954
public static final F<Long, F<Long, Long>> remainder = curry((a, b) -> a % b);
5055

56+
/**
57+
* Sums a list of longs.
58+
*
59+
* @param longs A list of longs to sum.
60+
* @return The sum of the longs in the list.
61+
*/
62+
public static long sum(final List<Long> longs) {
63+
return Monoid.longAdditionMonoid.sumLeft(longs);
64+
}
65+
66+
/**
67+
* Returns the product of a list of integers.
68+
*
69+
* @param longs A list of longs to multiply together.
70+
* @return The product of the longs in the list.
71+
*/
72+
public static long product(final List<Long> longs) {
73+
return Monoid.longMultiplicationMonoid.sumLeft(longs);
74+
}
75+
76+
/**
77+
* A function that converts strings to integers.
78+
*
79+
* @return A function that converts strings to integers.
80+
*/
81+
public static F<String, Option<Long>> fromString() {
82+
return s -> {
83+
try { return some(Long.valueOf(s)); }
84+
catch (final NumberFormatException ignored) {
85+
return none();
86+
}
87+
};
88+
}
89+
5190
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package fj.function;
2+
3+
import org.junit.Test;
4+
5+
import java.lang.reflect.Constructor;
6+
import java.lang.reflect.InvocationTargetException;
7+
8+
import static fj.data.List.list;
9+
import static fj.data.Option.none;
10+
import static org.hamcrest.core.Is.is;
11+
import static org.junit.Assert.*;
12+
13+
public class LongsTest {
14+
15+
@Test
16+
public void testSum() {
17+
assertThat(Longs.sum(list(3L, 4L, 5L)), is(12L));
18+
}
19+
20+
@Test
21+
public void testProduct() {
22+
assertThat(Longs.product(list(3L, 4L, 5L)), is(60L));
23+
}
24+
25+
@Test
26+
public void testAdd() {
27+
assertThat(Longs.add.f(10L).f(20L), is(30L));
28+
}
29+
30+
@Test
31+
public void testMultiply() {
32+
assertThat(Longs.multiply.f(3L).f(5L), is(15L));
33+
}
34+
35+
@Test
36+
public void testAbs() {
37+
assertThat(Longs.abs.f(-5L), is(5L));
38+
}
39+
40+
@Test
41+
public void testFromString() {
42+
assertThat(Longs.fromString().f("-123").some(), is(-123L));
43+
}
44+
45+
@Test
46+
public void testFromStringFail() {
47+
assertThat(Longs.fromString().f("w"), is(none()));
48+
}
49+
50+
@Test
51+
public void testCannotInstantiate() throws NoSuchMethodException, IllegalAccessException, InstantiationException {
52+
Constructor<Longs> constructor = Longs.class.getDeclaredConstructor();
53+
constructor.setAccessible(true);
54+
try {
55+
constructor.newInstance();
56+
fail("expected InvocationTargetException");
57+
} catch (InvocationTargetException ite) {
58+
assertTrue(ite.getCause() instanceof UnsupportedOperationException);
59+
}
60+
}
61+
62+
}

0 commit comments

Comments
 (0)