Guest User

Untitled

a guest
Jun 8th, 2015
401
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.io.Reader;
  3. import java.io.StringReader;
  4. import java.util.Random;
  5.  
  6. public class ShouldWork {
  7. public static void main(String args[]) throws Exception {
  8. // NOTE: deterministic seed, yet fails a different way each time (compile issue)
  9. Random random = new Random(0L);
  10. for (int i = 0; i < 1000000; i++) {
  11. String expected = randomString(random);
  12. Reader reader = new StringReader(expected);
  13. StringBuilder sb = new StringBuilder();
  14. int ch = 0;
  15. while ((ch = readChar(random, reader)) >= 0) {
  16. sb.append((char) ch);
  17. }
  18. String actual = sb.toString();
  19. if (!expected.equals(actual)) {
  20. throw new AssertionError("expected=" + expected + ", actual=" + actual);
  21. }
  22. }
  23. }
  24.  
  25. // reads a single character with read(char[])
  26. static int readChar(Random random, Reader input) throws IOException {
  27. char c[] = new char[1];
  28. int ret = input.read(c);
  29. assert ret != 0;
  30. return ret < 0 ? ret : c[0];
  31. }
  32.  
  33. static String randomString(Random random) {
  34. int length = random.nextInt(10);
  35. char chars[] = new char[length];
  36. for (int i = 0; i < chars.length; i++) {
  37. chars[i] = (char) (random.nextInt(26) + 'a');
  38. }
  39. return new String(chars);
  40. }
  41. }
RAW Paste Data