|
| 1 | +package com.baeldung.algorithms.topkelements; |
| 2 | + |
| 3 | +import org.junit.Test; |
| 4 | + |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import static org.assertj.core.api.Java6Assertions.assertThat; |
| 9 | + |
| 10 | +public class TopKElementsFinderUnitTest { |
| 11 | + private final TopKElementsFinder<Integer> bruteForceFinder = new BruteForceTopKElementsFinder(); |
| 12 | + private final TopKElementsFinder<Integer> maxHeapFinder = new MaxHeapTopKElementsFinder(); |
| 13 | + private final TopKElementsFinder<Integer> treeSetFinder = new TreeSetTopKElementsFinder(); |
| 14 | + |
| 15 | + private final int k = 4; |
| 16 | + private final List<Integer> distinctIntegers = Arrays.asList(1, 2, 3, 9, 7, 6, 12); |
| 17 | + private final List<Integer> distinctIntegersTopK = Arrays.asList(9, 7, 6, 12); |
| 18 | + private final List<Integer> nonDistinctIntegers = Arrays.asList(1, 2, 3, 3, 9, 9, 7, 6, 12); |
| 19 | + private final List<Integer> nonDistinctIntegersTopK = Arrays.asList(9, 9, 7, 12); |
| 20 | + |
| 21 | + |
| 22 | + @Test |
| 23 | + public void givenArrayDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() { |
| 24 | + assertThat(bruteForceFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK); |
| 25 | + } |
| 26 | + |
| 27 | + @Test |
| 28 | + public void givenArrayDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() { |
| 29 | + assertThat(maxHeapFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void givenArrayDistinctIntegers_whenTreeSetFindTopK_thenReturnKLargest() { |
| 34 | + assertThat(treeSetFinder.findTopK(distinctIntegers, k)).containsOnlyElementsOf(distinctIntegersTopK); |
| 35 | + } |
| 36 | + |
| 37 | + @Test |
| 38 | + public void givenArrayNonDistinctIntegers_whenBruteForceFindTopK_thenReturnKLargest() { |
| 39 | + assertThat(bruteForceFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK); |
| 40 | + } |
| 41 | + |
| 42 | + @Test |
| 43 | + public void givenArrayNonDistinctIntegers_whenMaxHeapFindTopK_thenReturnKLargest() { |
| 44 | + assertThat(maxHeapFinder.findTopK(nonDistinctIntegers, k)).containsOnlyElementsOf(nonDistinctIntegersTopK); |
| 45 | + } |
| 46 | +} |
0 commit comments