File: util_mitTest.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 (232 lines) | stat: -rw-r--r-- 11,271 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
// -*- 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 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.

// Tests for the util functions in util.cc and escaping.cc

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

#include <cstring>
#include <string>

#include "util_mit.h"

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

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

class util_mitTest: public TestFixture {
private:

public:
    util_mitTest() = default;
    ~util_mitTest() = default;

    CPPUNIT_TEST_SUITE(util_mitTest);

    CPPUNIT_TEST(test_parse_time_1);
    CPPUNIT_TEST(test_parse_time_2);
    CPPUNIT_TEST(test_parse_time_3);
    CPPUNIT_TEST(test_parse_time_4);
    CPPUNIT_TEST(test_parse_time_5);
    CPPUNIT_TEST(test_parse_time_6);
    CPPUNIT_TEST(test_parse_time_7);
    CPPUNIT_TEST(test_parse_time_8);
    CPPUNIT_TEST(test_parse_time_9);
    CPPUNIT_TEST(test_parse_time_10);
    CPPUNIT_TEST(test_parse_time_11);
    CPPUNIT_TEST(test_parse_time_12);

    CPPUNIT_TEST(test_date_time_str_1);
    CPPUNIT_TEST(test_date_time_str_2);
    CPPUNIT_TEST(test_date_time_str_3);
    CPPUNIT_TEST(test_date_time_str_4);
    CPPUNIT_TEST(test_date_time_str_5);

    CPPUNIT_TEST_SUITE_END();

    void test_date_time_str_1() {
        time_t t = 726974999;
        DBG(cerr << "date_time_str(" << t << "): " << date_time_str(&t) << endl);
        CPPUNIT_ASSERT_MESSAGE("date_time_str(726974999) should return a string", date_time_str(&t) == "Thu, 14 Jan 1993 01:29:59 GMT");
    }

    void test_date_time_str_2() {
        time_t t = 0;
        DBG(cerr << "date_time_str(" << t << "): " << date_time_str(&t) << endl);
        CPPUNIT_ASSERT_MESSAGE("date_time_str(726974999) should return a string", date_time_str(&t) == "Thu, 01 Jan 1970 00:00:00 GMT");
    }

    // Tricky, in localtime, 0 it before 1 Jan 1970 (Wed, 31 Dec 1969 17:00:00), but we don't test
    // the value since this test has to run in any time zone.
    void test_date_time_str_3() {
        time_t t = 0;
        DBG(cerr << "date_time_str(" << t << ", true): " << date_time_str(&t, true) << endl);
        // No 'GMT' because 'true' == localtime. If we're in us mountain time zone, look at the
        // returned value, else, test that there's no GMT on the end
        CPPUNIT_ASSERT_MESSAGE("date_time_str(0, true) should return a string",
                               date_time_str(&t, true).find("GMT") == string::npos);
    }

    void test_date_time_str_4() {
        DBG(cerr << "date_time_str(nullptr): " << date_time_str(nullptr) << endl);
        CPPUNIT_ASSERT_MESSAGE("date_time_str(726974999) should return a string", date_time_str(nullptr) == "");
    }

    void test_date_time_str_5() {
        time_t t = 0x7FFFFFFF;  // unix time is unsigned
        DBG(cerr << "date_time_str(" << t << "): " << date_time_str(&t) << endl); // 2147183648Fri Jan 15 2038 15:54:08 GMT+0000
        CPPUNIT_ASSERT_MESSAGE("date_time_str(726974999) should return a string", date_time_str(&t) == "Tue, 19 Jan 2038 03:14:07 GMT");
    }

    void test_parse_time_1() {
        CPPUNIT_ASSERT_MESSAGE("parse_time(nullptr) should return zero.", parse_time(nullptr) == 0);
        CPPUNIT_ASSERT_MESSAGE("parse_time(nullptr, false) should return zero.", parse_time(nullptr, false) == 0);
    }

    // Thursday, 10-Jun-93 01:29:59 GMT, Thu, 10 Jan 1993 01:29:59 GMT == 726974999
    // Thu Jan 01 1970 00:00:01 GMT == 1

