Fix for PR18735 - self-assignment for map/multimap gives incorrect results in C++03

git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@201021 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Marshall Clow 2014-02-08 04:03:14 +00:00
parent cf7278afd7
commit ebfc50ee89
10 changed files with 171 additions and 20 deletions

View File

@ -884,10 +884,12 @@ public:
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
__tree_ = __m.__tree_; __tree_ = __m.__tree_;
#else #else
__tree_.clear(); if (this != &__m) {
__tree_.value_comp() = __m.__tree_.value_comp(); __tree_.clear();
__tree_.__copy_assign_alloc(__m.__tree_); __tree_.value_comp() = __m.__tree_.value_comp();
insert(__m.begin(), __m.end()); __tree_.__copy_assign_alloc(__m.__tree_);
insert(__m.begin(), __m.end());
}
#endif #endif
return *this; return *this;
} }
@ -1616,10 +1618,12 @@ public:
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
__tree_ = __m.__tree_; __tree_ = __m.__tree_;
#else #else
__tree_.clear(); if (this != &__m) {
__tree_.value_comp() = __m.__tree_.value_comp(); __tree_.clear();
__tree_.__copy_assign_alloc(__m.__tree_); __tree_.value_comp() = __m.__tree_.value_comp();
insert(__m.begin(), __m.end()); __tree_.__copy_assign_alloc(__m.__tree_);
insert(__m.begin(), __m.end());
}
#endif #endif
return *this; return *this;
} }

View File

@ -831,12 +831,14 @@ public:
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
__table_ = __u.__table_; __table_ = __u.__table_;
#else #else
__table_.clear(); if (this != &__u) {
__table_.hash_function() = __u.__table_.hash_function(); __table_.clear();
__table_.key_eq() = __u.__table_.key_eq(); __table_.hash_function() = __u.__table_.hash_function();
__table_.max_load_factor() = __u.__table_.max_load_factor(); __table_.key_eq() = __u.__table_.key_eq();
__table_.__copy_assign_alloc(__u.__table_); __table_.max_load_factor() = __u.__table_.max_load_factor();
insert(__u.begin(), __u.end()); __table_.__copy_assign_alloc(__u.__table_);
insert(__u.begin(), __u.end());
}
#endif #endif
return *this; return *this;
} }
@ -1567,12 +1569,14 @@ public:
#if __cplusplus >= 201103L #if __cplusplus >= 201103L
__table_ = __u.__table_; __table_ = __u.__table_;
#else #else
__table_.clear(); if (this != &__u) {
__table_.hash_function() = __u.__table_.hash_function(); __table_.clear();
__table_.key_eq() = __u.__table_.key_eq(); __table_.hash_function() = __u.__table_.hash_function();
__table_.max_load_factor() = __u.__table_.max_load_factor(); __table_.key_eq() = __u.__table_.key_eq();
__table_.__copy_assign_alloc(__u.__table_); __table_.max_load_factor() = __u.__table_.max_load_factor();
insert(__u.begin(), __u.end()); __table_.__copy_assign_alloc(__u.__table_);
insert(__u.begin(), __u.end());
}
#endif #endif
return *this; return *this;
} }

View File

