Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify TopKSelectorTest.testExceedMaxIteration() #5704

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
package com.google.common.collect;

import static com.google.common.truth.Truth.assertThat;
import static java.util.Collections.sort;

import com.google.common.annotations.GwtCompatible;
import com.google.common.math.IntMath;
import com.google.common.primitives.Ints;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;

/**
* Tests for {@code TopKSelector}.
*
* @author Louis Wasserman
*/
@GwtCompatible
public class TopKSelectorTest extends TestCase {

public void testNegativeK() {
Expand Down Expand Up @@ -125,33 +124,10 @@ public int compare(Integer o1, Integer o2) {

public void testExceedMaxIteration() {
/*
* Bug #5692 occurred when TopKSelector called Arrays.sort incorrectly. Test data that would
* trigger a problematic call to Arrays.sort is hard to construct by hand, so we searched for
* one among randomly generated inputs. To reach the Arrays.sort call, we need to pass an input
* that requires many iterations of partitioning inside trim(). So, to construct our random
* inputs, we concatenated 10 sorted lists together.
* Bug #5692 occurred when TopKSelector called Arrays.sort incorrectly.
*/

int k = 10000;
Random random = new Random(1629833645599L);

// target list to be sorted using TopKSelector
List<Integer> target = new ArrayList<>();
for (int i = 0; i < 9; i++) {
List<Integer> sortedArray = new ArrayList();
for (int j = 0; j < 10000; j++) {
sortedArray.add(random.nextInt());
}
sort(sortedArray, Ordering.natural());
target.addAll(sortedArray);
}

TopKSelector<Integer> top = TopKSelector.least(k, Ordering.natural());
for (int value : target) {
top.offer(value);
}

sort(target, Ordering.natural());
assertEquals(top.topK(), target.subList(0, k));
TopKSelector<Integer> top = TopKSelector.least(7);
top.offerAll(Ints.asList(5, 7, 6, 2, 4, 3, 1, 0, 0, 0, 0, 0, 0, 0));
assertThat(top.topK()).isEqualTo(Ints.asList(0, 0, 0, 0, 0, 0, 0));
}
}
36 changes: 6 additions & 30 deletions guava-tests/test/com/google/common/collect/TopKSelectorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,22 @@
package com.google.common.collect;

import static com.google.common.truth.Truth.assertThat;
import static java.util.Collections.sort;

import com.google.common.annotations.GwtCompatible;
import com.google.common.math.IntMath;
import com.google.common.primitives.Ints;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Random;
import junit.framework.TestCase;

/**
* Tests for {@code TopKSelector}.
*
* @author Louis Wasserman
*/
@GwtCompatible
public class TopKSelectorTest extends TestCase {

public void testNegativeK() {
Expand Down Expand Up @@ -125,33 +124,10 @@ public int compare(Integer o1, Integer o2) {

public void testExceedMaxIteration() {
/*
* Bug #5692 occurred when TopKSelector called Arrays.sort incorrectly. Test data that would
* trigger a problematic call to Arrays.sort is hard to construct by hand, so we searched for
* one among randomly generated inputs. To reach the Arrays.sort call, we need to pass an input
* that requires many iterations of partitioning inside trim(). So, to construct our random
* inputs, we concatenated 10 sorted lists together.
* Bug #5692 occurred when TopKSelector called Arrays.sort incorrectly.
*/

int k = 10000;
Random random = new Random(1629833645599L);

// target list to be sorted using TopKSelector
List<Integer> target = new ArrayList<>();
for (int i = 0; i < 9; i++) {
List<Integer> sortedArray = new ArrayList();
for (int j = 0; j < 10000; j++) {
sortedArray.add(random.nextInt());
}
sort(sortedArray, Ordering.natural());
target.addAll(sortedArray);
}

TopKSelector<Integer> top = TopKSelector.least(k, Ordering.natural());
for (int value : target) {
top.offer(value);
}

sort(target, Ordering.natural());
assertEquals(top.topK(), target.subList(0, k));
TopKSelector<Integer> top = TopKSelector.least(7);
top.offerAll(Ints.asList(5, 7, 6, 2, 4, 3, 1, 0, 0, 0, 0, 0, 0, 0));
assertThat(top.topK()).isEqualTo(Ints.asList(0, 0, 0, 0, 0, 0, 0));
}
}