File: DmrToDap2Test.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 (459 lines) | stat: -rw-r--r-- 14,304 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
// -*- mode: c++; c-basic-offset:4 -*-

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

// Copyright (c) 2014 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  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>

#include <sstream>
#include <memory>

#include "Byte.h"
#include "Int16.h"
#include "UInt16.h"
#include "Int32.h"
#include "UInt32.h"
#include "Float32.h"
#include "Float64.h"
#include "Str.h"
#include "Url.h"
#include "Array.h"
#include "Structure.h"
#include "Sequence.h"
#include "Grid.h"

#include "DDS.h"
#include "DMR.h"
#include "XMLWriter.h"
#include "D4BaseTypeFactory.h"
#include "D4ParserSax2.h"

#include "GNURegex.h"

#include "util.h"
#include "mime_util.h"
#include "debug.h"

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



static bool mo_debug = false;

#undef DBG
#undef DBG2
#define DBG(x) do { if (debug) {x;} } while(false)
#define DBG2(x) do { if (mo_debug) {x;} } while(false)

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

static string THE_TESTS_DIR("/dmr-to-dap2-testsuite/");

class DmrToDap2Test: public TestFixture {
private:

public:
    DmrToDap2Test()
    {
    }
    ~DmrToDap2Test()
    {
    }

    void setUp()
    {
    }

    void tearDown()
    {
    }

    bool re_match(Regex &r, const string &s)
    {
        int match = r.match(s.c_str(), s.length());
        DBG2(cerr << "Match: " << match << " should be: " << s.length() << endl);
        return match == static_cast<int>(s.length());
    }

    /**
     * Given the name of a DDS and optional DAS file, build a DMR using the
     * hackery known as transform_to_dap4 and the new DMR ctor.
     *
     * @param dds_file
     * @param attr
     * @return A pointer to the new DMR; caller must delete
     */
    DMR *build_dmr(const string &dmr_file)
    {
        DBG2(cerr << __func__ << "() - BEGIN" << endl);
        DBG2(cerr << __func__ << "() - dmr_file: " << dmr_file << endl);

        try {
            DMR *dmr = new DMR();
            dmr->set_filename(dmr_file);
            dmr->set_name(name_path(dmr_file));
            D4BaseTypeFactory BaseFactory;   // Use the factory for this handler's types
            dmr->set_factory(&BaseFactory);
            D4ParserSax2 parser;
            ifstream in(dmr_file.c_str(), ios::in);
            parser.intern(in, dmr, mo_debug);
            dmr->set_factory(0);
            DBG2(cerr << __func__ << "() - END" << endl);
            return dmr;
        }
        catch (Error &e) {
            CPPUNIT_FAIL(string("Caught Error: ") + e.get_error_message());
        }
        catch (...) {
            CPPUNIT_FAIL(string("CPPUNIT FAIL: OUCH! Caught unknown exception! "));
        }

        return 0;
    }

    void test_template(const string &test_base_name)
    {
        string prefix = string(TEST_SRC_DIR) + THE_TESTS_DIR;

        string dmr_file = prefix + test_base_name + ".dmr";
        string dds_file = prefix + test_base_name + ".dds";
        string das_file = prefix + test_base_name + ".das";

        DBG(cerr << __func__ << "() - BEGIN (test_base: " << test_base_name << ")" << endl);

        try {
            unique_ptr<DMR> dmr(build_dmr(dmr_file));
            CPPUNIT_ASSERT(dmr.get() != 0);
            XMLWriter xml;
            dmr->print_dap4(xml);
            string result_dmr(xml.get_doc());
            string baseline_dmr = read_test_baseline(dmr_file);

            DBG2(cerr << "BASELINE DMR(" << baseline_dmr.size() << " chars): " << dmr_file << endl << baseline_dmr << endl);
            DBG2(cerr << "RESULT DMR(" << result_dmr.size() << " chars): " << endl << result_dmr << endl);

            CPPUNIT_ASSERT(result_dmr == baseline_dmr);

            unique_ptr<DDS> dds(dmr->getDDS());
            ostringstream result_dds;
            dds->print(result_dds);
            string baseline_dds = read_test_baseline(dds_file);

            DBG2(cerr << "BASELINE DDS(" << baseline_dds.size() << " chars): " << dds_file << endl << baseline_dds << endl);
            DBG2(cerr << "RESULT DDS(" << result_dds.str().size() << " chars): " << endl << result_dds.str() << endl);

            CPPUNIT_ASSERT(result_dds.str() == baseline_dds);

            ostringstream result_das;
            dds->print_das(result_das);
            string source_das = read_test_baseline(das_file);

            DBG2(cerr << "BASELINE DAS(" << source_das.size() << " chars): " << das_file << endl << source_das << endl);
            DBG2(cerr << "RESULT DAS(" << result_das.str().size() << " chars): " << endl << result_das.str() << endl);

            CPPUNIT_ASSERT(result_das.str() == source_das);
        }
        catch (Error &e) {
            CPPUNIT_FAIL(string("Caught Error: ") + e.get_error_message());
        }
        catch (CPPUNIT_NS::Exception &e) {
            CPPUNIT_FAIL(string("CPPUNIT FAIL: ") + e.message().details());
        }
        catch (...) {
            CPPUNIT_FAIL(string("CPPUNIT FAIL: OUCH! Caught unknown exception! "));
        }

        DBG(cerr << __func__ << "() - END" << endl);
    }

