Flesh out AllocString

Change-Id: Ie8c1170e71374942eafdcb40775ca2df3cf7bbc7
diff --git a/src/object_test.cc b/src/object_test.cc
index dc74b8b..969378c 100644
--- a/src/object_test.cc
+++ b/src/object_test.cc
@@ -8,12 +8,35 @@
 #include "object.h"
 #include "scoped_ptr.h"
 
+#include <stdint.h>
 #include <stdio.h>
 #include "gtest/gtest.h"
 
 namespace art {
 
-class ObjectTest : public RuntimeTest {};
+class ObjectTest : public RuntimeTest {
+ protected:
+  void AssertString(size_t length,
+                    const char* utf8_in,
+                    const char* utf16_expected_le,
+                    uint32_t hash_expected) {
+    uint16_t utf16_expected[length];
+    for (size_t i = 0; i < length; i++) {
+      uint16_t ch = (((utf16_expected_le[i*2 + 0] & 0xff) << 8) |
+                     ((utf16_expected_le[i*2 + 1] & 0xff) << 0));
+      utf16_expected[i] = ch;
+    }
+
+    String* string = class_linker_->AllocStringFromModifiedUtf8(length, utf8_in);
+    ASSERT_EQ(length, string->count_);
+    ASSERT_TRUE(string->array_ != NULL);
+    ASSERT_TRUE(string->array_->GetChars() != NULL);
+    for (size_t i = 0; i < length; i++) {
+      EXPECT_EQ(utf16_expected[i], string->array_->GetChar(i));
+    }
+    EXPECT_EQ(hash_expected, string->hash_code_);
+  }
+};
 
 TEST_F(ObjectTest, IsInSamePackage) {
   // Matches
@@ -42,4 +65,26 @@
     EXPECT_TRUE(oa->Get(1) == oa);
 }
 
+TEST_F(ObjectTest, String) {
+  // Test the empty string.
+  AssertString(0, "",     "", 0);
+
+  // Test one-byte characters.
+  AssertString(1, " ",    "\x00\x20",         0x20);
+  AssertString(1, "",     "\x00\x00",         0);
+  AssertString(1, "\x7f", "\x00\x7f",         0x7f);
+  AssertString(2, "hi",   "\x00\x68\x00\x69", (31 * 0x68) + 0x69);
+
+  // Test two-byte characters.
+  AssertString(1, "\xc2\x80",   "\x00\x80",                 0x80);
+  AssertString(1, "\xd9\xa6",   "\x06\x66",                 0x0666);
+  AssertString(1, "\xdf\xbf",   "\x07\xff",                 0x07ff);
+  AssertString(3, "h\xd9\xa6i", "\x00\x68\x06\x66\x00\x69", (31 * ((31 * 0x68) + 0x0666)) + 0x69);
+
+  // Test three-byte characters.
+  AssertString(1, "\xe0\xa0\x80",   "\x08\x00",                 0x0800);
+  AssertString(1, "\xe1\x88\xb4",   "\x12\x34",                 0x1234);
+  AssertString(1, "\xef\xbf\xbf",   "\xff\xff",                 0xffff);
+  AssertString(3, "h\xe1\x88\xb4i", "\x00\x68\x12\x34\x00\x69", (31 * ((31 * 0x68) + 0x1234)) + 0x69);
+}
 }  // namespace art