1 /* This tests some corner cases of arithmetic in StringBuffer. */
3 /* These tests can all be run on a 32 bit machine with modest amounts
6 /* The symptom of the problem is that ArrayIndexOutOfBoundsException
7 * gets thrown, while the documentation says that
8 * StringIndexOutOfBoundsException should be thrown. */
10 class StringBuffer_overflow
12 /* Test correct exception on getChars. */
13 static void getChars()
15 StringBuffer b
= new StringBuffer ("x");
16 char[] s
= new char [1];
19 // The substring we are attempting to obtain is invalid,
20 // so we should get a StringIndexOutOfBoundsException.
21 b
.getChars (1, -1 << 31, s
, 0);
22 Fail ("getChars", "no exception");
26 ExpectStringIndex ("getChars()", e
);
30 /* Test correct exception on append with bogus count. */
33 StringBuffer s
= new StringBuffer("a");
36 s
.append ("".toCharArray(), 1, (1<<31)-1);
37 Fail ("append", "no exception");
41 ExpectStringIndex ("append", e
);
45 // Check that append still more or less works.
46 static void appendbasic()
48 StringBuffer s
= new StringBuffer();
52 if (!new StringBuffer().append ("abcdefg".toCharArray())
53 .toString().equals ("abcdefg"))
55 Fail ("appendbasic", "append gives incorrect result");
60 Fail ("appendbasic", e
);
64 /* Test correct expception on substring with bogus indexes. */
65 static void substring()
67 StringBuffer s
= new StringBuffer ("abc");
70 // end - begin == -2 - ((1<<31)-1) == (1<<31) - 1 > 0. */
71 s
.substring ((1<<31)-1, -2);
72 Fail ("substring", "no exception");
76 ExpectStringIndex ("substring", e
);
82 StringBuffer s
= new StringBuffer ("");
85 s
.insert (0, "abcd".toCharArray(), (1<<31)-1, 1);
86 Fail ("insert", "no exception");
90 ExpectStringIndex ("insert", e
);
95 public static void main (String
[] unused
)
103 if (tests_failed
== 0)
105 System
.out
.println ("ok");
109 static int tests_failed
= 0;
111 static void ExpectStringIndex (String name
, Throwable exception
)
113 if (! (exception
instanceof StringIndexOutOfBoundsException
))
115 Fail (name
, exception
);
118 static void Fail (String name
, Object why
)
122 System
.err
.print (name
);
123 System
.err
.print ('\t');
124 System
.err
.println (why
);