Skip to content

Commit 83465c7

Browse files
committed
Add tests for stability to list::sort and forward_list::sort. Thanks to Jonathan Wakely for the notice
llvm-svn: 358541
1 parent 6b44291 commit 83465c7

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/sort.pass.cpp

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,46 @@ void test(int N)
3838
assert(*j == i);
3939
}
4040

41+
struct Payload
42+
{
43+
int val;
44+
int side;
45+
Payload(int v) : val(v), side(0) {}
46+
Payload(int v, int s) : val(v), side(s) {}
47+
bool operator< (const Payload &rhs) const { return val < rhs.val;}
48+
// bool operator==(const Payload &rhs) const { return val == rhs.val;}
49+
};
50+
51+
void test_stable(int N)
52+
{
53+
typedef Payload T;
54+
typedef std::forward_list<T> C;
55+
typedef std::vector<T> V;
56+
V v;
57+
for (int i = 0; i < N; ++i)
58+
v.push_back(Payload(i/2));
59+
std::shuffle(v.begin(), v.end(), randomness);
60+
for (int i = 0; i < N; ++i)
61+
v[i].side = i;
62+
63+
C c(v.begin(), v.end());
64+
c.sort();
65+
assert(distance(c.begin(), c.end()) == N);
66+
67+
// Are we sorted?
68+
typename C::const_iterator j = c.begin();
69+
for (int i = 0; i < N; ++i, ++j)
70+
assert(j->val == i/2);
71+
72+
// Are we stable?
73+
for (C::const_iterator it = c.begin(); it != c.end(); ++it)
74+
{
75+
C::const_iterator next = std::next(it);
76+
if (next != c.end() && it->val == next->val)
77+
assert(it->side < next->side);
78+
}
79+
}
80+
4181
int main(int, char**)
4282
{
4383
for (int i = 0; i < 40; ++i)
@@ -47,5 +87,8 @@ int main(int, char**)
4787
test<std::forward_list<int, min_allocator<int>> >(i);
4888
#endif
4989

90+
for (int i = 0; i < 40; ++i)
91+
test_stable(i);
92+
5093
return 0;
5194
}

libcxx/test/std/containers/sequences/forwardlist/forwardlist.ops/sort_pred.pass.cpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,53 @@
1717
#include <functional>
1818
#include <random>
1919
#include <cassert>
20+
#include <iostream>
2021

2122
#include "min_allocator.h"
2223

2324
std::mt19937 randomness;
2425

26+
struct Payload
27+
{
28+
int val;
29+
int side;
30+
Payload(int v) : val(v), side(0) {}
31+
Payload(int v, int s) : val(v), side(s) {}
32+
bool operator< (const Payload &rhs) const { return val < rhs.val;}
33+
};
34+
35+
bool greater(const Payload &lhs, const Payload &rhs) { return lhs.val > rhs.val; }
36+
37+
void test_stable(int N)
38+
{
39+
typedef Payload T;
40+
typedef std::forward_list<T> C;
41+
typedef std::vector<T> V;
42+
V v;
43+
for (int i = 0; i < N; ++i)
44+
v.push_back(Payload(i/2));
45+
std::shuffle(v.begin(), v.end(), randomness);
46+
for (int i = 0; i < N; ++i)
47+
v[i].side = i;
48+
49+
C c(v.begin(), v.end());
50+
c.sort(greater);
51+
assert(distance(c.begin(), c.end()) == N);
52+
53+
// Are we sorted?
54+
typename C::const_iterator j = c.begin();
55+
for (int i = 0; i < N; ++i, ++j)
56+
assert(j->val == (N-1-i)/2);
57+
58+
// Are we stable?
59+
for (C::const_iterator it = c.begin(); it != c.end(); ++it)
60+
{
61+
C::const_iterator next = std::next(it);
62+
if (next != c.end() && it->val == next->val)
63+
assert(it->side < next->side);
64+
}
65+
}
66+
2567
template <class C>
2668
void test(int N)
2769
{
@@ -48,5 +90,8 @@ int main(int, char**)
4890
test<std::forward_list<int, min_allocator<int>> >(i);
4991
#endif
5092

93+
for (int i = 0; i < 40; ++i)
94+
test_stable(i);
95+
5196
return 0;
5297
}

