1f88cac03SShawn O. Pearce /* 2*5c5f7c6bSMatthias Sohn * Copyright (C) 2008-2010, Google Inc. and others 3f88cac03SShawn O. Pearce * 4*5c5f7c6bSMatthias Sohn * This program and the accompanying materials are made available under the 5*5c5f7c6bSMatthias Sohn * terms of the Eclipse Distribution License v. 1.0 which is available at 6*5c5f7c6bSMatthias Sohn * https://www.eclipse.org/org/documents/edl-v10.php. 7f88cac03SShawn O. Pearce * 8*5c5f7c6bSMatthias Sohn * SPDX-License-Identifier: BSD-3-Clause 9f88cac03SShawn O. Pearce */ 10f88cac03SShawn O. Pearce 11f88cac03SShawn O. Pearce package org.eclipse.jgit.junit; 12f88cac03SShawn O. Pearce 13a90b75b4SMatthias Sohn /** 14a90b75b4SMatthias Sohn * Toy RNG to ensure we get predictable numbers during unit tests. 15a90b75b4SMatthias Sohn */ 16f88cac03SShawn O. Pearce public class TestRng { 17f88cac03SShawn O. Pearce private int next; 18f88cac03SShawn O. Pearce 19f88cac03SShawn O. Pearce /** 20f88cac03SShawn O. Pearce * Create a new random number generator, seeded by a string. 21f88cac03SShawn O. Pearce * 22f88cac03SShawn O. Pearce * @param seed 23f88cac03SShawn O. Pearce * seed to bootstrap, usually this is the test method name. 24f88cac03SShawn O. Pearce */ TestRng(String seed)256d370d83SHan-Wen Nienhuys public TestRng(String seed) { 26f88cac03SShawn O. Pearce next = 0; 27f88cac03SShawn O. Pearce for (int i = 0; i < seed.length(); i++) 28f88cac03SShawn O. Pearce next = next * 11 + seed.charAt(i); 29f88cac03SShawn O. Pearce } 30f88cac03SShawn O. Pearce 31f88cac03SShawn O. Pearce /** 32f88cac03SShawn O. Pearce * Get the next {@code cnt} bytes of random data. 33f88cac03SShawn O. Pearce * 34f88cac03SShawn O. Pearce * @param cnt 35f88cac03SShawn O. Pearce * number of random bytes to produce. 36f88cac03SShawn O. Pearce * @return array of {@code cnt} randomly generated bytes. 37f88cac03SShawn O. Pearce */ nextBytes(int cnt)386d370d83SHan-Wen Nienhuys public byte[] nextBytes(int cnt) { 39f88cac03SShawn O. Pearce final byte[] r = new byte[cnt]; 40f88cac03SShawn O. Pearce for (int i = 0; i < cnt; i++) 41f88cac03SShawn O. Pearce r[i] = (byte) nextInt(); 42f88cac03SShawn O. Pearce return r; 43f88cac03SShawn O. Pearce } 44f88cac03SShawn O. Pearce 45f88cac03SShawn O. Pearce /** 46a90b75b4SMatthias Sohn * Next int 47a90b75b4SMatthias Sohn * 48f88cac03SShawn O. Pearce * @return the next random integer. 49f88cac03SShawn O. Pearce */ nextInt()50f88cac03SShawn O. Pearce public int nextInt() { 51f88cac03SShawn O. Pearce next = next * 1103515245 + 12345; 52f88cac03SShawn O. Pearce return next; 53f88cac03SShawn O. Pearce } 54f88cac03SShawn O. Pearce } 55