test_intrusive_ptr.cc 2.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97
/* Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */

#include <future>
#include <vector>

#include "gtest/gtest.h"

#include "paddle/pten/core/utils/intrusive_ptr.h"
#include "paddle/pten/core/utils/intrusive_ref_counter.h"

namespace pten {
namespace tests {

struct SharedObject : public intrusive_ref_counter<SharedObject> {
  int i{0};
};

TEST(intrusive_ref_counter, async) {
  SharedObject obj;
  const size_t num{100};
  std::vector<std::future<void>> results;
  auto add_ref_and_release = [](const SharedObject* p) {
    intrusive_ptr_add_ref<SharedObject>(p);
    intrusive_ptr_release<SharedObject>(p);
  };
  for (size_t i = 0; i < num; ++i) {
    results.emplace_back(std::async(add_ref_and_release, &obj));
  }
  for (auto& result : results) {
    result.get();
  }
  CHECK_EQ(obj.use_count(), 1u);
}

TEST(intrusive_ptr, default_ctor) {
  intrusive_ptr<SharedObject> p;
  CHECK(p == nullptr);
}

TEST(intrusive_ptr, private_ctor) {
  auto p = make_intrusive<SharedObject>();
  const auto* ptr0 = p.get();
  auto p1 = std::move(p);
  intrusive_ptr<intrusive_ref_counter<SharedObject>> p2(std::move(p1));
  const auto* ptr1 = p2.get();
  CHECK_EQ(ptr0, ptr1);
}

TEST(intrusive_ptr, reset_with_obj) {
  SharedObject obj;
  obj.i = 1;
  intrusive_ptr<SharedObject> p;
  p.reset(&obj, true);
  CHECK_EQ(p->i, obj.i);
}

TEST(intrusive_ptr, reset_with_ptr) {
  auto* ptr = new SharedObject;
  ptr->i = 1;
  intrusive_ptr<SharedObject> p;
  p.reset(ptr, false);
  CHECK_EQ((*p).i, ptr->i);
  p.reset();
  CHECK(p == nullptr);
}

TEST(intrusive_ptr, op_comp) {
  auto p = make_intrusive<SharedObject>();
  auto copy = copy_intrusive<SharedObject>(p);
  auto null = intrusive_ptr<SharedObject>();
  auto p1 = make_intrusive<SharedObject>();
  CHECK(p == copy);
  CHECK(p != p1);
  CHECK(p == copy.get());
  CHECK(p != p1.get());
  CHECK(p.get() == copy);
  CHECK(p.get() != p1);
  CHECK(null == nullptr);
  CHECK(nullptr == null);
  CHECK(p != nullptr);
  CHECK(nullptr != p);
}

}  // namespace tests
}  // namespace pten