From 5a95ad76227b727778c43a00deead236ab49d069 Mon Sep 17 00:00:00 2001 From: Dmitry Noranovich Date: Thu, 8 Dec 2016 07:40:35 -0500 Subject: [PATCH 1/5] dibs: I will implement list-ops --- .keep | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 .keep diff --git a/.keep b/.keep new file mode 100644 index 000000000..e69de29bb From c19aaa9b945351711178fcfba8c1752917369868 Mon Sep 17 00:00:00 2001 From: Dmitry Noranovich Date: Mon, 9 Jan 2017 15:32:35 -0500 Subject: [PATCH 2/5] Added list-ops exercise to the Java track --- config.json | 8 +- .../Exercism-d41d8cd98f00b204e9800998ecf8427e | Bin 0 -> 9131 bytes .../.nb-gradle/profiles/private/aux-config | 7 + exercises/list-ops/build.gradle | 18 + .../list-ops/src/example/java/ListOps.java | 56 +++ exercises/list-ops/src/main/java/.keep | 0 .../list-ops/src/test/java/ListOpsTest.java | 473 ++++++++++++++++++ exercises/settings.gradle | 1 + 8 files changed, 562 insertions(+), 1 deletion(-) create mode 100644 exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e create mode 100644 exercises/list-ops/.nb-gradle/profiles/private/aux-config create mode 100644 exercises/list-ops/build.gradle create mode 100644 exercises/list-ops/src/example/java/ListOps.java create mode 100644 exercises/list-ops/src/main/java/.keep create mode 100644 exercises/list-ops/src/test/java/ListOpsTest.java diff --git a/config.json b/config.json index 848b1e8a6..ec818a870 100644 --- a/config.json +++ b/config.json @@ -53,7 +53,8 @@ "all-your-base", "custom-set", "wordy", - "palindrome-products" + "palindrome-products", + "list-ops" ], "exercises": [ { @@ -305,6 +306,11 @@ "slug": "palindrome-products", "difficulty": 1, "topics": [] + }, + { + "slug": "list-ops", + "difficulty": 1, + "topics": [] } ], "deprecated": [ diff --git a/exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e b/exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e new file mode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O323CSiwybE-7=gM~C_4~t;|L`mG{8jGYWg%Bk3MRknGoplI{Zh3-7@%QI)Kz` z8hkX%b$pDdJN;@}zY4q$^+mx1OBy()fhiWo6<8z;CN)6dhhqw;#S25(GIwyQgBMMH zC~H$N4B|yE#%NK8z&O5=4;mm&00F}oLbPx@Mj(M_1)D&i;8{}w{?I&vPEC#!ju6lVoTk~9#`~H?5qk@%Nmo)H64UEwgK{%lh znwB-v7$V_k8i=YQ{p8elj}-3f?ISG_!x#ZwX(1TT6qPKy*9u*zci bPCaRm(d@ow)Q|@oQFnB@x{RI1iCXP{Sc + + + + + + diff --git a/exercises/list-ops/build.gradle b/exercises/list-ops/build.gradle new file mode 100644 index 000000000..9e1448e14 --- /dev/null +++ b/exercises/list-ops/build.gradle @@ -0,0 +1,18 @@ +apply plugin: "java" +apply plugin: "eclipse" +apply plugin: "idea" + +repositories { + mavenCentral() +} + +dependencies { + testCompile "junit:junit:4.12" +} + +test { + testLogging { + exceptionFormat = 'full' + events = ["passed", "failed", "skipped"] + } +} diff --git a/exercises/list-ops/src/example/java/ListOps.java b/exercises/list-ops/src/example/java/ListOps.java new file mode 100644 index 000000000..b44d91498 --- /dev/null +++ b/exercises/list-ops/src/example/java/ListOps.java @@ -0,0 +1,56 @@ + +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.BinaryOperator; +import java.util.function.Predicate; +import java.util.function.UnaryOperator; +import java.util.stream.Collectors; +import java.util.stream.Stream; + +public class ListOps { + + private ListOps() { + } + + public static int length(final List list) { + return list.size(); + } + + public static List reverse(final List list) { + List result = new ArrayList(list); + Collections.reverse(result); + return result; + } + + public static List map(final List list, + UnaryOperator mapper) { + return list.stream().map(mapper).collect(Collectors.toList()); + } + + public static List filter(final List list, + Predicate predicate) { + return list.stream().filter(predicate).collect(Collectors.toList()); + } + + public static U reduce(final List list, + U identity, + BiFunction accumulator, + BinaryOperator combiner) { + return list.stream().reduce(identity, accumulator, combiner); + } + + public static boolean append(final List listTo, + List listFrom) { + return listTo.addAll(listFrom); + } + + public static List concat(final List... lists) { + return Stream.of(lists) + .flatMap(Collection::stream) + .collect(Collectors.toList()); + } + +} diff --git a/exercises/list-ops/src/main/java/.keep b/exercises/list-ops/src/main/java/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/exercises/list-ops/src/test/java/ListOpsTest.java b/exercises/list-ops/src/test/java/ListOpsTest.java new file mode 100644 index 000000000..68b495c38 --- /dev/null +++ b/exercises/list-ops/src/test/java/ListOpsTest.java @@ -0,0 +1,473 @@ + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.function.BiFunction; +import java.util.function.BinaryOperator; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNotNull; +import static junit.framework.TestCase.assertTrue; +import org.junit.Ignore; +import org.junit.Test; + +public class ListOpsTest { + + private static final List EMPTY_LIST + = Collections.emptyList(); + + @Test + public void lengthOfAnEmptyListShouldBeZero() { + final int expected = 0; + final int actual = ListOps.length(EMPTY_LIST); + + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReturnCorrectLengthOfAnNonEmptyList() { + final int expected = 4; + final List list = Collections.unmodifiableList( + IntStream.range(0, expected) + .boxed() + .collect(Collectors.toList()) + ); + final int actual = ListOps.length(list); + + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReverseAnEmptyList() { + final List actual = ListOps.reverse(EMPTY_LIST); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldReverseNonEmptyList() { + final int length = 100; + final int startValue = 0; + final List reversedList + = IntStream.range(startValue, length) + .boxed() + .collect(Collectors.toList()); + Collections.reverse(reversedList); + final List expected + = Collections.unmodifiableList(reversedList); + final List list = Collections.unmodifiableList( + IntStream.range(startValue, length) + .boxed() + .collect(Collectors.toList()) + ); + final List actual + = ListOps.reverse(list); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void mapOfAnEmptyListShouldBeAnEmptyList() { + final List actual = ListOps.map(EMPTY_LIST, x -> x + 1); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldMapNonEmptyList() { + final List expected + = Collections.unmodifiableList(Arrays.asList(2, 4, 6, 8)); + final List list + = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7)); + final List actual = ListOps.map(list, x -> x + 1); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void fileteredEmptyListShouldBeAnEmptyList() { + final List actual = ListOps.filter(EMPTY_LIST, x -> x > 0); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldFilterNonEmptyList() { + final List expected + = Collections.unmodifiableList(Arrays.asList(1, 3)); + final List list = Collections.unmodifiableList( + IntStream.range(0, 4).boxed().collect(Collectors.toList()) + ); + final List actual = ListOps.filter(list, x -> x % 2 > 0); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReturnAnEmptyListWhenAnEmptyListIsAppendedToAnEmptyList() { + List listTo = Collections.emptyList(); + assertFalse(ListOps.append(listTo, EMPTY_LIST)); + final List actual = listTo; + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldAppendAnEmptyListToANonEmptyList() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List listTo + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); + assertFalse(ListOps.append(listTo, EMPTY_LIST)); + final List actual = listTo; + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldAppendANonEmptyListToAnEmptyList() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + List listTo = new ArrayList<>(); + final List listFrom + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + assertTrue(ListOps.append(listTo, listFrom)); + final List actual = listTo; + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldAppendNonEmptyLists() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 8) + .boxed() + .collect(Collectors.toList()) + ); + final List listTo + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); + final List listFrom + = Collections.unmodifiableList( + IntStream.range(4, 8) + .boxed() + .collect(Collectors.toList()) + ); + assertTrue(ListOps.append(listTo, listFrom)); + final List actual = listTo; + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldConcatenateZeroLists() { + List actual = ListOps.concat(); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldConcatenateOneNonEmptyList() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List list + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List actual = ListOps.concat(list); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldConcatenateOneEmptyList() { + final List actual = ListOps.concat(EMPTY_LIST); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldConcatenateTwoEmptyLists() { + final List actual = ListOps.concat(EMPTY_LIST, EMPTY_LIST); + + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List list + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List actual = ListOps.concat(list, EMPTY_LIST); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldConcatenateOneNonEmptyAndOneEmptyLists() { + final List expected + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List list + = Collections.unmodifiableList( + IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()) + ); + final List actual = ListOps.concat(EMPTY_LIST, list); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldConcatenateTwoListsWithSameElements() { + final List list1 = Collections.unmodifiableList( + IntStream.range(0, 4).boxed().collect(Collectors.toList()) + ); + final List list2 = Collections.unmodifiableList( + IntStream.range(1, 6).boxed().collect(Collectors.toList()) + ); + final List sortedCancatenatedList + = Stream.concat(list1.stream(), list2.stream()) + .collect(Collectors.toList()); + Collections.sort(sortedCancatenatedList); + final List expected = Collections.unmodifiableList( + sortedCancatenatedList); + final List actual = ListOps.concat(list1, list2); + Collections.sort(actual); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldConcatenateSeveralLists() { + final List list1 = Collections.unmodifiableList( + IntStream.range(0, 4).boxed().collect(Collectors.toList()) + ); + final List list2 = Collections.unmodifiableList( + IntStream.range(4, 8).boxed().collect(Collectors.toList()) + ); + final List list3 = Collections.unmodifiableList( + IntStream.range(8, 12).boxed().collect(Collectors.toList()) + ); + final List list4 = Collections.unmodifiableList( + IntStream.range(12, 16).boxed().collect(Collectors.toList()) + ); + final List sortedCancatenatedList + = new ArrayList<>(list1); + sortedCancatenatedList.addAll(list2); + sortedCancatenatedList.addAll(list3); + sortedCancatenatedList.addAll(list4); + Collections.sort(sortedCancatenatedList); + final List expected = Collections.unmodifiableList( + sortedCancatenatedList + ); + final List actual + = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4); + Collections.sort(actual); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReturnIdentityWhenAnEmptyListIsReduced() { + final int expected = 0; + final int actual + = ListOps.reduce(EMPTY_LIST, 0, (x, y) -> x + y, Integer::sum); + + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldCalculateTheSumOfANonEmptyIntegerList() { + final int expected = 10; + final List list = Collections.unmodifiableList( + IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()) + ); + final int actual = ListOps.reduce(list, 0, + (x, y) -> x + y, + Integer::sum); + + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReduceWithAnticommutativeAccumulator() { + final int expected = 0; + final List list = Collections.unmodifiableList( + IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()) + ); + final int actual + = ListOps.reduce(list, 10, (x, y) -> x - y, (x, y) + -> { + throw new IllegalStateException( + "Operation cannot be parallelyzed."); + }); + + assertEquals(expected, actual); + } + + /* + https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html + https://docs.oracle.com/javase/8/docs/api/java/util/stream/Stream.html#reduce-U-java.util.function.BiFunction-java.util.function.BinaryOperator- + */ + private BiFunction, Integer, List> accumulator + = (List partial, Integer elem) -> { + List result = new ArrayList<>(partial); + result.add(elem); + return result; + }; + + private BinaryOperator> combiner + = (list1, list2) -> { + List result = new ArrayList<>(list1); + result.addAll(list2); + return result; + }; + + @Test + @Ignore + public void reduceShouldConcatenateAnEmptyListAndANonEmptyList() { + final List expected = Collections.unmodifiableList( + IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()) + ); + final List list = Collections.unmodifiableList( + IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()) + ); + final List actual + = ListOps.reduce(list, + new ArrayList(), + accumulator, + combiner); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void reduceShouldConcatenateTwoNonEmptyLists() { + final List expected = Collections.unmodifiableList( + IntStream.range(0, 10) + .boxed() + .collect(Collectors.toList()) + ); + final List listOne = Collections.unmodifiableList( + IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()) + ); + final List listTwo = Collections.unmodifiableList( + IntStream.range(5, 10) + .boxed() + .collect(Collectors.toList()) + ); + final List actual + = ListOps.reduce(listTwo, + listOne, + accumulator, + combiner); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } +} diff --git a/exercises/settings.gradle b/exercises/settings.gradle index b076cc220..886e932e4 100644 --- a/exercises/settings.gradle +++ b/exercises/settings.gradle @@ -20,6 +20,7 @@ include 'hexadecimal' include 'hello-world' include 'largest-series-product' include 'linked-list' +include 'list-ops' include 'luhn' include 'minesweeper' include 'meetup' From bf2c51b7f3966635378f8064dd19bdb796f36130 Mon Sep 17 00:00:00 2001 From: Dmitry Noranovich Date: Mon, 9 Jan 2017 16:25:53 -0500 Subject: [PATCH 3/5] Removed NetBeans settings from the project dir --- .../Exercism-d41d8cd98f00b204e9800998ecf8427e | Bin 9131 -> 0 bytes .../.nb-gradle/profiles/private/aux-config | 7 ------- 2 files changed, 7 deletions(-) delete mode 100644 exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e delete mode 100644 exercises/list-ops/.nb-gradle/profiles/private/aux-config diff --git a/exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e b/exercises/list-ops/.nb-gradle/private/cache/Exercism-d41d8cd98f00b204e9800998ecf8427e deleted file mode 100644 index ebfde4f5a810f616c21ed97b382f3f49fbe69cf3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 9131 zcmcf{OKcoj@%jImas0_o0VV7zo3Mmji-~I>Dlf# z_9TZW9JsOwX+bLlCnPR8E@H(Eap170Z~-BKIBO_8X$7IzZc+_MeGe!{KW#hV1M ziR(U-|N8pbzz6J%=ep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#!ju6lVoTk~9#`~H?5qk@%Nmo)H64UEwgK{%lh znwB-v7$V_k8i=YQ{p8elj}-3f?ISG_!x#ZwX(1TT6qPKy*9u*zci bPCaRm(d@ow)Q|@oQFnB@x{RI1iCXP{Sc - - - - - - From 716536edba7c0db7832691104cd2320d83c8c227 Mon Sep 17 00:00:00 2001 From: Dmitry Noranovich Date: Thu, 12 Jan 2017 15:56:43 -0500 Subject: [PATCH 4/5] Updated test according to some changes required --- .../list-ops/src/test/java/ListOpsTest.java | 115 +++++++----------- 1 file changed, 47 insertions(+), 68 deletions(-) diff --git a/exercises/list-ops/src/test/java/ListOpsTest.java b/exercises/list-ops/src/test/java/ListOpsTest.java index 68b495c38..f3ab34c3d 100644 --- a/exercises/list-ops/src/test/java/ListOpsTest.java +++ b/exercises/list-ops/src/test/java/ListOpsTest.java @@ -30,14 +30,14 @@ public void lengthOfAnEmptyListShouldBeZero() { @Test @Ignore - public void shouldReturnCorrectLengthOfAnNonEmptyList() { - final int expected = 4; + public void shouldReturnTheCorrectLengthOfAnNonEmptyList() { final List list = Collections.unmodifiableList( - IntStream.range(0, expected) + IntStream.range(0, 4) .boxed() .collect(Collectors.toList()) ); final int actual = ListOps.length(list); + final int expected = list.size(); assertEquals(expected, actual); } @@ -53,7 +53,7 @@ public void shouldReverseAnEmptyList() { @Test @Ignore - public void shouldReverseNonEmptyList() { + public void shouldReverseANonEmptyList() { final int length = 100; final int startValue = 0; final List reversedList @@ -61,8 +61,6 @@ public void shouldReverseNonEmptyList() { .boxed() .collect(Collectors.toList()); Collections.reverse(reversedList); - final List expected - = Collections.unmodifiableList(reversedList); final List list = Collections.unmodifiableList( IntStream.range(startValue, length) .boxed() @@ -70,6 +68,7 @@ public void shouldReverseNonEmptyList() { ); final List actual = ListOps.reverse(list); + final List expected = reversedList; assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -78,7 +77,7 @@ public void shouldReverseNonEmptyList() { @Test @Ignore - public void mapOfAnEmptyListShouldBeAnEmptyList() { + public void shouldMapAnEmptyListAndReturnAnEmptyList() { final List actual = ListOps.map(EMPTY_LIST, x -> x + 1); assertNotNull(actual); @@ -88,11 +87,11 @@ public void mapOfAnEmptyListShouldBeAnEmptyList() { @Test @Ignore public void shouldMapNonEmptyList() { - final List expected - = Collections.unmodifiableList(Arrays.asList(2, 4, 6, 8)); final List list = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7)); final List actual = ListOps.map(list, x -> x + 1); + final List expected + = Arrays.asList(2, 4, 6, 8); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -101,7 +100,7 @@ public void shouldMapNonEmptyList() { @Test @Ignore - public void fileteredEmptyListShouldBeAnEmptyList() { + public void shouldFilterAnEmptyListanddReturnAnEmptyList() { final List actual = ListOps.filter(EMPTY_LIST, x -> x > 0); assertNotNull(actual); @@ -111,12 +110,11 @@ public void fileteredEmptyListShouldBeAnEmptyList() { @Test @Ignore public void shouldFilterNonEmptyList() { - final List expected - = Collections.unmodifiableList(Arrays.asList(1, 3)); final List list = Collections.unmodifiableList( IntStream.range(0, 4).boxed().collect(Collectors.toList()) ); final List actual = ListOps.filter(list, x -> x % 2 > 0); + final List expected = Arrays.asList(1, 3); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -137,18 +135,16 @@ public void shouldReturnAnEmptyListWhenAnEmptyListIsAppendedToAnEmptyList() { @Test @Ignore public void shouldAppendAnEmptyListToANonEmptyList() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 4) - .boxed() - .collect(Collectors.toList()) - ); final List listTo = IntStream.range(0, 4) .boxed() .collect(Collectors.toList()); assertFalse(ListOps.append(listTo, EMPTY_LIST)); final List actual = listTo; + final List expected + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -158,12 +154,6 @@ public void shouldAppendAnEmptyListToANonEmptyList() { @Test @Ignore public void shouldAppendANonEmptyListToAnEmptyList() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 4) - .boxed() - .collect(Collectors.toList()) - ); List listTo = new ArrayList<>(); final List listFrom = Collections.unmodifiableList( @@ -173,6 +163,10 @@ public void shouldAppendANonEmptyListToAnEmptyList() { ); assertTrue(ListOps.append(listTo, listFrom)); final List actual = listTo; + final List expected + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -182,12 +176,6 @@ public void shouldAppendANonEmptyListToAnEmptyList() { @Test @Ignore public void shouldAppendNonEmptyLists() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 8) - .boxed() - .collect(Collectors.toList()) - ); final List listTo = IntStream.range(0, 4) .boxed() @@ -200,6 +188,10 @@ public void shouldAppendNonEmptyLists() { ); assertTrue(ListOps.append(listTo, listFrom)); final List actual = listTo; + final List expected + = IntStream.range(0, 8) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -218,12 +210,6 @@ public void shouldConcatenateZeroLists() { @Test @Ignore public void shouldConcatenateOneNonEmptyList() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 4) - .boxed() - .collect(Collectors.toList()) - ); final List list = Collections.unmodifiableList( IntStream.range(0, 4) @@ -231,6 +217,10 @@ public void shouldConcatenateOneNonEmptyList() { .collect(Collectors.toList()) ); final List actual = ListOps.concat(list); + final List expected + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -258,12 +248,6 @@ public void shouldConcatenateTwoEmptyLists() { @Test @Ignore public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 4) - .boxed() - .collect(Collectors.toList()) - ); final List list = Collections.unmodifiableList( IntStream.range(0, 4) @@ -271,6 +255,10 @@ public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { .collect(Collectors.toList()) ); final List actual = ListOps.concat(list, EMPTY_LIST); + final List expected + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -280,12 +268,6 @@ public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { @Test @Ignore public void shouldConcatenateOneNonEmptyAndOneEmptyLists() { - final List expected - = Collections.unmodifiableList( - IntStream.range(0, 4) - .boxed() - .collect(Collectors.toList()) - ); final List list = Collections.unmodifiableList( IntStream.range(0, 4) @@ -293,6 +275,10 @@ public void shouldConcatenateOneNonEmptyAndOneEmptyLists() { .collect(Collectors.toList()) ); final List actual = ListOps.concat(EMPTY_LIST, list); + final List expected + = IntStream.range(0, 4) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -308,14 +294,10 @@ public void shouldConcatenateTwoListsWithSameElements() { final List list2 = Collections.unmodifiableList( IntStream.range(1, 6).boxed().collect(Collectors.toList()) ); - final List sortedCancatenatedList + final List expected = Stream.concat(list1.stream(), list2.stream()) .collect(Collectors.toList()); - Collections.sort(sortedCancatenatedList); - final List expected = Collections.unmodifiableList( - sortedCancatenatedList); final List actual = ListOps.concat(list1, list2); - Collections.sort(actual); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -343,9 +325,8 @@ public void shouldConcatenateSeveralLists() { sortedCancatenatedList.addAll(list3); sortedCancatenatedList.addAll(list4); Collections.sort(sortedCancatenatedList); - final List expected = Collections.unmodifiableList( - sortedCancatenatedList - ); + final List expected + = sortedCancatenatedList; final List actual = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4); Collections.sort(actual); @@ -420,12 +401,7 @@ public void shouldReduceWithAnticommutativeAccumulator() { @Test @Ignore - public void reduceShouldConcatenateAnEmptyListAndANonEmptyList() { - final List expected = Collections.unmodifiableList( - IntStream.range(0, 5) - .boxed() - .collect(Collectors.toList()) - ); + public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() { final List list = Collections.unmodifiableList( IntStream.range(0, 5) .boxed() @@ -436,6 +412,10 @@ public void reduceShouldConcatenateAnEmptyListAndANonEmptyList() { new ArrayList(), accumulator, combiner); + final List expected + = IntStream.range(0, 5) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); @@ -444,12 +424,7 @@ public void reduceShouldConcatenateAnEmptyListAndANonEmptyList() { @Test @Ignore - public void reduceShouldConcatenateTwoNonEmptyLists() { - final List expected = Collections.unmodifiableList( - IntStream.range(0, 10) - .boxed() - .collect(Collectors.toList()) - ); + public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() { final List listOne = Collections.unmodifiableList( IntStream.range(0, 5) .boxed() @@ -465,6 +440,10 @@ public void reduceShouldConcatenateTwoNonEmptyLists() { listOne, accumulator, combiner); + final List expected + = IntStream.range(0, 10) + .boxed() + .collect(Collectors.toList()); assertNotNull(actual); assertFalse(actual.isEmpty()); From bc261457042c1e8722c7569cf692184dbadd12ca Mon Sep 17 00:00:00 2001 From: Dmitry Noranovich Date: Thu, 26 Jan 2017 17:05:06 -0500 Subject: [PATCH 5/5] Added collect() operation and tests --- .../list-ops/src/example/java/ListOps.java | 15 ++ .../list-ops/src/test/java/ListOpsTest.java | 194 ++++++++++++++---- 2 files changed, 165 insertions(+), 44 deletions(-) diff --git a/exercises/list-ops/src/example/java/ListOps.java b/exercises/list-ops/src/example/java/ListOps.java index 7a0fe66dd..acc71ec60 100644 --- a/exercises/list-ops/src/example/java/ListOps.java +++ b/exercises/list-ops/src/example/java/ListOps.java @@ -3,10 +3,13 @@ import java.util.Collection; import java.util.Collections; import java.util.List; +import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.BinaryOperator; import java.util.function.Predicate; +import java.util.function.Supplier; import java.util.function.UnaryOperator; +import java.util.stream.Collector; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -48,4 +51,16 @@ public static List concat(final List... lists) { .collect(Collectors.toList()); } + public static R collect(final List list, + Supplier supplier, + BiConsumer accumulator, + BiConsumer combiner) { + return list.stream().collect(supplier, accumulator, combiner); + } + + public static R collect(final List list, + Collector collector) { + return list.stream().collect(collector); + } + } diff --git a/exercises/list-ops/src/test/java/ListOpsTest.java b/exercises/list-ops/src/test/java/ListOpsTest.java index ba75e951d..a3bdd4b28 100644 --- a/exercises/list-ops/src/test/java/ListOpsTest.java +++ b/exercises/list-ops/src/test/java/ListOpsTest.java @@ -17,18 +17,18 @@ import org.junit.Test; public class ListOpsTest { - + private static final List EMPTY_LIST = Collections.emptyList(); - + @Test public void lengthOfAnEmptyListShouldBeZero() { final int expected = 0; final int actual = ListOps.length(EMPTY_LIST); - + assertEquals(expected, actual); } - + @Test @Ignore public void shouldReturnTheCorrectLengthOfAnNonEmptyList() { @@ -37,19 +37,19 @@ public void shouldReturnTheCorrectLengthOfAnNonEmptyList() { ); final int actual = ListOps.length(list); final int expected = list.size(); - + assertEquals(expected, actual); } - + @Test @Ignore public void shouldReverseAnEmptyList() { final List actual = ListOps.reverse(EMPTY_LIST); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldReverseANonEmptyList() { @@ -60,42 +60,42 @@ public void shouldReverseANonEmptyList() { = ListOps.reverse(list); final List expected = Arrays.asList(8, 7, 6, 5, 4, 3, 2, 1, 0); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldMapAnEmptyListAndReturnAnEmptyList() { final List actual = ListOps.map(EMPTY_LIST, x -> x + 1); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldMapNonEmptyList() { final List list = Collections.unmodifiableList(Arrays.asList(1, 3, 5, 7)); final List actual = ListOps.map(list, x -> x + 1); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(Arrays.asList(2, 4, 6, 8), actual); } - + @Test @Ignore public void shouldFilterAnEmptyListanddReturnAnEmptyList() { final List actual = ListOps.filter(EMPTY_LIST, x -> x > 0); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldFilterNonEmptyList() { @@ -107,21 +107,21 @@ public void shouldFilterNonEmptyList() { final List expected = list.stream() .filter(predicate) .collect(Collectors.toList()); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldConcatenateZeroLists() { List actual = ListOps.concat(); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldConcatenateOneNonEmptyList() { @@ -131,30 +131,30 @@ public void shouldConcatenateOneNonEmptyList() { ); final List actual = ListOps.concat(list); final List expected = Arrays.asList(0, 1, 2, 3, 4); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldConcatenateOneEmptyList() { final List actual = ListOps.concat(EMPTY_LIST); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldConcatenateTwoEmptyLists() { final List actual = ListOps.concat(EMPTY_LIST, EMPTY_LIST); - + assertNotNull(actual); assertTrue(actual.isEmpty()); } - + @Test @Ignore public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { @@ -165,12 +165,12 @@ public void shouldConcatenateOneEmptyAndOneNonEmptyLists() { final List actual = ListOps.concat(list, EMPTY_LIST); final List expected = Arrays.asList(0, 1, 2, 3, 4); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldConcatenateOneNonEmptyAndOneEmptyLists() { @@ -181,12 +181,12 @@ public void shouldConcatenateOneNonEmptyAndOneEmptyLists() { final List actual = ListOps.concat(EMPTY_LIST, list); final List expected = Arrays.asList(0, 1, 2, 3, 4); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldConcatenateTwoListsWithSameElements() { @@ -200,12 +200,12 @@ public void shouldConcatenateTwoListsWithSameElements() { = Stream.concat(list1.stream(), list2.stream()) .collect(Collectors.toList()); final List actual = ListOps.concat(list1, list2); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldConcatenateSeveralLists() { @@ -224,25 +224,25 @@ public void shouldConcatenateSeveralLists() { final List expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15); - + final List actual = ListOps.concat(list1, list2, EMPTY_LIST, list3, list4); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldReturnIdentityWhenAnEmptyListIsReduced() { final int expected = 0; final int actual = ListOps.reduce(EMPTY_LIST, 0, (x, y) -> x + y, Integer::sum); - + assertEquals(expected, actual); } - + @Test @Ignore public void shouldCalculateTheSumOfANonEmptyIntegerList() { @@ -252,10 +252,10 @@ public void shouldCalculateTheSumOfANonEmptyIntegerList() { final int actual = ListOps.reduce(list, 0, (x, y) -> x + y, Integer::sum); - + assertEquals(10, actual); } - + @Test @Ignore public void shouldReduceWithAnticommutativeAccumulator() { @@ -268,7 +268,7 @@ public void shouldReduceWithAnticommutativeAccumulator() { throw new IllegalStateException( "Operation cannot be parallelyzed."); }); - + assertEquals(0, actual); } @@ -282,14 +282,14 @@ public void shouldReduceWithAnticommutativeAccumulator() { result.add(elem); return result; }; - + private BinaryOperator> combiner = (list1, list2) -> { List result = new ArrayList<>(list1); result.addAll(list2); return result; }; - + @Test @Ignore public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() { @@ -303,12 +303,12 @@ public void shouldReduceAnEmptyListAndANonEmptyListAndReturnConcatenation() { combiner); final List expected = Arrays.asList(0, 1, 2, 3, 4, 5); - + assertNotNull(actual); assertFalse(actual.isEmpty()); assertEquals(expected, actual); } - + @Test @Ignore public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() { @@ -325,9 +325,115 @@ public void shouldReduceTwoNonEmptyListsAndReturnConcatenation() { combiner); final List expected = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldReturnZeroWhenAnEmptyListIsCollected() { + final int expected = 0; + final int actual + = ListOps.collect(EMPTY_LIST, + Collectors.summingInt(Integer::intValue)); + + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldCalculateTheSumOfANonEmptyIntegerListUsigCollect() { + final List list = Collections.unmodifiableList( + Arrays.asList(0, 1, 2, 3, 4) + ); + final int actual = ListOps.collect(list, + Collectors.summingInt(Integer::intValue)); + + assertEquals(10, actual); + } + @Ignore + public void shouldCollectAnEmptyList() { + final List actual = ListOps.collect(EMPTY_LIST, + ArrayList::new, ArrayList::add, ArrayList::addAll); + assertNotNull(actual); + assertTrue(actual.isEmpty()); + } + + @Test + @Ignore + public void shouldCollectANonEmptyListAndReturnTheSameList() { + final List list = Collections.unmodifiableList( + Arrays.asList(0, 1, 2, 3, 4, 5) + ); + final List actual + = ListOps.collect(list, + ArrayList::new, ArrayList::add, ArrayList::addAll); + final List expected + = Arrays.asList(0, 1, 2, 3, 4, 5); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldCollectTwoNonEmptyListsAndReturnConcatenation() { + final List listOne = Collections.unmodifiableList( + Arrays.asList(0, 1, 2, 3, 4) + ); + final List listTwo = Collections.unmodifiableList( + Arrays.asList(5, 6, 7, 8, 9) + ); + final List actual + = ListOps.collect(listTwo, + () -> new ArrayList(listOne), + ArrayList::add, + ArrayList::addAll + ); + final List expected + = Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); + + assertNotNull(actual); + assertFalse(actual.isEmpty()); + assertEquals(expected, actual); + } + + @Test + @Ignore + public void shouldCollectAnEmptyListAndProduceAnEmptyString() { + final StringBuilder expected + = ListOps.collect(EMPTY_LIST, StringBuilder::new, + StringBuilder::append, StringBuilder::append); + + assertNotNull(expected); + assertTrue(expected.toString().isEmpty()); + } + + @Test + @Ignore + public void shouldConcatenateStringsUsingCollect() { + final List list = Collections.unmodifiableList( + Arrays.asList("we ", "could ", "use ", "a ", "parallelizable ", + "collect ", "form") + ); + + final StringBuilder builder + = ListOps.collect(list, + StringBuilder::new, StringBuilder::append, + StringBuilder::append); + assertNotNull(builder); + + final String actual + = builder.toString(); + final String expected = "we could use a parallelizable collect form"; + assertFalse(actual.isEmpty()); assertEquals(expected, actual); } + }