Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(8)

Side by Side Diff: core/fpdfapi/parser/cpdf_array_unittest.cpp

Issue 2484033002: Return unique_ptr from CPDF_Object::Clone(). (Closed)
Patch Set: std::move() it Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « core/fpdfapi/parser/cpdf_array.cpp ('k') | core/fpdfapi/parser/cpdf_boolean.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 PDFium Authors. All rights reserved. 1 // Copyright 2016 PDFium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "core/fpdfapi/parser/cpdf_array.h" 5 #include "core/fpdfapi/parser/cpdf_array.h"
6 6
7 #include <memory> 7 #include <memory>
8 8
9 #include "core/fpdfapi/parser/cpdf_number.h" 9 #include "core/fpdfapi/parser/cpdf_number.h"
10 #include "core/fpdfapi/parser/cpdf_reference.h" 10 #include "core/fpdfapi/parser/cpdf_reference.h"
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
80 } 80 }
81 } 81 }
82 82
83 TEST(cpdf_array, Clone) { 83 TEST(cpdf_array, Clone) {
84 { 84 {
85 // Basic case. 85 // Basic case.
86 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 86 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
87 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 87 std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
88 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 88 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
89 arr->InsertAt(i, new CPDF_Number(elems[i])); 89 arr->InsertAt(i, new CPDF_Number(elems[i]));
90 std::unique_ptr<CPDF_Array> arr2(arr->Clone()->AsArray()); 90 std::unique_ptr<CPDF_Array> arr2 = ToArray(arr->Clone());
91 EXPECT_EQ(arr->GetCount(), arr2->GetCount()); 91 EXPECT_EQ(arr->GetCount(), arr2->GetCount());
92 for (size_t i = 0; i < FX_ArraySize(elems); ++i) { 92 for (size_t i = 0; i < FX_ArraySize(elems); ++i) {
93 // Clone() always create new objects. 93 // Clone() always create new objects.
94 EXPECT_NE(arr->GetObjectAt(i), arr2->GetObjectAt(i)); 94 EXPECT_NE(arr->GetObjectAt(i), arr2->GetObjectAt(i));
95 EXPECT_EQ(arr->GetIntegerAt(i), arr2->GetIntegerAt(i)); 95 EXPECT_EQ(arr->GetIntegerAt(i), arr2->GetIntegerAt(i));
96 } 96 }
97 } 97 }
98 { 98 {
99 // Clone() with and without dereferencing reference objects. 99 // Clone() with and without dereferencing reference objects.
100 static const size_t kNumOfRows = 3; 100 static const size_t kNumOfRows = 3;
(...skipping 12 matching lines...) Expand all
113 int obj_num = i * kNumOfRowElems + j + 1; 113 int obj_num = i * kNumOfRowElems + j + 1;
114 obj_holder->ReplaceIndirectObjectIfHigherGeneration(obj_num, 114 obj_holder->ReplaceIndirectObjectIfHigherGeneration(obj_num,
115 std::move(obj)); 115 std::move(obj));
116 arr_elem->InsertAt(j, new CPDF_Reference(obj_holder.get(), obj_num)); 116 arr_elem->InsertAt(j, new CPDF_Reference(obj_holder.get(), obj_num));
117 } 117 }
118 arr->InsertAt(i, arr_elem); 118 arr->InsertAt(i, arr_elem);
119 } 119 }
120 ASSERT_EQ(kNumOfRows, arr->GetCount()); 120 ASSERT_EQ(kNumOfRows, arr->GetCount());
121 // Not dereferencing reference objects means just creating new references 121 // Not dereferencing reference objects means just creating new references
122 // instead of new copies of direct objects. 122 // instead of new copies of direct objects.
123 std::unique_ptr<CPDF_Array> arr1(arr->Clone()->AsArray()); 123 std::unique_ptr<CPDF_Array> arr1 = ToArray(arr->Clone());
124 EXPECT_EQ(arr->GetCount(), arr1->GetCount()); 124 EXPECT_EQ(arr->GetCount(), arr1->GetCount());
125 // Dereferencing reference objects creates new copies of direct objects. 125 // Dereferencing reference objects creates new copies of direct objects.
126 std::unique_ptr<CPDF_Array> arr2(arr->CloneDirectObject()->AsArray()); 126 std::unique_ptr<CPDF_Array> arr2 = ToArray(arr->CloneDirectObject());
127 EXPECT_EQ(arr->GetCount(), arr2->GetCount()); 127 EXPECT_EQ(arr->GetCount(), arr2->GetCount());
128 for (size_t i = 0; i < kNumOfRows; ++i) { 128 for (size_t i = 0; i < kNumOfRows; ++i) {
129 CPDF_Array* arr_elem = arr->GetObjectAt(i)->AsArray(); 129 CPDF_Array* arr_elem = arr->GetObjectAt(i)->AsArray();
130 CPDF_Array* arr1_elem = arr1->GetObjectAt(i)->AsArray(); 130 CPDF_Array* arr1_elem = arr1->GetObjectAt(i)->AsArray();
131 CPDF_Array* arr2_elem = arr2->GetObjectAt(i)->AsArray(); 131 CPDF_Array* arr2_elem = arr2->GetObjectAt(i)->AsArray();
132 EXPECT_NE(arr_elem, arr1_elem); 132 EXPECT_NE(arr_elem, arr1_elem);
133 EXPECT_NE(arr_elem, arr2_elem); 133 EXPECT_NE(arr_elem, arr2_elem);
134 for (size_t j = 0; j < kNumOfRowElems; ++j) { 134 for (size_t j = 0; j < kNumOfRowElems; ++j) {
135 auto elem_obj = arr_elem->GetObjectAt(j); 135 auto elem_obj = arr_elem->GetObjectAt(j);
136 auto elem_obj1 = arr1_elem->GetObjectAt(j); 136 auto elem_obj1 = arr1_elem->GetObjectAt(j);
(...skipping 30 matching lines...) Expand all
167 TEST(cpdf_array, Iterator) { 167 TEST(cpdf_array, Iterator) {
168 int elems[] = {-23, -11, 3, 455, 2345877, 168 int elems[] = {-23, -11, 3, 455, 2345877,
169 0, 7895330, -12564334, 10000, -100000}; 169 0, 7895330, -12564334, 10000, -100000};
170 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 170 std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
171 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 171 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
172 arr->InsertAt(i, new CPDF_Number(elems[i])); 172 arr->InsertAt(i, new CPDF_Number(elems[i]));
173 size_t index = 0; 173 size_t index = 0;
174 for (const auto& it : *arr) 174 for (const auto& it : *arr)
175 EXPECT_EQ(elems[index++], it->AsNumber()->GetInteger()); 175 EXPECT_EQ(elems[index++], it->AsNumber()->GetInteger());
176 } 176 }
OLDNEW
« no previous file with comments | « core/fpdfapi/parser/cpdf_array.cpp ('k') | core/fpdfapi/parser/cpdf_boolean.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698