File: test_slice.cpp

package info (click to toggle)
exiv2 0.27.3-3%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,588 kB
  • sloc: cpp: 80,772; python: 4,537; sh: 1,497; makefile: 329; javascript: 237; awk: 92; ansic: 78; sed: 16
file content (433 lines) | stat: -rw-r--r-- 12,570 bytes parent folder | download
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
#include <exiv2/exiv2.hpp>
#include <stdint.h>

#include "slice.hpp"
#include "types.hpp"

#include "gtestwrapper.h"

using namespace Exiv2;

template <typename T>
class slice;

/*!
 * This namespace contains the helper-function get_test_data. It is intented
 * to be used for test with the slice fixture: it returns the appropriate
 * data to the constructor of slice. For (const) T==std::vector it returns the
 * fixtures meber vec_, for (const) T==int* it returns vec_.data()
 *
 * Due to C++98's limitations, this requires a separate traits class, that
 * specifies the return type *and* a specialization of get_test_data for each
 * case (maybe some can be reduced with SFINAE, but that ain't improving
 * readability either).
 *
 * Unfortunately, C++11 will probably only make the return_type_traits go away,
 * but not the template specializations of get_test_data (for that we need
 * C++17, so see you in 2025).
 */
namespace cpp_98_boilerplate
{
    template <typename T>
    struct return_type_traits
    {
        typedef T type;
    };

    template <typename U>
    struct return_type_traits<std::vector<U> >
    {
        typedef typename std::vector<U>& type;
    };

    template <typename U>
    struct return_type_traits<const std::vector<U> >
    {
        typedef const typename std::vector<U>& type;
    };

    template <typename T>
    typename return_type_traits<T>::type get_test_data(slice<T>& st);

}  // namespace cpp_98_boilerplate

/*!
 * Fixture for slice testing. Has one public vector of ints with size vec_size
 * that is filled with the numbers from 0 to vec_size - 1.
 *
 * The vector vec_ is used to construct slices either from a std::vector, or
 * from raw C-arrays. Which type is used, is set by the template parameter
 * T. Thus we guarantee, that the interface is completely independent of the
 * underlying datatype.
 *
 * @tparam T  Type that is used to construct a slice for testing.
 */
template <typename T>
class slice : public ::testing::Test
{
public:
    static const size_t vec_size = 10;

    virtual void SetUp()
    {
        vec_.reserve(vec_size);
        for (unsigned int i = 0; i < vec_size; ++i) {
            vec_.push_back(i);
        }
    }

    Slice<T> getTestSlice(size_t begin = 1, size_t end = vec_size - 1)
    {
        return Slice<T>(cpp_98_boilerplate::get_test_data<T>(*this), begin, end);
    }

    // TODO: once we have C++11: use initializer list
    std::vector<int> vec_;
};

// specializations of get_test_data are provided here, since they must have the
// full definition of slice available
namespace cpp_98_boilerplate
{
    template <>
    int* get_test_data<int*>(slice<int*>& st)
    {
        return st.vec_.data();
    }

    template <>
    const int* get_test_data<const int*>(slice<const int*>& st)
    {
        return st.vec_.data();
    }

    template <>
    std::vector<int>& get_test_data<std::vector<int> >(slice<std::vector<int> >& st)
    {
        return st.vec_;
    }

    template <>
    const std::vector<int>& get_test_data<const std::vector<int> >(slice<const std::vector<int> >& st)
    {
        return st.vec_;
    }
}  // namespace cpp_98_boilerplate

/*!
 * Fixture to run test for mutable slices.
 *
 * It adds nothing new, it is just a separate class, so that we can run
 * different tests on it.
 */
template <typename T>
class mutableSlice : public slice<T>
{
};

TYPED_TEST_CASE_P(slice);
TYPED_TEST_CASE_P(mutableSlice);

TYPED_TEST_P(slice, atAccess)
{
    // typedef Slice<TypeParam> slice_t;
    // const size_t begin = 1;
    // const size_t end = this->vec_.size() - 1;
    Slice<TypeParam> sl = this->getTestSlice();

    ASSERT_EQ(this->vec_.size() - 2, sl.size());

    for (unsigned int i = 0; i < sl.size(); ++i) {
        ASSERT_EQ(this->vec_.at(i + 1), sl.at(i));
    }
}

// TODO C++11: test range based for loop
TYPED_TEST_P(slice, iteratorAccess)
{
    Slice<TypeParam> sl = this->getTestSlice();

    std::vector<int>::const_iterator vec_it = this->vec_.begin() + 1;
    for (typename Slice<TypeParam>::const_iterator it = sl.cbegin(); it < sl.cend(); ++it, ++vec_it) {
        ASSERT_EQ(*it, *vec_it);
    }

    ASSERT_THROW(sl.at(sl.size()), std::out_of_range);
}

TYPED_TEST_P(slice, constructionFailsFromInvalidRange)
{
    // start > end
    ASSERT_THROW(this->getTestSlice(2, 1), std::out_of_range);
}

TYPED_TEST_P(slice, constructionFailsWithZeroLength)
{
    ASSERT_THROW(this->getTestSlice(1, 1), std::out_of_range);
}

