ref_counted_memory_unittest.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. #include "base/memory/ref_counted_memory.h"
  5. #include <stdint.h>
  6. #include <utility>
  7. #include "base/containers/span.h"
  8. #include "base/memory/read_only_shared_memory_region.h"
  9. #include "testing/gmock/include/gmock/gmock.h"
  10. #include "testing/gtest/include/gtest/gtest.h"
  11. using testing::Each;
  12. using testing::ElementsAre;
  13. namespace base {
  14. namespace {
  15. void ConvertToByteSpanAndCheckSize(span<const uint8_t> data,
  16. size_t expected_size) {
  17. EXPECT_EQ(expected_size, data.size());
  18. }
  19. } // namespace
  20. TEST(RefCountedMemoryUnitTest, RefCountedStaticMemory) {
  21. auto mem = MakeRefCounted<RefCountedStaticMemory>("static mem00", 10);
  22. EXPECT_EQ(10U, mem->size());
  23. EXPECT_EQ("static mem", std::string(mem->front_as<char>(), mem->size()));
  24. ConvertToByteSpanAndCheckSize(*mem, 10);
  25. }
  26. TEST(RefCountedMemoryUnitTest, RefCountedBytes) {
  27. std::vector<uint8_t> data;
  28. data.push_back(45);
  29. data.push_back(99);
  30. scoped_refptr<RefCountedMemory> mem = RefCountedBytes::TakeVector(&data);
  31. EXPECT_EQ(0U, data.size());
  32. ASSERT_EQ(2U, mem->size());
  33. EXPECT_EQ(45U, mem->front()[0]);
  34. EXPECT_EQ(99U, mem->front()[1]);
  35. scoped_refptr<RefCountedMemory> mem2;
  36. {
  37. const unsigned char kData[] = {12, 11, 99};
  38. mem2 = MakeRefCounted<RefCountedBytes>(kData, std::size(kData));
  39. }
  40. ASSERT_EQ(3U, mem2->size());
  41. EXPECT_EQ(12U, mem2->front()[0]);
  42. EXPECT_EQ(11U, mem2->front()[1]);
  43. EXPECT_EQ(99U, mem2->front()[2]);
  44. ConvertToByteSpanAndCheckSize(*mem2, 3);
  45. }
  46. TEST(RefCountedMemoryUnitTest, RefCountedBytesMutable) {
  47. auto mem = MakeRefCounted<RefCountedBytes>(10);
  48. ASSERT_EQ(10U, mem->size());
  49. EXPECT_THAT(mem->data(), Each(0U));
  50. // Test non-const versions of data(), front() and front_as<>().
  51. mem->data()[0] = 1;
  52. mem->front()[1] = 2;
  53. mem->front_as<char>()[2] = 3;
  54. EXPECT_THAT(mem->data(), ElementsAre(1, 2, 3, 0, 0, 0, 0, 0, 0, 0));
  55. }
  56. TEST(RefCountedMemoryUnitTest, RefCountedString) {
  57. std::string s("destroy me");
  58. scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
  59. EXPECT_EQ(0U, s.size());
  60. ASSERT_EQ(10U, mem->size());
  61. EXPECT_EQ('d', mem->front()[0]);
  62. EXPECT_EQ('e', mem->front()[1]);
  63. EXPECT_EQ('e', mem->front()[9]);
  64. ConvertToByteSpanAndCheckSize(*mem, 10);
  65. }
  66. TEST(RefCountedMemoryUnitTest, Equals) {
  67. std::string s1("same");
  68. scoped_refptr<RefCountedMemory> mem1 = RefCountedString::TakeString(&s1);
  69. std::vector<unsigned char> d2 = {'s', 'a', 'm', 'e'};
  70. scoped_refptr<RefCountedMemory> mem2 = RefCountedBytes::TakeVector(&d2);
  71. EXPECT_TRUE(mem1->Equals(mem2));
  72. std::string s3("diff");
  73. scoped_refptr<RefCountedMemory> mem3 = RefCountedString::TakeString(&s3);
  74. EXPECT_FALSE(mem1->Equals(mem3));
  75. EXPECT_FALSE(mem2->Equals(mem3));
  76. }
  77. TEST(RefCountedMemoryUnitTest, EqualsNull) {
  78. std::string s("str");
  79. scoped_refptr<RefCountedMemory> mem = RefCountedString::TakeString(&s);
  80. EXPECT_FALSE(mem->Equals(nullptr));
  81. }
  82. } // namespace base