libcxx/test/std/containers/sequences/list/list.ops/sort.pass.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,55 @@
1111
// void sort();
1212

1313
#include <list>
14+
#include <random>
15+
#include <vector>
1416
#include <cassert>
1517

1618
#include "min_allocator.h"
1719

20+
std::mt19937 randomness;
21+
22+
struct Payload
23+
{
24+
int val;
25+
int side;
26+
Payload(int v) : val(v), side(0) {}
27+
Payload(int v, int s) : val(v), side(s) {}
28+
bool operator< (const Payload &rhs) const { return val < rhs.val;}
29+
// bool operator==(const Payload &rhs) const { return val == rhs.val;}
30+
};
31+
32+
void test_stable(int N)
33+
{
34+
typedef Payload T;
35+
typedef std::list<T> C;
36+
typedef std::vector<T> V;
37+
V v;
38+
for (int i = 0; i < N; ++i)
39+
v.push_back(Payload(i/2));
40+
std::shuffle(v.begin(), v.end(), randomness);
41+
for (int i = 0; i < N; ++i)
42+
v[i].side = i;
43+
44+
C c(v.begin(), v.end());
45+
c.sort();
46+
assert(distance(c.begin(), c.end()) == N);
47+
48+
// Are we sorted?
49+
typename C::const_iterator j = c.begin();
50+
for (int i = 0; i < N; ++i, ++j)
51+
assert(j->val == i/2);
52+
53+
// Are we stable?
54+
for (C::const_iterator it = c.begin(); it != c.end(); ++it)
55+
{
56+
C::const_iterator next = std::next(it);
57+
if (next != c.end() && it->val == next->val)
58+
assert(it->side < next->side);
59+
}
60+
}
61+
62+
1863
int main(int, char**)
1964
{
2065
{
@@ -34,5 +79,8 @@ int main(int, char**)
3479
}
3580
#endif
3681

82+
for (int i = 0; i < 40; ++i)
83+
test_stable(i);
84+
3785
return 0;
3886
}

libcxx/test/std/containers/sequences/list/list.ops/sort_comp.pass.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,13 @@
1313
#include <list>
1414
#include <functional>
1515
#include <algorithm> // for is_permutation
16+
#include <random>
17+
#include <vector>
1618
#include <cassert>
1719

1820
#include "min_allocator.h"
1921

22+
std::mt19937 randomness;
2023

2124
#ifndef TEST_HAS_NO_EXCEPTIONS
2225
template <typename T>
@@ -35,6 +38,48 @@ struct throwingLess {
3538
#endif
3639

3740

41+
struct Payload
42+
{
43+
int val;
44+
int side;
45+
Payload(int v) : val(v), side(0) {}
46+
Payload(int v, int s) : val(v), side(s) {}
47+
bool operator< (const Payload &rhs) const { return val < rhs.val;}
48+
};
49+
50+
bool greater(const Payload &lhs, const Payload &rhs) { return lhs.val > rhs.val; }
51+
52+
void test_stable(int N)
53+
{
54+
typedef Payload T;
55+
typedef std::list<T> C;
56+
typedef std::vector<T> V;
57+
V v;
58+
for (int i = 0; i < N; ++i)
59+
v.push_back(Payload(i/2));
60+
std::shuffle(v.begin(), v.end(), randomness);
61+
for (int i = 0; i < N; ++i)
62+
v[i].side = i;
63+
64+
C c(v.begin(), v.end());
65+
c.sort(greater);
66+
assert(distance(c.begin(), c.end()) == N);
67+
68+
// Are we sorted?
69+
typename C::const_iterator j = c.begin();
70+
for (int i = 0; i < N; ++i, ++j)
71+
assert(j->val == (N-1-i)/2);
72+
73+
// Are we stable?
74+
for (C::const_iterator it = c.begin(); it != c.end(); ++it)
75+
{
76+
C::const_iterator next = std::next(it);
77+
if (next != c.end() && it->val == next->val)
78+
assert(it->side < next->side);
79+
}
80+
}
81+
82+
3883
int main(int, char**)
3984
{
4085
{
@@ -76,5 +121,8 @@ int main(int, char**)
76121
}
77122
#endif
78123

124+
for (int i = 0; i < 40; ++i)
125+
test_stable(i);
126+
79127
return 0;
80128
}

0 commit comments

Comments
 (0)