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
3 // found in the LICENSE file.
5 #include "base/memory/ref_counted_memory.h"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(RefCountedMemoryUnitTest
, RefCountedStaticMemory
) {
12 scoped_refptr
<RefCountedMemory
> mem
= new RefCountedStaticMemory(
13 reinterpret_cast<const uint8
*>("static mem00"), 10);
15 EXPECT_EQ(10U, mem
->size());
16 EXPECT_EQ("static mem",
17 std::string(reinterpret_cast<const char*>(mem
->front()),
21 TEST(RefCountedMemoryUnitTest
, RefCountedBytes
) {
22 std::vector
<uint8
> data
;
25 scoped_refptr
<RefCountedMemory
> mem
= RefCountedBytes::TakeVector(&data
);
27 EXPECT_EQ(0U, data
.size());
29 EXPECT_EQ(2U, mem
->size());
30 EXPECT_EQ(45U, mem
->front()[0]);
31 EXPECT_EQ(99U, mem
->front()[1]);
34 TEST(RefCountedMemoryUnitTest
, RefCountedString
) {
35 std::string
s("destroy me");
36 scoped_refptr
<RefCountedMemory
> mem
= RefCountedString::TakeString(&s
);
38 EXPECT_EQ(0U, s
.size());
40 EXPECT_EQ(10U, mem
->size());
41 EXPECT_EQ('d', mem
->front()[0]);
42 EXPECT_EQ('e', mem
->front()[1]);
45 TEST(RefCountedMemoryUnitTest
, Equals
) {
46 std::string
s1("same");
47 scoped_refptr
<RefCountedMemory
> mem1
= RefCountedString::TakeString(&s1
);
49 std::vector
<unsigned char> d2
;
54 scoped_refptr
<RefCountedMemory
> mem2
= RefCountedBytes::TakeVector(&d2
);
56 EXPECT_TRUE(mem1
->Equals(mem2
));
58 std::string
s3("diff");
59 scoped_refptr
<RefCountedMemory
> mem3
= RefCountedString::TakeString(&s3
);
61 EXPECT_FALSE(mem1
->Equals(mem3
));
62 EXPECT_FALSE(mem2
->Equals(mem3
));
65 TEST(RefCountedMemoryUnitTest
, EqualsNull
) {
67 scoped_refptr
<RefCountedMemory
> mem
= RefCountedString::TakeString(&s
);
68 EXPECT_FALSE(mem
->Equals(NULL
));