summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/lllazy_test.cpp
blob: db581d650f40e59bbf8bc9be00d6e13ebb347458 (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
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
/**
 * @file   lllazy_test.cpp
 * @author Nat Goodspeed
 * @date   2009-01-28
 * @brief  Tests of lllazy.h.
 * 
 * $LicenseInfo:firstyear=2009&license=viewergpl$
 * Copyright (c) 2009, Linden Research, Inc.
 * $/LicenseInfo$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "lllazy.h"
// STL headers
#include <iostream>
// std headers
// external library headers
#include <boost/lambda/construct.hpp>
#include <boost/lambda/bind.hpp>
// other Linden headers
#include "../test/lltut.h"

namespace bll = boost::lambda;

/*****************************************************************************
*   Test classes
*****************************************************************************/

// Let's say that because of its many external dependencies, YuckyFoo is very
// hard to instantiate in a test harness.
class YuckyFoo
{
public:
    virtual ~YuckyFoo() {}
    virtual std::string whoami() const { return "YuckyFoo"; }
};

// Let's further suppose that YuckyBar is another hard-to-instantiate class.
class YuckyBar
{
public:
    YuckyBar(const std::string& which):
        mWhich(which)
    {}
    virtual ~YuckyBar() {}

    virtual std::string identity() const { return std::string("YuckyBar(") + mWhich + ")"; }

private:
    const std::string mWhich;
};

// Pretend that this class would be tough to test because, up until we started
// trying to test it, it contained instances of both YuckyFoo and YuckyBar.
// Now we've refactored so it contains LLLazy<YuckyFoo> and LLLazy<YuckyBar>.
// More than that, it contains them by virtue of deriving from
// LLLazyBase<YuckyFoo> and LLLazyBase<YuckyBar>.
// We postulate two different LLLazyBases because, with only one, you need not
// specify *which* get()/set() method you're talking about. That's a simpler
// case.
class NeedsTesting: public LLLazyBase<YuckyFoo>, public LLLazyBase<YuckyBar>
{
public:
    NeedsTesting():
        // mYuckyBar("RealYuckyBar")
        LLLazyBase<YuckyBar>(bll::bind(bll::new_ptr<YuckyBar>(), "RealYuckyBar"))
    {}
    virtual ~NeedsTesting() {}

    virtual std::string describe() const
    {
        return std::string("NeedsTesting(") + getLazy<YuckyFoo>(this).whoami() + ", " +
            getLazy<YuckyBar>(this).identity() + ")";
    }

private:
    // These instance members were moved to LLLazyBases:
    // YuckyFoo mYuckyFoo;
    // YuckyBar mYuckyBar;
};

// Fake up a test YuckyFoo class
class TestFoo: public YuckyFoo
{
public:
    virtual std::string whoami() const { return "TestFoo"; }
};

// and a test YuckyBar
class TestBar: public YuckyBar
{
public:
    TestBar(const std::string& which): YuckyBar(which) {}
    virtual std::string identity() const
    {
        return std::string("TestBar(") + YuckyBar::identity() + ")";
    }
};

// So here's a test subclass of NeedsTesting that uses TestFoo and TestBar
// instead of YuckyFoo and YuckyBar.
class TestNeedsTesting: public NeedsTesting
{
public:
    TestNeedsTesting()
    {
        // Exercise setLazy(T*)
        setLazy<YuckyFoo>(this, new TestFoo());
        // Exercise setLazy(Factory)
        setLazy<YuckyBar>(this, bll::bind(bll::new_ptr<TestBar>(), "TestYuckyBar"));
    }

    virtual std::string describe() const
    {
        return std::string("TestNeedsTesting(") + NeedsTesting::describe() + ")";
    }

    void toolate()
    {
        setLazy<YuckyFoo>(this, new TestFoo());
    }
};

// This class tests having an explicit LLLazy<T> instance as a named member,
// rather than deriving from LLLazyBase<T>.
class LazyMember
{
public:
    YuckyFoo& getYuckyFoo() { return *mYuckyFoo; }
    std::string whoisit() const { return mYuckyFoo->whoami(); }

protected:
    LLLazy<YuckyFoo> mYuckyFoo;
};

// This is a test subclass of the above, dynamically replacing the
// LLLazy<YuckyFoo> member.
class TestLazyMember: public LazyMember
{
public:
    // use factory setter
    TestLazyMember()
    {
        mYuckyFoo.set(bll::new_ptr<TestFoo>());
    }

    // use instance setter
    TestLazyMember(YuckyFoo* instance)
    {
        mYuckyFoo.set(instance);
    }
};

/*****************************************************************************
*   TUT
*****************************************************************************/
namespace tut
{
    struct lllazy_data
    {
    };
    typedef test_group<lllazy_data> lllazy_group;
    typedef lllazy_group::object lllazy_object;
    lllazy_group lllazygrp("lllazy");

    template<> template<>
    void lllazy_object::test<1>()
    {
        // Instantiate an official one, just because we can
        NeedsTesting nt;
        // and a test one
        TestNeedsTesting tnt;
//      std::cout << nt.describe() << '\n';
        ensure_equals(nt.describe(), "NeedsTesting(YuckyFoo, YuckyBar(RealYuckyBar))");
//      std::cout << tnt.describe() << '\n';
        ensure_equals(tnt.describe(),
                      "TestNeedsTesting(NeedsTesting(TestFoo, TestBar(YuckyBar(TestYuckyBar))))");
    }

    template<> template<>
    void lllazy_object::test<2>()
    {
        TestNeedsTesting tnt;
        std::string threw;
        try
        {
            tnt.toolate();
        }
        catch (const LLLazyCommon::InstanceChange& e)
        {
            threw = e.what();
        }
        ensure_contains("InstanceChange exception", threw, "replace LLLazy instance");
    }

    template<> template<>
    void lllazy_object::test<3>()
    {
        {
            LazyMember lm;
            // operator*() on-demand instantiation
            ensure_equals(lm.getYuckyFoo().whoami(), "YuckyFoo");
        }
        {
            LazyMember lm;
            // operator->() on-demand instantiation
            ensure_equals(lm.whoisit(), "YuckyFoo");
        }
    }

    template<> template<>
    void lllazy_object::test<4>()
    {
        {
            // factory setter
            TestLazyMember tlm;
            ensure_equals(tlm.whoisit(), "TestFoo");
        }
        {
            // instance setter
            TestLazyMember tlm(new TestFoo());
            ensure_equals(tlm.whoisit(), "TestFoo");
        }
    }
} // namespace tut