File: attrTableT.cc

package info (click to toggle)
libdap 3.20.11-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 24,568 kB
  • sloc: cpp: 50,809; sh: 41,536; xml: 23,511; ansic: 20,030; yacc: 2,508; exp: 1,544; makefile: 990; lex: 309; perl: 52; fortran: 8
file content (291 lines) | stat: -rw-r--r-- 9,011 bytes parent folder | download
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
#include <cppunit/TestFixture.h>
#include <cppunit/TestAssert.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/ui/text/TestRunner.h>
#include <cppunit/extensions/HelperMacros.h>
#include <cppunit/CompilerOutputter.h>

#include <iostream>
#include <vector>
#include "AttrTable.h"

#include "run_tests_cppunit.h"
#include "run_tests_cppunit.h"
#include "test_config.h"


using std::cerr;
using std::endl;
using std::vector;

using namespace CppUnit;
using namespace libdap;

int test_variable_sleep_interval = 0; // Used in Test* classes for testing
// timeouts.

class attrTableT: public CppUnit::TestFixture {

CPPUNIT_TEST_SUITE (attrTableT);
    CPPUNIT_TEST(attrTableT_test);CPPUNIT_TEST_SUITE_END( )
    ;

private:
    /* TEST PRIVATE DATA */

public:
    void setUp()
    {
    }

    void tearDown()
    {
    }

