| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium 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 <string> | 5 #include <string> |
| 6 | 6 |
| 7 #include "base/basictypes.h" | 7 #include "base/basictypes.h" |
| 8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
| 9 #include "base/pickle.h" | 9 #include "base/pickle.h" |
| 10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 const uint16 testuint16 = 32123; | 23 const uint16 testuint16 = 32123; |
| 24 const uint32 testuint32 = 1593847192; | 24 const uint32 testuint32 = 1593847192; |
| 25 const int64 testint64 = -0x7E8CA9253104BDFCLL; | 25 const int64 testint64 = -0x7E8CA9253104BDFCLL; |
| 26 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; | 26 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; |
| 27 const size_t testsizet = 0xFEDC7654; | 27 const size_t testsizet = 0xFEDC7654; |
| 28 const float testfloat = 3.1415926935f; | 28 const float testfloat = 3.1415926935f; |
| 29 const double testdouble = 2.71828182845904523; | 29 const double testdouble = 2.71828182845904523; |
| 30 const std::string teststring("Hello world"); // note non-aligned string length | 30 const std::string teststring("Hello world"); // note non-aligned string length |
| 31 const std::wstring testwstring(L"Hello, world"); | 31 const std::wstring testwstring(L"Hello, world"); |
| 32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world")); | 32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world")); |
| 33 const char testrawstring[] = "Hello new world"; // Test raw string writing | |
| 34 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. | |
| 35 const base::char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; | |
| 36 const char testdata[] = "AAA\0BBB\0"; | 33 const char testdata[] = "AAA\0BBB\0"; |
| 37 const int testdatalen = arraysize(testdata) - 1; | 34 const int testdatalen = arraysize(testdata) - 1; |
| 38 | 35 |
| 39 // checks that the results can be read correctly from the Pickle | 36 // checks that the result |
| 40 void VerifyResult(const Pickle& pickle) { | 37 void VerifyResult(const Pickle& pickle) { |
| 41 PickleIterator iter(pickle); | 38 PickleIterator iter(pickle); |
| 42 | 39 |
| 43 bool outbool; | 40 bool outbool; |
| 44 EXPECT_TRUE(iter.ReadBool(&outbool)); | 41 EXPECT_TRUE(iter.ReadBool(&outbool)); |
| 45 EXPECT_FALSE(outbool); | 42 EXPECT_FALSE(outbool); |
| 46 EXPECT_TRUE(iter.ReadBool(&outbool)); | 43 EXPECT_TRUE(iter.ReadBool(&outbool)); |
| 47 EXPECT_TRUE(outbool); | 44 EXPECT_TRUE(outbool); |
| 48 | 45 |
| 49 int outint; | 46 int outint; |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 87 EXPECT_EQ(teststring, outstring); | 84 EXPECT_EQ(teststring, outstring); |
| 88 | 85 |
| 89 std::wstring outwstring; | 86 std::wstring outwstring; |
| 90 EXPECT_TRUE(iter.ReadWString(&outwstring)); | 87 EXPECT_TRUE(iter.ReadWString(&outwstring)); |
| 91 EXPECT_EQ(testwstring, outwstring); | 88 EXPECT_EQ(testwstring, outwstring); |
| 92 | 89 |
| 93 base::string16 outstring16; | 90 base::string16 outstring16; |
| 94 EXPECT_TRUE(iter.ReadString16(&outstring16)); | 91 EXPECT_TRUE(iter.ReadString16(&outstring16)); |
| 95 EXPECT_EQ(teststring16, outstring16); | 92 EXPECT_EQ(teststring16, outstring16); |
| 96 | 93 |
| 97 base::StringPiece outstringpiece; | |
| 98 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece)); | |
| 99 EXPECT_EQ(testrawstring, outstringpiece); | |
| 100 | |
| 101 base::StringPiece16 outstringpiece16; | |
| 102 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16)); | |
| 103 EXPECT_EQ(testrawstring16, outstringpiece16); | |
| 104 | |
| 105 const char* outdata; | 94 const char* outdata; |
| 106 int outdatalen; | 95 int outdatalen; |
| 107 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); | 96 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
| 108 EXPECT_EQ(testdatalen, outdatalen); | 97 EXPECT_EQ(testdatalen, outdatalen); |
| 109 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 98 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
| 110 | 99 |
| 111 // reads past the end should fail | 100 // reads past the end should fail |
| 112 EXPECT_FALSE(iter.ReadInt(&outint)); | 101 EXPECT_FALSE(iter.ReadInt(&outint)); |
| 113 } | 102 } |
| 114 | 103 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 125 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 114 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
| 126 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); | 115 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); |
| 127 EXPECT_TRUE(pickle.WriteInt64(testint64)); | 116 EXPECT_TRUE(pickle.WriteInt64(testint64)); |
| 128 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); | 117 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
| 129 EXPECT_TRUE(pickle.WriteSizeT(testsizet)); | 118 EXPECT_TRUE(pickle.WriteSizeT(testsizet)); |
| 130 EXPECT_TRUE(pickle.WriteFloat(testfloat)); | 119 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
| 131 EXPECT_TRUE(pickle.WriteDouble(testdouble)); | 120 EXPECT_TRUE(pickle.WriteDouble(testdouble)); |
| 132 EXPECT_TRUE(pickle.WriteString(teststring)); | 121 EXPECT_TRUE(pickle.WriteString(teststring)); |
| 133 EXPECT_TRUE(pickle.WriteWString(testwstring)); | 122 EXPECT_TRUE(pickle.WriteWString(testwstring)); |
| 134 EXPECT_TRUE(pickle.WriteString16(teststring16)); | 123 EXPECT_TRUE(pickle.WriteString16(teststring16)); |
| 135 EXPECT_TRUE(pickle.WriteString(testrawstring)); | |
| 136 EXPECT_TRUE(pickle.WriteString16(testrawstring16)); | |
| 137 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 124 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
| 138 VerifyResult(pickle); | 125 VerifyResult(pickle); |
| 139 | 126 |
| 140 // test copy constructor | 127 // test copy constructor |
| 141 Pickle pickle2(pickle); | 128 Pickle pickle2(pickle); |
| 142 VerifyResult(pickle2); | 129 VerifyResult(pickle2); |
| 143 | 130 |
| 144 // test operator= | 131 // test operator= |
| 145 Pickle pickle3; | 132 Pickle pickle3; |
| 146 pickle3 = pickle; | 133 pickle3 = pickle; |
| (...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 433 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
| 434 | 421 |
| 435 PickleIterator iter(pickle); | 422 PickleIterator iter(pickle); |
| 436 const char* outdata_char = NULL; | 423 const char* outdata_char = NULL; |
| 437 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); | 424 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); |
| 438 | 425 |
| 439 int outdata; | 426 int outdata; |
| 440 memcpy(&outdata, outdata_char, sizeof(outdata)); | 427 memcpy(&outdata, outdata_char, sizeof(outdata)); |
| 441 EXPECT_EQ(data, outdata); | 428 EXPECT_EQ(data, outdata); |
| 442 } | 429 } |
| OLD | NEW |