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" |
11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
13 | 13 |
14 // Remove when this file is in the base namespace. | 14 namespace base { |
15 using base::string16; | |
16 | 15 |
17 namespace { | 16 namespace { |
18 | 17 |
19 const bool testbool1 = false; | 18 const bool testbool1 = false; |
20 const bool testbool2 = true; | 19 const bool testbool2 = true; |
21 const int testint = 2093847192; | 20 const int testint = 2093847192; |
22 const long testlong = 1093847192; | 21 const long testlong = 1093847192; |
23 const uint16 testuint16 = 32123; | 22 const uint16 testuint16 = 32123; |
24 const uint32 testuint32 = 1593847192; | 23 const uint32 testuint32 = 1593847192; |
25 const int64 testint64 = -0x7E8CA9253104BDFCLL; | 24 const int64 testint64 = -0x7E8CA9253104BDFCLL; |
26 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; | 25 const uint64 testuint64 = 0xCE8CA9253104BDF7ULL; |
27 const size_t testsizet = 0xFEDC7654; | 26 const size_t testsizet = 0xFEDC7654; |
28 const float testfloat = 3.1415926935f; | 27 const float testfloat = 3.1415926935f; |
29 const double testdouble = 2.71828182845904523; | 28 const double testdouble = 2.71828182845904523; |
30 const std::string teststring("Hello world"); // note non-aligned string length | 29 const std::string teststring("Hello world"); // note non-aligned string length |
31 const std::wstring testwstring(L"Hello, world"); | 30 const std::wstring testwstring(L"Hello, world"); |
32 const base::string16 teststring16(base::ASCIIToUTF16("Hello, world")); | 31 const string16 teststring16(ASCIIToUTF16("Hello, world")); |
33 const char testrawstring[] = "Hello new world"; // Test raw string writing | 32 const char testrawstring[] = "Hello new world"; // Test raw string writing |
34 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. | 33 // Test raw char16 writing, assumes UTF16 encoding is ANSI for alpha chars. |
35 const base::char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; | 34 const char16 testrawstring16[] = {'A', 'l', 'o', 'h', 'a', 0}; |
36 const char testdata[] = "AAA\0BBB\0"; | 35 const char testdata[] = "AAA\0BBB\0"; |
37 const int testdatalen = arraysize(testdata) - 1; | 36 const int testdatalen = arraysize(testdata) - 1; |
38 | 37 |
39 // checks that the results can be read correctly from the Pickle | 38 // checks that the results can be read correctly from the Pickle |
40 void VerifyResult(const Pickle& pickle) { | 39 void VerifyResult(const Pickle& pickle) { |
41 PickleIterator iter(pickle); | 40 PickleIterator iter(pickle); |
42 | 41 |
43 bool outbool; | 42 bool outbool; |
44 EXPECT_TRUE(iter.ReadBool(&outbool)); | 43 EXPECT_TRUE(iter.ReadBool(&outbool)); |
45 EXPECT_FALSE(outbool); | 44 EXPECT_FALSE(outbool); |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
79 EXPECT_EQ(testfloat, outfloat); | 78 EXPECT_EQ(testfloat, outfloat); |
80 | 79 |
81 double outdouble; | 80 double outdouble; |
82 EXPECT_TRUE(iter.ReadDouble(&outdouble)); | 81 EXPECT_TRUE(iter.ReadDouble(&outdouble)); |
83 EXPECT_EQ(testdouble, outdouble); | 82 EXPECT_EQ(testdouble, outdouble); |
84 | 83 |
85 std::string outstring; | 84 std::string outstring; |
86 EXPECT_TRUE(iter.ReadString(&outstring)); | 85 EXPECT_TRUE(iter.ReadString(&outstring)); |
87 EXPECT_EQ(teststring, outstring); | 86 EXPECT_EQ(teststring, outstring); |
88 | 87 |
89 base::string16 outstring16; | 88 string16 outstring16; |
90 EXPECT_TRUE(iter.ReadString16(&outstring16)); | 89 EXPECT_TRUE(iter.ReadString16(&outstring16)); |
91 EXPECT_EQ(teststring16, outstring16); | 90 EXPECT_EQ(teststring16, outstring16); |
92 | 91 |
93 base::StringPiece outstringpiece; | 92 StringPiece outstringpiece; |
94 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece)); | 93 EXPECT_TRUE(iter.ReadStringPiece(&outstringpiece)); |
95 EXPECT_EQ(testrawstring, outstringpiece); | 94 EXPECT_EQ(testrawstring, outstringpiece); |
96 | 95 |
97 base::StringPiece16 outstringpiece16; | 96 StringPiece16 outstringpiece16; |
98 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16)); | 97 EXPECT_TRUE(iter.ReadStringPiece16(&outstringpiece16)); |
99 EXPECT_EQ(testrawstring16, outstringpiece16); | 98 EXPECT_EQ(testrawstring16, outstringpiece16); |
100 | 99 |
101 const char* outdata; | 100 const char* outdata; |
102 int outdatalen; | 101 int outdatalen; |
103 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); | 102 EXPECT_TRUE(iter.ReadData(&outdata, &outdatalen)); |
104 EXPECT_EQ(testdatalen, outdatalen); | 103 EXPECT_EQ(testdatalen, outdatalen); |
105 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 104 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
106 | 105 |
107 // reads past the end should fail | 106 // reads past the end should fail |
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
201 EXPECT_TRUE(pickle.WriteString(std::string())); | 200 EXPECT_TRUE(pickle.WriteString(std::string())); |
202 | 201 |
203 PickleIterator iter(pickle); | 202 PickleIterator iter(pickle); |
204 std::string outstr; | 203 std::string outstr; |
205 EXPECT_TRUE(iter.ReadString(&outstr)); | 204 EXPECT_TRUE(iter.ReadString(&outstr)); |
206 EXPECT_EQ("", outstr); | 205 EXPECT_EQ("", outstr); |
207 } | 206 } |
208 | 207 |
209 TEST(PickleTest, ZeroLenStr16) { | 208 TEST(PickleTest, ZeroLenStr16) { |
210 Pickle pickle; | 209 Pickle pickle; |
211 EXPECT_TRUE(pickle.WriteString16(base::string16())); | 210 EXPECT_TRUE(pickle.WriteString16(string16())); |
212 | 211 |
213 PickleIterator iter(pickle); | 212 PickleIterator iter(pickle); |
214 std::string outstr; | 213 std::string outstr; |
215 EXPECT_TRUE(iter.ReadString(&outstr)); | 214 EXPECT_TRUE(iter.ReadString(&outstr)); |
216 EXPECT_EQ("", outstr); | 215 EXPECT_EQ("", outstr); |
217 } | 216 } |
218 | 217 |
219 TEST(PickleTest, BadLenStr) { | 218 TEST(PickleTest, BadLenStr) { |
220 Pickle pickle; | 219 Pickle pickle; |
221 EXPECT_TRUE(pickle.WriteInt(-2)); | 220 EXPECT_TRUE(pickle.WriteInt(-2)); |
222 | 221 |
223 PickleIterator iter(pickle); | 222 PickleIterator iter(pickle); |
224 std::string outstr; | 223 std::string outstr; |
225 EXPECT_FALSE(iter.ReadString(&outstr)); | 224 EXPECT_FALSE(iter.ReadString(&outstr)); |
226 } | 225 } |
227 | 226 |
228 TEST(PickleTest, BadLenStr16) { | 227 TEST(PickleTest, BadLenStr16) { |
229 Pickle pickle; | 228 Pickle pickle; |
230 EXPECT_TRUE(pickle.WriteInt(-1)); | 229 EXPECT_TRUE(pickle.WriteInt(-1)); |
231 | 230 |
232 PickleIterator iter(pickle); | 231 PickleIterator iter(pickle); |
233 base::string16 outstr; | 232 string16 outstr; |
234 EXPECT_FALSE(iter.ReadString16(&outstr)); | 233 EXPECT_FALSE(iter.ReadString16(&outstr)); |
235 } | 234 } |
236 | 235 |
237 TEST(PickleTest, FindNext) { | 236 TEST(PickleTest, FindNext) { |
238 Pickle pickle; | 237 Pickle pickle; |
239 EXPECT_TRUE(pickle.WriteInt(1)); | 238 EXPECT_TRUE(pickle.WriteInt(1)); |
240 EXPECT_TRUE(pickle.WriteString("Domo")); | 239 EXPECT_TRUE(pickle.WriteString("Domo")); |
241 | 240 |
242 const char* start = reinterpret_cast<const char*>(pickle.data()); | 241 const char* start = reinterpret_cast<const char*>(pickle.data()); |
243 const char* end = start + pickle.size(); | 242 const char* end = start + pickle.size(); |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
421 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 420 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
422 | 421 |
423 PickleIterator iter(pickle); | 422 PickleIterator iter(pickle); |
424 const char* outdata_char = NULL; | 423 const char* outdata_char = NULL; |
425 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); | 424 EXPECT_TRUE(iter.ReadBytes(&outdata_char, sizeof(data))); |
426 | 425 |
427 int outdata; | 426 int outdata; |
428 memcpy(&outdata, outdata_char, sizeof(outdata)); | 427 memcpy(&outdata, outdata_char, sizeof(outdata)); |
429 EXPECT_EQ(data, outdata); | 428 EXPECT_EQ(data, outdata); |
430 } | 429 } |
| 430 |
| 431 } // namespace base |
OLD | NEW |