Index: core/fpdfapi/parser/cpdf_object_unittest.cpp |
diff --git a/core/fpdfapi/parser/cpdf_object_unittest.cpp b/core/fpdfapi/parser/cpdf_object_unittest.cpp |
index 4145f248fa51dcb67c01c0ee332525d3a069e6f8..c34128460288ea3ab203c6238eb55abc528f7ee8 100644 |
--- a/core/fpdfapi/parser/cpdf_object_unittest.cpp |
+++ b/core/fpdfapi/parser/cpdf_object_unittest.cpp |
@@ -59,8 +59,8 @@ class PDFObjectsTest : public testing::Test { |
CPDF_Name* name_obj = new CPDF_Name("space"); |
// Array object. |
m_ArrayObj = new CPDF_Array; |
- m_ArrayObj->InsertAt(0, new CPDF_Number(8902)); |
- m_ArrayObj->InsertAt(1, new CPDF_Name("address")); |
+ m_ArrayObj->InsertNewAt<CPDF_Number>(0, 8902); |
+ m_ArrayObj->InsertNewAt<CPDF_Name>(1, "address"); |
// Dictionary object. |
m_DictObj = new CPDF_Dictionary(); |
m_DictObj->SetFor("bool", new CPDF_Boolean(false)); |
@@ -388,11 +388,11 @@ TEST(PDFArrayTest, GetMatrix) { |
{2.3f, 4.05f, 3, -2, -3, 0.0f}, |
{0.05f, 0.1f, 0.56f, 0.67f, 1.34f, 99.9f}}; |
for (size_t i = 0; i < FX_ArraySize(elems); ++i) { |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
CFX_Matrix matrix(elems[i][0], elems[i][1], elems[i][2], elems[i][3], |
elems[i][4], elems[i][5]); |
for (size_t j = 0; j < 6; ++j) |
- arr->AddNumber(elems[i][j]); |
+ arr->AddNew<CPDF_Number>(elems[i][j]); |
CFX_Matrix arr_matrix = arr->GetMatrix(); |
EXPECT_EQ(matrix.GetA(), arr_matrix.GetA()); |
EXPECT_EQ(matrix.GetB(), arr_matrix.GetB()); |
@@ -409,10 +409,10 @@ TEST(PDFArrayTest, GetRect) { |
{2.3f, 4.05f, -3, 0.0f}, |
{0.05f, 0.1f, 1.34f, 99.9f}}; |
for (size_t i = 0; i < FX_ArraySize(elems); ++i) { |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
CFX_FloatRect rect(elems[i]); |
for (size_t j = 0; j < 4; ++j) |
- arr->AddNumber(elems[i][j]); |
+ arr->AddNew<CPDF_Number>(elems[i][j]); |
CFX_FloatRect arr_rect = arr->GetRect(); |
EXPECT_EQ(rect.left, arr_rect.left); |
EXPECT_EQ(rect.right, arr_rect.right); |
@@ -425,9 +425,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
{ |
// Boolean array. |
const bool vals[] = {true, false, false, true, true}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) |
- arr->InsertAt(i, new CPDF_Boolean(vals[i])); |
+ arr->InsertNewAt<CPDF_Boolean>(i, vals[i]); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
vals[i] ? "true" : "false", // String value. |
@@ -442,9 +442,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
{ |
// Integer array. |
const int vals[] = {10, 0, -345, 2089345456, -1000000000, 567, 93658767}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) |
- arr->InsertAt(i, new CPDF_Number(vals[i])); |
+ arr->InsertNewAt<CPDF_Number>(i, vals[i]); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
char buf[33]; |
TestArrayAccessors(arr.get(), i, // Array and index. |
@@ -463,10 +463,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
897.34f, -2.5f, -1.0f, -345.0f, -0.0f}; |
const char* const expected_str[] = { |
"0", "0", "10", "10", "0.0345", "897.34", "-2.5", "-1", "-345", "0"}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
- for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
- arr->InsertAt(i, new CPDF_Number(vals[i])); |
- } |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
+ for (size_t i = 0; i < FX_ArraySize(vals); ++i) |
+ arr->InsertNewAt<CPDF_Number>(i, vals[i]); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
expected_str[i], // String value. |
@@ -485,8 +484,8 @@ TEST(PDFArrayTest, GetTypeAt) { |
std::unique_ptr<CPDF_Array> string_array(new CPDF_Array); |
std::unique_ptr<CPDF_Array> name_array(new CPDF_Array); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
- string_array->InsertAt(i, new CPDF_String(vals[i], false)); |
- name_array->InsertAt(i, new CPDF_Name(vals[i])); |
+ string_array->InsertNewAt<CPDF_String>(i, vals[i], false); |
+ name_array->InsertNewAt<CPDF_Name>(i, vals[i]); |
} |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
TestArrayAccessors(string_array.get(), i, // Array and index. |
@@ -509,9 +508,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
} |
{ |
// Null element array. |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < 3; ++i) |
- arr->InsertAt(i, new CPDF_Null); |
+ arr->InsertNewAt<CPDF_Null>(i); |
for (size_t i = 0; i < 3; ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
"", // String value. |
@@ -526,14 +525,13 @@ TEST(PDFArrayTest, GetTypeAt) { |
{ |
// Array of array. |
CPDF_Array* vals[3]; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < 3; ++i) { |
- vals[i] = new CPDF_Array; |
+ vals[i] = arr->AddNew<CPDF_Array>(); |
for (size_t j = 0; j < 3; ++j) { |
int value = j + 100; |
- vals[i]->InsertAt(i, new CPDF_Number(value)); |
+ vals[i]->InsertNewAt<CPDF_Number>(i, value); |
} |
- arr->InsertAt(i, vals[i]); |
} |
for (size_t i = 0; i < 3; ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
@@ -549,9 +547,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
{ |
// Dictionary array. |
CPDF_Dictionary* vals[3]; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < 3; ++i) { |
- vals[i] = new CPDF_Dictionary(); |
+ vals[i] = arr->AddNew<CPDF_Dictionary>(); |
for (size_t j = 0; j < 3; ++j) { |
std::string key("key"); |
char buf[33]; |
@@ -559,7 +557,6 @@ TEST(PDFArrayTest, GetTypeAt) { |
int value = j + 200; |
vals[i]->SetFor(key.c_str(), new CPDF_Number(value)); |
} |
- arr->InsertAt(i, vals[i]); |
} |
for (size_t i = 0; i < 3; ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
@@ -576,7 +573,7 @@ TEST(PDFArrayTest, GetTypeAt) { |
// Stream array. |
CPDF_Dictionary* vals[3]; |
CPDF_Stream* stream_vals[3]; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < 3; ++i) { |
vals[i] = new CPDF_Dictionary(); |
for (size_t j = 0; j < 3; ++j) { |
@@ -590,8 +587,7 @@ TEST(PDFArrayTest, GetTypeAt) { |
size_t data_size = FX_ArraySize(content); |
uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size)); |
memcpy(data, content, data_size); |
- stream_vals[i] = new CPDF_Stream(data, data_size, vals[i]); |
- arr->InsertAt(i, stream_vals[i]); |
+ stream_vals[i] = arr->AddNew<CPDF_Stream>(data, data_size, vals[i]); |
} |
for (size_t i = 0; i < 3; ++i) { |
TestArrayAccessors(arr.get(), i, // Array and index. |
@@ -606,27 +602,27 @@ TEST(PDFArrayTest, GetTypeAt) { |
} |
{ |
// Mixed array. |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
- // Array arr will take ownership of all the objects inserted. |
- arr->InsertAt(0, new CPDF_Boolean(true)); |
- arr->InsertAt(1, new CPDF_Boolean(false)); |
- arr->InsertAt(2, new CPDF_Number(0)); |
- arr->InsertAt(3, new CPDF_Number(-1234)); |
- arr->InsertAt(4, new CPDF_Number(2345.0f)); |
- arr->InsertAt(5, new CPDF_Number(0.05f)); |
- arr->InsertAt(6, new CPDF_String("", false)); |
- arr->InsertAt(7, new CPDF_String("It is a test!", false)); |
- arr->InsertAt(8, new CPDF_Name("NAME")); |
- arr->InsertAt(9, new CPDF_Name("test")); |
- arr->InsertAt(10, new CPDF_Null()); |
- CPDF_Array* arr_val = new CPDF_Array; |
- arr_val->AddNumber(1); |
- arr_val->AddNumber(2); |
- arr->InsertAt(11, arr_val); |
- CPDF_Dictionary* dict_val = new CPDF_Dictionary(); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
+ arr->InsertNewAt<CPDF_Boolean>(0, true); |
+ arr->InsertNewAt<CPDF_Boolean>(1, false); |
+ arr->InsertNewAt<CPDF_Number>(2, 0); |
+ arr->InsertNewAt<CPDF_Number>(3, -1234); |
+ arr->InsertNewAt<CPDF_Number>(4, 2345.0f); |
+ arr->InsertNewAt<CPDF_Number>(5, 0.05f); |
+ arr->InsertNewAt<CPDF_String>(6, "", false); |
+ arr->InsertNewAt<CPDF_String>(7, "It is a test!", false); |
+ arr->InsertNewAt<CPDF_Name>(8, "NAME"); |
+ arr->InsertNewAt<CPDF_Name>(9, "test"); |
+ arr->InsertNewAt<CPDF_Null>(10); |
+ |
+ CPDF_Array* arr_val = arr->InsertNewAt<CPDF_Array>(11); |
+ arr_val->AddNew<CPDF_Number>(1); |
+ arr_val->AddNew<CPDF_Number>(2); |
+ |
+ CPDF_Dictionary* dict_val = arr->InsertNewAt<CPDF_Dictionary>(12); |
dict_val->SetFor("key1", new CPDF_String("Linda", false)); |
dict_val->SetFor("key2", new CPDF_String("Zoe", false)); |
- arr->InsertAt(12, dict_val); |
+ |
CPDF_Dictionary* stream_dict = new CPDF_Dictionary(); |
stream_dict->SetFor("key1", new CPDF_String("John", false)); |
stream_dict->SetFor("key2", new CPDF_String("King", false)); |
@@ -636,8 +632,8 @@ TEST(PDFArrayTest, GetTypeAt) { |
size_t buf_size = sizeof(data); |
uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size)); |
memcpy(buf, data, buf_size); |
- CPDF_Stream* stream_val = new CPDF_Stream(buf, buf_size, stream_dict); |
- arr->InsertAt(13, stream_val); |
+ CPDF_Stream* stream_val = |
+ arr->InsertNewAt<CPDF_Stream>(13, buf, buf_size, stream_dict); |
const char* const expected_str[] = { |
"true", "false", "0", "-1234", "2345", "0.05", "", |
"It is a test!", "NAME", "test", "", "", "", ""}; |
@@ -671,9 +667,9 @@ TEST(PDFArrayTest, GetTypeAt) { |
TEST(PDFArrayTest, AddNumber) { |
float vals[] = {1.0f, -1.0f, 0, 0.456734f, |
12345.54321f, 0.5f, 1000, 0.000045f}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) |
- arr->AddNumber(vals[i]); |
+ arr->AddNew<CPDF_Number>(vals[i]); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
EXPECT_EQ(CPDF_Object::NUMBER, arr->GetObjectAt(i)->GetType()); |
EXPECT_EQ(vals[i], arr->GetObjectAt(i)->GetNumber()); |
@@ -682,9 +678,9 @@ TEST(PDFArrayTest, AddNumber) { |
TEST(PDFArrayTest, AddInteger) { |
int vals[] = {0, 1, 934435456, 876, 10000, -1, -24354656, -100}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) |
- arr->AddInteger(vals[i]); |
+ arr->AddNew<CPDF_Number>(vals[i]); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
EXPECT_EQ(CPDF_Object::NUMBER, arr->GetObjectAt(i)->GetType()); |
EXPECT_EQ(vals[i], arr->GetObjectAt(i)->GetNumber()); |
@@ -697,8 +693,8 @@ TEST(PDFArrayTest, AddStringAndName) { |
std::unique_ptr<CPDF_Array> string_array(new CPDF_Array); |
std::unique_ptr<CPDF_Array> name_array(new CPDF_Array); |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
- string_array->AddString(vals[i]); |
- name_array->AddName(vals[i]); |
+ string_array->AddNew<CPDF_String>(vals[i], false); |
+ name_array->AddNew<CPDF_Name>(vals[i]); |
} |
for (size_t i = 0; i < FX_ArraySize(vals); ++i) { |
EXPECT_EQ(CPDF_Object::STRING, string_array->GetObjectAt(i)->GetType()); |
@@ -720,14 +716,14 @@ TEST(PDFArrayTest, AddReferenceAndGetObjectAt) { |
CPDF_Object* indirect_objs[] = {boolean_obj, int_obj, float_obj, |
str_obj, name_obj, null_obj}; |
unsigned int obj_nums[] = {2, 4, 7, 2345, 799887, 1}; |
- std::unique_ptr<CPDF_Array> arr(new CPDF_Array); |
+ auto arr = pdfium::MakeUnique<CPDF_Array>(); |
std::unique_ptr<CPDF_Array> arr1(new CPDF_Array); |
// Create two arrays of references by different AddReference() APIs. |
for (size_t i = 0; i < FX_ArraySize(indirect_objs); ++i) { |
holder->ReplaceIndirectObjectIfHigherGeneration( |
obj_nums[i], pdfium::WrapUnique<CPDF_Object>(indirect_objs[i])); |
- arr->AddReference(holder.get(), obj_nums[i]); |
- arr1->AddReference(holder.get(), indirect_objs[i]->GetObjNum()); |
+ arr->AddNew<CPDF_Reference>(holder.get(), obj_nums[i]); |
+ arr1->AddNew<CPDF_Reference>(holder.get(), indirect_objs[i]->GetObjNum()); |
} |
// Check indirect objects. |
for (size_t i = 0; i < FX_ArraySize(obj_nums); ++i) |
@@ -747,7 +743,7 @@ TEST(PDFArrayTest, AddReferenceAndGetObjectAt) { |
TEST(PDFArrayTest, CloneDirectObject) { |
CPDF_IndirectObjectHolder objects_holder; |
std::unique_ptr<CPDF_Array> array(new CPDF_Array); |
- array->AddReference(&objects_holder, 1234); |
+ array->AddNew<CPDF_Reference>(&objects_holder, 1234); |
ASSERT_EQ(1U, array->GetCount()); |
CPDF_Object* obj = array->GetObjectAt(0); |
ASSERT_TRUE(obj); |
@@ -766,9 +762,8 @@ TEST(PDFArrayTest, CloneDirectObject) { |
TEST(PDFArrayTest, ConvertIndirect) { |
CPDF_IndirectObjectHolder objects_holder; |
- std::unique_ptr<CPDF_Array> array(new CPDF_Array); |
- CPDF_Object* pObj = new CPDF_Number(42); |
- array->Add(pObj); |
+ auto array = pdfium::MakeUnique<CPDF_Array>(); |
+ CPDF_Object* pObj = array->AddNew<CPDF_Number>(42); |
array->ConvertToIndirectObjectAt(0, &objects_holder); |
CPDF_Object* pRef = array->GetObjectAt(0); |
CPDF_Object* pNum = array->GetDirectObjectAt(0); |
@@ -802,11 +797,9 @@ TEST(PDFDictionaryTest, CloneDirectObject) { |
TEST(PDFObjectTest, CloneCheckLoop) { |
{ |
// Create a dictionary/array pair with a reference loop. |
- CPDF_Dictionary* dict_obj = new CPDF_Dictionary(); |
- std::unique_ptr<CPDF_Array> arr_obj(new CPDF_Array); |
+ auto arr_obj = pdfium::MakeUnique<CPDF_Array>(); |
+ CPDF_Dictionary* dict_obj = arr_obj->InsertNewAt<CPDF_Dictionary>(0); |
dict_obj->SetFor("arr", arr_obj.get()); |
- arr_obj->InsertAt(0, dict_obj); |
- |
// Clone this object to see whether stack overflow will be triggered. |
std::unique_ptr<CPDF_Array> cloned_array = ToArray(arr_obj->Clone()); |
// Cloned object should be the same as the original. |
@@ -840,8 +833,8 @@ TEST(PDFObjectTest, CloneCheckLoop) { |
// Create an object with a reference loop. |
CPDF_Dictionary* dict_obj = objects_holder.NewIndirect<CPDF_Dictionary>(); |
std::unique_ptr<CPDF_Array> arr_obj = pdfium::MakeUnique<CPDF_Array>(); |
- arr_obj->InsertAt( |
- 0, new CPDF_Reference(&objects_holder, dict_obj->GetObjNum())); |
+ arr_obj->InsertNewAt<CPDF_Reference>(0, &objects_holder, |
+ dict_obj->GetObjNum()); |
CPDF_Object* elem0 = arr_obj->GetObjectAt(0); |
dict_obj->SetFor("arr", arr_obj.release()); |
EXPECT_EQ(1u, dict_obj->GetObjNum()); |