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

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

Issue 2498223005: Make CPDF_Array take unique_ptrs (Closed)
Patch Set: nits 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_data_avail.cpp » ('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"
11 #include "testing/gtest/include/gtest/gtest.h" 11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "third_party/base/ptr_util.h" 12 #include "third_party/base/ptr_util.h"
13 13
14 TEST(cpdf_array, RemoveAt) { 14 TEST(cpdf_array, RemoveAt) {
15 { 15 {
16 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 16 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
17 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 17 std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
18 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 18 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
19 arr->AddInteger(elems[i]); 19 arr->AddNew<CPDF_Number>(elems[i]);
20 arr->RemoveAt(3, 3); 20 arr->RemoveAt(3, 3);
21 int expected[] = {1, 2, 3, 7, 8, 9, 10}; 21 int expected[] = {1, 2, 3, 7, 8, 9, 10};
22 EXPECT_EQ(FX_ArraySize(expected), arr->GetCount()); 22 EXPECT_EQ(FX_ArraySize(expected), arr->GetCount());
23 for (size_t i = 0; i < FX_ArraySize(expected); ++i) 23 for (size_t i = 0; i < FX_ArraySize(expected); ++i)
24 EXPECT_EQ(expected[i], arr->GetIntegerAt(i)); 24 EXPECT_EQ(expected[i], arr->GetIntegerAt(i));
25 arr->RemoveAt(4, 2); 25 arr->RemoveAt(4, 2);
26 int expected2[] = {1, 2, 3, 7, 10}; 26 int expected2[] = {1, 2, 3, 7, 10};
27 EXPECT_EQ(FX_ArraySize(expected2), arr->GetCount()); 27 EXPECT_EQ(FX_ArraySize(expected2), arr->GetCount());
28 for (size_t i = 0; i < FX_ArraySize(expected2); ++i) 28 for (size_t i = 0; i < FX_ArraySize(expected2); ++i)
29 EXPECT_EQ(expected2[i], arr->GetIntegerAt(i)); 29 EXPECT_EQ(expected2[i], arr->GetIntegerAt(i));
30 } 30 }
31 { 31 {
32 // When the range is out of bound, RemoveAt has no effect. 32 // When the range is out of bound, RemoveAt has no effect.
33 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 33 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
34 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 34 std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
35 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 35 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
36 arr->AddInteger(elems[i]); 36 arr->AddNew<CPDF_Number>(elems[i]);
37 arr->RemoveAt(8, 5); 37 arr->RemoveAt(8, 5);
38 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount()); 38 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount());
39 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 39 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
40 EXPECT_EQ(elems[i], arr->GetIntegerAt(i)); 40 EXPECT_EQ(elems[i], arr->GetIntegerAt(i));
41 arr->RemoveAt(0, 12); 41 arr->RemoveAt(0, 12);
42 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount()); 42 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount());
43 arr->RemoveAt(11, 1); 43 arr->RemoveAt(11, 1);
44 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount()); 44 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount());
45 } 45 }
46 } 46 }
47 47
48 TEST(cpdf_array, InsertAt) { 48 TEST(cpdf_array, InsertAt) {
49 { 49 {
50 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 50 int elems[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
51 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 51 auto arr = pdfium::MakeUnique<CPDF_Array>();
52 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 52 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
53 arr->InsertAt(i, new CPDF_Number(elems[i])); 53 arr->InsertNewAt<CPDF_Number>(i, elems[i]);
54 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount()); 54 EXPECT_EQ(FX_ArraySize(elems), arr->GetCount());
55 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 55 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
56 EXPECT_EQ(elems[i], arr->GetIntegerAt(i)); 56 EXPECT_EQ(elems[i], arr->GetIntegerAt(i));
57 arr->InsertAt(3, new CPDF_Number(33)); 57 arr->InsertNewAt<CPDF_Number>(3, 33);
58 arr->InsertAt(6, new CPDF_Number(55)); 58 arr->InsertNewAt<CPDF_Number>(6, 55);
59 arr->InsertAt(12, new CPDF_Number(12)); 59 arr->InsertNewAt<CPDF_Number>(12, 12);
60 int expected[] = {1, 2, 3, 33, 4, 5, 55, 6, 7, 8, 9, 10, 12}; 60 int expected[] = {1, 2, 3, 33, 4, 5, 55, 6, 7, 8, 9, 10, 12};
61 EXPECT_EQ(FX_ArraySize(expected), arr->GetCount()); 61 EXPECT_EQ(FX_ArraySize(expected), arr->GetCount());
62 for (size_t i = 0; i < FX_ArraySize(expected); ++i) 62 for (size_t i = 0; i < FX_ArraySize(expected); ++i)
63 EXPECT_EQ(expected[i], arr->GetIntegerAt(i)); 63 EXPECT_EQ(expected[i], arr->GetIntegerAt(i));
64 } 64 }
65 { 65 {
66 // When the position to insert is beyond the upper bound, 66 // When the position to insert is beyond the upper bound,
67 // an element is inserted at that position while other unfilled 67 // an element is inserted at that position while other unfilled
68 // positions have nullptr. 68 // positions have nullptr.
69 int elems[] = {1, 2}; 69 int elems[] = {1, 2};
70 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 70 auto arr = pdfium::MakeUnique<CPDF_Array>();
71 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 71 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
72 arr->InsertAt(i, new CPDF_Number(elems[i])); 72 arr->InsertNewAt<CPDF_Number>(i, elems[i]);
73 arr->InsertAt(10, new CPDF_Number(10)); 73 arr->InsertNewAt<CPDF_Number>(10, 10);
74 EXPECT_EQ(11u, arr->GetCount()); 74 EXPECT_EQ(11u, arr->GetCount());
75 for (size_t i = 0; i < FX_ArraySize(elems); ++i) 75 for (size_t i = 0; i < FX_ArraySize(elems); ++i)
76 EXPECT_EQ(elems[i], arr->GetIntegerAt(i)); 76 EXPECT_EQ(elems[i], arr->GetIntegerAt(i));
77 for (size_t i = FX_ArraySize(elems); i < 10; ++i) 77 for (size_t i = FX_ArraySize(elems); i < 10; ++i)
78 EXPECT_EQ(nullptr, arr->GetObjectAt(i)); 78 EXPECT_EQ(nullptr, arr->GetObjectAt(i));
79 EXPECT_EQ(10, arr->GetIntegerAt(10)); 79 EXPECT_EQ(10, arr->GetIntegerAt(10));
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 auto arr = pdfium::MakeUnique<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->InsertNewAt<CPDF_Number>(i, elems[i]);
90 std::unique_ptr<CPDF_Array> arr2 = ToArray(arr->Clone()); 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;
101 static const size_t kNumOfRowElems = 5; 101 static const size_t kNumOfRowElems = 5;
102 int elems[kNumOfRows][kNumOfRowElems] = { 102 int elems[kNumOfRows][kNumOfRowElems] = {
103 {1, 2, 3, 4, 5}, {10, 9, 8, 7, 6}, {11, 12, 13, 14, 15}}; 103 {1, 2, 3, 4, 5}, {10, 9, 8, 7, 6}, {11, 12, 13, 14, 15}};
104 std::unique_ptr<CPDF_Array> arr(new CPDF_Array); 104 std::unique_ptr<CPDF_Array> arr(new CPDF_Array);
105 // Indirect references to indirect objects. 105 // Indirect references to indirect objects.
106 std::unique_ptr<CPDF_IndirectObjectHolder> obj_holder( 106 std::unique_ptr<CPDF_IndirectObjectHolder> obj_holder(
107 new CPDF_IndirectObjectHolder()); 107 new CPDF_IndirectObjectHolder());
108 for (size_t i = 0; i < kNumOfRows; ++i) { 108 for (size_t i = 0; i < kNumOfRows; ++i) {
109 CPDF_Array* arr_elem = new CPDF_Array; 109 auto arr_elem = pdfium::MakeUnique<CPDF_Array>();
110 for (size_t j = 0; j < kNumOfRowElems; ++j) { 110 for (size_t j = 0; j < kNumOfRowElems; ++j) {
111 std::unique_ptr<CPDF_Number> obj(new CPDF_Number(elems[i][j])); 111 std::unique_ptr<CPDF_Number> obj(new CPDF_Number(elems[i][j]));
112 // Starts object number from 1. 112 // Starts object number from 1.
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->InsertNewAt<CPDF_Reference>(j, obj_holder.get(), obj_num);
117 } 117 }
118 arr->InsertAt(i, arr_elem); 118 arr->InsertAt(i, std::move(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 = ToArray(arr->Clone()); 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 = ToArray(arr->CloneDirectObject()); 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) {
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 } 162 }
163 } 163 }
164 } 164 }
165 } 165 }
166 166
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->InsertNewAt<CPDF_Number>(i, 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_data_avail.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698