summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/lleventdispatcher_test.cpp
blob: a1d7cf9eadfcbe7b59db6d17e531d2e5e2a2c35a (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
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
434
435
436
/**
 * @file   lleventdispatcher_test.cpp
 * @author Nat Goodspeed
 * @date   2011-01-20
 * @brief  Test for lleventdispatcher.
 * 
 * $LicenseInfo:firstyear=2011&license=viewerlgpl$
 * Copyright (c) 2011, Linden Research, Inc.
 * $/LicenseInfo$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "lleventdispatcher.h"
// STL headers
// std headers
// external library headers
// other Linden headers
#include "../test/lltut.h"
#include "llsd.h"
#include "llsdutil.h"
#include "stringize.h"
#include "tests/wrapllerrs.h"

// http://www.boost.org/doc/libs/1_45_0/libs/function_types/example/interpreter.hpp
// downloaded 2011-01-20 by NRG and adapted with example usage
// (C) Copyright Tobias Schwinger
//
// Use modification and distribution are subject to the boost Software License,
// Version 1.0. (See http://www.boost.org/LICENSE_1_0.txt).

//------------------------------------------------------------------------------
//
// This example implements a simple batch-style dispatcher that is capable of
// calling functions previously registered with it. The parameter types of the
// functions are used to control the parsing of the input.
//
// Implementation description
// ==========================
//
// When a function is registered, an 'invoker' template is instantiated with
// the function's type. The 'invoker' fetches a value from the 'arg_source'
// for each parameter of the function into a tuple and finally invokes the the
// function with these values as arguments. The invoker's entrypoint, which
// is a function of the callable builtin that describes the function to call and
// a reference to the 'arg_source', is partially bound to the registered
// function and put into a map so it can be found by name during parsing.

#include <map>
#include <string>
#include <stdexcept>

#include <boost/bind.hpp>
#include <boost/function.hpp>
#include <boost/range.hpp>

#include <boost/lambda/lambda.hpp>

#include <iostream>

using boost::lambda::constant;
using boost::lambda::constant_ref;
using boost::lambda::var;

/*****************************************************************************
*   Output control
*****************************************************************************/
#ifdef DEBUG_ON
using std::cout;
#else
static std::ostringstream cout;
#endif

/*****************************************************************************
*   Example data, functions, classes
*****************************************************************************/
// sensing globals
static std::string gs;
static float gf;
static int gi;
static LLSD gl;

void clear()
{
    gs.clear();
    gf = 0;
    gi = 0;
    gl = LLSD();
}

void abc(const std::string& message)
{
    cout << "abc('" << message << "')\n";
    gs = message;
}

void def(float value, std::string desc)
{
    cout << "def(" << value << ", '" << desc << "')\n";
    gf = value;
    gs = desc;
}

void ghi(const std::string& foo, int bar)
{
    cout << "ghi('" << foo << "', " << bar << ")\n";
    gs = foo;
    gi = bar;
}

void jkl(const char* message)
{
    cout << "jkl('" << message << "')\n";
    gs = message;
}

void somefunc(const LLSD& value)
{
    cout << "somefunc(" << value << ")\n";
    gl = value;
}

class Dummy
{
public:
    Dummy(): _id("Dummy") {}

    void mno(const std::string& message)
    {
        cout << _id << "::mno('" << message << "')\n";
        s = message;
    }

    void pqr(float value, std::string desc)
    {
        cout << _id << "::pqr(" << value << ", '" << desc << "')\n";
        f = value;
        s = desc;
    }

    void stu(const std::string& foo, int bar)
    {
        cout << _id << "::stu('" << foo << "', " << bar << ")\n";
        s = foo;
        i = bar;
    }

    void vwx(const char* message)
    {
        cout << _id << "::vwx('" << message << "')\n";
        s = message;
    }

    static void yz1(const std::string& message)
    {
        cout << "Dummy::yz1('" << message << "')\n";
        // can't access sensing members...
        gs = message;
    }

    // sensing members
    std::string s;
    float f;
    int i;

private:
    std::string _id;
};

/*****************************************************************************
*   TUT
*****************************************************************************/
namespace tut
{
    struct lleventdispatcher_data
    {
        WrapLL_ERRS redirect;
        LLEventDispatcher work;
        Dummy dummy;

        lleventdispatcher_data():
            work("test dispatcher", "op")
        {
            // This object is reconstructed for every test<n> method. But
            // clear global variables every time too.
            ::clear();

            work.add("abc", "abc", abc, LLSDArray("message"));
            work.add("def", "def", def);
            work.add("ghi", "ghi", ghi);
            work.add("jkl", "jkl", jkl);
            work.add("yz1", "yz1", &Dummy::yz1);
            work.add("mno", "mno", &Dummy::mno, var(dummy),
                     LLSDArray("message"), LLSDArray("default message"));
            work.add("mnoptr", "mno", &Dummy::mno, constant(&dummy));
            work.add("pqr", "pqr", &Dummy::pqr, var(dummy),
                     LLSDArray("value")("desc"));
            work.add("stu", "stu", &Dummy::stu, var(dummy),
                     LLSDArray("foo")("bar"), LLSDArray(-1));
            work.add("vwx", "vwx", &Dummy::vwx, var(dummy),
                     LLSDArray("message"));
        }

        void ensure_has(const std::string& outer, const std::string& inner)
        {
            ensure(STRINGIZE("'" << outer << "' does not contain '" << inner << "'").c_str(),
                   outer.find(inner) != std::string::npos);
        }

        void call_exc(const std::string& func, const LLSD& args, const std::string& exc_frag)
        {
            std::string threw;
            try
            {
                work(func, args);
            }
            catch (const std::runtime_error& e)
            {
                cout << "*** " << e.what() << '\n';
                threw = e.what();
            }
            ensure_has(threw, exc_frag);
        }
    };
    typedef test_group<lleventdispatcher_data> lleventdispatcher_group;
    typedef lleventdispatcher_group::object object;
    lleventdispatcher_group lleventdispatchergrp("lleventdispatcher");

    template<> template<>
    void object::test<1>()
    {
        LLSD hello("Hello test!");
//        cout << std::string(hello) << "\n";
        clear();
        jkl(LLSDParam<const char*>(hello));
        ensure_equals(gs, hello.asString());
    }

    template<> template<>
    void object::test<2>()
    {
        somefunc("abc");
        ensure_equals(gl.asString(), "abc");

        somefunc(17);
        ensure_equals(gl.asInteger(), 17);

        somefunc(3.14);
        // 7 bits is 128, just enough to express two decimal places.
        ensure_approximately_equals(gl.asReal(), 3.14, 7);

        somefunc(LLSD().with(0, "abc").with(1, 17).with(2, 3.14));
        ensure(gl.isArray());
        ensure_equals(gl.size(), 4); // !!! bug in LLSD::with(Integer, const LLSD&) !!!
        ensure_equals(gl[1].asInteger(), 17);

        somefunc(LLSD().with("alpha", "abc").with("random", 17).with("pi", 3.14));
        ensure(gl.isMap());
        ensure_equals(gl.size(), 3);
        ensure_equals(gl["random"].asInteger(), 17);

        somefunc(LLSDArray("abc")(17)(3.14));
        ensure(gl.isArray());
        ensure_equals(gl.size(), 3);
        ensure_equals(gl[0].asString(), "abc");

        somefunc(LLSDMap("alpha", "abc")("random", 17)("pi", 3.14));
        ensure(gl.isMap());
        ensure_equals(gl.size(), 3);
        ensure_equals(gl["alpha"].asString(), "abc");
    }

    template<> template<>
    void object::test<3>()
    {
        call_exc("gofish", LLSDArray(1), "not found");
    }

    template<> template<>
    void object::test<4>()
    {
        call_exc("abc", LLSD(), "missing required");
    }

    template<> template<>
    void object::test<5>()
    {
        work("abc", LLSDMap("message", "something"));
        ensure_equals(gs, "something");
    }

    template<> template<>
    void object::test<6>()
    {
        work("abc", LLSDMap("message", "something")("plus", "more"));
        ensure_equals(gs, "something");
    }

    template<> template<>
    void object::test<7>()
    {
        call_exc("def", LLSDMap("value", 20)("desc", "questions"), "needs an args array");
    }

    template<> template<>
    void object::test<8>()
    {
        work("def", LLSDArray(20)("questions"));
        ensure_equals(gf, 20);
        ensure_equals(gs, "questions");
    }

    template<> template<>
    void object::test<9>()
    {
        work("def", LLSDArray(3.14)("pies"));
        ensure_approximately_equals(gf, 3.14, 7);
        ensure_equals(gs, "pies");
    }

    template<> template<>
    void object::test<10>()
    {
        work("ghi", LLSDArray("answer")(17));
        ensure_equals(gs, "answer");
        ensure_equals(gi, 17);
    }

    template<> template<>
    void object::test<11>()
    {
        work("ghi", LLSDArray("answer")(3.14));
        ensure_equals(gs, "answer");
        ensure_equals(gi, 3);
    }

    template<> template<>
    void object::test<12>()
    {
        work("jkl", LLSDArray("sample message"));
        ensure_equals(gs, "sample message");
    }

    template<> template<>
    void object::test<13>()
    {
        work("yz1", LLSDArray("w00t"));
        ensure_equals(gs, "w00t");
    }

    template<> template<>
    void object::test<14>()
    {
        std::string msg("nonstatic member function");
        work("mno", LLSDMap("message", msg));
        ensure_equals(dummy.s, msg);
    }

    template<> template<>
    void object::test<15>()
    {
        std::string msg("nonstatic member function reached by ptr");
        work("mnoptr", LLSDArray(msg));
        ensure_equals(dummy.s, msg);
    }

    template<> template<>
    void object::test<16>()
    {
        work("mno", LLSD());
        ensure_equals(dummy.s, "default message");
    }

    template<> template<>
    void object::test<17>()
    {
        work("pqr", LLSDMap("value", 3.14)("desc", "pies"));
        ensure_approximately_equals(dummy.f, 3.14, 7);
        ensure_equals(dummy.s, "pies");
    }

    template<> template<>
    void object::test<18>()
    {
        call_exc("pqr", LLSD(), "missing required");
    }

    template<> template<>
    void object::test<19>()
    {
        call_exc("pqr", LLSDMap("value", 3.14), "missing required");
    }

    template<> template<>
    void object::test<20>()
    {
        call_exc("pqr", LLSDMap("desc", "pies"), "missing required");
    }

    template<> template<>
    void object::test<21>()
    {
        work("stu", LLSDMap("bar", 3.14)("foo", "pies"));
        ensure_equals(dummy.s, "pies");
        ensure_equals(dummy.i, 3);
    }

    template<> template<>
    void object::test<22>()
    {
        call_exc("stu", LLSD(), "missing required");
    }

    template<> template<>
    void object::test<23>()
    {
        call_exc("stu", LLSDMap("bar", 3.14), "missing required");
    }

    template<> template<>
    void object::test<24>()
    {
        work("stu", LLSDMap("foo", "pies"));
        ensure_equals(dummy.s, "pies");
        ensure_equals(dummy.i, -1);
    }

    template<> template<>
    void object::test<25>()
    {
        std::string msg("nonstatic method(const char*)");
        work("vwx", LLSDMap("message", msg));
        ensure_equals(dummy.s, msg);
    }
} // namespace tut