OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2012 Google Inc. | 2 * Copyright 2012 Google Inc. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
6 */ | 6 */ |
7 | 7 |
8 #include "SkRefCnt.h" | 8 #include "SkRefCnt.h" |
9 #include "SkThreadUtils.h" | 9 #include "SkThreadUtils.h" |
10 #include "SkTypes.h" | 10 #include "SkTypes.h" |
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 public: | 127 public: |
128 sk_sp<Effect> fEffect; | 128 sk_sp<Effect> fEffect; |
129 | 129 |
130 const sk_sp<Effect>& get() const { return fEffect; } | 130 const sk_sp<Effect>& get() const { return fEffect; } |
131 | 131 |
132 void set(sk_sp<Effect> value) { | 132 void set(sk_sp<Effect> value) { |
133 fEffect = std::move(value); | 133 fEffect = std::move(value); |
134 } | 134 } |
135 }; | 135 }; |
136 | 136 |
137 DEF_TEST(sk_sp, reporter) { | 137 struct EffectImpl : public Effect { |
| 138 static sk_sp<EffectImpl> Create() { |
| 139 return sk_sp<EffectImpl>(new EffectImpl); |
| 140 } |
| 141 int fValue; |
| 142 }; |
| 143 static sk_sp<Effect> make_effect() { |
| 144 auto foo = EffectImpl::Create(); |
| 145 foo->fValue = 42; |
| 146 return std::move(foo); |
| 147 } |
| 148 |
| 149 static void reset_counters() { |
138 gRefCounter = 0; | 150 gRefCounter = 0; |
139 gUnrefCounter = 0; | 151 gUnrefCounter = 0; |
140 gNewCounter = 0; | 152 gNewCounter = 0; |
141 gDeleteCounter = 0; | 153 gDeleteCounter = 0; |
| 154 } |
| 155 DEF_TEST(sk_sp, reporter) { |
| 156 reset_counters(); |
142 | 157 |
143 Paint paint; | 158 Paint paint; |
144 REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr); | 159 REPORTER_ASSERT(reporter, paint.fEffect.get() == nullptr); |
145 REPORTER_ASSERT(reporter, !paint.get()); | 160 REPORTER_ASSERT(reporter, !paint.get()); |
146 check(reporter, 0, 0, 0, 0); | 161 check(reporter, 0, 0, 0, 0); |
147 | 162 |
148 paint.set(Create()); | 163 paint.set(Create()); |
149 check(reporter, 0, 0, 1, 0); | 164 check(reporter, 0, 0, 1, 0); |
150 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1); | 165 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 1); |
151 | 166 |
152 paint.set(nullptr); | 167 paint.set(nullptr); |
153 check(reporter, 0, 1, 1, 1); | 168 check(reporter, 0, 1, 1, 1); |
154 | 169 |
155 auto e = Create(); | 170 auto e = Create(); |
156 REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*)); | 171 REPORTER_ASSERT(reporter, sizeof(e) == sizeof(void*)); |
157 | 172 |
158 check(reporter, 0, 1, 2, 1); | 173 check(reporter, 0, 1, 2, 1); |
159 paint.set(e); | 174 paint.set(e); |
160 check(reporter, 1, 1, 2, 1); | 175 check(reporter, 1, 1, 2, 1); |
161 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2); | 176 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 2); |
162 | 177 |
163 Paint paint2; | 178 Paint paint2; |
164 paint2.set(paint.get()); | 179 paint2.set(paint.get()); |
165 check(reporter, 2, 1, 2, 1); | 180 check(reporter, 2, 1, 2, 1); |
166 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3); | 181 REPORTER_ASSERT(reporter, paint.fEffect.get()->fRefCnt == 3); |
167 | 182 |
168 delete paint.get()->method(); | 183 delete paint.get()->method(); |
169 check(reporter, 2, 1, 2, 1); | 184 check(reporter, 2, 1, 2, 1); |
| 185 |
| 186 paint.set(nullptr); |
| 187 e = nullptr; |
| 188 paint2.set(nullptr); |
| 189 check(reporter, 2, 4, 2, 2); |
| 190 |
| 191 reset_counters(); |
| 192 { |
| 193 // Test convertible sk_sp assignment. |
| 194 check(reporter, 0, 0, 0, 0); |
| 195 sk_sp<Effect> foo(nullptr); |
| 196 REPORTER_ASSERT(reporter, !foo); |
| 197 foo = make_effect(); |
| 198 REPORTER_ASSERT(reporter, foo); |
| 199 check(reporter, 0, 0, 1, 0); |
| 200 } |
| 201 check(reporter, 0, 1, 1, 1); |
| 202 |
| 203 // Test passing convertible rvalue into funtion. |
| 204 reset_counters(); |
| 205 paint.set(EffectImpl::Create()); |
| 206 check(reporter, 0, 0, 1, 0); |
| 207 paint.set(nullptr); |
| 208 check(reporter, 0, 1, 1, 1); |
| 209 |
| 210 reset_counters(); |
| 211 auto baz = EffectImpl::Create(); |
| 212 check(reporter, 0, 0, 1, 0); |
| 213 paint.set(std::move(baz)); |
| 214 check(reporter, 0, 0, 1, 0); |
| 215 REPORTER_ASSERT(reporter, !baz); |
| 216 paint.set(nullptr); |
| 217 check(reporter, 0, 1, 1, 1); |
| 218 |
| 219 reset_counters(); |
| 220 { |
| 221 // test comparison operator with convertible type. |
| 222 sk_sp<EffectImpl> bar1 = EffectImpl::Create(); |
| 223 sk_sp<Effect> bar2(bar1); // convertible copy constructor |
| 224 check(reporter, 1, 0, 1, 0); |
| 225 REPORTER_ASSERT(reporter, bar1); |
| 226 REPORTER_ASSERT(reporter, bar2); |
| 227 REPORTER_ASSERT(reporter, bar1 == bar2); |
| 228 REPORTER_ASSERT(reporter, bar2 == bar1); |
| 229 REPORTER_ASSERT(reporter, !(bar1 != bar2)); |
| 230 REPORTER_ASSERT(reporter, !(bar2 != bar1)); |
| 231 sk_sp<Effect> bar3(nullptr); |
| 232 bar3 = bar1; // convertible copy assignment |
| 233 check(reporter, 2, 0, 1, 0); |
| 234 |
| 235 } |
| 236 check(reporter, 2, 3, 1, 1); |
| 237 |
| 238 // test passing convertible copy into funtion. |
| 239 reset_counters(); |
| 240 baz = EffectImpl::Create(); |
| 241 check(reporter, 0, 0, 1, 0); |
| 242 paint.set(baz); |
| 243 check(reporter, 1, 0, 1, 0); |
| 244 baz = nullptr; |
| 245 check(reporter, 1, 1, 1, 0); |
| 246 paint.set(nullptr); |
| 247 check(reporter, 1, 2, 1, 1); |
170 } | 248 } |
171 | 249 |
OLD | NEW |