/*!
 * Test the construction of subSlices and their behavior.
 */
TYPED_TEST_P(slice, subSliceSuccessfulConstruction)
{
    typedef Slice<TypeParam> slice_t;

    // 0 1 2 3 4 5 6 7 8 9
    //       |     |       center_vals
    //         | |         middle
    slice_t center_vals = this->getTestSlice(3, 7);
    ASSERT_EQ(center_vals.size(), static_cast<size_t>(4));
    ASSERT_NO_THROW(center_vals.subSlice(1, 3));

    ASSERT_NO_THROW(center_vals.subSlice(1, center_vals.size()));
}

TYPED_TEST_P(slice, subSliceFunctions)
{
    Slice<TypeParam> middle = this->getTestSlice(3, 7).subSlice(1, 3);

    ASSERT_EQ(middle.size(), static_cast<size_t>(2));
    ASSERT_EQ(middle.at(1), static_cast<typename Slice<TypeParam>::value_type>(5));
}

TYPED_TEST_P(slice, subSliceFailedConstruction)
{
    // 0 1 2 3 4 5 6 7 8 9
    //         | |         middle
    Slice<TypeParam> middle = this->getTestSlice(4, 6);

    ASSERT_THROW(middle.subSlice(1, 5), std::out_of_range);
    ASSERT_THROW(middle.subSlice(2, 1), std::out_of_range);
    ASSERT_THROW(middle.subSlice(2, 2), std::out_of_range);
}

/*! try to cause integer overflows in a sub-optimal implementation */
TYPED_TEST_P(slice, subSliceConstructionOverflowResistance)
{
    Slice<TypeParam> center_vals = this->getTestSlice(3, 7);

    ASSERT_THROW(center_vals.subSlice(std::numeric_limits<size_t>::max() - 2, 3), std::out_of_range);
    ASSERT_THROW(center_vals.subSlice(2, std::numeric_limits<size_t>::max() - 1), std::out_of_range);
}

/*!
 * This function's purpose is only to check whether we can pass all slices by
 * constant reference.
 */
template <typename T>
void checkConstSliceValueAt(const Slice<T>& sl, typename Slice<T>::value_type value, size_t index)
{
    ASSERT_EQ(sl.at(index), value);
}

/*!
 * Check that the contents of the slice are ascending via an iterator based for
 * loop.
 */
template <typename T>
void checkConstSliceIterator(const Slice<T>& sl, typename Slice<T>::value_type first_value)
{
    for (typename Slice<T>::const_iterator it = sl.cbegin(); it < sl.cend(); ++it) {
        ASSERT_EQ(*it, first_value++);
    }
}

template <typename T>
void checkSubSlice(const Slice<T>& sl)
{
    ASSERT_EQ(sl.at(1), sl.subSlice(1, sl.size()).at(0));
}

/*!
 * Test that all slices can be also passed as const references and still work
 */
TYPED_TEST_P(slice, constMethodsPreserveConst)
{
    typedef Slice<TypeParam> slice_t;

    // 0 1 2 3 4 5 6 7 8 9
    //       |     |       center_vals
    slice_t center_vals = this->getTestSlice(3, 7);

    // check at() const works
    checkConstSliceValueAt(center_vals, 4, 1);

    checkConstSliceIterator(center_vals, 3);

    checkSubSlice(center_vals);
}

/*!
 * Test the non-const iterators
 */
TYPED_TEST_P(mutableSlice, iterators)
{
    typedef Slice<TypeParam> slice_t;
    slice_t sl = this->getTestSlice();

    ASSERT_EQ(*sl.begin(), static_cast<typename slice_t::value_type>(1));
    ASSERT_EQ(*sl.end(), static_cast<typename slice_t::value_type>(this->vec_size - 1));

    for (typename slice_t::iterator it = sl.begin(); it < sl.end(); ++it) {
        *it = 2 * (*it);
    }

    ASSERT_EQ(this->vec_.at(0), 0);
    for (size_t j = 1; j < this->vec_size - 1; ++j) {
        ASSERT_EQ(this->vec_.at(j), static_cast<typename slice_t::value_type>(2 * j));
        ASSERT_EQ(this->vec_.at(j), sl.at(j - 1));
    }
    ASSERT_EQ(this->vec_.at(this->vec_size - 1), static_cast<typename slice_t::value_type>(this->vec_size - 1));
}

/*!
 * Test the non-const version of at()
 */
TYPED_TEST_P(mutableSlice, at)
{
    typedef Slice<TypeParam> slice_t;
    slice_t sl = this->getTestSlice(2, 4);

    sl.at(0) = 6;
    sl.at(1) = 12;

    ASSERT_EQ(this->vec_.at(2), 6);
    ASSERT_EQ(this->vec_.at(3), 12);
    for (size_t j = 0; j < this->vec_size - 1; ++j) {
        if (j == 2 || j == 3) {
            continue;
        }
        ASSERT_EQ(this->vec_.at(j), static_cast<typename slice_t::value_type>(j));
    }
}

