Skip to content

Commit 7e0dc16

Browse files
committed
Add tests for move-construction
1 parent 8f5d315 commit 7e0dc16

1 file changed

Lines changed: 118 additions & 2 deletions

File tree

test/scope.t.cpp

Lines changed: 118 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,44 @@ CASE( "unique_resource: an unsuccessfully acquired resource is not deleted" )
321321
EXPECT_NOT( Resource::is_deleted() );
322322
}
323323

324-
CASE( "unique_resource: op=() replaces the managed resouce and the deleter with the give one's" " [move-assignment]" )
324+
CASE( "unique_resource: move construction moves the managed resource and the deleter from the give one's" " [move-construction]" )
325+
{
326+
size_t r1;
327+
size_t r2;
328+
329+
// scope:
330+
{
331+
#if scope_USE_POST_CPP98_VERSION
332+
auto cr1( make_unique_resource_checked(
333+
Resource::open( true ), Resource::invalid(), Amp(Resource::close)
334+
));
335+
336+
auto cr2( std::move(cr1) );
337+
#else
338+
unique_resource<size_t, void(*)(size_t)> cr1( make_unique_resource_checked(
339+
Resource::open( true ), Resource::invalid(), Amp(Resource::close)
340+
));
341+
342+
unique_resource<size_t, void(*)(size_t)> cr2( cr1 );
343+
#endif
344+
EXPECT_NOT( Resource::is_deleted( cr1.get() ) );
345+
EXPECT_NOT( Resource::is_deleted( cr2.get() ) );
346+
347+
// moved-from cr1 must not delete resource on reset or on descruction:
348+
349+
cr1.reset();
350+
351+
EXPECT_NOT( Resource::is_deleted( cr1.get() ) );
352+
353+
r1 = cr1.get();
354+
r2 = cr2.get();
355+
}
356+
357+
EXPECT( Resource::is_deleted( r1 ) );
358+
EXPECT( Resource::is_deleted( r2 ) );
359+
}
360+
361+
CASE( "unique_resource: assignment replaces the managed resource and the deleter with the give one's" " [move-assignment]" )
325362
{
326363
size_t r1;
327364
size_t r2;
@@ -494,6 +531,85 @@ CASE( "unique_resource: op->() provides the pointee if the resource handle is a
494531
EXPECT( cr->i == 77 );
495532
}
496533

497-
CASE( "unique_resource: " "[move-construction][on-deleter-throws]")
534+
struct ThrowingResource
535+
{
536+
ThrowingResource() {}
537+
ThrowingResource( ThrowingResource const & ) { throw std::runtime_error("ThrowingResource"); }
538+
539+
static size_t invalid()
540+
{
541+
return size_t(0);
542+
}
543+
544+
static size_t open()
545+
{
546+
return 7;
547+
}
548+
};
549+
550+
struct NonThrowingResource
498551
{
552+
NonThrowingResource() {}
553+
NonThrowingResource( NonThrowingResource const & ) {}
554+
555+
static size_t invalid()
556+
{
557+
return size_t(0);
558+
}
559+
560+
static size_t open()
561+
{
562+
return 7;
563+
}
564+
};
565+
566+
struct ThrowingDeleter
567+
{
568+
ThrowingDeleter() {}
569+
ThrowingDeleter( NonThrowingResource const & ){ throw std::runtime_error("ThrowingDeleter"); }
570+
571+
static void close( size_t ) {}
572+
};
573+
574+
struct NonThrowingDeleter
575+
{
576+
NonThrowingDeleter() {}
577+
NonThrowingDeleter( NonThrowingDeleter const & ){}
578+
579+
static void close( size_t ) {}
580+
};
581+
582+
CASE( "unique_resource: " "[move-construction][resource-copy-ctor-throws]")
583+
{
584+
// scope:
585+
{
586+
#if scope_USE_POST_CPP98_VERSION
587+
auto cr( make_unique_resource_checked(
588+
ThrowingResource::open(), ThrowingResource::invalid(), Amp(NonThrowingDeleter::close)
589+
));
590+
#else
591+
unique_resource<size_t, void(*)(size_t)> cr( make_unique_resource_checked(
592+
ThrowingResource::open(), ThrowingResource::invalid(), Amp(NonThrowingDeleter::close)
593+
));
594+
#endif
595+
EXPECT( cr.get() == 7u );
596+
}
597+
}
598+
599+
CASE( "unique_resource: " "[move-construction][deleter-copy-ctor-throws]")
600+
{
601+
// scope:
602+
{
603+
#if scope_USE_POST_CPP98_VERSION
604+
auto cr( make_unique_resource_checked(
605+
NonThrowingResource::open(), NonThrowingResource::invalid(), Amp(ThrowingDeleter::close)
606+
));
607+
#else
608+
unique_resource<size_t, void(*)(size_t)> cr( make_unique_resource_checked(
609+
NonThrowingResource::open(), NonThrowingResource::invalid(), Amp(ThrowingDeleter::close)
610+
));
611+
#endif
612+
EXPECT( cr.get() == 7u );
613+
}
499614
}
615+

0 commit comments

Comments
 (0)