    void test_parse_time_2() {
        const char *time = "Thursday, 14-Jan-93 01:29:59 GMT";
        const unsigned int time_val = 726974999; // see https://www.unixtimestamp.com/
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thursday, 14-Jan-93 01:29:59 GMT) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thursday, 14-Jan-93 01:29:59 GMT) should return a time.",
                               parse_time(time, false) == time_val);
    }

    void test_parse_time_3() {
        // The leading comma is needed to use the first block of conversion code.
        const char *time = ",14-Jan-93 01:29:59";
        const unsigned int time_val = 726974999; // see https://www.unixtimestamp.com/
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14-Jan-93 01:29:59) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14-Jan-93 01:29:59) should return a time.",
                               parse_time(time, false) == time_val);
    }

    void test_parse_time_4() {
        const char *time = ",14-Jan-9301:29:59";
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14-Jan-9301:29:59) should return 0 (<18 chars).",
                               parse_time(time) == 0);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14-Jan-9301:29:59) should return 0 (<18 chars).",
                               parse_time(time, false) == 0);
    }

    void test_parse_time_5() {
        const char *time = "Thu, 14 Jan 1993 01:29:59 GMT";
        const unsigned int time_val = 726974999;
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return a time.",
                               parse_time(time, false) == time_val);
    }

    void test_parse_time_6() {
        // The leading comma is needed to use the first block of conversion code.
        const char *time = ",14 Jan 1993 01:29:59";
        const unsigned int time_val = 726974999;
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14 Jan 1993 01:29:59) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14 Jan 1993 01:29:59) should return a time.",
                               parse_time(time, false) == time_val);
    }

    void test_parse_time_7() {
        const char *time = ",14 Jan 199301:29:59";
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14 Jan 199301:29:59) should return 0 (<20 chars).",
                               parse_time(time) == 0);
        CPPUNIT_ASSERT_MESSAGE("parse_time(14 Jan 199301:29:59) should return 0 (<20 chars).",
                               parse_time(time, false) == 0);
    }

    // YYYY.MM.DDThh:mmStzWkd
    void test_parse_time_8() {
        const char *time = "1993.01.14T01:29:59+00:00"; // cf. https://en.wikipedia.org/wiki/ISO_8601
        const unsigned int time_val = 726974999;
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return 0 (length < 21).",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return 0 (length < 21).",
                               parse_time(time, false) == time_val);
    }

    void test_parse_time_9() {
        const char *time = "1993.01.14T01:29:59";
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return 0 (length < 21).",
                               parse_time(time) == 0);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thu, 14 Jan 1993 01:29:59 GMT) should return 0 (length < 21).",
                               parse_time(time, false) == 0);
    }

    void test_parse_time_10() {
        const char *time = "Thursday, 14-Jan-93 24:29:59 GMT";  // 24 is not a valid hour
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(<hour value too large>) should return 0.",
                               parse_time(time) == 0);
        CPPUNIT_ASSERT_MESSAGE("parse_time(<hour value too large>) should return 0.",
                               parse_time(time, false) == 0);
    }

    // Mon 00 00:00:00 0000 GMT
    void test_parse_time_11() {
        const char *time = "Jan 14 01:29:59 1993 GMT";
        const unsigned int time_val = 726974999;
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thursday Jan 14 01:29:59 1993 GMT) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Thursday Jan 14 01:29:59 1993 GMT) should return a time.",
                               parse_time(time, false) == time_val);
    }

    // Mon 00 00:00:00 0000 GMT
    void test_parse_time_12() {
        const char *time = "Jan 14 01:29:59 2022 GMT";
        const unsigned int time_val = 1642123799;
        DBG(cerr << "parse_time(" << time << "): " << parse_time(time) << ", string length: " << strnlen(time, 64) << endl);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Jan 14 01:29:59 2022 GMT) should return a time.",
                               parse_time(time) == time_val);
        CPPUNIT_ASSERT_MESSAGE("parse_time(Jan 14 01:29:59 2022 GMT) should return a time.",
                               parse_time(time, false) == time_val);
    }
};

CPPUNIT_TEST_SUITE_REGISTRATION(util_mitTest);

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