Skip to content

Commit

Permalink
add missed self-assignment check
Browse files Browse the repository at this point in the history
A copy assignment operator must prevent that self-copy assignment ruins
the object state.

Issue: saebyn#28
Signed-off-by: Gluttton <[email protected]>
  • Loading branch information
Gluttton committed Feb 5, 2022
1 parent a9d5cbc commit ef2779f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/munkres-cpp/matrix.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ Matrix<T>::Matrix (size_t rows, size_t columns)
template<class T>
Matrix<T> & Matrix<T>::operator= (const Matrix<T> & other)
{
if (this == & other) {
return *this;
}

if (other.m_matrix != nullptr) {
resize (other.m_rows, other.m_columns);
std::copy (other.m_matrix[0], other.m_matrix[0] + m_rows * m_columns, m_matrix[0]);
Expand Down
16 changes: 16 additions & 0 deletions tests/matrix_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,22 @@ TEST_F (MatrixTest, operatorAssignment_0x0_Success)



TEST_F (MatrixTest, operatorAssignment_1x1_self_Success)
{
// Arrange.
munkres_cpp::Matrix<double> etalon_matrix {
{1.1}
};

// Act.
etalon_matrix = etalon_matrix;

// Assert.
EXPECT_EQ (1.1, etalon_matrix (0, 0) );
}



TEST_F (MatrixTest, operatorAssignment_3x3_Success)
{
// Arrange.
Expand Down

0 comments on commit ef2779f

Please sign in to comment.