Skip to content
Snippets Groups Projects
Commit f88cac03 authored by Shawn Pearce's avatar Shawn Pearce
Browse files

Move TestRng to our JUnit helper package


Other test suites may find this useful, especially when trying
to defeat the pack file compression with random data files.

Change-Id: Ic00a4ac626af7a1c94d18ee99305e295b267b1a3
Signed-off-by: default avatarShawn O. Pearce <spearce@spearce.org>
parent 15e2b45a
No related branches found
No related tags found
No related merge requests found
/* /*
* Copyright (C) 2008, Google Inc. * Copyright (C) 2008-2010, Google Inc.
* and other copyright owners as documented in the project's IP log. * and other copyright owners as documented in the project's IP log.
* *
* This program and the accompanying materials are made available * This program and the accompanying materials are made available
...@@ -41,18 +41,31 @@ ...@@ -41,18 +41,31 @@
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/ */
package org.eclipse.jgit.util; package org.eclipse.jgit.junit;
/** Toy RNG to ensure we get predictable numbers during unit tests. */ /** Toy RNG to ensure we get predictable numbers during unit tests. */
public class TestRng { public class TestRng {
private int next; private int next;
/**
* Create a new random number generator, seeded by a string.
*
* @param seed
* seed to bootstrap, usually this is the test method name.
*/
public TestRng(final String seed) { public TestRng(final String seed) {
next = 0; next = 0;
for (int i = 0; i < seed.length(); i++) for (int i = 0; i < seed.length(); i++)
next = next * 11 + seed.charAt(i); next = next * 11 + seed.charAt(i);
} }
/**
* Get the next {@code cnt} bytes of random data.
*
* @param cnt
* number of random bytes to produce.
* @return array of {@code cnt} randomly generated bytes.
*/
public byte[] nextBytes(final int cnt) { public byte[] nextBytes(final int cnt) {
final byte[] r = new byte[cnt]; final byte[] r = new byte[cnt];
for (int i = 0; i < cnt; i++) for (int i = 0; i < cnt; i++)
...@@ -60,6 +73,9 @@ public byte[] nextBytes(final int cnt) { ...@@ -60,6 +73,9 @@ public byte[] nextBytes(final int cnt) {
return r; return r;
} }
/**
* @return the next random integer.
*/
public int nextInt() { public int nextInt() {
next = next * 1103515245 + 12345; next = next * 1103515245 + 12345;
return next; return next;
......
...@@ -48,6 +48,8 @@ ...@@ -48,6 +48,8 @@
import java.io.IOException; import java.io.IOException;
import java.util.Arrays; import java.util.Arrays;
import org.eclipse.jgit.junit.TestRng;
import junit.framework.TestCase; import junit.framework.TestCase;
public class TemporaryBufferTest extends TestCase { public class TemporaryBufferTest extends TestCase {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment