• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9 
10 #include "base/memory/ref_counted_memory.h"
11 
12 #include <stdint.h>
13 
14 #include <utility>
15 
16 #include "base/containers/span.h"
17 #include "base/memory/read_only_shared_memory_region.h"
18 #include "testing/gmock/include/gmock/gmock.h"
19 #include "testing/gtest/include/gtest/gtest.h"
20 
21 using testing::ElementsAre;
22 
23 namespace base {
24 
TEST(RefCountedMemoryUnitTest,RefCountedStaticMemory)25 TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) {
26   {
27     auto mem = MakeRefCounted<RefCountedStaticMemory>(
28         byte_span_from_cstring("static mem"));
29 
30     EXPECT_THAT(span(*mem),
31                 ElementsAre('s', 't', 'a', 't', 'i', 'c', ' ', 'm', 'e', 'm'));
32   }
33 }
34 
TEST(RefCountedMemoryUnitTest,RefCountedBytes)35 TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
36   std::vector<uint8_t> data;
37   data.push_back(45);
38   data.push_back(99);
39   scoped_refptr<RefCountedMemory> mem =
40       MakeRefCounted<RefCountedBytes>(std::move(data));
41 
42   EXPECT_THAT(span(*mem), ElementsAre(45, 99));
43 
44   scoped_refptr<RefCountedMemory> mem2;
45   {
46     const uint8_t kData[] = {12, 11, 99};
47     mem2 = MakeRefCounted<RefCountedBytes>(span(kData));
48   }
49 
50   EXPECT_THAT(span(*mem2), ElementsAre(12, 11, 99));
51 }
52 
TEST(RefCountedMemoryUnitTest,RefCountedBytesMutable)53 TEST(RefCountedMemoryUnitTest, RefCountedBytesMutable) {
54   auto mem = MakeRefCounted<RefCountedBytes>(10);
55 
56   EXPECT_THAT(span(*mem), ElementsAre(0, 0, 0, 0, 0, 0, 0, 0, 0, 0));
57 
58   // Test non-const version of as_vector().
59   mem->as_vector()[1u] = 1;
60 
61   EXPECT_THAT(span(*mem), ElementsAre(0, 1, 0, 0, 0, 0, 0, 0, 0, 0));
62 }
63 
TEST(RefCountedMemoryUnitTest,RefCountedString)64 TEST(RefCountedMemoryUnitTest, RefCountedString) {
65   scoped_refptr<RefCountedMemory> mem =
66       MakeRefCounted<RefCountedString>(std::string("destroy me"));
67 
68   EXPECT_EQ(span(*mem), span_from_cstring("destroy me"));
69 }
70 
TEST(RefCountedMemoryUnitTest,Equals)71 TEST(RefCountedMemoryUnitTest, Equals) {
72   scoped_refptr<RefCountedMemory> mem1 =
73       MakeRefCounted<RefCountedString>(std::string("same"));
74 
75   std::vector<uint8_t> d2 = {'s', 'a', 'm', 'e'};
76   scoped_refptr<RefCountedMemory> mem2 =
77       MakeRefCounted<RefCountedBytes>(std::move(d2));
78 
79   EXPECT_TRUE(mem1->Equals(mem2));
80 
81   std::string s3("diff");
82   scoped_refptr<RefCountedMemory> mem3 =
83       MakeRefCounted<RefCountedString>(std::move(s3));
84 
85   EXPECT_FALSE(mem1->Equals(mem3));
86   EXPECT_FALSE(mem2->Equals(mem3));
87 }
88 
TEST(RefCountedMemoryUnitTest,EqualsNull)89 TEST(RefCountedMemoryUnitTest, EqualsNull) {
90   std::string s("str");
91   scoped_refptr<RefCountedMemory> mem =
92       MakeRefCounted<RefCountedString>(std::move(s));
93   EXPECT_FALSE(mem->Equals(nullptr));
94 }
95 
96 }  //  namespace base
97