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/string16.h" | 10 #include "base/string16.h" |
11 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
12 | 12 |
13 namespace { | 13 namespace { |
14 | 14 |
15 const int testint = 2093847192; | 15 const int testint = 2093847192; |
16 const std::string teststr("Hello world"); // note non-aligned string length | 16 const std::string teststr("Hello world"); // note non-aligned string length |
17 const std::wstring testwstr(L"Hello, world"); | 17 const std::wstring testwstr(L"Hello, world"); |
18 const char testdata[] = "AAA\0BBB\0"; | 18 const char testdata[] = "AAA\0BBB\0"; |
19 const int testdatalen = arraysize(testdata) - 1; | 19 const int testdatalen = arraysize(testdata) - 1; |
20 const bool testbool1 = false; | 20 const bool testbool1 = false; |
21 const bool testbool2 = true; | 21 const bool testbool2 = true; |
22 const uint16 testuint16 = 32123; | 22 const uint16 testuint16 = 32123; |
| 23 const float testfloat = 3.1415926935f; |
23 | 24 |
24 // checks that the result | 25 // checks that the result |
25 void VerifyResult(const Pickle& pickle) { | 26 void VerifyResult(const Pickle& pickle) { |
26 PickleIterator iter(pickle); | 27 PickleIterator iter(pickle); |
27 | 28 |
28 int outint; | 29 int outint; |
29 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | 30 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
30 EXPECT_EQ(testint, outint); | 31 EXPECT_EQ(testint, outint); |
31 | 32 |
32 std::string outstr; | 33 std::string outstr; |
33 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 34 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
34 EXPECT_EQ(teststr, outstr); | 35 EXPECT_EQ(teststr, outstr); |
35 | 36 |
36 std::wstring outwstr; | 37 std::wstring outwstr; |
37 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); | 38 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); |
38 EXPECT_EQ(testwstr, outwstr); | 39 EXPECT_EQ(testwstr, outwstr); |
39 | 40 |
40 bool outbool; | 41 bool outbool; |
41 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 42 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
42 EXPECT_EQ(testbool1, outbool); | 43 EXPECT_EQ(testbool1, outbool); |
43 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 44 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
44 EXPECT_EQ(testbool2, outbool); | 45 EXPECT_EQ(testbool2, outbool); |
45 | 46 |
46 uint16 outuint16; | 47 uint16 outuint16; |
47 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); | 48 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
48 EXPECT_EQ(testuint16, outuint16); | 49 EXPECT_EQ(testuint16, outuint16); |
49 | 50 |
| 51 float outfloat; |
| 52 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); |
| 53 EXPECT_EQ(testfloat, outfloat); |
| 54 |
50 const char* outdata; | 55 const char* outdata; |
51 int outdatalen; | 56 int outdatalen; |
52 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 57 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
53 EXPECT_EQ(testdatalen, outdatalen); | 58 EXPECT_EQ(testdatalen, outdatalen); |
54 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 59 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
55 | 60 |
56 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 61 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
57 EXPECT_EQ(testdatalen, outdatalen); | 62 EXPECT_EQ(testdatalen, outdatalen); |
58 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 63 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
59 | 64 |
60 // reads past the end should fail | 65 // reads past the end should fail |
61 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); | 66 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
62 } | 67 } |
63 | 68 |
64 } // namespace | 69 } // namespace |
65 | 70 |
66 TEST(PickleTest, EncodeDecode) { | 71 TEST(PickleTest, EncodeDecode) { |
67 Pickle pickle; | 72 Pickle pickle; |
68 | 73 |
69 EXPECT_TRUE(pickle.WriteInt(testint)); | 74 EXPECT_TRUE(pickle.WriteInt(testint)); |
70 EXPECT_TRUE(pickle.WriteString(teststr)); | 75 EXPECT_TRUE(pickle.WriteString(teststr)); |
71 EXPECT_TRUE(pickle.WriteWString(testwstr)); | 76 EXPECT_TRUE(pickle.WriteWString(testwstr)); |
72 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 77 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
73 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 78 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
74 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 79 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
| 80 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
75 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 81 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
76 | 82 |
77 // Over allocate BeginWriteData so we can test TrimWriteData. | 83 // Over allocate BeginWriteData so we can test TrimWriteData. |
78 char* dest = pickle.BeginWriteData(testdatalen + 100); | 84 char* dest = pickle.BeginWriteData(testdatalen + 100); |
79 EXPECT_TRUE(dest); | 85 EXPECT_TRUE(dest); |
80 memcpy(dest, testdata, testdatalen); | 86 memcpy(dest, testdata, testdatalen); |
81 | 87 |
82 pickle.TrimWriteData(testdatalen); | 88 pickle.TrimWriteData(testdatalen); |
83 | 89 |
84 VerifyResult(pickle); | 90 VerifyResult(pickle); |
(...skipping 242 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
327 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 333 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
328 | 334 |
329 PickleIterator iter(pickle); | 335 PickleIterator iter(pickle); |
330 const char* outdata_char = NULL; | 336 const char* outdata_char = NULL; |
331 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 337 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
332 | 338 |
333 int outdata; | 339 int outdata; |
334 memcpy(&outdata, outdata_char, sizeof(outdata)); | 340 memcpy(&outdata, outdata_char, sizeof(outdata)); |
335 EXPECT_EQ(data, outdata); | 341 EXPECT_EQ(data, outdata); |
336 } | 342 } |
OLD | NEW |