You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TestRng.java 1.2KB

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