PageRenderTime 54ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/java-1.7.0-openjdk/openjdk/jdk/test/java/io/charStreams/StringConvert.java

#
Java | 150 lines | 95 code | 14 blank | 41 comment | 18 complexity | c73434e1fba9504c360b74ed87cab5a6 MD5 | raw file
Possible License(s): GPL-2.0, BSD-3-Clause-No-Nuclear-License-2014, LGPL-3.0, LGPL-2.0
  1. /*
  2. * Copyright (c) 1997, 2000, Oracle and/or its affiliates. All rights reserved.
  3. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4. *
  5. * This code is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 only, as
  7. * published by the Free Software Foundation.
  8. *
  9. * This code is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  12. * version 2 for more details (a copy is included in the LICENSE file that
  13. * accompanied this code).
  14. *
  15. * You should have received a copy of the GNU General Public License version
  16. * 2 along with this work; if not, write to the Free Software Foundation,
  17. * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. *
  19. * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20. * or visit www.oracle.com if you need additional information or have any
  21. * questions.
  22. */
  23. /* @test
  24. @summary General tests of String constructors and methods that convert
  25. between character encodings. This should really be in
  26. java/lang/String, but it shares code with the I/O charStream
  27. tests.
  28. */
  29. import java.io.*;
  30. public class StringConvert {
  31. static String enc = "UTF8";
  32. static int limit = 500;
  33. static int max = 0xffff;
  34. static void fail(String s) {
  35. throw new RuntimeException(s);
  36. }
  37. public static void main(String[] args) throws Exception {
  38. PrintStream log = System.err;
  39. IntGenerator ig = new IntGenerator();
  40. CharGenerator cg;
  41. StringGenerator sg;
  42. String s;
  43. int i = 0;
  44. /* String(byte[] bytes, String enc)
  45. getBytes(String enc)
  46. */
  47. log.println("-- String(byte[], String), getBytes(String)");
  48. i = 0;
  49. cg = new CharGenerator(ig, 0, max);
  50. sg = new StringGenerator(ig, cg, limit);
  51. while ((s = sg.next()) != null) {
  52. byte[] b = s.getBytes(enc);
  53. String t = new String(b, enc);
  54. if (!s.equals(t)) {
  55. int n = Math.min(s.length(), t.length());
  56. for (int j = 0; j < n; j++) {
  57. if (s.charAt(j) != t.charAt(j)) {
  58. log.println("Mismatch: " + j + " "
  59. + Integer.toHexString(s.charAt(j))
  60. + " != "
  61. + Integer.toHexString(t.charAt(j)));
  62. }
  63. }
  64. fail("Conversion failure");
  65. }
  66. log.println("[" + i + "] " + s.length());
  67. i++;
  68. }
  69. /* String(byte[] bytes)
  70. getBytes()
  71. */
  72. log.println("-- String(byte[]), getBytes()");
  73. i = 0;
  74. cg = new CharGenerator(ig, 0x20, 0x7e);
  75. sg = new StringGenerator(ig, cg, limit);
  76. while ((s = sg.next()) != null) {
  77. log.println("[" + i + "] \"" + s + "\"");
  78. byte[] b = s.getBytes();
  79. String t = new String(b);
  80. if (! s.equals(t))
  81. fail("Conversion failure");
  82. i++;
  83. }
  84. /* String(byte[] bytes, int offset, int length)
  85. getBytes()
  86. */
  87. log.println("-- String(byte[], int, int), getBytes()");
  88. i = 0;
  89. cg = new CharGenerator(ig, 0x20, 0x7e);
  90. sg = new StringGenerator(ig, cg, limit);
  91. while ((s = sg.next()) != null) {
  92. log.println("[" + i + "] \"" + s + "\"");
  93. byte[] b = s.getBytes();
  94. int o = ig.next(s.length() - 1);
  95. int n = ig.next(s.length() - o);
  96. String t = new String(b, o, n);
  97. if (! s.substring(o, o + n).equals(t))
  98. fail("Conversion failure");
  99. i++;
  100. }
  101. /* String(byte[] bytes, int offset, int length, String enc)
  102. getBytes(String enc)
  103. */
  104. log.println("-- String(byte[], int, int, String), getBytes(String)");
  105. i = 0;
  106. cg = new CharGenerator(ig);
  107. sg = new StringGenerator(ig, cg, limit);
  108. while ((s = sg.next()) != null) {
  109. log.println("[" + i + "] " + s.length());
  110. byte[] b = s.getBytes(enc);
  111. int o = ig.next(100);
  112. byte[] b2 = new byte[b.length + o];
  113. System.arraycopy(b, 0, b2, o, b.length);
  114. String t = new String(b2, o, b.length, enc);
  115. if (! s.equals(t))
  116. fail("Conversion failure");
  117. i++;
  118. }
  119. /* Substrings */
  120. log.println("-- Substrings");
  121. i = 0;
  122. cg = new CharGenerator(ig, 0x20, 0x7e);
  123. sg = new StringGenerator(ig, cg, limit);
  124. while ((s = sg.next()) != null) {
  125. log.println("[" + i + "] \"" + s + "\"");
  126. int o = ig.next(s.length() - 1);
  127. int n = ig.next(s.length() - o);
  128. String s2 = s.substring(o, o + n);
  129. byte[] b = s2.getBytes();
  130. String t = new String(b);
  131. if (! s2.equals(t))
  132. fail("Conversion failure");
  133. i++;
  134. }
  135. }
  136. }