blob: 0869b8942183d7b8a01b07668363ae7c298c27c7 [file] [log] [blame]
// Copyright 2011 Google Inc. All Rights Reserved.
#include "object.h"
#include "common_test.h"
#include "utils.h"
namespace art {
class UtilsTest : public CommonTest {
};
#define EXPECT_DESCRIPTOR(pretty_descriptor, descriptor) \
do { \
String* s = String::AllocFromModifiedUtf8(descriptor); \
std::string result(PrettyDescriptor(s)); \
EXPECT_EQ(pretty_descriptor, result); \
} while (false)
TEST_F(UtilsTest, PrettyDescriptor_ArrayReferences) {
EXPECT_DESCRIPTOR("java.lang.Class[]", "[Ljava/lang/Class;");
EXPECT_DESCRIPTOR("java.lang.Class[][]", "[[Ljava/lang/Class;");
}
TEST_F(UtilsTest, PrettyDescriptor_ScalarReferences) {
EXPECT_DESCRIPTOR("java.lang.String", "Ljava.lang.String;");
EXPECT_DESCRIPTOR("java.lang.String", "Ljava/lang/String;");
}
TEST_F(UtilsTest, PrettyDescriptor_PrimitiveArrays) {
EXPECT_DESCRIPTOR("boolean[]", "[Z");
EXPECT_DESCRIPTOR("boolean[][]", "[[Z");
EXPECT_DESCRIPTOR("byte[]", "[B");
EXPECT_DESCRIPTOR("byte[][]", "[[B");
EXPECT_DESCRIPTOR("char[]", "[C");
EXPECT_DESCRIPTOR("char[][]", "[[C");
EXPECT_DESCRIPTOR("double[]", "[D");
EXPECT_DESCRIPTOR("double[][]", "[[D");
EXPECT_DESCRIPTOR("float[]", "[F");
EXPECT_DESCRIPTOR("float[][]", "[[F");
EXPECT_DESCRIPTOR("int[]", "[I");
EXPECT_DESCRIPTOR("int[][]", "[[I");
EXPECT_DESCRIPTOR("long[]", "[J");
EXPECT_DESCRIPTOR("long[][]", "[[J");
EXPECT_DESCRIPTOR("short[]", "[S");
EXPECT_DESCRIPTOR("short[][]", "[[S");
}
TEST_F(UtilsTest, PrettyDescriptor_PrimitiveScalars) {
EXPECT_DESCRIPTOR("boolean", "Z");
EXPECT_DESCRIPTOR("byte", "B");
EXPECT_DESCRIPTOR("char", "C");
EXPECT_DESCRIPTOR("double", "D");
EXPECT_DESCRIPTOR("float", "F");
EXPECT_DESCRIPTOR("int", "I");
EXPECT_DESCRIPTOR("long", "J");
EXPECT_DESCRIPTOR("short", "S");
}
TEST_F(UtilsTest, PrettyTypeOf) {
EXPECT_EQ("null", PrettyTypeOf(NULL));
String* s = String::AllocFromModifiedUtf8("");
EXPECT_EQ("java.lang.String", PrettyTypeOf(s));
ShortArray* a = ShortArray::Alloc(2);
EXPECT_EQ("short[]", PrettyTypeOf(a));
Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
ASSERT_TRUE(c != NULL);
Object* o = ObjectArray<String>::Alloc(c, 0);
EXPECT_EQ("java.lang.String[]", PrettyTypeOf(o));
EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyTypeOf(o->GetClass()));
}
TEST_F(UtilsTest, PrettyClass) {
EXPECT_EQ("null", PrettyClass(NULL));
Class* c = class_linker_->FindSystemClass("[Ljava/lang/String;");
ASSERT_TRUE(c != NULL);
Object* o = ObjectArray<String>::Alloc(c, 0);
EXPECT_EQ("java.lang.Class<java.lang.String[]>", PrettyClass(o->GetClass()));
}
TEST_F(UtilsTest, PrettyField) {
EXPECT_EQ("null", PrettyField(NULL));
Class* java_lang_String = class_linker_->FindSystemClass("Ljava/lang/String;");
Class* int_class = class_linker_->FindPrimitiveClass('I');
Class* char_array_class = class_linker_->FindSystemClass("[C");
Field* f;
f = java_lang_String->FindDeclaredInstanceField("count", int_class);
EXPECT_EQ("int java.lang.String.count", PrettyField(f));
EXPECT_EQ("java.lang.String.count", PrettyField(f, false));
f = java_lang_String->FindDeclaredInstanceField("value", char_array_class);
EXPECT_EQ("char[] java.lang.String.value", PrettyField(f));
EXPECT_EQ("java.lang.String.value", PrettyField(f, false));
}
TEST_F(UtilsTest, MangleForJni) {
EXPECT_EQ("hello_00024world", MangleForJni("hello$world"));
EXPECT_EQ("hello_000a9world", MangleForJni("hello\xc2\xa9world"));
EXPECT_EQ("hello_1world", MangleForJni("hello_world"));
EXPECT_EQ("Ljava_lang_String_2", MangleForJni("Ljava/lang/String;"));
EXPECT_EQ("_3C", MangleForJni("[C"));
}
TEST_F(UtilsTest, JniShortName_JniLongName) {
Class* c = class_linker_->FindSystemClass("Ljava/lang/String;");
ASSERT_TRUE(c != NULL);
Method* m;
m = c->FindVirtualMethod("charAt", "(I)C");
ASSERT_TRUE(m != NULL);
EXPECT_EQ("Java_java_lang_String_charAt", JniShortName(m));
EXPECT_EQ("Java_java_lang_String_charAt__I", JniLongName(m));
m = c->FindVirtualMethod("indexOf", "(Ljava/lang/String;I)I");
ASSERT_TRUE(m != NULL);
EXPECT_EQ("Java_java_lang_String_indexOf", JniShortName(m));
EXPECT_EQ("Java_java_lang_String_indexOf__Ljava_lang_String_2I", JniLongName(m));
m = c->FindDirectMethod("copyValueOf", "([CII)Ljava/lang/String;");
ASSERT_TRUE(m != NULL);
EXPECT_EQ("Java_java_lang_String_copyValueOf", JniShortName(m));
EXPECT_EQ("Java_java_lang_String_copyValueOf___3CII", JniLongName(m));
}
TEST_F(UtilsTest, Split) {
std::vector<std::string> actual;
std::vector<std::string> expected;
expected.clear();
actual.clear();
Split("", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":", ':', actual);
EXPECT_EQ(expected, actual);
expected.clear();
expected.push_back("foo");
actual.clear();
Split(":foo", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split("foo:", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":foo:", ':', actual);
EXPECT_EQ(expected, actual);
expected.push_back("bar");
actual.clear();
Split("foo:bar", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":foo:bar", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split("foo:bar:", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":foo:bar:", ':', actual);
EXPECT_EQ(expected, actual);
expected.push_back("baz");
actual.clear();
Split("foo:bar:baz", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":foo:bar:baz", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split("foo:bar:baz:", ':', actual);
EXPECT_EQ(expected, actual);
actual.clear();
Split(":foo:bar:baz:", ':', actual);
EXPECT_EQ(expected, actual);
}
} // namespace art