summaryrefslogtreecommitdiff
path: root/flower/test-std.cc
blob: 1c1a71fa3a058f3e91a388cedc749010c6b39a14 (about) (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#define STD_VECTOR 1

#if !STD_VECTOR
#define Array flower_vector
#endif

#define HAVE_BOOST_LAMBDA 1
#include "std-vector.hh"

#include <iostream>

#define YAFFUT_MAIN
#include "yaffut.hh"

#if !STD_VECTOR
#define vector flower_vector
#endif

using namespace std;

template<typename T>
void
print (vector<T> v)
{
  for (vsize i = 0; i < v.size (); i++)
    cout << "v[" << i << "] = " << v[i] << endl;
  cout << endl;
}

#if !STD_VECTOR
template<typename T>
void
print (Link_array<T> v)
{
  for (vsize i = 0; i < v.size (); i++)
    cout << "v[" << i << "] = " << *v[i] << endl;
  cout << endl;
}
#endif

FUNC (vector_erase)
{
  vector<int> v;
  v.push_back (0);
  v.push_back (1);
  EQUAL (v.size (), vsize (2));
  v.erase (v.begin () + 1);
  EQUAL (v.size (), vsize (1));
  EQUAL (v.back (), 0);

  v.push_back (1);
  EQUAL (v.size (), vsize (2));
  v.erase (v.begin () + 0);
  EQUAL (v.size (), vsize (1));
  EQUAL (v.back (), 1);
}

FUNC (vector_slice)
{
  vector<int> v;
  v.push_back (0);
  v.push_back (1);
  v.push_back (2);
  v.push_back (3);
#if VECTOR_SLICE
  EQUAL (v.slice (0, 0).size (), vsize (0));
  EQUAL (v.slice (0, v.size ()).size (), v.size ());
  EQUAL (v.slice (1, 2).size (), vsize (1));
#else
  EQUAL (vector<int> (v.begin (), v.begin ()).size (), vsize (0));
  EQUAL (vector<int> (v.begin (), v.end ()).size (), v.size ());
  EQUAL (vector<int> (v.begin () + 1, v.begin () + 2).size (),
		     vsize (1));
#endif
}

FUNC (vector_sorting)
{
  vector<int> v;
  v.push_back (2);
  v.push_back (1);
  v.push_back (0);
#if VECTOR_SORT
  v.sort (default_compare);
#else
  //sort (v.begin (), v.end ());
  vector_sort (v, less<int> ());
#endif
  EQUAL (v[0], 0);
  EQUAL (v[1], 1);
  EQUAL (v[2], 2);
}

FUNC (vector_insert)
{
  vector<int> v;
  v.push_back (0);
#if VECTOR_INSERT
  v.insert (1, 0);
#else
  v.insert (v.begin (), 1);
#endif  
  EQUAL (v[0], 1);
#if VECTOR_INSERT
  v.insert (2, v.size ());
#else
  v.insert (v.end (), 2);
#endif  
  EQUAL (v.back (), 2);
  vector<int> u;
  u.insert (u.begin (), v.begin (), v.end ());
  EQUAL (u.size (), v.size ());
  u.clear ();
  u.insert (u.end (), v.begin (), v.end ());
  EQUAL (u.size (), v.size ());
  u.clear ();
}

FUNC (parray_concat)
{
#if !STD_VECTOR
  Link_array<int> u, v;
#else
  vector<int*> u, v;
#endif  
  int a[5] = { 0, 1, 2, 3, 4 };
  u.push_back (&a[0]);
  u.push_back (&a[1]);
  u.push_back (&a[2]);
  v.push_back (&a[3]);
  v.push_back (&a[4]);
  concat (u, v);
  EQUAL (u[0], &a[0]);
  EQUAL (u[1], &a[1]);
  EQUAL (u[2], &a[2]);
  EQUAL (u[3], &a[3]);
  EQUAL (u[4], &a[4]);
  EQUAL (u.size (), vsize (5));
  concat (u, v);
  EQUAL (u.size (), vsize (7));

  u.clear ();
  v.clear ();
  v.push_back (&a[0]);
  v.push_back (&a[1]);
  v.push_back (&a[2]);
  v.push_back (&a[3]);
  v.push_back (&a[4]);
  concat (u, v);
  EQUAL (u[0], &a[0]);
  EQUAL (u[1], &a[1]);
  EQUAL (u[2], &a[2]);
  EQUAL (u[3], &a[3]);
  EQUAL (u[4], &a[4]);
  EQUAL (u.size (), vsize (5));
}

FUNC (parray_uniq)
{
  vector<int> v;
  v.push_back (0);
  v.push_back (1);
  v.push_back (0);
  vector_sort (v, less<int> ());
  uniq (v);
  EQUAL (v.size (), vsize (2));
}

FUNC (vector_search)
{
  vector<int> v;
  v.push_back (0);
  v.push_back (1);
  v.push_back (2);
  vsize i = binary_search (v, 1, less<int> ());
  EQUAL (i, vsize (1));
}