blob: bae4d4f3aa91d7ac65148961d6b8f0fed94b8057 [file] [log] [blame]
jeffhao5d1ac922011-09-29 17:41:15 -07001/*
2 * Copyright (C) 2007 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
jeffhao5d1ac922011-09-29 17:41:15 -070017import junit.framework.Assert;
18
19public class Main {
Elliott Hughes28c384b2012-06-15 16:46:25 -070020 public static void main(String args[]) {
21 test_Double_doubleToRawLongBits();
22 test_Double_longBitsToDouble();
23 test_Float_floatToRawIntBits();
24 test_Float_intBitsToFloat();
25 test_Math_abs_I();
26 test_Math_abs_J();
27 test_Math_min();
28 test_Math_max();
29 test_String_charAt();
30 test_String_compareTo();
31 test_String_indexOf();
32 test_String_isEmpty();
33 test_String_length();
34 }
35
36 public static void test_String_length() {
37 String str0 = "";
38 String str1 = "x";
39 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
40
41 Assert.assertEquals(str0.length(), 0);
42 Assert.assertEquals(str1.length(), 1);
43 Assert.assertEquals(str80.length(), 80);
44
45 String strNull = null;
46 try {
47 strNull.length();
48 Assert.fail();
49 } catch (NullPointerException expected) {
50 }
51 }
52
53 public static void test_String_isEmpty() {
54 String str0 = "";
55 String str1 = "x";
56
57 Assert.assertTrue(str0.isEmpty());
58 Assert.assertFalse(str1.isEmpty());
59
60 String strNull = null;
61 try {
62 strNull.isEmpty();
63 Assert.fail();
64 } catch (NullPointerException expected) {
65 }
66 }
67
68 public static void test_String_charAt() {
69 String testStr = "Now is the time";
70
71 Assert.assertEquals('N', testStr.charAt(0));
72 Assert.assertEquals('o', testStr.charAt(1));
73 Assert.assertEquals(' ', testStr.charAt(10));
74 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
75
76 try {
77 testStr.charAt(-1);
78 Assert.fail();
79 } catch (StringIndexOutOfBoundsException expected) {
80 }
81 try {
82 testStr.charAt(80);
83 Assert.fail();
84 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -070085 }
86
Elliott Hughes28c384b2012-06-15 16:46:25 -070087 String strNull = null;
88 try {
89 strNull.charAt(0);
90 Assert.fail();
91 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -070092 }
Elliott Hughes28c384b2012-06-15 16:46:25 -070093 }
jeffhao5d1ac922011-09-29 17:41:15 -070094
Elliott Hughes28c384b2012-06-15 16:46:25 -070095 public static void test_String_indexOf() {
96 String str0 = "";
97 String str3 = "abc";
98 String str10 = "abcdefghij";
99 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700100
Elliott Hughes28c384b2012-06-15 16:46:25 -0700101 int supplementaryChar = 0x20b9f;
102 String surrogatePair = "\ud842\udf9f";
103 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700104
Elliott Hughes28c384b2012-06-15 16:46:25 -0700105 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
106 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
107 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
108 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700109
Elliott Hughes28c384b2012-06-15 16:46:25 -0700110 Assert.assertEquals(str0.indexOf('a'), -1);
111 Assert.assertEquals(str3.indexOf('a'), 0);
112 Assert.assertEquals(str3.indexOf('b'), 1);
113 Assert.assertEquals(str3.indexOf('c'), 2);
114 Assert.assertEquals(str10.indexOf('j'), 9);
115 Assert.assertEquals(str40.indexOf('a'), 0);
116 Assert.assertEquals(str40.indexOf('b'), 38);
117 Assert.assertEquals(str40.indexOf('c'), 39);
118 Assert.assertEquals(str0.indexOf('a',20), -1);
119 Assert.assertEquals(str0.indexOf('a',0), -1);
120 Assert.assertEquals(str0.indexOf('a',-1), -1);
121 Assert.assertEquals(str3.indexOf('a',0), 0);
122 Assert.assertEquals(str3.indexOf('a',1), -1);
123 Assert.assertEquals(str3.indexOf('a',1234), -1);
124 Assert.assertEquals(str3.indexOf('b',0), 1);
125 Assert.assertEquals(str3.indexOf('b',1), 1);
126 Assert.assertEquals(str3.indexOf('c',2), 2);
127 Assert.assertEquals(str10.indexOf('j',5), 9);
128 Assert.assertEquals(str10.indexOf('j',9), 9);
129 Assert.assertEquals(str40.indexOf('a',10), 10);
130 Assert.assertEquals(str40.indexOf('b',40), -1);
131
132 String strNull = null;
133 try {
134 strNull.indexOf('a');
135 Assert.fail();
136 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700137 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700138 try {
139 strNull.indexOf('a', 0);
140 Assert.fail();
141 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700142 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700143 try {
144 strNull.indexOf('a', -1);
145 Assert.fail();
146 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700147 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700148 }
jeffhao5d1ac922011-09-29 17:41:15 -0700149
Elliott Hughes28c384b2012-06-15 16:46:25 -0700150 public static void test_String_compareTo() {
151 String test = "0123456789";
152 String test1 = new String("0123456789"); // different object
153 String test2 = new String("0123456780"); // different value
154 String offset = new String("xxx0123456789yyy");
155 String sub = offset.substring(3, 13);
156 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
157 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
158 String lc = "abcdefg";
159 String uc = "ABCDEFG";
160 Object blah = new Object();
161
162 Assert.assertTrue(lc.toUpperCase().equals(uc));
163
164 Assert.assertEquals(str32.compareTo(str33), -1);
165 Assert.assertEquals(str33.compareTo(str32), 1);
166
167 Assert.assertTrue(test.equals(test));
168 Assert.assertTrue(test.equals(test1));
169 Assert.assertFalse(test.equals(test2));
170
171 Assert.assertEquals(test.compareTo(test1), 0);
172 Assert.assertTrue(test1.compareTo(test2) > 0);
173 Assert.assertTrue(test2.compareTo(test1) < 0);
174
175 // Compare string with a nonzero offset, in left/right side.
176 Assert.assertEquals(test.compareTo(sub), 0);
177 Assert.assertEquals(sub.compareTo(test), 0);
178 Assert.assertTrue(test.equals(sub));
179 Assert.assertTrue(sub.equals(test));
180 // Same base, one is a substring.
181 Assert.assertFalse(offset.equals(sub));
182 Assert.assertFalse(sub.equals(offset));
183 // Wrong class.
184 Assert.assertFalse(test.equals(blah));
185
186 // Null lhs - throw.
187 try {
188 test.compareTo(null);
189 Assert.fail("didn't get expected npe");
190 } catch (NullPointerException npe) {
191 }
192 // Null rhs - okay.
193 Assert.assertFalse(test.equals(null));
194
195 test = test.substring(1);
196 Assert.assertTrue(test.equals("123456789"));
197 Assert.assertFalse(test.equals(test1));
198
199 test = test.substring(1);
200 Assert.assertTrue(test.equals("23456789"));
201
202 test = test.substring(1);
203 Assert.assertTrue(test.equals("3456789"));
204
205 test = test.substring(1);
206 Assert.assertTrue(test.equals("456789"));
207
208 test = test.substring(3,5);
209 Assert.assertTrue(test.equals("78"));
210
211 test = "this/is/a/path";
212 String[] strings = test.split("/");
213 Assert.assertEquals(4, strings.length);
214
215 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
216 Assert.assertEquals("this is a path", test.replace("/", " "));
217 }
218
219 public static void test_Math_abs_I() {
220 Assert.assertEquals(Math.abs(0), 0);
221 Assert.assertEquals(Math.abs(123), 123);
222 Assert.assertEquals(Math.abs(-123), 123);
223 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
224 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
225 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
226 }
227
228 public static void test_Math_abs_J() {
229 Assert.assertEquals(Math.abs(0L), 0L);
230 Assert.assertEquals(Math.abs(123L), 123L);
231 Assert.assertEquals(Math.abs(-123L), 123L);
232 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
233 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
234 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
235 }
236
237 public static void test_Math_min() {
238 Assert.assertEquals(Math.min(0, 0), 0);
239 Assert.assertEquals(Math.min(1, 0), 0);
240 Assert.assertEquals(Math.min(0, 1), 0);
241 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
242 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
243 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
244 }
245
246 public static void test_Math_max() {
247 Assert.assertEquals(Math.max(0, 0), 0);
248 Assert.assertEquals(Math.max(1, 0), 1);
249 Assert.assertEquals(Math.max(0, 1), 1);
250 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
251 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
252 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
253 }
254
255 public static void test_Float_floatToRawIntBits() {
256 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
257 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
258 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
259 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
260 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
261 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
262 }
263
264 public static void test_Float_intBitsToFloat() {
265 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
266 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
267 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
268 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
269 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
270 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
271 }
272
273 public static void test_Double_doubleToRawLongBits() {
274 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
275 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
276 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
277 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
278 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
279 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
280 }
281
282 public static void test_Double_longBitsToDouble() {
283 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
284 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
285 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
286 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
287 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
288 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
289 }
jeffhao5d1ac922011-09-29 17:41:15 -0700290}