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 | |
| 17 | #include "object.h" |
| 18 | #include "common_test.h" |
| 19 | #include "utils.h" |
| 20 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 21 | namespace art { |
| 22 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 23 | class UtilsTest : public CommonTest { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 24 | }; |
| 25 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 26 | #define EXPECT_DESCRIPTOR(pretty_descriptor, descriptor) \ |
| 27 | do { \ |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 28 | SirtRef<String> s(String::AllocFromModifiedUtf8(descriptor)); \ |
| 29 | std::string result(PrettyDescriptor(s.get())); \ |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 30 | EXPECT_EQ(pretty_descriptor, result); \ |
| 31 | } while (false) |
| 32 | |
| 33 | TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) { |
| 34 | EXPECT_DESCRIPTOR("java.lang.Class[]", "[Ljava/lang/Class;"); |
| 35 | EXPECT_DESCRIPTOR("java.lang.Class[][]", "[[Ljava/lang/Class;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 36 | } |
| 37 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 38 | TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) { |
| 39 | EXPECT_DESCRIPTOR("java.lang.String", "Ljava.lang.String;"); |
| 40 | EXPECT_DESCRIPTOR("java.lang.String", "Ljava/lang/String;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 43 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) { |
| 44 | EXPECT_DESCRIPTOR("boolean[]", "[Z"); |
| 45 | EXPECT_DESCRIPTOR("boolean[][]", "[[Z"); |
| 46 | EXPECT_DESCRIPTOR("byte[]", "[B"); |
| 47 | EXPECT_DESCRIPTOR("byte[][]", "[[B"); |
| 48 | EXPECT_DESCRIPTOR("char[]", "[C"); |
| 49 | EXPECT_DESCRIPTOR("char[][]", "[[C"); |
| 50 | EXPECT_DESCRIPTOR("double[]", "[D"); |
| 51 | EXPECT_DESCRIPTOR("double[][]", "[[D"); |
| 52 | EXPECT_DESCRIPTOR("float[]", "[F"); |
| 53 | EXPECT_DESCRIPTOR("float[][]", "[[F"); |
| 54 | EXPECT_DESCRIPTOR("int[]", "[I"); |
| 55 | EXPECT_DESCRIPTOR("int[][]", "[[I"); |
| 56 | EXPECT_DESCRIPTOR("long[]", "[J"); |
| 57 | EXPECT_DESCRIPTOR("long[][]", "[[J"); |
| 58 | EXPECT_DESCRIPTOR("short[]", "[S"); |
| 59 | EXPECT_DESCRIPTOR("short[][]", "[[S"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 60 | } |
| 61 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 62 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) { |
| 63 | EXPECT_DESCRIPTOR("boolean", "Z"); |
| 64 | EXPECT_DESCRIPTOR("byte", "B"); |
| 65 | EXPECT_DESCRIPTOR("char", "C"); |
| 66 | EXPECT_DESCRIPTOR("double", "D"); |
| 67 | EXPECT_DESCRIPTOR("float", "F"); |
| 68 | EXPECT_DESCRIPTOR("int", "I"); |
| 69 | EXPECT_DESCRIPTOR("long", "J"); |
| 70 | EXPECT_DESCRIPTOR("short", "S"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 73 | TEST_F(UtilsTest, PrettyTypeOf) { |
| 74 | EXPECT_EQ("null", PrettyTypeOf(NULL)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 75 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 76 | SirtRef<String> s(String::AllocFromModifiedUtf8("")); |
| 77 | EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 78 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 79 | SirtRef<ShortArray> a(ShortArray::Alloc(2)); |
| 80 | EXPECT_EQ("short[]", PrettyTypeOf(a.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 81 | |
| 82 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 83 | ASSERT_TRUE(c != NULL); |
| 84 | Object* o = ObjectArray<String>::Alloc(c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 85 | EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o)); |
| 86 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass())); |
| 87 | } |
| 88 | |
| 89 | TEST_F(UtilsTest, PrettyClass) { |
| 90 | EXPECT_EQ("null", PrettyClass(NULL)); |
| 91 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 92 | ASSERT_TRUE(c != NULL); |
| 93 | Object* o = ObjectArray<String>::Alloc(c, 0); |
| 94 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass())); |
| 95 | } |
| 96 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame] | 97 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
| 98 | EXPECT_EQ("null", PrettyClassAndClassLoader(NULL)); |
| 99 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 100 | ASSERT_TRUE(c != NULL); |
| 101 | Object* o = ObjectArray<String>::Alloc(c, 0); |
| 102 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass())); |
| 103 | } |
| 104 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 105 | TEST_F(UtilsTest, PrettyField) { |
| 106 | EXPECT_EQ("null", PrettyField(NULL)); |
| 107 | |
| 108 | Class* java_lang_String = class_linker_->FindSystemClass("Ljava/lang/String;"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 109 | |
| 110 | Field* f; |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 111 | f = java_lang_String->FindDeclaredInstanceField("count", "I"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 112 | EXPECT_EQ("int java.lang.String.count", PrettyField(f)); |
| 113 | EXPECT_EQ("java.lang.String.count", PrettyField(f, false)); |
Brian Carlstrom | 6b4ef02 | 2011-10-23 14:59:04 -0700 | [diff] [blame] | 114 | f = java_lang_String->FindDeclaredInstanceField("value", "[C"); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 115 | EXPECT_EQ("char[] java.lang.String.value", PrettyField(f)); |
| 116 | EXPECT_EQ("java.lang.String.value", PrettyField(f, false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Ian Rogers | 3bb17a6 | 2012-01-27 23:56:44 -0800 | [diff] [blame] | 119 | TEST_F(UtilsTest, PrettySize) { |
| 120 | EXPECT_EQ("1GB", PrettySize(1 * GB)); |
| 121 | EXPECT_EQ("2GB", PrettySize(2 * GB)); |
| 122 | if (sizeof(size_t) > sizeof(uint32_t)) { |
| 123 | EXPECT_EQ("100GB", PrettySize(100 * GB)); |
| 124 | } |
| 125 | EXPECT_EQ("1MB", PrettySize(1 * MB)); |
| 126 | EXPECT_EQ("10MB", PrettySize(10 * MB)); |
| 127 | EXPECT_EQ("100MB", PrettySize(100 * MB)); |
| 128 | EXPECT_EQ("1KiB", PrettySize(1 * KB)); |
| 129 | EXPECT_EQ("10KiB", PrettySize(10 * KB)); |
| 130 | EXPECT_EQ("100KiB", PrettySize(100 * KB)); |
| 131 | EXPECT_EQ("1B", PrettySize(1)); |
| 132 | EXPECT_EQ("10B", PrettySize(10)); |
| 133 | EXPECT_EQ("100B", PrettySize(100)); |
| 134 | } |
| 135 | |
| 136 | TEST_F(UtilsTest, PrettyDuration) { |
| 137 | const uint64_t one_sec = 1000000000; |
| 138 | const uint64_t one_ms = 1000000; |
| 139 | const uint64_t one_us = 1000; |
| 140 | |
| 141 | EXPECT_EQ("1s", PrettyDuration(1 * one_sec)); |
| 142 | EXPECT_EQ("10s", PrettyDuration(10 * one_sec)); |
| 143 | EXPECT_EQ("100s", PrettyDuration(100 * one_sec)); |
| 144 | EXPECT_EQ("1.001s", PrettyDuration(1 * one_sec + one_ms)); |
| 145 | EXPECT_EQ("1.000001s", PrettyDuration(1 * one_sec + one_us)); |
| 146 | EXPECT_EQ("1.000000001s", PrettyDuration(1 * one_sec + 1)); |
| 147 | |
| 148 | EXPECT_EQ("1ms", PrettyDuration(1 * one_ms)); |
| 149 | EXPECT_EQ("10ms", PrettyDuration(10 * one_ms)); |
| 150 | EXPECT_EQ("100ms", PrettyDuration(100 * one_ms)); |
| 151 | EXPECT_EQ("1.001ms", PrettyDuration(1 * one_ms + one_us)); |
| 152 | EXPECT_EQ("1.000001ms", PrettyDuration(1 * one_ms + 1)); |
| 153 | |
| 154 | EXPECT_EQ("1us", PrettyDuration(1 * one_us)); |
| 155 | EXPECT_EQ("10us", PrettyDuration(10 * one_us)); |
| 156 | EXPECT_EQ("100us", PrettyDuration(100 * one_us)); |
| 157 | EXPECT_EQ("1.001us", PrettyDuration(1 * one_us + 1)); |
| 158 | |
| 159 | EXPECT_EQ("1ns", PrettyDuration(1)); |
| 160 | EXPECT_EQ("10ns", PrettyDuration(10)); |
| 161 | EXPECT_EQ("100ns", PrettyDuration(100)); |
| 162 | } |
| 163 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 164 | TEST_F(UtilsTest, MangleForJni) { |
| 165 | EXPECT_EQ("hello_00024world", MangleForJni("hello$world")); |
| 166 | EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world")); |
| 167 | EXPECT_EQ("hello_1world", MangleForJni("hello_world")); |
| 168 | EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;")); |
| 169 | EXPECT_EQ("_3C", MangleForJni("[C")); |
| 170 | } |
| 171 | |
| 172 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
| 173 | Class* c = class_linker_->FindSystemClass("Ljava/lang/String;"); |
| 174 | ASSERT_TRUE(c != NULL); |
| 175 | Method* m; |
| 176 | |
| 177 | m = c->FindVirtualMethod("charAt", "(I)C"); |
| 178 | ASSERT_TRUE(m != NULL); |
| 179 | EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m)); |
| 180 | EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m)); |
| 181 | |
| 182 | m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I"); |
| 183 | ASSERT_TRUE(m != NULL); |
| 184 | EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m)); |
| 185 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m)); |
| 186 | |
| 187 | m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;"); |
| 188 | ASSERT_TRUE(m != NULL); |
| 189 | EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m)); |
| 190 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m)); |
| 191 | } |
| 192 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 193 | TEST_F(UtilsTest, Split) { |
| 194 | std::vector<std::string> actual; |
| 195 | std::vector<std::string> expected; |
| 196 | |
| 197 | expected.clear(); |
| 198 | |
| 199 | actual.clear(); |
| 200 | Split("", ':', actual); |
| 201 | EXPECT_EQ(expected, actual); |
| 202 | |
| 203 | actual.clear(); |
| 204 | Split(":", ':', actual); |
| 205 | EXPECT_EQ(expected, actual); |
| 206 | |
| 207 | expected.clear(); |
| 208 | expected.push_back("foo"); |
| 209 | |
| 210 | actual.clear(); |
| 211 | Split(":foo", ':', actual); |
| 212 | EXPECT_EQ(expected, actual); |
| 213 | |
| 214 | actual.clear(); |
| 215 | Split("foo:", ':', actual); |
| 216 | EXPECT_EQ(expected, actual); |
| 217 | |
| 218 | actual.clear(); |
| 219 | Split(":foo:", ':', actual); |
| 220 | EXPECT_EQ(expected, actual); |
| 221 | |
| 222 | expected.push_back("bar"); |
| 223 | |
| 224 | actual.clear(); |
| 225 | Split("foo:bar", ':', actual); |
| 226 | EXPECT_EQ(expected, actual); |
| 227 | |
| 228 | actual.clear(); |
| 229 | Split(":foo:bar", ':', actual); |
| 230 | EXPECT_EQ(expected, actual); |
| 231 | |
| 232 | actual.clear(); |
| 233 | Split("foo:bar:", ':', actual); |
| 234 | EXPECT_EQ(expected, actual); |
| 235 | |
| 236 | actual.clear(); |
| 237 | Split(":foo:bar:", ':', actual); |
| 238 | EXPECT_EQ(expected, actual); |
| 239 | |
| 240 | expected.push_back("baz"); |
| 241 | |
| 242 | actual.clear(); |
| 243 | Split("foo:bar:baz", ':', actual); |
| 244 | EXPECT_EQ(expected, actual); |
| 245 | |
| 246 | actual.clear(); |
| 247 | Split(":foo:bar:baz", ':', actual); |
| 248 | EXPECT_EQ(expected, actual); |
| 249 | |
| 250 | actual.clear(); |
| 251 | Split("foo:bar:baz:", ':', actual); |
| 252 | EXPECT_EQ(expected, actual); |
| 253 | |
| 254 | actual.clear(); |
| 255 | Split(":foo:bar:baz:", ':', actual); |
| 256 | EXPECT_EQ(expected, actual); |
| 257 | } |
| 258 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 259 | } // namespace art |