File: D4AttributesTest.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 (481 lines) | stat: -rw-r--r-- 16,751 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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
// -*- mode: c++; c-basic-offset:4 -*-

// This file is part of libdap, A C++ implementation of the OPeNDAP Data
// Access Protocol.

// Copyright (c) 2002,2003,2013 OPeNDAP, Inc.
// Author: James Gallagher <jgallagher@opendap.org>
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.

#include "config.h"

#include <cppunit/TextTestRunner.h>
#include <cppunit/extensions/TestFactoryRegistry.h>
#include <cppunit/extensions/HelperMacros.h>

//#define DODS_DEBUG

#include "D4Attributes.h"
#include "XMLWriter.h"
#include "debug.h"

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



using namespace CppUnit;
using namespace std;
using namespace libdap;




class D4AttributesTest: public TestFixture {
private:
    XMLWriter *xml;
    D4Attributes *attrs;

    D4Attribute a, a2, a3, a4, a5, c, c2;

public:
    D4AttributesTest()
    {
    }

    ~D4AttributesTest()
    {
    }

    void setUp()
    {
        attrs = new D4Attributes;
        xml = new XMLWriter;

        a.set_name("first");
        a.set_type(attr_byte_c);
        a.add_value("1");
        a.add_value("2");

        a2.set_name("second");
        a2.set_type(attr_int32_c);
        a2.add_value("10000");

        c.set_name("container_1");
        c.set_type(attr_container_c);

        a3.set_name("color");
        a3.set_type(attr_str_c);
        vector<string> colors;
        colors.push_back("red");
        colors.push_back("blue");
        colors.push_back("green");
        a3.add_value_vector(colors);

        a4 = a2;
        a4.set_name("control");

        c.attributes()->add_attribute(&a3);

        c2.set_name("container_2");
        c2.set_type(attr_container_c);

        c2.attributes()->add_attribute(&a4);
        c2.attributes()->add_attribute(&c);
    }

    void tearDown()
    {
        delete xml;
        delete attrs;
    }

