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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531
|
#if ISPTR_USE_MODULES
import isptr;
#else
#include <intrusive_shared_ptr/intrusive_shared_ptr.h>
#endif
#include <doctest/doctest.h>
#include "mocks.h"
#include <sstream>
#include <type_traits>
#include <cstdint>
using namespace isptr;
TEST_SUITE("traits") {
TEST_CASE( "Type traits are correct" ) {
using ptr = mock_ptr<instrumented_counted<1>>;
SUBCASE("Construction, destruction and assignment") {
CHECK( sizeof(ptr) == sizeof(instrumented_counted<1> *) );
CHECK( std::alignment_of_v<ptr> == std::alignment_of_v<instrumented_counted<1> *> );
CHECK( std::is_default_constructible_v<ptr> );
CHECK( std::is_nothrow_default_constructible_v<ptr> );
CHECK( !std::is_trivially_default_constructible_v<ptr> );
CHECK( std::is_copy_constructible_v<ptr> );
CHECK( !std::is_trivially_copy_constructible_v<ptr> );
CHECK( std::is_nothrow_copy_constructible_v<ptr> );
CHECK( std::is_move_constructible_v<ptr> );
CHECK( !std::is_trivially_move_constructible_v<ptr> );
CHECK( std::is_nothrow_move_constructible_v<ptr> );
CHECK( std::is_copy_assignable_v<ptr> );
CHECK( !std::is_trivially_copy_assignable_v<ptr> );
CHECK( std::is_nothrow_copy_assignable_v<ptr> );
CHECK( std::is_move_assignable_v<ptr> );
CHECK( !std::is_trivially_move_assignable_v<ptr> );
CHECK( std::is_nothrow_move_assignable_v<ptr> );
CHECK( std::is_swappable_v<ptr> );
CHECK( std::is_nothrow_swappable_v<ptr> );
CHECK( std::is_destructible_v<ptr> );
CHECK( !std::is_trivially_destructible_v<ptr> );
CHECK( std::is_nothrow_destructible_v<ptr> );
CHECK( std::is_constructible_v<ptr, std::nullptr_t> );
CHECK( std::is_nothrow_constructible_v<ptr, std::nullptr_t> );
CHECK( !std::is_trivially_constructible_v<ptr, std::nullptr_t> );
}
using another_ptr = mock_ptr<derived_instrumented_counted<1>>;
SUBCASE( "Conversions to/from other ptr types" ) {
CHECK( std::is_convertible_v<another_ptr, ptr> );
CHECK( std::is_constructible_v<ptr, another_ptr> );
CHECK( !std::is_trivially_constructible_v<ptr, another_ptr> );
CHECK( std::is_nothrow_constructible_v<ptr, another_ptr> );
CHECK( std::is_assignable_v<ptr, another_ptr> );
CHECK( !std::is_trivially_assignable_v<ptr, another_ptr> );
CHECK( std::is_nothrow_assignable_v<ptr, another_ptr> );
CHECK( !std::is_convertible_v<ptr, another_ptr> );
CHECK( !std::is_constructible_v<another_ptr, ptr> );
CHECK( !std::is_assignable_v<another_ptr, ptr> );
}
using ptr_different_traits = mock_ptr_different_traits<instrumented_counted<1>>;
SUBCASE( "Conversions to/from other traits" ) {
CHECK( std::is_convertible_v<ptr_different_traits, ptr> );
CHECK( std::is_convertible_v<ptr, ptr_different_traits> );
CHECK( std::is_constructible_v<ptr, ptr_different_traits> );
CHECK( !std::is_trivially_constructible_v<ptr, ptr_different_traits> );
CHECK( std::is_nothrow_constructible_v<ptr, ptr_different_traits> );
CHECK( std::is_assignable_v<ptr, ptr_different_traits> );
CHECK( !std::is_trivially_assignable_v<ptr, ptr_different_traits> );
CHECK( std::is_nothrow_assignable_v<ptr, ptr_different_traits> );
}
}
using IncompatibleTypes = std::tuple<bool,
char, signed char, unsigned char, wchar_t, char16_t, char32_t,
short, unsigned short,
int, unsigned int,
long, unsigned long,
long long, unsigned long long,
float,
double,
long double,
void *,
instrumented_counted<1> *,
mock_ptr<instrumented_counted<2>>
>;
TEST_CASE_TEMPLATE_DEFINE( "Conversion and assignment from other types", TestType, conv_assign_incompat) {
using ptr = mock_ptr<instrumented_counted<1>>;
CHECK( !std::is_constructible_v<ptr, TestType> );
CHECK( !std::is_assignable_v<ptr, TestType> );
}
TEST_CASE_TEMPLATE_APPLY(conv_assign_incompat, IncompatibleTypes);
}
TEST_SUITE("empty") {
TEST_CASE( "Default constructed and constructed from nullptr ptr behaves like nullptr " ) {
mock_ptr<instrumented_counted<>> empty;
SUBCASE("Default constructed") {
CHECK(empty.get() == nullptr);
CHECK(!bool(empty));
CHECK(empty.operator->() == nullptr);
}
SUBCASE("Construsted from nullptr") {
mock_ptr<instrumented_counted<>> empty1(nullptr);
CHECK(empty1.get() == nullptr);
CHECK(!bool(empty1));
CHECK(empty1.operator->() == nullptr);
}
SUBCASE( "Comparing to another empty ptr" ) {
mock_ptr<instrumented_counted<>> empty1;
CHECK(empty == empty1);
CHECK(!(empty != empty1));
CHECK(!(empty < empty1));
CHECK(empty <= empty1);
CHECK(!(empty > empty1));
CHECK(empty >= empty1);
}
SUBCASE( "Comparing to a ptr created from nullptr" ) {
mock_ptr<instrumented_counted<>> empty1(nullptr);
CHECK(empty == empty1);
CHECK(!(empty != empty1));
CHECK(!(empty < empty1));
CHECK(empty <= empty1);
CHECK(!(empty > empty1));
CHECK(empty >= empty1);
}
SUBCASE( "Comparing to nullptr" ) {
CHECK(empty == nullptr);
CHECK(nullptr == empty );
CHECK(!(empty != nullptr));
CHECK(!(nullptr != empty));
}
SUBCASE( "Comparing to raw pointer" ) {
instrumented_counted<> * raw = nullptr;
CHECK(empty == raw);
CHECK(raw == empty );
CHECK(!(empty != raw));
CHECK(!(raw != empty));
CHECK(!(empty < raw));
CHECK(!(raw < empty));
CHECK(empty <= raw);
CHECK(raw <= empty);
CHECK(!(empty > raw));
CHECK(!(raw > empty));
CHECK(empty >= raw);
CHECK(raw >= empty);
}
SUBCASE( "Comparing to void pointer" ) {
void * raw = nullptr;
CHECK(empty == raw);
CHECK(raw == empty );
CHECK(!(empty != raw));
CHECK(!(raw != empty));
CHECK(!(empty < raw));
CHECK(!(raw < empty));
CHECK(empty <= raw);
CHECK(raw <= empty);
CHECK(!(empty > raw));
CHECK(!(raw > empty));
CHECK(empty >= raw);
CHECK(raw >= empty);
}
SUBCASE( "Comparing to different traits" ) {
mock_ptr_different_traits<instrumented_counted<>> empty1;
CHECK(empty == empty1);
CHECK(!(empty != empty1));
CHECK(!(empty < empty1));
CHECK(empty <= empty1);
CHECK(!(empty > empty1));
CHECK(empty >= empty1);
}
}
}
TEST_SUITE("counting") {
TEST_CASE( "Basic counting " ) {
SUBCASE( "Attach" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
REQUIRE( &object == ptr.get() );
REQUIRE( object.count == 1 );
CHECK(bool(ptr));
CHECK(ptr.operator->() == &object);
}
SUBCASE( "Ref" ) {
instrumented_counted<> object;
auto ptr = mock_ref(&object);
REQUIRE( &object == ptr.get() );
REQUIRE( object.count == 2 );
CHECK(bool(ptr));
CHECK(ptr.operator->() == &object);
mock_traits<>::sub_ref(&object);
}
SUBCASE( "Release" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
auto p = ptr.release();
CHECK( p == &object );
CHECK(ptr.get() == nullptr);
CHECK(!bool(ptr));
CHECK(ptr.operator->() == nullptr);
mock_traits<>::sub_ref(&object);
}
SUBCASE( "Reset" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
ptr.reset();
CHECK(ptr.get() == nullptr);
CHECK(!bool(ptr));
CHECK(ptr.operator->() == nullptr);
}
SUBCASE( "Copy and assignment" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
auto ptr2(ptr);
REQUIRE( object.count == 2 );
auto ptr3(mock_ref(&object));
REQUIRE( object.count == 3 );
mock_ptr<instrumented_counted<>> ptr4;
ptr4 = ptr;
REQUIRE( object.count == 4 );
mock_ptr<instrumented_counted<>> ptr5;
ptr5 = mock_ref(&object);
REQUIRE( object.count == 5 );
}
SUBCASE( "Self assignment" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
ptr = ptr;
REQUIRE( object.count == 1 );
ptr = std::move(ptr);
REQUIRE( ptr.get() == &object );
REQUIRE( object.count == 1 );
}
SUBCASE( " Swap ") {
instrumented_counted<> object1, object2;
auto ptr1 = mock_noref(&object1);
auto ptr2 = mock_noref(&object2);
swap(ptr1, ptr2);
CHECK( ptr1.get() == &object2 );
CHECK( ptr2.get() == &object1 );
REQUIRE( object1.count == 1 );
REQUIRE( object2.count == 1 );
ptr1.swap(ptr2);
CHECK( ptr1.get() == &object1 );
CHECK( ptr2.get() == &object2 );
REQUIRE( object1.count == 1 );
REQUIRE( object2.count == 1 );
ptr2.swap(ptr1);
CHECK( ptr1.get() == &object2 );
CHECK( ptr2.get() == &object1 );
REQUIRE( object1.count == 1 );
REQUIRE( object2.count == 1 );
}
}
TEST_CASE( "Conversions " ) {
SUBCASE( "Attach" ) {
derived_instrumented_counted<> object;
mock_ptr<instrumented_counted<>> ptr = mock_noref(&object);
REQUIRE( &object == ptr.get() );
REQUIRE( object.count == 1 );
CHECK(bool(ptr));
CHECK(ptr.operator->() == &object);
}
SUBCASE( "Ref" ) {
derived_instrumented_counted<> object;
mock_ptr<instrumented_counted<>> ptr = mock_ref(&object);
REQUIRE( &object == ptr.get() );
REQUIRE( object.count == 2 );
CHECK(bool(ptr));
CHECK(ptr.operator->() == &object);
mock_traits<>::sub_ref(&object);
}
SUBCASE( "Release" ) {
derived_instrumented_counted<> object;
mock_ptr<instrumented_counted<>> ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
auto p = ptr.release();
CHECK( p == &object );
CHECK(ptr.get() == nullptr);
CHECK(!bool(ptr));
CHECK(ptr.operator->() == nullptr);
mock_traits<>::sub_ref(&object);
}
SUBCASE( "Reset" ) {
derived_instrumented_counted<> object;
mock_ptr<instrumented_counted<>> ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
ptr.reset();
CHECK(ptr.get() == nullptr);
CHECK(!bool(ptr));
CHECK(ptr.operator->() == nullptr);
}
SUBCASE( "Copy and assignment" ) {
derived_instrumented_counted<> object;
auto ptr = mock_noref(&object);
REQUIRE( object.count == 1 );
mock_ptr<instrumented_counted<>> ptr2(ptr);
REQUIRE( object.count == 2 );
mock_ptr<instrumented_counted<>> ptr3(mock_ref(&object));
REQUIRE( object.count == 3 );
mock_ptr<instrumented_counted<>> ptr4;
ptr4 = ptr;
REQUIRE( object.count == 4 );
mock_ptr<instrumented_counted<>> ptr5;
ptr5 = mock_ref(&object);
REQUIRE( object.count == 5 );
}
}
TEST_CASE( "Different traits " ) {
SUBCASE( "Copy" ) {
instrumented_counted<> object;
mock_ptr_different_traits<instrumented_counted<>> ptr = mock_noref_different_traits(&object);
mock_ptr<instrumented_counted<>> ptr1(ptr);
REQUIRE( &object == ptr1.get() );
REQUIRE( object.count == 2 );
}
SUBCASE( "Move" ) {
instrumented_counted<> object;
mock_ptr_different_traits<instrumented_counted<>> ptr = mock_noref_different_traits(&object);
mock_ptr<instrumented_counted<>> ptr1(std::move(ptr));
REQUIRE( &object == ptr1.get() );
REQUIRE( object.count == 1 );
}
}
TEST_CASE( "Casts" ) {
derived_instrumented_counted<> object;
const instrumented_counted<> const_object;
derived_instrumented_counted<> derived_object;
mock_ptr<instrumented_counted<>> ptr = mock_noref(&object);
auto ptr_const = mock_noref(&const_object);
auto ptr_derived = mock_noref(&derived_object);
SUBCASE( "Const cast" ) {
auto res = intrusive_const_cast<mock_ptr<instrumented_counted<>>>(ptr_const);
CHECK( res.get() == &const_object );
CHECK( const_object.count == 2 );
res = intrusive_const_cast<mock_ptr<instrumented_counted<>>>(mock_ref(&const_object));
CHECK( res.get() == &const_object );
CHECK( const_object.count == 2 );
}
SUBCASE( "Dynamic cast" ) {
auto res = intrusive_dynamic_cast<mock_ptr<derived_instrumented_counted<>>>(ptr);
CHECK( res.get() == &object );
CHECK( object.count == 2 );
res = intrusive_dynamic_cast<mock_ptr<derived_instrumented_counted<>>>(mock_ptr<instrumented_counted<>>(ptr));
CHECK( res.get() == &object );
CHECK( object.count == 2 );
auto res1 = intrusive_dynamic_cast<mock_ptr<const derived_instrumented_counted<>>>(ptr_const);
CHECK( !res1 );
res1 = intrusive_dynamic_cast<mock_ptr<const derived_instrumented_counted<>>>(mock_ref(&const_object));
CHECK( !res1 );
}
SUBCASE( "Static cast" ) {
auto res = intrusive_static_cast<mock_ptr<derived_instrumented_counted<>>>(ptr);
CHECK( res.get() == &object );
CHECK( object.count == 2 );
res = intrusive_static_cast<mock_ptr<derived_instrumented_counted<>>>(mock_ptr<instrumented_counted<>>(ptr));
CHECK( res.get() == &object );
CHECK( object.count == 2 );
}
}
}
TEST_SUITE("output") {
TEST_CASE( "Output" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
std::ostringstream str1;
auto & res = (str1 << ptr);
CHECK( &res == &str1 );
std::ostringstream str2;
str2 << &object;
CHECK( str1.str() == str2.str() );
}
}
TEST_SUITE("output param") {
TEST_CASE( "Output param" ) {
instrumented_counted<> object, object1;
auto ptr = mock_noref(&object);
auto ptr1 = mock_noref(&object1);
auto func = [&object1] (instrumented_counted<> ** out) {
mock_traits<>::add_ref(&object1);
*out = &object1;
};
func(ptr.get_output_param());
CHECK( ptr.get() == &object1 );
CHECK( object.count == -1 );
CHECK( object1.count == 2 );
}
TEST_CASE( "Member pointer" ) {
instrumented_counted<> object;
auto ptr = mock_noref(&object);
int instrumented_counted<>::*pcount = &instrumented_counted<>::count;
auto x = ptr->*pcount;
CHECK(x == 1);
}
}
|