2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / testsuite / libjava.lang / StringBuffer_overflow.java
blob68e18da2f961b2ba8b292c3bce852c11032a8ae9
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
4 * of memory. */
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];
17 try
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");
24 catch (Throwable e)
26 ExpectStringIndex ("getChars()", e);
30 /* Test correct exception on append with bogus count. */
31 static void append()
33 StringBuffer s = new StringBuffer("a");
34 try
36 s.append ("".toCharArray(), 1, (1<<31)-1);
37 Fail ("append", "no exception");
39 catch (Throwable e)
41 ExpectStringIndex ("append", e);
45 // Check that append still more or less works.
46 static void appendbasic()
48 StringBuffer s = new StringBuffer();
50 try
52 if (!new StringBuffer().append ("abcdefg".toCharArray())
53 .toString().equals ("abcdefg"))
55 Fail ("appendbasic", "append gives incorrect result");
58 catch (Throwable e)
60 Fail ("appendbasic", e);
64 /* Test correct expception on substring with bogus indexes. */
65 static void substring()
67 StringBuffer s = new StringBuffer ("abc");
68 try
70 // end - begin == -2 - ((1<<31)-1) == (1<<31) - 1 > 0. */
71 s.substring ((1<<31)-1, -2);
72 Fail ("substring", "no exception");
74 catch (Throwable e)
76 ExpectStringIndex ("substring", e);
80 static void insert()
82 StringBuffer s = new StringBuffer ("");
83 try
85 s.insert (0, "abcd".toCharArray(), (1<<31)-1, 1);
86 Fail ("insert", "no exception");
88 catch (Throwable e)
90 ExpectStringIndex ("insert", e);
95 public static void main (String[] unused)
97 getChars();
98 append();
99 appendbasic();
100 substring();
101 insert();
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)
120 ++tests_failed;
122 System.err.print (name);
123 System.err.print ('\t');
124 System.err.println (why);