blob: 969378cef83a83405d9ecdf88ddec28e85021a76 [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,
22 uint32_t hash_expected) {
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
30 String* string = class_linker_->AllocStringFromModifiedUtf8(length, utf8_in);
31 ASSERT_EQ(length, string->count_);
32 ASSERT_TRUE(string->array_ != NULL);
33 ASSERT_TRUE(string->array_->GetChars() != NULL);
34 for (size_t i = 0; i < length; i++) {
35 EXPECT_EQ(utf16_expected[i], string->array_->GetChar(i));
36 }
37 EXPECT_EQ(hash_expected, string->hash_code_);
38 }
39};
Brian Carlstroma331b3c2011-07-18 17:47:56 -070040
41TEST_F(ObjectTest, IsInSamePackage) {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070042 // Matches
43 EXPECT_TRUE(Class::IsInSamePackage("Ljava/lang/Object;",
44 "Ljava/lang/Class"));
45 EXPECT_TRUE(Class::IsInSamePackage("LFoo;",
46 "LBar;"));
47
48 // Mismatches
49 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;",
50 "Ljava/io/File;"));
51 EXPECT_FALSE(Class::IsInSamePackage("Ljava/lang/Object;",
52 "Ljava/lang/reflect/Method;"));
53}
54
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070055TEST_F(ObjectTest, AllocObjectArray) {
Brian Carlstrom4a96b602011-07-26 16:40:23 -070056 ObjectArray<Object>* oa = class_linker_->AllocObjectArray<Object>(2);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 EXPECT_EQ(2U, oa->GetLength());
58 EXPECT_TRUE(oa->Get(0) == NULL);
59 EXPECT_TRUE(oa->Get(1) == NULL);
60 oa->Set(0, oa);
61 EXPECT_TRUE(oa->Get(0) == oa);
62 EXPECT_TRUE(oa->Get(1) == NULL);
63 oa->Set(1, oa);
64 EXPECT_TRUE(oa->Get(0) == oa);
65 EXPECT_TRUE(oa->Get(1) == oa);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070066}
67
Brian Carlstrom0b138b22011-07-27 15:19:17 -070068TEST_F(ObjectTest, String) {
69 // Test the empty string.
70 AssertString(0, "", "", 0);
71
72 // Test one-byte characters.
73 AssertString(1, " ", "\x00\x20", 0x20);
74 AssertString(1, "", "\x00\x00", 0);
75 AssertString(1, "\x7f", "\x00\x7f", 0x7f);
76 AssertString(2, "hi", "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
77
78 // Test two-byte characters.
79 AssertString(1, "\xc2\x80", "\x00\x80", 0x80);
80 AssertString(1, "\xd9\xa6", "\x06\x66", 0x0666);
81 AssertString(1, "\xdf\xbf", "\x07\xff", 0x07ff);
82 AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69);
83
84 // Test three-byte characters.
85 AssertString(1, "\xe0\xa0\x80", "\x08\x00", 0x0800);
86 AssertString(1, "\xe1\x88\xb4", "\x12\x34", 0x1234);
87 AssertString(1, "\xef\xbf\xbf", "\xff\xff", 0xffff);
88 AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69);
89}
Carl Shapiro894d0fa2011-06-30 14:48:49 -070090} // namespace art