Skip to content

Commit ef2779f

Browse files
committed
add missed self-assignment check
A copy assignment operator must prevent that self-copy assignment ruins the object state. Issue: saebyn#28 Signed-off-by: Gluttton <gluttton@ukr.net>
1 parent a9d5cbc commit ef2779f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/munkres-cpp/matrix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ Matrix<T>::Matrix (size_t rows, size_t columns)
120120
template<class T>
121121
Matrix<T> & Matrix<T>::operator= (const Matrix<T> & other)
122122
{
123+
if (this == & other) {
124+
return *this;
125+
}
126+
123127
if (other.m_matrix != nullptr) {
124128
resize (other.m_rows, other.m_columns);
125129
std::copy (other.m_matrix[0], other.m_matrix[0] + m_rows * m_columns, m_matrix[0]);

tests/matrix_test.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,22 @@ TEST_F (MatrixTest, operatorAssignment_0x0_Success)
127127

128128

129129

130+
TEST_F (MatrixTest, operatorAssignment_1x1_self_Success)
131+
{
132+
// Arrange.
133+
munkres_cpp::Matrix<double> etalon_matrix {
134+
{1.1}
135+
};
136+
137+
// Act.
138+
etalon_matrix = etalon_matrix;
139+
140+
// Assert.
141+
EXPECT_EQ (1.1, etalon_matrix (0, 0) );
142+
}
143+
144+
145+
130146
TEST_F (MatrixTest, operatorAssignment_3x3_Success)
131147
{
132148
// Arrange.

0 commit comments

Comments
 (0)