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