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 // Remove when this file is in the base namespace. | 13 // Remove when this file is in the base namespace. |
14 using base::string16; | 14 using base::string16; |
15 | 15 |
16 namespace { | 16 namespace { |
17 | 17 |
18 const int testint = 2093847192; | 18 const int testint = 2093847192; |
19 const std::string teststr("Hello world"); // note non-aligned string length | 19 const std::string teststr("Hello world"); // note non-aligned string length |
20 const std::wstring testwstr(L"Hello, world"); | 20 const std::wstring testwstr(L"Hello, world"); |
21 const char testdata[] = "AAA\0BBB\0"; | 21 const char testdata[] = "AAA\0BBB\0"; |
22 const int testdatalen = arraysize(testdata) - 1; | 22 const int testdatalen = arraysize(testdata) - 1; |
23 const bool testbool1 = false; | 23 const bool testbool1 = false; |
24 const bool testbool2 = true; | 24 const bool testbool2 = true; |
25 const uint16 testuint16 = 32123; | 25 const uint16 testuint16 = 32123; |
26 const float testfloat = 3.1415926935f; | 26 const float testfloat = 3.1415926935f; |
| 27 const double testdouble = 2.71828182845904523; |
27 | 28 |
28 // checks that the result | 29 // checks that the result |
29 void VerifyResult(const Pickle& pickle) { | 30 void VerifyResult(const Pickle& pickle) { |
30 PickleIterator iter(pickle); | 31 PickleIterator iter(pickle); |
31 | 32 |
32 int outint; | 33 int outint; |
33 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); | 34 EXPECT_TRUE(pickle.ReadInt(&iter, &outint)); |
34 EXPECT_EQ(testint, outint); | 35 EXPECT_EQ(testint, outint); |
35 | 36 |
36 std::string outstr; | 37 std::string outstr; |
(...skipping 11 matching lines...) Expand all Loading... |
48 EXPECT_TRUE(outbool); | 49 EXPECT_TRUE(outbool); |
49 | 50 |
50 uint16 outuint16; | 51 uint16 outuint16; |
51 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); | 52 EXPECT_TRUE(pickle.ReadUInt16(&iter, &outuint16)); |
52 EXPECT_EQ(testuint16, outuint16); | 53 EXPECT_EQ(testuint16, outuint16); |
53 | 54 |
54 float outfloat; | 55 float outfloat; |
55 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); | 56 EXPECT_TRUE(pickle.ReadFloat(&iter, &outfloat)); |
56 EXPECT_EQ(testfloat, outfloat); | 57 EXPECT_EQ(testfloat, outfloat); |
57 | 58 |
| 59 double outdouble; |
| 60 EXPECT_TRUE(pickle.ReadDouble(&iter, &outdouble)); |
| 61 EXPECT_EQ(testdouble, outdouble); |
| 62 |
58 const char* outdata; | 63 const char* outdata; |
59 int outdatalen; | 64 int outdatalen; |
60 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); | 65 EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); |
61 EXPECT_EQ(testdatalen, outdatalen); | 66 EXPECT_EQ(testdatalen, outdatalen); |
62 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); | 67 EXPECT_EQ(memcmp(testdata, outdata, outdatalen), 0); |
63 | 68 |
64 // reads past the end should fail | 69 // reads past the end should fail |
65 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); | 70 EXPECT_FALSE(pickle.ReadInt(&iter, &outint)); |
66 } | 71 } |
67 | 72 |
68 } // namespace | 73 } // namespace |
69 | 74 |
70 TEST(PickleTest, EncodeDecode) { | 75 TEST(PickleTest, EncodeDecode) { |
71 Pickle pickle; | 76 Pickle pickle; |
72 | 77 |
73 EXPECT_TRUE(pickle.WriteInt(testint)); | 78 EXPECT_TRUE(pickle.WriteInt(testint)); |
74 EXPECT_TRUE(pickle.WriteString(teststr)); | 79 EXPECT_TRUE(pickle.WriteString(teststr)); |
75 EXPECT_TRUE(pickle.WriteWString(testwstr)); | 80 EXPECT_TRUE(pickle.WriteWString(testwstr)); |
76 EXPECT_TRUE(pickle.WriteBool(testbool1)); | 81 EXPECT_TRUE(pickle.WriteBool(testbool1)); |
77 EXPECT_TRUE(pickle.WriteBool(testbool2)); | 82 EXPECT_TRUE(pickle.WriteBool(testbool2)); |
78 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); | 83 EXPECT_TRUE(pickle.WriteUInt16(testuint16)); |
79 EXPECT_TRUE(pickle.WriteFloat(testfloat)); | 84 EXPECT_TRUE(pickle.WriteFloat(testfloat)); |
| 85 EXPECT_TRUE(pickle.WriteDouble(testdouble)); |
80 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); | 86 EXPECT_TRUE(pickle.WriteData(testdata, testdatalen)); |
81 VerifyResult(pickle); | 87 VerifyResult(pickle); |
82 | 88 |
83 // test copy constructor | 89 // test copy constructor |
84 Pickle pickle2(pickle); | 90 Pickle pickle2(pickle); |
85 VerifyResult(pickle2); | 91 VerifyResult(pickle2); |
86 | 92 |
87 // test operator= | 93 // test operator= |
88 Pickle pickle3; | 94 Pickle pickle3; |
89 pickle3 = pickle; | 95 pickle3 = pickle; |
(...skipping 265 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
355 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); | 361 EXPECT_TRUE(pickle.WriteBytes(&data, sizeof(data))); |
356 | 362 |
357 PickleIterator iter(pickle); | 363 PickleIterator iter(pickle); |
358 const char* outdata_char = NULL; | 364 const char* outdata_char = NULL; |
359 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); | 365 EXPECT_TRUE(pickle.ReadBytes(&iter, &outdata_char, sizeof(data))); |
360 | 366 |
361 int outdata; | 367 int outdata; |
362 memcpy(&outdata, outdata_char, sizeof(outdata)); | 368 memcpy(&outdata, outdata_char, sizeof(outdata)); |
363 EXPECT_EQ(data, outdata); | 369 EXPECT_EQ(data, outdata); |
364 } | 370 } |
OLD | NEW |