|
1 | 1 | package org.baeldung.guava.collections;
|
2 | 2 |
|
| 3 | +import static org.hamcrest.Matchers.equalTo; |
3 | 4 | import static org.hamcrest.Matchers.nullValue;
|
4 | 5 | import static org.junit.Assert.assertFalse;
|
5 | 6 | import static org.junit.Assert.assertThat;
|
|
11 | 12 |
|
12 | 13 | import org.junit.Test;
|
13 | 14 |
|
| 15 | +import com.google.common.base.Functions; |
14 | 16 | import com.google.common.collect.Lists;
|
15 | 17 | import com.google.common.collect.Ordering;
|
16 | 18 | import com.google.common.primitives.Ints;
|
@@ -59,6 +61,16 @@ public final void whenSortingWithNaturalOrdering_thenCorectlySorted() {
|
59 | 61 | assertTrue(Ordering.natural().isOrdered(toSort));
|
60 | 62 | }
|
61 | 63 |
|
| 64 | + // checking string ordering |
| 65 | + |
| 66 | + @Test |
| 67 | + public final void givenCollectionContainsDuplicates_whenCheckingStringOrdering_thenNo() { |
| 68 | + final List<Integer> toSort = Arrays.asList(3, 5, 4, 2, 1, 2); |
| 69 | + Collections.sort(toSort, Ordering.natural()); |
| 70 | + |
| 71 | + assertFalse(Ordering.natural().isStrictlyOrdered(toSort)); |
| 72 | + } |
| 73 | + |
62 | 74 | // custom - by length
|
63 | 75 |
|
64 | 76 | @Test
|
@@ -124,4 +136,44 @@ public final void givenUnorderdList_whenUsingToStringForSortingObjects_thenSorte
|
124 | 136 | assertTrue(expectedOrder.isOrdered(toSort));
|
125 | 137 | }
|
126 | 138 |
|
| 139 | + // binary search |
| 140 | + |
| 141 | + @Test |
| 142 | + public final void whenPerformingBinarySearch_thenFound() { |
| 143 | + final List<Integer> toSort = Arrays.asList(1, 2, 11); |
| 144 | + Collections.sort(toSort, Ordering.usingToString()); |
| 145 | + final int found = Ordering.usingToString().binarySearch(toSort, 2); |
| 146 | + |
| 147 | + System.out.println(found); |
| 148 | + } |
| 149 | + |
| 150 | + // min/max without actually sorting |
| 151 | + |
| 152 | + @Test |
| 153 | + public final void whenFindingTheMinimalElementWithoutSorting_thenFound() { |
| 154 | + final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14); |
| 155 | + final int found = Ordering.natural().min(toSort); |
| 156 | + assertThat(found, equalTo(1)); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + public final void whenFindingTheFirstFewElements_thenCorrect() { |
| 161 | + final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14); |
| 162 | + final List<Integer> leastOf = Ordering.natural().leastOf(toSort, 3); |
| 163 | + final List<Integer> expected = Lists.newArrayList(1, 2, 8); |
| 164 | + assertThat(expected, equalTo(leastOf)); |
| 165 | + } |
| 166 | + |
| 167 | + // order the results of a Function |
| 168 | + |
| 169 | + @Test |
| 170 | + public final void givenListOfNumbers_whenRunningAToStringFunctionThenSorting_thenCorrect() { |
| 171 | + final List<Integer> toSort = Arrays.asList(2, 1, 11, 100, 8, 14); |
| 172 | + final Ordering<Object> ordering = Ordering.natural().onResultOf(Functions.toStringFunction()); |
| 173 | + final List<Integer> sortedCopy = ordering.sortedCopy(toSort); |
| 174 | + |
| 175 | + final List<Integer> expected = Lists.newArrayList(1, 100, 11, 14, 2, 8); |
| 176 | + assertThat(expected, equalTo(sortedCopy)); |
| 177 | + } |
| 178 | + |
127 | 179 | }
|
0 commit comments