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 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 47 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 48 | 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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 66 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) { |
Elliott Hughes | 9058f2b | 2012-03-22 18:06:48 -0700 | [diff] [blame] | 67 | 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 | |
| 77 | TEST_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 | |
| 86 | TEST_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 Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 94 | TEST_F(UtilsTest, PrettyTypeOf) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 95 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 96 | EXPECT_EQ("null", PrettyTypeOf(NULL)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 97 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 98 | StackHandleScope<2> hs(soa.Self()); |
| 99 | Handle<mirror::String> s(hs.NewHandle(mirror::String::AllocFromModifiedUtf8(soa.Self(), ""))); |
| 100 | EXPECT_EQ("java.lang.String", PrettyTypeOf(s.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 101 | |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 102 | Handle<mirror::ShortArray> a(hs.NewHandle(mirror::ShortArray::Alloc(soa.Self(), 2))); |
| 103 | EXPECT_EQ("short[]", PrettyTypeOf(a.Get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 104 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 105 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 106 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 107 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 108 | EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o)); |
| 109 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass())); |
| 110 | } |
| 111 | |
| 112 | TEST_F(UtilsTest, PrettyClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 113 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 114 | EXPECT_EQ("null", PrettyClass(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 115 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 116 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 117 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 118 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass())); |
| 119 | } |
| 120 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 121 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 122 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 123 | EXPECT_EQ("null", PrettyClassAndClassLoader(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 124 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 125 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 126 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 127 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass())); |
| 128 | } |
| 129 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 130 | TEST_F(UtilsTest, PrettyField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 131 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 132 | EXPECT_EQ("null", PrettyField(NULL)); |
| 133 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 134 | mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(), |
| 135 | "Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 136 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 137 | mirror::ArtField* f; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 138 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 139 | EXPECT_EQ("int java.lang.String.count", PrettyField(f)); |
| 140 | EXPECT_EQ("java.lang.String.count", PrettyField(f, false)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 141 | f = java_lang_String->FindDeclaredInstanceField("value", "[C"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 142 | EXPECT_EQ("char[] java.lang.String.value", PrettyField(f)); |
| 143 | EXPECT_EQ("java.lang.String.value", PrettyField(f, false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 146 | TEST_F(UtilsTest, PrettySize) { |
| 147 | EXPECT_EQ("1GB", PrettySize(1 * GB)); |
| 148 | EXPECT_EQ("2GB", PrettySize(2 * GB)); |
| 149 | if (sizeof(size_t) > sizeof(uint32_t)) { |
| 150 | EXPECT_EQ("100GB", PrettySize(100 * GB)); |
| 151 | } |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 152 | EXPECT_EQ("1024KB", PrettySize(1 * MB)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 153 | EXPECT_EQ("10MB", PrettySize(10 * MB)); |
| 154 | EXPECT_EQ("100MB", PrettySize(100 * MB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 155 | EXPECT_EQ("1024B", PrettySize(1 * KB)); |
Elliott Hughes | 409d273 | 2012-04-03 13:34:44 -0700 | [diff] [blame] | 156 | EXPECT_EQ("10KB", PrettySize(10 * KB)); |
| 157 | EXPECT_EQ("100KB", PrettySize(100 * KB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 158 | EXPECT_EQ("0B", PrettySize(0)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 159 | EXPECT_EQ("1B", PrettySize(1)); |
| 160 | EXPECT_EQ("10B", PrettySize(10)); |
| 161 | EXPECT_EQ("100B", PrettySize(100)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 162 | EXPECT_EQ("512B", PrettySize(512)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | TEST_F(UtilsTest, PrettyDuration) { |
| 166 | const uint64_t one_sec = 1000000000; |
| 167 | const uint64_t one_ms = 1000000; |
| 168 | const uint64_t one_us = 1000; |
| 169 | |
| 170 | EXPECT_EQ("1s", PrettyDuration(1 * one_sec)); |
| 171 | EXPECT_EQ("10s", PrettyDuration(10 * one_sec)); |
| 172 | EXPECT_EQ("100s", PrettyDuration(100 * one_sec)); |
| 173 | EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms)); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame^] | 174 | EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us, 6)); |
| 175 | EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1, 9)); |
| 176 | EXPECT_EQ("1.000s", PrettyDuration(1 * one_sec + one_us, 3)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 177 | |
| 178 | EXPECT_EQ("1ms", PrettyDuration(1 * one_ms)); |
| 179 | EXPECT_EQ("10ms", PrettyDuration(10 * one_ms)); |
| 180 | EXPECT_EQ("100ms", PrettyDuration(100 * one_ms)); |
| 181 | EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us)); |
Mathieu Chartier | f5997b4 | 2014-06-20 10:37:54 -0700 | [diff] [blame^] | 182 | EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1, 6)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 183 | |
| 184 | EXPECT_EQ("1us", PrettyDuration(1 * one_us)); |
| 185 | EXPECT_EQ("10us", PrettyDuration(10 * one_us)); |
| 186 | EXPECT_EQ("100us", PrettyDuration(100 * one_us)); |
| 187 | EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1)); |
| 188 | |
| 189 | EXPECT_EQ("1ns", PrettyDuration(1)); |
| 190 | EXPECT_EQ("10ns", PrettyDuration(10)); |
| 191 | EXPECT_EQ("100ns", PrettyDuration(100)); |
| 192 | } |
| 193 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 194 | TEST_F(UtilsTest, MangleForJni) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 195 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 196 | EXPECT_EQ("hello_00024world", MangleForJni("hello$world")); |
| 197 | EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world")); |
| 198 | EXPECT_EQ("hello_1world", MangleForJni("hello_world")); |
| 199 | EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;")); |
| 200 | EXPECT_EQ("_3C", MangleForJni("[C")); |
| 201 | } |
| 202 | |
| 203 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 204 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 205 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 206 | ASSERT_TRUE(c != NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 207 | mirror::ArtMethod* m; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 208 | |
| 209 | m = c->FindVirtualMethod("charAt", "(I)C"); |
| 210 | ASSERT_TRUE(m != NULL); |
| 211 | EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m)); |
| 212 | EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m)); |
| 213 | |
| 214 | m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I"); |
| 215 | ASSERT_TRUE(m != NULL); |
| 216 | EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m)); |
| 217 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m)); |
| 218 | |
| 219 | m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;"); |
| 220 | ASSERT_TRUE(m != NULL); |
| 221 | EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m)); |
| 222 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m)); |
| 223 | } |
| 224 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 225 | TEST_F(UtilsTest, Split) { |
| 226 | std::vector<std::string> actual; |
| 227 | std::vector<std::string> expected; |
| 228 | |
| 229 | expected.clear(); |
| 230 | |
| 231 | actual.clear(); |
| 232 | Split("", ':', actual); |
| 233 | EXPECT_EQ(expected, actual); |
| 234 | |
| 235 | actual.clear(); |
| 236 | Split(":", ':', actual); |
| 237 | EXPECT_EQ(expected, actual); |
| 238 | |
| 239 | expected.clear(); |
| 240 | expected.push_back("foo"); |
| 241 | |
| 242 | actual.clear(); |
| 243 | Split(":foo", ':', actual); |
| 244 | EXPECT_EQ(expected, actual); |
| 245 | |
| 246 | actual.clear(); |
| 247 | Split("foo:", ':', actual); |
| 248 | EXPECT_EQ(expected, actual); |
| 249 | |
| 250 | actual.clear(); |
| 251 | Split(":foo:", ':', actual); |
| 252 | EXPECT_EQ(expected, actual); |
| 253 | |
| 254 | expected.push_back("bar"); |
| 255 | |
| 256 | actual.clear(); |
| 257 | Split("foo:bar", ':', actual); |
| 258 | EXPECT_EQ(expected, actual); |
| 259 | |
| 260 | actual.clear(); |
| 261 | Split(":foo:bar", ':', actual); |
| 262 | EXPECT_EQ(expected, actual); |
| 263 | |
| 264 | actual.clear(); |
| 265 | Split("foo:bar:", ':', actual); |
| 266 | EXPECT_EQ(expected, actual); |
| 267 | |
| 268 | actual.clear(); |
| 269 | Split(":foo:bar:", ':', actual); |
| 270 | EXPECT_EQ(expected, actual); |
| 271 | |
| 272 | expected.push_back("baz"); |
| 273 | |
| 274 | actual.clear(); |
| 275 | Split("foo:bar:baz", ':', actual); |
| 276 | EXPECT_EQ(expected, actual); |
| 277 | |
| 278 | actual.clear(); |
| 279 | Split(":foo:bar:baz", ':', actual); |
| 280 | EXPECT_EQ(expected, actual); |
| 281 | |
| 282 | actual.clear(); |
| 283 | Split("foo:bar:baz:", ':', actual); |
| 284 | EXPECT_EQ(expected, actual); |
| 285 | |
| 286 | actual.clear(); |
| 287 | Split(":foo:bar:baz:", ':', actual); |
| 288 | EXPECT_EQ(expected, actual); |
| 289 | } |
| 290 | |
Elliott Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 291 | TEST_F(UtilsTest, Join) { |
| 292 | std::vector<std::string> strings; |
| 293 | |
| 294 | strings.clear(); |
| 295 | EXPECT_EQ("", Join(strings, ':')); |
| 296 | |
| 297 | strings.clear(); |
| 298 | strings.push_back("foo"); |
| 299 | EXPECT_EQ("foo", Join(strings, ':')); |
| 300 | |
| 301 | strings.clear(); |
| 302 | strings.push_back(""); |
| 303 | strings.push_back("foo"); |
| 304 | EXPECT_EQ(":foo", Join(strings, ':')); |
| 305 | |
| 306 | strings.clear(); |
| 307 | strings.push_back("foo"); |
| 308 | strings.push_back(""); |
| 309 | EXPECT_EQ("foo:", Join(strings, ':')); |
| 310 | |
| 311 | strings.clear(); |
| 312 | strings.push_back(""); |
| 313 | strings.push_back("foo"); |
| 314 | strings.push_back(""); |
| 315 | EXPECT_EQ(":foo:", Join(strings, ':')); |
| 316 | |
| 317 | strings.clear(); |
| 318 | strings.push_back("foo"); |
| 319 | strings.push_back("bar"); |
| 320 | EXPECT_EQ("foo:bar", Join(strings, ':')); |
| 321 | |
| 322 | strings.clear(); |
| 323 | strings.push_back("foo"); |
| 324 | strings.push_back("bar"); |
| 325 | strings.push_back("baz"); |
| 326 | EXPECT_EQ("foo:bar:baz", Join(strings, ':')); |
| 327 | } |
| 328 | |
Brian Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 329 | TEST_F(UtilsTest, StartsWith) { |
| 330 | EXPECT_FALSE(StartsWith("foo", "bar")); |
| 331 | EXPECT_TRUE(StartsWith("foo", "foo")); |
| 332 | EXPECT_TRUE(StartsWith("food", "foo")); |
| 333 | EXPECT_FALSE(StartsWith("fo", "foo")); |
| 334 | } |
| 335 | |
| 336 | TEST_F(UtilsTest, EndsWith) { |
| 337 | EXPECT_FALSE(EndsWith("foo", "bar")); |
| 338 | EXPECT_TRUE(EndsWith("foo", "foo")); |
| 339 | EXPECT_TRUE(EndsWith("foofoo", "foo")); |
| 340 | EXPECT_FALSE(EndsWith("oo", "foo")); |
| 341 | } |
| 342 | |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 343 | TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 344 | EXPECT_STREQ("/foo/system@app@Foo.apk@classes.dex", |
| 345 | GetDalvikCacheFilenameOrDie("/system/app/Foo.apk", "/foo").c_str()); |
| 346 | |
| 347 | EXPECT_STREQ("/foo/data@app@foo-1.apk@classes.dex", |
| 348 | GetDalvikCacheFilenameOrDie("/data/app/foo-1.apk", "/foo").c_str()); |
| 349 | EXPECT_STREQ("/foo/system@framework@core.jar@classes.dex", |
| 350 | GetDalvikCacheFilenameOrDie("/system/framework/core.jar", "/foo").c_str()); |
| 351 | EXPECT_STREQ("/foo/system@framework@boot.art", |
| 352 | GetDalvikCacheFilenameOrDie("/system/framework/boot.art", "/foo").c_str()); |
Brian Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 353 | } |
| 354 | |
Brian Carlstrom | 0e12bdc | 2014-05-14 17:44:28 -0700 | [diff] [blame] | 355 | TEST_F(UtilsTest, GetSystemImageFilename) { |
| 356 | EXPECT_STREQ("/system/framework/arm/boot.art", |
| 357 | GetSystemImageFilename("/system/framework/boot.art", kArm).c_str()); |
| 358 | } |
| 359 | |
| 360 | TEST_F(UtilsTest, DexFilenameToOdexFilename) { |
| 361 | EXPECT_STREQ("/foo/bar/arm/baz.odex", |
| 362 | DexFilenameToOdexFilename("/foo/bar/baz.jar", kArm).c_str()); |
| 363 | } |
| 364 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 365 | TEST_F(UtilsTest, ExecSuccess) { |
| 366 | std::vector<std::string> command; |
| 367 | if (kIsTargetBuild) { |
| 368 | command.push_back("/system/bin/id"); |
| 369 | } else { |
| 370 | command.push_back("/usr/bin/id"); |
| 371 | } |
| 372 | std::string error_msg; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 373 | if (RUNNING_ON_VALGRIND == 0) { |
| 374 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 375 | EXPECT_TRUE(Exec(command, &error_msg)); |
| 376 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 377 | EXPECT_EQ(0U, error_msg.size()) << error_msg; |
| 378 | } |
| 379 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 380 | TEST_F(UtilsTest, ExecError) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 381 | std::vector<std::string> command; |
| 382 | command.push_back("bogus"); |
| 383 | std::string error_msg; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 384 | if (RUNNING_ON_VALGRIND == 0) { |
| 385 | // Running on valgrind fails due to some memory that leaks in thread alternate signal stacks. |
| 386 | EXPECT_FALSE(Exec(command, &error_msg)); |
| 387 | EXPECT_NE(0U, error_msg.size()); |
| 388 | } |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 389 | } |
| 390 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 391 | } // namespace art |