blob: 86a03abca215ef69c52335c426688961030ce1e1 [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();
Sebastien Hertzbf1442d2013-03-05 15:12:40 +010029 test_StrictMath_abs_I();
30 test_StrictMath_abs_J();
31 test_StrictMath_min();
32 test_StrictMath_max();
Elliott Hughes28c384b2012-06-15 16:46:25 -070033 test_String_charAt();
34 test_String_compareTo();
35 test_String_indexOf();
36 test_String_isEmpty();
37 test_String_length();
38 }
39
40 public static void test_String_length() {
41 String str0 = "";
42 String str1 = "x";
43 String str80 = "01234567890123456789012345678901234567890123456789012345678901234567890123456789";
44
45 Assert.assertEquals(str0.length(), 0);
46 Assert.assertEquals(str1.length(), 1);
47 Assert.assertEquals(str80.length(), 80);
48
49 String strNull = null;
50 try {
51 strNull.length();
52 Assert.fail();
53 } catch (NullPointerException expected) {
54 }
55 }
56
57 public static void test_String_isEmpty() {
58 String str0 = "";
59 String str1 = "x";
60
61 Assert.assertTrue(str0.isEmpty());
62 Assert.assertFalse(str1.isEmpty());
63
64 String strNull = null;
65 try {
66 strNull.isEmpty();
67 Assert.fail();
68 } catch (NullPointerException expected) {
69 }
70 }
71
72 public static void test_String_charAt() {
73 String testStr = "Now is the time";
74
75 Assert.assertEquals('N', testStr.charAt(0));
76 Assert.assertEquals('o', testStr.charAt(1));
77 Assert.assertEquals(' ', testStr.charAt(10));
78 Assert.assertEquals('e', testStr.charAt(testStr.length()-1));
79
80 try {
81 testStr.charAt(-1);
82 Assert.fail();
83 } catch (StringIndexOutOfBoundsException expected) {
84 }
85 try {
86 testStr.charAt(80);
87 Assert.fail();
88 } catch (StringIndexOutOfBoundsException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -070089 }
90
Elliott Hughes28c384b2012-06-15 16:46:25 -070091 String strNull = null;
92 try {
93 strNull.charAt(0);
94 Assert.fail();
95 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -070096 }
Elliott Hughes28c384b2012-06-15 16:46:25 -070097 }
jeffhao5d1ac922011-09-29 17:41:15 -070098
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +070099 static int start;
Elliott Hughes28c384b2012-06-15 16:46:25 -0700100 public static void test_String_indexOf() {
101 String str0 = "";
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700102 String str1 = "/";
Elliott Hughes28c384b2012-06-15 16:46:25 -0700103 String str3 = "abc";
104 String str10 = "abcdefghij";
105 String str40 = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaabc";
jeffhao5d1ac922011-09-29 17:41:15 -0700106
Elliott Hughes28c384b2012-06-15 16:46:25 -0700107 int supplementaryChar = 0x20b9f;
108 String surrogatePair = "\ud842\udf9f";
109 String stringWithSurrogates = "hello " + surrogatePair + " world";
jeffhao5d1ac922011-09-29 17:41:15 -0700110
Elliott Hughes28c384b2012-06-15 16:46:25 -0700111 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar), "hello ".length());
112 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 2), "hello ".length());
113 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 6), 6);
114 Assert.assertEquals(stringWithSurrogates.indexOf(supplementaryChar, 7), -1);
jeffhao5d1ac922011-09-29 17:41:15 -0700115
Elliott Hughes28c384b2012-06-15 16:46:25 -0700116 Assert.assertEquals(str0.indexOf('a'), -1);
117 Assert.assertEquals(str3.indexOf('a'), 0);
118 Assert.assertEquals(str3.indexOf('b'), 1);
119 Assert.assertEquals(str3.indexOf('c'), 2);
120 Assert.assertEquals(str10.indexOf('j'), 9);
121 Assert.assertEquals(str40.indexOf('a'), 0);
122 Assert.assertEquals(str40.indexOf('b'), 38);
123 Assert.assertEquals(str40.indexOf('c'), 39);
124 Assert.assertEquals(str0.indexOf('a',20), -1);
125 Assert.assertEquals(str0.indexOf('a',0), -1);
126 Assert.assertEquals(str0.indexOf('a',-1), -1);
Yevgeny Rouban34fa0d92014-03-13 12:15:58 +0700127 Assert.assertEquals(str1.indexOf('/',++start), -1);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700128 Assert.assertEquals(str3.indexOf('a',0), 0);
129 Assert.assertEquals(str3.indexOf('a',1), -1);
130 Assert.assertEquals(str3.indexOf('a',1234), -1);
131 Assert.assertEquals(str3.indexOf('b',0), 1);
132 Assert.assertEquals(str3.indexOf('b',1), 1);
133 Assert.assertEquals(str3.indexOf('c',2), 2);
134 Assert.assertEquals(str10.indexOf('j',5), 9);
135 Assert.assertEquals(str10.indexOf('j',9), 9);
136 Assert.assertEquals(str40.indexOf('a',10), 10);
137 Assert.assertEquals(str40.indexOf('b',40), -1);
138
139 String strNull = null;
140 try {
141 strNull.indexOf('a');
142 Assert.fail();
143 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700144 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700145 try {
146 strNull.indexOf('a', 0);
147 Assert.fail();
148 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700149 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700150 try {
151 strNull.indexOf('a', -1);
152 Assert.fail();
153 } catch (NullPointerException expected) {
jeffhao5d1ac922011-09-29 17:41:15 -0700154 }
Elliott Hughes28c384b2012-06-15 16:46:25 -0700155 }
jeffhao5d1ac922011-09-29 17:41:15 -0700156
Elliott Hughes28c384b2012-06-15 16:46:25 -0700157 public static void test_String_compareTo() {
158 String test = "0123456789";
159 String test1 = new String("0123456789"); // different object
160 String test2 = new String("0123456780"); // different value
161 String offset = new String("xxx0123456789yyy");
162 String sub = offset.substring(3, 13);
163 String str32 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
164 String str33 = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxy";
165 String lc = "abcdefg";
166 String uc = "ABCDEFG";
167 Object blah = new Object();
168
169 Assert.assertTrue(lc.toUpperCase().equals(uc));
170
171 Assert.assertEquals(str32.compareTo(str33), -1);
172 Assert.assertEquals(str33.compareTo(str32), 1);
173
174 Assert.assertTrue(test.equals(test));
175 Assert.assertTrue(test.equals(test1));
176 Assert.assertFalse(test.equals(test2));
177
178 Assert.assertEquals(test.compareTo(test1), 0);
179 Assert.assertTrue(test1.compareTo(test2) > 0);
180 Assert.assertTrue(test2.compareTo(test1) < 0);
181
182 // Compare string with a nonzero offset, in left/right side.
183 Assert.assertEquals(test.compareTo(sub), 0);
184 Assert.assertEquals(sub.compareTo(test), 0);
185 Assert.assertTrue(test.equals(sub));
186 Assert.assertTrue(sub.equals(test));
187 // Same base, one is a substring.
188 Assert.assertFalse(offset.equals(sub));
189 Assert.assertFalse(sub.equals(offset));
190 // Wrong class.
191 Assert.assertFalse(test.equals(blah));
192
193 // Null lhs - throw.
194 try {
195 test.compareTo(null);
196 Assert.fail("didn't get expected npe");
197 } catch (NullPointerException npe) {
198 }
199 // Null rhs - okay.
200 Assert.assertFalse(test.equals(null));
201
202 test = test.substring(1);
203 Assert.assertTrue(test.equals("123456789"));
204 Assert.assertFalse(test.equals(test1));
205
206 test = test.substring(1);
207 Assert.assertTrue(test.equals("23456789"));
208
209 test = test.substring(1);
210 Assert.assertTrue(test.equals("3456789"));
211
212 test = test.substring(1);
213 Assert.assertTrue(test.equals("456789"));
214
215 test = test.substring(3,5);
216 Assert.assertTrue(test.equals("78"));
217
218 test = "this/is/a/path";
219 String[] strings = test.split("/");
220 Assert.assertEquals(4, strings.length);
221
222 Assert.assertEquals("this is a path", test.replaceAll("/", " "));
223 Assert.assertEquals("this is a path", test.replace("/", " "));
224 }
225
226 public static void test_Math_abs_I() {
227 Assert.assertEquals(Math.abs(0), 0);
228 Assert.assertEquals(Math.abs(123), 123);
229 Assert.assertEquals(Math.abs(-123), 123);
230 Assert.assertEquals(Math.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
231 Assert.assertEquals(Math.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
232 Assert.assertEquals(Math.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100233 Assert.assertEquals(Math.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
Elliott Hughes28c384b2012-06-15 16:46:25 -0700234 }
235
236 public static void test_Math_abs_J() {
237 Assert.assertEquals(Math.abs(0L), 0L);
238 Assert.assertEquals(Math.abs(123L), 123L);
239 Assert.assertEquals(Math.abs(-123L), 123L);
240 Assert.assertEquals(Math.abs(Long.MAX_VALUE), Long.MAX_VALUE);
241 Assert.assertEquals(Math.abs(Long.MIN_VALUE), Long.MIN_VALUE);
242 Assert.assertEquals(Math.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
243 }
244
245 public static void test_Math_min() {
246 Assert.assertEquals(Math.min(0, 0), 0);
247 Assert.assertEquals(Math.min(1, 0), 0);
248 Assert.assertEquals(Math.min(0, 1), 0);
249 Assert.assertEquals(Math.min(0, Integer.MAX_VALUE), 0);
250 Assert.assertEquals(Math.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
251 Assert.assertEquals(Math.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
252 }
253
254 public static void test_Math_max() {
255 Assert.assertEquals(Math.max(0, 0), 0);
256 Assert.assertEquals(Math.max(1, 0), 1);
257 Assert.assertEquals(Math.max(0, 1), 1);
258 Assert.assertEquals(Math.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
259 Assert.assertEquals(Math.max(Integer.MIN_VALUE, 0), 0);
260 Assert.assertEquals(Math.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
261 }
262
Sebastien Hertzbf1442d2013-03-05 15:12:40 +0100263 public static void test_StrictMath_abs_I() {
264 Assert.assertEquals(StrictMath.abs(0), 0);
265 Assert.assertEquals(StrictMath.abs(123), 123);
266 Assert.assertEquals(StrictMath.abs(-123), 123);
267 Assert.assertEquals(StrictMath.abs(Integer.MAX_VALUE), Integer.MAX_VALUE);
268 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE), Integer.MIN_VALUE);
269 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE - 1), Integer.MAX_VALUE);
270 Assert.assertEquals(StrictMath.abs(Integer.MIN_VALUE + 1), Integer.MAX_VALUE);
271 }
272
273 public static void test_StrictMath_abs_J() {
274 Assert.assertEquals(StrictMath.abs(0L), 0L);
275 Assert.assertEquals(StrictMath.abs(123L), 123L);
276 Assert.assertEquals(StrictMath.abs(-123L), 123L);
277 Assert.assertEquals(StrictMath.abs(Long.MAX_VALUE), Long.MAX_VALUE);
278 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE), Long.MIN_VALUE);
279 Assert.assertEquals(StrictMath.abs(Long.MIN_VALUE - 1), Long.MAX_VALUE);
280 }
281
282 public static void test_StrictMath_min() {
283 Assert.assertEquals(StrictMath.min(0, 0), 0);
284 Assert.assertEquals(StrictMath.min(1, 0), 0);
285 Assert.assertEquals(StrictMath.min(0, 1), 0);
286 Assert.assertEquals(StrictMath.min(0, Integer.MAX_VALUE), 0);
287 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, 0), Integer.MIN_VALUE);
288 Assert.assertEquals(StrictMath.min(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MIN_VALUE);
289 }
290
291 public static void test_StrictMath_max() {
292 Assert.assertEquals(StrictMath.max(0, 0), 0);
293 Assert.assertEquals(StrictMath.max(1, 0), 1);
294 Assert.assertEquals(StrictMath.max(0, 1), 1);
295 Assert.assertEquals(StrictMath.max(0, Integer.MAX_VALUE), Integer.MAX_VALUE);
296 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, 0), 0);
297 Assert.assertEquals(StrictMath.max(Integer.MIN_VALUE, Integer.MAX_VALUE), Integer.MAX_VALUE);
298 }
299
Elliott Hughes28c384b2012-06-15 16:46:25 -0700300 public static void test_Float_floatToRawIntBits() {
301 Assert.assertEquals(Float.floatToRawIntBits(-1.0f), 0xbf800000);
302 Assert.assertEquals(Float.floatToRawIntBits(0.0f), 0);
303 Assert.assertEquals(Float.floatToRawIntBits(1.0f), 0x3f800000);
304 Assert.assertEquals(Float.floatToRawIntBits(Float.NaN), 0x7fc00000);
305 Assert.assertEquals(Float.floatToRawIntBits(Float.POSITIVE_INFINITY), 0x7f800000);
306 Assert.assertEquals(Float.floatToRawIntBits(Float.NEGATIVE_INFINITY), 0xff800000);
307 }
308
309 public static void test_Float_intBitsToFloat() {
310 Assert.assertEquals(Float.intBitsToFloat(0xbf800000), -1.0f);
311 Assert.assertEquals(Float.intBitsToFloat(0x00000000), 0.0f);
312 Assert.assertEquals(Float.intBitsToFloat(0x3f800000), 1.0f);
313 Assert.assertEquals(Float.intBitsToFloat(0x7fc00000), Float.NaN);
314 Assert.assertEquals(Float.intBitsToFloat(0x7f800000), Float.POSITIVE_INFINITY);
315 Assert.assertEquals(Float.intBitsToFloat(0xff800000), Float.NEGATIVE_INFINITY);
316 }
317
318 public static void test_Double_doubleToRawLongBits() {
319 Assert.assertEquals(Double.doubleToRawLongBits(-1.0), 0xbff0000000000000L);
320 Assert.assertEquals(Double.doubleToRawLongBits(0.0), 0x0000000000000000L);
321 Assert.assertEquals(Double.doubleToRawLongBits(1.0), 0x3ff0000000000000L);
322 Assert.assertEquals(Double.doubleToRawLongBits(Double.NaN), 0x7ff8000000000000L);
323 Assert.assertEquals(Double.doubleToRawLongBits(Double.POSITIVE_INFINITY), 0x7ff0000000000000L);
324 Assert.assertEquals(Double.doubleToRawLongBits(Double.NEGATIVE_INFINITY), 0xfff0000000000000L);
325 }
326
327 public static void test_Double_longBitsToDouble() {
328 Assert.assertEquals(Double.longBitsToDouble(0xbff0000000000000L), -1.0);
329 Assert.assertEquals(Double.longBitsToDouble(0x0000000000000000L), 0.0);
330 Assert.assertEquals(Double.longBitsToDouble(0x3ff0000000000000L), 1.0);
331 Assert.assertEquals(Double.longBitsToDouble(0x7ff8000000000000L), Double.NaN);
332 Assert.assertEquals(Double.longBitsToDouble(0x7ff0000000000000L), Double.POSITIVE_INFINITY);
333 Assert.assertEquals(Double.longBitsToDouble(0xfff0000000000000L), Double.NEGATIVE_INFINITY);
334 }
jeffhao5d1ac922011-09-29 17:41:15 -0700335}