From 9ee376d5d62e79db2db5383c48aa4f8778fd0005 Mon Sep 17 00:00:00 2001 From: Marja van Aken Date: Tue, 6 Feb 2024 16:20:33 +0100 Subject: [PATCH] Add additional test for reversed immutable list. --- .../metal/data/ReversedImmutableList.java | 4 +-- .../metal/data/ReversedImmutableListTest.java | 33 +++++++++++++++++-- 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/io/parsingdata/metal/data/ReversedImmutableList.java b/core/src/main/java/io/parsingdata/metal/data/ReversedImmutableList.java index c2d84e37..a3306c64 100644 --- a/core/src/main/java/io/parsingdata/metal/data/ReversedImmutableList.java +++ b/core/src/main/java/io/parsingdata/metal/data/ReversedImmutableList.java @@ -38,8 +38,8 @@ public ImmutableList addHead(final T head) { @Override public ImmutableList addList(final ImmutableList list) { - final ImmutableList ts2 = new ImmutableList<>(originalList); - ts2.addAll(list.reverse()); + final LinkedList ts2 = new LinkedList<>(list.reverse()); + ts2.addAll(originalList); return new ReversedImmutableList<>(ts2); } diff --git a/core/src/test/java/io/parsingdata/metal/data/ReversedImmutableListTest.java b/core/src/test/java/io/parsingdata/metal/data/ReversedImmutableListTest.java index f177fb7d..306d90d8 100644 --- a/core/src/test/java/io/parsingdata/metal/data/ReversedImmutableListTest.java +++ b/core/src/test/java/io/parsingdata/metal/data/ReversedImmutableListTest.java @@ -1,13 +1,11 @@ package io.parsingdata.metal.data; - import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertIterableEquals; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.util.ArrayList; import java.util.List; -import java.util.stream.Collectors; import java.util.stream.Stream; import org.junit.jupiter.params.ParameterizedTest; @@ -28,7 +26,36 @@ public static Stream addHeadTest() { @ParameterizedTest @MethodSource public void addHeadTest(final List originalToReverse, final int head, final List expected) { - final ImmutableList reverse = new ImmutableList<>(originalToReverse).reverse().addHead(head); + final ImmutableList reverse1 = new ImmutableList<>(originalToReverse).reverse(); + final ImmutableList reverse = reverse1.addHead(head); + final List actual = new ArrayList<>(reverse); + assertIterableEquals(expected, actual); + assertEquals(head, reverse.head()); + } + + public static Stream addListTest() { + return Stream.of( + arguments(List.of(), List.of(), List.of()), + arguments(List.of(1), List.of(), List.of(1)), + arguments(List.of(1, 2), List.of(), List.of(2, 1)), + arguments(List.of(1, 2, 3), List.of(), List.of(3, 2, 1)), + + arguments(List.of(), List.of(4), List.of(4)), + arguments(List.of(1), List.of(4), List.of(1, 4)), + arguments(List.of(1, 2), List.of(4), List.of(2, 1, 4)), + arguments(List.of(1, 2, 3), List.of(4), List.of(3, 2, 1, 4)), + + arguments(List.of(), List.of(4, 5), List.of(4, 5)), + arguments(List.of(1), List.of(4, 5), List.of(1, 4, 5)), + arguments(List.of(1, 2), List.of(4, 5), List.of(2, 1, 4, 5)), + arguments(List.of(1, 2, 3), List.of(4, 5), List.of(3, 2, 1, 4, 5)) + ); + } + + @ParameterizedTest + @MethodSource + public void addListTest(final List listToReverse, final List listToAdd, final List expected) { + final ImmutableList reverse = new ImmutableList<>(listToReverse).reverse().addList(new ImmutableList<>(listToAdd)); final List actual = new ArrayList<>(reverse); assertIterableEquals(expected, actual); }