Skip to content

Commit 224f712

Browse files
committed
#173: Fixed order of foldMap for Set which switch the left and right branches
1 parent cfab9d2 commit 224f712

4 files changed

Lines changed: 57 additions & 31 deletions

File tree

core/src/main/java/fj/data/Set.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ public final <B> Set<B> map(final Ord<B> o, final F<A, B> f) {
265265
public final <B> B foldMap(final F<A, B> f, final Monoid<B> m) {
266266
return isEmpty() ?
267267
m.zero() :
268-
m.sum(m.sum(r().foldMap(f, m), f.f(head())), l().foldMap(f, m));
268+
m.sum(m.sum(l().foldMap(f, m), f.f(head())), r().foldMap(f, m));
269269
}
270270

271271
/**
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package fj.data;
2+
3+
import org.junit.Test;
4+
5+
import static fj.Ord.intOrd;
6+
import static org.hamcrest.CoreMatchers.equalTo;
7+
import static org.junit.Assert.assertThat;
8+
9+
/**
10+
* Created by MarkPerry on 18/08/2015.
11+
*/
12+
public class SetTest {
13+
14+
@Test
15+
public void toStream() {
16+
Set<Integer> s = Set.set(intOrd, 1, 2, 3);
17+
assertThat(s.toStream(), equalTo(Stream.stream(1, 2, 3)));
18+
}
19+
20+
@Test
21+
public void testString() {
22+
Set<Integer> s = Set.set(intOrd, 1, 2, 3);
23+
assertThat(s.toString(), equalTo("Set(1,2,3)"));
24+
}
25+
26+
}

core/src/test/java/fj/data/TestTreeMap.java

Lines changed: 0 additions & 26 deletions
This file was deleted.

core/src/test/java/fj/data/TreeMapTest.java

Lines changed: 30 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@
66
import fj.Ord;
77
import fj.P3;
88
import fj.Show;
9-
import fj.P;
9+
import fj.P2;
1010

1111
import org.junit.Test;
1212

13+
import static fj.P.p;
1314
import static fj.data.Option.some;
15+
import static org.hamcrest.CoreMatchers.equalTo;
1416
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertThat;
1518
import static org.junit.Assert.assertTrue;
1619

1720
/**
@@ -42,7 +45,7 @@ public void split() {
4245
Equal<Set<String>> seq = Equal.setEqual(Equal.stringEqual);
4346
Set<String> left = toSetString(List.range(1, pivot));
4447
Set<String> right = toSetString(List.range(pivot + 1, max + 1));
45-
P3<Set<String>, Option<String>, Set<String>> expected = P.p(left, some(Integer.toString(pivot)), right);
48+
P3<Set<String>, Option<String>, Set<String>> expected = p(left, some(Integer.toString(pivot)), right);
4649
assertTrue(Equal.p3Equal(seq, Equal.optionEqual(Equal.stringEqual), seq).eq(p, expected));
4750
}
4851

@@ -75,7 +78,7 @@ public void splitLookup() {
7578
// do the assert
7679
Equal<TreeMap<Integer, String>> tme = Equal.treeMapEqual(Equal.intEqual, Equal.stringEqual);
7780
Equal<P3<TreeMap<Integer, String>, Option<String>, TreeMap<Integer, String>>> eq = Equal.p3Equal(tme, Equal.optionEqual(Equal.stringEqual), tme);
78-
assertTrue(eq.eq(p3, P.p(leftMap, some(Integer.toString(pivot)), rightMap)));
81+
assertTrue(eq.eq(p3, p(leftMap, some(Integer.toString(pivot)), rightMap)));
7982
}
8083

8184
@Test
@@ -84,6 +87,29 @@ public void toMutableMap() {
8487
List<List<Integer>> l = List.range(1, max + 1).map(n -> List.single(n));
8588
TreeMap<List<Integer>, String> m2 = TreeMap.treeMap(Ord.listOrd(Ord.intOrd), l.zip(l.map(i -> i.toString())));
8689
Map<List<Integer>, String> mm = m2.toMutableMap();
87-
assertEquals(m2.keys().reverse(), List.iterableList(mm.keySet()));
90+
assertEquals(m2.keys(), List.iterableList(mm.keySet()));
8891
}
92+
93+
94+
@Test
95+
public void testLargeInserts() {
96+
// check that inserting a large number of items performs ok
97+
// taken from https://code.google.com/p/functionaljava/issues/detail?id=31 and
98+
// https://github.com/functionaljava/functionaljava/pull/13/files
99+
final int n = 10000;
100+
TreeMap<Integer, String> m = TreeMap.empty(Ord.intOrd);
101+
for (int i = 0; i < n; i++) {
102+
m = m.set(i, "abc " + i);
103+
}
104+
}
105+
106+
@Test
107+
public void testString() {
108+
TreeMap<Integer, String> t = TreeMap.treeMap(Ord.intOrd, p(1, "a"), p(2, "b"), p(3, "c"));
109+
TreeMap<Integer, String> t2 = TreeMap.treeMap(Ord.intOrd, p(3, "c"), p(2, "b"), p(1, "a"));
110+
Stream<P2<Integer, String>> s = Stream.stream(p(1, "a"), p(2, "b"), p(3, "c"));
111+
assertThat(t.toStream(), equalTo(s));
112+
assertThat(t2.toStream(), equalTo(s));
113+
}
114+
89115
}

0 commit comments

Comments
 (0)