Skip to content

Commit

Permalink
Simplify TopKSelectorTest.testExceedMaxIteration()
Browse files Browse the repository at this point in the history
Fixes #5698

PiperOrigin-RevId: 394579757
  • Loading branch information
perceptron8 authored and Google Java Core Libraries committed Sep 3, 2021
1 parent 54c174a commit f28b8db
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 60 deletions.
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));
}
}

0 comments on commit f28b8db

Please sign in to comment.