OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 "base/memory/ref_counted_memory.h" | 5 #include "base/memory/ref_counted_memory.h" |
6 | 6 |
7 #include "testing/gtest/include/gtest/gtest.h" | 7 #include "testing/gtest/include/gtest/gtest.h" |
8 | 8 |
9 namespace base { | 9 namespace base { |
10 | 10 |
(...skipping 24 matching lines...) Expand all Loading... |
35 std::string s("destroy me"); | 35 std::string s("destroy me"); |
36 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); | 36 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); |
37 | 37 |
38 EXPECT_EQ(0U, s.size()); | 38 EXPECT_EQ(0U, s.size()); |
39 | 39 |
40 EXPECT_EQ(10U, mem->size()); | 40 EXPECT_EQ(10U, mem->size()); |
41 EXPECT_EQ('d', mem->front()[0]); | 41 EXPECT_EQ('d', mem->front()[0]); |
42 EXPECT_EQ('e', mem->front()[1]); | 42 EXPECT_EQ('e', mem->front()[1]); |
43 } | 43 } |
44 | 44 |
| 45 TEST(RefCountedMemoryUnitTest, RefCountedMallocedMemory) { |
| 46 void* data = malloc(6); |
| 47 memcpy(data, "hello", 6); |
| 48 |
| 49 scoped_refptr<RefCountedMemory> mem = new RefCountedMallocedMemory(data, 6); |
| 50 |
| 51 EXPECT_EQ(6U, mem->size()); |
| 52 EXPECT_EQ(0, memcmp("hello", mem->front(), 6)); |
| 53 } |
| 54 |
45 TEST(RefCountedMemoryUnitTest, Equals) { | 55 TEST(RefCountedMemoryUnitTest, Equals) { |
46 std::string s1("same"); | 56 std::string s1("same"); |
47 scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1); | 57 scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1); |
48 | 58 |
49 std::vector<unsigned char> d2; | 59 std::vector<unsigned char> d2; |
50 d2.push_back('s'); | 60 d2.push_back('s'); |
51 d2.push_back('a'); | 61 d2.push_back('a'); |
52 d2.push_back('m'); | 62 d2.push_back('m'); |
53 d2.push_back('e'); | 63 d2.push_back('e'); |
54 scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2); | 64 scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2); |
55 | 65 |
56 EXPECT_TRUE(mem1->Equals(mem2)); | 66 EXPECT_TRUE(mem1->Equals(mem2)); |
57 | 67 |
58 std::string s3("diff"); | 68 std::string s3("diff"); |
59 scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3); | 69 scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3); |
60 | 70 |
61 EXPECT_FALSE(mem1->Equals(mem3)); | 71 EXPECT_FALSE(mem1->Equals(mem3)); |
62 EXPECT_FALSE(mem2->Equals(mem3)); | 72 EXPECT_FALSE(mem2->Equals(mem3)); |
63 } | 73 } |
64 | 74 |
65 TEST(RefCountedMemoryUnitTest, EqualsNull) { | 75 TEST(RefCountedMemoryUnitTest, EqualsNull) { |
66 std::string s("str"); | 76 std::string s("str"); |
67 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); | 77 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s); |
68 EXPECT_FALSE(mem->Equals(NULL)); | 78 EXPECT_FALSE(mem->Equals(NULL)); |
69 } | 79 } |
70 | 80 |
71 } // namespace base | 81 } // namespace base |
OLD | NEW |