    void test_type_to_string()
    {
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_null_c) == "null");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_byte_c) == "Byte");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_int16_c) == "Int16");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_uint16_c) == "UInt16");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_int32_c) == "Int32");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_uint32_c) == "UInt32");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_float32_c) == "Float32");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_float64_c) == "Float64");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_str_c) == "String");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_url_c) == "Url");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_int8_c) == "Int8");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_uint8_c) == "UInt8");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_int64_c) == "Int64");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_uint64_c) == "UInt64");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_enum_c) == "Enum");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_opaque_c) == "Opaque");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_container_c) == "Container");
        CPPUNIT_ASSERT(D4AttributeTypeToString(attr_otherxml_c) == "OtherXML");
    }

    void test_string_to_type()
    {
        CPPUNIT_ASSERT(StringToD4AttributeType("null") == attr_null_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Byte") == attr_byte_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Int16") == attr_int16_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("UInt16") == attr_uint16_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Int32") == attr_int32_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("UInt32") == attr_uint32_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Float32") == attr_float32_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Float64") == attr_float64_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("String") == attr_str_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Url") == attr_url_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Int8") == attr_int8_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("UInt8") == attr_uint8_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Int64") == attr_int64_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("UInt64") == attr_uint64_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Enum") == attr_enum_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Opaque") == attr_opaque_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Container") == attr_container_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("OtherXML") == attr_otherxml_c);
        CPPUNIT_ASSERT(StringToD4AttributeType("Punk") == attr_null_c);
    }

    void test_attr_type()
    {
        //attrs->get_dap2_AttrType(attr_null_c);
    }

    // An empty D4Dimensions object prints nothing; the XMLWriter class adds
    // a xml doc preface.
    void test_print_empty()
    {
        attrs->print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_empty.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_print_single_attribute()
    {
        a.print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_values_1.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);

    }

    void test_print_1()
    {
        attrs->add_attribute(&a);

        attrs->print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_1.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_print_2()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);

        attrs->print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_2.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_print_3()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        attrs->print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_3.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_dump()
    {
        attrs->add_attribute(&c2);
        ostringstream sof;        
        attrs->dump(sof);
        CPPUNIT_ASSERT(sof.str().find("<Attribute name=\"container_2\" type=\"Container\">") != string::npos);
    }

    void test_2_dump()
    {
        ostringstream sof;        
        a.dump(sof);
        CPPUNIT_ASSERT(sof.str().find("<Attribute name=\"first\" type=\"Byte\">") != string::npos);
    }

    void test_print_assignment()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);

        D4Attributes lhs = *attrs;

        lhs.print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_assignment.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_print_assignment_2()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attributes lhs = *attrs;

        lhs.print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_assignment_2.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_print_copy_ctor()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);

        D4Attributes lhs(*attrs);

        lhs.print_dap4(*xml);
        string doc = xml->get_doc();
        string baseline = read_test_baseline(string(TEST_SRC_DIR) + "/D4-xml/D4Attributes_copy_ctor.xml");
        DBG(cerr << "D4Attributes: " << doc << endl);
        CPPUNIT_ASSERT(doc == baseline);
    }

    void test_find()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *find_result = attrs->find("color");

        // 'color' is in the container 'container' which is the third
        // attribute
        D4Attribute *baseline = *(attrs->attribute_begin() + 2);
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_container_c);
        D4Attributes *local_attrs = baseline->attributes();

        // it is the first attribute in that container
        baseline = *(local_attrs->attribute_begin());
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_str_c);
        CPPUNIT_ASSERT(baseline->name() == "color");

        // We get a pointer to actual container, not a copy
        CPPUNIT_ASSERT(baseline == find_result);
    }

    void test_get()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *get_result = attrs->get("container_1.color");

        // 'color' is in the container 'container_1' which is the third
        // attribute
        D4Attribute *baseline = *(attrs->attribute_begin() + 2);
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_container_c);
        D4Attributes *local_attrs = baseline->attributes();

        // it is the first attribute in that container
        baseline = *(local_attrs->attribute_begin());
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_str_c);
        CPPUNIT_ASSERT(baseline->name() == "color");

        // We get a pointer to actual container, not a copy
        CPPUNIT_ASSERT(baseline == get_result);
    }

    void test_get2()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *get_result = attrs->get("container_2.container_1.color");

        // 'color' is (also) in the container 'container_1' which is in
        // the container 'container_2' which is the fourth attribute
        D4Attribute *baseline = *(attrs->attribute_begin() + 3);
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_container_c);
        CPPUNIT_ASSERT(baseline->name() == "container_2");
        D4Attributes *local_attrs = baseline->attributes();

        // 'container_1 is the second attribute in 'container_2'
        baseline = *(local_attrs->attribute_begin() + 1);
        CPPUNIT_ASSERT(baseline);
        CPPUNIT_ASSERT(baseline->type() == attr_container_c);
        CPPUNIT_ASSERT(baseline->name() == "container_1");
        local_attrs = baseline->attributes();

        // finally, 'color' is the first attribute in 'container_1'
        baseline = *(local_attrs->attribute_begin());
        CPPUNIT_ASSERT(baseline->type() == attr_str_c);
        CPPUNIT_ASSERT(baseline->name() == "color");

        // We get a pointer to actual container, not a copy
        CPPUNIT_ASSERT(baseline == get_result);
    }

    // test erasing an attribute
    void test_erase_1()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *first = attrs->get("first");
        CPPUNIT_ASSERT_MESSAGE("The attribute should be present before calling erase()", first != nullptr);
        // sanity check
        CPPUNIT_ASSERT(first->type() == attr_byte_c);
        CPPUNIT_ASSERT(first->name() == "first");

        attrs->erase("first");

        first = attrs->get("first");
        CPPUNIT_ASSERT_MESSAGE("The attribute should not be present after calling erase()", first == nullptr);
    }

    // test erasing an attribute in a container
    void test_erase_2()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *color = attrs->get("container_1.color");
        CPPUNIT_ASSERT_MESSAGE("The attribute should be present before calling erase()", color != nullptr);
        // sanity check
        CPPUNIT_ASSERT(color->type() == attr_str_c);
        CPPUNIT_ASSERT(color->name() == "color");

        attrs->erase("container_1.color");

        color = attrs->get("container_1.color");
        // attrs->dump(cerr), attrs <-- Trick to get a return value for the ternary op. jhrg 2/16/22
        DBG(cerr << "after erase: attrs: " << (attrs != nullptr ? attrs->dump(cerr), attrs: 0) << endl);
        CPPUNIT_ASSERT_MESSAGE("The attribute should not be present after calling erase()", color == nullptr);
    }

    // test erasing a container
    void test_erase_3()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *color = attrs->get("container_1");
        CPPUNIT_ASSERT_MESSAGE("The attribute should be present before calling erase()", color != nullptr);
        // sanity check
        CPPUNIT_ASSERT(color->type() == attr_container_c);
        CPPUNIT_ASSERT(color->name() == "container_1");

        attrs->erase("container_1");

        color = attrs->get("container_1");
        // attrs->dump(cerr), attrs <-- Trick to get a return value for the ternary op. jhrg 2/16/22
        DBG(cerr << "after erase: attrs: " << (attrs != nullptr ? attrs->dump(cerr), attrs: 0) << endl);
        CPPUNIT_ASSERT_MESSAGE("The attribute should not be present after calling erase()", color == nullptr);
    }

    // test erasing a container, using erase_named_attribute() and instead of erase()
    void test_erase_4()
    {
        attrs->add_attribute(&a);
        attrs->add_attribute(&a2);
        attrs->add_attribute(&c);
        attrs->add_attribute(&c2);

        D4Attribute *color = attrs->get("container_1");
        CPPUNIT_ASSERT_MESSAGE("The attribute should be present before calling erase()", color != nullptr);
        // sanity check
        CPPUNIT_ASSERT(color->type() == attr_container_c);
        CPPUNIT_ASSERT(color->name() == "container_1");

        attrs->erase_named_attribute("container_1");

        color = attrs->get("container_1");
        // attrs->dump(cerr), attrs <-- Trick to get a return value for the ternary op. jhrg 2/16/22
        DBG(cerr << "after erase: attrs: " << (attrs != nullptr ? attrs->dump(cerr), attrs: 0) << endl);
        CPPUNIT_ASSERT_MESSAGE("The attribute should not be present after calling erase()", color == nullptr);
    }

    CPPUNIT_TEST_SUITE (D4AttributesTest);

    CPPUNIT_TEST (test_type_to_string);
    
    CPPUNIT_TEST (test_print_empty);
    CPPUNIT_TEST (test_print_single_attribute);
    CPPUNIT_TEST (test_print_1);
    CPPUNIT_TEST (test_print_2);
    CPPUNIT_TEST (test_print_3);
    CPPUNIT_TEST (test_dump);
    CPPUNIT_TEST (test_2_dump);

    CPPUNIT_TEST (test_print_assignment);
    CPPUNIT_TEST (test_print_assignment_2);

    CPPUNIT_TEST (test_print_copy_ctor);

    CPPUNIT_TEST (test_find);
    CPPUNIT_TEST (test_get);
    CPPUNIT_TEST (test_get2);

    CPPUNIT_TEST (test_erase_1);
    CPPUNIT_TEST (test_erase_2);
    CPPUNIT_TEST (test_erase_3);
    CPPUNIT_TEST (test_erase_4);

    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION (D4AttributesTest);

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