TEST(pointerSlice, failedConstructionFromNullpointer)
{
    ASSERT_THROW(Slice<long*>(NULL, 1, 2), std::invalid_argument);
}

/*!
 * Test the construction of an invalid slices from a container (so that a proper
 * range check can be conducted)
 */
TEST(containerSlice, failedConstructionFromContainer)
{
    std::vector<int> tmp(10);
    // slice end too large
    ASSERT_THROW(Slice<std::vector<int> >(tmp, 1, tmp.size() + 1), std::out_of_range);
}

/*!
 * Test all functions from the makeSlice* family.
 */
TEST(containerSlice, makeSlice)
{
    std::string str = "this is a sentence";

    Slice<std::string> is = makeSlice(str, 5, 7);
    ASSERT_TRUE(std::equal(is.begin(), is.end(), "is"));

    Slice<std::string> sl_this = makeSliceUntil(str, 4);
    ASSERT_TRUE(std::equal(sl_this.begin(), sl_this.end(), "this"));

    Slice<std::string> sl_sentence = makeSliceFrom(str, 10);
    ASSERT_TRUE(std::equal(sl_sentence.begin(), sl_sentence.end(), "sentence"));

    Slice<std::string> sl_full = makeSlice(str);
    ASSERT_TRUE(std::equal(sl_full.begin(), sl_full.end(), str.c_str()));
}

struct stringSlice : public ::testing::Test
{
    std::string sentence;

    virtual void SetUp()
    {
        sentence = "this is a sentence";
    }
};

TEST_F(stringSlice, at)
{
    const Slice<const std::string> is_a = makeSlice(static_cast<const std::string&>(this->sentence), 5, 10);

    ASSERT_EQ(is_a.at(0), 'i');
    ASSERT_EQ(is_a.at(4), ' ');
}

TEST_F(stringSlice, atFailure)
{
    const Slice<const std::string> is_a = makeSlice(static_cast<const std::string&>(this->sentence), 5, 10);
    ASSERT_THROW(is_a.at(5), std::out_of_range);
}

TEST_F(stringSlice, size)
{
    const Slice<const std::string> is_a = makeSlice(static_cast<const std::string&>(this->sentence), 5, 10);
    ASSERT_EQ(is_a.size(), static_cast<size_t>(5));
}

TEST_F(stringSlice, mutateString)
{
    Slice<std::string> is_a_mutable = makeSlice(this->sentence, 5, 10);

    for (Slice<std::string>::iterator it = is_a_mutable.begin(); it < is_a_mutable.end(); ++it) {
        *it = ' ';
    }

    ASSERT_STREQ(this->sentence.c_str(), "this      sentence");
}

template <typename T>
struct dataBufSlice : public ::testing::Test
{
    static byte data[4];  // = {0xde, 0xad, 0xbe, 0xef};
    DataBuf buf;

    virtual void SetUp()
    {
        buf = DataBuf(data, sizeof(data));
    }
};

template <typename T>
byte dataBufSlice<T>::data[4] = {0xde, 0xad, 0xbe, 0xef};

TYPED_TEST_CASE_P(dataBufSlice);

TYPED_TEST_P(dataBufSlice, successfulConstruction)
{
    // just check that makeSlice appears to work
    ASSERT_EQ(makeSlice(static_cast<TypeParam>(this->buf), 1, 3).size(), static_cast<size_t>(2));
}

TYPED_TEST_P(dataBufSlice, failedConstruction)
{
    // check that we get an exception when end is larger than LONG_MAX
    ASSERT_THROW(
        makeSlice(static_cast<TypeParam>(this->buf), 1, static_cast<size_t>(std::numeric_limits<long>::max()) + 1),
        std::invalid_argument);

    // check that we get an exception when end is larger than the DataBuf
    ASSERT_THROW(makeSlice(static_cast<TypeParam>(this->buf), 1, 5), std::out_of_range);
}

//
// GTest boilerplate to get the tests running for all the different types
//
REGISTER_TYPED_TEST_CASE_P(slice, atAccess, iteratorAccess, constructionFailsFromInvalidRange,
                           constructionFailsWithZeroLength, subSliceSuccessfulConstruction, subSliceFunctions,
                           subSliceFailedConstruction, subSliceConstructionOverflowResistance,
                           constMethodsPreserveConst);

typedef ::testing::Types<const std::vector<int>, std::vector<int>, int*, const int*> test_types_t;
INSTANTIATE_TYPED_TEST_CASE_P(slice, slice, test_types_t);

REGISTER_TYPED_TEST_CASE_P(mutableSlice, iterators, at);
typedef ::testing::Types<std::vector<int>, int*> mut_test_types_t;
INSTANTIATE_TYPED_TEST_CASE_P(slice, mutableSlice, mut_test_types_t);

REGISTER_TYPED_TEST_CASE_P(dataBufSlice, successfulConstruction, failedConstruction);
typedef ::testing::Types<DataBuf&, const DataBuf&> data_buf_types_t;
INSTANTIATE_TYPED_TEST_CASE_P(slice, dataBufSlice, data_buf_types_t);