From 98dd51244e6ec692c425dd72668f52dcb16c29ed Mon Sep 17 00:00:00 2001 From: Wim Haeck Date: Wed, 9 Oct 2024 17:36:24 -0600 Subject: [PATCH 1/2] replacing placement new --- .../ReactionMultiplicities/src/ctor.hpp | 14 ++++++++++++-- .../ReactionCrossSections/src/ctor.hpp | 16 ++++++++++++++-- .../multigroup/ScatteringMatrix/src/ctor.hpp | 18 ++++++++++++++++-- 3 files changed, 42 insertions(+), 6 deletions(-) diff --git a/src/NDItk/depletion/ReactionMultiplicities/src/ctor.hpp b/src/NDItk/depletion/ReactionMultiplicities/src/ctor.hpp index 6dd8fe1..53f1078 100644 --- a/src/NDItk/depletion/ReactionMultiplicities/src/ctor.hpp +++ b/src/NDItk/depletion/ReactionMultiplicities/src/ctor.hpp @@ -85,7 +85,12 @@ ReactionMultiplicities( ReactionMultiplicities&& base ) : */ ReactionMultiplicities& operator=( const ReactionMultiplicities& base ) { - new (this) ReactionMultiplicities( base ); + if ( this != &base ) { + + base::IntegerListRecord::operator=( base ); + this->reactions_ = base.reactions_; + this->generateBlocks(); + } return *this; } @@ -94,6 +99,11 @@ ReactionMultiplicities& operator=( const ReactionMultiplicities& base ) { */ ReactionMultiplicities& operator=( ReactionMultiplicities&& base ) { - new (this) ReactionMultiplicities( std::move( base ) ); + if ( this != &base ) { + + base::IntegerListRecord::operator=( std::move( base ) ); + this->reactions_ = base.reactions_; + this->generateBlocks(); + } return *this; } diff --git a/src/NDItk/multigroup/ReactionCrossSections/src/ctor.hpp b/src/NDItk/multigroup/ReactionCrossSections/src/ctor.hpp index 47cebbb..1c9ece3 100644 --- a/src/NDItk/multigroup/ReactionCrossSections/src/ctor.hpp +++ b/src/NDItk/multigroup/ReactionCrossSections/src/ctor.hpp @@ -50,7 +50,13 @@ ReactionCrossSections( ReactionCrossSections&& base ) : */ ReactionCrossSections& operator=( const ReactionCrossSections& base ) { - new (this) ReactionCrossSections( base ); + if ( this != &base ) { + + base::RealListRecord::operator=( base ); + this->groups_ = base.groups_; + this->reactions_ = base.reactions_; + this->generateBlocks(); + } return *this; } @@ -59,6 +65,12 @@ ReactionCrossSections& operator=( const ReactionCrossSections& base ) { */ ReactionCrossSections& operator=( ReactionCrossSections&& base ) { - new (this) ReactionCrossSections( std::move( base ) ); + if ( this != &base ) { + + base::RealListRecord::operator=( std::move( base ) ); + this->groups_ = base.groups_; + this->reactions_ = base.reactions_; + this->generateBlocks(); + } return *this; } diff --git a/src/NDItk/multigroup/ScatteringMatrix/src/ctor.hpp b/src/NDItk/multigroup/ScatteringMatrix/src/ctor.hpp index 055a0c2..a2197db 100644 --- a/src/NDItk/multigroup/ScatteringMatrix/src/ctor.hpp +++ b/src/NDItk/multigroup/ScatteringMatrix/src/ctor.hpp @@ -91,7 +91,14 @@ ScatteringMatrix( ScatteringMatrix&& base ) : */ ScatteringMatrix& operator=( const ScatteringMatrix& base ) { - new (this) ScatteringMatrix( base ); + if ( this != &base ) { + + base::RealListRecord::operator=( base ); + this->incident_ = base.incident_; + this->outgoing_ = base.outgoing_; + this->number_moments_ = base.number_moments_; + this->generateBlocks(); + } return *this; } @@ -100,6 +107,13 @@ ScatteringMatrix& operator=( const ScatteringMatrix& base ) { */ ScatteringMatrix& operator=( ScatteringMatrix&& base ) { - new (this) ScatteringMatrix( std::move( base ) ); + if ( this != &base ) { + + base::RealListRecord::operator=( std::move( base ) ); + this->incident_ = base.incident_; + this->outgoing_ = base.outgoing_; + this->number_moments_ = base.number_moments_; + this->generateBlocks(); + } return *this; } From 317b051cfd3cfbd86b99e175b50b13290c26d0ea Mon Sep 17 00:00:00 2001 From: Wim Haeck Date: Wed, 9 Oct 2024 17:40:03 -0600 Subject: [PATCH 2/2] Updating test to include ctor and assignment oepartor tests for object that used placement new --- .../test/ReactionMultiplicities.test.cpp | 210 ++++++++++++++++++ .../test/ReactionCrossSections.test.cpp | 108 +++++++++ .../test/ScatteringMatrix.test.cpp | 210 ++++++++++++++++++ 3 files changed, 528 insertions(+) diff --git a/src/NDItk/depletion/ReactionMultiplicities/test/ReactionMultiplicities.test.cpp b/src/NDItk/depletion/ReactionMultiplicities/test/ReactionMultiplicities.test.cpp index a29051a..939b7c3 100644 --- a/src/NDItk/depletion/ReactionMultiplicities/test/ReactionMultiplicities.test.cpp +++ b/src/NDItk/depletion/ReactionMultiplicities/test/ReactionMultiplicities.test.cpp @@ -19,6 +19,7 @@ void verifyChunk( const ReactionMultiplicities& ); std::string chunkWithMultiplicityType(); void verifyChunkWithMultiplicityType( const ReactionMultiplicities& ); std::string chunkWithInsufficientNumberReactions(); +ReactionMultiplicities makeDummyRecord(); SCENARIO( "ReactionMultiplicities" ) { @@ -75,6 +76,108 @@ SCENARIO( "ReactionMultiplicities" ) { CHECK( buffer == record ); } // THEN } // WHEN + + WHEN( "using the copy constructor" ) { + + auto iter = record.begin() + 5; + auto end = record.end(); + ReactionMultiplicities chunk; + chunk.read( iter, end, 2 ); + + ReactionMultiplicities copy( chunk ); + + THEN( "a ReactionMultiplicities can be constructed and members can " + "be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using the move constructor" ) { + + auto iter = record.begin() + 5; + auto end = record.end(); + ReactionMultiplicities chunk; + chunk.read( iter, end, 2 ); + + ReactionMultiplicities move( std::move( chunk ) ); + + THEN( "a ReactionMultiplicities can be constructed and members can " + "be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using copy assignment" ) { + + auto iter = record.begin() + 5; + auto end = record.end(); + ReactionMultiplicities chunk; + chunk.read( iter, end, 2 ); + + ReactionMultiplicities copy = makeDummyRecord(); + copy = chunk; + + THEN( "an ReactionMultiplicities can be copy assigned and " + "members can be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using move assignment" ) { + + auto iter = record.begin() + 5; + auto end = record.end(); + ReactionMultiplicities chunk; + chunk.read( iter, end, 2 ); + + ReactionMultiplicities move = makeDummyRecord(); + move = std::move( chunk ); + + THEN( "an ReactionMultiplicities can be copy assigned and " + "members can be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN } // GIVEN GIVEN( "valid data for a ReactionMultiplicities instance with a multiplicity type" ) { @@ -131,6 +234,108 @@ SCENARIO( "ReactionMultiplicities" ) { CHECK( buffer == record ); } // THEN } // WHEN + + WHEN( "using the copy constructor" ) { + + auto iter = record.begin() + 9; + auto end = record.end(); + ReactionMultiplicities chunk( ReactionMultiplicityType::All ); + chunk.read( iter, end, 2 ); + + ReactionMultiplicities copy( chunk ); + + THEN( "a ReactionMultiplicities can be constructed and members can " + "be tested" ) { + + verifyChunkWithMultiplicityType( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using the move constructor" ) { + + auto iter = record.begin() + 9; + auto end = record.end(); + ReactionMultiplicities chunk( ReactionMultiplicityType::All ); + chunk.read( iter, end, 2 ); + + ReactionMultiplicities move( std::move( chunk ) ); + + THEN( "a ReactionMultiplicities can be constructed and members can " + "be tested" ) { + + verifyChunkWithMultiplicityType( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using copy assignment" ) { + + auto iter = record.begin() + 9; + auto end = record.end(); + ReactionMultiplicities chunk( ReactionMultiplicityType::All ); + chunk.read( iter, end, 2 ); + + ReactionMultiplicities copy = makeDummyRecord(); + copy = chunk; + + THEN( "an ReactionMultiplicities can be copy assigned and " + "members can be tested" ) { + + verifyChunkWithMultiplicityType( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using move assignment" ) { + + auto iter = record.begin() + 9; + auto end = record.end(); + ReactionMultiplicities chunk( ReactionMultiplicityType::All ); + chunk.read( iter, end, 2 ); + + ReactionMultiplicities move = makeDummyRecord(); + move = std::move( chunk ); + + THEN( "an ReactionMultiplicities can be copy assigned and " + "members can be tested" ) { + + verifyChunkWithMultiplicityType( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN } // GIVEN GIVEN( "invalid data for a ReactionMultiplicities instance" ) { @@ -302,3 +507,8 @@ std::string chunkWithInsufficientNumberReactions() { return "rprod\n"; } + +ReactionMultiplicities makeDummyRecord() { + + return ReactionMultiplicities( { { 102, { 1 }, { 1 } } } ); +} diff --git a/src/NDItk/multigroup/ReactionCrossSections/test/ReactionCrossSections.test.cpp b/src/NDItk/multigroup/ReactionCrossSections/test/ReactionCrossSections.test.cpp index c6dd7bd..5704065 100644 --- a/src/NDItk/multigroup/ReactionCrossSections/test/ReactionCrossSections.test.cpp +++ b/src/NDItk/multigroup/ReactionCrossSections/test/ReactionCrossSections.test.cpp @@ -17,6 +17,7 @@ std::string chunk(); void verifyChunk( const ReactionCrossSections& ); std::string chunkWithInsufficientNumberReactions(); std::string chunkWithInsufficientNumberCrossSectionValues(); +ReactionCrossSections makeDummyRecord(); SCENARIO( "ReactionCrossSections" ) { @@ -73,6 +74,108 @@ SCENARIO( "ReactionCrossSections" ) { CHECK( buffer == record ); } // THEN } // WHEN + + WHEN( "using the copy constructor" ) { + + auto iter = record.begin() + 8; + auto end = record.end(); + ReactionCrossSections chunk; + chunk.read( iter, end, 2, 7 ); + + ReactionCrossSections copy( chunk ); + + THEN( "a ReactionCrossSections can be constructed and members can " + "be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using the move constructor" ) { + + auto iter = record.begin() + 8; + auto end = record.end(); + ReactionCrossSections chunk; + chunk.read( iter, end, 2, 7 ); + + ReactionCrossSections move( std::move( chunk ) ); + + THEN( "a ReactionCrossSections can be constructed and members can " + "be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using copy assignment" ) { + + auto iter = record.begin() + 8; + auto end = record.end(); + ReactionCrossSections chunk; + chunk.read( iter, end, 2, 7 ); + + ReactionCrossSections copy = makeDummyRecord(); + copy = chunk; + + THEN( "a ReactionCrossSections can be copy assigned and " + "members can be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using move assignment" ) { + + auto iter = record.begin() + 8; + auto end = record.end(); + ReactionCrossSections chunk; + chunk.read( iter, end, 2, 7 ); + + ReactionCrossSections move = makeDummyRecord(); + move = std::move( chunk ); + + THEN( "a ReactionCrossSections can be copy assigned and " + "members can be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN } // GIVEN GIVEN( "invalid data for a ReactionCrossSections instance" ) { @@ -228,3 +331,8 @@ std::string chunkWithInsufficientNumberCrossSectionValues() { " 2 0\n" " 16 1.1234567\n"; } + +ReactionCrossSections makeDummyRecord() { + + return ReactionCrossSections( { { 18, 200.0, { 10., 20. } } } ); +} diff --git a/src/NDItk/multigroup/ScatteringMatrix/test/ScatteringMatrix.test.cpp b/src/NDItk/multigroup/ScatteringMatrix/test/ScatteringMatrix.test.cpp index cda24cf..504c28c 100644 --- a/src/NDItk/multigroup/ScatteringMatrix/test/ScatteringMatrix.test.cpp +++ b/src/NDItk/multigroup/ScatteringMatrix/test/ScatteringMatrix.test.cpp @@ -17,6 +17,7 @@ std::string chunk(); void verifyChunk( const ScatteringMatrix& ); std::string chunkForProductionMatrix(); void verifyChunkForProductionMatrix( const ScatteringMatrix& ); +ScatteringMatrix makeDummyRecord(); SCENARIO( "ScatteringMatrix" ) { @@ -73,6 +74,108 @@ SCENARIO( "ScatteringMatrix" ) { CHECK( buffer == record ); } // THEN } // WHEN + + WHEN( "using the copy constructor" ) { + + auto iter = record.begin() + 7; + auto end = record.end(); + ScatteringMatrix chunk; + chunk.read( iter, end, 3, 2 ); + + ScatteringMatrix copy( chunk ); + + THEN( "a ScatteringMatrix can be constructed and members can " + "be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using the move constructor" ) { + + auto iter = record.begin() + 7; + auto end = record.end(); + ScatteringMatrix chunk; + chunk.read( iter, end, 3, 2 ); + + ScatteringMatrix move( std::move( chunk ) ); + + THEN( "a ScatteringMatrix can be constructed and members can " + "be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using copy assignment" ) { + + auto iter = record.begin() + 7; + auto end = record.end(); + ScatteringMatrix chunk; + chunk.read( iter, end, 3, 2 ); + + ScatteringMatrix copy = makeDummyRecord(); + copy = chunk; + + THEN( "a ScatteringMatrix can be copy assigned and " + "members can be tested" ) { + + verifyChunk( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using move assignment" ) { + + auto iter = record.begin() + 7; + auto end = record.end(); + ScatteringMatrix chunk; + chunk.read( iter, end, 3, 2 ); + + ScatteringMatrix move = makeDummyRecord(); + move = std::move( chunk ); + + THEN( "a ScatteringMatrix can be copy assigned and " + "members can be tested" ) { + + verifyChunk( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN } // GIVEN GIVEN( "valid data for a ScatteringMatrix instance for a secondary particle" ) { @@ -128,6 +231,108 @@ SCENARIO( "ScatteringMatrix" ) { CHECK( buffer == record ); } // THEN } // WHEN + + WHEN( "using the copy constructor" ) { + + auto iter = record.begin() + 17; + auto end = record.end(); + ScatteringMatrix chunk( 1001 ); + chunk.read( iter, end, 3, 2, 2 ); + + ScatteringMatrix copy( chunk ); + + THEN( "a ScatteringMatrix can be constructed and members can " + "be tested" ) { + + verifyChunkForProductionMatrix( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using the move constructor" ) { + + auto iter = record.begin() + 17; + auto end = record.end(); + ScatteringMatrix chunk( 1001 ); + chunk.read( iter, end, 3, 2, 2 ); + + ScatteringMatrix move( std::move( chunk ) ); + + THEN( "a ScatteringMatrix can be constructed and members can " + "be tested" ) { + + verifyChunkForProductionMatrix( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using copy assignment" ) { + + auto iter = record.begin() + 17; + auto end = record.end(); + ScatteringMatrix chunk( 1001 ); + chunk.read( iter, end, 3, 2, 2 ); + + ScatteringMatrix copy = makeDummyRecord(); + copy = chunk; + + THEN( "a ScatteringMatrix can be copy assigned and " + "members can be tested" ) { + + verifyChunkForProductionMatrix( copy ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + copy.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN + + WHEN( "using move assignment" ) { + + auto iter = record.begin() + 17; + auto end = record.end(); + ScatteringMatrix chunk( 1001 ); + chunk.read( iter, end, 3, 2, 2 ); + + ScatteringMatrix move = makeDummyRecord(); + move = std::move( chunk ); + + THEN( "a ScatteringMatrix can be copy assigned and " + "members can be tested" ) { + + verifyChunkForProductionMatrix( move ); + } // THEN + + THEN( "the record can be printed" ) { + + std::string buffer; + auto output = std::back_inserter( buffer ); + move.print( output ); + + CHECK( buffer == record ); + } // THEN + } // WHEN } // GIVEN GIVEN( "invalid data for a ScatteringMatrix instance" ) { @@ -335,3 +540,8 @@ void verifyChunkForProductionMatrix( const ScatteringMatrix& chunk ) { CHECK_THAT( 15, WithinRel( moment.matrix()[2][0] ) ); CHECK_THAT( 16, WithinRel( moment.matrix()[2][1] ) ); } + +ScatteringMatrix makeDummyRecord() { + + return { { { 0, { 1, 2, 3, 4 }, 2 } } }; +}