Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 1 | /* |
| 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 16 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 17 | #include "utils.h" |
| 18 | |
| 19 | #include "common_runtime_test.h" |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 20 | #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 Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 25 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 26 | #include "handle_scope-inl.h" |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 27 | |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 28 | #include <valgrind.h> |
| 29 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 30 | namespace art { |
| 31 | |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 32 | std::string PrettyArguments(const char* signature); |
| 33 | std::string PrettyReturnType(const char* signature); |
| 34 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 35 | class UtilsTest : public CommonRuntimeTest {}; |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 37 | TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 38 | EXPECT_EQ("java.lang.Class[]", PrettyDescriptor("[Ljava/lang/Class;")); |
| 39 | EXPECT_EQ("java.lang.Class[][]", PrettyDescriptor("[[Ljava/lang/Class;")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 42 | TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 43 | EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava.lang.String;")); |
| 44 | EXPECT_EQ("java.lang.String", PrettyDescriptor("Ljava/lang/String;")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 45 | } |
| 46 | |
Mathieu Chartier | 7643327 | 2014-09-26 14:32:37 -0700 | [diff] [blame] | 47 | TEST_F(UtilsTest, PrettyDescriptor_Primitive) { |
| 48 | EXPECT_EQ("boolean", PrettyDescriptor(Primitive::kPrimBoolean)); |
| 49 | EXPECT_EQ("byte", PrettyDescriptor(Primitive::kPrimByte)); |
| 50 | EXPECT_EQ("char", PrettyDescriptor(Primitive::kPrimChar)); |
| 51 | EXPECT_EQ("short", PrettyDescriptor(Primitive::kPrimShort)); |
| 52 | EXPECT_EQ("int", PrettyDescriptor(Primitive::kPrimInt)); |
| 53 | EXPECT_EQ("float", PrettyDescriptor(Primitive::kPrimFloat)); |
| 54 | EXPECT_EQ("long", PrettyDescriptor(Primitive::kPrimLong)); |
| 55 | EXPECT_EQ("double", PrettyDescriptor(Primitive::kPrimDouble)); |
| 56 | EXPECT_EQ("void", PrettyDescriptor(Primitive::kPrimVoid)); |
| 57 | } |
| 58 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 59 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 60 | EXPECT_EQ("boolean[]", PrettyDescriptor("[Z")); |
| 61 | EXPECT_EQ("boolean[][]", PrettyDescriptor("[[Z")); |
| 62 | EXPECT_EQ("byte[]", PrettyDescriptor("[B")); |
| 63 | EXPECT_EQ("byte[][]", PrettyDescriptor("[[B")); |
| 64 | EXPECT_EQ("char[]", PrettyDescriptor("[C")); |
| 65 | EXPECT_EQ("char[][]", PrettyDescriptor("[[C")); |
| 66 | EXPECT_EQ("double[]", PrettyDescriptor("[D")); |
| 67 | EXPECT_EQ("double[][]", PrettyDescriptor("[[D")); |
| 68 | EXPECT_EQ("float[]", PrettyDescriptor("[F")); |
| 69 | EXPECT_EQ("float[][]", PrettyDescriptor("[[F")); |
| 70 | EXPECT_EQ("int[]", PrettyDescriptor("[I")); |
| 71 | EXPECT_EQ("int[][]", PrettyDescriptor("[[I")); |
| 72 | EXPECT_EQ("long[]", PrettyDescriptor("[J")); |
| 73 | EXPECT_EQ("long[][]", PrettyDescriptor("[[J")); |
| 74 | EXPECT_EQ("short[]", PrettyDescriptor("[S")); |
| 75 | EXPECT_EQ("short[][]", PrettyDescriptor("[[S")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 78 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 79 | EXPECT_EQ("boolean", PrettyDescriptor("Z")); |
| 80 | EXPECT_EQ("byte", PrettyDescriptor("B")); |
| 81 | EXPECT_EQ("char", PrettyDescriptor("C")); |
| 82 | EXPECT_EQ("double", PrettyDescriptor("D")); |
| 83 | EXPECT_EQ("float", PrettyDescriptor("F")); |
| 84 | EXPECT_EQ("int", PrettyDescriptor("I")); |
| 85 | EXPECT_EQ("long", PrettyDescriptor("J")); |
| 86 | EXPECT_EQ("short", PrettyDescriptor("S")); |
| 87 | } |
| 88 | |
| 89 | TEST_F(UtilsTest, PrettyArguments) { |
| 90 | EXPECT_EQ("()", PrettyArguments("()V")); |
| 91 | EXPECT_EQ("(int)", PrettyArguments("(I)V")); |
| 92 | EXPECT_EQ("(int, int)", PrettyArguments("(II)V")); |
| 93 | EXPECT_EQ("(int, int, int[][])", PrettyArguments("(II[[I)V")); |
| 94 | EXPECT_EQ("(int, int, int[][], java.lang.Poop)", PrettyArguments("(II[[ILjava/lang/Poop;)V")); |
| 95 | EXPECT_EQ("(int, int, int[][], java.lang.Poop, java.lang.Poop[][])", PrettyArguments("(II[[ILjava/lang/Poop;[[Ljava/lang/Poop;)V")); |
| 96 | } |
| 97 | |
| 98 | TEST_F(UtilsTest, PrettyReturnType) { |
| 99 | EXPECT_EQ("void", PrettyReturnType("()V")); |
| 100 | EXPECT_EQ("int", PrettyReturnType("()I")); |
| 101 | EXPECT_EQ("int[][]", PrettyReturnType("()[[I")); |
| 102 | EXPECT_EQ("java.lang.Poop", PrettyReturnType("()Ljava/lang/Poop;")); |
| 103 | EXPECT_EQ("java.lang.Poop[][]", PrettyReturnType("()[[Ljava/lang/Poop;")); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 106 | TEST_F(UtilsTest, PrettyTypeOf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 107 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 108 | EXPECT_EQ("null", PrettyTypeOf(NULL)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 109 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 110 | StackHandleScope<2> hs(soa.Self()); |
| 111 | Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), ""))); |
| 112 | EXPECT_EQ("java.lang.String", PrettyTypeOf(s.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 113 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 114 | Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2))); |
| 115 | EXPECT_EQ("short[]", PrettyTypeOf(a.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 116 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 117 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 118 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 119 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 120 | EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o)); |
| 121 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass())); |
| 122 | } |
| 123 | |
| 124 | TEST_F(UtilsTest, PrettyClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 125 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 126 | EXPECT_EQ("null", PrettyClass(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 127 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 128 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 129 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 130 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass())); |
| 131 | } |
| 132 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 133 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 134 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 135 | EXPECT_EQ("null", PrettyClassAndClassLoader(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 136 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 137 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 138 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 139 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass())); |
| 140 | } |
| 141 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 142 | TEST_F(UtilsTest, PrettyField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 143 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 144 | EXPECT_EQ("null", PrettyField(NULL)); |
| 145 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 146 | mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(), |
| 147 | "Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 148 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 149 | mirror::ArtField* f; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 150 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 151 | EXPECT_EQ("int java.lang.String.count", PrettyField(f)); |
| 152 | EXPECT_EQ("java.lang.String.count", PrettyField(f, false)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 153 | f = java_lang_String->FindDeclaredInstanceField("value", "[C"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 154 | EXPECT_EQ("char[] java.lang.String.value", PrettyField(f)); |
| 155 | EXPECT_EQ("java.lang.String.value", PrettyField(f, false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 158 | TEST_F(UtilsTest, PrettySize) { |
| 159 | EXPECT_EQ("1GB", PrettySize(1 * GB)); |
| 160 | EXPECT_EQ("2GB", PrettySize(2 * GB)); |
| 161 | if (sizeof(size_t) > sizeof(uint32_t)) { |
| 162 | EXPECT_EQ("100GB", PrettySize(100 * GB)); |
| 163 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 164 | EXPECT_EQ("1024KB", PrettySize(1 * MB)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 165 | EXPECT_EQ("10MB", PrettySize(10 * MB)); |
| 166 | EXPECT_EQ("100MB", PrettySize(100 * MB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 167 | EXPECT_EQ("1024B", PrettySize(1 * KB)); |
Elliott Hughes | 409d273 | 2012-04-03 13:34:44 -0700 | [diff] [blame] | 168 | EXPECT_EQ("10KB", PrettySize(10 * KB)); |
| 169 | EXPECT_EQ("100KB", PrettySize(100 * KB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 170 | EXPECT_EQ("0B", PrettySize(0)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 171 | EXPECT_EQ("1B", PrettySize(1)); |
| 172 | EXPECT_EQ("10B", PrettySize(10)); |
| 173 | EXPECT_EQ("100B", PrettySize(100)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 174 | EXPECT_EQ("512B", PrettySize(512)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | TEST_F(UtilsTest, PrettyDuration) { |
| 178 | const uint64_t one_sec = 1000000000; |
| 179 | const uint64_t one_ms = 1000000; |
| 180 | const uint64_t one_us = 1000; |
| 181 | |
| 182 | EXPECT_EQ("1s", PrettyDuration(1 * one_sec)); |
| 183 | EXPECT_EQ("10s", PrettyDuration(10 * one_sec)); |
| 184 | EXPECT_EQ("100s", PrettyDuration(100 * one_sec)); |
| 185 | EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms)); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 186 | EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us, 6)); |
| 187 | EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1, 9)); |
| 188 | EXPECT_EQ("1.000s", PrettyDuration(1 * one_sec + one_us, 3)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 189 | |
| 190 | EXPECT_EQ("1ms", PrettyDuration(1 * one_ms)); |
| 191 | EXPECT_EQ("10ms", PrettyDuration(10 * one_ms)); |
| 192 | EXPECT_EQ("100ms", PrettyDuration(100 * one_ms)); |
| 193 | EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us)); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame] | 194 | EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1, 6)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 195 | |
| 196 | EXPECT_EQ("1us", PrettyDuration(1 * one_us)); |
| 197 | EXPECT_EQ("10us", PrettyDuration(10 * one_us)); |
| 198 | EXPECT_EQ("100us", PrettyDuration(100 * one_us)); |
| 199 | EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1)); |
| 200 | |
| 201 | EXPECT_EQ("1ns", PrettyDuration(1)); |
| 202 | EXPECT_EQ("10ns", PrettyDuration(10)); |
| 203 | EXPECT_EQ("100ns", PrettyDuration(100)); |
| 204 | } |
| 205 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 206 | TEST_F(UtilsTest, MangleForJni) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 207 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 208 | EXPECT_EQ("hello_00024world", MangleForJni("hello$world")); |
| 209 | EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world")); |
| 210 | EXPECT_EQ("hello_1world", MangleForJni("hello_world")); |
| 211 | EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;")); |
| 212 | EXPECT_EQ("_3C", MangleForJni("[C")); |
| 213 | } |
| 214 | |
| 215 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 216 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 217 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 218 | ASSERT_TRUE(c != NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 219 | mirror::ArtMethod* m; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 220 | |
| 221 | m = c->FindVirtualMethod("charAt", "(I)C"); |
| 222 | ASSERT_TRUE(m != NULL); |
| 223 | EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m)); |
| 224 | EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m)); |
| 225 | |
| 226 | m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I"); |
| 227 | ASSERT_TRUE(m != NULL); |
| 228 | EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m)); |
| 229 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m)); |
| 230 | |
| 231 | m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;"); |
| 232 | ASSERT_TRUE(m != NULL); |
| 233 | EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m)); |
| 234 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m)); |
| 235 | } |
| 236 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 237 | TEST_F(UtilsTest, Split) { |
| 238 | std::vector<std::string> actual; |
| 239 | std::vector<std::string> expected; |
| 240 | |
| 241 | expected.clear(); |
| 242 | |
| 243 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 244 | Split("", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 245 | EXPECT_EQ(expected, actual); |
| 246 | |
| 247 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 248 | Split(":", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 249 | EXPECT_EQ(expected, actual); |
| 250 | |
| 251 | expected.clear(); |
| 252 | expected.push_back("foo"); |
| 253 | |
| 254 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 255 | Split(":foo", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 256 | EXPECT_EQ(expected, actual); |
| 257 | |
| 258 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 259 | Split("foo:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 260 | EXPECT_EQ(expected, actual); |
| 261 | |
| 262 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 263 | Split(":foo:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 264 | EXPECT_EQ(expected, actual); |
| 265 | |
| 266 | expected.push_back("bar"); |
| 267 | |
| 268 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 269 | Split("foo:bar", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 270 | EXPECT_EQ(expected, actual); |
| 271 | |
| 272 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 273 | Split(":foo:bar", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 274 | EXPECT_EQ(expected, actual); |
| 275 | |
| 276 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 277 | Split("foo:bar:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 278 | EXPECT_EQ(expected, actual); |
| 279 | |
| 280 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 281 | Split(":foo:bar:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 282 | EXPECT_EQ(expected, actual); |
| 283 | |
| 284 | expected.push_back("baz"); |
| 285 | |
| 286 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 287 | Split("foo:bar:baz", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 288 | EXPECT_EQ(expected, actual); |
| 289 | |
| 290 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 291 | Split(":foo:bar:baz", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 292 | EXPECT_EQ(expected, actual); |
| 293 | |
| 294 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 295 | Split("foo:bar:baz:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 296 | EXPECT_EQ(expected, actual); |
| 297 | |
| 298 | actual.clear(); |
Ian Rogers | 6f3dbba | 2014-10-14 17:41:57 -0700 | [diff] [blame] | 299 | Split(":foo:bar:baz:", ':', &actual); |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 300 | EXPECT_EQ(expected, actual); |
| 301 | } |
| 302 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 303 | TEST_F(UtilsTest, Join) { |
| 304 | std::vector<std::string> strings; |
| 305 | |
| 306 | strings.clear(); |
| 307 | EXPECT_EQ("", Join(strings, ':')); |
| 308 | |
| 309 | strings.clear(); |
| 310 | strings.push_back("foo"); |
| 311 | EXPECT_EQ("foo", Join(strings, ':')); |
| 312 | |
| 313 | strings.clear(); |
| 314 | strings.push_back(""); |
| 315 | strings.push_back("foo"); |
| 316 | EXPECT_EQ(":foo", Join(strings, ':')); |
| 317 | |
| 318 | strings.clear(); |
| 319 | strings.push_back("foo"); |
| 320 | strings.push_back(""); |
| 321 | EXPECT_EQ("foo:", Join(strings, ':')); |
| 322 | |
| 323 | strings.clear(); |
| 324 | strings.push_back(""); |
| 325 | strings.push_back("foo"); |
| 326 | strings.push_back(""); |
| 327 | EXPECT_EQ(":foo:", Join(strings, ':')); |
| 328 | |
| 329 | strings.clear(); |
| 330 | strings.push_back("foo"); |
| 331 | strings.push_back("bar"); |
| 332 | EXPECT_EQ("foo:bar", Join(strings, ':')); |
| 333 | |
| 334 | strings.clear(); |
| 335 | strings.push_back("foo"); |
| 336 | strings.push_back("bar"); |
| 337 | strings.push_back("baz"); |
| 338 | EXPECT_EQ("foo:bar:baz", Join(strings, ':')); |
| 339 | } |
| 340 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 341 | TEST_F(UtilsTest, StartsWith) { |
| 342 | EXPECT_FALSE(StartsWith("foo", "bar")); |
| 343 | EXPECT_TRUE(StartsWith("foo", "foo")); |
| 344 | EXPECT_TRUE(StartsWith("food", "foo")); |
| 345 | EXPECT_FALSE(StartsWith("fo", "foo")); |
| 346 | } |
| 347 | |
| 348 | TEST_F(UtilsTest, EndsWith) { |
| 349 | EXPECT_FALSE(EndsWith("foo", "bar")); |
| 350 | EXPECT_TRUE(EndsWith("foo", "foo")); |
| 351 | EXPECT_TRUE(EndsWith("foofoo", "foo")); |
| 352 | EXPECT_FALSE(EndsWith("oo", "foo")); |
| 353 | } |
| 354 | |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 355 | TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 356 | EXPECT_STREQ("/foo/system@app@Foo.apk@classes.dex", |
| 357 | GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str()); |
| 358 | |
| 359 | EXPECT_STREQ("/foo/data@app@foo-1.apk@classes.dex", |
| 360 | GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str()); |
| 361 | EXPECT_STREQ("/foo/system@framework@core.jar@classes.dex", |
| 362 | GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str()); |
| 363 | EXPECT_STREQ("/foo/system@framework@boot.art", |
| 364 | GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str()); |
Alex Light | 6e183f2 | 2014-07-18 14:57:04 -0700 | [diff] [blame] | 365 | EXPECT_STREQ("/foo/system@framework@boot.oat", |
| 366 | GetDalvikCacheFilenameOrDie("/system/framework/boot.oat", "/foo").c_str()); |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 367 | } |
| 368 | |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 369 | TEST_F(UtilsTest, GetSystemImageFilename) { |
| 370 | EXPECT_STREQ("/system/framework/arm/boot.art", |
| 371 | GetSystemImageFilename("/system/framework/boot.art", kArm).c_str()); |
| 372 | } |
| 373 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 374 | TEST_F(UtilsTest, ExecSuccess) { |
| 375 | std::vector<std::string> command; |
| 376 | if (kIsTargetBuild) { |
| 377 | command.push_back("/system/bin/id"); |
| 378 | } else { |
| 379 | command.push_back("/usr/bin/id"); |
| 380 | } |
| 381 | std::string error_msg; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 382 | if (RUNNING_ON_VALGRIND == 0) { |
| 383 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 384 | EXPECT_TRUE(Exec(command, &error_msg)); |
| 385 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 386 | EXPECT_EQ(0U, error_msg.size()) << error_msg; |
| 387 | } |
| 388 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 389 | TEST_F(UtilsTest, ExecError) { |
Andreas Gampe | 369810a | 2015-01-14 19:53:31 -0800 | [diff] [blame] | 390 | // This will lead to error messages in the log. |
| 391 | ScopedLogSeverity sls(LogSeverity::FATAL); |
| 392 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 393 | std::vector<std::string> command; |
| 394 | command.push_back("bogus"); |
| 395 | std::string error_msg; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 396 | if (RUNNING_ON_VALGRIND == 0) { |
| 397 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 398 | EXPECT_FALSE(Exec(command, &error_msg)); |
| 399 | EXPECT_NE(0U, error_msg.size()); |
| 400 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 401 | } |
| 402 | |
Igor Murashkin | f5b4c50 | 2014-11-14 15:01:59 -0800 | [diff] [blame] | 403 | TEST_F(UtilsTest, RoundUpToPowerOfTwo) { |
| 404 | // Tests the constexpr variant since all the parameters are constexpr |
| 405 | EXPECT_EQ(0, RoundUpToPowerOfTwo(0)); |
| 406 | EXPECT_EQ(1, RoundUpToPowerOfTwo(1)); |
| 407 | EXPECT_EQ(2, RoundUpToPowerOfTwo(2)); |
| 408 | EXPECT_EQ(4, RoundUpToPowerOfTwo(3)); |
| 409 | EXPECT_EQ(8, RoundUpToPowerOfTwo(7)); |
| 410 | |
| 411 | EXPECT_EQ(0b10000L, RoundUpToPowerOfTwo(0b01101L)); |
| 412 | EXPECT_EQ(1ULL << 63, RoundUpToPowerOfTwo(1ULL << 62 | 1ULL)); |
| 413 | } |
| 414 | |
| 415 | TEST_F(UtilsTest, MostSignificantBit) { |
| 416 | EXPECT_EQ(-1, MostSignificantBit(0)); |
| 417 | EXPECT_EQ(0, MostSignificantBit(1)); |
| 418 | EXPECT_EQ(31, MostSignificantBit(~static_cast<uint32_t>(0))); |
| 419 | EXPECT_EQ(2, MostSignificantBit(0b110)); |
| 420 | EXPECT_EQ(2, MostSignificantBit(0b100)); |
| 421 | } |
| 422 | |
| 423 | TEST_F(UtilsTest, MinimumBitsToStore) { |
| 424 | EXPECT_EQ(0u, MinimumBitsToStore(0)); |
| 425 | EXPECT_EQ(1u, MinimumBitsToStore(1)); |
| 426 | EXPECT_EQ(2u, MinimumBitsToStore(0b10)); |
| 427 | EXPECT_EQ(2u, MinimumBitsToStore(0b11)); |
| 428 | EXPECT_EQ(3u, MinimumBitsToStore(0b100)); |
| 429 | EXPECT_EQ(3u, MinimumBitsToStore(0b110)); |
| 430 | EXPECT_EQ(3u, MinimumBitsToStore(0b101)); |
| 431 | EXPECT_EQ(8u, MinimumBitsToStore(0xFF)); |
| 432 | EXPECT_EQ(32u, MinimumBitsToStore(~static_cast<uint32_t>(0))); |
| 433 | } |
| 434 | |
David Brazdil | f646810 | 2015-03-27 10:31:38 +0000 | [diff] [blame^] | 435 | static constexpr int64_t INT_MIN_minus1 = static_cast<int64_t>(INT_MIN) - 1; |
| 436 | static constexpr int64_t INT_MAX_plus1 = static_cast<int64_t>(INT_MAX) + 1; |
| 437 | static constexpr int64_t UINT_MAX_plus1 = static_cast<int64_t>(UINT_MAX) + 1; |
| 438 | |
| 439 | TEST_F(UtilsTest, IsInt) { |
| 440 | EXPECT_FALSE(IsInt(1, -2)); |
| 441 | EXPECT_TRUE(IsInt(1, -1)); |
| 442 | EXPECT_TRUE(IsInt(1, 0)); |
| 443 | EXPECT_FALSE(IsInt(1, 1)); |
| 444 | |
| 445 | EXPECT_FALSE(IsInt(4, -9)); |
| 446 | EXPECT_TRUE(IsInt(4, -8)); |
| 447 | EXPECT_TRUE(IsInt(4, 7)); |
| 448 | EXPECT_FALSE(IsInt(4, 8)); |
| 449 | |
| 450 | EXPECT_FALSE(IsInt(32, INT_MIN_minus1)); |
| 451 | EXPECT_TRUE(IsInt(32, INT_MIN)); |
| 452 | EXPECT_TRUE(IsInt(32, INT_MAX)); |
| 453 | EXPECT_FALSE(IsInt(32, INT_MAX_plus1)); |
| 454 | } |
| 455 | |
| 456 | TEST_F(UtilsTest, IsInt_Static) { |
| 457 | EXPECT_FALSE(IsInt<1>(-2)); |
| 458 | EXPECT_TRUE(IsInt<1>(-1)); |
| 459 | EXPECT_TRUE(IsInt<1>(0)); |
| 460 | EXPECT_FALSE(IsInt<1>(1)); |
| 461 | |
| 462 | EXPECT_FALSE(IsInt<4>(-9)); |
| 463 | EXPECT_TRUE(IsInt<4>(-8)); |
| 464 | EXPECT_TRUE(IsInt<4>(7)); |
| 465 | EXPECT_FALSE(IsInt<4>(8)); |
| 466 | |
| 467 | EXPECT_FALSE(IsInt<32>(INT_MIN_minus1)); |
| 468 | EXPECT_TRUE(IsInt<32>(INT_MIN)); |
| 469 | EXPECT_TRUE(IsInt<32>(INT_MAX)); |
| 470 | EXPECT_FALSE(IsInt<32>(INT_MAX_plus1)); |
| 471 | } |
| 472 | |
| 473 | TEST_F(UtilsTest, IsUint) { |
| 474 | EXPECT_FALSE(IsUint<1>(-1)); |
| 475 | EXPECT_TRUE(IsUint<1>(0)); |
| 476 | EXPECT_TRUE(IsUint<1>(1)); |
| 477 | EXPECT_FALSE(IsUint<1>(2)); |
| 478 | |
| 479 | EXPECT_FALSE(IsUint<4>(-1)); |
| 480 | EXPECT_TRUE(IsUint<4>(0)); |
| 481 | EXPECT_TRUE(IsUint<4>(15)); |
| 482 | EXPECT_FALSE(IsUint<4>(16)); |
| 483 | |
| 484 | EXPECT_FALSE(IsUint<32>(-1)); |
| 485 | EXPECT_TRUE(IsUint<32>(0)); |
| 486 | EXPECT_TRUE(IsUint<32>(UINT_MAX)); |
| 487 | EXPECT_FALSE(IsUint<32>(UINT_MAX_plus1)); |
| 488 | } |
| 489 | |
| 490 | TEST_F(UtilsTest, IsAbsoluteUint) { |
| 491 | EXPECT_FALSE(IsAbsoluteUint<1>(-2)); |
| 492 | EXPECT_TRUE(IsAbsoluteUint<1>(-1)); |
| 493 | EXPECT_TRUE(IsAbsoluteUint<32>(0)); |
| 494 | EXPECT_TRUE(IsAbsoluteUint<1>(1)); |
| 495 | EXPECT_FALSE(IsAbsoluteUint<1>(2)); |
| 496 | |
| 497 | EXPECT_FALSE(IsAbsoluteUint<4>(-16)); |
| 498 | EXPECT_TRUE(IsAbsoluteUint<4>(-15)); |
| 499 | EXPECT_TRUE(IsAbsoluteUint<32>(0)); |
| 500 | EXPECT_TRUE(IsAbsoluteUint<4>(15)); |
| 501 | EXPECT_FALSE(IsAbsoluteUint<4>(16)); |
| 502 | |
| 503 | EXPECT_FALSE(IsAbsoluteUint<32>(-UINT_MAX_plus1)); |
| 504 | EXPECT_TRUE(IsAbsoluteUint<32>(-UINT_MAX)); |
| 505 | EXPECT_TRUE(IsAbsoluteUint<32>(0)); |
| 506 | EXPECT_TRUE(IsAbsoluteUint<32>(UINT_MAX)); |
| 507 | EXPECT_FALSE(IsAbsoluteUint<32>(UINT_MAX_plus1)); |
| 508 | } |
| 509 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 510 | } // namespace art |