Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | |
| 3 | #include "object.h" |
| 4 | #include "common_test.h" |
| 5 | #include "utils.h" |
| 6 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 7 | namespace art { |
| 8 | |
Brian Carlstrom | f734cf5 | 2011-08-17 16:28:14 -0700 | [diff] [blame] | 9 | class UtilsTest : public CommonTest { |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 10 | }; |
| 11 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 12 | #define EXPECT_DESCRIPTOR(pretty_descriptor, descriptor) \ |
| 13 | do { \ |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 14 | SirtRef<String> s(String::AllocFromModifiedUtf8(descriptor)); \ |
| 15 | std::string result(PrettyDescriptor(s.get())); \ |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 16 | EXPECT_EQ(pretty_descriptor, result); \ |
| 17 | } while (false) |
| 18 | |
| 19 | TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) { |
| 20 | EXPECT_DESCRIPTOR("java.lang.Class[]", "[Ljava/lang/Class;"); |
| 21 | EXPECT_DESCRIPTOR("java.lang.Class[][]", "[[Ljava/lang/Class;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 22 | } |
| 23 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 24 | TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) { |
| 25 | EXPECT_DESCRIPTOR("java.lang.String", "Ljava.lang.String;"); |
| 26 | EXPECT_DESCRIPTOR("java.lang.String", "Ljava/lang/String;"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 27 | } |
| 28 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 29 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) { |
| 30 | EXPECT_DESCRIPTOR("boolean[]", "[Z"); |
| 31 | EXPECT_DESCRIPTOR("boolean[][]", "[[Z"); |
| 32 | EXPECT_DESCRIPTOR("byte[]", "[B"); |
| 33 | EXPECT_DESCRIPTOR("byte[][]", "[[B"); |
| 34 | EXPECT_DESCRIPTOR("char[]", "[C"); |
| 35 | EXPECT_DESCRIPTOR("char[][]", "[[C"); |
| 36 | EXPECT_DESCRIPTOR("double[]", "[D"); |
| 37 | EXPECT_DESCRIPTOR("double[][]", "[[D"); |
| 38 | EXPECT_DESCRIPTOR("float[]", "[F"); |
| 39 | EXPECT_DESCRIPTOR("float[][]", "[[F"); |
| 40 | EXPECT_DESCRIPTOR("int[]", "[I"); |
| 41 | EXPECT_DESCRIPTOR("int[][]", "[[I"); |
| 42 | EXPECT_DESCRIPTOR("long[]", "[J"); |
| 43 | EXPECT_DESCRIPTOR("long[][]", "[[J"); |
| 44 | EXPECT_DESCRIPTOR("short[]", "[S"); |
| 45 | EXPECT_DESCRIPTOR("short[][]", "[[S"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Elliott Hughes | 5174fe6 | 2011-08-23 15:12:35 -0700 | [diff] [blame] | 48 | TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) { |
| 49 | EXPECT_DESCRIPTOR("boolean", "Z"); |
| 50 | EXPECT_DESCRIPTOR("byte", "B"); |
| 51 | EXPECT_DESCRIPTOR("char", "C"); |
| 52 | EXPECT_DESCRIPTOR("double", "D"); |
| 53 | EXPECT_DESCRIPTOR("float", "F"); |
| 54 | EXPECT_DESCRIPTOR("int", "I"); |
| 55 | EXPECT_DESCRIPTOR("long", "J"); |
| 56 | EXPECT_DESCRIPTOR("short", "S"); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 57 | } |
| 58 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 59 | TEST_F(UtilsTest, PrettyTypeOf) { |
| 60 | EXPECT_EQ("null", PrettyTypeOf(NULL)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 61 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 62 | SirtRef<String> s(String::AllocFromModifiedUtf8("")); |
| 63 | EXPECT_EQ("java.lang.String", PrettyTypeOf(s.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 64 | |
Brian Carlstrom | 40381fb | 2011-10-19 14:13:40 -0700 | [diff] [blame] | 65 | SirtRef<ShortArray> a(ShortArray::Alloc(2)); |
| 66 | EXPECT_EQ("short[]", PrettyTypeOf(a.get())); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 67 | |
| 68 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 69 | ASSERT_TRUE(c != NULL); |
| 70 | Object* o = ObjectArray<String>::Alloc(c, 0); |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 71 | EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o)); |
| 72 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass())); |
| 73 | } |
| 74 | |
| 75 | TEST_F(UtilsTest, PrettyClass) { |
| 76 | EXPECT_EQ("null", PrettyClass(NULL)); |
| 77 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 78 | ASSERT_TRUE(c != NULL); |
| 79 | Object* o = ObjectArray<String>::Alloc(c, 0); |
| 80 | EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass())); |
| 81 | } |
| 82 | |
Ian Rogers | d81871c | 2011-10-03 13:57:23 -0700 | [diff] [blame^] | 83 | TEST_F(UtilsTest, PrettyClassAndClassLoader) { |
| 84 | EXPECT_EQ("null", PrettyClassAndClassLoader(NULL)); |
| 85 | Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;"); |
| 86 | ASSERT_TRUE(c != NULL); |
| 87 | Object* o = ObjectArray<String>::Alloc(c, 0); |
| 88 | EXPECT_EQ("java.lang.Class<java.lang.String[],null>", PrettyClassAndClassLoader(o->GetClass())); |
| 89 | } |
| 90 | |
Elliott Hughes | 54e7df1 | 2011-09-16 11:47:04 -0700 | [diff] [blame] | 91 | TEST_F(UtilsTest, PrettyField) { |
| 92 | EXPECT_EQ("null", PrettyField(NULL)); |
| 93 | |
| 94 | Class* java_lang_String = class_linker_->FindSystemClass("Ljava/lang/String;"); |
| 95 | Class* int_class = class_linker_->FindPrimitiveClass('I'); |
| 96 | Class* char_array_class = class_linker_->FindSystemClass("[C"); |
| 97 | |
| 98 | Field* f; |
| 99 | f = java_lang_String->FindDeclaredInstanceField("count", int_class); |
| 100 | EXPECT_EQ("int java.lang.String.count", PrettyField(f)); |
| 101 | EXPECT_EQ("java.lang.String.count", PrettyField(f, false)); |
| 102 | f = java_lang_String->FindDeclaredInstanceField("value", char_array_class); |
| 103 | EXPECT_EQ("char[] java.lang.String.value", PrettyField(f)); |
| 104 | EXPECT_EQ("java.lang.String.value", PrettyField(f, false)); |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Elliott Hughes | 79082e3 | 2011-08-25 12:07:32 -0700 | [diff] [blame] | 107 | TEST_F(UtilsTest, MangleForJni) { |
| 108 | EXPECT_EQ("hello_00024world", MangleForJni("hello$world")); |
| 109 | EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world")); |
| 110 | EXPECT_EQ("hello_1world", MangleForJni("hello_world")); |
| 111 | EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;")); |
| 112 | EXPECT_EQ("_3C", MangleForJni("[C")); |
| 113 | } |
| 114 | |
| 115 | TEST_F(UtilsTest, JniShortName_JniLongName) { |
| 116 | Class* c = class_linker_->FindSystemClass("Ljava/lang/String;"); |
| 117 | ASSERT_TRUE(c != NULL); |
| 118 | Method* m; |
| 119 | |
| 120 | m = c->FindVirtualMethod("charAt", "(I)C"); |
| 121 | ASSERT_TRUE(m != NULL); |
| 122 | EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m)); |
| 123 | EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m)); |
| 124 | |
| 125 | m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I"); |
| 126 | ASSERT_TRUE(m != NULL); |
| 127 | EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m)); |
| 128 | EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m)); |
| 129 | |
| 130 | m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;"); |
| 131 | ASSERT_TRUE(m != NULL); |
| 132 | EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m)); |
| 133 | EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m)); |
| 134 | } |
| 135 | |
Elliott Hughes | 3402380 | 2011-08-30 12:06:17 -0700 | [diff] [blame] | 136 | TEST_F(UtilsTest, Split) { |
| 137 | std::vector<std::string> actual; |
| 138 | std::vector<std::string> expected; |
| 139 | |
| 140 | expected.clear(); |
| 141 | |
| 142 | actual.clear(); |
| 143 | Split("", ':', actual); |
| 144 | EXPECT_EQ(expected, actual); |
| 145 | |
| 146 | actual.clear(); |
| 147 | Split(":", ':', actual); |
| 148 | EXPECT_EQ(expected, actual); |
| 149 | |
| 150 | expected.clear(); |
| 151 | expected.push_back("foo"); |
| 152 | |
| 153 | actual.clear(); |
| 154 | Split(":foo", ':', actual); |
| 155 | EXPECT_EQ(expected, actual); |
| 156 | |
| 157 | actual.clear(); |
| 158 | Split("foo:", ':', actual); |
| 159 | EXPECT_EQ(expected, actual); |
| 160 | |
| 161 | actual.clear(); |
| 162 | Split(":foo:", ':', actual); |
| 163 | EXPECT_EQ(expected, actual); |
| 164 | |
| 165 | expected.push_back("bar"); |
| 166 | |
| 167 | actual.clear(); |
| 168 | Split("foo:bar", ':', actual); |
| 169 | EXPECT_EQ(expected, actual); |
| 170 | |
| 171 | actual.clear(); |
| 172 | Split(":foo:bar", ':', actual); |
| 173 | EXPECT_EQ(expected, actual); |
| 174 | |
| 175 | actual.clear(); |
| 176 | Split("foo:bar:", ':', actual); |
| 177 | EXPECT_EQ(expected, actual); |
| 178 | |
| 179 | actual.clear(); |
| 180 | Split(":foo:bar:", ':', actual); |
| 181 | EXPECT_EQ(expected, actual); |
| 182 | |
| 183 | expected.push_back("baz"); |
| 184 | |
| 185 | actual.clear(); |
| 186 | Split("foo:bar:baz", ':', actual); |
| 187 | EXPECT_EQ(expected, actual); |
| 188 | |
| 189 | actual.clear(); |
| 190 | Split(":foo:bar:baz", ':', actual); |
| 191 | EXPECT_EQ(expected, actual); |
| 192 | |
| 193 | actual.clear(); |
| 194 | Split("foo:bar:baz:", ':', actual); |
| 195 | EXPECT_EQ(expected, actual); |
| 196 | |
| 197 | actual.clear(); |
| 198 | Split(":foo:bar:baz:", ':', actual); |
| 199 | EXPECT_EQ(expected, actual); |
| 200 | } |
| 201 | |
Elliott Hughes | 11e4507 | 2011-08-16 17:40:46 -0700 | [diff] [blame] | 202 | } // namespace art |