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

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

Issue 2584683002: Return unique_ptr from CFX_BinaryBuf::DetachBuffer() (Closed)
Patch Set: Created 4 years 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/page/cpdf_streamparser.cpp ('k') | core/fpdfapi/parser/cpdf_stream.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 #include "core/fpdfapi/parser/cpdf_boolean.h" 6 #include "core/fpdfapi/parser/cpdf_boolean.h"
7 #include "core/fpdfapi/parser/cpdf_dictionary.h" 7 #include "core/fpdfapi/parser/cpdf_dictionary.h"
8 #include "core/fpdfapi/parser/cpdf_name.h" 8 #include "core/fpdfapi/parser/cpdf_name.h"
9 #include "core/fpdfapi/parser/cpdf_null.h" 9 #include "core/fpdfapi/parser/cpdf_null.h"
10 #include "core/fpdfapi/parser/cpdf_number.h" 10 #include "core/fpdfapi/parser/cpdf_number.h"
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 m_ArrayObj = new CPDF_Array; 62 m_ArrayObj = new CPDF_Array;
63 m_ArrayObj->InsertNewAt<CPDF_Number>(0, 8902); 63 m_ArrayObj->InsertNewAt<CPDF_Number>(0, 8902);
64 m_ArrayObj->InsertNewAt<CPDF_Name>(1, "address"); 64 m_ArrayObj->InsertNewAt<CPDF_Name>(1, "address");
65 // Dictionary object. 65 // Dictionary object.
66 m_DictObj = new CPDF_Dictionary(); 66 m_DictObj = new CPDF_Dictionary();
67 m_DictObj->SetNewFor<CPDF_Boolean>("bool", false); 67 m_DictObj->SetNewFor<CPDF_Boolean>("bool", false);
68 m_DictObj->SetNewFor<CPDF_Number>("num", 0.23f); 68 m_DictObj->SetNewFor<CPDF_Number>("num", 0.23f);
69 // Stream object. 69 // Stream object.
70 const char content[] = "abcdefghijklmnopqrstuvwxyz"; 70 const char content[] = "abcdefghijklmnopqrstuvwxyz";
71 size_t buf_len = FX_ArraySize(content); 71 size_t buf_len = FX_ArraySize(content);
72 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_len)); 72 std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_Alloc(uint8_t, buf_len));
73 memcpy(buf, content, buf_len); 73 memcpy(buf.get(), content, buf_len);
74 auto pNewDict = pdfium::MakeUnique<CPDF_Dictionary>(); 74 auto pNewDict = pdfium::MakeUnique<CPDF_Dictionary>();
75 m_StreamDictObj = pNewDict.get(); 75 m_StreamDictObj = pNewDict.get();
76 m_StreamDictObj->SetNewFor<CPDF_String>("key1", L" test dict"); 76 m_StreamDictObj->SetNewFor<CPDF_String>("key1", L" test dict");
77 m_StreamDictObj->SetNewFor<CPDF_Number>("key2", -1); 77 m_StreamDictObj->SetNewFor<CPDF_Number>("key2", -1);
78 CPDF_Stream* stream_obj = 78 CPDF_Stream* stream_obj =
79 new CPDF_Stream(buf, buf_len, std::move(pNewDict)); 79 new CPDF_Stream(std::move(buf), buf_len, std::move(pNewDict));
80 // Null Object. 80 // Null Object.
81 CPDF_Null* null_obj = new CPDF_Null; 81 CPDF_Null* null_obj = new CPDF_Null;
82 // All direct objects. 82 // All direct objects.
83 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj, 83 CPDF_Object* objs[] = {boolean_false_obj, boolean_true_obj, number_int_obj,
84 number_float_obj, str_reg_obj, str_spec_obj, 84 number_float_obj, str_reg_obj, str_spec_obj,
85 name_obj, m_ArrayObj, m_DictObj, 85 name_obj, m_ArrayObj, m_DictObj,
86 stream_obj, null_obj}; 86 stream_obj, null_obj};
87 m_DirectObjTypes = { 87 m_DirectObjTypes = {
88 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER, 88 CPDF_Object::BOOLEAN, CPDF_Object::BOOLEAN, CPDF_Object::NUMBER,
89 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING, 89 CPDF_Object::NUMBER, CPDF_Object::STRING, CPDF_Object::STRING,
(...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after
581 vals[i] = new CPDF_Dictionary(); 581 vals[i] = new CPDF_Dictionary();
582 for (size_t j = 0; j < 3; ++j) { 582 for (size_t j = 0; j < 3; ++j) {
583 std::string key("key"); 583 std::string key("key");
584 char buf[33]; 584 char buf[33];
585 key.append(FXSYS_itoa(j, buf, 10)); 585 key.append(FXSYS_itoa(j, buf, 10));
586 int value = j + 200; 586 int value = j + 200;
587 vals[i]->SetNewFor<CPDF_Number>(key.c_str(), value); 587 vals[i]->SetNewFor<CPDF_Number>(key.c_str(), value);
588 } 588 }
589 uint8_t content[] = "content: this is a stream"; 589 uint8_t content[] = "content: this is a stream";
590 size_t data_size = FX_ArraySize(content); 590 size_t data_size = FX_ArraySize(content);
591 uint8_t* data = reinterpret_cast<uint8_t*>(malloc(data_size)); 591 std::unique_ptr<uint8_t, FxFreeDeleter> data(
592 memcpy(data, content, data_size); 592 FX_Alloc(uint8_t, data_size));
593 stream_vals[i] = arr->AddNew<CPDF_Stream>(data, data_size, 593 memcpy(data.get(), content, data_size);
594 stream_vals[i] = arr->AddNew<CPDF_Stream>(std::move(data), data_size,
594 pdfium::WrapUnique(vals[i])); 595 pdfium::WrapUnique(vals[i]));
595 } 596 }
596 for (size_t i = 0; i < 3; ++i) { 597 for (size_t i = 0; i < 3; ++i) {
597 TestArrayAccessors(arr.get(), i, // Array and index. 598 TestArrayAccessors(arr.get(), i, // Array and index.
598 "", // String value. 599 "", // String value.
599 nullptr, // Const string value. 600 nullptr, // Const string value.
600 0, // Integer value. 601 0, // Integer value.
601 0, // Float value. 602 0, // Float value.
602 nullptr, // Array value. 603 nullptr, // Array value.
603 vals[i], // Dictionary value. 604 vals[i], // Dictionary value.
(...skipping 23 matching lines...) Expand all
627 dict_val->SetNewFor<CPDF_String>("key1", "Linda", false); 628 dict_val->SetNewFor<CPDF_String>("key1", "Linda", false);
628 dict_val->SetNewFor<CPDF_String>("key2", "Zoe", false); 629 dict_val->SetNewFor<CPDF_String>("key2", "Zoe", false);
629 630
630 CPDF_Dictionary* stream_dict = new CPDF_Dictionary(); 631 CPDF_Dictionary* stream_dict = new CPDF_Dictionary();
631 stream_dict->SetNewFor<CPDF_String>("key1", "John", false); 632 stream_dict->SetNewFor<CPDF_String>("key1", "John", false);
632 stream_dict->SetNewFor<CPDF_String>("key2", "King", false); 633 stream_dict->SetNewFor<CPDF_String>("key2", "King", false);
633 uint8_t data[] = "A stream for test"; 634 uint8_t data[] = "A stream for test";
634 // The data buffer will be owned by stream object, so it needs to be 635 // The data buffer will be owned by stream object, so it needs to be
635 // dynamically allocated. 636 // dynamically allocated.
636 size_t buf_size = sizeof(data); 637 size_t buf_size = sizeof(data);
637 uint8_t* buf = reinterpret_cast<uint8_t*>(malloc(buf_size)); 638 std::unique_ptr<uint8_t, FxFreeDeleter> buf(FX_Alloc(uint8_t, buf_size));
638 memcpy(buf, data, buf_size); 639 memcpy(buf.get(), data, buf_size);
639 CPDF_Stream* stream_val = arr->InsertNewAt<CPDF_Stream>( 640 CPDF_Stream* stream_val = arr->InsertNewAt<CPDF_Stream>(
640 13, buf, buf_size, pdfium::WrapUnique(stream_dict)); 641 13, std::move(buf), buf_size, pdfium::WrapUnique(stream_dict));
641 const char* const expected_str[] = { 642 const char* const expected_str[] = {
642 "true", "false", "0", "-1234", "2345", "0.05", "", 643 "true", "false", "0", "-1234", "2345", "0.05", "",
643 "It is a test!", "NAME", "test", "", "", "", ""}; 644 "It is a test!", "NAME", "test", "", "", "", ""};
644 const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0, 645 const int expected_int[] = {1, 0, 0, -1234, 2345, 0, 0,
645 0, 0, 0, 0, 0, 0, 0}; 646 0, 0, 0, 0, 0, 0, 0};
646 const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0, 647 const float expected_float[] = {0, 0, 0, -1234, 2345, 0.05f, 0,
647 0, 0, 0, 0, 0, 0, 0}; 648 0, 0, 0, 0, 0, 0, 0};
648 for (size_t i = 0; i < arr->GetCount(); ++i) { 649 for (size_t i = 0; i < arr->GetCount(); ++i) {
649 EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str()); 650 EXPECT_STREQ(expected_str[i], arr->GetStringAt(i).c_str());
650 EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i)); 651 EXPECT_EQ(expected_int[i], arr->GetIntegerAt(i));
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
870 CPDF_Object* pObj = dict->SetNewFor<CPDF_Number>("clams", 42); 871 CPDF_Object* pObj = dict->SetNewFor<CPDF_Number>("clams", 42);
871 dict->ConvertToIndirectObjectFor("clams", &objects_holder); 872 dict->ConvertToIndirectObjectFor("clams", &objects_holder);
872 CPDF_Object* pRef = dict->GetObjectFor("clams"); 873 CPDF_Object* pRef = dict->GetObjectFor("clams");
873 CPDF_Object* pNum = dict->GetDirectObjectFor("clams"); 874 CPDF_Object* pNum = dict->GetDirectObjectFor("clams");
874 EXPECT_TRUE(pRef->IsReference()); 875 EXPECT_TRUE(pRef->IsReference());
875 EXPECT_TRUE(pNum->IsNumber()); 876 EXPECT_TRUE(pNum->IsNumber());
876 EXPECT_NE(pObj, pRef); 877 EXPECT_NE(pObj, pRef);
877 EXPECT_EQ(pObj, pNum); 878 EXPECT_EQ(pObj, pNum);
878 EXPECT_EQ(42, dict->GetIntegerFor("clams")); 879 EXPECT_EQ(42, dict->GetIntegerFor("clams"));
879 } 880 }
OLDNEW
« no previous file with comments | « core/fpdfapi/page/cpdf_streamparser.cpp ('k') | core/fpdfapi/parser/cpdf_stream.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698