File: vectors.cpp

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (389 lines) | stat: -rw-r--r-- 9,257 bytes parent folder | download | duplicates (4)
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
///////////////////////////////////////////////////////////////////////////////
// Name:        tests/vectors/vectors.cpp
// Purpose:     wxVector<T> unit test
// Author:      Vaclav Slavik
// Created:     2007-07-07
// Copyright:   (c) 2007 Vaclav Slavik
///////////////////////////////////////////////////////////////////////////////

// ----------------------------------------------------------------------------
// headers
// ----------------------------------------------------------------------------

#include "testprec.h"


#ifndef WX_PRECOMP
    #include "wx/wx.h"
#endif // WX_PRECOMP

#include "wx/vector.h"

#if wxUSE_STD_CONTAINERS_COMPATIBLY
    #include <vector>
#endif // wxUSE_STD_CONTAINERS_COMPATIBLY

// ----------------------------------------------------------------------------
// simple class capable of detecting leaks of its objects
// ----------------------------------------------------------------------------

class CountedObject
{
public:
    CountedObject(int n = 0) : m_n(n) { ms_count++; }
    CountedObject(const CountedObject& co) : m_n(co.m_n) { ms_count++; }
    ~CountedObject() { ms_count--; }

    int GetValue() const { return m_n; }

    static int GetCount() { return ms_count; }

private:
    static int ms_count;

    int m_n;
};

int CountedObject::ms_count = 0;

// ----------------------------------------------------------------------------
// simple class capable of checking its "this" pointer validity
// ----------------------------------------------------------------------------

class SelfPointingObject
{
public:
    SelfPointingObject() { m_self = this; }
    SelfPointingObject(const SelfPointingObject&) { m_self = this; }
    ~SelfPointingObject() { CHECK( this == m_self ); }

    // the assignment operator should not modify our "this" pointer so
    // implement it just to prevent the default version from doing it
    SelfPointingObject& operator=(const SelfPointingObject&) { return *this; }

private:
    SelfPointingObject *m_self;
};

// ----------------------------------------------------------------------------
// test class
// ----------------------------------------------------------------------------

TEST_CASE("wxVector::Push/Pop", "[vector][push_back][pop_back]")
{
    wxVector<int> v;

    CHECK( v.size() == 0 );
    v.push_back(1);
    CHECK( v.size() == 1 );
    v.push_back(2);
    CHECK( v.size() == 2 );
    v.push_back(42);
    CHECK( v.size() == 3 );

    CHECK( v[0] == 1 );
    CHECK( v[1] == 2 );
    CHECK( v[2] == 42 );

    v.pop_back();
    CHECK( v.size() == 2 );
    CHECK( v[0] == 1 );
    CHECK( v[1] == 2 );

    v.pop_back();
    CHECK( v.size() == 1 );
    CHECK( v[0] == 1 );

    v.pop_back();
    CHECK( v.size() == 0 );
    CHECK( v.empty() );

    wxVector<char> vEmpty;
}

TEST_CASE("wxVector::Insert", "[vector][insert]")
{
    wxVector<char> v;

    v.insert(v.end(), 'a');
    CHECK( v.size() == 1 );
    CHECK( v[0] == 'a' );

    v.insert(v.end(), 'b');
    CHECK( v.size() == 2 );
    CHECK( v[0] == 'a' );
    CHECK( v[1] == 'b' );

    v.insert(v.begin(), '0');
    CHECK( v.size() == 3 );
    CHECK( v[0] == '0' );
    CHECK( v[1] == 'a' );
    CHECK( v[2] == 'b' );

    v.insert(v.begin() + 2, 'X');
    CHECK( v.size() == 4 );
    CHECK( v[0] == '0' );
    CHECK( v[1] == 'a' );
    CHECK( v[2] == 'X' );
    CHECK( v[3] == 'b' );

    v.insert(v.begin() + 3, 3, 'Z');
    REQUIRE( v.size() == 7 );
    CHECK( v[0] == '0' );
    CHECK( v[1] == 'a' );
    CHECK( v[2] == 'X' );
    CHECK( v[3] == 'Z' );
    CHECK( v[4] == 'Z' );
    CHECK( v[5] == 'Z' );
    CHECK( v[6] == 'b' );
}

TEST_CASE("wxVector::Erase", "[vector][erase]")
{
    wxVector<int> v;

    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    CHECK( v.size() == 4 );

    v.erase(v.begin(), v.end()-1);
    CHECK( v.size() == 1 );
    CHECK( v[0] == 4 );

    v.clear();
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);
    CHECK( v.size() == 4 );

    v.erase(v.begin());
    CHECK( v.size() == 3 );
    CHECK( v[0] == 2 );
    CHECK( v[1] == 3 );
    CHECK( v[2] == 4 );
}

TEST_CASE("wxVector::Iterators", "[vector][iterator]")
{
    wxVector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    v.push_back(4);

    int value = 1;
    for ( wxVector<int>::iterator i = v.begin(); i != v.end(); ++i, ++value )
    {
        CHECK( *i == value );
    }
}

TEST_CASE("wxVector::Objects", "[vector]")
{
    wxVector<CountedObject> v;
    v.push_back(CountedObject(1));
    v.push_back(CountedObject(2));
    v.push_back(CountedObject(3));

    v.erase(v.begin());
    CHECK( v.size() == 2 );
    CHECK( CountedObject::GetCount() == 2 );

    v.clear();
    CHECK( CountedObject::GetCount() == 0 );
}

