blob: 0966e717ca340ce939520d8de8f343a7c8975b2c [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
Elliott Hughes11e45072011-08-16 17:40:46 -070017#include "common_test.h"
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080018#include "mirror/array.h"
19#include "mirror/array-inl.h"
20#include "mirror/object-inl.h"
21#include "mirror/object_array-inl.h"
22#include "mirror/string.h"
Ian Rogers00f7d0e2012-07-19 15:28:27 -070023#include "scoped_thread_state_change.h"
Ian Rogers1f539342012-10-03 21:09:42 -070024#include "sirt_ref.h"
Elliott Hughes11e45072011-08-16 17:40:46 -070025#include "utils.h"
26
Elliott Hughes11e45072011-08-16 17:40:46 -070027namespace art {
28
Elliott Hughes9058f2b2012-03-22 18:06:48 -070029std::string PrettyArguments(const char* signature);
30std::string PrettyReturnType(const char* signature);
31
Brian Carlstromf734cf52011-08-17 16:28:14 -070032class UtilsTest : public CommonTest {
Elliott Hughes11e45072011-08-16 17:40:46 -070033};
34
Elliott Hughes5174fe62011-08-23 15:12:35 -070035TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070036 EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;"));
37 EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070038}
39
Elliott Hughes5174fe62011-08-23 15:12:35 -070040TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070041 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;"));
42 EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070043}
44
Elliott Hughes5174fe62011-08-23 15:12:35 -070045TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070046 EXPECT_EQ("boolean[]", PrettyDescriptor("[Z"));
47 EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z"));
48 EXPECT_EQ("byte[]", PrettyDescriptor("[B"));
49 EXPECT_EQ("byte[][]", PrettyDescriptor("[[B"));
50 EXPECT_EQ("char[]", PrettyDescriptor("[C"));
51 EXPECT_EQ("char[][]", PrettyDescriptor("[[C"));
52 EXPECT_EQ("double[]", PrettyDescriptor("[D"));
53 EXPECT_EQ("double[][]", PrettyDescriptor("[[D"));
54 EXPECT_EQ("float[]", PrettyDescriptor("[F"));
55 EXPECT_EQ("float[][]", PrettyDescriptor("[[F"));
56 EXPECT_EQ("int[]", PrettyDescriptor("[I"));
57 EXPECT_EQ("int[][]", PrettyDescriptor("[[I"));
58 EXPECT_EQ("long[]", PrettyDescriptor("[J"));
59 EXPECT_EQ("long[][]", PrettyDescriptor("[[J"));
60 EXPECT_EQ("short[]", PrettyDescriptor("[S"));
61 EXPECT_EQ("short[][]", PrettyDescriptor("[[S"));
Elliott Hughes11e45072011-08-16 17:40:46 -070062}
63
Elliott Hughes5174fe62011-08-23 15:12:35 -070064TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
Elliott Hughes9058f2b2012-03-22 18:06:48 -070065 EXPECT_EQ("boolean", PrettyDescriptor("Z"));
66 EXPECT_EQ("byte", PrettyDescriptor("B"));
67 EXPECT_EQ("char", PrettyDescriptor("C"));
68 EXPECT_EQ("double", PrettyDescriptor("D"));
69 EXPECT_EQ("float", PrettyDescriptor("F"));
70 EXPECT_EQ("int", PrettyDescriptor("I"));
71 EXPECT_EQ("long", PrettyDescriptor("J"));
72 EXPECT_EQ("short", PrettyDescriptor("S"));
73}
74
75TEST_F(UtilsTest, PrettyArguments) {
76 EXPECT_EQ("()", PrettyArguments("()V"));
77 EXPECT_EQ("(int)", PrettyArguments("(I)V"));
78 EXPECT_EQ("(int, int)", PrettyArguments("(II)V"));
79 EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V"));
80 EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V"));
81 EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V"));
82}
83
84TEST_F(UtilsTest, PrettyReturnType) {
85 EXPECT_EQ("void", PrettyReturnType("()V"));
86 EXPECT_EQ("int", PrettyReturnType("()I"));
87 EXPECT_EQ("int[][]", PrettyReturnType("()[[I"));
88 EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;"));
89 EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;"));
Elliott Hughes11e45072011-08-16 17:40:46 -070090}
91
Elliott Hughes54e7df12011-09-16 11:47:04 -070092TEST_F(UtilsTest, PrettyTypeOf) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -070093 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -070094 EXPECT_EQ("null", PrettyTypeOf(NULL));
Elliott Hughes11e45072011-08-16 17:40:46 -070095
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080096 SirtRef<mirror::String> s(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), ""));
Brian Carlstrom40381fb2011-10-19 14:13:40 -070097 EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get()));
Elliott Hughes11e45072011-08-16 17:40:46 -070098
Ian Rogers2dd0e2c2013-01-24 12:42:14 -080099 SirtRef<mirror::ShortArray> a(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 2));
Brian Carlstrom40381fb2011-10-19 14:13:40 -0700100 EXPECT_EQ("short[]", PrettyTypeOf(a.get()));
Elliott Hughes11e45072011-08-16 17:40:46 -0700101
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800102 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
Elliott Hughes11e45072011-08-16 17:40:46 -0700103 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800104 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700105 EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
106 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
107}
108
109TEST_F(UtilsTest, PrettyClass) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700110 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700111 EXPECT_EQ("null", PrettyClass(NULL));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800112 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700113 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800114 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Elliott Hughes54e7df12011-09-16 11:47:04 -0700115 EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
116}
117
Ian Rogersd81871c2011-10-03 13:57:23 -0700118TEST_F(UtilsTest, PrettyClassAndClassLoader) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700119 ScopedObjectAccess soa(Thread::Current());
Ian Rogersd81871c2011-10-03 13:57:23 -0700120 EXPECT_EQ("null", PrettyClassAndClassLoader(NULL));
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800121 mirror::Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
Ian Rogersd81871c2011-10-03 13:57:23 -0700122 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800123 mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0);
Ian Rogersd81871c2011-10-03 13:57:23 -0700124 EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass()));
125}
126
Elliott Hughes54e7df12011-09-16 11:47:04 -0700127TEST_F(UtilsTest, PrettyField) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700128 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes54e7df12011-09-16 11:47:04 -0700129 EXPECT_EQ("null", PrettyField(NULL));
130
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800131 mirror::Class* java_lang_String = class_linker_->FindSystemClass("Ljava/lang/String;");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700132
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800133 mirror::Field* f;
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700134 f = java_lang_String->FindDeclaredInstanceField("count", "I");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700135 EXPECT_EQ("int java.lang.String.count", PrettyField(f));
136 EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
Brian Carlstrom6b4ef022011-10-23 14:59:04 -0700137 f = java_lang_String->FindDeclaredInstanceField("value", "[C");
Elliott Hughes54e7df12011-09-16 11:47:04 -0700138 EXPECT_EQ("char[] java.lang.String.value", PrettyField(f));
139 EXPECT_EQ("java.lang.String.value", PrettyField(f, false));
Elliott Hughes11e45072011-08-16 17:40:46 -0700140}
141
Ian Rogers3bb17a62012-01-27 23:56:44 -0800142TEST_F(UtilsTest, PrettySize) {
143 EXPECT_EQ("1GB", PrettySize(1 * GB));
144 EXPECT_EQ("2GB", PrettySize(2 * GB));
145 if (sizeof(size_t) > sizeof(uint32_t)) {
146 EXPECT_EQ("100GB", PrettySize(100 * GB));
147 }
Elliott Hughesc967f782012-04-16 10:23:15 -0700148 EXPECT_EQ("1024KB", PrettySize(1 * MB));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800149 EXPECT_EQ("10MB", PrettySize(10 * MB));
150 EXPECT_EQ("100MB", PrettySize(100 * MB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700151 EXPECT_EQ("1024B", PrettySize(1 * KB));
Elliott Hughes409d2732012-04-03 13:34:44 -0700152 EXPECT_EQ("10KB", PrettySize(10 * KB));
153 EXPECT_EQ("100KB", PrettySize(100 * KB));
Elliott Hughesc967f782012-04-16 10:23:15 -0700154 EXPECT_EQ("0B", PrettySize(0));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800155 EXPECT_EQ("1B", PrettySize(1));
156 EXPECT_EQ("10B", PrettySize(10));
157 EXPECT_EQ("100B", PrettySize(100));
Elliott Hughesc967f782012-04-16 10:23:15 -0700158 EXPECT_EQ("512B", PrettySize(512));
Ian Rogers3bb17a62012-01-27 23:56:44 -0800159}
160
161TEST_F(UtilsTest, PrettyDuration) {
162 const uint64_t one_sec = 1000000000;
163 const uint64_t one_ms = 1000000;
164 const uint64_t one_us = 1000;
165
166 EXPECT_EQ("1s", PrettyDuration(1 * one_sec));
167 EXPECT_EQ("10s", PrettyDuration(10 * one_sec));
168 EXPECT_EQ("100s", PrettyDuration(100 * one_sec));
169 EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms));
170 EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us));
171 EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1));
172
173 EXPECT_EQ("1ms", PrettyDuration(1 * one_ms));
174 EXPECT_EQ("10ms", PrettyDuration(10 * one_ms));
175 EXPECT_EQ("100ms", PrettyDuration(100 * one_ms));
176 EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us));
177 EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1));
178
179 EXPECT_EQ("1us", PrettyDuration(1 * one_us));
180 EXPECT_EQ("10us", PrettyDuration(10 * one_us));
181 EXPECT_EQ("100us", PrettyDuration(100 * one_us));
182 EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1));
183
184 EXPECT_EQ("1ns", PrettyDuration(1));
185 EXPECT_EQ("10ns", PrettyDuration(10));
186 EXPECT_EQ("100ns", PrettyDuration(100));
187}
188
Elliott Hughes79082e32011-08-25 12:07:32 -0700189TEST_F(UtilsTest, MangleForJni) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700190 ScopedObjectAccess soa(Thread::Current());
Elliott Hughes79082e32011-08-25 12:07:32 -0700191 EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
192 EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
193 EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
194 EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
195 EXPECT_EQ("_3C", MangleForJni("[C"));
196}
197
198TEST_F(UtilsTest, JniShortName_JniLongName) {
Ian Rogers00f7d0e2012-07-19 15:28:27 -0700199 ScopedObjectAccess soa(Thread::Current());
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800200 mirror::Class* c = class_linker_->FindSystemClass("Ljava/lang/String;");
Elliott Hughes79082e32011-08-25 12:07:32 -0700201 ASSERT_TRUE(c != NULL);
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800202 mirror::AbstractMethod* m;
Elliott Hughes79082e32011-08-25 12:07:32 -0700203
204 m = c->FindVirtualMethod("charAt", "(I)C");
205 ASSERT_TRUE(m != NULL);
206 EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
207 EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
208
209 m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I");
210 ASSERT_TRUE(m != NULL);
211 EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
212 EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
213
214 m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;");
215 ASSERT_TRUE(m != NULL);
216 EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
217 EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
218}
219
Elliott Hughes34023802011-08-30 12:06:17 -0700220TEST_F(UtilsTest, Split) {
221 std::vector<std::string> actual;
222 std::vector<std::string> expected;
223
224 expected.clear();
225
226 actual.clear();
227 Split("", ':', actual);
228 EXPECT_EQ(expected, actual);
229
230 actual.clear();
231 Split(":", ':', actual);
232 EXPECT_EQ(expected, actual);
233
234 expected.clear();
235 expected.push_back("foo");
236
237 actual.clear();
238 Split(":foo", ':', actual);
239 EXPECT_EQ(expected, actual);
240
241 actual.clear();
242 Split("foo:", ':', actual);
243 EXPECT_EQ(expected, actual);
244
245 actual.clear();
246 Split(":foo:", ':', actual);
247 EXPECT_EQ(expected, actual);
248
249 expected.push_back("bar");
250
251 actual.clear();
252 Split("foo:bar", ':', actual);
253 EXPECT_EQ(expected, actual);
254
255 actual.clear();
256 Split(":foo:bar", ':', actual);
257 EXPECT_EQ(expected, actual);
258
259 actual.clear();
260 Split("foo:bar:", ':', actual);
261 EXPECT_EQ(expected, actual);
262
263 actual.clear();
264 Split(":foo:bar:", ':', actual);
265 EXPECT_EQ(expected, actual);
266
267 expected.push_back("baz");
268
269 actual.clear();
270 Split("foo:bar:baz", ':', actual);
271 EXPECT_EQ(expected, actual);
272
273 actual.clear();
274 Split(":foo:bar:baz", ':', actual);
275 EXPECT_EQ(expected, actual);
276
277 actual.clear();
278 Split("foo:bar:baz:", ':', actual);
279 EXPECT_EQ(expected, actual);
280
281 actual.clear();
282 Split(":foo:bar:baz:", ':', actual);
283 EXPECT_EQ(expected, actual);
284}
285
Elliott Hughes48436bb2012-02-07 15:23:28 -0800286TEST_F(UtilsTest, Join) {
287 std::vector<std::string> strings;
288
289 strings.clear();
290 EXPECT_EQ("", Join(strings, ':'));
291
292 strings.clear();
293 strings.push_back("foo");
294 EXPECT_EQ("foo", Join(strings, ':'));
295
296 strings.clear();
297 strings.push_back("");
298 strings.push_back("foo");
299 EXPECT_EQ(":foo", Join(strings, ':'));
300
301 strings.clear();
302 strings.push_back("foo");
303 strings.push_back("");
304 EXPECT_EQ("foo:", Join(strings, ':'));
305
306 strings.clear();
307 strings.push_back("");
308 strings.push_back("foo");
309 strings.push_back("");
310 EXPECT_EQ(":foo:", Join(strings, ':'));
311
312 strings.clear();
313 strings.push_back("foo");
314 strings.push_back("bar");
315 EXPECT_EQ("foo:bar", Join(strings, ':'));
316
317 strings.clear();
318 strings.push_back("foo");
319 strings.push_back("bar");
320 strings.push_back("baz");
321 EXPECT_EQ("foo:bar:baz", Join(strings, ':'));
322}
323
Brian Carlstrom7a967b32012-03-28 15:23:10 -0700324TEST_F(UtilsTest, StartsWith) {
325 EXPECT_FALSE(StartsWith("foo", "bar"));
326 EXPECT_TRUE(StartsWith("foo", "foo"));
327 EXPECT_TRUE(StartsWith("food", "foo"));
328 EXPECT_FALSE(StartsWith("fo", "foo"));
329}
330
331TEST_F(UtilsTest, EndsWith) {
332 EXPECT_FALSE(EndsWith("foo", "bar"));
333 EXPECT_TRUE(EndsWith("foo", "foo"));
334 EXPECT_TRUE(EndsWith("foofoo", "foo"));
335 EXPECT_FALSE(EndsWith("oo", "foo"));
336}
337
Elliott Hughes11e45072011-08-16 17:40:46 -0700338} // namespace art