Initial import from FreeBSD RELENG_4:
[dragonfly.git] / contrib / libstdc++ / tests / tlist.cc
1 // test/demo of generic lists
2
3 #include <assert.h>
4
5 #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
6                        else _assert(#ex, __FILE__,__LINE__); }
7
8 #include <iostream.h>
9 #include <list.h>
10 #include <algo.h>
11
12 bool int_compare(int a, int b)
13 {
14   return a < b;
15 }
16
17 int inc(int x)
18 {
19   return x + 1;
20 }
21
22 void print(list<int>& l)
23 {
24   for (list<int>::iterator it = l.begin(); it != l.end(); it++)
25     cout << *it << " ";
26   cout << "\n";
27 }
28
29 int is_odd(int x)
30 {
31   return x & 1;
32 }
33
34 int is_even(int x)
35 {
36   return (x & 1) == 0;
37 }
38
39 void sequence(list<int>& a, int lo, int hi)
40 {
41   back_insert_iterator<list<int> > it(a);
42   while (lo <= hi)
43     *it++ = lo++;
44 }
45
46 int old_rand = 9999;
47
48 int get_rand()
49 {
50     old_rand = ((long)old_rand * (long)1243) % (long)971;
51     return old_rand;
52 }
53
54 void randseq(list<int>& a, int n)
55 {
56   back_insert_iterator<list<int> > it(a);
57   while (--n >= 0)
58     *it++ = get_rand() % 50;
59 }
60
61 int array1 [] = { 9, 16, 36 };
62 int array2 [] = { 1, 4 };
63
64 int test_splice ()
65 {
66   list<int> l1 (array1, array1 + 3);
67   list<int> l2 (array2, array2 + 2);
68   list<int>::iterator i1 = l1.begin ();
69   l1.splice (i1, l2);
70   list<int>::iterator i2 = l1.begin ();
71   while (i2 != l1.end ())
72     cout << *i2++ << endl;
73   return 0;
74 }
75
76 main()
77 {
78   list<int> a;  int i;
79   list<int>::iterator it, bit;
80   sequence(a, 1, 20);
81   cout << "\nlist<int> a = sequence(1, 20);\n"; print(a);
82   for (it = a.begin (), i = 0; it != a.end (); it++, i++)
83     assert (*it == i + 1);
84   list<int> b;
85   randseq(b, 20);
86   cout << "\nlist<int> b = randseq(20);\n"; print(b);
87   list<int> c;
88   c.insert (c.end(), a.begin(), a.end());
89   c.insert (c.end(), b.begin(), b.end());
90   cout << "\nlist<int> c = a and b;\n"; print(c);
91
92   list<int> d;
93   for (it = a.begin(); it != a.end(); it++)
94     d.insert(d.end (), inc(*it));
95   cout << "\nlist<int> d = map(inc, a);\n"; print(d);
96
97   list<int> e;
98   back_insert_iterator<list<int> > e_insertor (e);
99   reverse_copy (a.begin(), a.end (), e_insertor);
100   cout << "\nlist<int> e = reverse(a);\n"; print(e);
101
102   list<int> f;
103   for (it = a.begin(); it != a.end(); it++)
104     if (is_odd (*it))
105       f.insert(f.end (), *it);
106   cout << "\nlist<int> f = select(is_odd, a);\n"; print(f);
107   list<int> ff;
108   for (it = f.begin(); it != f.end(); it++)
109     if (is_even (*it))
110       ff.insert(ff.end (), *it);
111   assert(ff.empty());
112
113   int red = 0;
114   for (it = a.begin(); it != a.end(); it++)
115     red += *it;
116   cout << "\nint  red = a.reduce(plus, 0);\n"; cout << red;
117   it = a.begin(); ++it; ++it;
118   int second = *it;
119   cout << "\nint second = a[2];\n"; cout << second;
120   list<int> g;
121   for (it = a.begin(), bit = b.begin(); it != a.end () && bit != b.end (); )
122     g.insert (g.end (), *it++ + *bit++);
123   cout << "\nlist<int> g = combine(plus, a, b);\n"; print(g);
124   g.remove_if (is_odd);
125   cout << "\ng.del(is_odd);\n"; print(g);
126
127   ff.erase (ff.begin (), ff.end());
128   for (it = g.begin(); it != g.end(); it++)
129     if (is_odd (*it))
130       ff.insert (ff.end (), *it);
131   assert(ff.empty());
132
133   b.sort();
134   for (it = b.begin(); bit = it++, it != b.end (); ) assert (*it >= *bit);
135   cout << "\nb.sort(int_compare);\n"; print(b);
136
137   list<int> h;
138   back_insert_iterator<list<int> > h_insertor (h);
139   merge (a.begin (), a.end (), b.begin (), b.end (), h_insertor, int_compare);
140   cout << "\nlist<int> h = merge(a, b, int_compare);\n"; print(h);
141   for (it = h.begin(); bit = it++, it != h.end (); ) assert (*it >= *bit);
142
143   cout << "\nh via iterator:\n";
144   for (it = h.begin(); it != h.end (); it++)
145     cout << *it << ", ";
146   cout << "\n";
147
148   test_splice ();
149
150   cout << "\ndone\n";
151 }