Only create gcc/configargs.h if gcc build directory is present
[official-gcc.git] / libstdc++ / tests / tlist.cc
bloba37c999d0b55ae030037d157e326a3e067f8f571
1 // test/demo of generic lists
3 #include <assert.h>
5 #define tassert(ex) {if ((ex)) cerr << #ex << "\n"; \
6 else _assert(#ex, __FILE__,__LINE__); }
8 #include <iostream.h>
9 #include <list.h>
10 #include <algo.h>
12 bool int_compare(int a, int b)
14 return a < b;
17 int inc(int x)
19 return x + 1;
22 void print(list<int>& l)
24 for (list<int>::iterator it = l.begin(); it != l.end(); it++)
25 cout << *it << " ";
26 cout << "\n";
29 int is_odd(int x)
31 return x & 1;
34 int is_even(int x)
36 return (x & 1) == 0;
39 void sequence(list<int>& a, int lo, int hi)
41 back_insert_iterator<list<int> > it(a);
42 while (lo <= hi)
43 *it++ = lo++;
46 int old_rand = 9999;
48 int get_rand()
50 old_rand = ((long)old_rand * (long)1243) % (long)971;
51 return old_rand;
54 void randseq(list<int>& a, int n)
56 back_insert_iterator<list<int> > it(a);
57 while (--n >= 0)
58 *it++ = get_rand() % 50;
61 int array1 [] = { 9, 16, 36 };
62 int array2 [] = { 1, 4 };
64 int test_splice ()
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;
76 main()
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);
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);
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);
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());
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);
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());
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);
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);
143 cout << "\nh via iterator:\n";
144 for (it = h.begin(); it != h.end (); it++)
145 cout << *it << ", ";
146 cout << "\n";
148 test_splice ();
150 cout << "\ndone\n";