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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/vespalib/util/array.h>
#include <vespa/vespalib/util/allocinarray.h>
#include <vespa/vespalib/stllike/string.h>
#include <vespa/vespalib/testkit/testapp.h>
#include <deque>
using namespace vespalib;
class Test : public TestApp
{
public:
int Main() override;
private:
template <typename T, typename V>
void testAllocInArray();
};
int
Test::Main()
{
TEST_INIT("allocinarray_test");
testAllocInArray<int64_t, vespalib::Array<int64_t> >();
testAllocInArray<int64_t, vespalib::Array<int64_t> >();
testAllocInArray<int64_t, std::vector<int64_t> >();
testAllocInArray<int64_t, std::deque<int64_t> >();
TEST_DONE();
}
template <typename T, typename V>
void Test::testAllocInArray()
{
typedef AllocInArray<T, V> AA;
AA alloc;
EXPECT_EQUAL(0ul, alloc.size());
EXPECT_EQUAL(0ul, alloc.alloc(1));
EXPECT_EQUAL(1ul, alloc.size());
EXPECT_EQUAL(1, alloc[0]);
alloc.free(0);
EXPECT_EQUAL(0ul, alloc.size());
alloc.free(0);
EXPECT_EQUAL(0ul, alloc.size());
alloc.free(1);
EXPECT_EQUAL(0ul, alloc.size());
alloc.alloc(7);
alloc.alloc(17);
alloc.alloc(-17);
EXPECT_EQUAL(3ul, alloc.size());
EXPECT_EQUAL(7, alloc[0]);
EXPECT_EQUAL(17, alloc[1]);
EXPECT_EQUAL(-17, alloc[2]);
alloc[1] = 99;
EXPECT_EQUAL(99, alloc[1]);
alloc.free(1);
EXPECT_EQUAL(2ul, alloc.size());
EXPECT_EQUAL(7, alloc[0]);
EXPECT_EQUAL(-17, alloc[2]);
EXPECT_EQUAL(1ul, alloc.alloc(103));
EXPECT_EQUAL(3ul, alloc.size());
EXPECT_EQUAL(7, alloc[0]);
EXPECT_EQUAL(103, alloc[1]);
EXPECT_EQUAL(-17, alloc[2]);
alloc.clear();
EXPECT_EQUAL(0ul, alloc.size());
}
TEST_APPHOOK(Test)
|