blob: d240d0c1bed00c7cfe798bd9ea0df3da86c8230b [file] [log] [blame]
Carl Shapiro894d0fa2011-06-30 14:48:49 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2// Author: cshapiro@google.com (Carl Shapiro)
3
Brian Carlstrom578bbdc2011-07-21 14:07:47 -07004#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 Shapiro894d0fa2011-06-30 14:48:49 -070010
Brian Carlstrom0b138b22011-07-27 15:19:17 -070011#include <stdint.h>
Carl Shapiro894d0fa2011-06-30 14:48:49 -070012#include <stdio.h>
13#include "gtest/gtest.h"
14
15namespace art {
16
Brian Carlstrom0b138b22011-07-27 15:19:17 -070017class ObjectTest : public RuntimeTest {
18 protected:
19 void AssertString(size_t length,
20 const char* utf8_in,
21 const char* utf16_expected_le,
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -040022 int32_t hash_expected) {
Brian Carlstrom0b138b22011-07-27 15:19:17 -070023 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 Wilson8989d992011-08-02 13:39:42 -070030 String* string = String::AllocFromModifiedUtf8(length, utf8_in);
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -040031 ASSERT_EQ(length, static_cast<size_t>(string->count_));
Brian Carlstrom0b138b22011-07-27 15:19:17 -070032 ASSERT_TRUE(string->array_ != NULL);
33 ASSERT_TRUE(string->array_->GetChars() != NULL);
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -040034 // strlen is necessary because the 1-character string "\0" is interpreted as ""
Jesse Wilsonf7e85a52011-08-01 18:45:58 -070035 ASSERT_TRUE(String::EqualsUtf8(string, utf8_in) || length != strlen(utf8_in));
Brian Carlstrom0b138b22011-07-27 15:19:17 -070036 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 Carlstroma331b3c2011-07-18 17:47:56 -070042
43TEST_F(ObjectTest, IsInSamePackage) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070044 // 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 Carlstrom578bbdc2011-07-21 14:07:47 -070057TEST_F(ObjectTest, AllocObjectArray) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070058 ObjectArray<Object>* oa = class_linker_->AllocObjectArray<Object>(2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070059 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 Shapiro0e5d75d2011-07-06 18:28:37 -070068}
69
Brian Carlstrom0b138b22011-07-27 15:19:17 -070070TEST_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 Wilsoncbe9fc02011-07-29 18:59:50 -040092
Jesse Wilsonf7e85a52011-08-01 18:45:58 -070093static bool StringNotEqualsUtf8(const String* a, const char* b) {
94 return !String::EqualsUtf8(a, b);
95}
96
97TEST_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
111static bool StringNotEquals(const String* a, const String* b) {
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400112 return !String::Equals(a, b);
113}
114
115TEST_F(ObjectTest, StringEquals) {
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700116 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 Wilsoncbe9fc02011-07-29 18:59:50 -0400123
Jesse Wilsonf7e85a52011-08-01 18:45:58 -0700124 String* empty = String::AllocFromAscii("");
125 EXPECT_PRED2(String::Equals, empty, String::AllocFromAscii(""));
126 EXPECT_PRED2(StringNotEquals, empty, String::AllocFromAscii("a"));
Jesse Wilsoncbe9fc02011-07-29 18:59:50 -0400127}
128
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700129} // namespace art