OSDN Git Service

PR java/16789:
[pf3gnuchains/gcc-fork.git] / libjava / testsuite / libjava.lang / StringBuffer_overflow.java
1 /* This tests some corner cases of arithmetic in StringBuffer.  */
2
3 /* These tests can all be run on a 32 bit machine with modest amounts
4  * of memory.  */
5
6 /* The symptom of the problem is that ArrayIndexOutOfBoundsException
7  * gets thrown, while the documentation says that
8  * StringIndexOutOfBoundsException should be thrown.  */
9
10 class StringBuffer_overflow
11 {
12   /* Test correct exception on getChars.  */
13   static void getChars()
14   {
15     StringBuffer b = new StringBuffer ("x");
16     char[] s = new char [1];
17     try
18       {
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");
23       }
24     catch (Throwable e)
25       {
26         ExpectStringIndex ("getChars()", e);
27       }
28   }
29
30   /* Test correct exception on append with bogus count. */
31   static void append()
32   {
33     StringBuffer s = new StringBuffer("a");
34     try
35       {
36         s.append ("".toCharArray(), 1, (1<<31)-1);
37         Fail ("append", "no exception");
38       }
39     catch (Throwable e)
40       {
41         ExpectStringIndex ("append", e);
42       }
43   }
44
45   // Check that append still more or less works.
46   static void appendbasic()
47   {
48     StringBuffer s = new StringBuffer();
49
50     try
51       {
52         if (!new StringBuffer().append ("abcdefg".toCharArray())
53             .toString().equals ("abcdefg"))
54           {
55             Fail ("appendbasic", "append gives incorrect result");
56           }
57       }
58     catch (Throwable e)
59       {
60         Fail ("appendbasic", e);
61       }
62   }
63
64   /* Test correct expception on substring with bogus indexes.  */
65   static void substring()
66   {
67     StringBuffer s = new StringBuffer ("abc");
68     try
69       {
70         // end - begin == -2 - ((1<<31)-1) == (1<<31) - 1 > 0.  */
71         s.substring ((1<<31)-1, -2);
72         Fail ("substring", "no exception");
73       }
74     catch (Throwable e)
75       {
76         ExpectStringIndex ("substring", e);
77       }
78   }
79
80   static void insert()
81   {
82     StringBuffer s = new StringBuffer ("");
83     try
84       {
85         s.insert (0, "abcd".toCharArray(), (1<<31)-1, 1);
86         Fail ("insert", "no exception");
87       }
88     catch (Throwable e)
89       {
90         ExpectStringIndex ("insert", e);
91       }
92   }
93
94
95   public static void main (String[] unused)
96   {
97     getChars();
98     append();
99     appendbasic();
100     substring();
101     insert();
102
103     if (tests_failed == 0)
104       {
105         System.out.println ("ok");
106       }
107   }
108
109   static int tests_failed = 0;
110
111   static void ExpectStringIndex (String name, Throwable exception)
112   {
113     if (! (exception instanceof StringIndexOutOfBoundsException))
114       {
115         Fail (name, exception);
116       }
117   }
118   static void Fail (String name, Object why)
119   {
120     ++tests_failed;
121
122     System.err.print (name);
123     System.err.print ('\t');
124     System.err.println (why);
125   }
126 }