    void attrTableT_test()
    {
        AttrTable at;

        unsigned int at_size = at.get_size();
        CPPUNIT_ASSERT(at_size == 0);

        string at_name = at.get_name();
        CPPUNIT_ASSERT(at_name == "");

        at.set_name("My Attributes");
        at_name = at.get_name();
        CPPUNIT_ASSERT(at_name == "My Attributes");

        AttrTable *container = at.find_container("dummy_container");
        CPPUNIT_ASSERT(!container);

        AttrTable *dummy_at = 0;

        AttrTable::Attr_iter iter;
        at.find("dummy_attr", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter == at.attr_end());

        string attr_name = "attr1";
        string attr_type = "string";
        string attr_value = "attr1Value1";
        at.append_attr(attr_name, attr_type, attr_value);

        attr_name = "attr2";
        attr_type = "string";
        attr_value = "attr2Value1";
        at.append_attr(attr_name, attr_type, attr_value);

        attr_name = "attr3";
        attr_type = "string";
        attr_value = "attr3Value1";
        at.append_attr(attr_name, attr_type, attr_value);

        at.append_attr("attr4", "string", "attr4Value1");

        at_size = at.get_size();
        CPPUNIT_ASSERT(at_size == 4);

        //at.print( stdout ) ;
        iter = at.attr_end();
        at.find("attr3", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter != at.attr_end());

        iter = at.attr_end();
        at.find("dummy_attr", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter == at.attr_end());

        attr_type = at.get_type("attr3");
        CPPUNIT_ASSERT(attr_type == "String");

        AttrType attr_type_enum = at.get_attr_type("attr3");
        CPPUNIT_ASSERT(attr_type_enum == Attr_string);

        unsigned int num_attrs = at.get_attr_num("attr3");
        CPPUNIT_ASSERT(num_attrs == 1);

        attr_value = at.get_attr("attr3");
        CPPUNIT_ASSERT(attr_value == "attr3Value1");

        at.append_attr("attr3", "string", "attr3Value2");
        at.append_attr("attr3", "string", "attr3Value3");
        at.append_attr("attr3", "string", "attr3Value4");

        attr_value = at.get_attr("attr3");
        CPPUNIT_ASSERT(attr_value == "attr3Value1");

        vector<string> sb;
        sb.push_back("attr3Value1");
        sb.push_back("attr3Value2");
        sb.push_back("attr3Value3");
        sb.push_back("attr3Value4");

        typedef vector<string>::const_iterator str_citer;
        typedef vector<string>::iterator str_iter;

        vector<string> *values = at.get_attr_vector("attr3");
        CPPUNIT_ASSERT(values);
        if (values) {
            str_citer vi = values->begin();
            str_citer sbi = sb.begin();
            for (; vi != values->end() && sbi != sb.end(); vi++, sbi++) {
                CPPUNIT_ASSERT((*vi) == (*sbi));
            }
            CPPUNIT_ASSERT(vi == values->end() && sbi == sb.end());
            if (vi == values->end() && sbi != sb.end()) {
                CPPUNIT_FAIL("not enough values");
            }
            else if (vi != values->end() && sbi == sb.end()) {
                CPPUNIT_FAIL("too many values");
            }
        }

        vector<string> attrs;
        attrs.push_back("attr1");
        attrs.push_back("attr2");
        attrs.push_back("attr3");
        attrs.push_back("attr4");

        str_citer ai = attrs.begin();
        AttrTable::Attr_iter i = at.attr_begin();
        // ai = attrs.begin() ;
        for (; i != at.attr_end() && ai != attrs.end(); i++, ai++) {
            CPPUNIT_ASSERT((*i)->name == (*ai));
        }
        CPPUNIT_ASSERT(i == at.attr_end() && ai == attrs.end());
        if (i != at.attr_end() && ai == attrs.end()) {
            CPPUNIT_FAIL("too many attributes");
        }
        else if (i == at.attr_end() && ai != attrs.end()) {
            CPPUNIT_FAIL("not enough attributes");
        }

        iter = at.attr_end();
        at.find("attr3", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter != at.attr_end());

        attr_name = at.get_name(iter);
        CPPUNIT_ASSERT(attr_name == "attr3");

        bool isit = at.is_container(iter);
        CPPUNIT_ASSERT(isit == false);

        dummy_at = at.get_attr_table(iter);
        CPPUNIT_ASSERT(!dummy_at);

        attr_type = at.get_type(iter);
        CPPUNIT_ASSERT(attr_type == "String");

        attr_type_enum = at.get_attr_type(iter);
        CPPUNIT_ASSERT(attr_type_enum == Attr_string);

        attr_value = at.get_attr(iter);
        CPPUNIT_ASSERT(attr_value == "attr3Value1");

        attr_value = at.get_attr(iter, 1);
        CPPUNIT_ASSERT(attr_value == "attr3Value2");

        values = at.get_attr_vector(iter);
        CPPUNIT_ASSERT(values);
        if (values) {
            str_citer vi = values->begin();
            str_citer sbi = sb.begin();
            for (; vi != values->end() && sbi != sb.end(); vi++, sbi++) {
                CPPUNIT_ASSERT((*vi) == (*sbi));
            }
            CPPUNIT_ASSERT(vi == values->end() && sbi == sb.end());
            if (vi == values->end() && sbi != sb.end()) {
                CPPUNIT_FAIL("not enough values");
            }
            else if (vi != values->end() && sbi == sb.end()) {
                CPPUNIT_FAIL("too many values");
            }
        }

        {
            str_iter sbi = sb.begin();
            sbi++;
            sb.erase(sbi);
        }

        at.del_attr("attr3", 1);
        values = at.get_attr_vector(iter);
        CPPUNIT_ASSERT(values);
        if (values) {
            str_citer vi = values->begin();
            str_citer sbi = sb.begin();
            for (; vi != values->end() && sbi != sb.end(); vi++, sbi++) {
                CPPUNIT_ASSERT((*vi) == (*sbi));
            }
            CPPUNIT_ASSERT(vi == values->end() && sbi == sb.end());
            if (vi == values->end() && sbi != sb.end()) {
                CPPUNIT_FAIL("not enough values");
            }
            else if (vi != values->end() && sbi == sb.end()) {
                CPPUNIT_FAIL("too many values");
            }
        }

        at.del_attr("attr3");
        container = 0;
        try {
            container = at.append_container("attr2");
            CPPUNIT_FAIL("added container named attr2 successfully - already exists");
        }
        catch (Error &e) {
        }
        CPPUNIT_ASSERT(!container);

        try {
            container = at.append_container("attr5");
        }
        catch (Error &e) {
            CPPUNIT_FAIL("failed to add new container attr5");
        }
        CPPUNIT_ASSERT(container);
        if (container) {
            CPPUNIT_ASSERT(container->get_name() == "attr5");
        }

        container = at.find_container("attr5");
        CPPUNIT_ASSERT(container);
        if (container) {
            string container_name = container->get_name();
            CPPUNIT_ASSERT(container_name == "attr5");
        }

        iter = at.attr_end();
        at.find("attr5", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter != at.attr_end());
        attr_name = at.get_name(iter);
        CPPUNIT_ASSERT(attr_name == "attr5");
        isit = at.is_container(iter);
        CPPUNIT_ASSERT(isit == true);
        container = at.get_attr_table(iter);
        CPPUNIT_ASSERT(container);
        attr_type = at.get_type(iter);
        CPPUNIT_ASSERT(attr_type == "Container");
        attr_type_enum = at.get_attr_type(iter);
        CPPUNIT_ASSERT(attr_type_enum == Attr_container);

        /* FIX: does append attr return anything? */
        container->append_attr("attr5-1", "string", "attr5.1Value1");
        container->append_attr("attr5-2", "string", "attr5.2Value1");
        container->append_attr("attr5-3", "string", "attr5.3Value1");
        container->append_attr("attr5-4", "string", "attr5.4Value1");
        iter = at.attr_end();
        at.find("attr5.attr5-3", &dummy_at, &iter);
        CPPUNIT_ASSERT(iter != at.attr_end());
        CPPUNIT_ASSERT(container == dummy_at);

        //at.print( stdout ) ;
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(attrTableT);

/* NOTHING NEEDS TO BE CHANGED BELOW HERE */

int main(int argc, char *argv[])
{
    return run_tests<attrTableT>(argc, argv) ? 0: 1;
}