File: D4UnMarshallerTest.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 (346 lines) | stat: -rw-r--r-- 10,894 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
// -*- mode: c++; c-basic-offset:4 -*-

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

// Copyright (c) 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., 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/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>

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <fcntl.h>
#include <stdint.h>

#include <iostream>
#include <fstream>
#include <iomanip>
#include <cstring>

#include "D4StreamUnMarshaller.h"

#include "Type.h"


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





#if WORDS_BIGENDIAN
const static string path = string(TEST_SRC_DIR) + "/D4-marshaller/big-endian";
#else
const static string path = string(TEST_SRC_DIR) + "/D4-marshaller/little-endian";
#endif

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

class D4UnMarshallerTest: public CppUnit::TestFixture {

    CPPUNIT_TEST_SUITE (D4UnMarshallerTest);

    CPPUNIT_TEST (test_scalars);
    CPPUNIT_TEST (test_real_scalars);
    CPPUNIT_TEST (test_str);
    CPPUNIT_TEST (test_opaque);
    CPPUNIT_TEST (test_vector);

    CPPUNIT_TEST_SUITE_END( );

    static inline bool is_host_big_endian()
    {
#ifdef COMPUTE_ENDIAN_AT_RUNTIME

        dods_int16 i = 0x0100;
        char *c = reinterpret_cast<char*>(&i);
        return *c;

#else

#ifdef __BIG_ENDIAN__
        return true;
#else
        return false;
#endif

#endif
    }

public:
    D4UnMarshallerTest()
    {
    }

    void setUp()
    {
    }

    void tearDown()
    {
    }

