blob: d425620c1cbbeaac2924fd8372d79187aa26d237 [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
2 * Copyright (C) 2011 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 */
Elliott Hughes11e45072011-08-16 17:40:46 -070016
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080017#include "utils.h"
18
19#include "common_runtime_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080020#include "mirror/array.h"
21#include "mirror/array-inl.h"
22#include "mirror/object-inl.h"
23#include "mirror/object_array-inl.h"
24#include "mirror/string.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070025#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070026#include "sirt_ref.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070027
Mathieu Chartier661974a2014-01-09 11:23:53 -080028#include <valgrind.h>
29
Elliott Hughes11e45072011-08-16 17:40:46 -070030namespace art {
31
Elliott Hughes9058f2b2012-03-22 18:06:48 -070032std::string PrettyArguments(const char* signature);
33std::string PrettyReturnType(const char* signature);
34
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -080035class UtilsTest : public CommonRuntimeTest {};
Elliott Hughes11e45072011-08-16 17:40:46 -070036
Elliott Hughes5174fe62011-08-23 15:12:35 -070037TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070038 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
39 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070040}
41
Elliott Hughes5174fe62011-08-23 15:12:35 -070042TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070043 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
44 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070045}
46
Elliott Hughes5174fe62011-08-23 15:12:35 -070047TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070048 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
49 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
50 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
51 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
52 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
53 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
54 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
55 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
56 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
57 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
58 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
59 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
60 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
61 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
62 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
63 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
Elliott Hughes11e45072011-08-16 17:40:46 -070064}
65
Elliott Hughes5174fe62011-08-23 15:12:35 -070066TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070067 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
68 EXPECT_EQ("byte", PrettyDescriptor("B"));
69 EXPECT_EQ("char", PrettyDescriptor("C"));
70 EXPECT_EQ("double", PrettyDescriptor("D"));
71 EXPECT_EQ("float", PrettyDescriptor("F"));
72 EXPECT_EQ("int", PrettyDescriptor("I"));
73 EXPECT_EQ("long", PrettyDescriptor("J"));
74 EXPECT_EQ("short", PrettyDescriptor("S"));
75}
76
77TEST_F(UtilsTest, PrettyArguments) {
78 EXPECT_EQ("()", PrettyArguments("()V"));
79 EXPECT_EQ("(int)", PrettyArguments("(I)V"));
80 EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
81 EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
82 EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
83 EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
84}
85
86TEST_F(UtilsTest, PrettyReturnType) {
87 EXPECT_EQ("void", PrettyReturnType("()V"));
88 EXPECT_EQ("int", PrettyReturnType("()I"));
89 EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
90 EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
91 EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070092}
93
Elliott Hughes54e7df12011-09-16 11:47:04 -070094TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070095 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -070096 EXPECT_EQ("null", PrettyTypeOf(NULL));
Elliott Hughes11e45072011-08-16 17:40:46 -070097
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080098 SirtRef<mirror::String> s(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), ""));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070099 EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700100
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800101 SirtRef<mirror::ShortArray> a(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 2));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700102 EXPECT_EQ("short[]", PrettyTypeOf(a.get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700103
Ian Rogers98379392014-02-24 16:53:16 -0800104 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Elliott Hughes11e45072011-08-16 17:40:46 -0700105 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800106 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700107 EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
108 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
109}
110
111TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700112 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700113 EXPECT_EQ("null", PrettyClass(NULL));
Ian Rogers98379392014-02-24 16:53:16 -0800114 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700115 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800116 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700117 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
118}
119
Ian Rogersd81871c2011-10-03 13:57:23 -0700120TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700121 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd81871c2011-10-03 13:57:23 -0700122 EXPECT_EQ("null", PrettyClassAndClassLoader(NULL));
Ian Rogers98379392014-02-24 16:53:16 -0800123 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;");
Ian Rogersd81871c2011-10-03 13:57:23 -0700124 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800125 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -0700126 EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
127}
128
Elliott Hughes54e7df12011-09-16 11:47:04 -0700129TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700130 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700131 EXPECT_EQ("null", PrettyField(NULL));
132
Ian Rogers98379392014-02-24 16:53:16 -0800133 mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(),
134 "Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700135
Brian Carlstromea46f952013-07-30 01:26:50 -0700136 mirror::ArtField* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 f = java_lang_String->FindDeclaredInstanceField("count", "I");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700138 EXPECT_EQ("int java.lang.String.count", PrettyField(f));
139 EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700140 f = java_lang_String->FindDeclaredInstanceField("value", "[C");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700141 EXPECT_EQ("char[] java.lang.String.value", PrettyField(f));
142 EXPECT_EQ("java.lang.String.value", PrettyField(f, false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700143}
144
Ian Rogers3bb17a62012-01-27 23:56:44 -0800145TEST_F(UtilsTest, PrettySize) {
146 EXPECT_EQ("1GB", PrettySize(1 * GB));
147 EXPECT_EQ("2GB", PrettySize(2 * GB));
148 if (sizeof(size_t) > sizeof(uint32_t)) {
149 EXPECT_EQ("100GB", PrettySize(100 * GB));
150 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700151 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800152 EXPECT_EQ("10MB", PrettySize(10 * MB));
153 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700154 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700155 EXPECT_EQ("10KB", PrettySize(10 * KB));
156 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700157 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800158 EXPECT_EQ("1B", PrettySize(1));
159 EXPECT_EQ("10B", PrettySize(10));
160 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700161 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800162}
163
164TEST_F(UtilsTest, PrettyDuration) {
165 const uint64_t one_sec = 1000000000;
166 const uint64_t one_ms = 1000000;
167 const uint64_t one_us = 1000;
168
169 EXPECT_EQ("1s", PrettyDuration(1 * one_sec));
170 EXPECT_EQ("10s", PrettyDuration(10 * one_sec));
171 EXPECT_EQ("100s", PrettyDuration(100 * one_sec));
172 EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms));
173 EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us));
174 EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1));
175
176 EXPECT_EQ("1ms", PrettyDuration(1 * one_ms));
177 EXPECT_EQ("10ms", PrettyDuration(10 * one_ms));
178 EXPECT_EQ("100ms", PrettyDuration(100 * one_ms));
179 EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us));
180 EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1));
181
182 EXPECT_EQ("1us", PrettyDuration(1 * one_us));
183 EXPECT_EQ("10us", PrettyDuration(10 * one_us));
184 EXPECT_EQ("100us", PrettyDuration(100 * one_us));
185 EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1));
186
187 EXPECT_EQ("1ns", PrettyDuration(1));
188 EXPECT_EQ("10ns", PrettyDuration(10));
189 EXPECT_EQ("100ns", PrettyDuration(100));
190}
191
Elliott Hughes79082e32011-08-25 12:07:32 -0700192TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700193 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700194 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
195 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
196 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
197 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
198 EXPECT_EQ("_3C", MangleForJni("[C"));
199}
200
201TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700202 ScopedObjectAccess soa(Thread::Current());
Ian Rogers98379392014-02-24 16:53:16 -0800203 mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;");
Elliott Hughes79082e32011-08-25 12:07:32 -0700204 ASSERT_TRUE(c != NULL);
Brian Carlstromea46f952013-07-30 01:26:50 -0700205 mirror::ArtMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700206
207 m = c->FindVirtualMethod("charAt", "(I)C");
208 ASSERT_TRUE(m != NULL);
209 EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
210 EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
211
212 m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I");
213 ASSERT_TRUE(m != NULL);
214 EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
215 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
216
217 m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;");
218 ASSERT_TRUE(m != NULL);
219 EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
220 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
221}
222
Elliott Hughes34023802011-08-30 12:06:17 -0700223TEST_F(UtilsTest, Split) {
224 std::vector<std::string> actual;
225 std::vector<std::string> expected;
226
227 expected.clear();
228
229 actual.clear();
230 Split("", ':', actual);
231 EXPECT_EQ(expected, actual);
232
233 actual.clear();
234 Split(":", ':', actual);
235 EXPECT_EQ(expected, actual);
236
237 expected.clear();
238 expected.push_back("foo");
239
240 actual.clear();
241 Split(":foo", ':', actual);
242 EXPECT_EQ(expected, actual);
243
244 actual.clear();
245 Split("foo:", ':', actual);
246 EXPECT_EQ(expected, actual);
247
248 actual.clear();
249 Split(":foo:", ':', actual);
250 EXPECT_EQ(expected, actual);
251
252 expected.push_back("bar");
253
254 actual.clear();
255 Split("foo:bar", ':', actual);
256 EXPECT_EQ(expected, actual);
257
258 actual.clear();
259 Split(":foo:bar", ':', actual);
260 EXPECT_EQ(expected, actual);
261
262 actual.clear();
263 Split("foo:bar:", ':', actual);
264 EXPECT_EQ(expected, actual);
265
266 actual.clear();
267 Split(":foo:bar:", ':', actual);
268 EXPECT_EQ(expected, actual);
269
270 expected.push_back("baz");
271
272 actual.clear();
273 Split("foo:bar:baz", ':', actual);
274 EXPECT_EQ(expected, actual);
275
276 actual.clear();
277 Split(":foo:bar:baz", ':', actual);
278 EXPECT_EQ(expected, actual);
279
280 actual.clear();
281 Split("foo:bar:baz:", ':', actual);
282 EXPECT_EQ(expected, actual);
283
284 actual.clear();
285 Split(":foo:bar:baz:", ':', actual);
286 EXPECT_EQ(expected, actual);
287}
288
Elliott Hughes48436bb2012-02-07 15:23:28 -0800289TEST_F(UtilsTest, Join) {
290 std::vector<std::string> strings;
291
292 strings.clear();
293 EXPECT_EQ("", Join(strings, ':'));
294
295 strings.clear();
296 strings.push_back("foo");
297 EXPECT_EQ("foo", Join(strings, ':'));
298
299 strings.clear();
300 strings.push_back("");
301 strings.push_back("foo");
302 EXPECT_EQ(":foo", Join(strings, ':'));
303
304 strings.clear();
305 strings.push_back("foo");
306 strings.push_back("");
307 EXPECT_EQ("foo:", Join(strings, ':'));
308
309 strings.clear();
310 strings.push_back("");
311 strings.push_back("foo");
312 strings.push_back("");
313 EXPECT_EQ(":foo:", Join(strings, ':'));
314
315 strings.clear();
316 strings.push_back("foo");
317 strings.push_back("bar");
318 EXPECT_EQ("foo:bar", Join(strings, ':'));
319
320 strings.clear();
321 strings.push_back("foo");
322 strings.push_back("bar");
323 strings.push_back("baz");
324 EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
325}
326
Brian Carlstrom7a967b32012-03-28 15:23:10 -0700327TEST_F(UtilsTest, StartsWith) {
328 EXPECT_FALSE(StartsWith("foo", "bar"));
329 EXPECT_TRUE(StartsWith("foo", "foo"));
330 EXPECT_TRUE(StartsWith("food", "foo"));
331 EXPECT_FALSE(StartsWith("fo", "foo"));
332}
333
334TEST_F(UtilsTest, EndsWith) {
335 EXPECT_FALSE(EndsWith("foo", "bar"));
336 EXPECT_TRUE(EndsWith("foo", "foo"));
337 EXPECT_TRUE(EndsWith("foofoo", "foo"));
338 EXPECT_FALSE(EndsWith("oo", "foo"));
339}
340
Brian Carlstrom19a08362013-10-16 14:37:22 -0700341TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) {
Narayan Kamath11d9f062014-04-23 20:24:57 +0100342 EXPECT_STREQ("/foo/system@app@Foo.apk@classes.dex",
343 GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str());
344
345 EXPECT_STREQ("/foo/data@app@foo-1.apk@classes.dex",
346 GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str());
347 EXPECT_STREQ("/foo/system@framework@core.jar@classes.dex",
348 GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str());
349 EXPECT_STREQ("/foo/system@framework@boot.art",
350 GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str());
Brian Carlstrom19a08362013-10-16 14:37:22 -0700351}
352
Brian Carlstrom6449c622014-02-10 23:48:36 -0800353TEST_F(UtilsTest, ExecSuccess) {
354 std::vector<std::string> command;
355 if (kIsTargetBuild) {
356 command.push_back("/system/bin/id");
357 } else {
358 command.push_back("/usr/bin/id");
359 }
360 std::string error_msg;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800361 if (RUNNING_ON_VALGRIND == 0) {
362 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
363 EXPECT_TRUE(Exec(command, &error_msg));
364 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800365 EXPECT_EQ(0U, error_msg.size()) << error_msg;
366}
367
Brian Carlstroma1ce1fe2014-02-24 23:23:58 -0800368TEST_F(UtilsTest, ExecError) {
Brian Carlstrom6449c622014-02-10 23:48:36 -0800369 std::vector<std::string> command;
370 command.push_back("bogus");
371 std::string error_msg;
Mathieu Chartier661974a2014-01-09 11:23:53 -0800372 if (RUNNING_ON_VALGRIND == 0) {
373 // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks.
374 EXPECT_FALSE(Exec(command, &error_msg));
375 EXPECT_NE(0U, error_msg.size());
376 }
Brian Carlstrom6449c622014-02-10 23:48:36 -0800377}
378
Elliott Hughes11e45072011-08-16 17:40:46 -0700379} // namespace art