1 // -*- mode: c++; c-basic-offset:4 -*-
2 
3 // This file is part of libdap, A C++ implementation of the OPeNDAP Data
4 // Access Protocol.
5 
6 // Copyright (c) 2002,2003 OPeNDAP, Inc.
7 // Author: James Gallagher <jgallagher@opendap.org>
8 //
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
13 //
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 // Lesser General Public License for more details.
18 //
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22 //
23 // You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
24 
25 #include "config.h"
26 
27 #include <cppunit/TextTestRunner.h>
28 #include <cppunit/extensions/TestFactoryRegistry.h>
29 #include <cppunit/extensions/HelperMacros.h>
30 
31 #include <sstream>
32 #include <string.h>
33 
34 #include "Byte.h"
35 #include "Int8.h"
36 #include "Int16.h"
37 #include "UInt16.h"
38 #include "Int32.h"
39 #include "UInt32.h"
40 #include "Int64.h"
41 #include "UInt64.h"
42 #include "Float32.h"
43 #include "Float64.h"
44 #include "Str.h"
45 #include "Url.h"
46 #include "Array.h"
47 #include "Structure.h"
48 #include "Sequence.h"
49 #include "Grid.h"
50 #include "crc.h"
51 
52 #include "DDS.h"
53 
54 #include "GNURegex.h"
55 #include "GetOpt.h"
56 #include "util.h"
57 #include "debug.h"
58 #include "ce_expr.tab.hh"
59 
60 #include "testFile.h"
61 #include "test_config.h"
62 
63 static bool debug = false;
64 
65 #undef DBG
66 #define DBG(x) do { if (debug) {x;} } while(false)
67 
68 using namespace CppUnit;
69 using namespace std;
70 
71 namespace libdap {
72 
73 class UInt64Test: public TestFixture {
74 private:
75     UInt64 *i1, *i2;
76     char a[1024];
77 
78 public:
UInt64Test()79     UInt64Test() : i1(0), i2(0)
80     {
81     }
~UInt64Test()82     ~UInt64Test()
83     {
84     }
85 
setUp()86     void setUp()
87     {
88         i1 = new UInt64("a", "b");
89         i2 = new UInt64("e");
90     }
91 
tearDown()92     void tearDown()
93     {
94         delete i1;
95         delete i2;
96     }
97 
98     CPPUNIT_TEST_SUITE(UInt64Test);
99 
100     CPPUNIT_TEST(cons_UInt64_test);
101     CPPUNIT_TEST(checksum_test);
102     // CPPUNIT_TEST(val2buf_test);
103     // CPPUNIT_TEST(buf2val_test);
104     CPPUNIT_TEST(set_value_test);
105     CPPUNIT_TEST(equals_test);
106     CPPUNIT_TEST(type_compare_test);
107     CPPUNIT_TEST(ops_exception_1_test);
108     CPPUNIT_TEST(ops_exception_2_test);
109     CPPUNIT_TEST(dump_test);
110     // CPPUNIT_TEST(print_test);
111     CPPUNIT_TEST(check_types);
112 
113     CPPUNIT_TEST_SUITE_END();
114 
cons_UInt64_test()115     void cons_UInt64_test()
116     {
117         CPPUNIT_ASSERT(i1->value() == 0 && i1->dataset() == "b" && i1->name() == "a" &&
118                        i1->type() == dods_uint64_c);
119         CPPUNIT_ASSERT(i2->value() == 0);
120     }
121 
checksum_test()122     void checksum_test()
123     {
124         Crc32 cs;
125         i2->compute_checksum(cs);
126     }
127 
128     // void val2buf_test()
129     // {
130     //     int i = 42;
131     //     i2->val2buf(&i, true);
132     //     CPPUNIT_ASSERT(i2->value() == 42);
133     //     CPPUNIT_ASSERT_THROW(i2->val2buf(NULL, true), InternalErr);
134     // }
135 
136     // void buf2val_test()
137     // {
138     //     int i = 42;
139     //     void *v = &i;
140     //     void *v2 = NULL;
141     //     CPPUNIT_ASSERT(i2->set_value(0));
142     //     CPPUNIT_ASSERT(i2->buf2val(&v) == 4 && i == 0);
143     //     CPPUNIT_ASSERT_THROW(i2->buf2val(NULL), InternalErr);
144     //     CPPUNIT_ASSERT(i2->buf2val(&v2) == 4 && *(int *)v2 == 0);
145     // }
146 
set_value_test()147     void set_value_test()
148     {
149         CPPUNIT_ASSERT(i2->set_value(42) && i2->value() == 42);
150     }
151 
equals_test()152     void equals_test()
153     {
154         UInt64 i3 = UInt64("a", "b");
155         UInt64 i4 = UInt64("e");
156         CPPUNIT_ASSERT(i4.set_value(42) && i4.value() == 42);
157         i3 = i4;
158         CPPUNIT_ASSERT(i3.value() == 42);
159         i3 = i3;
160     }
161 
type_compare_test()162     void type_compare_test()
163     {
164         Byte b1 = Byte("a");
165         Int8 i8 = Int8("a");
166         Int16 i16 = Int16("a");
167         UInt16 ui16 = UInt16("a");
168         Int32 i32 = Int32("a", "b");
169         UInt32 ui32 = UInt32("a", "b");
170         Int64 i64 = Int64("a", "b");
171         UInt64 ui64 = UInt64("a", "b");
172         Float32 f32 = Float32("a");
173         Float64 f64 = Float64("a");
174         Url url = Url("a");
175         Str str = Str("a");
176         Array array = Array("a", &i16, true);
177 
178         b1.set_value(42);
179         i8.set_value(42);
180         i16.set_value(42);
181         ui16.set_value(42);
182         i32.set_value(42);
183         ui32.set_value(42);
184         i64.set_value(42);
185         ui64.set_value(42);
186         f32.set_value(42);
187         f64.set_value(42);
188         CPPUNIT_ASSERT(ui64.value() == 42);
189         CPPUNIT_ASSERT(ui64.ops(&b1, SCAN_EQUAL));
190         CPPUNIT_ASSERT(ui64.d4_ops(&b1, SCAN_EQUAL));
191         CPPUNIT_ASSERT(ui64.d4_ops(&i8, SCAN_EQUAL));
192         CPPUNIT_ASSERT(ui64.d4_ops(&i16, SCAN_EQUAL));
193         CPPUNIT_ASSERT(ui64.d4_ops(&ui16, SCAN_EQUAL));
194         CPPUNIT_ASSERT(ui64.d4_ops(&i32, SCAN_EQUAL));
195         CPPUNIT_ASSERT(ui64.d4_ops(&ui32, SCAN_EQUAL));
196         CPPUNIT_ASSERT(ui64.d4_ops(&i64, SCAN_EQUAL));
197         CPPUNIT_ASSERT(ui64.d4_ops(&ui64, SCAN_EQUAL));
198         CPPUNIT_ASSERT(ui64.d4_ops(&f32, SCAN_EQUAL));
199         CPPUNIT_ASSERT(ui64.d4_ops(&f64, SCAN_EQUAL));
200 
201         // CPPUNIT_ASSERT_THROW(ui64.d4_ops(&url, SCAN_EQUAL), Error);
202         // CPPUNIT_ASSERT_THROW(ui64.d4_ops(&str, SCAN_EQUAL), Error);
203         // CPPUNIT_ASSERT_THROW(ui64.d4_ops(&array, SCAN_EQUAL), Error);
204         CPPUNIT_ASSERT(!ui64.d4_ops(&url, SCAN_EQUAL));
205         CPPUNIT_ASSERT(!ui64.d4_ops(&str, SCAN_EQUAL));
206         CPPUNIT_ASSERT(!ui64.d4_ops(&array, SCAN_EQUAL));
207         CPPUNIT_ASSERT_THROW(ui64.ops(0, SCAN_EQUAL), Error);
208     }
209 
ops_exception_1_test()210     void ops_exception_1_test()
211     {
212         Byte b1 = Byte("a");
213         UInt64 ui64 = UInt64("a", "b");
214         b1.set_read_p(false);
215         CPPUNIT_ASSERT_THROW(ui64.ops(&b1, SCAN_EQUAL), InternalErr);
216     }
217 
ops_exception_2_test()218     void ops_exception_2_test()
219     {
220         Byte b1 = Byte("a");
221         UInt64 ui64 = UInt64("a", "b");
222         ui64.set_read_p(false);
223         CPPUNIT_ASSERT_THROW(ui64.ops(&b1, SCAN_EQUAL), InternalErr);
224     }
225 
dump_test()226     void dump_test()
227     {
228         ofstream ofs("UInt64Test_dump.output", ios::trunc);
229         i1->set_value(21);
230         i1->dump(ofs);
231         ofs.close();
232         ifstream ifs("UInt64Test_dump.output");
233         while(!ifs.eof())
234             ifs >> a;
235         ifs.close();
236         CPPUNIT_ASSERT(!strcmp(a, "21"));
237     }
238 
239     // void print_test()
240     // {
241     //     FILE *fp;
242     //     CPPUNIT_ASSERT(fp = fopen("UInt64Test.output", "w"));
243     //     i1->set_value(22);
244     //     i1->print_val(fp, " ", true);
245     //     fclose(fp);
246     //     ifstream ifs("UInt64Test.output");
247     //     while(!ifs.eof())
248     //         ifs >> a;
249     //     ifs.close();
250     //     CPPUNIT_ASSERT(!strcmp(a, "22;"));
251     // }
252 
check_types()253     void check_types()
254     {
255         Byte *b1 = new Byte("b");
256         b1->set_value(14);
257         i1->set_value(14);
258 //        CPPUNIT_ASSERT(b1 == i1);
259         delete b1;
260     }
261 
262 };
263 
264 CPPUNIT_TEST_SUITE_REGISTRATION(UInt64Test);
265 
266 } // namespace libdap
267 
main(int argc,char * argv[])268 int main(int argc, char *argv[])
269 {
270     GetOpt getopt(argc, argv, "dh");
271     int option_char;
272 
273     while ((option_char = getopt()) != -1)
274         switch (option_char) {
275         case 'd':
276             debug = 1;  // debug is a static global
277             break;
278 
279         case 'h': {     // help - show test names
280             cerr << "Usage: UInt64Test has the following tests:" << endl;
281             const std::vector<Test*> &tests = libdap::UInt64Test::suite()->getTests();
282             unsigned int prefix_len = libdap::UInt64Test::suite()->getName().append("::").length();
283             for (std::vector<Test*>::const_iterator i = tests.begin(), e = tests.end(); i != e; ++i) {
284                 cerr << (*i)->getName().replace(0, prefix_len, "") << endl;
285             }
286             return 1;
287             break;
288         }
289 
290         default:
291             break;
292         }
293 
294     CppUnit::TextTestRunner runner;
295     runner.addTest(CppUnit::TestFactoryRegistry::getRegistry().makeTest());
296 
297     bool wasSuccessful = true;
298     string test = "";
299     int i = getopt.optind;
300     if (i == argc) {
301         // run them all
302         wasSuccessful = runner.run("");
303     }
304     else {
305         for (; i < argc; ++i) {
306             if (debug) cerr << "Running " << argv[i] << endl;
307             test = libdap::UInt64Test::suite()->getName().append("::").append(argv[i]);
308             wasSuccessful = wasSuccessful && runner.run(test);
309         }
310     }
311 
312     return wasSuccessful ? 0 : 1;
313 }
314 
315