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(
15 EXPECT_EQ(10U, mem
->size());
16 EXPECT_EQ("static mem", std::string(mem
->front_as
<char>(), mem
->size()));
19 TEST(RefCountedMemoryUnitTest
, RefCountedBytes
) {
20 std::vector
<uint8
> data
;
23 scoped_refptr
<RefCountedMemory
> mem
= RefCountedBytes::TakeVector(&data
);
25 EXPECT_EQ(0U, data
.size());
27 EXPECT_EQ(2U, mem
->size());
28 EXPECT_EQ(45U, mem
->front()[0]);
29 EXPECT_EQ(99U, mem
->front()[1]);
31 scoped_refptr
<RefCountedMemory
> mem2
;
33 unsigned char data2
[] = { 12, 11, 99 };
34 mem2
= new RefCountedBytes(data2
, 3);
36 EXPECT_EQ(3U, mem2
->size());
37 EXPECT_EQ(12U, mem2
->front()[0]);
38 EXPECT_EQ(11U, mem2
->front()[1]);
39 EXPECT_EQ(99U, mem2
->front()[2]);
42 TEST(RefCountedMemoryUnitTest
, RefCountedString
) {
43 std::string
s("destroy me");
44 scoped_refptr
<RefCountedMemory
> mem
= RefCountedString::TakeString(&s
);
46 EXPECT_EQ(0U, s
.size());
48 EXPECT_EQ(10U, mem
->size());
49 EXPECT_EQ('d', mem
->front()[0]);
50 EXPECT_EQ('e', mem
->front()[1]);
53 TEST(RefCountedMemoryUnitTest
, RefCountedMallocedMemory
) {
54 void* data
= malloc(6);
55 memcpy(data
, "hello", 6);
57 scoped_refptr
<RefCountedMemory
> mem
= new RefCountedMallocedMemory(data
, 6);
59 EXPECT_EQ(6U, mem
->size());
60 EXPECT_EQ(0, memcmp("hello", mem
->front(), 6));
63 TEST(RefCountedMemoryUnitTest
, Equals
) {
64 std::string
s1("same");
65 scoped_refptr
<RefCountedMemory
> mem1
= RefCountedString::TakeString(&s1
);
67 std::vector
<unsigned char> d2
;
72 scoped_refptr
<RefCountedMemory
> mem2
= RefCountedBytes::TakeVector(&d2
);
74 EXPECT_TRUE(mem1
->Equals(mem2
));
76 std::string
s3("diff");
77 scoped_refptr
<RefCountedMemory
> mem3
= RefCountedString::TakeString(&s3
);
79 EXPECT_FALSE(mem1
->Equals(mem3
));
80 EXPECT_FALSE(mem2
->Equals(mem3
));
83 TEST(RefCountedMemoryUnitTest
, EqualsNull
) {
85 scoped_refptr
<RefCountedMemory
> mem
= RefCountedString::TakeString(&s
);
86 EXPECT_FALSE(mem
->Equals(NULL
));