TEST_CASE("wxVector::NonPODs", "[vector]")
{
    wxVector<SelfPointingObject> v;
    v.push_back(SelfPointingObject());
    v.push_back(SelfPointingObject());
    v.push_back(SelfPointingObject());

    v.erase(v.begin());
    v.clear();

    // try the same with wxString, which is not POD, but is implemented in
    // a movable way (this won't assert, but would crash or show some memory
    // problems under Valgrind if wxString couldn't be safely moved with
    // memmove()):
    wxVector<wxString> vs;
    vs.push_back("one");
    vs.push_back("two");
    vs.push_back("three");

    vs.erase(vs.begin());
    vs.clear();
}

TEST_CASE("wxVector::Resize", "[vector][resize]")
{
    wxVector<CountedObject> v;
    v.resize(3);

    CHECK( v.size() == 3 );
    CHECK( CountedObject::GetCount() == 3 );
    CHECK( v[0].GetValue() == 0 );
    CHECK( v[1].GetValue() == 0 );
    CHECK( v[2].GetValue() == 0 );

    v.resize(1);
    CHECK( v.size() == 1 );
    CHECK( CountedObject::GetCount() == 1 );

    v.resize(4, CountedObject(17));
    CHECK( v.size() == 4 );
    CHECK( CountedObject::GetCount() == 4 );
    CHECK( v[0].GetValue() == 0 );
    CHECK( v[1].GetValue() == 17 );
    CHECK( v[2].GetValue() == 17 );
    CHECK( v[3].GetValue() == 17 );
}

TEST_CASE("wxVector::Swap", "[vector][swap]")
{
    wxVector<int> v1, v2;
    v1.push_back(17);
    v1.swap(v2);
    CHECK( v1.empty() );
    CHECK( v2.size() == 1 );
    CHECK( v2[0] == 17 );

    v1.push_back(9);
    v2.swap(v1);
    CHECK( v1.size() == 1 );
    CHECK( v1[0] == 17 );
    CHECK( v2.size() == 1 );
    CHECK( v2[0] == 9 );

    v2.clear();
    v1.swap(v2);
    CHECK( v1.empty() );
}

TEST_CASE("wxVector::Sort", "[vector][sort]")
{
    size_t  idx;
    wxVector<int> v;

    v.push_back(5);
    v.push_back(7);
    v.push_back(2);
    v.push_back(9);
    v.push_back(4);
    v.push_back(1);
    v.push_back(3);
    v.push_back(8);
    v.push_back(0);
    v.push_back(6);

    wxVectorSort(v);

    for (idx=1; idx<v.size(); idx++)
    {
        CHECK( v[idx-1] <= v[idx] );
    }
}

TEST_CASE("wxVector::Contains", "[vector][contains]")
{
    wxVector<int> v;
    CHECK( !wxVectorContains(v, 0) );

    v.push_back(3);
    CHECK( wxVectorContains(v, 3) );

    v.push_back(2);
    v.push_back(3);

    CHECK( wxVectorContains(v, 2) );
    CHECK( wxVectorContains(v, 3) );
    CHECK( !wxVectorContains(v, 1) );
}

TEST_CASE("wxVector::operator==", "[vector][compare]")
{
    wxVector<wxString> v1, v2;
    CHECK( v1 == v2 );
    CHECK( !(v1 != v2) );

    v1.push_back("foo");
    CHECK( v1 != v2 );

    v2.push_back("foo");
    CHECK( v1 == v2 );

    v1.push_back("bar");
    v2.push_back("baz");
    CHECK( v1 != v2 );
}

TEST_CASE("wxVector::reverse_iterator", "[vector][reverse_iterator]")
{
    wxVector<int> v;
    for ( int i = 0; i < 10; ++i )
        v.push_back(i + 1);

    const wxVector<int>::reverse_iterator rb = v.rbegin();
    const wxVector<int>::reverse_iterator re = v.rend();
    CHECK( re - rb == 10 );

    wxVector<int>::reverse_iterator ri = rb;
    ++ri;
    CHECK( ri - rb == 1 );
    CHECK( re - ri == 9 );

    ri = rb + 2;
    CHECK( ri - rb == 2 );
    CHECK( re - ri == 8 );

    CHECK( rb < ri );
    CHECK( rb <= ri );
    CHECK( ri <= ri );
    CHECK( ri >= ri );
    CHECK( ri < re );
    CHECK( ri <= re );

    CHECK( rb.base() == v.end() );
    CHECK( re.base() == v.begin() );
    CHECK( *ri.base() == 9 );

#if wxUSE_STD_CONTAINERS_COMPATIBLY
    std::vector<int> stdvec(rb, re);
    REQUIRE( stdvec.size() == 10 );
    CHECK( stdvec[0] == 10 );
    CHECK( stdvec[9] == 1 );
#endif // wxUSE_STD_CONTAINERS_COMPATIBLY
}

TEST_CASE("wxVector::capacity", "[vector][capacity][shrink_to_fit]")
{
    wxVector<int> v;
    CHECK( v.capacity() == 0 );

    v.push_back(0);
    // When using the standard library vector, we don't know what growth
    // strategy it uses, so we can't rely on the stricter check passing, but
    // with our own one we can, allowing us to check that shrink_to_fit()
    // really shrinks the capacity below.
#if !wxUSE_STD_CONTAINERS
    CHECK( v.capacity() > 1 );
#else
    CHECK( v.capacity() >= 1 );
#endif

    // There is no shrink_to_fit() in STL build when not using C++11.
#if !wxUSE_STD_CONTAINERS || __cplusplus >= 201103L || wxCHECK_VISUALC_VERSION(10)
    v.shrink_to_fit();
    CHECK( v.capacity() == 1 );

    v.erase(v.begin());
    CHECK( v.capacity() == 1 );

    v.shrink_to_fit();
    CHECK( v.capacity() == 0 );
#endif
}