Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / base / memory / ref_counted_memory_unittest.cc
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.
4
5 #include "base/memory/ref_counted_memory.h"
6
7 #include "testing/gtest/include/gtest/gtest.h"
8
9 namespace base {
10
11 TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) {
12   scoped_refptr<RefCountedMemory> mem = new RefCountedStaticMemory(
13       "static mem00", 10);
14
15   EXPECT_EQ(10U, mem->size());
16   EXPECT_EQ("static mem", std::string(mem->front_as<char>(), mem->size()));
17 }
18
19 TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
20   std::vector<uint8> data;
21   data.push_back(45);
22   data.push_back(99);
23   scoped_refptr<RefCountedMemory> mem = RefCountedBytes::TakeVector(&data);
24
25   EXPECT_EQ(0U, data.size());
26
27   EXPECT_EQ(2U, mem->size());
28   EXPECT_EQ(45U, mem->front()[0]);
29   EXPECT_EQ(99U, mem->front()[1]);
30 }
31
32 TEST(RefCountedMemoryUnitTest, RefCountedString) {
33   std::string s("destroy me");
34   scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
35
36   EXPECT_EQ(0U, s.size());
37
38   EXPECT_EQ(10U, mem->size());
39   EXPECT_EQ('d', mem->front()[0]);
40   EXPECT_EQ('e', mem->front()[1]);
41 }
42
43 TEST(RefCountedMemoryUnitTest, RefCountedMallocedMemory) {
44   void* data = malloc(6);
45   memcpy(data, "hello", 6);
46
47   scoped_refptr<RefCountedMemory> mem = new RefCountedMallocedMemory(data, 6);
48
49   EXPECT_EQ(6U, mem->size());
50   EXPECT_EQ(0, memcmp("hello", mem->front(), 6));
51 }
52
53 TEST(RefCountedMemoryUnitTest, Equals) {
54   std::string s1("same");
55   scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1);
56
57   std::vector<unsigned char> d2;
58   d2.push_back('s');
59   d2.push_back('a');
60   d2.push_back('m');
61   d2.push_back('e');
62   scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2);
63
64   EXPECT_TRUE(mem1->Equals(mem2));
65
66   std::string s3("diff");
67   scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3);
68
69   EXPECT_FALSE(mem1->Equals(mem3));
70   EXPECT_FALSE(mem2->Equals(mem3));
71 }
72
73 TEST(RefCountedMemoryUnitTest, EqualsNull) {
74   std::string s("str");
75   scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
76   EXPECT_FALSE(mem->Equals(NULL));
77 }
78
79 }  //  namespace base