summaryrefslogtreecommitdiff
path: root/indra/llcommon/tests/llheteromap_test.cpp
blob: 686bffb878b44c8de1da45fb45d80e606cbb1127 (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
/**
 * @file   llheteromap_test.cpp
 * @author Nat Goodspeed
 * @date   2016-10-12
 * @brief  Test for llheteromap.
 * 
 * $LicenseInfo:firstyear=2016&license=viewerlgpl$
 * Copyright (c) 2016, Linden Research, Inc.
 * $/LicenseInfo$
 */

// Precompiled header
#include "linden_common.h"
// associated header
#include "llheteromap.h"
// STL headers
#include <set>
// std headers
// external library headers

// (pacify clang)
std::ostream& operator<<(std::ostream& out, const std::set<std::string>& strset);
// other Linden headers
#include "../test/lltut.h"

static std::string clog;
static std::set<std::string> dlog;

// want to be able to use ensure_equals() on a set<string>
std::ostream& operator<<(std::ostream& out, const std::set<std::string>& strset)
{
    out << '{';
    const char* delim = "";
    for (std::set<std::string>::const_iterator si(strset.begin()), se(strset.end());
         si != se; ++si)
    {
        out << delim << '"' << *si << '"';
        delim = ", ";
    }
    out << '}';
    return out;
}

// unrelated test classes
struct Chalk
{
    int dummy;
    std::string name;

    Chalk():
        dummy(0)
    {
        clog.append("a");
    }

    ~Chalk()
    {
        dlog.insert("a");
    }

private:
    Chalk(const Chalk&);            // no implementation
};

struct Cheese
{
    std::string name;

    Cheese()
    {
        clog.append("e");
    }

    ~Cheese()
    {
        dlog.insert("e");
    }

private:
    Cheese(const Cheese&);          // no implementation
};

struct Chowdah
{
    char displace[17];
    std::string name;

    Chowdah()
    {
        displace[0] = '\0';
        clog.append("o");
    }

    ~Chowdah()
    {
        dlog.insert("o");
    }

private:
    Chowdah(const Chowdah&);        // no implementation
};

/*****************************************************************************
*   TUT
*****************************************************************************/
namespace tut
{
    struct llheteromap_data
    {
        llheteromap_data()
        {
            clog.erase();
            dlog.clear();
        }
    };
    typedef test_group<llheteromap_data> llheteromap_group;
    typedef llheteromap_group::object object;
    llheteromap_group llheteromapgrp("llheteromap");

    template<> template<>
    void object::test<1>()
    {
        set_test_name("create, get, delete");

        {
            LLHeteroMap map;

            {
                // create each instance
                Chalk& chalk = map.obtain<Chalk>();
                chalk.name = "Chalk";

                Cheese& cheese = map.obtain<Cheese>();
                cheese.name = "Cheese";

                Chowdah& chowdah = map.obtain<Chowdah>();
                chowdah.name = "Chowdah";
            } // refs go out of scope

            {
                // verify each instance
                Chalk& chalk = map.obtain<Chalk>();
                ensure_equals(chalk.name, "Chalk");

                Cheese& cheese = map.obtain<Cheese>();
                ensure_equals(cheese.name, "Cheese");

                Chowdah& chowdah = map.obtain<Chowdah>();
                ensure_equals(chowdah.name, "Chowdah");
            }
        } // destroy map

        // Chalk, Cheese and Chowdah should have been created in specific order
        ensure_equals(clog, "aeo");

        // We don't care what order they're destroyed in, as long as each is
        // appropriately destroyed.
        std::set<std::string> dtorset;
        for (const char* cp = "aeo"; *cp; ++cp)
            dtorset.insert(std::string(1, *cp));
        ensure_equals(dlog, dtorset);
    }
} // namespace tut