diff --git a/src/munkres-cpp/matrix.h b/src/munkres-cpp/matrix.h index 835f300..fc8c9a6 100644 --- a/src/munkres-cpp/matrix.h +++ b/src/munkres-cpp/matrix.h @@ -120,6 +120,10 @@ Matrix::Matrix (size_t rows, size_t columns) template Matrix & Matrix::operator= (const Matrix & 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]); diff --git a/tests/matrix_test.cpp b/tests/matrix_test.cpp index 2fed785..31b71b0 100644 --- a/tests/matrix_test.cpp +++ b/tests/matrix_test.cpp @@ -127,6 +127,22 @@ TEST_F (MatrixTest, operatorAssignment_0x0_Success) +TEST_F (MatrixTest, operatorAssignment_1x1_self_Success) +{ + // Arrange. + munkres_cpp::Matrix 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.