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" |
Ian Rogers | 1f53934 | 2012-10-03 21:09:42 -0700 | [diff] [blame] | 26 | #include "sirt_ref.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 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 98 | SirtRef<mirror::String> s(soa.Self(), mirror::String::AllocFromModifiedUtf8(soa.Self(), "")); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 99 | EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 100 | |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 101 | SirtRef<mirror::ShortArray> a(soa.Self(), mirror::ShortArray::Alloc(soa.Self(), 2)); |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 102 | EXPECT_EQ("short[]", PrettyTypeOf(a.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 103 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 104 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 105 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 106 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 107 | EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o)); |
| 108 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass())); |
| 109 | } |
| 110 | |
| 111 | TEST_F(UtilsTest, PrettyClass) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 112 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 113 | EXPECT_EQ("null", PrettyClass(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 114 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 115 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 116 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 117 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass())); |
| 118 | } |
| 119 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 120 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 121 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 122 | EXPECT_EQ("null", PrettyClassAndClassLoader(NULL)); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 123 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "[Ljava/lang/String;"); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 124 | ASSERT_TRUE(c != NULL); |
Ian Rogers | 2dd0e2c | 2013-01-24 12:42:14 -0800 | [diff] [blame] | 125 | mirror::Object* o = mirror::ObjectArray<mirror::String>::Alloc(soa.Self(), c, 0); |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 126 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass())); |
| 127 | } |
| 128 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 129 | TEST_F(UtilsTest, PrettyField) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 130 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 131 | EXPECT_EQ("null", PrettyField(NULL)); |
| 132 | |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 133 | mirror::Class* java_lang_String = class_linker_->FindSystemClass(soa.Self(), |
| 134 | "Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 135 | |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 136 | mirror::ArtField* f; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 137 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 138 | EXPECT_EQ("int java.lang.String.count", PrettyField(f)); |
| 139 | EXPECT_EQ("java.lang.String.count", PrettyField(f, false)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 140 | f = java_lang_String->FindDeclaredInstanceField("value", "[C"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 141 | EXPECT_EQ("char[] java.lang.String.value", PrettyField(f)); |
| 142 | EXPECT_EQ("java.lang.String.value", PrettyField(f, false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 145 | TEST_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 Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 151 | EXPECT_EQ("1024KB", PrettySize(1 * MB)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 152 | EXPECT_EQ("10MB", PrettySize(10 * MB)); |
| 153 | EXPECT_EQ("100MB", PrettySize(100 * MB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 154 | EXPECT_EQ("1024B", PrettySize(1 * KB)); |
Elliott Hughes | 409d273 | 2012-04-03 13:34:44 -0700 | [diff] [blame] | 155 | EXPECT_EQ("10KB", PrettySize(10 * KB)); |
| 156 | EXPECT_EQ("100KB", PrettySize(100 * KB)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 157 | EXPECT_EQ("0B", PrettySize(0)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 158 | EXPECT_EQ("1B", PrettySize(1)); |
| 159 | EXPECT_EQ("10B", PrettySize(10)); |
| 160 | EXPECT_EQ("100B", PrettySize(100)); |
Elliott Hughes | c967f78 | 2012-04-16 10:23:15 -0700 | [diff] [blame] | 161 | EXPECT_EQ("512B", PrettySize(512)); |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | TEST_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 Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 192 | TEST_F(UtilsTest, MangleForJni) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 193 | ScopedObjectAccess soa(Thread::Current()); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 194 | 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 | |
| 201 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
Ian Rogers | 00f7d0e | 2012-07-19 15:28:27 -0700 | [diff] [blame] | 202 | ScopedObjectAccess soa(Thread::Current()); |
Ian Rogers | 9837939 | 2014-02-24 16:53:16 -0800 | [diff] [blame] | 203 | mirror::Class* c = class_linker_->FindSystemClass(soa.Self(), "Ljava/lang/String;"); |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 204 | ASSERT_TRUE(c != NULL); |
Brian Carlstrom | ea46f95 | 2013-07-30 01:26:50 -0700 | [diff] [blame] | 205 | mirror::ArtMethod* m; |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 206 | |
| 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 Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 223 | TEST_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 Hughes | 48436bb | 2012-02-07 15:23:28 -0800 | [diff] [blame] | 289 | TEST_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 Carlstrom | 7a967b3 | 2012-03-28 15:23:10 -0700 | [diff] [blame] | 327 | TEST_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 | |
| 334 | TEST_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 Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 341 | TEST_F(UtilsTest, GetDalvikCacheFilenameOrDie) { |
Narayan Kamath | 11d9f06 | 2014-04-23 20:24:57 +0100 | [diff] [blame] | 342 | 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 Carlstrom | 19a0836 | 2013-10-16 14:37:22 -0700 | [diff] [blame] | 351 | } |
| 352 | |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 353 | TEST_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 Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 361 | 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 Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 365 | EXPECT_EQ(0U, error_msg.size()) << error_msg; |
| 366 | } |
| 367 | |
Brian Carlstrom | a1ce1fe | 2014-02-24 23:23:58 -0800 | [diff] [blame] | 368 | TEST_F(UtilsTest, ExecError) { |
Brian Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 369 | std::vector<std::string> command; |
| 370 | command.push_back("bogus"); |
| 371 | std::string error_msg; |
Mathieu Chartier | 661974a | 2014-01-09 11:23:53 -0800 | [diff] [blame] | 372 | 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 Carlstrom | 6449c62 | 2014-02-10 23:48:36 -0800 | [diff] [blame] | 377 | } |
| 378 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 379 | } // namespace art |