summaryrefslogtreecommitdiffstats
path: root/vespalib/src/tests/memory/memory_test.cpp
blob: c9b0634f3bcc6e16d85b31b559001dbdf6fc8f0e (plain) (blame)
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
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/fastos/fastos.h>
#include <vespa/log/log.h>
LOG_SETUP("memory_test");
#include <vespa/vespalib/testkit/testapp.h>
#include <vespa/vespalib/util/memory.h>

using namespace vespalib;

class B
{
public:
    virtual ~B() { }
    virtual B * clone() const { return new B(*this); }
};

class A : public B
{
public:
    virtual ~A() { }
    virtual A * clone() const { return new A(*this); }
};

class Test : public TestApp
{
public:
    int Main();
};

B* fn(auto_arr<B> param) { return param.get(); }
auto_arr<B> fn(B *param) { auto_arr<B> bb(param); return bb; }

int
Test::Main()
{
    TEST_INIT("memory_test");
    {
        B* p = new B[5];
        auto_arr<B> apb(p);
        EXPECT_TRUE(apb.get() == p);
        EXPECT_TRUE(fn(apb) == p);
        EXPECT_TRUE(apb.get() == nullptr);
    }
    {
        A* p = new A[5];
        auto_arr<A> apa(p);
        EXPECT_TRUE(apa.get() == p);
        auto_arr<A> apb = apa;
        EXPECT_TRUE(apa.get() == nullptr);
        EXPECT_TRUE(apb.get() == p);
        A& ref = apb[2];
        EXPECT_TRUE(&ref == (p+2));
    }
    {
        B* p = new B[5];
        auto_arr<B> apb = fn(p);
        EXPECT_TRUE(apb.get() == p);
    }
    {
        MallocAutoPtr a(malloc(30));
        EXPECT_TRUE(a.get() != nullptr);
        void * tmp = a.get();
        MallocAutoPtr b(a);
        EXPECT_TRUE(tmp == b.get());
        EXPECT_TRUE(a.get() == nullptr);
        MallocAutoPtr c;
        c = b;
        EXPECT_TRUE(b.get() == nullptr);
        EXPECT_TRUE(tmp == c.get());
        MallocAutoPtr d(malloc(30));
        EXPECT_TRUE(d.get() != nullptr);
        tmp = c.get();
        d = c;
        EXPECT_TRUE(tmp == d.get());
        EXPECT_TRUE(c.get() == nullptr);
    }
    {

        MallocPtr a(100);
        EXPECT_TRUE(a.size() == 100);
        EXPECT_TRUE(a.get() != nullptr);
        memset(a.get(), 17, a.size());
        MallocPtr b(a);
        EXPECT_TRUE(a.size() == 100);
        EXPECT_TRUE(a.get() != nullptr);
        EXPECT_TRUE(b.size() == 100);
        EXPECT_TRUE(b.get() != nullptr);
        EXPECT_TRUE(a.get() != b.get());
        EXPECT_TRUE(memcmp(a.get(), b.get(), a.size()) == 0);
        void * tmp = a.get();
        a = b;
        EXPECT_TRUE(a.size() == 100);
        EXPECT_TRUE(a.get() != nullptr);
        EXPECT_TRUE(a.get() != tmp);
        EXPECT_TRUE(memcmp(a.get(), b.get(), a.size()) == 0);
        MallocPtr d = std::move(b);
        EXPECT_TRUE(d.size() == 100);
        EXPECT_TRUE(d.get() != nullptr);
        EXPECT_TRUE(b.size() == 0);
        EXPECT_TRUE(b.get() == nullptr);
        MallocPtr c;
        c.realloc(89);
        EXPECT_EQUAL(c.size(), 89u);
        c.realloc(0);
        EXPECT_EQUAL(c.size(), 0u);
        EXPECT_TRUE(c == nullptr);

    }
    {
        CloneablePtr<B> a(new A());
        EXPECT_TRUE(a.get() != nullptr);
        CloneablePtr<B> b(a);
        EXPECT_TRUE(a.get() != nullptr);
        EXPECT_TRUE(b.get() != nullptr);
        EXPECT_TRUE(b.get() != a.get());
        CloneablePtr<B> c;
        c = a;
        EXPECT_TRUE(a.get() != nullptr);
        EXPECT_TRUE(c.get() != nullptr);
        EXPECT_TRUE(c.get() != a.get());

        b = CloneablePtr<B>(new B());
        EXPECT_TRUE(dynamic_cast<B*>(b.get()) != nullptr);
        EXPECT_TRUE(dynamic_cast<A*>(b.get()) == nullptr);
        EXPECT_TRUE(dynamic_cast<B*>(a.get()) != nullptr);
        EXPECT_TRUE(dynamic_cast<A*>(a.get()) != nullptr);
        EXPECT_TRUE(dynamic_cast<B*>(c.get()) != nullptr);
        EXPECT_TRUE(dynamic_cast<A*>(c.get()) != nullptr);
        c = b;
        EXPECT_TRUE(dynamic_cast<B*>(c.get()) != nullptr);
        EXPECT_TRUE(dynamic_cast<A*>(c.get()) == nullptr);
    }
    {
        CloneablePtr<B> null;
        if (null) {
            EXPECT_TRUE(false);
        } else {
            EXPECT_FALSE(bool(null));
            EXPECT_TRUE(!null);
        }
    }
    {
        CloneablePtr<B> notNull(new A());
        if (notNull) {
            EXPECT_TRUE(bool(notNull));
            EXPECT_FALSE(!notNull);
        } else {
            EXPECT_TRUE(false);
        }
    }
    {
        int a[3];
        int b[4] = {0,1,2,3};
        int c[4] = {0,1,2};
        int d[] = {0,1,2,3,4};
        EXPECT_EQUAL(VESPA_NELEMS(a), 3u);
        EXPECT_EQUAL(VESPA_NELEMS(b), 4u);
        EXPECT_EQUAL(VESPA_NELEMS(c), 4u);
        EXPECT_EQUAL(VESPA_NELEMS(d), 5u);
    }
    TEST_DONE();
}

TEST_APPHOOK(Test)