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: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Shawn O. Pearce 2010-01-05 19:14:48 -08:00
parent 15e2b45a81
commit f88cac039e
2 changed files with 20 additions and 2 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (C) 2008, Google Inc.
* Copyright (C) 2008-2010, Google Inc.
* and other copyright owners as documented in the project's IP log.
*
* This program and the accompanying materials are made available
@ -41,18 +41,31 @@
* 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. */
public class TestRng {
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) {
next = 0;
for (int i = 0; i < seed.length(); 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) {
final byte[] r = new byte[cnt];
for (int i = 0; i < cnt; i++)
@ -60,6 +73,9 @@ public byte[] nextBytes(final int cnt) {
return r;
}
/**
* @return the next random integer.
*/
public int nextInt() {
next = next * 1103515245 + 12345;
return next;

View File

@ -48,6 +48,8 @@
import java.io.IOException;
import java.util.Arrays;
import org.eclipse.jgit.junit.TestRng;
import junit.framework.TestCase;
public class TemporaryBufferTest extends TestCase {