You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2018/02/25 00:44:54 UTC
commons-rng git commit: Additional unit test.
Repository: commons-rng
Updated Branches:
refs/heads/master 9d3bb1904 -> 1b0e32ec0
Additional unit test.
New "support" method for unit tests.
Project: http://git-wip-us.apache.org/repos/asf/commons-rng/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-rng/commit/1b0e32ec
Tree: http://git-wip-us.apache.org/repos/asf/commons-rng/tree/1b0e32ec
Diff: http://git-wip-us.apache.org/repos/asf/commons-rng/diff/1b0e32ec
Branch: refs/heads/master
Commit: 1b0e32ec0ead1816f9d15eccc6cc5ef3538fba44
Parents: 9d3bb19
Author: Gilles <er...@apache.org>
Authored: Sun Feb 25 01:42:42 2018 +0100
Committer: Gilles <er...@apache.org>
Committed: Sun Feb 25 01:42:42 2018 +0100
----------------------------------------------------------------------
.../commons/rng/sampling/ListSamplerTest.java | 55 ++++++++++++--------
1 file changed, 34 insertions(+), 21 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/commons-rng/blob/1b0e32ec/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
----------------------------------------------------------------------
diff --git a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
index 5799357..bc1d1d1 100644
--- a/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
+++ b/commons-rng-sampling/src/test/java/org/apache/commons/rng/sampling/ListSamplerTest.java
@@ -95,6 +95,19 @@ public class ListSamplerTest {
}
@Test
+ public void testShuffle() {
+ final List<Integer> orig = new ArrayList<Integer>();
+ for (int i = 0; i < 10; i++) {
+ orig.add((i + 1) * rng.nextInt());
+ }
+ final List<Integer> list = new ArrayList<Integer>(orig);
+
+ ListSampler.shuffle(rng, list);
+ // Ensure that at least one entry has moved.
+ Assert.assertTrue(compare(orig, list, 0, orig.size(), false));
+ }
+
+ @Test
public void testShuffleTail() {
final List<Integer> orig = new ArrayList<Integer>();
for (int i = 0; i < 10; i++) {
@@ -106,19 +119,10 @@ public class ListSamplerTest {
ListSampler.shuffle(rng, list, start, false);
// Ensure that all entries below index "start" did not move.
- for (int i = 0; i < start; i++) {
- Assert.assertEquals(orig.get(i), list.get(i));
- }
+ Assert.assertTrue(compare(orig, list, 0, start, true));
// Ensure that at least one entry has moved.
- boolean ok = false;
- for (int i = start; i < orig.size() - 1; i++) {
- if (!orig.get(i).equals(list.get(i))) {
- ok = true;
- break;
- }
- }
- Assert.assertTrue(ok);
+ Assert.assertTrue(compare(orig, list, start, orig.size(), false));
}
@Test
@@ -133,23 +137,32 @@ public class ListSamplerTest {
ListSampler.shuffle(rng, list, start, true);
// Ensure that all entries above index "start" did not move.
- for (int i = start + 1; i < orig.size(); i++) {
- Assert.assertEquals(orig.get(i), list.get(i));
- }
+ Assert.assertTrue(compare(orig, list, start + 1, orig.size(), true));
// Ensure that at least one entry has moved.
- boolean ok = false;
- for (int i = 0; i <= start; i++) {
+ Assert.assertTrue(compare(orig, list, 0, start + 1, false));
+ }
+
+ //// Support methods.
+
+ /**
+ * If {@code same == true}, return {@code true} if all entries are
+ * the same; if {@code same == false}, return {@code true} if at
+ * least one entry is different.
+ */
+ private <T> boolean compare(List<T> orig,
+ List<T> list,
+ int start,
+ int end,
+ boolean same) {
+ for (int i = start; i < end; i++) {
if (!orig.get(i).equals(list.get(i))) {
- ok = true;
- break;
+ return same ? false : true;
}
}
- Assert.assertTrue(ok);
+ return same ? true : false;
}
- //// Support methods.
-
private <T extends Set<String>> int findSample(List<T> u,
Collection<String> sampList) {
final String[] samp = sampList.toArray(new String[sampList.size()]);