Skip to content

Commit

Permalink
new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Glen K. Peterson committed Mar 13, 2014
1 parent 89ec445 commit 00e3457
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
13 changes: 11 additions & 2 deletions src/test/java/EasyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
import org.organicdesign.fp.ephemeral.View;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;

@RunWith(JUnit4.class)
public class EasyTest {
// This test should pass as-is to prove that everything is installed correctly
@Test public void helloWorld() {
assertArrayEquals(new String[] { "Hello World" },
View.ofArray("Hello World").toArray());
assertEquals("Hello World", "Hello World");
}
// Construct an array by passing individual arguments to View
@Test public void fromAndToArray() {
Expand Down Expand Up @@ -60,11 +62,18 @@ public class EasyTest {
// View.ofArray(1, 2, 3, 4).map(???).toArray());
}

// What higher-order function is used to accept numbers that match a certain test?
// Write a function to accept numbers that match a certain test
@Test public void filterEven() {
// assertArrayEquals(new Integer[]{2, 4, 6, 8},
// View.ofArray(1,2,3,4,5,6,7,8,9)
// .???(i -> i % 2 == 0).toArray());
// .filter(i -> ???).toArray());
}

// Concatenate some strings - try using the new :: method pointer syntax
@Test public void concat() {
// StringBuilder sB = new StringBuilder();
// View.ofArray("Little ", "strokes ", "fell ", "great ", "oaks.").forEach(???);
// assertEquals("Little strokes fell great oaks.", sB.toString());
}

// Chain together two higher order functions
Expand Down
34 changes: 31 additions & 3 deletions src/test/java/MediumTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ public void foldLeftTimes() {
// .foldLeft(???, (accum, item) -> accum * item));
}


// You can use foldLeft to do the same work as map()
// You can use foldLeft to do the same work as map() (but use map() in practice)
@Test
public void foldLeftMap() {
public void foldLeftMapLike() {
// assertArrayEquals(new Integer[] { 2, 3, 4, 5 },
// View.ofArray(1, 2, 3, 4)
// .foldLeft(new ArrayList<>(), (ls, i) -> {
Expand All @@ -51,4 +50,33 @@ public void foldLeftMap() {
// }).toArray());
}

// foldLeft can do the work of flatMap() too (but use flatMap() in practice)
@Test
public void foldLeftFlatMapLike() {
// assertArrayEquals(new Integer[] { 1,2,3, 10,20,30, 100,200,300 },
// View.ofArray(1, 10, 100)
// .foldLeft(new ArrayList<>(), (ls, i) -> {
// ???;
// ???;
// ???;
// return ls;
// }).toArray());
}

// foldLeft can find a maximum number
@Test
public void findMax() {
// assertEquals((Integer) 73,
// View.ofArray(-201, 53, 73, 26, -59, 72)
// .foldLeft(???, (max, i) -> (i > max) ? i : max));
}

// or a minimum
@Test
public void findMin() {
// assertEquals(Integer.valueOf(-201),
// View.ofArray(-201, 53, 73, 26, -59, 72)
// .foldLeft(Integer.MAX_VALUE, ???));
}

}

0 comments on commit 00e3457

Please sign in to comment.