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]);
32 TEST(RefCountedMemoryUnitTest, RefCountedString) {
33 std::string s("destroy me");
34 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
36 EXPECT_EQ(0U, s.size());
38 EXPECT_EQ(10U, mem->size());
39 EXPECT_EQ('d', mem->front()[0]);
40 EXPECT_EQ('e', mem->front()[1]);
43 TEST(RefCountedMemoryUnitTest, RefCountedMallocedMemory) {
44 void* data = malloc(6);
45 memcpy(data, "hello", 6);
47 scoped_refptr<RefCountedMemory> mem = new RefCountedMallocedMemory(data, 6);
49 EXPECT_EQ(6U, mem->size());
50 EXPECT_EQ(0, memcmp("hello", mem->front(), 6));
53 TEST(RefCountedMemoryUnitTest, Equals) {
54 std::string s1("same");
55 scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1);
57 std::vector<unsigned char> d2;
62 scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2);
64 EXPECT_TRUE(mem1->Equals(mem2));
66 std::string s3("diff");
67 scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3);
69 EXPECT_FALSE(mem1->Equals(mem3));
70 EXPECT_FALSE(mem2->Equals(mem3));
73 TEST(RefCountedMemoryUnitTest, EqualsNull) {
75 scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
76 EXPECT_FALSE(mem->Equals(NULL));