Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 1 | // Copyright 2011 Google Inc. All Rights Reserved. |
| 2 | // Author: cshapiro@google.com (Carl Shapiro) |
| 3 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 4 | #include "class_linker.h" |
| 5 | #include "common_test.h" |
| 6 | #include "dex_file.h" |
| 7 | #include "heap.h" |
| 8 | #include "object.h" |
| 9 | #include "scoped_ptr.h" |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 10 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 11 | #include <stdint.h> |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include "gtest/gtest.h" |
| 14 | |
| 15 | namespace art { |
| 16 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 17 | class ObjectTest : public RuntimeTest { |
| 18 | protected: |
| 19 | void AssertString(size_t length, |
| 20 | const char* utf8_in, |
| 21 | const char* utf16_expected_le, |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 22 | int32_t hash_expected) { |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 23 | uint16_t utf16_expected[length]; |
| 24 | for (size_t i = 0; i < length; i++) { |
| 25 | uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) | |
| 26 | ((utf16_expected_le[i*2 + 1] & 0xff) << 0)); |
| 27 | utf16_expected[i] = ch; |
| 28 | } |
| 29 | |
Jesse Wilson | 8989d99 | 2011-08-02 13:39:42 -0700 | [diff] [blame] | 30 | String* string = String::AllocFromModifiedUtf8(length, utf8_in); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 31 | ASSERT_EQ(length, static_cast<size_t>(string->count_)); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 32 | ASSERT_TRUE(string->array_ != NULL); |
| 33 | ASSERT_TRUE(string->array_->GetChars() != NULL); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 34 | // strlen is necessary because the 1-character string "\0" is interpreted as "" |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 35 | ASSERT_TRUE(String::EqualsUtf8(string, utf8_in) || length != strlen(utf8_in)); |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 36 | for (size_t i = 0; i < length; i++) { |
| 37 | EXPECT_EQ(utf16_expected[i], string->array_->GetChar(i)); |
| 38 | } |
| 39 | EXPECT_EQ(hash_expected, string->hash_code_); |
| 40 | } |
| 41 | }; |
Brian Carlstrom | a331b3c | 2011-07-18 17:47:56 -0700 | [diff] [blame] | 42 | |
| 43 | TEST_F(ObjectTest, IsInSamePackage) { |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 44 | // Matches |
| 45 | EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;", |
| 46 | "Ljava/lang/Class")); |
| 47 | EXPECT_TRUE(Class::IsInSamePackage("LFoo;", |
| 48 | "LBar;")); |
| 49 | |
| 50 | // Mismatches |
| 51 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", |
| 52 | "Ljava/io/File;")); |
| 53 | EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;", |
| 54 | "Ljava/lang/reflect/Method;")); |
| 55 | } |
| 56 | |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 57 | TEST_F(ObjectTest, AllocObjectArray) { |
Brian Carlstrom | 4a96b60 | 2011-07-26 16:40:23 -0700 | [diff] [blame] | 58 | ObjectArray<Object>* oa = class_linker_->AllocObjectArray<Object>(2); |
Brian Carlstrom | 578bbdc | 2011-07-21 14:07:47 -0700 | [diff] [blame] | 59 | EXPECT_EQ(2U, oa->GetLength()); |
| 60 | EXPECT_TRUE(oa->Get(0) == NULL); |
| 61 | EXPECT_TRUE(oa->Get(1) == NULL); |
| 62 | oa->Set(0, oa); |
| 63 | EXPECT_TRUE(oa->Get(0) == oa); |
| 64 | EXPECT_TRUE(oa->Get(1) == NULL); |
| 65 | oa->Set(1, oa); |
| 66 | EXPECT_TRUE(oa->Get(0) == oa); |
| 67 | EXPECT_TRUE(oa->Get(1) == oa); |
Carl Shapiro | 0e5d75d | 2011-07-06 18:28:37 -0700 | [diff] [blame] | 68 | } |
| 69 | |
Brian Carlstrom | 0b138b2 | 2011-07-27 15:19:17 -0700 | [diff] [blame] | 70 | TEST_F(ObjectTest, String) { |
| 71 | // Test the empty string. |
| 72 | AssertString(0, "", "", 0); |
| 73 | |
| 74 | // Test one-byte characters. |
| 75 | AssertString(1, " ", "\x00\x20", 0x20); |
| 76 | AssertString(1, "", "\x00\x00", 0); |
| 77 | AssertString(1, "\x7f", "\x00\x7f", 0x7f); |
| 78 | AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69); |
| 79 | |
| 80 | // Test two-byte characters. |
| 81 | AssertString(1, "\xc2\x80", "\x00\x80", 0x80); |
| 82 | AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666); |
| 83 | AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff); |
| 84 | AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69); |
| 85 | |
| 86 | // Test three-byte characters. |
| 87 | AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800); |
| 88 | AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234); |
| 89 | AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff); |
| 90 | AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69); |
| 91 | } |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 92 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 93 | static bool StringNotEqualsUtf8(const String* a, const char* b) { |
| 94 | return !String::EqualsUtf8(a, b); |
| 95 | } |
| 96 | |
| 97 | TEST_F(ObjectTest, StringEqualsUtf8) { |
| 98 | String* string = String::AllocFromAscii("android"); |
| 99 | EXPECT_PRED2(String::EqualsUtf8, string, "android"); |
| 100 | EXPECT_PRED2(StringNotEqualsUtf8, string, "Android"); |
| 101 | EXPECT_PRED2(StringNotEqualsUtf8, string, "ANDROID"); |
| 102 | EXPECT_PRED2(StringNotEqualsUtf8, string, ""); |
| 103 | EXPECT_PRED2(StringNotEqualsUtf8, string, "and"); |
| 104 | EXPECT_PRED2(StringNotEqualsUtf8, string, "androids"); |
| 105 | |
| 106 | String* empty = String::AllocFromAscii(""); |
| 107 | EXPECT_PRED2(String::EqualsUtf8, empty, ""); |
| 108 | EXPECT_PRED2(StringNotEqualsUtf8, empty, "a"); |
| 109 | } |
| 110 | |
| 111 | static bool StringNotEquals(const String* a, const String* b) { |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 112 | return !String::Equals(a, b); |
| 113 | } |
| 114 | |
| 115 | TEST_F(ObjectTest, StringEquals) { |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 116 | String* string = String::AllocFromAscii("android"); |
| 117 | EXPECT_PRED2(String::Equals, string, String::AllocFromAscii("android")); |
| 118 | EXPECT_PRED2(StringNotEquals, string, String::AllocFromAscii("Android")); |
| 119 | EXPECT_PRED2(StringNotEquals, string, String::AllocFromAscii("ANDROID")); |
| 120 | EXPECT_PRED2(StringNotEquals, string, String::AllocFromAscii("")); |
| 121 | EXPECT_PRED2(StringNotEquals, string, String::AllocFromAscii("and")); |
| 122 | EXPECT_PRED2(StringNotEquals, string, String::AllocFromAscii("androids")); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 123 | |
Jesse Wilson | f7e85a5 | 2011-08-01 18:45:58 -0700 | [diff] [blame] | 124 | String* empty = String::AllocFromAscii(""); |
| 125 | EXPECT_PRED2(String::Equals, empty, String::AllocFromAscii("")); |
| 126 | EXPECT_PRED2(StringNotEquals, empty, String::AllocFromAscii("a")); |
Jesse Wilson | cbe9fc0 | 2011-07-29 18:59:50 -0400 | [diff] [blame] | 127 | } |
| 128 | |
Carl Shapiro | 894d0fa | 2011-06-30 14:48:49 -0700 | [diff] [blame] | 129 | } // namespace art |