2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libjava / testsuite / libjava.lang / String_overflow.java
blob5a1a907c50329c4f47222765873b48bb559ad2cf
1 class String_overflow
3 static void getChars()
5 String source = "abcdefg";
6 char[] dest = new char [3];
8 try
10 source.getChars (0, 5, // Source
11 dest, (1<<31) - 1);
12 Fail ("getChars", "Should not have succeeded");
14 catch (Throwable e)
16 ExpectArrayIndex ("getChars", e);
20 /* How do I stop a compiler warning causing a test to fail?
21 static void getBytes()
23 String source = "abcdefg";
24 byte[] dest = new byte[3];
26 try
28 source.getBytes (0, 5, dest, (1<<31) - 1);
29 Fail ("getBytes", "Should not have succeeded");
31 catch (Throwable e)
33 ExpectArrayIndex ("getBytes", e);
38 static void regionMatches()
40 if ("abcdefg".regionMatches (4, "abcdefg", 4, -1))
42 Fail ("regionMatches", "Should not return true");
45 try
47 if ("abcdefg".regionMatches (4, "abcdefg", 4, (1<<31)-1))
49 Fail ("regionMatches (2nd)", "Should not return true");
52 catch (Throwable e)
54 Fail ("regionMatches (2nd)", e);
58 static void regionMatchesCase()
60 if ("abcdefg".regionMatches (true, 4, "abcdefg", 4, -1))
62 Fail ("regionMatchesCase", "Should not return true");
65 try
67 if ("abcdefg".regionMatches (true, 4, "abcdefg", 4, (1<<31)-1))
69 Fail ("regionMatchesCase (2nd)", "Should not return true");
72 catch (Throwable e)
74 Fail ("regionMatchesCase (2nd)", e);
78 static void startsWith()
80 // We make the arg pretty big to try and cause a segfault.
81 String s = new String ("abcdef");
82 StringBuffer b = new StringBuffer (1000000);
83 b.setLength (1000000);
84 String arg = new String (b);
86 try
88 s.startsWith (arg, (1<<31) - 1000000);
90 catch (Throwable e)
92 Fail ("startsWith", e);
96 static void valueOf()
98 char[] array = new char[] {'a', 'b', 'c', 'd', 'e'};
99 try
101 String.valueOf (array, 4, (1<<31)-1);
102 Fail ("valueOf", "should not succeed");
104 catch (Throwable e)
106 ExpectArrayIndex ("valueOf", e);
110 public static void main (String[] args) throws Throwable
112 getChars();
113 // getBytes();
114 regionMatches();
115 regionMatchesCase();
116 startsWith();
117 valueOf();
119 if (tests_failed == 0)
120 System.out.println ("ok");
123 static void ExpectArrayIndex (String test, Throwable e)
125 if (e instanceof ArrayIndexOutOfBoundsException)
126 return;
128 Fail (test, e);
131 static void Fail (String test, Object problem)
133 ++tests_failed;
134 System.err.print (test);
135 System.err.print ('\t');
136 System.err.println (problem);
139 static int tests_failed;