enabling <coroutine> from std library for folly when c++ version is 20
[hiphop-php.git] / hphp / runtime / test / req-ptr-test.cpp
blobc0438baf778100e60275b7bf1a8e348447e51f2d
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include <gtest/gtest.h>
19 #include "hphp/runtime/base/type-resource.h"
20 #include "hphp/runtime/base/dummy-resource.h"
21 #include "hphp/runtime/base/file.h"
23 namespace HPHP {
25 struct DummyResource2 : DummyResource {
26 DECLARE_RESOURCE_ALLOCATION_NO_SWEEP(DummyResource2);
27 CLASSNAME_IS("Unknown");
28 DummyResource2() {}
29 String m_class_name;
30 const String& o_getClassNameHook() const override {
31 if (m_class_name.empty()) {
32 return classnameof();
34 return m_class_name;
36 bool isInvalid() const override { return m_class_name.empty(); }
39 TEST(ReqPtr, Refcounts) {
41 auto ptr = req::make<DummyResource>();
42 EXPECT_TRUE(ptr->hasExactlyOneRef());
43 req::ptr<ResourceData> r(std::move(ptr));
44 EXPECT_TRUE(r.get()->hasExactlyOneRef());
48 auto ptr = req::make<DummyResource>();
49 EXPECT_TRUE(ptr->hasExactlyOneRef());
51 req::ptr<ResourceData> r(ptr);
52 EXPECT_TRUE(ptr->hasMultipleRefs()); // count==2
53 EXPECT_TRUE(r.get()->hasMultipleRefs());
55 EXPECT_TRUE(ptr->hasExactlyOneRef());
59 TEST(ReqPtr, Assignment) {
60 auto ptr = req::make<DummyResource>();
61 auto* tmp = ptr.get();
62 #pragma clang diagnostic push
63 #pragma clang diagnostic ignored "-Wself-assign-overloaded"
64 ptr = ptr;
65 #pragma clang diagnostic pop
66 EXPECT_TRUE(ptr->hasExactlyOneRef());
67 EXPECT_TRUE(ptr.get() == tmp);
70 TEST(ReqPtr, Operators) {
72 req::ptr<DummyResource> p1;
73 req::ptr<DummyResource2> p2;
74 EXPECT_TRUE(p1 == p1);
75 EXPECT_TRUE(p1 == p2);
76 EXPECT_TRUE(p1 == nullptr);
77 EXPECT_TRUE(nullptr == p1);
78 EXPECT_FALSE(p1 != p1);
79 EXPECT_FALSE(p1 != p2);
80 EXPECT_FALSE(p1 != nullptr);
81 EXPECT_FALSE(nullptr != p1);
84 auto p1 = req::make<DummyResource>();
85 auto p2 = req::make<DummyResource>();
86 auto p3 = p1;
87 EXPECT_FALSE(p1 == p2);
88 EXPECT_TRUE(p1 == p3);
89 EXPECT_FALSE(p1 == nullptr);
90 EXPECT_FALSE(nullptr == p1);
91 EXPECT_TRUE(p1 != p2);
92 EXPECT_FALSE(p1 != p3);
93 EXPECT_TRUE(p1 != nullptr);
94 EXPECT_TRUE(nullptr != p1);
97 auto p1 = req::make<DummyResource>();
98 auto p2 = req::make<DummyResource2>();
99 EXPECT_FALSE(p1 == p2);
100 EXPECT_TRUE(p1 != p2);
103 auto p1 = req::make<DummyResource>();
104 req::ptr<ResourceData> p2(p1);
105 EXPECT_TRUE(p1 == p2);
106 EXPECT_FALSE(p1 != p2);
110 TEST(ReqPtr, Casts) {
111 // Test cast operations
113 EXPECT_FALSE(isa<DummyResource>(req::ptr<DummyResource>(nullptr)));
114 EXPECT_TRUE(isa_or_null<DummyResource>(req::ptr<DummyResource>(nullptr)));
116 auto dummy = req::make<DummyResource>();
117 req::ptr<ResourceData> res(dummy);
118 req::ptr<ResourceData> empty;
119 req::ptr<File> emptyFile;
121 EXPECT_TRUE(isa<DummyResource>(res));
122 EXPECT_TRUE(isa_or_null<DummyResource>(res));
124 EXPECT_FALSE(isa<File>(res));
125 EXPECT_FALSE(isa_or_null<File>(res));
127 // cast tests
128 // Bad types and null pointers should throw.
129 EXPECT_EQ(cast<DummyResource>(res), dummy);
130 EXPECT_EQ(cast<ResourceData>(res), dummy);
132 try {
133 cast<File>(res);
134 ADD_FAILURE();
135 } catch(...) {
136 SUCCEED();
139 try {
140 cast<DummyResource>(empty);
141 ADD_FAILURE();
142 } catch(...) {
143 SUCCEED();
146 // cast_or_null tests
147 // Bad types should throw, null pointers are ok.
148 EXPECT_EQ(cast_or_null<ResourceData>(empty), nullptr);
149 EXPECT_EQ(cast_or_null<ResourceData>(dummy), dummy);
151 try {
152 cast_or_null<File>(res);
153 ADD_FAILURE();
154 } catch(...) {
155 SUCCEED();
158 // dyn_cast tests
159 // Bad types are ok, null pointers should throw.
160 EXPECT_EQ(dyn_cast<DummyResource>(res), dummy);
161 EXPECT_EQ(dyn_cast<ResourceData>(res), dummy);
162 EXPECT_EQ(dyn_cast<File>(res), nullptr);
163 try {
164 dyn_cast<File>(emptyFile);
165 ADD_FAILURE();
166 } catch(...) {
167 SUCCEED();
170 // dyn_cast_or_null
171 // Bad types and null pointers are ok. Should never throw.
172 EXPECT_EQ(dyn_cast_or_null<DummyResource>(res), res);
173 EXPECT_EQ(dyn_cast_or_null<File>(res), nullptr);
174 EXPECT_EQ(dyn_cast_or_null<ResourceData>(empty), nullptr);
175 EXPECT_EQ(dyn_cast_or_null<ResourceData>(emptyFile), nullptr);
179 TEST(ReqPtr, MoveCasts) {
180 auto res = unsafe_cast_or_null<DummyResource>(req::make<DummyResource>());
181 EXPECT_NE(res, nullptr);
182 auto res2 = dyn_cast<DummyResource>(req::make<DummyResource>());
183 EXPECT_NE(res2, nullptr);
184 auto res3 = dyn_cast<DummyResource2>(req::make<DummyResource>());
185 EXPECT_EQ(res3, nullptr);