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 "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 uintptr_t testuintptr_t = 42; |
23 const float testfloat = 3.1415926935f; | 24 const float testfloat = 3.1415926935f; |
24 | 25 |
25 // checks that the result | 26 // checks that the result |
26 void VerifyResult(const Pickle& pickle) { | 27 void VerifyResult(const Pickle& pickle) { |
27 PickleIterator iter(pickle); | 28 PickleIterator iter(pickle); |
28 | 29 |
29 int outint; | 30 int outint; |
30 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | 31 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
31 EXPECT_EQ(testint, outint); | 32 EXPECT_EQ(testint, outint); |
32 | 33 |
33 std::string outstr; | 34 std::string outstr; |
34 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); | 35 EXPECT_TRUE(pickle.ReadString(&iter, &outstr)); |
35 EXPECT_EQ(teststr, outstr); | 36 EXPECT_EQ(teststr, outstr); |
36 | 37 |
37 std::wstring outwstr; | 38 std::wstring outwstr; |
38 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); | 39 EXPECT_TRUE(pickle.ReadWString(&iter, &outwstr)); |
39 EXPECT_EQ(testwstr, outwstr); | 40 EXPECT_EQ(testwstr, outwstr); |
40 | 41 |
41 bool outbool; | 42 bool outbool; |
42 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 43 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
43 EXPECT_FALSE(outbool); | 44 EXPECT_FALSE(outbool); |
44 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); | 45 EXPECT_TRUE(pickle.ReadBool(&iter, &outbool)); |
45 EXPECT_TRUE(outbool); | 46 EXPECT_TRUE(outbool); |
46 | 47 |
47 uint16 outuint16; | 48 uint16 outuint16; |
48 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); | 49 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
49 EXPECT_EQ(testuint16, outuint16); | 50 EXPECT_EQ(testuint16, outuint16); |
50 | 51 |
| 52 uintptr_t outuintptr_t; |
| 53 EXPECT_TRUE(pickle.ReadUIntPtr(&iter, &outuintptr_t)); |
| 54 EXPECT_EQ(testuintptr_t, outuintptr_t); |
| 55 |
51 float outfloat; | 56 float outfloat; |
52 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); | 57 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); |
53 EXPECT_EQ(testfloat, outfloat); | 58 EXPECT_EQ(testfloat, outfloat); |
54 | 59 |
55 const char* outdata; | 60 const char* outdata; |
56 int outdatalen; | 61 int outdatalen; |
57 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 62 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
58 EXPECT_EQ(testdatalen, outdatalen); | 63 EXPECT_EQ(testdatalen, outdatalen); |
59 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 64 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
60 | 65 |
61 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 66 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
62 EXPECT_EQ(testdatalen, outdatalen); | 67 EXPECT_EQ(testdatalen, outdatalen); |
63 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 68 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
64 | 69 |
65 // reads past the end should fail | 70 // reads past the end should fail |
66 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); | 71 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
67 } | 72 } |
68 | 73 |
69 } // namespace | 74 } // namespace |
70 | 75 |
71 TEST(PickleTest, EncodeDecode) { | 76 TEST(PickleTest, EncodeDecode) { |
72 Pickle pickle; | 77 Pickle pickle; |
73 | 78 |
74 EXPECT_TRUE(pickle.WriteInt(testint)); | 79 EXPECT_TRUE(pickle.WriteInt(testint)); |
75 EXPECT_TRUE(pickle.WriteString(teststr)); | 80 EXPECT_TRUE(pickle.WriteString(teststr)); |
76 EXPECT_TRUE(pickle.WriteWString(testwstr)); | 81 EXPECT_TRUE(pickle.WriteWString(testwstr)); |
77 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 82 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
78 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 83 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
79 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 84 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
| 85 EXPECT_TRUE(pickle.WriteUIntPtr(testuintptr_t)); |
80 EXPECT_TRUE(pickle.WriteFloat(testfloat)); | 86 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
81 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 87 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
82 | 88 |
83 // Over allocate BeginWriteData so we can test TrimWriteData. | 89 // Over allocate BeginWriteData so we can test TrimWriteData. |
84 char* dest = pickle.BeginWriteData(testdatalen + 100); | 90 char* dest = pickle.BeginWriteData(testdatalen + 100); |
85 EXPECT_TRUE(dest); | 91 EXPECT_TRUE(dest); |
86 memcpy(dest, testdata, testdatalen); | 92 memcpy(dest, testdata, testdatalen); |
87 | 93 |
88 pickle.TrimWriteData(testdatalen); | 94 pickle.TrimWriteData(testdatalen); |
89 | 95 |
(...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
333 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 339 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
334 | 340 |
335 PickleIterator iter(pickle); | 341 PickleIterator iter(pickle); |
336 const char* outdata_char = NULL; | 342 const char* outdata_char = NULL; |
337 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 343 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
338 | 344 |
339 int outdata; | 345 int outdata; |
340 memcpy(&outdata, outdata_char, sizeof(outdata)); | 346 memcpy(&outdata, outdata_char, sizeof(outdata)); |
341 EXPECT_EQ(data, outdata); | 347 EXPECT_EQ(data, outdata); |
342 } | 348 } |
OLD | NEW |