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}; |
33 const char testdata[] = "AAA\0BBB\0"; | 36 const char testdata[] = "AAA\0BBB\0"; |
34 const int testdatalen = arraysize(testdata) - 1; | 37 const int testdatalen = arraysize(testdata) - 1; |
35 | 38 |
36 // checks that the result | 39 // checks that the results can be read correctly from the Pickle |
37 void VerifyResult(const Pickle& pickle) { | 40 void VerifyResult(const Pickle& pickle) { |
38 PickleIterator iter(pickle); | 41 PickleIterator iter(pickle); |
39 | 42 |
40 bool outbool; | 43 bool outbool; |
41 EXPECT_TRUE(iter.ReadBool(&outbool)); | 44 EXPECT_TRUE(iter.ReadBool(&outbool)); |
42 EXPECT_FALSE(outbool); | 45 EXPECT_FALSE(outbool); |
43 EXPECT_TRUE(iter.ReadBool(&outbool)); | 46 EXPECT_TRUE(iter.ReadBool(&outbool)); |
44 EXPECT_TRUE(outbool); | 47 EXPECT_TRUE(outbool); |
45 | 48 |
46 int outint; | 49 int outint; |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
84 EXPECT_EQ(teststring, outstring); | 87 EXPECT_EQ(teststring, outstring); |
85 | 88 |
86 std::wstring outwstring; | 89 std::wstring outwstring; |
87 EXPECT_TRUE(iter.ReadWString(&outwstring)); | 90 EXPECT_TRUE(iter.ReadWString(&outwstring)); |
88 EXPECT_EQ(testwstring, outwstring); | 91 EXPECT_EQ(testwstring, outwstring); |
89 | 92 |
90 base::string16 outstring16; | 93 base::string16 outstring16; |
91 EXPECT_TRUE(iter.ReadString16(&outstring16)); | 94 EXPECT_TRUE(iter.ReadString16(&outstring16)); |
92 EXPECT_EQ(teststring16, outstring16); | 95 EXPECT_EQ(teststring16, outstring16); |
93 | 96 |
| 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 |
94 const char* outdata; | 105 const char* outdata; |
95 int outdatalen; | 106 int outdatalen; |
96 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); | 107 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
97 EXPECT_EQ(testdatalen, outdatalen); | 108 EXPECT_EQ(testdatalen, outdatalen); |
98 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 109 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
99 | 110 |
100 // reads past the end should fail | 111 // reads past the end should fail |
101 EXPECT_FALSE(iter.ReadInt(&outint)); | 112 EXPECT_FALSE(iter.ReadInt(&outint)); |
102 } | 113 } |
103 | 114 |
(...skipping 10 matching lines...) Expand all Loading... |
114 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 125 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
115 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); | 126 EXPECT_TRUE(pickle.WriteUInt32(testuint32)); |
116 EXPECT_TRUE(pickle.WriteInt64(testint64)); | 127 EXPECT_TRUE(pickle.WriteInt64(testint64)); |
117 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); | 128 EXPECT_TRUE(pickle.WriteUInt64(testuint64)); |
118 EXPECT_TRUE(pickle.WriteSizeT(testsizet)); | 129 EXPECT_TRUE(pickle.WriteSizeT(testsizet)); |
119 EXPECT_TRUE(pickle.WriteFloat(testfloat)); | 130 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
120 EXPECT_TRUE(pickle.WriteDouble(testdouble)); | 131 EXPECT_TRUE(pickle.WriteDouble(testdouble)); |
121 EXPECT_TRUE(pickle.WriteString(teststring)); | 132 EXPECT_TRUE(pickle.WriteString(teststring)); |
122 EXPECT_TRUE(pickle.WriteWString(testwstring)); | 133 EXPECT_TRUE(pickle.WriteWString(testwstring)); |
123 EXPECT_TRUE(pickle.WriteString16(teststring16)); | 134 EXPECT_TRUE(pickle.WriteString16(teststring16)); |
| 135 EXPECT_TRUE(pickle.WriteString(testrawstring)); |
| 136 EXPECT_TRUE(pickle.WriteString16(testrawstring16)); |
124 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 137 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
125 VerifyResult(pickle); | 138 VerifyResult(pickle); |
126 | 139 |
127 // test copy constructor | 140 // test copy constructor |
128 Pickle pickle2(pickle); | 141 Pickle pickle2(pickle); |
129 VerifyResult(pickle2); | 142 VerifyResult(pickle2); |
130 | 143 |
131 // test operator= | 144 // test operator= |
132 Pickle pickle3; | 145 Pickle pickle3; |
133 pickle3 = pickle; | 146 pickle3 = pickle; |
(...skipping 286 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 433 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
421 | 434 |
422 PickleIterator iter(pickle); | 435 PickleIterator iter(pickle); |
423 const char* outdata_char = NULL; | 436 const char* outdata_char = NULL; |
424 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); | 437 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); |
425 | 438 |
426 int outdata; | 439 int outdata; |
427 memcpy(&outdata, outdata_char, sizeof(outdata)); | 440 memcpy(&outdata, outdata_char, sizeof(outdata)); |
428 EXPECT_EQ(data, outdata); | 441 EXPECT_EQ(data, outdata); |
429 } | 442 } |
OLD | NEW |