    void test_scalars()
    {
        fstream in;
        in.exceptions(ostream::failbit | ostream::badbit);

        // computes checksums and writes data
        try {
            string file = path + "/test_scalars_1_bin.dat";
            DBG(cerr << "file: " << file << endl);
            in.open(file.c_str(), fstream::binary | fstream::in);

            // Don't use is_host_big_endian() because these tests should
            // never 'twiddle bytes' They are always testing little to little
            // of big to big
            D4StreamUnMarshaller dsm(in, 0 /*is_host_big_endian()*/);

            dods_byte b;
            dsm.get_byte(b);
            CPPUNIT_ASSERT(b == 17);
            string ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            // Same checksum for both big- and little-endian
            CPPUNIT_ASSERT(ck == "b8b2cf7f");

            dods_int16 i1;
            dsm.get_int16(i1);
            CPPUNIT_ASSERT(i1 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            // little-endian || big-endian checksum values
            CPPUNIT_ASSERT(ck == "120031ef" || ck == "2b69320d");

            dods_int32 i2;
            dsm.get_int32(i2);
            CPPUNIT_ASSERT(i2 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "c9e1efe6" || ck == "4bf4ffee");

            dods_int64 i3;
            dsm.get_int64(i3);
            CPPUNIT_ASSERT(i3 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "d533eedc" || ck == "0f92ff9b");

            dods_uint16 ui1;
            dsm.get_uint16(ui1);
            CPPUNIT_ASSERT(ui1 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "120031ef" || ck == "2b69320d");

            dods_uint32 ui2;
            dsm.get_uint32(ui2);
            CPPUNIT_ASSERT(ui2 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "c9e1efe6" || ck == "4bf4ffee");

            dods_uint64 ui3;
            dsm.get_uint64(ui3);
            CPPUNIT_ASSERT(ui3 == 17);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "d533eedc" || ck == "0f92ff9b");
        }
        catch (Error &e) {
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
        catch (istream::failure &e) {
            cerr << "File error: " << e.what() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
    }

    void test_real_scalars()
    {
        fstream in;
        in.exceptions(ostream::failbit | ostream::badbit);

        // computes checksums and writes data
        try {
            string file = path + "/test_scalars_2_bin.dat";
            in.open(file.c_str(), fstream::binary | fstream::in);
            D4StreamUnMarshaller dsm(in, 0);

            dods_float32 r1;
            dsm.get_float32(r1);
            CPPUNIT_ASSERT(r1 == 17.0);
            string ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "d3c5bc59" || ck == "edcaaa7c");

            dods_float64 r2;
            dsm.get_float64(r2);
            CPPUNIT_ASSERT(r2 == 17.0);
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "d5a3994b" || ck == "42abb362");
        }
        catch (Error &e) {
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
        catch (istream::failure &e) {
            cerr << "File error: " << e.what() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
    }

    void test_str()
    {
        fstream in;
        in.exceptions(ostream::failbit | ostream::badbit);

        // computes checksums and writes data
        try {
            string file = path + "/test_scalars_3_bin.dat";
            in.open(file.c_str(), fstream::binary | fstream::in);
            D4StreamUnMarshaller dsm(in, 0);

            string s;
            dsm.get_str(s);
            CPPUNIT_ASSERT(s == "This is a test string with 40 characters");
            string ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "af117544");

            string u;
            dsm.get_url(u);
            CPPUNIT_ASSERT(u == "http://www.opendap.org/lame/unit/test");
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "41e10081");
        }
        catch (Error &e) {
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
        catch (istream::failure &e) {
            cerr << "File error: " << e.what() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
    }

    void test_opaque()
    {
        fstream in;
        in.exceptions(ostream::failbit | ostream::badbit);

        // computes checksums and writes data
        try {
            string file = path + "/test_opaque_1_bin.dat";
            in.open(file.c_str(), fstream::binary | fstream::in);
            D4StreamUnMarshaller dsm(in, 0);

            char *buf2;
            int64_t len;
            dsm.get_opaque_dap4(&buf2, len);
            CPPUNIT_ASSERT(len == 32768);
            for (int i = 0; i < 32768; ++i)
                CPPUNIT_ASSERT(buf2[i] == i % (1 << 7));
            string ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "199ad7f5");

            delete[] buf2;
        }
        catch (Error &e) {
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
        catch (istream::failure &e) {
            cerr << "File error: " << e.what() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
    }

    void test_vector()
    {
        fstream in;
        in.exceptions(ostream::failbit | ostream::badbit);

        // computes checksums and writes data
        try {
            string file = path + "/test_vector_1_bin.dat";
            in.open(file.c_str(), fstream::binary | fstream::in);
            D4StreamUnMarshaller dsm(in, 0);

            vector<unsigned char> buf1(32768);
            dsm.get_vector(reinterpret_cast<char*>(buf1.data()), 32768);
            for (int i = 0; i < 32768; ++i)
                CPPUNIT_ASSERT(buf1[i] == i % (1 << 7));
            string ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "199ad7f5" || ck == "199ad7f5");

            vector<dods_int32> buf2(32768);
            dsm.get_vector(reinterpret_cast<char*>(buf2.data()), 32768, sizeof(dods_int32));
            for (int i = 0; i < 32768; ++i)
                CPPUNIT_ASSERT(buf2[i] == i % (1 << 9));
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "5c1bf29f" || ck == "8efd2d3d");

            vector<dods_float64> buf3(32768);
            dsm.get_vector_float64(reinterpret_cast<char*>(buf3.data()), 32768);
            for (int i = 0; i < 32768; ++i) {
                if (buf3[i] != i % (1 << 9)) cerr << "buf3[" << i << "]: " << buf3[i] << endl;
                CPPUNIT_ASSERT(buf3[i] == i % (1 << 9));
            }
            ck = dsm.get_checksum_str();
            DBG(cerr << "ck: " << ck << endl);
            CPPUNIT_ASSERT(ck == "aafc2a91" || ck == "7bdf9931");
        }
        catch (Error &e) {
            cerr << "Error: " << e.get_error_message() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
        catch (istream::failure &e) {
            cerr << "File error: " << e.what() << endl;
            CPPUNIT_FAIL("Caught an exception.");
        }
    }

};

CPPUNIT_TEST_SUITE_REGISTRATION (D4UnMarshallerTest);

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