diff --git a/src/test/java/software/leonov/common/util/function/NaturalOrderingTest.java b/src/test/java/software/leonov/common/util/function/NaturalOrderingTest.java new file mode 100644 index 0000000..5ac8582 --- /dev/null +++ b/src/test/java/software/leonov/common/util/function/NaturalOrderingTest.java @@ -0,0 +1,78 @@ +package software.leonov.common.util.function; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.truth.Truth.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static software.leonov.common.util.function.CheckedComparator.evalUnchecked; +import static software.leonov.common.util.function.CheckedComparator.naturalOrder; + +import java.util.List; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class NaturalOrderingTest { + + @BeforeAll + static void setUpBeforeClass() throws Exception { + } + + @AfterAll + static void tearDownAfterClass() throws Exception { + } + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void test_naturalOrder_strings() throws Exception { + final List actual = newArrayList("b", "a", "d", "c"); + + actual.sort(evalUnchecked(naturalOrder())); + + assertThat(actual).isEqualTo(newArrayList("a", "b", "c", "d")); + } + + @Test + void test_naturalOrder_strings_reversed() throws Exception { + final List actual = newArrayList("b", "a", "d", "c"); + + actual.sort(evalUnchecked(CheckedComparator.naturalOrder().reversed())); + + assertThat(actual).isEqualTo(newArrayList("d", "c", "b", "a")); + } + + @Test + void test_naturalOrder_ints() throws Exception { + final List actual = newArrayList(3, 7, 25, -6, 8, 5); + + actual.sort(evalUnchecked(naturalOrder())); + + assertThat(actual).isEqualTo(newArrayList(-6, 3, 5, 7, 8, 25)); + } + + @Test + void test_naturalOrder_ints_reversed() throws Exception { + final List actual = newArrayList(3, 7, 25, -6, 8, 5); + + actual.sort(evalUnchecked(CheckedComparator.naturalOrder().reversed())); + + assertThat(actual).isEqualTo(newArrayList(25, 8, 7, 5, 3, -6)); + } + + @Test + void test_naturalOrder_ints_npe() throws Exception { + final List actual = newArrayList(3, 7, 25, null, -6, 8, 5); + + assertThrows(NullPointerException.class, () -> actual.sort(evalUnchecked(naturalOrder()))); + } + +} diff --git a/src/test/java/software/leonov/common/util/function/NullsTest.java b/src/test/java/software/leonov/common/util/function/NullsTest.java new file mode 100644 index 0000000..07cb5c9 --- /dev/null +++ b/src/test/java/software/leonov/common/util/function/NullsTest.java @@ -0,0 +1,90 @@ +package software.leonov.common.util.function; + +import static com.google.common.collect.Lists.newArrayList; +import static com.google.common.truth.Truth.assertThat; +import static software.leonov.common.util.function.CheckedComparator.evalUnchecked; +import static software.leonov.common.util.function.CheckedComparator.naturalOrder; +import static software.leonov.common.util.function.CheckedComparator.nullsFirst; +import static software.leonov.common.util.function.CheckedComparator.nullsLast; + +import java.util.List; + +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class NullsTest { + + @BeforeAll + static void setUpBeforeClass() throws Exception { + } + + @AfterAll + static void tearDownAfterClass() throws Exception { + } + + @BeforeEach + void setUp() throws Exception { + } + + @AfterEach + void tearDown() throws Exception { + } + + @Test + void test_nullsFirst_naturalOrder() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsFirst(naturalOrder()))); + + assertThat(actual).isEqualTo(newArrayList(null, null, "a", "b", "c", "d")); + } + + @Test + void test_nullsFirst_naturalOrder_reversed() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsFirst(CheckedComparator.naturalOrder()).reversed())); + + assertThat(actual).isEqualTo(newArrayList("d", "c", "b", "a", null, null)); + } + + @Test + void test_nullsLast_naturalOrder() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsLast(naturalOrder()))); + + assertThat(actual).isEqualTo(newArrayList("a", "b", "c", "d", null, null)); + } + + @Test + void test_nullsLast_naturalOrder_reversed() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsLast(CheckedComparator.naturalOrder()).reversed())); + + assertThat(actual).isEqualTo(newArrayList(null, null, "d", "c", "b", "a")); + } + + @Test + void test_nullsFirst_null() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsFirst(null))); + + assertThat(actual).isEqualTo(newArrayList(null, null, "b", "a", "d", "c")); + } + + @Test + void test_nullsFirst_last() throws Exception { + final List actual = newArrayList("b", "a", null, "d", "c", null); + + actual.sort(evalUnchecked(nullsLast(null))); + + assertThat(actual).isEqualTo(newArrayList("b", "a", "d", "c", null, null)); + } + +} diff --git a/src/test/java/software/leonov/common/util/function/Test.java b/src/test/java/software/leonov/common/util/function/Test.java deleted file mode 100644 index f1811be..0000000 --- a/src/test/java/software/leonov/common/util/function/Test.java +++ /dev/null @@ -1,27 +0,0 @@ -package software.leonov.common.util.function; - -import static software.leonov.common.util.function.CheckedFunction.evalUnchecked; - -import java.io.IOException; -import java.net.MalformedURLException; -import java.net.URL; -import java.util.stream.Stream; - -public class Test { - - public static void main(String[] args) throws IOException { - - - Stream.of("https://www.google.com").map(t -> { - try { - return new URL(t); - } catch (MalformedURLException e) { - throw new RuntimeException(e); - } - }); - - Stream.of("https://www.google.com").map(evalUnchecked(URL::new)); - - } - -}