test/java/lang/String/SubSequence.java

Print this page
rev 6546 : 7197183: Alternate implementation of String.subSequence which uses shared backing array.
Reviewed-by: duke

@@ -20,28 +20,87 @@
  * or visit www.oracle.com if you need additional information or have any
  * questions.
  */
 
 /**
- * @test
- * @summary Basic isEmpty functionality
- * @bug 6189137
+ * @test @summary Basic subSequence functionality.
+ * @bug 7197183
  */
+public class SubSequence {
 
-public class IsEmpty {
-    private static String [] tests = { "", " ", "a",
-                                       "It was the best of times, it was the worst of times, it was the age of wisdom, it was the age of foolishness, it was the epoch of belief, it was the epoch of incredulity, it was the season of Light, it was the season of Darkness, it was the spring of hope, it was the winter of despair, we had everything before us, we had nothing before us, we were all going direct to Heaven, we were all going direct the other way- in short, the period was so far like the present period, that some of its noisiest authorities insisted on its being received, for good or for evil, in the superlative degree of comparison only.  -- Charles Dickens, Tale of Two Cities"
-    };
-
-    public static void main(String [] args) {
-        for (int i = 0; i < tests.length; i++) {
-            String s = tests[i];
-            int len = s.length();
-            boolean empty = s.isEmpty();
-
-            if ((len != 0 && empty) || (len == 0 && !empty))
-                throw new RuntimeException("String \"" + s + "\": "
-                                           + " isEmpty = " + empty
-                                           + ", length = " + len);
+    private final static String[] TESTS = {"", "0", "0123456789"};
+
+    public static void test(String testString) {
+        // empty subsequences
+        CharSequence subSequence = testString.subSequence(0, 0);
+        if (subSequence.length() != 0) {
+            throw new RuntimeException("not empty");
+        }
+        if (0 != subSequence.hashCode()) {
+            throw new RuntimeException("bad hashCode");
+        }
+
+        // single char sub-sequences
+        for (int each = 0; each < testString.length(); each++) {
+            subSequence = testString.subSequence(each, each + 1);
+            String subString = testString.substring(each, each + 1);
+
+            if (subSequence.length() != subString.length()) {
+                throw new RuntimeException("wrong length");
+            }
+
+            if (subSequence.charAt(0) != testString.charAt(each)) {
+                throw new RuntimeException("wrong char" + subSequence.charAt(0) + "!=" + testString.charAt(each));
+            }
+
+            if (subString.hashCode() != subSequence.hashCode()) {
+                throw new RuntimeException("bad hashCode " + subString.hashCode() + " != " + subSequence.hashCode());
+            }
+            if (!subSequence.equals(subString)) {
+                throw new RuntimeException("not equal");
+            }
+            if (!subString.equals(subSequence)) {
+                throw new RuntimeException("not equal");
+            }
+            if (!subString.equals(subSequence.toString())) {
+                throw new RuntimeException("not equal");
+            }
+        }
+
+        subSequence = testString.subSequence(testString.length(), testString.length());
+        if (subSequence.length() != 0) {
+            throw new RuntimeException("not empty");
+        }
+        if (0 != subSequence.hashCode()) {
+            throw new RuntimeException("bad hashCode");
+        }
+
+        // full length sub-sequence
+        subSequence = testString.subSequence(0, testString.length());
+        if (testString.length() != subSequence.length()) {
+            throw new RuntimeException("wrong length");
+        }
+        if (testString.hashCode() != subSequence.hashCode()) {
+            throw new RuntimeException("bad hashCode");
+        }
+        if (!subSequence.equals(testString)) {
+            throw new RuntimeException("not equal");
+        }
+        if (!testString.equals(subSequence)) {
+            throw new RuntimeException("not equal");
+        }
+        if (!testString.equals(subSequence.toString())) {
+            throw new RuntimeException("not equal");
+        }
+    }
+
+    public static void main(String[] args) {
+        for (String each : TESTS) {
+            try {
+                System.out.println("Test : \"" + each + "\"");
+                test(each);
+            } catch (Throwable all) {
+                throw new AssertionError("Failed testing : \"" + each + "\"", all);
+            }
         }
     }
 }