diff --git a/base/include/sorted_vector.h b/base/include/sorted_vector.h index 33dc693a822388a9cfbb2cb25716688c96c4804b..065dda754554092f8f759e1cd5a1528f3d1887a9 100755 --- a/base/include/sorted_vector.h +++ b/base/include/sorted_vector.h @@ -162,6 +162,7 @@ template SortedVector& SortedVector::operator=(const SortedVector& rhs) { // this class: AllowDuplicate or Not AllowDuplicate same type + vec_.clear(); std::copy(rhs.Begin(), rhs.End(), std::back_inserter(vec_)); return *this; } diff --git a/base/test/unittest/common/utils_sorted_vector_test.cpp b/base/test/unittest/common/utils_sorted_vector_test.cpp index 7c8f84b2226f92c8dc77fe6c158ae56cfdbaa5cb..56e693824b91e6184249ab0433475c70d398e0d4 100755 --- a/base/test/unittest/common/utils_sorted_vector_test.cpp +++ b/base/test/unittest/common/utils_sorted_vector_test.cpp @@ -209,6 +209,33 @@ HWTEST_F(UtilsSortedVector, testOperatorEqNotAllowToAllowOrNotAllow, TestSize.Le } } +HWTEST_F(UtilsSortedVector, testOperatorEqAssignmentTwice, TestSize.Level0) +{ + SortedVector svec; + + std::vector vec; + for (int i = 20; i < 30; i++) { + vec.push_back(i); + } + + for (int i = 0; i < 20; i++) { + svec.Add(i); + } + + SortedVector newSvecFalse; + + for (int i = 20; i < 30; i++) { + newSvecFalse.Add(i); + } + + svec = newSvecFalse; + + ASSERT_EQ(static_cast(10), svec.Size()); + for (int i = 0; i < 10; i++) { + ASSERT_EQ(vec[i], svec[i]); + } +} + HWTEST_F(UtilsSortedVector, testoperatorconsteq, TestSize.Level0) { SortedVector svec;