    // Test a DDS with simple scalar types and no attributes
    void dmr_to_dap2_01()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("test.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    // Test a DDS with simple scalar types and no attributes
    void basic_dmr_to_dap2_0_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_0.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void basic_dmr_to_dap2_0_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_0.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void basic_dmr_to_dap2_1_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_1.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void basic_dmr_to_dap2_2_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void basic_dmr_to_dap2_2_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void basic_dmr_to_dap2_2_2()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.2");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void basic_dmr_to_dap2_2_3()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.3");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void basic_dmr_to_dap2_2_4()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.4");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void basic_dmr_to_dap2_2_5()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_2.5");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void basic_dmr_to_dap2_3_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("DMR_3.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_0_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_0.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_0_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_0.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_2()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.2");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_3()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.3");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_4()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.4");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_1_5()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_1.5");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_2_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_2.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_2_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_2.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_3_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_3.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_3_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_3.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_4_0()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_4.0");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void enum_dmr_to_dap2_4_1()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_4.1");
        DBG(cerr << __func__ << "() - END" << endl);
    }
    void enum_dmr_to_dap2_4_2()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("EnumDMR_4.2");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void dmr_to_grid_01()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("coads_climatology.nc");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void dmr_to_grid_02()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("sst.mnmean.nc.gz");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void dmr_to_grid_03()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("sresa1b_ncar_ccsm3_0_run1_200001.nc");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void dmr_to_grid_04()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("tos_O1_2001-2002.nc");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void dmr_to_grid_05()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("cami_0000-09-01_64x128_L26_c030918.nc");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    void big_airs_metadata()
    {
        DBG(cerr << endl << __func__ << "() - BEGIN" << endl);
        test_template("big_airs_metadata");
        DBG(cerr << __func__ << "() - END" << endl);
    }

    CPPUNIT_TEST_SUITE (DmrToDap2Test);

    // good (as in should be working) tests
    CPPUNIT_TEST(dmr_to_dap2_01);
    CPPUNIT_TEST(basic_dmr_to_dap2_0_0);
    CPPUNIT_TEST(basic_dmr_to_dap2_0_1);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_0);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_1);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_2);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_4);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_3);
    CPPUNIT_TEST(basic_dmr_to_dap2_2_5); // Drops Variables

    CPPUNIT_TEST(enum_dmr_to_dap2_0_0);
    CPPUNIT_TEST(enum_dmr_to_dap2_0_1);
    CPPUNIT_TEST(enum_dmr_to_dap2_1_0);
    CPPUNIT_TEST(enum_dmr_to_dap2_1_1);
    CPPUNIT_TEST(enum_dmr_to_dap2_1_2);
    CPPUNIT_TEST(enum_dmr_to_dap2_1_3);
    CPPUNIT_TEST(enum_dmr_to_dap2_1_4);
    CPPUNIT_TEST(enum_dmr_to_dap2_2_0); // Drops Variables
    CPPUNIT_TEST(enum_dmr_to_dap2_2_1); // Drops Variables
    CPPUNIT_TEST(enum_dmr_to_dap2_3_0); // Drops Variables
    CPPUNIT_TEST(enum_dmr_to_dap2_3_1); // Drops Variables

    CPPUNIT_TEST(enum_dmr_to_dap2_4_0); // Drops Groups
    CPPUNIT_TEST(enum_dmr_to_dap2_4_1); // Drops Groups
    CPPUNIT_TEST(enum_dmr_to_dap2_4_1);
    CPPUNIT_TEST(enum_dmr_to_dap2_4_2);

    CPPUNIT_TEST(dmr_to_grid_01);
    CPPUNIT_TEST(dmr_to_grid_02);
    CPPUNIT_TEST(dmr_to_grid_03);
    CPPUNIT_TEST(dmr_to_grid_04);
    CPPUNIT_TEST(dmr_to_grid_05);

    // bad tests, here then is the woodshed of Testville.

    CPPUNIT_TEST_FAIL(big_airs_metadata); // Expect this test to fail. jhrg 6/17/19 HK-403
    CPPUNIT_TEST_FAIL(enum_dmr_to_dap2_1_5); // Broken: Parser issue with look-ahead

    CPPUNIT_TEST_SUITE_END();
};

CPPUNIT_TEST_SUITE_REGISTRATION(DmrToDap2Test);

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