@ -57,6 +57,21 @@ int main()
assert(*next(mo.begin()) == V(2, 1)); assert(*next(mo.begin()) == V(2, 1));
assert(*next(mo.begin(), 2) == V(3, 1)); assert(*next(mo.begin(), 2) == V(3, 1));
} }
{
typedef std::pair<const int, double> V;
const V ar[] =
{
V(1, 1),
V(2, 1),
V(3, 1),
};
std::map<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
std::map<int, double> *p = &m;
m = *p;
assert(m.size() == 3);
assert(std::equal(m.begin(), m.end(), ar));
}
{ {
typedef std::pair<const int, double> V; typedef std::pair<const int, double> V;
V ar[] = V ar[] =

View File

@ -48,6 +48,26 @@ int main()
assert(mo.get_allocator() == A(2)); assert(mo.get_allocator() == A(2));
assert(mo.key_comp() == C(5)); assert(mo.key_comp() == C(5));
} }
{
typedef std::pair<const int, double> V;
const V ar[] =
{
V(1, 1),
V(1, 1.5),
V(1, 2),
V(2, 1),
V(2, 1.5),
V(2, 2),
V(3, 1),
V(3, 1.5),
V(3, 2),
};
std::multimap<int, double> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
std::multimap<int, double> *p = &m;
m = *p;
assert(m.size() == sizeof(ar)/sizeof(ar[0]));
assert(std::equal(m.begin(), m.end(), ar));
}
{ {
typedef std::pair<const int, double> V; typedef std::pair<const int, double> V;
V ar[] = V ar[] =

View File

@ -68,6 +68,26 @@ int main()
assert(*next(mo.begin(), 7) == 3); assert(*next(mo.begin(), 7) == 3);
assert(*next(mo.begin(), 8) == 3); assert(*next(mo.begin(), 8) == 3);
} }
{
typedef int V;
const V ar[] =
{
1,
1,
1,
2,
2,
2,
3,
3,
3
};
std::multiset<int> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
std::multiset<int> *p = &m;
m = *p;
assert(m.size() == 9);
assert(std::equal(m.begin(), m.end(), ar));
}
{ {
typedef int V; typedef int V;
V ar[] = V ar[] =

View File

@ -56,6 +56,21 @@ int main()
assert(*next(mo.begin()) == 2); assert(*next(mo.begin()) == 2);
assert(*next(mo.begin(), 2) == 3); assert(*next(mo.begin(), 2) == 3);
} }
{
typedef int V;
const V ar[] =
{
1,
2,
3
};
std::set<int> m(ar, ar+sizeof(ar)/sizeof(ar[0]));
std::set<int> *p = &m;
m = *p;
assert(m.size() == 3);
assert(std::equal(m.begin(), m.end(), ar));
}
{ {
typedef int V; typedef int V;
V ar[] = V ar[] =

View File

@ -72,6 +72,24 @@ int main()
assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
assert(c.max_load_factor() == 1); assert(c.max_load_factor() == 1);
} }
{
typedef std::unordered_map<int, std::string> C;
typedef std::pair<const int, std::string> P;
const P a[] =
{
P(1, "one"),
P(2, "two"),
P(3, "three"),
P(4, "four"),
P(1, "four"),
P(2, "four"),
};
C c(a, a + sizeof(a)/sizeof(a[0]));
C *p = &c;
c = *p;
assert(c.size() == 4);
assert(std::is_permutation(c.begin(), c.end(), a));
}
{ {
typedef other_allocator<std::pair<const int, std::string> > A; typedef other_allocator<std::pair<const int, std::string> > A;
typedef std::unordered_map<int, std::string, typedef std::unordered_map<int, std::string,

View File

@ -86,6 +86,24 @@ int main()
assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
assert(c.max_load_factor() == 1); assert(c.max_load_factor() == 1);
} }
{
typedef std::unordered_multimap<int, std::string> C;
typedef std::pair<const int, std::string> P;
const P a[] =
{
P(1, "one"),
P(2, "two"),
P(3, "three"),
P(4, "four"),
P(1, "four"),
P(2, "four"),
};
C c(a, a+sizeof(a)/sizeof(a[0]));
C *p = &c;
c = *p;
assert(c.size() == 6);
assert(std::is_permutation(c.begin(), c.end(), a));
}
{ {
typedef other_allocator<std::pair<const int, std::string> > A; typedef other_allocator<std::pair<const int, std::string> > A;
typedef std::unordered_multimap<int, std::string, typedef std::unordered_multimap<int, std::string,

View File

@ -79,6 +79,25 @@ int main()
assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
assert(c.max_load_factor() == 1); assert(c.max_load_factor() == 1);
} }
{
typedef std::unordered_multiset<int> C;
typedef int P;
P a[] =
{
P(1),
P(2),
P(3),
P(4),
P(1),
P(2)
};
C c(a, a + sizeof(a)/sizeof(a[0]));
C *p = &c;
c = *p;
assert(c.size() == 6);
assert(std::is_permutation(c.begin(), c.end(), a));
}
{ {
typedef other_allocator<int> A; typedef other_allocator<int> A;
typedef std::unordered_multiset<int, typedef std::unordered_multiset<int,

View File

@ -71,6 +71,24 @@ int main()
assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON); assert(fabs(c.load_factor() - (float)c.size()/c.bucket_count()) < FLT_EPSILON);
assert(c.max_load_factor() == 1); assert(c.max_load_factor() == 1);
} }
{
typedef std::unordered_set<int> C;
typedef int P;
P a[] =
{
P(1),
P(2),
P(3),
P(4),
P(1),
P(2)
};
C c(a, a + sizeof(a)/sizeof(a[0]));
C *p = &c;
c = *p;
assert(c.size() == 4);
assert(std::is_permutation(c.begin(), c.end(), a));
}
{ {
typedef other_allocator<int> A; typedef other_allocator<int> A;
typedef std::unordered_set<int, typedef std::unordered_set<int,